|
@@ -15,6 +15,7 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
+#include <stdio.h>
|
|
|
#include "linked_lists/search_algorithms.h"
|
|
|
#include "block_header.h"
|
|
|
|
|
@@ -59,13 +60,12 @@ void * search_on_fixed(heap_t * heap, size_t requested_size) {
|
|
|
* found.
|
|
|
*/
|
|
|
void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
|
|
|
- void *current_block, *previous_block, *ptr;
|
|
|
+ void *current_block, *previous_block;
|
|
|
void *best_block, *best_previous_block;
|
|
|
size_t best_size;
|
|
|
|
|
|
previous_block = NULL;
|
|
|
best_previous_block = NULL;
|
|
|
- ptr = NULL;
|
|
|
best_block = NULL;
|
|
|
best_size = (size_t) -1; /* SIZE_MAX */
|
|
|
|
|
@@ -76,6 +76,8 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
|
|
|
best_block = current_block;
|
|
|
best_size = get_size(current_block);
|
|
|
best_previous_block = previous_block;
|
|
|
+ /* If the requested size is found, there is no need to keep
|
|
|
+ * searching for a better sized block */
|
|
|
if(best_size == requested_size) {
|
|
|
break;
|
|
|
}
|
|
@@ -90,7 +92,9 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
|
|
|
if(best_block == heap->free_list_head) {
|
|
|
heap->free_list_head = get_next(best_block);
|
|
|
#ifdef BLOCKS_IN_DLL
|
|
|
- set_previous(heap->free_list_head, NULL);
|
|
|
+ if(heap->free_list_head != NULL) {
|
|
|
+ set_previous(heap->free_list_head, NULL);
|
|
|
+ }
|
|
|
#endif /* BLOCKS_IN_DLL */
|
|
|
} else {
|
|
|
set_next(best_previous_block, get_next(best_block));
|
|
@@ -98,10 +102,9 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
|
|
|
set_previous(get_next(best_block), best_previous_block);
|
|
|
#endif /* BLOCKS_IN_DLL */
|
|
|
}
|
|
|
- ptr = best_block;
|
|
|
}
|
|
|
|
|
|
- return ptr;
|
|
|
+ return best_block;
|
|
|
}
|
|
|
|
|
|
/**
|