瀏覽代碼

Refactor sys_alloc() and memory space awareness

Ioannis Koutras 13 年之前
父節點
當前提交
8977e09cbc

+ 0 - 1
CMakeLists.txt

@@ -34,7 +34,6 @@ endif (WITH_DOC)
 
 message(STATUS "********************************************")
 message(STATUS "********** ${PROJECT_NAME} build options : **********")
-message(STATUS "Memory space awareness: " ${WITH_MEMORY_SPACE_AWARENESS})
 message(STATUS "OS call for memory requests: " ${WITH_SYSTEM_CALLS})
 message(STATUS "POSIX locking mechanisms: " ${HAVE_LOCKS})
 if(BLOCKS_ORGANIZATION STREQUAL "dll")

+ 9 - 7
DefineOptions.cmake

@@ -1,7 +1,6 @@
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" ON)
 option(WITH_REALLOC "Build with realloc" OFF)
 option(WITH_EXAMPLES "Build with examples" OFF)
-option(WITH_MEMORY_SPACE_AWARENESS "Build with memory space awareness" OFF)
 option(WITH_FIXED_LISTS "Build with predefined lists of fixed-sized blocks" ON)
 option(WITH_STATS "Build with statistics" OFF)
 option(COUNT_ACCESSES "Count memory accesses" OFF)
@@ -12,6 +11,7 @@ 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(WITH_SYSTEM_CALLS "Choose what system calls can be used, options are: none, sbrk, mmap" "none")
 option(SORT_POLICY "Choose the block sorting policy, options are: lifo, fifo, size, address")
 
 set(REQUEST_SIZE_INFO ON)
@@ -41,7 +41,7 @@ option(LINUXTEST "Build a case for Linux" ON)
 
 if (P2012)
   set(HAVE_LOCKS OFF)
-  set(WITH_MEMORY_SPACE_AWARENESS ON)
+  set(WITH_SYSTEM_CALLS "none")
   set(WITH_STATIC_LIB ON)
   set(NUM_HEAPS 1)
   set(LINUXTEST OFF)
@@ -53,7 +53,7 @@ endif (P2012)
 
 if (LEON3)
   set(HAVE_LOCKS OFF)
-  set(WITH_MEMORY_SPACE_AWARENESS ON)
+  set(WITH_SYSTEM_CALLS "none")
   set(WITH_STATIC_LIB ON)
   set(NUM_HEAPS 1)
   set(LINUXTEST OFF)
@@ -98,11 +98,13 @@ elseif(SORT_POLICY STREQUAL "address")
   set(ADDRESS_SORT_POLICY ON)
 endif(SORT_POLICY STREQUAL "lifo")
 
-if(WITH_SYSTEM_CALLS STREQUAL "mmap")
+if(WITH_SYSTEM_CALLS STREQUAL "none")
+  set(NO_SYSTEM_CALLS ON)
+elseif(WITH_SYSTEM_CALLS STREQUAL "sbrk")
+  set(WITH_SBRK ON)
+elseif(WITH_SYSTEM_CALLS STREQUAL "mmap")
   set(WITH_MMAP ON)
-else(WITH_SYSTEM_CALLS STREQUAL "mmap")
-  set(WITH_MMAP OFF)
-endif(WITH_SYSTEM_CALLS STREQUAL "mmap")
+endif(WITH_SYSTEM_CALLS STREQUAL "none")
 
 if(SEARCH_POLICY STREQUAL "best")
   set(BEST_FIT ON)

+ 3 - 1
dmm_config.h.in

@@ -4,7 +4,9 @@
 #cmakedefine LEON3
 
 #cmakedefine HAVE_LOCKS
-#cmakedefine WITH_MEMORY_SPACE_AWARENESS
+
+#cmakedefine NO_SYSTEM_CALLS
+#cmakedefine WITH_SBRK
 #cmakedefine WITH_MMAP
 
 /** The number of the heaps. */

+ 2 - 3
examples/CMakeLists.txt

