Browse Source

mark_used() and mark_free() now properly mark the previous_size record of the next block if there is one.

Ioannis Koutras 13 years ago
parent
commit
ee4b032338
4 changed files with 25 additions and 9 deletions
  1. 8 4
      private-include/block_header.h
  2. 14 2
      src/block_header.c
  3. 2 2
      src/custom_free.c
  4. 1 1
      src/custom_malloc.c

+ 8 - 4
private-include/block_header.h

@@ -123,18 +123,22 @@ void set_requested_size(void *ptr, size_t size);
 #endif /* FUTURE_FEATURES */
 
 /**
- * Mark the memory block as used
+ * Mark the memory block as used, as well as the previous_size element of the
+ * next block if there is one.
  *
+ * \param allocator The pointer to the allocator who manages the block.
  * \param ptr 	The pointer to the data part of the memory block.
  */
-void mark_used(void *ptr);
+void mark_used(allocator_t *allocator, void *ptr);
 
 /**
- * Mark the memory block as free
+ * Mark the memory block as free, as well as the previous_size element of the
+ * next block if there is one.
  *
+ * \param allocator The pointer to the allocator who manages the block.
  * \param ptr 	The pointer to the data part of the memory block.
  */
-void mark_free(void *ptr);
+void mark_free(allocator_t *allocator, void *ptr);
 
 /**
  * Set the availability and the size of the previous memory block

+ 14 - 2
src/block_header.c

@@ -51,12 +51,24 @@ void set_requested_size(void *ptr, size_t size) {
 }
 #endif /* FUTURE_FEATURES */
 
-void mark_used(void *ptr) {
+void mark_used(allocator_t *allocator, void *ptr) {
+    block_header_t *next_block_header;
+
     get_header(ptr)->size |= 1;
+    if(allocator->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size |= 1;
+    }
 }
 
-void mark_free(void *ptr) {
+void mark_free(allocator_t *allocator, void *ptr) {
+    block_header_t *next_block_header;
+    
     get_header(ptr)->size &= (~ 0x1);
+    if(allocator->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size &= (~ 0x1);
+    }    
 }
 
 #ifdef WITH_OWNERSHIP

+ 2 - 2
src/custom_free.c

@@ -88,14 +88,14 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
                 ptr = coalesce(ptr, heap, allocator);
                 coalesced = true;
             } else {
-                mark_free(ptr);
+                mark_free(allocator, ptr);
             }
 #ifdef WITH_OWNERSHIP
         }
 #endif /* WITH_OWNERSHIP */
     } else {
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-        mark_free(ptr);
+        mark_free(allocator, ptr);
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     }
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */

+ 1 - 1
src/custom_malloc.c

@@ -105,7 +105,7 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         }
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
-        mark_used(ptr);
+        mark_used(allocator, ptr);
 
 #ifdef FUTURE_FEATURES
         /* Update the used blocks list */