Browse Source

Code refactoring to improve the readability of custom_malloc() steps.

Ioannis Koutras 13 years ago
parent
commit
167e24d6b6
2 changed files with 16 additions and 33 deletions
  1. 14 30
      custom_malloc.c
  2. 2 3
      sys_alloc.c

+ 14 - 30
custom_malloc.c

@@ -9,7 +9,7 @@
 
 void * custom_malloc(heap_t* heap, size_t size) {
     void *ptr;
-    int fixed_list_id, i, found;
+    int fixed_list_id, i;
     maptable_node_t *current_maptable_node;
     void *current_block, *previous_block;
 
@@ -34,52 +34,36 @@ 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 = get_next(ptr);
-            set_requested_size(ptr, size);
-            set_next(ptr, heap->used_blocks_head);
-            heap->used_blocks_head = ptr;
         }
     }
 
     if(ptr == NULL) {
-        found = 0;
 
         // first fit from free list
         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;
-                heap->used_blocks_head = ptr;
-                if(current_block != heap->free_list_head) {
-                    set_next(previous_block, get_next(ptr));
-                } else {
+                if(current_block == heap->free_list_head) {
                     heap->free_list_head = get_next(ptr);
+                } else {
+                    set_next(previous_block, get_next(current_block));
                 }
-                set_requested_size(ptr, size);
-                set_next(ptr, heap->used_blocks_head);
-
-                // Begin of Stats
-
-                heap->dmm_stats.live_objects += 1;
-                heap->dmm_stats.num_malloc += 1;
-
-                // End of Stats
-
-#ifdef HAVE_LOCKS
-                posix_unlock(heap);
-#endif /* HAVE_LOCKS */
-                return ptr;
+                ptr = current_block;
+                break;
             }
             previous_block = current_block;
         }
+    }
 
-        if(!found) {
-            ptr = sys_alloc(heap, size);
-            heap->dmm_stats.mem_allocated += req_padding(size);
-            heap->dmm_stats.mem_requested += size;
-        }
-
+    if(ptr == NULL) {
+        ptr = sys_alloc(heap, size);
     }
 
+    set_requested_size(ptr, size);
+    set_next(ptr, heap->used_blocks_head);
+
+    heap->used_blocks_head = ptr;
+
     // Begin of Stats
 
     heap->dmm_stats.live_objects += 1;

+ 2 - 3
sys_alloc.c

@@ -28,9 +28,8 @@ void *sys_alloc(heap_t *heap, size_t size) {
     ptr = (void *) ((char *) ptr + HEADER_SIZE);
 
     set_size(ptr, req_padding(size));
-    set_requested_size(ptr, size);
-    set_next(ptr, heap->used_blocks_head);	
-    heap->used_blocks_head = ptr;
+    heap->dmm_stats.mem_allocated += req_padding(size);
+    heap->dmm_stats.mem_requested += size;
 
 #ifdef HAVE_LOCKS
     sbrk_unlock();