Implementation of malloc()




I have implemented memory allocation functions using data structures. These
functions are similar to malloc and free in c programming language. You can use newmalloc and
newfree functions instead of mallo and free.
in my programme, memory is allocated from an array of 50000 bytes. To keep track of
the memory we allocate, we also going to need some extra space from that array.
Newmalloc is implemented using doubly linked list data structure. That means newmalloc
maintains a linked list. When we call newmalloc, it looks for a suitable memory chunk in the array and
return it to the user. When newfree is called, program takes that memory chunk and put that back in to
the list. If the memory chunk that requested is smaller than the free memory we have in the array,
memory is split up and the part of memory is returned to the user.newfree puts that back in the free list.
To keep track of the data we allocate, I had to use data structures.

you can download the full report and the source codes  from here. Run main.c and see the results.


newmalloc.c
void Inz_Malloc();
void *NewMalloc(int s);
void NewFree(void *p);
void Defrag();
void PrintMyMallocFreeList();

static unsigned char raw_memory[50000]; //array of memory
static struct malloc_stc *My_malloc;    //linked list dtructure

struct malloc_stc *temp;
int heap_size;

/* ----------------------------------------------------------------- */
/*  structure for the free list                                      */
/*===================================================================*/
/* ----------------------------------------------------------------- */

struct malloc_stc{
    struct malloc_stc *next;
    struct malloc_stc *prev;
    int size;
    unsigned char *buffer;
} ;

/* ----------------------------------------------------------------- */
/*      initializing the buffer so that there is one, big block      */
/*    containing all of the free storage we can allocate, and         */
/*   initializing  the free list head pointer(My_malloc)             */
/*      to point to this block                                        */
/*===================================================================*/
/* ----------------------------------------------------------------- */

void Inz_Malloc(){

    My_malloc = (struct malloc_stc *)raw_memory;/* raw_memory is a array of characters which mean that each block 
                                        is one byte long . By typecasting , My_malloc will point to 
                                          a block of memory which has enough memory to store the four
                                         attributes of malloc_stc structure .*/
    My_malloc->next = NULL;
    My_malloc->prev = NULL;
    My_malloc->size = 50000 - sizeof(struct malloc_stc);
    My_malloc->buffer = (unsigned char *)(raw_memory + sizeof(struct malloc_stc));
    heap_size=50000 - sizeof(struct malloc_stc);
}

/* ----------------------------------------------------------------- */
/*  newmalloc -> this function allocate the memory. it takes memory  */
/*     chunksfrom the memory array and return it to the use           */
/*===================================================================*/
/* ----------------------------------------------------------------- */

void *NewMalloc(int s){


    if ( s <= 0 ) {
        printf("ERROR\n"); 
        return NULL;
    }

    if ( s > 50000 ) {
        printf("ERROR\n"); 
        return NULL;
    }
    
    temp=My_malloc;

    void *p;
    int z;

if((My_malloc->next == NULL) && (My_malloc->prev == NULL)){

    temp->next=NULL;
    temp->prev=NULL;
    z=temp->size;
    temp->size=s;
    temp->buffer=My_malloc->buffer;
    

    p=temp->buffer;
    
    My_malloc=temp->buffer+s;
    My_malloc->next=NULL;
    My_malloc->prev=NULL;
    My_malloc->size=z - ( s + sizeof(struct malloc_stc) );
    My_malloc->buffer = temp->buffer + s + sizeof(struct malloc_stc);

    
    return p;
}
else{
            struct malloc_stc *check=My_malloc;
            struct malloc_stc *chk;
    
        while(1){
            
            if( check->size >= (s+sizeof(struct malloc_stc)) ){

                chk=check;
                int cs=chk->size;

                if((chk->next != NULL) && (chk->prev == NULL) ){
        
                        temp=check;                
                        temp->buffer=check->buffer;
                        temp->size=s;                    
                        p=temp->buffer;
            
                        My_malloc=temp->buffer+s;                
                        My_malloc->buffer=check+sizeof(struct malloc_stc);
                        My_malloc->size=cs - (s+sizeof(struct malloc_stc));            
                        My_malloc->next=chk->next;
                        My_malloc->next->prev=check;
                        My_malloc->prev=NULL;
                        return p;
                            
                }
    
                if((chk->next == NULL) && (chk->prev != NULL) ){

                        temp=check;
                        temp->buffer=check->buffer;
                        temp->size=s;
                        p=temp->buffer;
            
                        check=temp->buffer+s;
                        check->buffer=check+sizeof(struct malloc_stc);
                        check->size=cs - (s+sizeof(struct malloc_stc));
                        check->next=NULL;
                        check->prev=chk->prev;
                        chk->prev->next=check;
                        return p;
                }

                if((chk->next != NULL) && (chk->prev != NULL)){

                        temp=check;
                        temp->buffer=check->buffer;
                        temp->size=s;
                        p=temp->buffer;

                        check=temp->buffer+s;
                        check->buffer=check+sizeof(struct malloc_stc);
                        check->size=cs - (s+sizeof(struct malloc_stc));
                        check->next=chk->next;
                        check->prev=chk->prev;
                        check->next->prev=check;
                        check->prev->next=check;
                        return p;

                }        
            }
            check=check->next;    
        }
    }
}

/* ----------------------------------------------------------------- */
/* newfree- this function takes the memory chunks back from the user */
/*    and put it back in the free memory list                         */
/*===================================================================*/
/* ----------------------------------------------------------------- */

void NewFree(void *p){
    
    
        if( My_malloc > p){
            
            struct malloc_stc *freetemp;
    
            My_malloc->prev=p-16;
            freetemp=My_malloc;
            My_malloc=p-16;
            
            My_malloc->next=freetemp;
            My_malloc->size=My_malloc->size;
            My_malloc->prev=NULL;
            My_malloc->next->prev=My_malloc;
            
            My_malloc->buffer = My_malloc->buffer;
            
        }

        else{

            struct malloc_stc *freetemp;
            
            freetemp=My_malloc;
        
            struct malloc_stc *frtmp;

            while(1){

                freetemp=freetemp->next;
        
                if(p < freetemp){

                    frtmp=freetemp;
                    freetemp=p-16;
                    freetemp->buffer=p;                    
                    freetemp->next=frtmp;                    
                    freetemp->prev=frtmp->prev;
                    freetemp->prev->next=freetemp;
                    freetemp->next->prev=freetemp;
                    freetemp->size=freetemp->size;
                
                    break;                    
                }
            }                
        }    
}

/* ----------------------------------------------------------------- */
/*                 end of newmalloc.c                                */
/*===================================================================*/
/* ----------------------------------------------------------------- */


SHARE

Harsha Jayamanna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment