ソースを参照

rewrite macros to refactor some code
* * *
correct the config header in remaining files

Ioannis Koutras 12 年 前
コミット
32df8bd4fe
共有12 個のファイルを変更した106 個の追加136 個の削除を含む
  1. 7 3
      private-include/debug.h
  2. 9 0
      private-include/freelist/block_header_funcs.h
  3. 14 3
      private-include/freelist/split.h
  4. 29 3
      private-include/statistics.h
  5. 6 17
      src/bitmap/free.c
  6. 4 15
      src/bitmap/malloc.c
  7. 3 13
      src/free.c
  8. 8 17
      src/freelist/free.c
  9. 12 29
      src/freelist/malloc.c
  10. 4 16
      src/malloc.c
  11. 10 17
      src/memalign.c
  12. 0 3
      src/realloc.c

+ 7 - 3
private-include/debug.h

@@ -22,13 +22,17 @@
  * @brief  Debug functions
  */
 
-#ifndef DEBUG_H
-#define DEBUG_H
+#ifndef DMMLIB_DEBUG_H_
+#define DMMLIB_DEBUG_H_
 #include <dmmlib/config.h>
 
+#ifdef WITH_DEBUG
+
 #include "dmmlib/allocator.h"
 #include "dmmlib/raw_block.h"
 
 void get_raw_blocks(allocator_t *allocator);
 
-#endif /* DEBUG_H */
+#endif /* WITH_DEBUG */
+
+#endif /* DMMLIB_DEBUG_H_ */

+ 9 - 0
private-include/freelist/block_header_funcs.h

@@ -46,6 +46,10 @@ size_t get_size(block_header_t *ptr);
 
 #ifdef REQUEST_SIZE_INFO
 
+/** Macro to set the requested size to the header of a free-list's memory
+ * block */
+#define SET_REQUESTED_SIZE(ptr, size) set_requested_size(ptr, size)
+
 /**
  * Set the requested size of memory block's data
  *
@@ -64,6 +68,11 @@ void set_requested_size(block_header_t *ptr, size_t size);
  * block.
  */
 size_t get_requested_size(block_header_t *ptr);
+#else /* REQUEST_SIZE_INFO */
+
+/** Does nothing */
+#define SET_REQUESTED_SIZE(...)
+
 #endif /* REQUEST_SIZE_INFO */
 
 /**

+ 14 - 3
private-include/freelist/split.h

@@ -15,13 +15,18 @@
  *
  */
 
-#ifndef SPLIT_H
-#define SPLIT_H
+#ifndef DMMLIB_SPLIT_H_
+#define DMMLIB_SPLIT_H_
 #include <dmmlib/config.h>
 
+#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
+
 #include "dmmlib/freelist/freelist_rb.h"
 #include "dmmlib/freelist/block_header.h"
 
+/** Macro to split a memory block */
+#define SPLIT(freelist_rb, ptr, size) split(freelist_rb, ptr, size)
+
 /**
  * Splits a memory block to two blocks: one with the requested size and the
  * another one with the remaining space.
@@ -33,4 +38,10 @@
  */ 
 void split(freelist_rb_t *raw_block, block_header_t *ptr, size_t req_size);
 
-#endif /* SPLIT_H */
+#else /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+
+/** Does nothing */
+#define SPLIT(...)
+
+#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+#endif /* DMMLIB_SPLIT_H_ */

+ 29 - 3
private-include/statistics.h

@@ -22,12 +22,31 @@
  * @brief  Statistics function.
  */
 
-#ifndef STATISTICS_H
-#define STATISTICS_H
+#ifndef DMMLIB_STATISTICS_H_
+#define DMMLIB_STATISTICS_H_
 #include <dmmlib/config.h>
 
+#ifdef WITH_ALLOCATOR_STATS
+
 #include <inttypes.h>
+
+#include "dmmlib/dmmlib.h"
 #include "dmmlib/dmmstats.h"
