| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /*
- * Copyright 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.
- *
- */
- /**
- * @file malloc.c
- * @author Ioannis Koutras (joko@microlab.ntua.gr)
- * @date September 2012
- *
- * @brief Implementation of malloc() call.
- */
- #include "dmmlib/dmmlib.h"
- #ifdef BITMAP_RB_ONLY
- #include "bitmap/bitmap.h"
- #include "bitmap/bitmap_rb.h"
- #endif /* BITMAP_RB_ONLY */
- #ifdef FL_RB_ONLY
- #include "freelist/freelist.h"
- #include "freelist/freelist_rb.h"
- #endif /* FL_RB_ONLY */
- #ifdef WITH_ALLOCATOR_STATS
- #include "statistics.h"
- #endif /* WITH_ALLOCATOR_STATS */
- #include "trace.h"
- void * malloc(size_t size) {
- raw_block_header_t *raw_block, *new_raw_block;
- #ifdef BITMAP_RB_ONLY
- bitmap_rb_t
- #endif /* BITMAP_RB_ONLY */
- #ifdef FL_RB_ONLY
- freelist_rb_t
- #endif /* FL_RB_ONLY */
- *encapsulated_rb;
- void *ptr;
- raw_block = systemallocator.raw_block_head;
- ptr = NULL;
- if(size == 0) {
- return NULL;
- }
-
- /* Try to find a raw block available for allocation */
- while(raw_block != NULL) {
- #ifdef HAVE_LOCKS
- pthread_mutex_lock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- encapsulated_rb =
- #ifdef BITMAP_RB_ONLY
- (bitmap_rb_t *)
- #endif /* BITMAP_RB_ONLY */
- #ifdef FL_RB_ONLY
- (freelist_rb_t *)
- #endif /* FL_RB_ONLY */
- ((char *)raw_block + sizeof(raw_block_header_t));
- ptr = dmmlib_malloc(encapsulated_rb, size);
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- if(ptr != NULL) {
- break;
- }
- raw_block = raw_block->next_raw_block;
- }
- if(ptr == NULL) {
- #ifdef BITMAP_RB_ONLY
- /* Check if the request would fit in a new bitmap raw block
- * FIXME currently the raw block size and resolution are fixed
- */
- size_t bm_vector_size = BMAP_EL_SIZE *
- (SYS_ALLOC_SIZE + BMAP_EL_SIZE -
- sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
- (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
- if(2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
- sizeof(bitmap_rb_t) - bm_vector_size) {
- #endif /* BITMAP_RB_ONLY */
- #ifdef FL_RB_ONLY
- if( 2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
- sizeof(freelist_rb_t)) {
- #endif /* FL_RB_ONLY */
- ptr = (void *)create_raw_block(size +
- sizeof(raw_block_header_t), BIGBLOCK);
- if(ptr != NULL) {
- #ifdef WITH_DEBUG
- #ifdef HAVE_LOCKS
- pthread_mutex_lock(&systemallocator.creation_mutex);
- #endif /* HAVE_LOCKS */
- ((raw_block_header_t *)ptr)->next_raw_block =
- systemallocator.big_blocks_head;
- systemallocator.big_blocks_head = (raw_block_header_t *)ptr;
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&systemallocator.creation_mutex);
- #endif /* HAVE_LOCKS */
- #endif /* WITH_DEBUG */
- #ifdef WITH_ALLOCATOR_STATS
- update_stats(&systemallocator.dmm_stats,
- MALLOC,
- #ifdef REQUEST_SIZE_INFO
- size,
- #endif /* REQUEST_SIZE_INFO */
- size + sizeof(raw_block_header_t));
- #endif /* WITH_ALLOCATOR_STATS */
- ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));
- }
- } else {
- #ifdef HAVE_LOCKS
- pthread_mutex_lock(&systemallocator.creation_mutex);
- #endif /* HAVE_LOCKS */
- new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE,
- #ifdef FL_RB_ONLY
- FREELIST
- #endif /* FL_RB_ONLY */
- #ifdef BITMAP_RB_ONLY
- BITMAP
- #endif /* BITMAP_RB_ONLY */
- );
- if(new_raw_block != NULL) {
- new_raw_block->next_raw_block = systemallocator.raw_block_head;
- systemallocator.raw_block_head = new_raw_block;
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&systemallocator.creation_mutex);
- pthread_mutex_lock(&new_raw_block->mutex);
- #endif /* HAVE_LOCKS */
- encapsulated_rb =
- #ifdef BITMAP_RB_ONLY
- (bitmap_rb_t *)
- #endif /* BITMAP_RB_ONLY */
- #ifdef FL_RB_ONLY
- (freelist_rb_t *)
- #endif /* FL_RB_ONLY */
- ((char *)new_raw_block + sizeof(raw_block_header_t));
- ptr = dmmlib_malloc(encapsulated_rb, size);
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&new_raw_block->mutex);
- #endif /* HAVE_LOCKS */
- }
- }
- }
- TRACE_1("dmmlib - m %p %zu\n", ptr, size);
-
- return ptr;
- }
|