如何为所有方法使用的LL建立全局头*

时间:2018-02-16 21:34:50

标签: c

我想知道如何使用头指针创建一个链接的结构列表,供其他函数使用。 我正在制作一个个人的malloc,我想制作一个结构memBlock {。isTaken,.size,.next}

我尝试在方法之外做静态和非静态,我不确定我做错了什么。

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;
}

当我运行它时,使用malloc(500)
IsUsed:0,BlkSize:5000
IsUsed:1,BlkSize:500

我的newBlk应该首先指向head,但是当我创建一个tmp时,它会自动设置为新头,我不确定我对指针有什么误解。

谢谢。

0 个答案:

没有答案