-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
83 lines (72 loc) · 1.73 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
** main.c for in /u/all/brenne_t/cu/rendu/proj/malloc
**
** Made by thomas brennetot
** Login <[email protected]>
**
** Started on Mon Feb 9 11:30:26 2009 thomas brennetot
** Last update Sun Mar 29 16:57:13 2009 thomas brennetot
*/
#include "pointh.h"
void *first_malloc(size_t size)
{
t_malloc st_new;
gl_malloc = sbrk(power_sqrt(sizeof(st_new) + size));
if (gl_malloc == FAIL)
return (NULL);
st_new.size_ask = size;
st_new.size_block = (int)sbrk(0) - (int)gl_malloc - sizeof(st_new);
st_new.use = NO;
st_new.next = NULL;
*gl_malloc = st_new;
return (malloc(size));
}
void *find_space(size_t size)
{
t_malloc *st_malloc;
st_malloc = gl_malloc;
while (st_malloc != NULL)
{
if (st_malloc->use == NO)
if (st_malloc->size_block <= size)
{
/* FRAGMENTER LE SEGMENT */
st_malloc->use = YES;
return (st_malloc + 1);
}
st_malloc = st_malloc->next;
}
return (NULL);
}
void *create_space(size_t size)
{
t_malloc *st_malloc;
int total_size;
st_malloc = gl_malloc;
while (st_malloc->next != NULL)
st_malloc = st_malloc->next;
total_size = power_sqrt(size + sizeof(*st_malloc));
if ((st_malloc->next = sbrk(total_size)) == FAIL)
{
st_malloc->next = NULL;
gl_malloc = FAIL;
return (NULL);
}
st_malloc = st_malloc->next;
st_malloc->size_block = (int)sbrk(0) - (int)st_malloc - sizeof(*st_malloc);
st_malloc->size_ask = size;
st_malloc->use = YES;
st_malloc->next = NULL;
return (st_malloc + 1);
}
void *malloc(size_t size)
{
void *ptr;
if (gl_malloc == FAIL)
return (NULL);
if (gl_malloc == NULL)
ptr = first_malloc(size);
else if ((ptr = find_space(size)) == NULL)
ptr = create_space(size);
return (ptr);
}