malloc continue

This commit is contained in:
Gregory 2019-04-26 13:59:34 +03:00
parent 7b7db9ff93
commit 140635eb5f
2 changed files with 17 additions and 6 deletions

View file

@ -7,7 +7,7 @@
typedef struct s_header
{
struct s_header *next;
size_t size;
size_t units;
} t_header;
static t_header base;

View file

@ -8,14 +8,25 @@ t_header *morecore(size_t size)
void *malloc(size_t size)
{
t_header *p;
t_header *prevp;
t_header *curr;
t_header *prev;
size_t nunits;
void *space;
space = NULL;
nunits = (size + sizeof(t_header) - 1) / (sizeof(t_header) + 1);
if ((prevp = freep) == NULL)
if ((prev = freep) == NULL)
{
base.next = freep = prevp = &base;
base.size = 0;
base.next = freep = prev = &base;
base.units = 0;
}
while(!space)
{
curr = prev->next;
if (curr->units >= nunits)
prev->next = curr->next;
prev = curr;
curr = curr->next;
}
return (space);
}