Browse Source

Improve support for request size on the block header

Ioannis Koutras 13 years ago
parent
commit
473b1b276f

+ 1 - 0
CMakeLists.txt

@@ -52,6 +52,7 @@ elseif(SORT_POLICY STREQUAL "address")
   message(STATUS "Block sorting policy: Address-ordered")
   message(STATUS "Block sorting policy: Address-ordered")
 endif(SORT_POLICY STREQUAL "lifo")
 endif(SORT_POLICY STREQUAL "lifo")
 message(STATUS "Predefined lists of fixed-sized blocks: " ${WITH_FIXED_LISTS})
 message(STATUS "Predefined lists of fixed-sized blocks: " ${WITH_FIXED_LISTS})
+message(STATUS "Requested Size per Block: " ${REQUEST_SIZE_INFO})
 message(STATUS "Heap Ownership per Block: " ${WITH_OWNERSHIP})
 message(STATUS "Heap Ownership per Block: " ${WITH_OWNERSHIP})
 message(STATUS "Have statistics: " ${WITH_STATS})
 message(STATUS "Have statistics: " ${WITH_STATS})
 if (WITH_STATS)
 if (WITH_STATS)

+ 2 - 0
DefineOptions.cmake

@@ -14,6 +14,8 @@ option(WITH_DOC "Build with documentation" OFF)
 
 
 option(SORT_POLICY "Choose the block sorting policy, options are: lifo, fifo, size, address")
 option(SORT_POLICY "Choose the block sorting policy, options are: lifo, fifo, size, address")
 
 
+set(REQUEST_SIZE_INFO ON)
+
 set(NUM_HEAPS 1)
 set(NUM_HEAPS 1)
 
 
 if (NUM_HEAPS GREATER 1)
 if (NUM_HEAPS GREATER 1)

+ 2 - 0
dmm_config.h.in

@@ -40,6 +40,8 @@
 
 
 #cmakedefine MIN_SPLITTING_SIZE @MIN_SPLITTING_SIZE@
 #cmakedefine MIN_SPLITTING_SIZE @MIN_SPLITTING_SIZE@
 
 
+#cmakedefine REQUEST_SIZE_INFO
+
 #cmakedefine WITH_STATS
 #cmakedefine WITH_STATS
 #cmakedefine COUNT_ACCESSES
 #cmakedefine COUNT_ACCESSES
 #cmakedefine COUNT_HOPS
 #cmakedefine COUNT_HOPS

+ 2 - 2
include/dmmlib/heap.h

