0

I was wondering how can i make a Linked list of structures with a head pointer to be used by other functions. I am making a somewhat of a personal malloc and i wanted to make a structure memBlock {.isTaken, .size, .next}

I tried doing static outside of methods and non static as well and i am not sure what i am doing wrong.

static memBlkHdr *head;  

/*
    Traverse LL to find best free block, return null if cant fit
    Can make void, but we can discuss later
*/
memBlkHdr* bestFit(memBlkHdr *ptr, int size){


    memBlkHdr *currentptr = ptr;
    memBlkHdr *bestptr = NULL;

    while(currentptr != NULL){
        //skip used blks    
        if((*currentptr).isUsed == 1) 
            continue;
        //skip small blks
        if((*currentptr).blkSize < size) 
            continue;
        //By this point its free and fits, set as best if none before
        if(bestptr == NULL) 
            bestptr = ptr;
        //Check if this is better fit than current best
        if(((*currentptr).blkSize - size) < ((*bestptr).blkSize - size)) 
            bestptr = ptr;  
        currentptr = (*currentptr).next;
    }

    return bestptr;

}

void* mymalloc(unsigned int size, char *file, int line){  

    //local variables
    memBlkHdr *newBlkptr;
    memBlkHdr newBlk;
    //printf("here\n");

    //Check for malloc(0)
    if(size == 0){
        fprintf(stderr, "Err: attempt to malloc(0) in FILE: '%s' on LINE: '%d'\n", file, line);
        return NULL;
    }

    //Check if blk req size is even, if not, add 1 extra byte for formatting
    if((size & 1) == 1)
        ++size;

    /*
    Check if head is NULL(first malloc)
    If yes, initialize, (free, 5000, NULL, point to first memblk of myblock)
    */
    if(head == NULL){
        memBlkHdr newHead = {.isUsed = 0, .blkSize = MAX, .next = NULL, 
        .current = (void*)(&myblock[0])};
        head = &newHead;        
    }

    //Find best place to allocate
    newBlkptr = bestFit(head, size);



    //printf("Best fit returned : %d\n", (*newBlkptr).blkSize);
    //No room
    if(newBlkptr == NULL){
        fprintf(stderr, "Err: Not enough free Memory in FILE: '%s' on LINE: '%d'\n", file, line);
        return NULL;    
    }

    newBlk = *newBlkptr;



    //ALL OF THIS NEEDS EXCESSIVE TESTING
    //if there is more room in the block;
    if(newBlk.blkSize > size){
        //Make new header for leftover space
        //size current-(what we're using)
        //set .next to cur.next 
        //add size to char arr ptr;
    printf("IsUsed: %d, BlkSize: %d\n", (*head).isUsed , (*head).blkSize);

        newBlk.isUsed = 0;
        newBlk.blkSize = newBlk.blkSize - size;

        memBlkHdr tmp;

        tmp.isUsed = 1;
        //Issue here, changes the head size to this. idk how to fix yet. got stuck
        tmp.blkSize = size;

        tmp.next = newBlk.next;
        tmp.current = newBlk.current+(newBlk.blkSize - size); 

        newBlk.next = &tmp;

    printf("IsUsed: %d, BlkSize: %d\n", (*head).isUsed , (*head).blkSize);



    } 
    else {
        //If perfect fit, just set to used
        //its next is defined already
        newBlk.isUsed = 1;
    }


    //returns mem adress as void type, pointing to char array
    //to be matched later. 

    return  newBlk.current;
}

When i run this, with malloc(500)
IsUsed: 0, BlkSize: 5000
IsUsed: 1, BlkSize: 500

my newBlk should have been pointing to head as its first go, but when i make a tmp, it auto sets its as new head and i am not sure what i am misunderstanding about pointers.

Thank you.

dbush
  • 205,898
  • 23
  • 218
  • 273
Slava A.
  • 97
  • 6
  • Would we not get stuck if the conditions were true around ` while(currentptr != NULL){ //skip used blks if((*currentptr).isUsed == 1) continue;` - never updating the currentptr, having it the same every loop? (should the .next be placed earlier?) – O.O. Feb 16 '18 at 21:42
  • Save yourself the trouble and don't deal with shared globals. And `head = &newHead;` is bad news... `newHead` is a stack variable, nothing is ever allocated. – Jeff Mercado Feb 16 '18 at 21:44
  • What would be a better way to do this. I cannot send the head* into method call, so how would i make a pointer that i can use between all other methods? – Slava A. Feb 16 '18 at 22:00

0 Answers0