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