+#include "locks.h"
+
+#ifdef REQUEST_SIZE_INFO
+/** Updates the global statistics for specific dmmlib events */
+#define UPDATE_GLOBAL_STATS(EVENT_TYPE, REQ_SIZE) \
+    LOCK_GLOBAL(); \
+    update_stats(&systemallocator.dmm_stats, EVENT_TYPE, REQ_SIZE); \
+    UNLOCK_GLOBAL();
+#else /* REQUEST_SIZE_INFO */
+/** Updates the global statistics for specific dmmlib events */
+#define UPDATE_GLOBAL_STATS(EVENT_TYPE) \
+    LOCK_GLOBAL(); \
+    update_stats(&systemallocator.dmm_stats, EVENT_TYPE); \
+    UNLOCK_GLOBAL();
+#endif /* REQUEST_SIZE_INFO */
 
 /** DMM event type enumeration */
 typedef enum dmm_event_type_en {
@@ -43,4 +62,11 @@ void update_stats
 #endif /* REQUEST_SIZE_INFO */
   );
 
-#endif /* STATISTICS_H */
+#else /* WITH_ALLOCATOR_STATS */
+
+/** Does nothing */
+#define UPDATE_GLOBAL_STATS(...)
+
+#endif /* WITH_ALLOCATOR_STATS */
+
+#endif /* DMMLIB_STATISTICS_H_ */

+ 6 - 17
src/bitmap/free.c

@@ -22,18 +22,13 @@
  * @brief  free() implementation for bitmap-organized raw blocks
  */
 
+#include "statistics.h"
+#include "trace.h"
+
 #include "bitmap/bitmap.h"
 #include "bitmap/bitmap_rb.h"
 #include "bitmap/bitmap_other.h"
 
-#include "trace.h"
-
-#ifdef WITH_ALLOCATOR_STATS
-#include "locks.h"
-#include "statistics.h"
-#include "dmmlib/dmmlib.h"
-#endif /* WITH_ALLOCATOR_STATS */
-
 /** Frees the memory block inside of a specific bitmap-organized raw block.
  * @param raw_block The pointer of the bitmap raw block.
  * @param ptr       The pointer of the memory block to be freed.
@@ -70,16 +65,10 @@ void bitmap_free(bitmap_rb_t *raw_block, void *ptr) {
         }
     }
 
-#ifdef WITH_ALLOCATOR_STATS
-    LOCK_GLOBAL();
-    update_stats
-        ( &systemallocator.dmm_stats
-        , FREE
 #ifdef REQUEST_SIZE_INFO
-        , chunk_header->requested_size
+    UPDATE_GLOBAL_STATS(FREE, chunk_header->requested_size);
+#else /* REQUEST_SIZE_INFO */
+    UPDATE_GLOBAL_STATS(FREE);
 #endif /* REQUEST_SIZE_INFO */
-        );
-    UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
 }

+ 4 - 15
src/bitmap/malloc.c

@@ -26,13 +26,8 @@
 #include "bitmap/bitmap_rb.h"
 #include "bitmap/bitmap_other.h"
 
-#include "trace.h"
-
-#ifdef WITH_ALLOCATOR_STATS
-#include "locks.h"
 #include "statistics.h"
-#include "dmmlib/dmmlib.h"
-#endif /* WITH_ALLOCATOR_STATS */
+#include "trace.h"
 
 /**
  * Returns a memory block from a bitmap-organized raw block
@@ -124,17 +119,11 @@ void * bitmap_malloc(bitmap_rb_t *raw_block, size_t req_size) {
             ret = (void *)((char *)chunk_address + CHUNK_HDR_SIZE);
 
             // 5. Statistics support
-#ifdef WITH_ALLOCATOR_STATS
-            LOCK_GLOBAL();
-            update_stats
-                ( &systemallocator.dmm_stats
-                , MALLOC
 #ifdef REQUEST_SIZE_INFO
-                , req_size - CHUNK_HDR_SIZE
+            UPDATE_GLOBAL_STATS(MALLOC, req_size - CHUNK_HDR_SIZE);
+#else /* REQUEST_SIZE_INFO */
+            UPDATE_GLOBAL_STATS(MALLOC);
 #endif /* REQUEST_SIZE_INFO */
-                );
-            UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
             break;
         }

