Browse Source

remove big blocks from list when debug is enabled; use uintptr_t instead of char *

Ioannis Koutras 12 years ago
parent
commit
11b5e54d3e
2 changed files with 43 additions and 9 deletions
  1. 36 6
      src/free.c
  2. 7 3
      src/malloc.c

+ 36 - 6
src/free.c

@@ -20,11 +20,12 @@
  * @author Ioannis Koutras (joko@microlab.ntua.gr)
  * @date   September 2012
  *
- * @brief  Implementation free() call.
+ * @brief  Implementation of the free() call.
  */
 
 #include "dmmlib/dmmlib.h"
 
+#include <inttypes.h>
 #include <stdbool.h>
 
 #ifdef BITMAP_RB_ONLY
@@ -43,6 +44,7 @@
 
 #include "release_memory.h"
 
+
 #include "trace.h"
 
 void free(void *ptr) {
@@ -59,8 +61,8 @@ void free(void *ptr) {
 
     current_raw_block = systemallocator.raw_block_head;
     while(current_raw_block) {
-        if(((char *)ptr > (char *)current_raw_block) &&
-                ((char *)ptr < (char *)(current_raw_block) +
+        if(((uintptr_t) ptr > (uintptr_t) current_raw_block) &&
+                ((uintptr_t) ptr < (uintptr_t) current_raw_block +
                  current_raw_block->size)) {
             found = true;
             break;
@@ -83,7 +85,7 @@ void free(void *ptr) {
 #ifdef FL_RB_ONLY
                 (freelist_rb_t *)
 #endif /* FL_RB_ONLY */
-                ((char *)current_raw_block + sizeof(raw_block_header_t));
+                ((uintptr_t) current_raw_block + sizeof(raw_block_header_t));
 
 #ifdef HAVE_LOCKS
         pthread_mutex_lock(&current_raw_block->mutex);
@@ -95,8 +97,36 @@ void free(void *ptr) {
 
     } else { // It has to be a BIGBLOCK, just munmap it
 
-        current_raw_block = (raw_block_header_t *)((char *)ptr -
-                sizeof(raw_block_header_t));
+        current_raw_block = (raw_block_header_t *)
+            ((uintptr_t) ptr - sizeof(raw_block_header_t));
+
+        /* Remove big block from the list of big blocks that exists when debug
+         * mode is on */
+#ifdef WITH_DEBUG
+        raw_block_header_t *previous_raw_block = NULL;
+
+        for(raw_block_header_t *block_in_list =
+                systemallocator.big_blocks_head;
+                block_in_list != NULL;
+                block_in_list = block_in_list->next_raw_block
+                ) {
+            if(block_in_list == current_raw_block) {
+                if(systemallocator.big_blocks_head == current_raw_block) {
+#ifdef HAVE_LOCKS
+                pthread_mutex_lock(&systemallocator.creation_mutex);
+#endif /* HAVE_LOCKS */
+                    systemallocator.big_blocks_head =
+                        current_raw_block->next_raw_block;
+#ifdef HAVE_LOCKS
+                pthread_mutex_unlock(&systemallocator.creation_mutex);
+#endif /* HAVE_LOCKS */
+                } else {
+                    previous_raw_block->next_raw_block =
+                        current_raw_block->next_raw_block;
+                }
+            }
+        }
+#endif /* WITH_DEBUG */
 
 #ifdef WITH_ALLOCATOR_STATS
         update_stats(&systemallocator.dmm_stats,

+ 7 - 3
src/malloc.c

@@ -25,6 +25,8 @@
 
 #include "dmmlib/dmmlib.h"
 
+#include <inttypes.h>
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
@@ -73,7 +75,7 @@ void * malloc(size_t size) {
 #ifdef FL_RB_ONLY
             (freelist_rb_t *)
 #endif /* FL_RB_ONLY */
-            ((char *)raw_block + sizeof(raw_block_header_t));
+            ((uintptr_t) raw_block + sizeof(raw_block_header_t));
         ptr = dmmlib_malloc(encapsulated_rb, size);
 #ifdef HAVE_LOCKS
         pthread_mutex_unlock(&raw_block->mutex);
@@ -109,6 +111,8 @@ void * malloc(size_t size) {
                     sizeof(raw_block_header_t), BIGBLOCK);
             if(ptr != NULL) {
 
+                /* When debugging is enabled, the big blocks are also stored on
+                 * a list. */
 #ifdef WITH_DEBUG
 #ifdef HAVE_LOCKS
                 pthread_mutex_lock(&systemallocator.creation_mutex);
@@ -130,7 +134,7 @@ void * malloc(size_t size) {
                         size + sizeof(raw_block_header_t));
 #endif /* WITH_ALLOCATOR_STATS */
 
-                ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));
+                ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
             }
 
         } else {
@@ -161,7 +165,7 @@ void * malloc(size_t size) {
 #ifdef FL_RB_ONLY
                     (freelist_rb_t *)
 #endif /* FL_RB_ONLY */
-                    ((char *)new_raw_block + sizeof(raw_block_header_t));
+                    ((uintptr_t) new_raw_block + sizeof(raw_block_header_t));
                 ptr = dmmlib_malloc(encapsulated_rb, size);
 #ifdef HAVE_LOCKS
                 pthread_mutex_unlock(&new_raw_block->mutex);