|
@@ -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;
|