@@ -12,11 +12,10 @@ if (HAVE_LOCKS)
   target_link_libraries(larson ${DMMLIB_SHARED_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
 endif (HAVE_LOCKS)
 
-if (WITH_MEMORY_SPACE_AWARENESS)
+if (NO_SYSTEM_CALLS)
   add_executable(test_for_memory_space_aware test_for_memory_space_aware.c)
   target_link_libraries(test_for_memory_space_aware ${DMMLIB_STATIC_LIBRARY})
 else (WITH_MEMORY_SPACE_AWARENESS)
   add_executable(test test.c)
   target_link_libraries(test ${DMMLIB_SHARED_LIBRARY})
-endif (WITH_MEMORY_SPACE_AWARENESS)
-
+endif (NO_SYSTEM_CALLS)

+ 5 - 5
include/dmmlib/initialize_allocator.h

@@ -28,9 +28,10 @@
 #include <dmmlib/heap.h>
 #include "dmm_config.h"
 
-#ifdef WITH_MEMORY_SPACE_AWARENESS
+#ifdef NO_SYSTEM_CALLS
 /**
- * Initialize an allocator.
+ * Initialize an allocator. Since no system calls can be made, the developer
+ * has to define explicitly the memory space the allocator will manage.
  *
  * \param allocator The address of the allocator.
  * \param starting_address The starting addres of the memory space which the
@@ -40,14 +41,13 @@
  */
 void initialize_allocator(allocator_t *allocator, void *starting_address,
   size_t size);
-#else
+#else /* NO_SYSTEM_CALLS */
 /**
  * Initialize an allocator.
  *
  * \param allocator The address of the allocator.
  */
 void initialize_allocator(allocator_t *allocator);
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
 
 #endif /* INITIALIZE_ALLOCATOR_H */
-

+ 2 - 3
private-include/other.h

@@ -27,9 +27,8 @@ size_t req_padding(size_t size);
 int map_size_to_list(heap_t *heap, size_t sz);
 #endif /* WITH_FIXED_LISTS */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+#ifndef NO_SYSTEM_CALLS
 int map_thread_heap(void);
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
 
 #endif /* OTHER_H */
-

+ 1 - 1
src/CMakeLists.txt

@@ -1,7 +1,7 @@
 project (dmmlib-library C)
 
 if(NOT CMAKE_BUILD_TYPE)
-  set(CMAKE_BUILD_TYPE RelWithDebInfo)
+  set(CMAKE_BUILD_TYPE Debug)
 endif(NOT CMAKE_BUILD_TYPE)
 
 set(DMMLIB_PUBLIC_INCLUDE_DIRS

+ 3 - 3
src/custom_free.c

@@ -248,11 +248,11 @@ void free(void *ptr) {
     allocator = &systemallocator;
 
     /* FIXME Space aware allocators currently use one heap */
-#ifdef WITH_MEMORY_SPACE_AWARENESS
+#ifdef NO_SYSTEM_CALLS
     heap_id = 0;
-#else /* WITH_MEMORY_SPACE_AWARENESS */
+#else /* NO_SYSTEM_CALLS */
     heap_id = map_thread_heap();
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
     heap = &allocator->heaps[heap_id];
 
     custom_ahfree(allocator, heap, ptr);

+ 5 - 5
src/custom_malloc.c

@@ -156,18 +156,18 @@ void * malloc(size_t size) {
     allocator = &systemallocator;
     /* Space aware allocators have to be already initialized, no need to do
      * that here. */
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+#ifndef NO_SYSTEM_CALLS
     if(allocator->initialized != true) {
         initialize_allocator(allocator);
     }
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
 
     /* FIXME Space aware allocators currently use one heap */
-#ifdef WITH_MEMORY_SPACE_AWARENESS
+#ifdef NO_SYSTEM_CALLS
     heap_id = 0;
-#else /* WITH_MEMORY_SPACE_AWARENESS */
+#else /* NO_SYSTEM_CALLS */
     heap_id = map_thread_heap();
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
     heap = &allocator->heaps[heap_id];
 
     return custom_ahmalloc(allocator, heap, size);

+ 7 - 7
src/custom_realloc.c

@@ -27,9 +27,9 @@
 #include <dmmlib/custom_realloc.h>
 #include <dmmlib/initialize_allocator.h>
 #include "block_header.h"
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+#ifndef NO_SYSTEM_CALLS
 #include "other.h"
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
 #include "split.h"
 #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
@@ -175,18 +175,18 @@ void * realloc(void *ptr, size_t size) {
     allocator = &systemallocator;
     /* Space aware allocators have to be already initialized, no need to do
      * that here. */
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+#ifndef NO_SYSTEM_CALLS
     if(allocator->initialized != true) {
         initialize_allocator(allocator);
     }
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
 
     /* FIXME Space aware allocators currently use one heap */
-#ifdef WITH_MEMORY_SPACE_AWARENESS
+#ifdef NO_SYSTEM_CALLS
     heap_id = 0;
-#else /* WITH_MEMORY_SPACE_AWARENESS */
+#else /* NO_SYSTEM_CALLS */
     heap_id = map_thread_heap();
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
     heap = &allocator->heaps[heap_id];
 
     return custom_ahrealloc(allocator, heap, ptr, size);

+ 5 - 8
src/initialize_allocator.c

@@ -15,9 +15,6 @@
  *
  */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-#include <unistd.h>
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
 #include <dmmlib/initialize_allocator.h>
 #ifdef HAVE_LOCKS
 #include "posix_lock.h"
@@ -29,12 +26,12 @@
 #include "linked_lists/search_algorithms.h"
 #endif /* defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT) */
 
-#ifdef WITH_MEMORY_SPACE_AWARENESS
+#ifdef NO_SYSTEM_CALLS
 void initialize_allocator(allocator_t *allocator, void *starting_address, 
         size_t size) {
 #else
 void initialize_allocator(allocator_t *allocator) {
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
     int i;
 #ifdef WITH_FIXED_LISTS
     heap_t *current_heap;
@@ -109,13 +106,13 @@ void initialize_allocator(allocator_t *allocator) {
 #endif /* HAVE_LOCKS */
     }
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
+#ifndef NO_SYSTEM_CALLS
     allocator->border_ptr = NULL;
     allocator->remaining_size = 0;
-#else
+#else /* NO_SYSTEM_CALLS */
     allocator->border_ptr = starting_address;
     allocator->remaining_size = size;
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
+#endif /* NO_SYSTEM_CALLS */
     allocator->initialized = false;
 
 #ifdef ALLOC_VAR_FIT

+ 2 - 5
src/other.c

@@ -60,12 +60,9 @@ int map_size_to_list(heap_t *heap, size_t sz) {
 
 #endif /* WITH_FIXED_LISTS */
 
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-
+#ifndef NO_SYSTEM_CALLS
 /* Random assignment */
 int map_thread_heap(void) {
     return (int) (((unsigned long) pthread_self() >> 10) % NUM_HEAPS);
 }
-
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-
+#endif /* NO_SYSTEM_CALLS */

+ 52 - 66
src/sys_alloc.c

@@ -44,11 +44,8 @@ static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
 
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
-    size_t allocation_size, previous_size_availability;
+    size_t allocation_size, previous_size, previous_size_availability;
     void *ptr;
-#if defined (WITH_MEMORY_SPACE_AWARENESS) || (!defined (WITH_MEMORY_SPACE_AWARENESS) && (defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)))
-    size_t previous_size;
-#endif /* check if previous_size is needed */
 #ifdef WITH_MMAP
     int fd;
 #endif /* WITH_MMAP */
@@ -59,84 +56,69 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
     allocation_size = req_padding(size) + HEADER_SIZE;
 
-    /* Before the first application malloc() call, the allocator uses
+    /* Step 1: Check if the memory allocator is initialized.
+     * 
+     * Before the first application malloc() call, the allocator uses
      * sys_alloc() to allocate space for its metadata. So, these are the first
      * data blocks in the heaps and there are no previous data blocks.
      */
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    if(allocator->border_ptr != NULL) {
-#if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE) 
-        previous_size = get_size(allocator->border_ptr);
-#endif /* COALESCING_FIXED || COALESCING_VARIABLE */
-#else
-    /* Note: border_ptr has already a value in memory space aware allocators,
-     * so in this case we have to check also the initialized value.
-     */
-    if(allocator->border_ptr != NULL && allocator->initialized) {
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
-        previous_size_availability = get_size_availability(allocator->border_ptr);
-    } else {
-#if defined (WITH_MEMORY_SPACE_AWARENESS) || (!defined (WITH_MEMORY_SPACE_AWARENESS) && (defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)))
+    if(allocator->initialized == false) {
         previous_size = 0;
-#endif /* check if previous_size is needed */
         previous_size_availability = 1; /* Occupied and of 0 size */
         allocator->initialized = true;
-    }
-
-#ifndef WITH_MEMORY_SPACE_AWARENESS
-    // TODO Make the page size configurable at design time
-    if(size + HEADER_SIZE < 4096) {
-        allocation_size = 4096;
     } else {
-        /* If allocation size is more than 4k, then request multiples of 4k */
-        allocation_size = ((size + HEADER_SIZE) / 4096 + 1) * 4096;
+        previous_size = get_size(allocator->border_ptr);
+        previous_size_availability = get_size_availability(allocator->border_ptr);
     }
+
+    if(allocation_size > allocator->remaining_size) {
+#ifndef NO_SYSTEM_CALLS
+        // TODO Make the page size configurable at design time
+        if(allocation_size < 4096) {
+            allocation_size = 4096;
+        } else {
+            /* If allocation size is more than 4k, then request multiples of 4k */
+            allocation_size = ((size + HEADER_SIZE) / 4096 + 1) * 4096;
+        }
 #ifdef WITH_MMAP
-    if(dev_zero_fd < 0) {
-        dev_zero_fd = open("/dev/zero", O_RDWR);
-    }
-    fd = dev_zero_fd;
-    ptr = mmap(0, allocation_size, PROT_READ|PROT_WRITE,
-            MAP_SHARED, fd, 0);
-#else /* WITH_MMAP */
-    ptr = sbrk((int) allocation_size);
+        if(dev_zero_fd < 0) {
+            dev_zero_fd = open("/dev/zero", O_RDWR);
+        }
+        fd = dev_zero_fd;
+        ptr = mmap(0, allocation_size, PROT_READ|PROT_WRITE,
+                MAP_SHARED, fd, 0);
 #endif /* WITH_MMAP */
-    allocator->remaining_size += allocation_size;
-    if(ptr == (void *) -1) {
-#ifdef WITH_STATS
-        printf("sbrk problem for size of: %zu\n", allocation_size);
-        printf("Error on sbrk: %s\n", strerror( errno ) );
-        print_stats(allocator);
-#endif /* WITH_STATS */
-        return NULL;
-    }   
+#ifdef WITH_SBRK
+        ptr = sbrk((int) allocation_size);
+#endif /* WITH_SBRK */
+
+        /* Print a warning for sbrk / mmap in case the user has enabled
+         * coalescing support
+         */
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
-    if(allocator->border_ptr != NULL && ptr != (void *) 
-            ((char *) allocator->border_ptr + previous_size)) {
-	printf("sbrk() / mmap() does not return sequential space. You should"
-			" disable coalescing.\n");
-    }
+        if(allocator->border_ptr != NULL && ptr != (void *) 
+                ((char *) allocator->border_ptr + previous_size)) {
+            printf("sbrk() / mmap() does not return sequential space. You should"
+                    " disable coalescing.\n");
+        }
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */    
-#else /* WITH_MEMORY_SPACE_AWARENESS */
 
-    /* Iraklis' request: On space-aware memory allocators, each sys_alloc() call
-     * is equivalent to 2 hops.
-     */
-#ifdef COUNT_HOPS
-    heap->dmm_stats.total_hops += 2;
-#endif /* COUNT_HOPS */   
-
-    if(allocator->remaining_size >= allocation_size) {
-        ptr = (void *) ((char *) allocator->border_ptr + previous_size);
-        allocator->remaining_size -= allocation_size;
-    } else {
-        printf("No more free space.\n");
+        if(ptr == (void *)-1) {
+#endif /* NO_SYSTEM_CALLS */
+            printf("Couldn't allocate more space from the system.\n");
 #ifdef WITH_STATS
-        print_stats(allocator);
+            print_stats(allocator);
 #endif /* WITH_STATS */
-        return NULL;
+            return NULL;
+#ifndef NO_SYSTEM_CALLS
+        } else {
+            allocator->remaining_size += allocation_size;
+        }
+#endif /* NO_SYSTEM_CALLS */
+    } else {
+        ptr = (void *)((char *)allocator->border_ptr + previous_size);
+        allocator->remaining_size -= allocation_size;
     }
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
 
     /* Go to the data part of the block */
     ptr = (void *) ((char *) ptr + HEADER_SIZE);
@@ -158,6 +140,10 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 #endif /* FUTURE_FEATURES */
 
 #ifdef WITH_STATS
+    /* Iraklis' request: Each sys_alloc() call is equivalent to 2 hops. */
+#ifdef COUNT_HOPS
+    heap->dmm_stats.total_hops += 2;
+#endif /* COUNT_HOPS */   
     /* Update statistics */
 #ifdef REQUEST_SIZE_INFO
     heap->dmm_stats.mem_requested += size;