@@ -70,9 +70,9 @@ typedef struct maptable_node_s {
 #ifdef WITH_STATS
 #ifdef WITH_STATS
 /** Statistics data structure. */
 /** Statistics data structure. */
 typedef struct dmmstats_s {
 typedef struct dmmstats_s {
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
 	size_t mem_requested; /**< Total memory currently requested. */
 	size_t mem_requested; /**< Total memory currently requested. */
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
 	size_t mem_used; /**< Total memory actively used (holds allocated
 	size_t mem_used; /**< Total memory actively used (holds allocated
 			   blocks). */
 			   blocks). */
 	size_t mem_allocated; /**< Total managed memory (heap size, malloc and
 	size_t mem_allocated; /**< Total managed memory (heap size, malloc and

+ 3 - 3
include/dmmlib/print_stats.h

@@ -23,9 +23,9 @@
 size_t get_used_space(heap_t *heap);
 size_t get_used_space(heap_t *heap);
 size_t get_allocated_space(heap_t *heap);
 size_t get_allocated_space(heap_t *heap);
 
 
-#ifdef FUTURE_FEATURES
-uint32_t get_requested_space(heap_t *heap);
-#endif /* FUTURE_FEATURES */
+#ifdef REQUEST_SIZE_INFO
+size_t get_requested_space(heap_t *heap);
+#endif /* REQUEST_SIZE_INFO */
 
 
 void print_stats(allocator_t *allocator);
 void print_stats(allocator_t *allocator);
 
 

+ 14 - 15
private-include/block_header.h

@@ -38,9 +38,9 @@ typedef struct __attribute__((__aligned__(32))) block_header_s {
     size_t size; /**< The LSB represents the availability of the block (1
     size_t size; /**< The LSB represents the availability of the block (1
                    for used, 0 for free), the rest the size of the data
                    for used, 0 for free), the rest the size of the data
                    part. */
                    part. */
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
     size_t requested_size; /**< The requested size of the data part */
     size_t requested_size; /**< The requested size of the data part */
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
     size_t previous_size; /**< The LSB represents the availability of the
     size_t previous_size; /**< The LSB represents the availability of the
                             previous block, the rest the size of the data
                             previous block, the rest the size of the data
                             part of the previous block in the memory space */
                             part of the previous block in the memory space */
@@ -73,7 +73,17 @@ block_header_t * get_header(void *ptr);
  */
  */
 size_t get_size(void *ptr);
 size_t get_size(void *ptr);
 
 
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
+
+/**
+ * Set the requested size of memory block's data
+ *
+ * \param ptr 	The pointer to the data part of the current memory block.
+ * \param size 	The requested size for the data part of the current memory
+ * block.
+ */
+void set_requested_size(void *ptr, size_t size);
+
 /**
 /**
  * Get the requested size of the memory block's data
  * Get the requested size of the memory block's data
  *
  *
@@ -83,7 +93,7 @@ size_t get_size(void *ptr);
  * block.
  * block.
  */
  */
 size_t get_requested_size(void *ptr);
 size_t get_requested_size(void *ptr);
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
 
 
 /**
 /**
  * Get all information of the memory block header's size record
  * Get all information of the memory block header's size record
@@ -113,17 +123,6 @@ void set_size_and_free(allocator_t *allocator, void *ptr, size_t size);
  */
  */
 void set_size_and_used(allocator_t *allocator, void *ptr, size_t size);
 void set_size_and_used(allocator_t *allocator, void *ptr, size_t size);
 
 
-#ifdef FUTURE_FEATURES
-/**
- * Set the requested size of memory block's data
- *
- * \param ptr 	The pointer to the data part of the current memory block.
- * \param size 	The requested size for the data part of the current memory
- * block.
- */
-void set_requested_size(void *ptr, size_t size);
-#endif /* FUTURE_FEATURES */
-
 /**
 /**
  * Mark the memory block as used, as well as the previous_size element of the
  * Mark the memory block as used, as well as the previous_size element of the
  * next block if there is one.
  * next block if there is one.

+ 6 - 8
src/block_header.c

@@ -25,11 +25,15 @@ size_t get_size(void *ptr) {
     return get_header(ptr)->size >> 1;
     return get_header(ptr)->size >> 1;
 }
 }
 
 
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
+void set_requested_size(void *ptr, size_t size) {
+    get_header(ptr)->requested_size = size;
+}
+
 size_t get_requested_size(void *ptr) {
 size_t get_requested_size(void *ptr) {
     return get_header(ptr)->requested_size;
     return get_header(ptr)->requested_size;
 }
 }
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
 
 
 size_t get_size_availability(void *ptr) {
 size_t get_size_availability(void *ptr) {
     return get_header(ptr)->size;
     return get_header(ptr)->size;
@@ -60,12 +64,6 @@ void set_size_and_used(allocator_t *allocator, void *ptr, size_t size) {
     }
     }
 }
 }
 
 
-#ifdef FUTURE_FEATURES
-void set_requested_size(void *ptr, size_t size) {
-    get_header(ptr)->requested_size = size;
-}
-#endif /* FUTURE_FEATURES */
-
 void mark_used(allocator_t *allocator, void *ptr) {
 void mark_used(allocator_t *allocator, void *ptr) {
     block_header_t *next_block_header;
     block_header_t *next_block_header;
 
 

+ 4 - 0
src/custom_free.c

@@ -55,6 +55,10 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
     posix_lock(heap);
     posix_lock(heap);
 #endif /* HAVE_LOCKS */
 #endif /* HAVE_LOCKS */
 
 
+#if defined (WITH_STATS) && defined (REQUEST_SIZE_INFO)
+    heap->dmm_stats.mem_requested -= get_requested_size(ptr);
+#endif /* WITH_STATS && REQUEST_SIZE_INFO */
+
 #ifdef COUNT_HOPS
 #ifdef COUNT_HOPS
     /* Initialize the hops counter */
     /* Initialize the hops counter */
     heap->dmm_stats.total_hops = 0;
     heap->dmm_stats.total_hops = 0;

+ 4 - 4
src/custom_malloc.c

@@ -79,9 +79,9 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 
 
     if(ptr != NULL) {
     if(ptr != NULL) {
 
 
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
         set_requested_size(ptr, size);
         set_requested_size(ptr, size);
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
 
 
         /* Try to split */
         /* Try to split */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
@@ -117,9 +117,9 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 
 
 #ifdef WITH_STATS
 #ifdef WITH_STATS
         /* Update statistics */
         /* Update statistics */
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
         heap->dmm_stats.mem_requested += size;
         heap->dmm_stats.mem_requested += size;
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
         heap->dmm_stats.mem_used += HEADER_SIZE + get_size(ptr);
         heap->dmm_stats.mem_used += HEADER_SIZE + get_size(ptr);
         heap->dmm_stats.live_objects += 1;
         heap->dmm_stats.live_objects += 1;
         heap->dmm_stats.num_malloc += 1;
         heap->dmm_stats.num_malloc += 1;

+ 12 - 0
src/custom_realloc.c

@@ -78,6 +78,9 @@ void * clean_realloc(allocator_t *allocator, heap_t *heap, void *ptr,
 void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size) {
 void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size) {
     void *next_block;
     void *next_block;
     size_t old_size;
     size_t old_size;
+#if defined (REQUEST_SIZE_INFO) && defined (WITH_STATS)
+    size_t old_requested_size;
+#endif /* (REQUEST_SIZE_INFO) && (WITH_STATS) */
 
 
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
     size_t min_split_size;
     size_t min_split_size;
@@ -88,6 +91,15 @@ void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t
         if(old_size > size) {
         if(old_size > size) {
             /* The old size is bigger than the new, use the old block */
             /* The old size is bigger than the new, use the old block */
 
 
+#ifdef REQUEST_SIZE_INFO
+#ifdef WITH_STATS
+            old_requested_size = get_requested_size(ptr);
+            heap->dmm_stats.mem_requested += old_requested_size;
+            heap->dmm_stats.mem_requested += size;
+#endif /* WITH_STATS */
+            set_requested_size(ptr, size);
+#endif /* REQUEST_SIZE_INFO */
+
             /* Try to split the currently allocated block */
             /* Try to split the currently allocated block */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
 #ifdef SPLITTING_FIXED
 #ifdef SPLITTING_FIXED

+ 7 - 7
src/print_stats.c

@@ -28,11 +28,11 @@ size_t get_used_space(heap_t *heap) {
     return heap->dmm_stats.mem_used;
     return heap->dmm_stats.mem_used;
 }
 }
 
 
-#ifdef FUTURE_FEATURES
-uint32_t get_requested_space(heap_t *heap) {
+#ifdef REQUEST_SIZE_INFO
+size_t get_requested_space(heap_t *heap) {
     return heap->dmm_stats.mem_requested;
     return heap->dmm_stats.mem_requested;
 }
 }
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
 
 
 void print_stats(allocator_t *allocator) {
 void print_stats(allocator_t *allocator) {
     int i;
     int i;
@@ -43,13 +43,13 @@ void print_stats(allocator_t *allocator) {
                 (int) allocator->heaps[i].dmm_stats.num_malloc);
                 (int) allocator->heaps[i].dmm_stats.num_malloc);
         printf("dmmlib - Total free() calls: %d\n",
         printf("dmmlib - Total free() calls: %d\n",
                 (int) allocator->heaps[i].dmm_stats.num_free);
                 (int) allocator->heaps[i].dmm_stats.num_free);
+#ifdef REQUEST_SIZE_INFO
+        printf("dmmlib - Memory currently requested: %zu\n",
+                get_requested_space(&allocator->heaps[i]));
+#endif /* REQUEST_SIZE_INFO */
         printf("dmmlib - Memory currently used: %zu\n",
         printf("dmmlib - Memory currently used: %zu\n",
                 get_used_space(&allocator->heaps[i]));
                 get_used_space(&allocator->heaps[i]));
         printf("dmmlib - Memory currently allocated: %zu\n",
         printf("dmmlib - Memory currently allocated: %zu\n",
                 get_allocated_space(&allocator->heaps[i]));
                 get_allocated_space(&allocator->heaps[i]));
-#ifdef FUTURE_FEATURES
-        printf("dmmlib - Memory currently requested: %d\n",
-                (int) allocator->heaps[i].dmm_stats.mem_requested);
-#endif /* FUTURE_FEATURES */
     }
     }
 }
 }

+ 5 - 3
src/sys_alloc.c

@@ -148,18 +148,20 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     set_size_and_used(allocator, ptr, req_padding(size));
     set_size_and_used(allocator, ptr, req_padding(size));
     set_previous_size_availability(ptr, previous_size_availability);
     set_previous_size_availability(ptr, previous_size_availability);
 
 
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
     set_requested_size(ptr, size);
     set_requested_size(ptr, size);
+#endif /* REQUEST_SIZE_INFO */
 
 
+#ifdef FUTURE_FEATURES
     /* Update the used blocks list */
     /* Update the used blocks list */
     push_block(&ptr, &heap->used_blocks_head);
     push_block(&ptr, &heap->used_blocks_head);
 #endif /* FUTURE_FEATURES */
 #endif /* FUTURE_FEATURES */
 
 
 #ifdef WITH_STATS
 #ifdef WITH_STATS
     /* Update statistics */
     /* Update statistics */
-#ifdef FUTURE_FEATURES
+#ifdef REQUEST_SIZE_INFO
     heap->dmm_stats.mem_requested += size;
     heap->dmm_stats.mem_requested += size;
-#endif /* FUTURE_FEATURES */
+#endif /* REQUEST_SIZE_INFO */
     heap->dmm_stats.mem_used += HEADER_SIZE + req_padding(size);
     heap->dmm_stats.mem_used += HEADER_SIZE + req_padding(size);
     heap->dmm_stats.mem_allocated += HEADER_SIZE + req_padding(size);
     heap->dmm_stats.mem_allocated += HEADER_SIZE + req_padding(size);
     heap->dmm_stats.live_objects += 1;
     heap->dmm_stats.live_objects += 1;