+ 3 - 13
src/free.c

@@ -23,25 +23,15 @@
  * @brief  Implementation of the free() call.
  */
 
-#include "dmmlib/dmmlib.h"
-
 #include <inttypes.h>
 
-#include "locks.h"
+#include "dmmlib/dmmlib.h"
 
 #include "default_rb.h"
+#include "locks.h"
 #include "other.h"
-
-#ifdef WITH_ALLOCATOR_STATS
-#include "statistics.h"
-#endif /* WITH_ALLOCATOR_STATS */
-
-#ifdef WITH_DEBUG
-#include "debug.h"
-#endif /* WITH_DEBUG */
-
 #include "release_memory.h"
-
+#include "statistics.h"
 #include "trace.h"
 
 void free(void *ptr) {

+ 8 - 17
src/freelist/free.c

@@ -21,7 +21,10 @@
  * @brief  free() implementation for freelist-organised raw blocks
  */
 
-#include "dmmlib/freelist/freelist.h"
+#include <dmmlib/config.h>
+
+#include "statistics.h"
+#include "trace.h"
 
 #include "freelist/block_header_funcs.h"
 #include "freelist/ordering_policy.h"
@@ -29,13 +32,7 @@
 #include "freelist/coalesce.h"
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
 
-#include "trace.h"
-
-#ifdef WITH_ALLOCATOR_STATS
-#include "locks.h"
-#include "statistics.h"
-#include "dmmlib/dmmlib.h"
-#endif /* WITH_ALLOCATOR_STATS */
+#include "dmmlib/freelist/freelist.h"
 
 /** Frees the memory block inside of a specific free-list organized raw block.
  * @param raw_block The pointer of the freelist raw block.
@@ -48,17 +45,11 @@ void freelist_free(freelist_rb_t *raw_block, void *ptr) {
     // Memory stats get updated here in case the space gets coalesced with its
     // free neighbors.
 
-#ifdef WITH_ALLOCATOR_STATS
-    LOCK_GLOBAL();
-    update_stats
-        ( &systemallocator.dmm_stats
-        , FREE
 #ifdef REQUEST_SIZE_INFO
-        , get_requested_size(block)
+    UPDATE_GLOBAL_STATS(FREE, get_requested_size(block));
+#else /* REQUEST_SIZE_INFO */
+    UPDATE_GLOBAL_STATS(FREE);
 #endif /* REQUEST_SIZE_INFO */
-        );
-    UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     coalesce(raw_block, block);

+ 12 - 29
src/freelist/malloc.c

@@ -22,24 +22,19 @@
  * @brief  malloc() implementation for freelist-organised raw blocks
  */
 
-#include "dmmlib/freelist/freelist.h"
 
 #include <inttypes.h>
 
-#include "freelist/fitting_policy.h"
-#include "freelist/block_header_funcs.h"
+#include <dmmlib/config.h>
 
-#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-#include "freelist/split.h"
-#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
+#include "dmmlib/freelist/freelist.h"
 
+#include "statistics.h"
 #include "trace.h"
 
-#ifdef WITH_ALLOCATOR_STATS
-#include "locks.h"
-#include "statistics.h"
-#include "dmmlib/dmmlib.h"
-#endif /* WITH_ALLOCATOR_STATS */
+#include "freelist/block_header_funcs.h"
+#include "freelist/fitting_policy.h"
+#include "freelist/split.h"
 
 size_t req_padding(size_t size);
 
@@ -80,14 +75,10 @@ void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
     ptr = SEARCH_LIST(raw_block, size);
 
     if(ptr != NULL) {
-#ifdef REQUEST_SIZE_INFO
-        set_requested_size(ptr, size);
-#endif /* REQUEST_SIZE_INFO */
+        SET_REQUESTED_SIZE(ptr, size);
 
         /* Try to split */
-#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-        split(raw_block, ptr, size);
-#endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
+        SPLIT(raw_block, ptr, size);
 
         mark_used(raw_block, ptr);
     } else {
@@ -114,25 +105,17 @@ void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
             // Update block metadata
             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, size);
