-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_free.c
40 lines (35 loc) · 794 Bytes
/
my_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
/*
** EPITECH PROJECT, 2021
** Malloc
** File description:
** Recreate standard allocation functions
*/
#include "memory_block.h"
mem_block_t free_block_fusion(mem_block_t freed_block)
{
if (freed_block->next && freed_block->next->free) {
freed_block->size += freed_block->next->size;
freed_block->next = freed_block->next->next;
}
return freed_block;
}
int is_block(void *ptr)
{
if (ptr < sbrk(0))
return 1;
return 0;
}
mem_block_t get_block_from_ptr(void *ptr)
{
char *adr = ptr;
return ptr = adr - offsetof(struct mem_block_s, data);
}
void free(void *ptr)
{
mem_block_t freed_block = get_block_from_ptr(ptr);
if (is_block(ptr) == 1) {
freed_block->free = 1;
free_block_fusion(freed_block);
}
return;
}