Prechádzať zdrojové kódy

Make REPLACE_MALLOC directive obsolete, dmmlib now accepts malloc(), free() and realloc() calls directly

Ioannis Koutras 13 rokov pred
rodič
commit
dfa095c4fd

+ 0 - 1
CMakeLists.txt

@@ -86,6 +86,5 @@ if(MIN_SPLITTING_SIZE)
 endif(MIN_SPLITTING_SIZE)
 message(STATUS "Adaptivity: " ${WITH_ADAPTIVITY})
 message(STATUS "Support for realloc(): " ${WITH_REALLOC})
-message(STATUS "Replace function calls: " ${REPLACE_MALLOC})
 message(STATUS "********************************************")
 

+ 0 - 1
DefineOptions.cmake

@@ -11,7 +11,6 @@ option(WITH_ADAPTIVITY "Build with adaptivity" OFF)
 option(WITH_STATIC_LIB "Build a static library" OFF)
 option(WITH_SHARED_LIB "Build a shared library" OFF)
 option(WITH_DOC "Build with documentation" OFF)
-option(REPLACE_MALLOC "Replace function calls" OFF)
 
 option(SORT_POLICY "Choose the block sorting policy, options are: lifo, fifo, size, address")
 

+ 0 - 1
dmm_config.h.in

@@ -55,7 +55,6 @@
 
 #define MIN_FRAG_THRESHOLD 0.05
 
-#cmakedefine REPLACE_MALLOC
 #cmakedefine WITH_REALLOC
 
 #endif /* DMM_CONFIG_H */

+ 7 - 11
include/dmmlib/custom_realloc.h

@@ -28,7 +28,7 @@
 #include <dmmlib/heap.h>
 
 /**
- * Try to change the size of an allocation
+ * Try to change the size of an allocation on a specific allocator and heap
  *
  * \param allocator 	The pointer to the allocator who manages the block.
  * \param heap 		The pointer to the heap who manages the block.
@@ -40,7 +40,10 @@
  */ 
 void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t size);
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+
+
+/* In case stdlib.h is used, there is no need to redeclare realloc() */
+#ifndef __malloc_and_calloc_defined
 /**
  * Try to change the size of an allocation
  * \param   ptr	    The pointer to the data part of the original memory block.
@@ -49,14 +52,7 @@ void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t
  * \return  The pointer to the data part of the memory block which
  * has the new, desired size.
  */
-void * custom_realloc(void *ptr, size_t size);
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-
-#ifdef REPLACE_MALLOC
-
-#define realloc(ptr, size) custom_ahrealloc(NULL, NULL, ptr, size)
-
-#endif /* REPLACE_MALLOC */
+void * realloc(void *ptr, size_t size);
+#endif /* __malloc_and_calloc_defined */
 
 #endif /* CUSTOM_REALLOC_H */
-

+ 7 - 11
include/dmmlib/dmmlib.h

@@ -24,18 +24,14 @@
 void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, 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);
+/* In case stdlib.h is used, there is no need to redeclare
+ * malloc() and free()
+ */
+#ifndef __malloc_and_calloc_defined
+void * malloc(size_t size);
+void free(void *ptr);
+#endif /* __malloc_and_calloc_defined */
 
 allocator_t systemallocator;
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-
-#ifdef REPLACE_MALLOC
-
-#define malloc(size) custom_ahmalloc(NULL, NULL, size)
-#define free(ptr) custom_ahfree(NULL, NULL, ptr)
-
-#endif /* REPLACE_MALLOC */
 
 #endif /* DMMLIB_H */

+ 21 - 20
src/custom_free.c

