c - making custom Malloc, what is wrong here? -
i've been working on little custom worstfit malloc using double-linked list while, , although small thought work. there obvious wrong code?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include "mymal.h" typedef struct node { int size; int status; struct node *next; struct node *previous; } node; node *endnode; node *rootnode; void *worstfit_mall(int size) { node *thenode = sbrk (size + sizeof(thenode)); void *ptr; if (rootnode == null) { thenode->status = 1; thenode->size = size; thenode->previous = thenode; thenode->next = thenode; rootnode = thenode; endnode = thenode; return thenode; } node *worstnode; worstnode = worstfit(size); if (worstnode != null) { thenode->status = 1; thenode->size = size; node *newnode = sbrk((worstnode->size - thenode->size) + sizeof(thenode)); newnode->status = 0; newnode->size = worstnode->size - thenode->size; thenode->next = newnode; thenode->previous = worstnode->previous; newnode->next = worstnode->next; return newnode; } endnode->next = thenode; endnode = thenode; endnode->status = 1; endnode->size = size; ptr = sbrk(size + sizeof(thenode)); return ptr; } void my_free(void *ptr) { node *pointer; pointer = (node*)ptr; pointer->status = 0; if ((pointer->next->status == 0) && (pointer->previous->status == 0)) sbrk(-1 * (pointer->next->size + pointer->size)); else if ((pointer->next->status == 1) && (pointer->previous->status == 0)) sbrk(-1 * (pointer->previous->size + pointer->size)); else if ((pointer->next->status == 0) && ( pointer->next->status == 0)) sbrk(-1 * (pointer->previous->size + pointer->next->size + pointer->size)); else sbrk(-1 * pointer->size); } void *worstfit(int size) { node *thenode = rootnode; node *worstnode; while (thenode != null) { if ((worstnode == null || thenode->size > worstnode->size) && (thenode->size >= size) && (thenode->status == 0)) worstnode = thenode; thenode = thenode->next; } return worstnode; }
here things strike me:
worstfit
not initializeworstnode
null
, tries read while it's still garbage.you create linked list of
node
s, tailnode
'snext
points itself. meanwhileworstfit
expectsnull
sentinel value when iterates on list.worstfit_mall
not initializeendnode
when creatingrootnode
.worstfit_mall
returns pointer allocatednode
, if it's meant substitutablemalloc
, should returning pointer memory caller allowed write to. don't want caller scribble onnode
data.i'd expect
worstfit_mall
return((char*) node) + sizeof *node)
(or more simply,node + 1
) instead of returningnode
directly.my_free
need corresponding, inverse adjustment retrievenode
pointer.void my_free(void *ptr) { node *nodeptr = ptr; nodeptr--; ... }
additionally, it's unclear me why
worstfit_mall
allocates memory viasbrk
when going downworstnode != null
path. isn't point of path find existing memory chunk reuse? furthermore, path callssbrk
twice.finally, appears me
my_free
unconditionally reduces amount of allocated memory, work if you're freeing last thing allocatedsbrk
. if calledworstfit_mall
twice , calledmy_free
on first result? there no pathmy_free
marks memory chunk no-longer-in-useworstfit_mall
can reuse later.
i don't know if there other problems code; there given these types of fundamental issues.
Comments
Post a Comment