-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
executable file
·69 lines (64 loc) · 1.21 KB
/
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
68
69
/*
** malloc.c for malloc in /u/all/locque_d/cu/rendu/proj/malloc
**
** Made by damien locque
** Login <[email protected]>
**
** Started on Mon Jan 31 14:47:38 2011 damien locque
** Last update Fri Feb 18 12:02:30 2011 damien locque
*/
#include <unistd.h>
#include "malloc.h"
static void move_chunk(t_chunk *tmp)
{
if (tmp->prev && tmp->next)
{
(tmp->prev)->next = tmp->next;
(tmp->next)->prev = tmp->prev;
}
else
{
if (!tmp->prev && !tmp->next)
{
free_list = NULL;
end_free = NULL;
}
else if (tmp->prev && !tmp->next)
{
end_free = tmp->prev;
if (end_free)
end_free->next = NULL;
}
else
{
free_list = tmp->next;
if (free_list)
free_list->prev = NULL;
}
}
}
void *find_chunk(size_t size)
{
t_chunk *tmp;
tmp = free_list;
while (tmp)
{
if (tmp->size >= (size + sizeof(*tmp)))
{
move_chunk(tmp);
tmp->next = NULL;
tmp->prev = NULL;
split(tmp, size);
add_by_address_list(tmp, &alloc_list, &end_alloc);
return (tmp + 1);
}
tmp = tmp->next;
}
return (extend_heap(size));
}
void *malloc(size_t size)
{
if (!size || (size + sizeof(*alloc_list)) < size)
return (NULL);
return (find_chunk(size));
}