@@ -45,19 +45,6 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
     size_t three_blocks_size;
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    int heap_id;
-    /* 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);
 
 #ifdef HAVE_LOCKS	
@@ -251,13 +238,27 @@ 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.
- */
+void free(void *ptr) {
+    allocator_t *allocator;
+    heap_t *heap;
+    int heap_id;
+
+    allocator = &systemallocator;
+    /* Space aware allocators have to be already initialized, no need to do
+     * that here. */
 #ifndef WITH_MEMORY_SPACE_AWARENESS
-void custom_free(void *ptr) {
-    custom_ahfree(NULL, NULL, ptr);
-}
+    if(allocator->initialized != true) {
+        initialize_allocator(allocator);
+    }
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
 
+    /* FIXME Space aware allocators currently use one heap */
+#ifdef WITH_MEMORY_SPACE_AWARENESS
+    heap_id = 0;
+#else /* WITH_MEMORY_SPACE_AWARENESS */
+    heap_id = map_thread_heap();
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+    heap = &allocator->heaps[heap_id];
+
+    custom_ahfree(allocator, heap, ptr);
+}

+ 22 - 25
src/custom_malloc.c

@@ -50,24 +50,6 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     size_t min_split_size;
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    int heap_id;
-
-    /* Go to the system allocator if none was given */
-    if(allocator == NULL) {
-        allocator = &systemallocator;
-        if(allocator->initialized != true) {
-            initialize_allocator(allocator);
-        }
-    }
-
-    if(heap == NULL) {
-        heap_id = map_thread_heap();
-        heap = &allocator->heaps[heap_id];
-    }
-
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-
     ptr = NULL;
 
 #ifdef HAVE_LOCKS
@@ -160,16 +142,31 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 #ifdef HAVE_LOCKS
     posix_unlock(heap);
 #endif /* HAVE_LOCKS */
+
     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.
- */
+void * malloc(size_t size) {
+    allocator_t *allocator;
+    heap_t *heap;
+    int heap_id;
+
+    allocator = &systemallocator;
+    /* Space aware allocators have to be already initialized, no need to do
+     * that here. */
 #ifndef WITH_MEMORY_SPACE_AWARENESS
-void * custom_malloc(size_t size) {
-    return custom_ahmalloc(NULL, NULL, size);
-}
+    if(allocator->initialized != true) {
+        initialize_allocator(allocator);
+    }
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+    /* FIXME Space aware allocators currently use one heap */
+#ifdef WITH_MEMORY_SPACE_AWARENESS
+    heap_id = 0;
+#else /* WITH_MEMORY_SPACE_AWARENESS */
+    heap_id = map_thread_heap();
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
+    heap = &allocator->heaps[heap_id];
 
+    return custom_ahmalloc(allocator, heap, size);
+}

+ 21 - 21
src/custom_realloc.c

@@ -83,24 +83,6 @@ void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t
     size_t min_split_size;
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    int heap_id;
-
-    /* Go to the system allocator if none was given */
-    if(allocator == NULL) {
-        allocator = &systemallocator;
-        if(allocator->initialized != true) {
-            initialize_allocator(allocator);
-        }
-    }
-
-    if(heap == NULL) {
-        heap_id = map_thread_heap();
-        heap = &allocator->heaps[heap_id];
-    }
-
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-
     if(ptr != NULL) {
         old_size = get_size(ptr);
         if(old_size > size) {
@@ -173,9 +155,27 @@ void * custom_ahrealloc(allocator_t *allocator, heap_t *heap, void *ptr, size_t
     }
 }
 
+void * realloc(void *ptr, size_t size) {
+    allocator_t *allocator;
+    heap_t *heap;
+    int heap_id;
+
+    allocator = &systemallocator;
+    /* Space aware allocators have to be already initialized, no need to do
+     * that here. */
 #ifndef WITH_MEMORY_SPACE_AWARENESS
-void * custom_realloc(void *ptr, size_t size) {
-    return custom_ahrealloc(NULL, NULL, ptr, size);
-}
+    if(allocator->initialized != true) {
+        initialize_allocator(allocator);
+    }
+#endif /* WITH_MEMORY_SPACE_AWARENESS */
+
+    /* FIXME Space aware allocators currently use one heap */
+#ifdef WITH_MEMORY_SPACE_AWARENESS
+    heap_id = 0;
+#else /* WITH_MEMORY_SPACE_AWARENESS */
+    heap_id = map_thread_heap();
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
+    heap = &allocator->heaps[heap_id];
 
+    return custom_ahrealloc(allocator, heap, ptr, size);
+}