Question: //C Program// Goal: Recreate the malloc function by hand without using malloc or its cousin functions (so using sbrk/brk) Data structure is a Doubly Linked

//C Program//

Goal: Recreate the malloc function by hand without using malloc or its cousin functions (so using sbrk/brk)

Data structure is a Doubly Linked List struct called heap_t (contains *next, *prev , int capacity, int size)

I am relatively new to C programming language. I think I've managed to add a block to the heap and search for free blocks to add data (using next_fit algorithm). I understand conceptually that the blocks that have extra space for data can be split but I am not sure how to implement that. Any help would be greatly appreciated!

static heap_t *next_fit = NULL;

void myalloc(size_t size)

{

heap_t *curr = NULL;

if(size == 0) {

return;

}

// adds to heap

curr = sbrk(1024);

curr->capacity = 1024;

curr->size = 0;

curr->next = NULL;

if(head == NULL) {

head = curr;

head->prev = NULL;

tail = head;

next_fit = head;

low_address_marker = head;

high_address_marker = sbrk(0);

}

else {

tail->next = curr;

curr->prev = tail;

tail = curr;

high_address_marker = sbrk(0);

}

if(curr != head)

curr = head;

//search for free block and add data

while(curr != NULL) {

if (curr->size == 0){

if(curr->capacity >= size) {

curr->size = size;

return;

}

}

curr = curr->next;

}

return;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!