123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- * Copyright 2012 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 bitmap_free.c
- * @author Ilias Pliotas, Ioannis Koutras
- * @date September 2012
- * @brief free() implementation for bitmap-organized raw blocks
- */
- #include "bitmap/bitmap.h"
- #include "bitmap/bitmap_rb.h"
- #include "bitmap/bitmap_other.h"
- #include "trace.h"
- #ifdef HAVE_LOCKS
- #include <pthread.h>
- #endif /* HAVE_LOCKS */
- #ifdef WITH_ALLOCATOR_STATS
- #include "dmmlib/dmmlib.h"
- #endif /* WITH_ALLOCATOR_STATS */
- /** Frees the memory block inside of a specific bitmap-organized raw block.
- * @param raw_block The pointer of the raw block.
- * @param ptr The pointer of the memory block to be freed.
- */
- void bitmap_free(raw_block_header_t *raw_block, void *ptr) {
- bitmap_rb_t *rb_header;
- chunk_header_t *chunk_header;
- size_t cells_used, cell_no, bmap_index, start_pos;
- BMAP_EL_TYPE mask1, mask2, *bmap_p;
- rb_header = (bitmap_rb_t *)((char *)raw_block + sizeof(raw_block_header_t));
- chunk_header = (chunk_header_t *)((char *)ptr - CHUNK_HDR_SIZE);
- #ifdef HAVE_LOCKS
- pthread_mutex_lock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- cells_used = chunk_header->num_of_cells;
- #ifndef REQUEST_SIZE_INFO
- TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p\n",
- cells_used * rb_header->bytes_per_cell, (void *)raw_block);
- #else /* REQUEST_SIZE_INFO */
- TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p, out of "
- "which %zu were used by the application\n",
- cells_used * rb_header->bytes_per_cell,
- (void *)raw_block,
- chunk_header->requested_size);
- #endif /* REQUEST_SIZE_INFO */
- #ifdef WITH_ALLOCATOR_STATS
- systemallocator.dmm_stats.total_mem_allocated -= cells_used *
- rb_header->bytes_per_cell;
- TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
- systemallocator.dmm_stats.total_mem_allocated);
- #ifdef REQUEST_SIZE_INFO
- systemallocator.dmm_stats.total_mem_requested -= chunk_header->requested_size;
- TRACE_1("dmmlib - global requested memory: %zu bytes\n",
- systemallocator.dmm_stats.total_mem_requested);
- #endif /* REQUEST_SIZE_INFO */
- systemallocator.dmm_stats.live_objects--;
- systemallocator.dmm_stats.num_free++;
- #endif /* WITH_ALLOCATOR_STATS */
- #ifdef WITH_RAWBLOCK_STATS
- raw_block->dmm_stats.total_mem_allocated -= cells_used *
- rb_header->bytes_per_cell;
- TRACE_1("dmmlib - total allocated memory: %zu bytes\n",
- raw_block->dmm_stats.total_mem_allocated);
- #ifdef REQUEST_SIZE_INFO
- raw_block->dmm_stats.total_mem_requested -= chunk_header->requested_size;
- TRACE_1("dmmlib - total requested memory: %zu bytes\n",
- raw_block->dmm_stats.total_mem_requested);
- #endif /* REQUEST_SIZE_INFO */
- raw_block->dmm_stats.live_objects--;
- raw_block->dmm_stats.num_free++;
- #endif /* WITH_RAWBLOCK_STATS */
- cell_no = ((char *)chunk_header -
- ((char *)rb_header + sizeof(bitmap_rb_t) +
- (rb_header->elements - 1) * BMAP_EL_SIZE))
- / rb_header->bytes_per_cell;
- bmap_index = cell_no / BMAP_EL_SIZE_BITS;
- start_pos = cell_no % BMAP_EL_SIZE_BITS;
- bmap_p = &rb_header->bmap;
- // If the sum of the starting position and the cells that are used is more
- // than the available bits of one bitmap array element, then we have to
- // modify the next element as well.
- if(start_pos + cells_used <= BMAP_EL_SIZE_BITS) {
- mask1 = make_bit_mask(start_pos + 1, cells_used);
- } else {
- mask1 = make_bit_mask(start_pos + 1, BMAP_EL_SIZE_BITS - start_pos);
- mask2 = make_bit_mask((size_t) 1,
- start_pos + cells_used - BMAP_EL_SIZE_BITS);
- bmap_p[bmap_index + 1] |= mask2;
- }
- bmap_p[bmap_index] |= mask1;
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&raw_block->mutex);
- #endif /* HAVE_LOCKS */
- }
|