Pārlūkot izejas kodu

Added support for always coalescing during free.

Ioannis Koutras 14 gadi atpakaļ
vecāks
revīzija
612182cedf
3 mainītis faili ar 11 papildinājumiem un 3 dzēšanām
  1. 3 0
      DefineOptions.cmake
  2. 3 2
      src/coalesce.c
  3. 5 1
      src/custom_free.c

+ 3 - 0
DefineOptions.cmake

@@ -3,11 +3,14 @@ option(WITH_EXAMPLES "Build with examples" OFF)
 option(WITH_MEMORY_SPACE_AWARENESS "Build with memory space awareness" OFF)
 option(WITH_STATIC_LIB "Build with a static library" OFF)
 
+option(WITH_COALESCING "Build with coalescing support" OFF)
+
 option(P2012 "Build for P2012 runtime" OFF)
 
 if (P2012)
   set(HAVE_LOCKS OFF)
   set(WITH_MEMORY_SPACE_AWARENESS ON)
   set(WITH_STATIC_LIB ON)
+  set(WITH_COALESCING ON)
 endif (P2012)
 

+ 3 - 2
src/coalesce.c

@@ -15,6 +15,8 @@ void * coalesce(void *ptr, heap_t *heap) {
     }
 
     // Try to coalesce with the previous memory block
+    // FIXME What happens if we have multiple heaps and we don't know the
+    // owner of the blocks?
     if(is_previous_free(ptr)) {
         prev = get_previous(ptr);
 
@@ -25,8 +27,7 @@ void * coalesce(void *ptr, heap_t *heap) {
             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;
+                    current_maptable_node = current_maptable_node->next;
                 }
             }
             remove_block(ptr, current_maptable_node->fixed_list_head);

+ 5 - 1
src/custom_free.c

@@ -16,7 +16,7 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
     if(allocator == NULL) {
         allocator = &systemallocator;
     }
-   
+
     if(heap == NULL) {
         heap_id = map_thread_heap();
         heap = &allocator->heaps[heap_id];
@@ -31,6 +31,10 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
 
     remove_block(ptr, heap->used_blocks_head);
 
+#ifdef WITH_COALESCING
+    ptr = coalesce(heap, ptr); 
+#endif /* WITH_COALESCING */
+
     // Check if the block could be put in a fixed list
     fixed_list_id = map_size_to_list(heap, size);