瀏覽代碼

Proper guarding on memory space aware allocators.

Ioannis Koutras 13 年之前
父節點
當前提交
852832353c
共有 4 個文件被更改,包括 30 次插入8 次删除
  1. 8 4
      include/dmmlib/dmmlib.h
  2. 10 1
      src/custom_free.c
  3. 10 3
      src/custom_malloc.c
  4. 2 0
      src/initialize_allocator.c

+ 8 - 4
include/dmmlib/dmmlib.h

@@ -1,12 +1,16 @@
 #ifndef DMMLIB_H
 #include <dmmlib/heap.h>
-
-static allocator_t systemallocator;
+#include "dmm_config.h"
 
 void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size);
-void * custom_malloc(size_t size);
-
 void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr);
+
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+void * custom_malloc(size_t size);
 void custom_free(void *ptr);
 
+static allocator_t systemallocator;
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+
 #endif /* DMMLIB_H */

+ 10 - 1
src/custom_free.c

@@ -11,14 +11,17 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
     int heap_id, fixed_list_id, i;
     maptable_node_t *current_maptable_node;
 
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+    // Currently all the memory space aware allocators are pre-initialized
     if(allocator == NULL) {
         allocator = &systemallocator;
     }
-    
+   
     if(heap == NULL) {
         heap_id = map_thread_heap();
         heap = &allocator->heaps[heap_id];
     }
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
 
     size = get_size(ptr);
 
@@ -67,7 +70,13 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
 #endif /* HAVE_LOCKS */
 }
 
+/* Currently all the memory space aware allocators are pre-initialized, so
+ * we do not expect any custom_ahmalloc call without knowing which allocator
+ * and heap are to be used.
+ */
+#ifndef WITH_MEMORY_SPACE_AWARENESS
 void custom_free(void *ptr) {
     custom_ahfree(NULL, NULL, ptr);
 }
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
 

+ 10 - 3
src/custom_malloc.c

@@ -17,15 +17,14 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     maptable_node_t *current_maptable_node;
     void *current_block, *previous_block;
 
+#ifndef WITH_MEMORY_SPACE_AWARENESS
+
     // Go to the system allocator if none was given
     if(allocator == NULL) {
         allocator = &systemallocator;
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-        // Currently all the memory space aware allocators are pre-initialized
         if(allocator->initialized != true) {
             initialize_allocator(allocator);
         }
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
     }
 
     if(heap == NULL) {
@@ -33,6 +32,8 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         heap = &allocator->heaps[heap_id];
     }
 
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
     ptr = NULL;
     previous_block = NULL;
 
@@ -108,7 +109,13 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     return ptr;
 }
 
+/* Currently all the memory space aware allocators are pre-initialized, so
+ * we do not expect any custom_ahmalloc call without knowing which allocator
+ * and heap are to be used.
+ */
+#ifndef WITH_MEMORY_SPACE_AWARENESS
 void * custom_malloc(size_t size) {
     return custom_ahmalloc(NULL, NULL, size);
 }
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
 

+ 2 - 0
src/initialize_allocator.c

@@ -1,4 +1,6 @@
+#ifndef WITH_MEMORY_SPACE_AWARENESS
 #include <unistd.h>
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
 #ifdef HAVE_LOCKS
 #include <pthread.h>
 #endif /* HAVE_LOCKS */