-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
120 lines (104 loc) · 2.21 KB
/
memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
** memory.c for PSU_2016_malloc in /home/morel_j/Documents/epitech/annee_2/semestre_2/unix_memory/PSU_2016_malloc
**
** Made by Antoine Morel
** Login <[email protected]>
**
** Started on Tue Jan 24 16:27:57 2017 Antoine Morel
** Last update Thu Jan 26 17:33:06 2017 Antoine Morel
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "memory.h"
static void *begin = NULL;
static size_t size_last_elem = 0;
pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
/*
* Allocation
*/
void *malloc(size_t size)
{
void *ptr;
if (size > 0xFFFFFFFF)
return (NULL);
if ((begin = sbrk(0)) == (void *) -1)
return (NULL);
pthread_mutex_lock(&mutex);
ptr = malloc_system(begin, size, &size_last_elem);
pthread_mutex_unlock(&mutex);
return (ptr);
}
void *calloc(size_t nmemb, size_t size)
{
void *ptr;
if (nmemb == 0 || size == 0)
return (NULL);
if (begin == NULL)
if ((begin = sbrk(0)) == (void *) -1)
return (NULL);
pthread_mutex_lock(&mutex);
if ((ptr = malloc(nmemb * size)) == NULL)
{
pthread_mutex_unlock(&mutex);
return (NULL);
}
memset(ptr, 0 , nmemb * size);
pthread_mutex_unlock(&mutex);
return (ptr);
}
/*
* Modify
*/
void *realloc(void *ptr, size_t size)
{
t_header *header;
void *n_ptr;
if (size == 0)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
return (malloc(size));
if (begin == NULL)
return (NULL);
pthread_mutex_lock(&mutex);
header = ptr - sizeof(t_header);
n_ptr = realloc_system(header, size);
pthread_mutex_unlock(&mutex);
return (n_ptr);
}
/*
* Release
*/
void free(void *ptr)
{
t_header *header;
if (ptr == NULL || begin == NULL)
return ;
pthread_mutex_lock(&mutex);
header = ptr - sizeof(t_header);
free_system(header, &size_last_elem);
pthread_mutex_unlock(&mutex);
}
/*
* Display
*/
void show_alloc_mem(void)
{
t_header *header;
void *end;
if ((end = sbrk(0)) == (void *) -1)
return ;
printf("break : %p\n", end);
if ((header = begin) == NULL)
return ;
while (header != end)
{
if (header->status == USED)
printf("%p - %p : %zu bytes\n", header + 1, EOS, header->size);
header = EOS;
}
}