在c中拥有free()|无熔数据块

时间:2018-11-21 12:53:02

标签: c memory-management merge malloc free

我必须编写自己的free&malloc函数。 到目前为止,我还没有遇到任何问题,但是现在我应该在halde_free()函数中合并相邻的空闲内存块。如果您能在那里帮助我,我将不胜感激。

    #include "halde.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>

/// Magic value for occupied memory chunks.
#define MAGIC ((void*)0xbaadf00d)

/// Size of the heap (in bytes).
#define SIZE (1024*1024*1)

/// Memory-chunk structure.
struct mblock {
    struct mblock *next;
    size_t size;
    char memory[];
};

/// Heap-memory area.
char memory[SIZE];

/// Pointer to the first element of the free-memory list.
static struct mblock *head;

/// Helper function to visualise the current state of the free-memory list.
void halde_print(void) {
    struct mblock* lauf = head;

    // Empty list
    if ( head == NULL ) {
        fprintf(stderr, "(empty)\n");
        return;
    }

    // Print each element in the list
    while ( lauf ) {
        fprintf(stderr, "(addr: 0x%08zx, off: %7zu, ", (uintptr_t) lauf, (uintptr_t)lauf - (uintptr_t)memory);
        fflush(stderr);
        fprintf(stderr, "size: %7zu)", lauf->size);
        fflush(stderr);

        if ( lauf->next != NULL ) {
            fprintf(stderr, "  -->  ");
            fflush(stderr);
        }
        lauf = lauf->next;
    }
    fprintf(stderr, "\n");
    fflush(stderr);
}

void *halde_malloc (size_t size) {
    static int initialized = 0;

    if(initialized == 0){
        head = (struct mblock *) memory;
        head->size = sizeof(memory) - sizeof (struct mblock);
        head->next = NULL;
        initialized = 1;
    }

    if(size == 0){
        return NULL;
    }

    struct mblock *lauf = head;
    struct mblock **prev_next = &head;

    while (lauf != NULL && lauf->size < size){
        prev_next = &(lauf->next);
        lauf = *prev_next;
    }

    if(lauf == NULL){
        errno = ENOMEM;
        return NULL;
    }

    if((lauf->size -size) <= sizeof(struct mblock)){
        *prev_next = lauf->next;
    } else {
        //mblock anlegen und init.
        struct mblock* neu = (struct mblock*) (lauf->memory + size);
        neu->size = lauf->size - sizeof(struct mblock) - size;
        neu->next = lauf->next;

        //mblock anpassen
        lauf->size = size;

        //verketten wiederherstellen
        *prev_next = neu;
    }

    lauf->next = MAGIC;

    return lauf->memory;
}

void halde_free (void *ptr) {
    if(ptr == NULL){
        return;
    }

    struct mblock *mbp = (struct mblock *) ptr - 1;
    if(mbp->next != MAGIC){
        abort();
    } else {
        mbp->next = head;
        head = mbp;
    }
}

到目前为止,代码仍然有效,但是我真的不知道如何合并块。 内存管理在一个简单的链接列表上运行。可变头指向第一个空闲存储块。

我的想法是直接在else部分中合并这些块,但是我没有一个好主意。

0 个答案:

没有答案