Bladeren bron

rewrite the knobs initialization as a static one, rename fitpercentage to fit_percentage, min_coalesce_size to min_coal_size and max_splitting_size to max_split_size

Ioannis Koutras 12 jaren geleden
bovenliggende
commit
d1c4bf913b

+ 20 - 20
CMakeLists.txt

@@ -62,26 +62,26 @@ if(RAW_BLOCKS_TYPE STREQUAL "freelist")
   endif(ORDERING_POLICY STREQUAL "address")
   
   if(WITH_COALESCING STREQUAL "fixed")
-    if(NOT DEFINED MAX_COALESCE_SIZE)
-      message(FATAL_ERROR "You have to set MAX_COALESCE_SIZE by using -DMAX_COALESCE_SIZE={int}.")
-    endif(NOT DEFINED MAX_COALESCE_SIZE)
+    if(NOT DEFINED MAX_COAL_SIZE)
+      message(FATAL_ERROR "You have to set MAX_COAL_SIZE by using -DMAX_COAL_SIZE={int}.")
+    endif(NOT DEFINED MAX_COAL_SIZE)
     set(COALESCING_FIXED ON)
   elseif(WITH_COALESCING STREQUAL "variable")
-    if(NOT DEFINED MAX_COALESCE_SIZE)
-      message(FATAL_ERROR "You have to set MAX_COALESCE_SIZE by using -DMAX_COALESCE_SIZE={int}.")
-    endif(NOT DEFINED MAX_COALESCE_SIZE)
+    if(NOT DEFINED MAX_COAL_SIZE)
+      message(FATAL_ERROR "You have to set MAX_COAL_SIZE by using -DMAX_COAL_SIZE={int}.")
+    endif(NOT DEFINED MAX_COAL_SIZE)
     set(COALESCING_VARIABLE ON)
   endif(WITH_COALESCING STREQUAL "fixed")
   
   if(WITH_SPLITTING STREQUAL "fixed")
-    if(NOT DEFINED MIN_SPLITTING_SIZE)
-      message(FATAL_ERROR "You have to set MIN_SPLITTING_SIZE by using -DMIN_SPLITTING_SIZE={int}.")
-    endif(NOT DEFINED MIN_SPLITTING_SIZE)
+    if(NOT DEFINED MIN_SPLIT_SIZE)
+      message(FATAL_ERROR "You have to set MIN_SPLIT_SIZE by using -DMIN_SPLIT_SIZE={int}.")
+    endif(NOT DEFINED MIN_SPLIT_SIZE)
     set(SPLITTING_FIXED ON)
   elseif(WITH_SPLITTING STREQUAL "variable")
-    if(NOT DEFINED MIN_SPLITTING_SIZE)
-      message(FATAL_ERROR "You have to set MIN_SPLITTING_SIZE by using -DMIN_SPLITTING_SIZE={int}.")
-    endif(NOT DEFINED MIN_SPLITTING_SIZE)
+    if(NOT DEFINED MIN_SPLIT_SIZE)
+      message(FATAL_ERROR "You have to set MIN_SPLIT_SIZE by using -DMIN_SPLIT_SIZE={int}.")
+    endif(NOT DEFINED MIN_SPLIT_SIZE)
     set(WITH_KNOBS ON)
     set(SPLITTING_VARIABLE ON)
   endif(WITH_SPLITTING STREQUAL "fixed")
@@ -159,27 +159,27 @@ if(RAW_BLOCKS_TYPE STREQUAL "freelist")
   message(STATUS "Ordering policy: " ${ORDERING_POLICY})
 
   message(STATUS "Coalescing: " ${WITH_COALESCING})
-  if(MAX_COALESCE_SIZE)
+  if(MAX_COAL_SIZE)
     if(WITH_COALESCING STREQUAL "fixed")
-      message(STATUS "  with max. coalescing size: " ${MAX_COALESCE_SIZE} " bytes")
+      message(STATUS "  with max. coalescing size: " ${MAX_COAL_SIZE} " bytes")
     elseif(WITH_COALESCING STREQUAL "variable")
-      message(STATUS "  with initial max. coalescing size: " ${MAX_COALESCE_SIZE}
+      message(STATUS "  with initial max. coalescing size: " ${MAX_COAL_SIZE}
         " bytes")
     endif(WITH_COALESCING STREQUAL "fixed")
-  endif(MAX_COALESCE_SIZE)
+  endif(MAX_COAL_SIZE)
 
   message(STATUS "Splitting: " ${WITH_SPLITTING})
-  if(MIN_SPLITTING_SIZE)
+  if(MIN_SPLIT_SIZE)
     if(WITH_SPLITTING STREQUAL "fixed")
-      message(STATUS "  with min. splitting size: " ${MIN_SPLITTING_SIZE} " bytes")
+      message(STATUS "  with min. splitting size: " ${MIN_SPLIT_SIZE} " bytes")
     elseif(WITH_SPLITTING STREQUAL "variable")
       message(STATUS "  with initial min. splitting size: "
-        ${MIN_SPLITTING_SIZE} " bytes")
+        ${MIN_SPLIT_SIZE} " bytes")
     endif(WITH_SPLITTING STREQUAL "fixed")
     if(COALESCE_AFTER_SPLIT)
       message(STATUS "Coalesce after split: " ${COALESCE_AFTER_SPLIT})
     endif(COALESCE_AFTER_SPLIT)
