/* * 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 realloc.c * @author Ioannis Koutras (joko@microlab.ntua.gr) * @date September 2012 * * @brief Implementation of realloc() call. */ #include "dmmlib/dmmlib.h" #include #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 */ #include "release_memory.h" #include #include #include "trace.h" void * realloc(void *ptr, size_t size) { raw_block_header_t *current_raw_block; bool found; void *return_ptr; if(ptr == NULL) { return malloc(size); } if(size == 0) { free(ptr); return_ptr = malloc((size_t) 32); // FIXME 32 <- minimum size goto done; } current_raw_block = systemallocator.raw_block_head; found = false; while(current_raw_block) { if(((char *)ptr > (char *)current_raw_block) && ((char *)ptr < (char *)(current_raw_block) + current_raw_block->size)) { found = true; break; } current_raw_block = current_raw_block->next_raw_block; } if(found == true) { #ifdef BITMAP_RB_ONLY bitmap_rb_t #endif /* BITMAP_RB_ONLY */ #ifdef FL_RB_ONLY freelist_rb_t #endif /* FL_RB_ONLY */ *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 *)current_raw_block + sizeof(raw_block_header_t)); return_ptr = dmmlib_realloc(encapsulated_rb, ptr, size); goto done; } else { /* This has to be a big block */ // The new size is checked and if it is smaller than the one from the // original request, the remaining memory is simply released. If it is // greater, a new big block is initialized, data is copied there and the // old big block gets de-allocated. current_raw_block = (raw_block_header_t *)((char *)ptr - sizeof(raw_block_header_t)); size_t full_size = sizeof(raw_block_header_t) + size; if(full_size <= current_raw_block->size) { size_t remaining_size = current_raw_block->size - full_size; current_raw_block->size = full_size; // FIXME This is mmap-specific munmap((void *)((char *)current_raw_block + current_raw_block->size), remaining_size); #ifdef WITH_ALLOCATOR_STATS systemallocator.dmm_stats.total_mem_allocated -= remaining_size; TRACE_2("dmmlib - ms all %zu\n", systemallocator.dmm_stats.total_mem_allocated); #ifdef REQUEST_SIZE_INFO systemallocator.dmm_stats.total_mem_requested -= remaining_size; TRACE_2("dmmlib - ms req %zu\n", systemallocator.dmm_stats.total_mem_requested); #endif /* REQUEST_SIZE_INFO */ systemallocator.dmm_stats.num_realloc++; #endif /* WITH_ALLOCATOR_STATS */ return_ptr = ptr; goto done; } else { size_t size_diff = full_size - current_raw_block->size; raw_block_header_t *new_block = create_raw_block(full_size, BIGBLOCK); if(new_block == NULL) { return_ptr = NULL; goto done; } else { memcpy((void *)((char *)new_block + sizeof(raw_block_header_t)), ptr, current_raw_block->size - sizeof(raw_block_header_t)); release_memory(current_raw_block); #ifdef WITH_ALLOCATOR_STATS systemallocator.dmm_stats.total_mem_allocated += size_diff; TRACE_2("dmmlib - ms all %zu\n", systemallocator.dmm_stats.total_mem_allocated); #ifdef REQUEST_SIZE_INFO systemallocator.dmm_stats.total_mem_requested += size_diff; TRACE_2("dmmlib - ms req %zu\n", systemallocator.dmm_stats.total_mem_requested); #endif /* REQUEST_SIZE_INFO */ systemallocator.dmm_stats.num_realloc++; #endif /* WITH_ALLOCATOR_STATS */ return_ptr = (void *)((char *)new_block + sizeof(raw_block_header_t)); goto done; } } } done: TRACE_1("dmmlib - r %p %p %zu\n", ptr, return_ptr, size); return return_ptr; }