Bläddra i källkod

Initial support for mmap().

Ioannis Koutras 13 år sedan
förälder
incheckning
46fa76b147
7 ändrade filer med 49 tillägg och 9 borttagningar
  1. 1 0
      CMakeLists.txt
  2. 7 0
      DefineOptions.cmake
  3. 1 0
      dmm_config.h.in
  4. 0 2
      include/dmmlib/heap.h
  5. 3 0
      private-include/sys_alloc.h
  6. 1 0
      src/initialize_allocator.c
  7. 36 7
      src/sys_alloc.c

+ 1 - 0
CMakeLists.txt

@@ -35,6 +35,7 @@ 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")
   message(STATUS "Block organization: Doubly-Linked Lists")

+ 7 - 0
DefineOptions.cmake

@@ -17,6 +17,8 @@ if (NUM_HEAPS GREATER 1)
   set(MULTIPLE_HEAPS ON)
 endif (NUM_HEAPS GREATER 1)
 
+set(WITH_SYSTEM_CALLS "no" "Build with OS call support for more memory")
+
 set(WITH_COALESCING "never" "Build with coalescing support")
 set(WITH_SPLITTING "never" "Build with splitting support")
 
@@ -54,6 +56,7 @@ if (LEON3)
 endif (LEON3)
 
 if (LINUXTEST)
+  set(WITH_SYSTEM_CALLS "mmap")
   set(HAVE_LOCKS ON)
   set(WITH_EXAMPLES ON)
   set(WITH_COALESCING "variable")
@@ -73,6 +76,10 @@ else(BLOCKS_ORGANIZATION STREQUAL "dll")
 	set(BLOCKS_IN_SLL ON)
 endif(BLOCKS_ORGANIZATION STREQUAL "dll")
 
+if(WITH_SYSTEM_CALLS STREQUAL "mmap")
+  set(WITH_MMAP ON)
+endif(WITH_SYSTEM_CALLS)
+
 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={size}.")

+ 1 - 0
dmm_config.h.in

@@ -5,6 +5,7 @@
 
 #cmakedefine HAVE_LOCKS
 #cmakedefine WITH_MEMORY_SPACE_AWARENESS
+#cmakedefine WITH_MMAP
 
 /** The number of the heaps. */
 #cmakedefine NUM_HEAPS @NUM_HEAPS@

+ 0 - 2
include/dmmlib/heap.h

@@ -124,10 +124,8 @@ typedef struct allocator_s {
 	heap_t heaps[NUM_HEAPS]; /**< The heaps that the allocator manages. */
 	bool initialized; /**< Initialization flag of the allocator. */
 	void *border_ptr; /**< Border pointer of the allocator. */
-#ifdef WITH_MEMORY_SPACE_AWARENESS
 	size_t remaining_size; /**< The size of the remaining free space which
 				 is handled by the allocator. */
-#endif /* WITH_MEMORY_SPACE_AWARENESS */
 } allocator_t;
 
 #endif /* HEAP_H */

+ 3 - 0
private-include/sys_alloc.h

@@ -18,6 +18,9 @@
 #ifndef SYS_ALLOC_H
 #define SYS_ALLOC_H
 
+#include "dmm_config.h"
+#include <dmmlib/heap.h>
+
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size);
 
 #endif /* SYS_ALLOC_H */

+ 1 - 0
src/initialize_allocator.c

@@ -84,6 +84,7 @@ void initialize_allocator(allocator_t *allocator) {
 
 #ifndef WITH_MEMORY_SPACE_AWARENESS
     allocator->border_ptr = NULL;
+    allocator->remaining_size = 0;
 #else
     allocator->border_ptr = starting_address;
     allocator->remaining_size = size;

+ 36 - 7
src/sys_alloc.c

@@ -15,26 +15,40 @@
  *
  */
 
-#include <unistd.h>
+#include "sys_alloc.h"
+
+#ifdef WITH_MMAP
+#include <sys/stat.h> /* for open() */
+#include <fcntl.h> /* for open() */
+
+#include <sys/mman.h>
+#else /* WITH_MMAP */
+#include <unistd.h> /* for sbrk() */
+#endif /* WITH_MMAP */
+
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <dmmlib/heap.h>
-#include "dmm_config.h"
 #ifdef HAVE_LOCKS
 #include "posix_lock.h"
 #endif /* HAVE_LOCKS */
 #include "other.h"
-#include "sys_alloc.h"
 #include "block_header.h"
 #ifdef WITH_STATS
 #include "print_stats.h"
 #endif /* WITH_STATS */
 
+#ifdef WITH_MMAP
+static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
+#endif /* WITH_MMAP */
+
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
     size_t allocation_size, previous_size, previous_size_availability;
     void *ptr;
+#ifdef WITH_MMAP
+    int fd;
+#endif /* WITH_MMAP */
 
 #ifdef HAVE_LOCKS
     sbrk_lock();
@@ -63,7 +77,22 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     }
 
 #ifndef WITH_MEMORY_SPACE_AWARENESS
+    if(size + HEADER_SIZE < 4096) {
+        allocation_size = 4096;
+    } else {
+        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);
+#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);
@@ -75,11 +104,11 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     if(allocator->border_ptr != NULL && ptr != (void *) 
             ((char *) allocator->border_ptr + previous_size)) {
-        printf("sbrk() does not return sequential space. "
-                "You should disable coalescing.\n");
+	printf("sbrk() / mmap() does not return sequential space. You should"
+			" disable coalescing.\n");
     }
 #endif /* COALESCING_FIXED || COALESCING_VARIABLE */    
-#else
+#else /* WITH_MEMORY_SPACE_AWARENESS */
     if(allocator->remaining_size >= allocation_size) {
         ptr = (void *) ((char *) allocator->border_ptr + previous_size);
         allocator->remaining_size -= allocation_size;