-1

I try to write my own custom malloc and free function in c. I worked around 12 hours on this and tried lots of things. But it doesn't work. Maybe you guys can figure out the error. Allocated memory gets removed from the list with a next pointer to a specific address to identify it later in the free function. The current error is a segmentation fault 11 in the split method. C-File:

Head:

#define MAGIC ((void*)0xbaadf00d)
#define SIZE (1024*1024*1)

typedef struct mblock {
    struct mblock *next;
    size_t size;
    char memory[];
}mblock;

char memory[SIZE];

static struct mblock *head;

malloc:

void *halde_malloc (size_t size) {
    printf("Starting\n");
    printf("%zu\n",size);
    if(size <= 0) {return NULL;}
    if(head == NULL){
        initializeBlock();
        printf("Memory initialized\n");
    }
    mblock *temp_block = head;
    while(temp_block != NULL) {
        printf("IN\n");
        if(temp_block->size == size) {
            list_remove(temp_block);
            temp_block->next = MAGIC;
            return (void*)(temp_block);
        } else if(temp_block->size > size) {
            size_t temp_size = temp_block->size;
            printf("size IS more than equal\n");
            list_split_AND_Remove(temp_size - size, temp_block);
            temp_block->size = size;
            temp_block->next = MAGIC;
            return (void*)(temp_block);
        }
        temp_block = temp_block->next;
        printf("One block checked\n");
    }
    errno = ENOMEM;
    return NULL;
}

Initialize:

void initializeBlock(){
    printf("Initializing\n");
    head = (mblock*)memory;
    head->size=sizeof(memory)-sizeof(mblock);
    head->next=NULL;
}

Split:

void list_split_AND_Remove(size_t size, mblock *lastBlock) {
    printf("Split\n");
    mblock *new = (void*)((mblock*)lastBlock+size+sizeof(mblock));
    new->size = size - sizeof(mblock);
    new->next = lastBlock->next;
    lastBlock->next = new;
    printf("START REMOVE");
    list_remove(lastBlock);

}

Remove:

void list_remove(mblock *p) {
    printf("Remove\n");
    mblock *temp_block = head;
    if(p == head) {
        if(head->next == NULL) {
            head = NULL;
            return;
        } else {
            head = p->next;
            return;
        }
    }
    while(temp_block->next != NULL) {
        if(temp_block->next == p) {
            printf("Found P:");
            temp_block = p->next;
            return;
        }
        temp_block = temp_block->next;
    }

}

Free:

void halde_free (void *ptr) {
    printf("FREE\n");
    mblock *new_block = ptr;
    if(new_block->next == MAGIC) {
        new_block->next = head;
        head = new_block;
    } else {abort();}

}
8
  • 1
    What, specifically, does "does not work" mean? How do you elicit the misbehavior, and how does it manifest? Nov 15, 2018 at 21:19
  • At this time I got a segmentation fault 11 in the split method. Nov 15, 2018 at 21:20
  • Those sizeof are very suspicious Nov 15, 2018 at 21:22
  • Anyone trying to implement their own malloc() should be advanced enough to know how to use a debugger and valgrind.
    – Barmar
    Nov 15, 2018 at 21:31
  • 2
    You're not connecting anything. You're just assigning to a local variable, which will go out of scope when you return on the next line.
    – Barmar
    Nov 15, 2018 at 21:48

1 Answer 1

3

Issues with your code include, but are not necessarily limited to:

  • list_remove() does not actually remove the specified block from the list unless it happens to be the current list head. In every other case, therefore, halde_malloc() corrupts the list after calling list_remove() when it modifies the node's next pointer.

  • list_split_AND_Remove() performs incorrect pointer arithmetic. Specifically, mblock *new = (void*)((mblock*)lastBlock+size+sizeof(mblock)); does not do what you appear to want to do, because pointer arithmetic operates in units the size of the pointed-to type, whereas the size argument and the result of the sizeof operator have units of individual bytes. (Also, both casts are useless, albeit not harmful in themselves.)

  • Your allocator returns a pointer to the block header, not to its data. As a result, the user will very likely overwrite the block header's contents, leading to havoc when you later try to free that block.

  • You seem to assume that mblock objects have an alignment requirement of 1. That might not be true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.