-  endif(MIN_SPLITTING_SIZE)
+  endif(MIN_SPLIT_SIZE)
 
 elseif(RAW_BLOCKS_TYPE STREQUAL "bitmap")
   message(STATUS "Bitmap cell resolution: " ${BITMAP_RESOLUTION} " bytes")

+ 2 - 2
DefineOptions.cmake

@@ -115,9 +115,9 @@ if(LINUX)
   set(FITTING_POLICY "good")
   set(GOOD_FIT_PERCENTAGE 0.8)
   set(WITH_COALESCING "variable")
-  set(MAX_COALESCE_SIZE 60000)
+  set(MAX_COAL_SIZE 60000)
   set(WITH_SPLITTING "variable")
-  set(MIN_SPLITTING_SIZE 64)
+  set(MIN_SPLIT_SIZE 64)
   set(FREELIST_COALESCE_AFTER_SPLIT ON)
   set(WITH_REALLOC ON)
   set(WITH_CALLOC ON)

+ 2 - 2
dmm_config.h.in

@@ -75,7 +75,7 @@
 #cmakedefine COALESCING_VARIABLE
 
 /** Default maximum coalescing size */
-#cmakedefine MAX_COALESCE_SIZE @MAX_COALESCE_SIZE@
+#cmakedefine MAX_COAL_SIZE @MAX_COAL_SIZE@
 
 /* Splitting Settings */
 
@@ -85,7 +85,7 @@
 #cmakedefine SPLITTING_VARIABLE
 
 /** Default minimum splitting size */
-#cmakedefine MIN_SPLITTING_SIZE @MIN_SPLITTING_SIZE@
+#cmakedefine MIN_SPLIT_SIZE @MIN_SPLIT_SIZE@
 
 /** Support to coalesce after splitting */
 #cmakedefine COALESCE_AFTER_SPLIT

+ 12 - 12
examples/test_for_memory_space_aware.c

@@ -5,7 +5,7 @@
 #include <dmmlib/dmmlib.h>
 #include <dmm_config.h>
 
-#define ALLOCATOR_SIZE 2*MAX_COALESCE_SIZE*sizeof(char)
+#define ALLOCATOR_SIZE 2*MAX_COAL_SIZE*sizeof(char)
 
 #define cmalloc(size) custom_ahmalloc(&systemallocator, &systemallocator.heaps[0], (size_t) size)
 #define cfree(ptr) custom_ahfree(&systemallocator, &systemallocator.heaps[0], ptr)
