|
@@ -2,12 +2,13 @@
|
|
|
#include "posix_lock.h"
|
|
|
#include "other.h"
|
|
|
#include "sys_alloc.h"
|
|
|
+#include "block_header.h"
|
|
|
|
|
|
void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
- leaHdr *ptr;
|
|
|
+ void *ptr;
|
|
|
int fixed_list_id, i, found;
|
|
|
- MAPTABLE_NODE *current_maptable_node;
|
|
|
- leaHdr *current_node;
|
|
|
+ maptable_node_t *current_maptable_node;
|
|
|
+ void *current_block, *previous_block;
|
|
|
|
|
|
ptr = NULL;
|
|
|
|
|
@@ -26,9 +27,8 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
}
|
|
|
if(current_maptable_node->fixed_list_head != NULL) {
|
|
|
ptr = current_maptable_node->fixed_list_head;
|
|
|
- current_maptable_node->fixed_list_head = ptr->next;
|
|
|
+ current_maptable_node->fixed_list_head = get_next(ptr);
|
|
|
set_requested_size(ptr, size);
|
|
|
- markInUse(ptr);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -36,28 +36,25 @@ void * custom_malloc(heap_t* heap, size_t size) {
|
|
|
found = 0;
|
|
|
|
|
|
// first fit from free list
|
|
|
- current_node = heap->free_list_head;
|
|
|
-
|
|
|
- if(current_node != NULL && getSize(current_node) >= size) {
|
|
|
- ptr = current_node;
|
|
|
- heap->free_list_head = ptr->next;
|
|
|
- set_requested_size(ptr, size);
|
|
|
- markInUse(ptr);
|
|
|
- }
|
|
|
- while(current_node && !found) {
|
|
|
- if(getSize(current_node->next) >= size) {
|
|
|
- ptr = current_node->next;
|
|
|
- current_node->next = ptr->next;
|
|
|
+ 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;
|
|
|
set_requested_size(ptr, size);
|
|
|
- markInUse(ptr);
|
|
|
- found = 1;
|
|
|
+ set_next(ptr, heap->used_blocks_head);
|
|
|
+ heap->used_blocks_head = ptr;
|
|
|
+ if(current_block != heap->free_list_head) {
|
|
|
+ set_next(previous_block, get_next(ptr));
|
|
|
+ } else {
|
|
|
+ heap->free_list_head = get_next(ptr);
|
|
|
+ }
|
|
|
+ posix_unlock(heap);
|
|
|
+ return ptr;
|
|
|
}
|
|
|
- current_node = current_node->next;
|
|
|
+ previous_block = current_block;
|
|
|
}
|
|
|
|
|
|
if(!found) {
|
|
|
- posix_unlock(heap);
|
|
|
- return sys_alloc(size);
|
|
|
+ ptr = sys_alloc(heap, size);
|
|
|
}
|
|
|
|
|
|
}
|