From 140635eb5f77164630db647aa37f18ca890e4b30 Mon Sep 17 00:00:00 2001 From: Gregory Date: Fri, 26 Apr 2019 13:59:34 +0300 Subject: [PATCH] malloc continue --- inc/ft_malloc.h | 2 +- src/malloc.c | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/inc/ft_malloc.h b/inc/ft_malloc.h index 8ce0f66..4fccf1b 100644 --- a/inc/ft_malloc.h +++ b/inc/ft_malloc.h @@ -7,7 +7,7 @@ typedef struct s_header { struct s_header *next; - size_t size; + size_t units; } t_header; static t_header base; diff --git a/src/malloc.c b/src/malloc.c index 744040d..39fdfc5 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -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); } \ No newline at end of file