bitmap_free.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /** Frees the memory block inside of a specific bitmap-organized raw block.
  31. * @param raw_block The pointer of the raw block.
  32. * @param ptr The pointer of the memory block to be freed.
  33. */
  34. void bitmap_free(raw_block_header_t *raw_block, void *ptr) {
  35. bitmap_rb_t *rb_header;
  36. chunk_header_t *chunk_header;
  37. size_t cells_used, cell_no, bmap_index, start_pos;
  38. BMAP_EL_TYPE mask1, mask2;
  39. rb_header = (bitmap_rb_t *)((char *)raw_block + sizeof(raw_block_header_t));
  40. chunk_header = (chunk_header_t *)((char *)ptr - sizeof(chunk_header_t));
  41. #ifdef HAVE_LOCKS
  42. pthread_mutex_lock(&raw_block->mutex);
  43. #endif /* HAVE_LOCKS */
  44. cells_used = chunk_header->num_of_cells;
  45. #ifndef REQUEST_SIZE_INFO
  46. TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p\n",
  47. cells_used * rb_header->bytes_per_cell, (void *)raw_block);
  48. #else /* REQUEST_SIZE_INFO */
  49. TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p, out of "
  50. "which %zu were used by the application\n",
  51. cells_used * rb_header->bytes_per_cell,
  52. (void *)raw_block,
  53. chunk_header->requested_size);
  54. #endif /* REQUEST_SIZE_INFO */
  55. cell_no = ((char *)chunk_header - ((char *)rb_header + sizeof(bitmap_rb_t)))
  56. / rb_header->bytes_per_cell;
  57. bmap_index = cell_no / BMAP_EL_SIZE_BITS;
  58. start_pos = cell_no % BMAP_EL_SIZE_BITS;
  59. // If the sum of the starting position and the cells that are used is more
  60. // than the available bits of one bitmap array element, then we have to
  61. // modify the next element as well.
  62. if(start_pos + cells_used <= BMAP_EL_SIZE_BITS) {
  63. mask1 = make_bit_mask(start_pos + 1, cells_used);
  64. } else {
  65. mask1 = make_bit_mask(start_pos + 1, BMAP_EL_SIZE_BITS - start_pos);
  66. mask2 = make_bit_mask((size_t) 1,
  67. start_pos + cells_used - BMAP_EL_SIZE_BITS);
  68. rb_header->bmap_array[bmap_index + 1] |= mask2;
  69. }
  70. rb_header->bmap_array[bmap_index] |= mask1;
  71. #ifdef HAVE_LOCKS
  72. pthread_mutex_unlock(&raw_block->mutex);
  73. #endif /* HAVE_LOCKS */
  74. }