0

一段时间以来,我一直在使用双链表开发一个小的自定义最差 Malloc,虽然这很小,但我认为这会起作用。这段代码有什么明显的问题吗?

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

1 回答 1

1

以下是立即打动我的事情:

  • worstFit当它仍然是垃圾时,它不会初始化并worstNode尝试读取它。NULL

  • 你创建了一个s的链表Node,但 tail总是指向它自己。同时,在迭代列表时需要一个哨兵值。NodenextworstFitNULL

  • worstfit_mallendNode最初创建时不初始化rootNode

  • worstfit_mall返回一个指向已分配的指针Node,但如果它可以替代malloc,它应该返回一个指向允许调用者写入的内存的指针。您不希望调用者在您的Node数据上乱涂乱画。

    我希望worstfit_mall返回((char*) node) + sizeof *node)(或更简单地说,node + 1)而不是node直接返回。 my_free需要进行相应的反向调整来检索Node指针。

    void my_free(void *ptr) { Node *nodePtr = ptr; nodePtr--; ... }

  • 此外,我不清楚为什么在沿着路径worstfit_mall分配内存sbrk时要分配内存。worstNode != NULL这条路径的重点不是找到一个现有的内存块来重用吗?此外,这条路径调用sbrk 了两次

  • 最后,在我看来,my_free无条件地减少了分配的内存量,但这只有在你释放你分配的最后一个东西时才有效sbrk。如果你调用worstfit_mall了两次然后调用my_free了第一个结果怎么办?my_free没有将内存块标记为不再使用的路径,以便worstfit_mall以后可以重用它。

我不知道您的代码是否还有其他问题;我会说,很可能存在这些类型的基本问题。

于 2013-04-09T18:08:38.693 回答