| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /*
- * Copyright 2011 Institute of Communication and Computer Systems (ICCS)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- #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
- #ifdef BLOCKS_IN_DLL
- set_previous(ptr, NULL);
- #endif /* BLOCKS_IN_DLL */
- 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;
- }
|