-#endif /* REQUEST_SIZE_INFO */
+            SET_REQUESTED_SIZE(ptr, size);
         }
     }
 
     if(ptr != NULL) {
 
-#ifdef WITH_ALLOCATOR_STATS
-        LOCK_GLOBAL();
-        update_stats
-            ( &systemallocator.dmm_stats
-            , MALLOC
 #ifdef REQUEST_SIZE_INFO
-            , size
+        UPDATE_GLOBAL_STATS(MALLOC, size);
+#else /* REQUEST_SIZE_INFO */
+        UPDATE_GLOBAL_STATS(MALLOC);
 #endif /* REQUEST_SIZE_INFO */
-            );
-        UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
         return (void *)((uintptr_t) ptr + HEADER_SIZE);
     } else {

+ 4 - 16
src/malloc.c

@@ -33,14 +33,8 @@
 #include "locks.h"
 #include "default_rb.h"
 
-#ifdef WITH_ALLOCATOR_STATS
-#include "statistics.h"
-#endif /* WITH_ALLOCATOR_STATS */
-
-#ifdef WITH_DEBUG
 #include "debug.h"
-#endif /* WITH_DEBUG */
-
+#include "statistics.h"
 #include "trace.h"
 
 void * malloc(size_t size) {
@@ -113,17 +107,11 @@ void * malloc(size_t size) {
                 UNLOCK_GLOBAL();
 #endif /* WITH_DEBUG */
 
-#ifdef WITH_ALLOCATOR_STATS
-                LOCK_GLOBAL();
-                update_stats
-                    ( &systemallocator.dmm_stats
-                    , MALLOC
 #ifdef REQUEST_SIZE_INFO
-                    , size
+                UPDATE_GLOBAL_STATS(MALLOC, size);
+#else /* REQUEST_SIZE_INFO */
+                UPDATE_GLOBAL_STATS(MALLOC);
 #endif /* REQUEST_SIZE_INFO */
-                    );
-                UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
                 ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
             }

+ 10 - 17
src/memalign.c

@@ -30,21 +30,20 @@
 #include <sys/mman.h>
 #include <assert.h>
 
-#include <dmmlib/config.h>
 #include "dmmlib/dmmlib.h"
+
 #include "memcpy.h"
-#include "locks.h"
-#include "default_rb.h"
-#include "freelist/block_header_funcs.h"
 
-#ifdef WITH_ALLOCATOR_STATS
+#include <dmmlib/config.h>
+#include "locks.h"
 #include "statistics.h"
-#endif /* WITH_ALLOCATOR_STATS */
-
 #include "trace.h"
 
+#include "default_rb.h"
+#include "freelist/block_header_funcs.h"
+
 #ifdef BITMAP_RB_ONLY
-#error Current memory-aligned allocation implementation support only \
+#error Current memory-aligned allocation implementation supports only \
 freelist-organized raw blocks.
 #endif /* BITMAP_RB_ONLY */
 
@@ -170,17 +169,11 @@ CheckAlignment:
                 UNLOCK_GLOBAL();
 #endif /* WITH_DEBUG */
 
-#ifdef WITH_ALLOCATOR_STATS
-                LOCK_GLOBAL();
-                update_stats
-                    ( &systemallocator.dmm_stats
-                    , MALLOC
 #ifdef REQUEST_SIZE_INFO
-                    , size
+                UPDATE_GLOBAL_STATS(MALLOC, size);
+#else /* REQUEST_SIZE_INFO */
+                UPDATE_GLOBAL_STATS(MALLOC);
 #endif /* REQUEST_SIZE_INFO */
-                    );
-                UNLOCK_GLOBAL();
-#endif /* WITH_ALLOCATOR_STATS */
 
                 *memptr = (void *)((uintptr_t) *memptr + sizeof(raw_block_header_t));
 

+ 0 - 3
src/realloc.c

@@ -35,10 +35,7 @@
 
 #include "memcpy.h"
 
-#ifdef WITH_DEBUG
 #include "debug.h"
-#endif /* WITH_DEBUG */
-
 #include "trace.h"
 
 void * realloc(void *ptr, size_t size) {