-
Notifications
You must be signed in to change notification settings - Fork 1
/
free.c
45 lines (40 loc) · 854 Bytes
/
free.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
/*
** EPITECH PROJECT, 2019
** PSU_2018_malloc
** File description:
** bttffree
*/
#include "malloc.h"
void delete_pointers(void)
{
header_malloc_t *buffer = start;
while (buffer) {
if (buffer->next == end) {
buffer->next = NULL;
end = buffer;
}
buffer = buffer->next;
}
}
void free(void *ptr)
{
header_malloc_t *header;
void *pbrk;
if (!ptr)
return;
pthread_mutex_lock(&lock);
header = (header_malloc_t*)ptr - 1;
pbrk = sbrk(0);
if ((char*)ptr + header->size == pbrk) {
if (start == end) {
start = NULL;
end = NULL;
} else {
delete_pointers();
}
sbrk((0 - sizeof(header_malloc_t) - header->size));
} else {
header->is_free = true;
}
pthread_mutex_unlock(&lock);
}