Commit 14a5001a authored by qmcelhiney22's avatar qmcelhiney22
Browse files

Update bf-alloc.c

parent 0690a224
......@@ -147,22 +147,22 @@ void init () {
*/
void* malloc (size_t size) {
// if heap hasn't yet been initialized, do it
// intitialize if it hasnt been
init();
// if the requested block size is 0, return NULL because there is nothing to do
// if the requested block size is 0, return NULL
if (size == 0) {
return NULL;
}
// going to loop through the free blocks on free LL
// create a pointer and point it to free LL head
// create a pointer to store best fit, initially set to NULL
// loop the free blocks on free LL
// create pointer to free LL head
// pointer to store best fit, set to NULL
header_s* current = free_list_head;
header_s* best = NULL;
// start looping thru free LL until we reach the end
// start looping thru free LL until end
while (current != NULL) {
// if there is an allocated block on free LL, raise an error
......@@ -170,9 +170,8 @@ void* malloc (size_t size) {
ERROR("Allocated block on free list", (intptr_t)current);
}
// if there is no best block and current block is => requested size,
// or if current block is closer to requested size than best block
// then make current block the best block
// if there is no best block and current block is geq requested size,
// or if current block is closer to requested size make current block best
if ( (best == NULL && size <= current->size) ||
(best != NULL && size <= current->size && current->size < best->size) ) {
best = current;
......@@ -203,14 +202,14 @@ void* malloc (size_t size) {
// remove best from LL by moving pointers
// if prev of best is NULL then it is the head of free LL
// so make next of best the new head of free LL
// else have prev of best skip over best with its next pointer
// else have prev of best skip over best with next pointer
if (best->prev == NULL) {
free_list_head = best->next;
} else {
best->prev->next = best->next;
}
// if best is not the end of the free LL
// then make the next of best skip over best with its prev pointer
// make next of best skip over best with prev pointer
if (best->next != NULL) {
best->next->prev = best->prev;
}
......@@ -222,7 +221,7 @@ void* malloc (size_t size) {
allocated_list_head = best;
// make prev of best NULL
best->prev = NULL;
// if best is not the only header in LL, make best the prev of next header in LL
// if best is not the only header , make best the prev of next header in LL
if (best->next != NULL) {
best->next->prev = best;
}
......@@ -255,7 +254,7 @@ void* malloc (size_t size) {
if (header_ptr->next != NULL) {
header_ptr->next->prev = header_ptr;
}
// store the size of the block in the header
// store size of the block in the header
header_ptr->size = size;
// set to true that the block has been allocated
header_ptr->allocated = true;
......@@ -293,7 +292,7 @@ void* malloc (size_t size) {
*/
void free (void* ptr) {
// if pointer is NULL there is nothing to free, jsut return
// if pointer is NULL there is nothing to free, return
if (ptr == NULL) {
return;
}
......@@ -301,29 +300,29 @@ void free (void* ptr) {
// get pointer to block from the header
header_s* header_ptr = BLOCK_TO_HEADER(ptr);
// if block is not allocated then it is already free, raise an error
// if block is not allocated then it is already free, raise error
if (!header_ptr->allocated) {
ERROR("Double-free: ", (intptr_t)header_ptr);
}
// remove header from allocated LL
// if header is not the end of the allocated LL, adjust prev pointer of next
// remove header from allocated list
// if header is not the end of the aLL, adjust prev pointer of next
if ( header_ptr->next != NULL) {
header_ptr->next->prev = header_ptr->prev;
}
// if header is not head of allocated LL, adjust next pointer of prev
// else make next pointer the head of allocated LL
// if header is not head of aLL, adjust next pointer of prev
// else make next pointer the head of aLL
if ( header_ptr->prev != NULL ){
header_ptr->prev->next = header_ptr->next;
} else {
allocated_list_head = header_ptr->next;
}
// add header to free LL
// make next the current head of free LL
// add header to fLL
// make next the current head of fLL
header_ptr->next = free_list_head;
// make freed header the new head of free LL
// make freed header the new head of fLL
free_list_head = header_ptr;
// make prev of freed header NULL
header_ptr->prev = NULL;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment