Bladeren bron

Fix the support of freelist-organised raw blocks (WIP)

Ioannis Koutras 12 jaren geleden
bovenliggende
commit
975d437a65

+ 3 - 3
DefineOptions.cmake

@@ -1,5 +1,5 @@
 set(WITH_SYSTEM_CALLS "none" CACHE STRING "Choose what system calls can be used, options are: none, sbrk, mmap")
-set(RAW_BLOCKS_TYPE "bitmap" CACHE STRING "Choose what raw blocks can be used, options are: freelist, bitmap")
+set(RAW_BLOCKS_TYPE "freelist" CACHE STRING "Choose what raw blocks can be used, options are: freelist, bitmap")
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" ON)
 option(WITH_REALLOC "Build with realloc" OFF)
 set(TRACE_LEVEL 1 CACHE INTEGER "Choose the trace level, options are: 0, 1, 2 and 3")
@@ -65,9 +65,9 @@ if (LINUXTEST)
   set(FIT_PERCENTAGE 0.6f)
   set(HAVE_LOCKS ON)
   set(WITH_EXAMPLES ON)
-  set(WITH_COALESCING "never")
+  set(WITH_COALESCING "fixed")
   set(MAX_COALESCE_SIZE 60000)
-  set(WITH_SPLITTING "never")
+  set(WITH_SPLITTING "fixed")
   set(MIN_SPLITTING_SIZE 300)
   set(COALESCE_AFTER_SPLIT OFF)
   set(WITH_SHARED_LIB ON)

+ 2 - 0
include/dmmlib/dmmlib.h

@@ -37,6 +37,8 @@ allocator_t systemallocator;
 /**
  * Allocates size bytes by using the system's allocator.
  * @param size Requested allocation size in bytes.
+ *
+ * @return The pointer to the memory location.
  */
 void * malloc(size_t size);
 

+ 1 - 0
private-include/bitmap/bitmap.h

@@ -30,6 +30,7 @@
 #ifdef BITMAP_RB_ONLY
 #define dmmlib_malloc(raw_block, size) bitmap_malloc(raw_block, size)
 #define dmmlib_free(raw_block, size) bitmap_free(raw_block, size)
+#define dmmlib_realloc(raw_block, ptr, size) bitmap_realloc(raw_block, ptr, size)
 #endif /* BITMAP_RB_ONLY */
 
 void * bitmap_malloc(bitmap_rb_t *raw_block, size_t size);

+ 7 - 9
private-include/freelist/freelist.h

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2012 Institute of Communication and Computer Systems (ICCS) 
+ *   Copyright Institute of Communication and Computer Systems (ICCS) 
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
@@ -25,20 +25,18 @@
 #define FREELIST_H
 #include "dmm_config.h"
 
-#include "dmmlib/raw_block.h"
+#include "freelist/freelist_rb.h"
 
 #ifdef FL_RB_ONLY
 #define dmmlib_malloc(raw_block, size) freelist_malloc(raw_block, size)
-#endif /* FL_RB_ONLY */
-
-void * freelist_malloc(raw_block_header_t *raw_block, size_t size);
-
-#ifdef FL_RB_ONLY
 #define dmmlib_free(raw_block, ptr) freelist_free(raw_block, ptr)
+#define dmmlib_realloc(raw_block, ptr, size) freelist_realloc(raw_block, ptr, size)
 #endif /* FL_RB_ONLY */
 
-void freelist_free(raw_block_header_t *raw_block, void *ptr);
+void * freelist_malloc(freelist_rb_t *raw_block, size_t size);
+
+void freelist_free(freelist_rb_t *raw_block, void *ptr);
 
-void * freelist_realloc(raw_block_header_t *raw_block, void *ptr, size_t size);
+void * freelist_realloc(freelist_rb_t *raw_block, void *ptr, size_t size);
 
 #endif /* FREELIST_H */

+ 1 - 0
private-include/freelist/freelist_rb.h

