bitmap_realloc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file bitmap_realloc.c
  19. * @author Ilias Pliotas, Ioannis Koutras
  20. * @date September 2012
  21. * @brief realloc() implementation for bitmap-organized raw blocks
  22. */
  23. #include "bitmap/bitmap.h"
  24. #include "dmmlib/dmmlib.h"
  25. #include <string.h>
  26. #ifdef HAVE_LOCKS
  27. #include <pthread.h>
  28. #endif /* HAVE_LOCKS */
  29. /**
  30. * Reallocates a memory block from a bitmap-organized raw block
  31. *
  32. * @param raw_block The pointer of the bitmap raw block.
  33. * @param ptr The pointer of the memory block to be re-allocated.
  34. * @param req_size The requested memory size.
  35. * @retval The address to serve the request.
  36. * @retval NULL No available memory space.
  37. */
  38. void * bitmap_realloc(bitmap_rb_t *raw_block, void * ptr,
  39. size_t req_size) {
  40. void *ret;
  41. chunk_header_t *chunk_header;
  42. chunk_header = (chunk_header_t *)((char *)ptr - CHUNK_HDR_SIZE);
  43. ret = malloc(req_size);
  44. if(req_size > chunk_header->num_of_cells * raw_block->bytes_per_cell -
  45. CHUNK_HDR_SIZE) {
  46. ret = memmove(ret, ptr, chunk_header->num_of_cells *
  47. raw_block->bytes_per_cell - CHUNK_HDR_SIZE);
  48. } else {
  49. ret = memmove(ret, ptr, req_size);
  50. }
  51. #ifdef HAVE_LOCKS
  52. raw_block_header_t *rb;
  53. rb = (raw_block_header_t *)((char *)raw_block - sizeof(raw_block_header_t));
  54. pthread_mutex_lock(&rb->mutex);
  55. #endif /* HAVE_LOCKS */
  56. bitmap_free(raw_block, ptr);
  57. #ifdef HAVE_LOCKS
  58. pthread_mutex_unlock(&rb->mutex);
  59. #endif /* HAVE_LOCKS */
  60. return ret;
  61. }