@@ -37,7 +37,7 @@ int main(void) {
     /* Test 2: Check if best fit on LIFO works */
     printf("Test 2: Check if best fit on LIFO works.\n");
     /* Allocate two memory blocks of 1024 and 432 bytes */
-    p1 = cmalloc(MAX_COALESCE_SIZE);
+    p1 = cmalloc(MAX_COAL_SIZE);
     p2 = cmalloc(432);
     /* Make them free */
     cfree(p2);
@@ -55,20 +55,20 @@ int main(void) {
      * Note: Splitting is checked for fixed-sized splitting or the
      * initial value of variable-sized splitting.
      */
-#ifdef MIN_SPLITTING_SIZE
+#ifdef MIN_SPLIT_SIZE
     printf("Test 3: Check if splitting works.\n");
     /* Reset everything */
     initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);    
-    if(1000 - MIN_SPLITTING_SIZE > 0 && MIN_SPLITTING_SIZE - 50 > 0) {
+    if(1000 - MIN_SPLIT_SIZE > 0 && MIN_SPLIT_SIZE - 50 > 0) {
         p1 = cmalloc(1000);
         cfree(p1);
         /* This would go to the first block and split it */
-        p1 = cmalloc(1000 - MIN_SPLITTING_SIZE - 50); /* 50 to compansate safely the
+        p1 = cmalloc(1000 - MIN_SPLIT_SIZE - 50); /* 50 to compansate safely the
                                                          header size */
         /* This goes to the third block */
         p2 = cmalloc(432);
         /* Check if the split free block is used */
-        p3 = cmalloc(MIN_SPLITTING_SIZE - 50); /* 50 to compansate safely the
+        p3 = cmalloc(MIN_SPLIT_SIZE - 50); /* 50 to compansate safely the
                                                   header size */
         assert( p3 > p1 ); /* Small sanity check */
         assert( p3 < p2 );
@@ -76,7 +76,7 @@ int main(void) {
     } else {
         printf("Please check maximum splitting size and try to fit it between 50 and 1000 bytes.\n");
     }
-#endif /* MIN_SPLITTING_SIZE */
+#endif /* MIN_SPLIT_SIZE */
 
 #ifdef WITH_REALLOC
     /* Testing realloc() */
@@ -104,13 +104,13 @@ int main(void) {
     /* Reset everything */
     initialize_allocator(&systemallocator, block_from_malloc, ALLOCATOR_SIZE);
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-    p1 = cmalloc(MIN_SPLITTING_SIZE);
-    p2 = cmalloc(2*MIN_SPLITTING_SIZE);
+    p1 = cmalloc(MIN_SPLIT_SIZE);
+    p2 = cmalloc(2*MIN_SPLIT_SIZE);
     cfree(p2);
-    p3 = crealloc(p1, 2*MIN_SPLITTING_SIZE);
-    p2 = cmalloc(MIN_SPLITTING_SIZE);
+    p3 = crealloc(p1, 2*MIN_SPLIT_SIZE);
+    p2 = cmalloc(MIN_SPLIT_SIZE);
     /* FIXME 24 is the current size of the block header */
-    assert( (char *) p2 == (char *) p1 + 24 + 2 * MIN_SPLITTING_SIZE);
+    assert( (char *) p2 == (char *) p1 + 24 + 2 * MIN_SPLIT_SIZE);
 #else /* SPLITTING_FIXED || SPLITTING_VARIABLE */
     p1 = cmalloc(100);
     p2 = cmalloc(100);

+ 2 - 2
include/dmmlib/knobs.h

@@ -33,10 +33,10 @@
 typedef struct dmm_knobs_s {
     double sys_alloc_size; /**< The default size of the raw block. */
 #ifdef GOOD_FIT
-    double fitpercentage; /**< The good-fit percentage. */
+    double fit_percentage; /**< The good-fit percentage. */
 #endif /* GOOD_FIT */
 #ifdef COALESCING_VARIABLE
-    double max_coalesce_size; /**< Maximum allowed size to be coalesced. */
+    double max_coal_size; /**< Maximum allowed size to be coalesced. */
 #endif /* COALESCING_VARIABLE */
 #ifdef SPLITTING_VARIABLE
     double min_split_size; /**< Minimum allowed size to be split. */

+ 1 - 0
src/CMakeLists.txt

@@ -34,6 +34,7 @@ if(NOT WITH_SYSTEM_CALLS STREQUAL "none")
     malloc.c
     free.c
     calloc.c
+    initialize.c
     release_memory.c
     other.c
     )

+ 2 - 2
src/freelist/coalesce.c

@@ -34,10 +34,10 @@ void coalesce(freelist_rb_t *raw_block, block_header_t *ptr) {
     block_header_t *previous_block, *next_block;
 
 #ifdef COALESCING_FIXED
-    max_coal_size = MAX_COALESCE_SIZE;
+    max_coal_size = MAX_COAL_SIZE;
 #endif /* COALESCING_FIXED */
 #ifdef COALESCING_VARIABLE
-    max_coal_size = (size_t) systemallocator.dmm_knobs.max_coalesce_size;
+    max_coal_size = (size_t) systemallocator.dmm_knobs.max_coal_size;
 #endif /* COALESCING_VARIABLE */
 
     size = get_size(ptr);

+ 1 - 1
src/freelist/fitting/good.c

@@ -36,7 +36,7 @@ block_header_t * fl_good_fit(freelist_rb_t *raw_block, size_t requested_size) {
     good_size = (size_t) -1; /* SIZE_MAX */
 #ifdef WITH_KNOBS
     ideal_size = (size_t) ((double) requested_size /
-            systemallocator.dmm_knobs.fitpercentage);
+            systemallocator.dmm_knobs.fit_percentage);
 #else /* WITH_KNOBS */
     ideal_size = (size_t) ((double) requested_size / GOOD_FIT_PERCENTAGE);
 #endif /* WITH_KNOBS */

+ 2 - 2
src/freelist/initialize.c

@@ -1,5 +1,5 @@
 /*
- *   Copyright 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.
@@ -16,7 +16,7 @@
  */
 
 /**
- * @file   initialize.c
+ * @file   freelist/initialize.c
  * @author Ioannis Koutras (joko@microlab.ntua.gr)
  * @date   October 2012
  *

+ 3 - 3
src/freelist/split.c

@@ -50,7 +50,7 @@ void split(freelist_rb_t *raw_block, block_header_t *ptr, size_t req_size) {
     }
 
 #ifdef SPLITTING_FIXED
-    min_split_size = MIN_SPLITTING_SIZE;
+    min_split_size = MIN_SPLIT_SIZE;
 #endif /* SPLITTING_FIXED */
 #ifdef SPLITTING_VARIABLE
     min_split_size = (size_t) systemallocator.dmm_knobs.min_split_size;
@@ -72,10 +72,10 @@ void split(freelist_rb_t *raw_block, block_header_t *ptr, size_t req_size) {
 #ifdef COALESCE_AFTER_SPLIT
     /* Try to coalesce with the next block if it is free */
 #ifdef COALESCING_FIXED
-    max_coal_size = MAX_COALESCE_SIZE;
+    max_coal_size = MAX_COAL_SIZE;
 #endif /* COALESCING_FIXED */
 #ifdef COALESCING_VARIABLE
-    max_coal_size = systemallocator.dmm_knobs.max_coalesce_size;
+    max_coal_size = systemallocator.dmm_knobs.max_coal_size;
 #endif /* COALESCING_VARIABLE */
 
     if(next_block != NULL && is_free(next_block) == true) {

+ 64 - 0
src/initialize.c

@@ -0,0 +1,64 @@
+/*
+ *   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.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+/**
+ * @file   src/initialize.c
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   December 2012
+ * @brief  System allocator's global object initializer.
+ */
+
+#include "dmm_config.h"
+#include "dmmlib/dmmlib.h"
+
+/** Global variable storing allocator's settings */
+allocator_t systemallocator =
+    { .rb_head = NULL
+#ifdef WITH_DEBUG
+    , .bb_head = NULL
+#endif /* WITH_DEBUG */
+#ifdef HAVE_LOCKS
+    , .creation_mutex = PTHREAD_MUTEX_INITIALIZER
+#endif /* HAVE_LOCKS */
+#ifdef WITH_KNOBS
+    , .dmm_knobs =
+            { .sys_alloc_size = SYS_ALLOC_SIZE
+#ifdef GOOD_FIT
+            , .fit_percentage = GOOD_FIT_PERCENTAGE
+#endif /* GOOD_FIT */
+#ifdef COALESCING_VARIABLE
+            , .max_coal_size = MAX_COAL_SIZE
+#endif /* COALESCING_VARIABLE */
+#ifdef SPLITTING_VARIABLE
+            , .min_split_size = MIN_SPLIT_SIZE
+#endif /* SPLITTING_VARIABLE */
+            }
+#endif /* WITH_KNOBS */
+#ifdef WITH_ALLOCATOR_STATS
+    ,   {
+#ifdef REQUEST_SIZE_INFO
+          0,
+#endif /* REQUEST_SIZE_INFO */
+          0
+        , 0
+        , 0
+        , 0
+        , 0
+        }
+#endif /* WITH_ALLOCATOR_STATS */
+};
+

+ 2 - 20
src/knobs.c

@@ -27,32 +27,14 @@
 
 #include "locks.h"
 
-__attribute__((constructor)) void knobs_init(void);
-
-/** Initializes the knobs structure of the system allocator. */
-void knobs_init(void) {
-    LOCK_GLOBAL();
-    systemallocator.dmm_knobs.sys_alloc_size = SYS_ALLOC_SIZE;
-#ifdef GOOD_FIT
-    systemallocator.dmm_knobs.fitpercentage = GOOD_FIT_PERCENTAGE;
-#endif /* GOOD_FIT_PERCENTAGE */
-#ifdef COALESCING_VARIABLE
-    systemallocator.dmm_knobs.max_coalesce_size = MAX_COALESCE_SIZE;
-#endif /* COALESCING_VARIABLE */
-#ifdef SPLITTING_VARIABLE
-    systemallocator.dmm_knobs.min_split_size = MIN_SPLITTING_SIZE;
-#endif /* SPLITTING_VARIABLE */
-    UNLOCK_GLOBAL();
-}
-
 uint32_t dmm_set_knobs(dmm_knobs_t *conf) {
     LOCK_GLOBAL();
     systemallocator.dmm_knobs.sys_alloc_size = conf->sys_alloc_size;
 #ifdef GOOD_FIT
-    systemallocator.dmm_knobs.fitpercentage = conf->fitpercentage;
+    systemallocator.dmm_knobs.fit_percentage = conf->fit_percentage;
 #endif /* GOOD_FIT_PERCENTAGE */
 #ifdef COALESCING_VARIABLE
-    systemallocator.dmm_knobs.max_coalesce_size = conf->max_coalesce_size;
+    systemallocator.dmm_knobs.max_coal_size = conf->max_coal_size;
 #endif /* COALESCING_VARIABLE */
 #ifdef SPLITTING_VARIABLE
     systemallocator.dmm_knobs.min_split_size = conf->min_split_size;