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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started