-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_malloc.c
67 lines (58 loc) · 1.58 KB
/
my_malloc.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
/*
** EPITECH PROJECT, 2021
** B-PSU-400-RUN-4-1-malloc-paul.brancieq
** File description:
** my_malloc
*/
#include "include/my_malloc.h"
#include <unistd.h>
my_malloc_t *memory_list = NULL;
my_malloc_t *search_for_space(size_t memorySize)
{
my_malloc_t *tmp = memory_list;
my_malloc_t *res = NULL;
if (memory_list == NULL)
return (NULL);
else
{
while (tmp != NULL)
{
if (tmp->state == FREE && ((tmp->b_offset >=
memorySize && res == NULL) || (res != NULL && tmp->b_offset
>= memorySize && tmp->b_offset < res->b_offset))) {
res = tmp;
}
tmp = tmp->next;
}
return res;
}
}
void *use_space(size_t memorySize, my_malloc_t *space)
{
int oc_rest = space->b_offset - memorySize;
my_malloc_t *rest = create_elem((void *)space + sizeof(my_malloc_t)
+ memorySize, oc_rest, FREE);
space->b_offset = memorySize;
space->state = USED;
if (rest == NULL) {
space->b_offset += oc_rest;
} else if (insert_elem(space, rest))
{
write(2, "Error: in insert", 17);
return (NULL);
}
return (space);
}
void *malloc(size_t memorySize)
{
my_malloc_t *ret;
if (memorySize == 0)
return (NULL);
int bloc_needed_new = (int)(memorySize /(getpagesize() * 2) + 1)
* (getpagesize() * 2);
my_malloc_t *free_space = search_for_space(memorySize);
if (free_space == NULL)
free_space = add_block(memory_list, bloc_needed_new);
ret = use_space(memorySize, free_space);
return (ret->memory);
}