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 initialize worstnode null , tries read while it's still garbage.

  • you create linked list of nodes, tail node's next points itself. meanwhile worstfit expects null sentinel value when iterates on list.

  • worstfit_mall not initialize endnode when creating rootnode.

  • worstfit_mall returns pointer allocated node, if it's meant substitutable malloc, should returning pointer memory caller allowed write to. don't want caller scribble on node data.

    i'd expect worstfit_mall return ((char*) node) + sizeof *node) (or more simply, node + 1) instead of returning node directly. my_free need corresponding, inverse adjustment retrieve node pointer.

    void my_free(void *ptr) { node *nodeptr = ptr; nodeptr--; ... }

  • additionally, it's unclear me why worstfit_mall allocates memory via sbrk when going down worstnode != null path. isn't point of path find existing memory chunk reuse? furthermore, path calls sbrk twice.

  • finally, appears me my_free unconditionally reduces amount of allocated memory, work if you're freeing last thing allocated sbrk. if called worstfit_mall twice , called my_free on first result? there no path my_free marks memory chunk no-longer-in-use worstfit_mall can reuse later.

i don't know if there other problems code; there given these types of fundamental issues.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -