|
@@ -1,3 +1,4 @@
|
|
|
+#include <stdio.h>
|
|
|
#include "custom_malloc.h"
|
|
|
#include "posix_lock.h"
|
|
|
#include "other.h"
|
|
@@ -11,13 +12,17 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
void *current_block, *previous_block;
|
|
|
|
|
|
ptr = NULL;
|
|
|
+ previous_block = NULL;
|
|
|
|
|
|
posix_lock(heap);
|
|
|
|
|
|
+ printf("Searching for a free block of size %d.\n", size);
|
|
|
+
|
|
|
fixed_list_id = map_size_to_list(heap, size);
|
|
|
|
|
|
// If a fixed list is found, do a first fit
|
|
|
if(fixed_list_id != -1) {
|
|
|
+ printf("Searching on fixed lists...\n");
|
|
|
current_maptable_node = heap->maptable_head;
|
|
|
// traverse through the maptable node list
|
|
|
if(fixed_list_id != 0) {
|
|
@@ -29,6 +34,8 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
ptr = current_maptable_node->fixed_list_head;
|
|
|
current_maptable_node->fixed_list_head = get_next(ptr);
|
|
|
set_requested_size(ptr, size);
|
|
|
+ set_next(ptr, heap->used_blocks_head);
|
|
|
+ heap->used_blocks_head = ptr;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -36,6 +43,7 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
found = 0;
|
|
|
|
|
|
// first fit from free list
|
|
|
+ printf("Searching on the free list...\n", size);
|
|
|
for(current_block = heap->free_list_head; current_block != NULL; current_block = get_next(current_block)) {
|
|
|
if(get_size(current_block) >= size) {
|
|
|
ptr = current_block;
|
|
@@ -48,13 +56,16 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
heap->free_list_head = get_next(ptr);
|
|
|
}
|
|
|
posix_unlock(heap);
|
|
|
+ printf("Block found in the free list.\n");
|
|
|
return ptr;
|
|
|
}
|
|
|
previous_block = current_block;
|
|
|
}
|
|
|
|
|
|
if(!found) {
|
|
|
+ printf("No free available chunk was found.\n");
|
|
|
ptr = sys_alloc(heap, size);
|
|
|
+ printf("Chuck was given by the OS.\n");
|
|
|
}
|
|
|
|
|
|
}
|