/* * 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 #endif /* HAVE_LOCKS */ /** 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; rb_header = (bitmap_rb_t *)((char *)raw_block + sizeof(raw_block_header_t)); chunk_header = (chunk_header_t *)((char *)ptr - sizeof(chunk_header_t)); #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 */ cell_no = ((char *)chunk_header - ((char *)rb_header + sizeof(bitmap_rb_t))) / rb_header->bytes_per_cell; bmap_index = cell_no / BMAP_EL_SIZE_BITS; start_pos = cell_no % BMAP_EL_SIZE_BITS; // 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); rb_header->bmap_array[bmap_index + 1] |= mask2; } rb_header->bmap_array[bmap_index] |= mask1; #ifdef HAVE_LOCKS pthread_mutex_unlock(&raw_block->mutex); #endif /* HAVE_LOCKS */ }