Browse Source

set_size_and_free() and set_size_and_free() should also update the previous_size of the next block if the latter exists.

Ioannis Koutras 13 years ago
parent
commit
5abecf9dfd
2 changed files with 24 additions and 5 deletions
  1. 4 2
      private-include/block_header.h
  2. 20 3
      src/block_header.c

+ 4 - 2
private-include/block_header.h

@@ -98,18 +98,20 @@ size_t get_size_availability(void *ptr);
 /**
  * Set the size of the memory block's data and mark it free
  *
+ * \param allocator The pointer to the allocator who manages the block.
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The size of the data part of the current memory block.
  */
-void set_size_and_free(void *ptr, size_t size);
+void set_size_and_free(allocator_t *allocator, void *ptr, size_t size);
 
 /**
  * Set the size of the memory block's data and mark it used
  *
+ * \param allocator The pointer to the allocator who manages the block.
  * \param ptr 	The pointer to the data part of the current memory block.
  * \param size 	The size of the data part of the current memory block.
  */
-void set_size_and_used(void *ptr, size_t size);
+void set_size_and_used(allocator_t *allocator, void *ptr, size_t size);
 
 #ifdef FUTURE_FEATURES
 /**

+ 20 - 3
src/block_header.c

@@ -35,14 +35,29 @@ size_t get_size_availability(void *ptr) {
     return get_header(ptr)->size;
 }
 
-void set_size_and_free(void *ptr, size_t size) {
+void set_size_and_free(allocator_t *allocator, void *ptr, size_t size) {
+    block_header_t *next_block_header;
+
     get_header(ptr)->size = size << 1;
+
+    if(allocator->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size = size << 1;
+    }
 }
 
-void set_size_and_used(void *ptr, size_t size) {
-    block_header_t *header = get_header(ptr);
+void set_size_and_used(allocator_t *allocator, void *ptr, size_t size) {
+    block_header_t *header, *next_block_header;
+
+    header = get_header(ptr);
     header->size = size << 1;
     header->size |= 1;
+
+    if(allocator->border_ptr != ptr) {
+        next_block_header = (block_header_t *) ((char *) ptr + get_size(ptr));
+        next_block_header->previous_size = size << 1;
+        next_block_header->previous_size |= 1;
+    }
 }
 
 #ifdef FUTURE_FEATURES
@@ -55,6 +70,7 @@ 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;
@@ -65,6 +81,7 @@ 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);