bitmap_free.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2012 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_free.c
  19. * @author Ilias Pliotas, Ioannis Koutras
  20. * @date September 2012
  21. * @brief free() implementation for bitmap-organized raw blocks
  22. */
  23. #include "bitmap/bitmap.h"
  24. #include "bitmap/bitmap_rb.h"
  25. #include "bitmap/bitmap_other.h"
  26. #include "trace.h"
  27. #ifdef HAVE_LOCKS
  28. #include <pthread.h>
  29. #endif /* HAVE_LOCKS */
  30. #ifdef WITH_ALLOCATOR_STATS
  31. #include "dmmlib/dmmlib.h"
  32. #endif /* WITH_ALLOCATOR_STATS */
  33. /** Frees the memory block inside of a specific bitmap-organized raw block.
  34. * @param raw_block The pointer of the raw block.
  35. * @param ptr The pointer of the memory block to be freed.
  36. */
  37. void bitmap_free(raw_block_header_t *raw_block, void *ptr) {
  38. bitmap_rb_t *rb_header;
  39. chunk_header_t *chunk_header;
  40. size_t cells_used, cell_no, bmap_index, start_pos;
  41. BMAP_EL_TYPE mask1, mask2, *bmap_p;
  42. rb_header = (bitmap_rb_t *)((char *)raw_block + sizeof(raw_block_header_t));
  43. chunk_header = (chunk_header_t *)((char *)ptr - CHUNK_HDR_SIZE);
  44. #ifdef HAVE_LOCKS
  45. pthread_mutex_lock(&raw_block->mutex);
  46. #endif /* HAVE_LOCKS */
  47. cells_used = chunk_header->num_of_cells;
  48. #ifndef REQUEST_SIZE_INFO
  49. TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p\n",
  50. cells_used * rb_header->bytes_per_cell, (void *)raw_block);
  51. #else /* REQUEST_SIZE_INFO */
  52. TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p, out of "
  53. "which %zu were used by the application\n",
  54. cells_used * rb_header->bytes_per_cell,
  55. (void *)raw_block,
  56. chunk_header->requested_size);
  57. #endif /* REQUEST_SIZE_INFO */
  58. #ifdef WITH_ALLOCATOR_STATS
  59. systemallocator.dmm_stats.total_mem_allocated -= cells_used *
  60. rb_header->bytes_per_cell;
  61. TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
  62. systemallocator.dmm_stats.total_mem_allocated);
  63. #ifdef REQUEST_SIZE_INFO
  64. systemallocator.dmm_stats.total_mem_requested -= chunk_header->requested_size;
  65. TRACE_1("dmmlib - global requested memory: %zu bytes\n",
  66. systemallocator.dmm_stats.total_mem_requested);
  67. #endif /* REQUEST_SIZE_INFO */
  68. systemallocator.dmm_stats.live_objects--;
  69. systemallocator.dmm_stats.num_free++;
  70. #endif /* WITH_ALLOCATOR_STATS */
  71. #ifdef WITH_RAWBLOCK_STATS
  72. raw_block->dmm_stats.total_mem_allocated -= cells_used *
  73. rb_header->bytes_per_cell;
  74. TRACE_1("dmmlib - total allocated memory: %zu bytes\n",
  75. raw_block->dmm_stats.total_mem_allocated);
  76. #ifdef REQUEST_SIZE_INFO
  77. raw_block->dmm_stats.total_mem_requested -= chunk_header->requested_size;
  78. TRACE_1("dmmlib - total requested memory: %zu bytes\n",
  79. raw_block->dmm_stats.total_mem_requested);
  80. #endif /* REQUEST_SIZE_INFO */
  81. raw_block->dmm_stats.live_objects--;
  82. raw_block->dmm_stats.num_free++;
  83. #endif /* WITH_RAWBLOCK_STATS */
  84. cell_no = ((char *)chunk_header -
  85. ((char *)rb_header + sizeof(bitmap_rb_t) +
  86. (rb_header->elements - 1) * BMAP_EL_SIZE))
  87. / rb_header->bytes_per_cell;
  88. bmap_index = cell_no / BMAP_EL_SIZE_BITS;
  89. start_pos = cell_no % BMAP_EL_SIZE_BITS;
  90. bmap_p = &rb_header->bmap;
  91. // If the sum of the starting position and the cells that are used is more
  92. // than the available bits of one bitmap array element, then we have to
  93. // modify the next element as well.
  94. if(start_pos + cells_used <= BMAP_EL_SIZE_BITS) {
  95. mask1 = make_bit_mask(start_pos + 1, cells_used);
  96. } else {
  97. mask1 = make_bit_mask(start_pos + 1, BMAP_EL_SIZE_BITS - start_pos);
  98. mask2 = make_bit_mask((size_t) 1,
  99. start_pos + cells_used - BMAP_EL_SIZE_BITS);
  100. bmap_p[bmap_index + 1] |= mask2;
  101. }
  102. bmap_p[bmap_index] |= mask1;
  103. #ifdef HAVE_LOCKS
  104. pthread_mutex_unlock(&raw_block->mutex);
  105. #endif /* HAVE_LOCKS */
  106. }