123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- * 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 bitmap_realloc.c
- * @author Ilias Pliotas, Ioannis Koutras
- * @date September 2012
- * @brief realloc() implementation for bitmap-organized raw blocks
- */
- #include "bitmap/bitmap.h"
- #include "dmmlib/dmmlib.h"
- #include <string.h>
- #ifdef HAVE_LOCKS
- #include <pthread.h>
- #endif /* HAVE_LOCKS */
- /**
- * Reallocates a memory block from a bitmap-organized raw block
- *
- * @param raw_block The pointer of the bitmap raw block.
- * @param ptr The pointer of the memory block to be re-allocated.
- * @param req_size The requested memory size.
- * @retval The address to serve the request.
- * @retval NULL No available memory space.
- */
- void * bitmap_realloc(bitmap_rb_t *raw_block, void * ptr,
- size_t req_size) {
- void *ret;
- chunk_header_t *chunk_header;
- chunk_header = (chunk_header_t *)((char *)ptr - CHUNK_HDR_SIZE);
- ret = malloc(req_size);
- if(req_size > chunk_header->num_of_cells * raw_block->bytes_per_cell -
- CHUNK_HDR_SIZE) {
- ret = memmove(ret, ptr, chunk_header->num_of_cells *
- raw_block->bytes_per_cell - CHUNK_HDR_SIZE);
- } else {
- ret = memmove(ret, ptr, req_size);
- }
- #ifdef HAVE_LOCKS
- raw_block_header_t *rb;
- rb = (raw_block_header_t *)((char *)raw_block - sizeof(raw_block_header_t));
- pthread_mutex_lock(&rb->mutex);
- #endif /* HAVE_LOCKS */
- bitmap_free(raw_block, ptr);
- #ifdef HAVE_LOCKS
- pthread_mutex_unlock(&rb->mutex);
- #endif /* HAVE_LOCKS */
- return ret;
- }
|