@@ -29,6 +29,7 @@
 /** Data structure of the required elements for a free-list organized raw block.
  */
 typedef struct freelist_rb_s {
+    size_t remaining_size; /**< The remaining size for new memory blocks. */
     block_header_t *border_ptr; /**< Pointer to the memory block allocated last. */
     block_header_t *free_list_head; /**< Head of the free list of memory blocks. */
 #ifdef FIFO_SORT_POLICY

+ 25 - 8
src/free.c

@@ -26,10 +26,17 @@
 #include "dmmlib/dmmlib.h"
 
 #include <stdbool.h>
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
 #endif /* BITMAP_RB_ONLY */
+
+#ifdef FL_RB_ONLY
+#include "freelist/freelist.h"
+#include "freelist/freelist_rb.h"
+#endif /* FL_RB_ONLY */
+
 #include "release_memory.h"
 
 #include "trace.h"
@@ -56,22 +63,32 @@ void free(void *ptr) {
     }
 
     if(found == true) {
+
 #ifdef BITMAP_RB_ONLY
-        bitmap_rb_t *bitmap_rb;
-        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
-                sizeof(raw_block_header_t));
+        bitmap_rb_t
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        freelist_rb_t
+#endif /* FL_RB_ONLY */
+            *encapsulated_rb = 
+#ifdef BITMAP_RB_ONLY
+                (bitmap_rb_t *)
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+                (freelist_rb_t *)
+#endif /* FL_RB_ONLY */
+                ((char *)current_raw_block + sizeof(raw_block_header_t));
+
 #ifdef HAVE_LOCKS
         pthread_mutex_lock(&current_raw_block->mutex);
 #endif /* HAVE_LOCKS */
-        dmmlib_free(bitmap_rb, ptr);
+        dmmlib_free(encapsulated_rb, ptr);
 #ifdef HAVE_LOCKS
         pthread_mutex_unlock(&current_raw_block->mutex);
 #endif /* HAVE_LOCKS */
-#endif /* BITMAP_RB_ONLY */
-#ifdef FL_RB_ONLY
-        dmmlib_free(current_raw_block, ptr);
-#endif /* FL_RB_ONLY */
+
     } else { // It has to be a BIGBLOCK, just munmap it
+
         current_raw_block = (raw_block_header_t *)((char *)ptr -
                 sizeof(raw_block_header_t));
         TRACE_1("dmmlib - free - free'ing %zu bytes from raw block %p\n",

+ 42 - 25
src/freelist/freelist_free.c

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2011 Institute of Communication and Computer Systems (ICCS) 
+ *   Copyright Institute of Communication and Computer Systems (ICCS) 
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
@@ -15,11 +15,13 @@
  *
  */
 
-#include "freelist/freelist.h"
+/**
+ * @file   freelist_free.c
+ * @author Ioannis Koutras
+ * @brief  free() implementation for freelist-organised raw blocks
+ */
 
-#ifdef HAVE_LOCKS
-#include <pthread.h>
-#endif /* HAVE_LOCKS */
+#include "freelist/freelist.h"
 
 #include "freelist/block_header_funcs.h"
 #include "freelist/linked_lists/add_block.h"
@@ -27,38 +29,53 @@
 #include "freelist/coalesce.h"
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
 
+#include "trace.h"
+
+#ifdef WITH_ALLOCATOR_STATS
+#include "dmmlib/dmmlib.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 /** Frees the memory block inside of a specific free-list organized raw block.
- * @param raw_block The pointer of the raw block.
+ * @param raw_block The pointer of the freelist raw block.
  * @param ptr       The pointer of the memory block to be freed.
  */
-void freelist_free(raw_block_header_t *raw_block, void *ptr) {
-    freelist_rb_t *rb_header;
+void freelist_free(freelist_rb_t *raw_block, void *ptr) {
 
-    rb_header = (freelist_rb_t *)((char *)raw_block +
-            sizeof(raw_block_header_t));
     ptr = get_header(ptr);
 
-#ifdef HAVE_LOCKS
-    pthread_mutex_lock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
+#ifndef REQUEST_SIZE_INFO
+    TRACE_1("dmmlib - free - free'ing %zu bytes from freelist raw block %p\n",
+            get_size(ptr), (void *)raw_block);
+#else /* REQUEST_SIZE_INFO */
+    TRACE_1("dmmlib - free - free'ing %zu bytes from freelist raw block %p, out of "
+            "which %zu were used by the application\n",
+            get_size(ptr),
+            (void *)raw_block,
+            get_requested_size(ptr));
+#endif /* REQUEST_SIZE_INFO */
 
     // Memory stats get updated here in case the space gets coalesced with its
     // free neighbors.
-#ifdef WITH_RAWBLOCK_STATS
-    raw_block->dmm_stats.total_mem_allocated -= get_size(ptr);
-    raw_block->dmm_stats.live_objects--;
-    raw_block->dmm_stats.num_free++;
-#endif /* WITH_RAWBLOCK_STATS */
+#ifdef WITH_ALLOCATOR_STATS
+    systemallocator.dmm_stats.total_mem_allocated -=
+        get_size(ptr) + HEADER_SIZE;
+    TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+            systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+    systemallocator.dmm_stats.total_mem_requested -=
+        get_requested_size(ptr);
+    TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+            systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+    systemallocator.dmm_stats.live_objects--;
+    systemallocator.dmm_stats.num_free++;
+#endif /* WITH_ALLOCATOR_STATS */
 
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-    coalesce(rb_header, ptr);
+    coalesce(raw_block, ptr);
 #else
-    mark_free(rb_header, ptr);
-    add_block(rb_header, ptr);
+    mark_free(raw_block, ptr);
+    add_block(raw_block, ptr);
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
 
-#ifdef HAVE_LOCKS
-    pthread_mutex_unlock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
-
 }

+ 50 - 46
src/freelist/freelist_malloc.c

@@ -1,5 +1,5 @@
 /*
- *   Copyright 2011 Institute of Communication and Computer Systems (ICCS) 
+ *   Copyright Institute of Communication and Computer Systems (ICCS) 
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
@@ -15,11 +15,14 @@
  *
  */
 
-#include "freelist/freelist.h"
+/**
+ * @file   freelist_malloc.c
+ * @author Ioannis Koutras
+ * @date   September 2012
+ * @brief  malloc() implementation for freelist-organised raw blocks
+ */
 
-#ifdef HAVE_LOCKS
-#include <pthread.h>
-#endif /* HAVE_LOCKS */
+#include "freelist/freelist.h"
 
 #include "freelist/block_header_funcs.h"
 #include "freelist/linked_lists/linked_lists.h"
@@ -29,18 +32,29 @@
 #include "freelist/split.h"
 #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
 
+#include "trace.h"
+
+#ifdef WITH_ALLOCATOR_STATS
+#include "dmmlib/dmmlib.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 #if defined (BEST_FIT)
-#define search_on_free(size) best_fit_on_freelist(rb_header, size)
+#define search_on_free(size) best_fit_on_freelist(raw_block, size)
 #elif defined (GOOD_FIT)
-#define search_on_free(size) good_fit_on_freelist(rb_header, size)
+#define search_on_free(size) good_fit_on_freelist(raw_block, size)
 #elif defined (EXACT_FIT)
-#define search_on_free(size) exact_fit_on_freelist(rb_header, size)
+#define search_on_free(size) exact_fit_on_freelist(raw_block, size)
 #elif defined (FIRST_FIT)
-#define search_on_free(size) first_fit_on_freelist(rb_header, size)
+#define search_on_free(size) first_fit_on_freelist(raw_block, size)
 #endif /* BEST_FIT */ 
 
 size_t req_padding(size_t size);
 
+/** Tries to align the input to 32, 64, 128, 256 or to a multiple of 4 if it is
+ * larger.
+ *
+ * @param size The input number.
+ */
 size_t req_padding(size_t size) {
     size_t align;
 
@@ -62,23 +76,16 @@ size_t req_padding(size_t size) {
 }
 
 /** Tries to allocate memory from a specific free-list organized raw block.
- * @param raw_block The pointer of the raw block.
+ * @param raw_block The pointer of the freelist-organised raw block.
  * @param size      The requested size.
  * @retval          The address of the returned memory space.
  */
-void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
-    freelist_rb_t *rb_header;
+void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
     block_header_t *ptr;
     size_t allocation_size, previous_size, previous_size_availability;
 
-    rb_header = (freelist_rb_t *)((char *)raw_block +
-            sizeof(raw_block_header_t));
     ptr = NULL;
 
-#ifdef HAVE_LOCKS
-    pthread_mutex_lock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
-
     ptr = search_on_free(size);
 
     if(ptr != NULL) {
@@ -88,59 +95,56 @@ void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
 
         /* Try to split */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-        split(rb_header, ptr, size);
+        split(raw_block, ptr, size);
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
-        mark_used(rb_header, ptr);
+        mark_used(raw_block, ptr);
     } else {
 
         allocation_size = req_padding(size) + HEADER_SIZE;
 
-        if(allocation_size < (char *) raw_block + raw_block->size -
-                (char *) rb_header->border_ptr -
-                get_size(rb_header->border_ptr)) {
-            if(rb_header->border_ptr == (block_header_t *)((char *)rb_header +
-                        sizeof(freelist_rb_t))) {
+        if(allocation_size <= raw_block->remaining_size) {
+            if(raw_block->border_ptr == NULL) {
                 previous_size_availability = 1; // Occupied and of 0 size
-                ptr = (block_header_t *)((char *)rb_header +
+                ptr = (block_header_t *)((char *)raw_block +
                         sizeof(freelist_rb_t));
             } else {
-                previous_size = get_size(rb_header->border_ptr);
+                previous_size = get_size(raw_block->border_ptr);
                 previous_size_availability =
-                    get_size_availability(rb_header->border_ptr);
-                ptr = (block_header_t *)((char *)rb_header->border_ptr +
+                    get_size_availability(raw_block->border_ptr);
+                ptr = (block_header_t *)((char *)raw_block->border_ptr +
                         previous_size);
             }
 
-            rb_header->border_ptr = ptr;
+            // Update raw block metadata
+            raw_block->remaining_size -= allocation_size;
+            raw_block->border_ptr = ptr;
 
             // Update block metadata
-            set_size_and_used(rb_header, ptr, req_padding(size));
+            set_size_and_used(raw_block, ptr, req_padding(size));
             set_previous_size_availability(ptr, previous_size_availability);
 #ifdef REQUEST_SIZE_INFO
-            set_requested_size(ptr, req_padding(size));
+            set_requested_size(ptr, size);
 #endif /* REQUEST_SIZE_INFO */
         }
     }
 
-#ifdef WITH_RAWBLOCK_STATS
     if(ptr != NULL) {
-#if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
-        raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
-#endif /* !((SPLITTING_FIXED) || (SPLITTING_VARIABLE)) */
-        raw_block->dmm_stats.live_objects++;
-        raw_block->dmm_stats.num_malloc++;
+
+#ifdef WITH_ALLOCATOR_STATS
+            systemallocator.dmm_stats.total_mem_allocated +=
+                get_size(ptr) + HEADER_SIZE;
+            systemallocator.dmm_stats.live_objects++;
+            systemallocator.dmm_stats.num_malloc++;
+            TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+                    systemallocator.dmm_stats.total_mem_allocated);
 #ifdef REQUEST_SIZE_INFO
-        raw_block->dmm_stats.total_mem_requested += size;
+            systemallocator.dmm_stats.total_mem_requested += size;
+            TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+                    systemallocator.dmm_stats.total_mem_requested);
 #endif /* REQUEST_SIZE_INFO */
-    }
-#endif /* WITH_RAWBLOCK_STATS */
-
-#ifdef HAVE_LOCKS
-    pthread_mutex_unlock(&raw_block->mutex);
-#endif /* HAVE_LOCKS */
+#endif /* WITH_ALLOCATOR_STATS */
 
-    if(ptr != NULL) {
         return (void *)((char *)ptr + HEADER_SIZE);
     } else {
         return NULL;

+ 14 - 2
src/freelist/freelist_realloc.c

@@ -28,16 +28,20 @@
 
 #include <string.h>
 
+#ifdef HAVE_LOCKS
+#include <pthread.h>
+#endif /* HAVE_LOCKS */
+
 /**
  * Reallocates a memory block from a freelist-organized raw block
  *
- * @param  raw_block The pointer of the raw block.
+ * @param  raw_block The pointer of the freelist raw block.
  * @param  ptr       The pointer of the memory block to be re-allocated.
  * @param  req_size  The requested memory size.
  * @retval           The address to serve the request.
  * @retval NULL      No available memory space.
  */
-void * freelist_realloc(raw_block_header_t *raw_block, void * ptr,
+void * freelist_realloc(freelist_rb_t *raw_block, void * ptr,
         size_t req_size) { 
     block_header_t *block;
     void *ret;
@@ -47,6 +51,14 @@ void * freelist_realloc(raw_block_header_t *raw_block, void * ptr,
     ret = malloc(req_size);
     ret = memmove(ret, ptr,
             block->size);
+#ifdef HAVE_LOCKS
+    raw_block_header_t *rb;
+    rb = (raw_block_header_t *)((char *)raw_block - sizeof(raw_block_header_t));
+    pthread_mutex_lock(&rb->mutex);
+#endif /* HAVE_LOCKS */
     freelist_free(raw_block, ptr);
+#ifdef HAVE_LOCKS
+    pthread_mutex_unlock(&rb->mutex);
+#endif /* HAVE_LOCKS */
     return ret;
 }

+ 46 - 24
src/malloc.c

@@ -24,30 +24,29 @@
  */
 
 #include "dmmlib/dmmlib.h"
-#ifdef FL_RB_ONLY
-#include "freelist/freelist.h"
-#include "freelist/freelist_rb.h"
-#endif /* FL_RB_ONLY */
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
 #endif /* BITMAP_RB_ONLY */
 
-#include "trace.h"
+#ifdef FL_RB_ONLY
+#include "freelist/freelist.h"
+#include "freelist/freelist_rb.h"
+#endif /* FL_RB_ONLY */
 
-#ifdef WITH_DEBUG
-#include "debug.h"
-#endif /* WITH_DEBUG */
+#include "trace.h"
 
 void * malloc(size_t size) {
     raw_block_header_t *raw_block, *new_raw_block;
+#ifdef BITMAP_RB_ONLY
+    bitmap_rb_t
+#endif /* BITMAP_RB_ONLY */
 #ifdef FL_RB_ONLY
-    size_t allocation_size;
+    freelist_rb_t
 #endif /* FL_RB_ONLY */
+        *encapsulated_rb;
     void *ptr;
-#ifdef BITMAP_RB_ONLY
-    bitmap_rb_t *bitmap_rb;
-#endif /* BITMAP_RB_ONLY */
 
     raw_block = systemallocator.raw_block_head;
     ptr = NULL;
@@ -64,9 +63,15 @@ void * malloc(size_t size) {
 #ifdef HAVE_LOCKS
         pthread_mutex_lock(&raw_block->mutex);
 #endif /* HAVE_LOCKS */
-        bitmap_rb = (bitmap_rb_t *)((char *)raw_block +
-                sizeof(raw_block_header_t));
-        ptr = dmmlib_malloc(bitmap_rb, size);
+        encapsulated_rb =
+#ifdef BITMAP_RB_ONLY
+            (bitmap_rb_t *)
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+            (freelist_rb_t *)
+#endif /* FL_RB_ONLY */
+            ((char *)raw_block + sizeof(raw_block_header_t));
+        ptr = dmmlib_malloc(encapsulated_rb, size);
 #ifdef HAVE_LOCKS
         pthread_mutex_unlock(&raw_block->mutex);
 #endif /* HAVE_LOCKS */
@@ -77,11 +82,8 @@ void * malloc(size_t size) {
     }
 
     if(ptr == NULL) {
-#ifdef FL_RB_ONLY
-        allocation_size = size + sizeof(raw_block_header_t) +
-            sizeof(freelist_rb_t);
-#endif /* FL_RB_ONLY */
 
+#ifdef BITMAP_RB_ONLY
         /* Check if the request would fit in a new bitmap raw block
          * FIXME currently the raw block size and resolution are fixed
          */
@@ -93,6 +95,13 @@ void * malloc(size_t size) {
 
         if(2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
                 sizeof(bitmap_rb_t) - bm_vector_size) {
+#endif /* BITMAP_RB_ONLY */
+
+#ifdef FL_RB_ONLY
+        if( 2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
+                sizeof(freelist_rb_t)) {
+#endif /* FL_RB_ONLY */
+
             ptr = (void *)create_raw_block(size +
                     sizeof(raw_block_header_t), BIGBLOCK);
             if(ptr != NULL) {
@@ -119,28 +128,41 @@ void * malloc(size_t size) {
             return ptr;
         }
 
+#ifdef HAVE_LOCKS
         pthread_mutex_lock(&systemallocator.creation_mutex);
+#endif /* HAVE_LOCKS */
+        new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE,
 #ifdef FL_RB_ONLY
-        new_raw_block = create_raw_block(allocation_size, FREELIST);
+                FREELIST
 #endif /* FL_RB_ONLY */
 #ifdef BITMAP_RB_ONLY
-        new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE, BITMAP);
+                BITMAP
 #endif /* BITMAP_RB_ONLY */
+                );
         if(new_raw_block != NULL) {
             new_raw_block->next_raw_block = systemallocator.raw_block_head;
             systemallocator.raw_block_head = new_raw_block;
             pthread_mutex_unlock(&systemallocator.creation_mutex);
-            bitmap_rb = (bitmap_rb_t *)((char *)new_raw_block +
-                    sizeof(raw_block_header_t));
 #ifdef HAVE_LOCKS
             pthread_mutex_lock(&new_raw_block->mutex);
 #endif /* HAVE_LOCKS */
-            ptr = dmmlib_malloc(bitmap_rb, size);
+
+            encapsulated_rb =
+#ifdef BITMAP_RB_ONLY
+                (bitmap_rb_t *)
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+                (freelist_rb_t *)
+#endif /* FL_RB_ONLY */
+                ((char *)new_raw_block + sizeof(raw_block_header_t));
+            ptr = dmmlib_malloc(encapsulated_rb, size);
 #ifdef HAVE_LOCKS
             pthread_mutex_unlock(&new_raw_block->mutex);
 #endif /* HAVE_LOCKS */
         }
+#ifdef HAVE_LOCKS
         pthread_mutex_unlock(&systemallocator.creation_mutex);
+#endif /* HAVE_LOCKS */
     }
     
     return ptr;

+ 3 - 2
src/raw_block.c

@@ -53,8 +53,9 @@ raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
         case FREELIST:
             fl_rb = (freelist_rb_t *)((char *)ptr +
                     sizeof(raw_block_header_t));
-            fl_rb->border_ptr = (block_header_t *)((char *)fl_rb +
-                    sizeof(freelist_rb_t));
+            fl_rb->remaining_size = raw_block_size -
+                sizeof(raw_block_header_t) - sizeof(freelist_rb_t);
+            fl_rb->border_ptr = NULL;
             fl_rb->free_list_head = NULL;
             break;
 #endif /* FL_RB_ONLY */

+ 21 - 11
src/realloc.c

@@ -26,10 +26,17 @@
 #include "dmmlib/dmmlib.h"
 
 #include <stdbool.h>
+
 #ifdef BITMAP_RB_ONLY
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
 #endif /* BITMAP_RB_ONLY */
+
+#ifdef FL_RB_ONLY
+#include "freelist/freelist.h"
+#include "freelist/freelist_rb.h"
+#endif /* FL_RB_ONLY */
+
 #include "release_memory.h"
 
 void * realloc(void *ptr, size_t size) {
@@ -42,17 +49,12 @@ void * realloc(void *ptr, size_t size) {
 
     if(size == 0) {
         free(ptr);
-#ifdef BITMAP_RB_ONLY
-        return malloc(CHUNK_HDR_SIZE + 32); // FIXME 32 <- minimum size
-#endif /* BITMAP_RB_ONLY */
-#ifdef FL_RB_ONLY
         return malloc((size_t) 32); // FIXME 32 <- minimum size
-#endif /* FL_RB_ONLY */
     }
 
+    current_raw_block = systemallocator.raw_block_head;
     found = false;
 
-    current_raw_block = systemallocator.raw_block_head;
     while(current_raw_block) {
         if(((char *)ptr > (char *)current_raw_block) &&
                 ((char *)ptr < (char *)(current_raw_block) +
@@ -64,15 +66,23 @@ void * realloc(void *ptr, size_t size) {
     }
 
     if(found == true) {
+
+#ifdef BITMAP_RB_ONLY
+        bitmap_rb_t
+#endif /* BITMAP_RB_ONLY */
+#ifdef FL_RB_ONLY
+        freelist_rb_t
+#endif /* FL_RB_ONLY */
+            *encapsulated_rb = 
 #ifdef BITMAP_RB_ONLY
-        bitmap_rb_t *bitmap_rb;
-        bitmap_rb = (bitmap_rb_t *)((char *)current_raw_block +
-                sizeof(raw_block_header_t));
-        return bitmap_realloc(bitmap_rb, ptr, size);
+                (bitmap_rb_t *)
 #endif /* BITMAP_RB_ONLY */
 #ifdef FL_RB_ONLY
-        return freelist_realloc(current_raw_block, ptr, size);
+                (freelist_rb_t *)
 #endif /* FL_RB_ONLY */
+                ((char *)current_raw_block + sizeof(raw_block_header_t));
+        
+        return dmmlib_realloc(encapsulated_rb, ptr, size);
     } else {
         return NULL; // FIXME what about big blocks?
     }