| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include <unistd.h>
- #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"
- void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
- size_t allocation_size, previous_size, previous_size_availability;
- void *ptr;
- #ifdef HAVE_LOCKS
- sbrk_lock();
- #endif /* HAVE_LOCKS */
- allocation_size = req_padding(size) + HEADER_SIZE;
- if(allocator->border_ptr != NULL) {
- previous_size = get_size(allocator->border_ptr);
- previous_size_availability = get_size_availability(allocator->border_ptr);
- } else {
- previous_size = 0;
- previous_size_availability = 1; /* Occupied and of 0 size */
- }
- #ifndef WITH_MEMORY_SPACE_AWARENESS
- ptr = sbrk((int) allocation_size);
- if(ptr == (void *) -1) {
- printf("sbrk problem for size of: %zu\n", allocation_size);
- printf("Error on sbrk: %s\n", strerror( errno ) );
- }
- if(allocator->border_ptr != NULL && ptr != (void *)
- ((char *) allocator->border_ptr + previous_size)) {
- printf("sbrk() does not return sequential space.\n");
- }
- #else
- 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");
- }
- #endif /* WITH_MEMORY_SPACE_AWARENESS */
- /* Go to the data part of the block */
- ptr = (void *) ((char *) ptr + HEADER_SIZE);
-
- /* Set the new border pointer */
- allocator->border_ptr = ptr;
- /* Set some values for block header */
- set_size(ptr, req_padding(size));
- set_previous_size_availability(ptr, previous_size_availability);
- /* Update stats */
- heap->dmm_stats.mem_allocated += req_padding(size);
- heap->dmm_stats.mem_requested += size;
- /* FIXME To be refactored - START */
- set_requested_size(ptr, size);
- mark_used(ptr);
- // Update the used blocks list
- set_next(ptr, heap->used_blocks_head);
- heap->used_blocks_head = ptr;
- // Begin of Stats
- heap->dmm_stats.live_objects += 1;
- heap->dmm_stats.num_malloc += 1;
- // End of Stats
- /* FIXME To be refactored - END */
- #ifdef HAVE_LOCKS
- sbrk_unlock();
- #endif /* HAVE_LOCKS */
- return ptr;
- }
|