| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- * 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 "freelist/freelist.h"
- #ifdef HAVE_LOCKS
- #include <pthread.h>
- #endif /* HAVE_LOCKS */
- #include "freelist/block_header_funcs.h"
- #include "freelist/linked_lists/linked_lists.h"
- #include "freelist/linked_lists/search_algorithms.h"
- #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
- #include "freelist/split.h"
- #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
- #if defined (BEST_FIT)
- #define search_on_free(size) best_fit_on_freelist(rb_header, size)
- #elif defined (GOOD_FIT)
- #define search_on_free(size) good_fit_on_freelist(rb_header, size)
- #elif defined (EXACT_FIT)
- #define search_on_free(size) exact_fit_on_freelist(rb_header, size)
- #elif defined (FIRST_FIT)
- #define search_on_free(size) first_fit_on_freelist(rb_header, size)
- #endif /* BEST_FIT */
- size_t req_padding(size_t size);
- size_t req_padding(size_t size) {
- size_t align;
- if(size <= 32)
- return 32;
- if(size <= 64)
- return 64;
- if(size <= 128)
- return 128;
- if(size <= 256)
- return 256;
- align = size % 4;
- if(align != 0) {
- size += 4 - align;
- }
- return size;
- }
- /** Tries to allocate memory from a specific free-list organized raw block.
- * @param raw_block The pointer of the raw block.
- * @param size The requested size.
- * @retval The address of the returned memory space.
- */
- void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
- freelist_rb_t *rb_header;
- block_header_t *ptr;
- size_t allocation_size, previous_size, previous_size_availability;
- rb_header = (freelist_rb_t *)((char *)raw_block +
- sizeof(raw_block_header_t));
- ptr = NULL;
- #ifdef HAVE_LOCKS
- pthread_mutex_lock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- ptr = search_on_free(size);
- if(ptr != NULL) {
- #ifdef REQUEST_SIZE_INFO
- set_requested_size(ptr, size);
- #endif /* REQUEST_SIZE_INFO */
- /* Try to split */
- #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
- split(rb_header, ptr, size);
- #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
- mark_used(rb_header, ptr);
- } else {
- allocation_size = req_padding(size) + HEADER_SIZE;
- if(allocation_size < (char *) raw_block + raw_block->size -
- (char *) rb_header->border_ptr -
- get_size(rb_header->border_ptr)) {
- if(rb_header->border_ptr == (block_header_t *)((char *)rb_header +
- sizeof(freelist_rb_t))) {
- previous_size_availability = 1; // Occupied and of 0 size
- ptr = (block_header_t *)((char *)rb_header +
- sizeof(freelist_rb_t));
- } else {
- previous_size = get_size(rb_header->border_ptr);
- previous_size_availability =
- get_size_availability(rb_header->border_ptr);
- ptr = (block_header_t *)((char *)rb_header->border_ptr +
- previous_size);
- }
- rb_header->border_ptr = ptr;
- // Update block metadata
- set_size_and_used(rb_header, ptr, req_padding(size));
- set_previous_size_availability(ptr, previous_size_availability);
- #ifdef REQUEST_SIZE_INFO
- set_requested_size(ptr, req_padding(size));
- #endif /* REQUEST_SIZE_INFO */
- }
- }
- #ifdef WITH_RAWBLOCK_STATS
- if(ptr != NULL) {
- #if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
- raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
- #endif /* !((SPLITTING_FIXED) || (SPLITTING_VARIABLE)) */
- raw_block->dmm_stats.live_objects++;
- raw_block->dmm_stats.num_malloc++;
- #ifdef REQUEST_SIZE_INFO
- raw_block->dmm_stats.total_mem_requested += size;
- #endif /* REQUEST_SIZE_INFO */
- }
- #endif /* WITH_RAWBLOCK_STATS */
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- if(ptr != NULL) {
- return (void *)((char *)ptr + HEADER_SIZE);
- } else {
- return NULL;
- }
- }
|