malloc continue
This commit is contained in:
parent
7b7db9ff93
commit
140635eb5f
2 changed files with 17 additions and 6 deletions
|
@ -7,7 +7,7 @@
|
||||||
typedef struct s_header
|
typedef struct s_header
|
||||||
{
|
{
|
||||||
struct s_header *next;
|
struct s_header *next;
|
||||||
size_t size;
|
size_t units;
|
||||||
} t_header;
|
} t_header;
|
||||||
|
|
||||||
static t_header base;
|
static t_header base;
|
||||||
|
|
21
src/malloc.c
21
src/malloc.c
|
@ -8,14 +8,25 @@ t_header *morecore(size_t size)
|
||||||
|
|
||||||
void *malloc(size_t size)
|
void *malloc(size_t size)
|
||||||
{
|
{
|
||||||
t_header *p;
|
t_header *curr;
|
||||||
t_header *prevp;
|
t_header *prev;
|
||||||
size_t nunits;
|
size_t nunits;
|
||||||
|
void *space;
|
||||||
|
|
||||||
|
space = NULL;
|
||||||
nunits = (size + sizeof(t_header) - 1) / (sizeof(t_header) + 1);
|
nunits = (size + sizeof(t_header) - 1) / (sizeof(t_header) + 1);
|
||||||
if ((prevp = freep) == NULL)
|
if ((prev = freep) == NULL)
|
||||||
{
|
{
|
||||||
base.next = freep = prevp = &base;
|
base.next = freep = prev = &base;
|
||||||
base.size = 0;
|
base.units = 0;
|
||||||
}
|
}
|
||||||
|
while(!space)
|
||||||
|
{
|
||||||
|
curr = prev->next;
|
||||||
|
if (curr->units >= nunits)
|
||||||
|
prev->next = curr->next;
|
||||||
|
prev = curr;
|
||||||
|
curr = curr->next;
|
||||||
|
}
|
||||||
|
return (space);
|
||||||
}
|
}
|
Loading…
Reference in a new issue