Bläddra i källkod

Refactored the code to push block on a list's head.

Ioannis Koutras 13 år sedan
förälder
incheckning
6e53d0f490
6 ändrade filer med 34 tillägg och 49 borttagningar
  1. 8 0
      private-include/linked_lists/linked_lists.h
  2. 7 22
      src/custom_free.c
  3. 4 10
      src/custom_malloc.c
  4. 10 0
      src/linked_lists/linked_lists.c
  5. 3 11
      src/split.c
  6. 2 6
      src/sys_alloc.c

+ 8 - 0
private-include/linked_lists/linked_lists.h

@@ -84,6 +84,14 @@ void * get_previous(void *ptr);
 #endif /* BLOCKS_IN_DLL */
 
 /**
+ * Push a memory block to the head of a linked list.
+ * 
+ * \param *block The block to be put.
+ * \param *starting_node The starting memory block of the list.
+ */
+void push_block(void *block, void *starting_node);
+
+/**
  * Removes a memory block from a linked list of memory blocks.
  *
  * \param *block The block to be removed.

+ 7 - 22
src/custom_free.c

@@ -76,6 +76,7 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
                 mark_free(ptr);
                 coalesced = false;
             }
+
         }
     } else {
         mark_free(ptr);
@@ -93,37 +94,21 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
                 current_maptable_node = current_maptable_node->next;
             }
         }
-#ifdef BLOCKS_IN_DLL
-        set_previous(ptr, NULL);
-        set_previous(current_maptable_node->fixed_list_head, ptr);
-#endif /* BLOCKS_IN_DLL */
-        set_next(ptr, current_maptable_node->fixed_list_head);
-        current_maptable_node->fixed_list_head = ptr;
+        push_block(ptr, current_maptable_node->fixed_list_head);
     } else { /* put it in the free list */
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-        /* If the block is coalesced, there is no need to add it to the free
-         * list as there is already an entry of the old block
+        /* The block should be added to the free list only if it is not
+         * coalesced
          */
         if(!coalesced) {
-#ifdef BLOCKS_IN_DLL
-            set_previous(heap->free_list_head, ptr);
-            set_previous(ptr, NULL);
-#endif /* BLOCKS_IN_DLL */
-            set_next(ptr, heap->free_list_head);
-            heap->free_list_head = ptr;
+            push_block(ptr, heap->free_list_head);
         }
 #else
-#ifdef BLOCKS_IN_DLL
-        set_previous(heap->free_list_head, ptr);
-        set_previous(ptr, NULL);
-#endif /* BLOCKS_IN_DLL */
-        set_next(ptr, heap->free_list_head);
-        heap->free_list_head = ptr;
+        push_block(ptr, heap->free_list_head);
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
     }
 
-    /* Begin of Stats */
-
+    /* Update Stats */
     heap->dmm_stats.live_objects -= 1;
     heap->dmm_stats.num_free += 1;
 

+ 4 - 10
src/custom_malloc.c

@@ -25,6 +25,7 @@
 #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
 #include <dmmlib/initialize_allocator.h>
 #include "other.h"
+#include "linked_lists/linked_lists.h"
 #include "linked_lists/search_algorithms.h"
 #include "sys_alloc.h"
 #include "block_header.h"
@@ -73,7 +74,7 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         set_requested_size(ptr, size);
         mark_used(ptr);
 
-        /* FIXME split to be put here */
+        /* Try to split */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
         new_size = get_size(ptr) - get_requested_size(ptr) - HEADER_SIZE;
 
@@ -91,19 +92,12 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
         /* Update the used blocks list */
-#ifdef BLOCKS_IN_DLL
-        set_previous(heap->used_blocks_head, ptr);
-        set_previous(ptr, NULL);
-#endif /* BLOCKS_IN_DLL */
-        set_next(ptr, heap->used_blocks_head);
-        heap->used_blocks_head = ptr;
-
-        /* Begin of Stats */
+        push_block(ptr, heap->used_blocks_head);
 
+        /* Update statistics */
         heap->dmm_stats.live_objects += 1;
         heap->dmm_stats.num_malloc += 1;
 
-        /* End of Stats */
         /* FIXME To be refactored - END */
 
     } else {

+ 10 - 0
src/linked_lists/linked_lists.c

@@ -40,6 +40,15 @@ void * get_previous(void *ptr) {
 
 #endif /* BLOCKS_IN_DLL */
 
+void push_block(void *block, void *starting_node) {
+#ifdef BLOCKS_IN_DLL
+    set_previous(starting_node, block);
+    set_previous(block, NULL);
+#endif /* BLOCKS_IN_DLL */
+    set_next(block, starting_node);
+    starting_node = block;
+}
+
 void remove_block(void *block, void *starting_node) {
 
     void *current_node, *previous_node;
@@ -64,3 +73,4 @@ void remove_block(void *block, void *starting_node) {
         previous_node = current_node;
     }
 }
+

+ 3 - 11
src/split.c

@@ -43,24 +43,16 @@ void split(allocator_t *allocator, heap_t *heap, void *ptr,
     /* Check if the block could be put in a fixed list */
     fixed_list_id = map_size_to_list(heap, new_block_size);
 
-    if(fixed_list_id != -1) {
+    if(fixed_list_id != -1) { /* put it in the right fixed list */
         current_maptable_node = heap->maptable_head;		
         if(fixed_list_id != 0) {
             for(i = 1; i < fixed_list_id; i++) {
                 current_maptable_node = current_maptable_node->next;
             }
         }
-#ifdef BLOCKS_IN_DLL
-        set_previous(new_block, NULL);
-#endif /* BLOCKS_IN_DLL */
-        set_next(new_block, current_maptable_node->fixed_list_head);
-        current_maptable_node->fixed_list_head = new_block;
+        push_block(new_block, current_maptable_node->fixed_list_head);
     } else { /* put it in the free list */
-#ifdef BLOCKS_IN_DLL
-        set_previous(new_block, NULL);
-#endif /* BLOCKS_IN_DLL */
-        set_next(new_block, heap->free_list_head);
-        heap->free_list_head = new_block;
+        push_block(new_block, heap->free_list_head);
     }
 
     set_owner(new_block, heap);

+ 2 - 6
src/sys_alloc.c

@@ -85,12 +85,8 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     set_requested_size(ptr, size);
     mark_used(ptr);
 
-    // Update the used blocks list
-#ifdef BLOCKS_IN_DLL
-    set_previous(ptr, NULL);
-#endif /* BLOCKS_IN_DLL */
-    set_next(ptr, heap->used_blocks_head);
-    heap->used_blocks_head = ptr;
+    /* Update the used blocks list */
+    push_block(ptr, heap->used_blocks_head);
 
     // Begin of Stats