使用伙伴系统分配malloc获得无限循环

使用伙伴系统分配malloc获得无限循环,c,C,我在C中用buddy系统分配编码malloc,我不明白为什么在分割内存时会陷入无限循环 以下是相关功能: void split_memory(int target_index, int current_index, metadata_t *current) { if (target_index == current_index) return; current->size /= 2; // Create new half metadata_t *new

我在C中用buddy系统分配编码malloc,我不明白为什么在分割内存时会陷入无限循环

以下是相关功能:

void split_memory(int target_index, int current_index, metadata_t *current)
{

    if (target_index == current_index) return;
    current->size /= 2;

    // Create new half
    metadata_t *new = (metadata_t *) ((char *) current + current->size);
    new->size = current->size;
    new->in_use = 0;
    new->next = NULL;
    new->prev = NULL;

    // Move current out of the free list
    metadata_t* next = current->next;
    current->next = NULL;
    current->prev = NULL;
    if (next)
    {
        freelist[current_index] = next;
        next->prev = NULL;
    }

    // Move current into the lower freelist
    current->next = new;
    current->prev = NULL;
    new->next = NULL;
    new->prev = current;

    metadata_t* lower = freelist[current_index - 1];

    // If there are any blocks currently in lower, then make sure we add it to the end of the freelist
    if (lower)
    {
        while(lower->next)
        {
            lower = lower->next;
        }
        lower->next = current;
        current->prev = lower;
    }
    else
    {
        freelist[current_index - 1] = current;
    }

    split_memory(target_index, current_index - 1, current);
    split_memory(target_index, current_index - 1, new);
}

看起来我无意中创建了一个循环链接列表,因此到达自由列表末尾的循环是永无止境的,但我不知道我是如何创建循环链接列表的。

看起来像无限递归…请不要使用名为
new
的变量。。。它让我在蜂箱里爆发@BitFiddlingCodeMonkey不应该成为目标。索引肯定小于当前的索引。很确定这是一个循环链表。@Eric抱歉!我会尽快改变的,我也很烦:)@Eric:我不同意。使用一个名为<> >新< /COD>的变量是防止试图用C++编译器编译C的傻瓜的一个很好的方法。