raw_block.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "dmmlib/raw_block.h"
  18. #include <inttypes.h>
  19. #ifdef PAGESIZE_ALIGN
  20. #include <unistd.h> /* for pagesize */
  21. #endif /* PAGESIZE_ALIGN */
  22. #include "request_memory.h"
  23. #ifdef FL_RB_ONLY
  24. #include "dmmlib/freelist/freelist_rb.h"
  25. #include "dmmlib/freelist/initialize.h"
  26. #endif /* FL_RB_ONLY */
  27. #ifdef BITMAP_RB_ONLY
  28. #include "bitmap/bitmap_rb.h"
  29. #endif /* BITMAP_RB_ONLY */
  30. #include "locks.h"
  31. raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
  32. raw_block_header_t *ptr;
  33. #ifdef BITMAP_RB_ONLY
  34. bitmap_rb_t *bitmap_rb;
  35. BMAP_EL_TYPE *bitmap_p;
  36. size_t vector_elements;
  37. size_t remaining_cells;
  38. #endif /* BITMAP_RB_ONLY */
  39. // In case mmap() function is used, align the requested size to multiple of
  40. // pagesizes
  41. #ifdef PAGESIZE_ALIGN
  42. size_t pagesize = (size_t) sysconf(_SC_PAGESIZE);
  43. raw_block_size = pagesize * ((raw_block_size + pagesize - 1) / pagesize);
  44. #endif /* PAGESIZE_ALIGN */
  45. ptr = (raw_block_header_t *)request_memory(raw_block_size);
  46. if(ptr == NULL) {
  47. return NULL;
  48. }
  49. init_raw_block_lock(ptr);
  50. lock_raw_block(ptr);
  51. #ifdef REQUEST_SIZE_INFO
  52. ptr->requested_size = raw_block_size;
  53. #endif /* REQUEST_SIZE_INFO */
  54. ptr->size = raw_block_size;
  55. switch(type) {
  56. #ifdef FL_RB_ONLY
  57. case FREELIST:
  58. initialize_freelist((char *) ptr + sizeof(raw_block_header_t),
  59. raw_block_size - sizeof(raw_block_header_t));
  60. break;
  61. #endif /* FL_RB_ONLY */
  62. #ifdef BITMAP_RB_ONLY
  63. case BITMAP:
  64. bitmap_rb = (bitmap_rb_t *)((char *)ptr +
  65. sizeof(raw_block_header_t));
  66. bitmap_rb->bytes_per_cell = BITMAP_RESOLUTION;
  67. vector_elements =
  68. (raw_block_size - sizeof(raw_block_header_t) -
  69. sizeof(bitmap_rb_t)) /
  70. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * bitmap_rb->bytes_per_cell);
  71. remaining_cells =
  72. ((raw_block_size - sizeof(raw_block_header_t) -
  73. sizeof(bitmap_rb_t)) %
  74. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * bitmap_rb->bytes_per_cell))
  75. / bitmap_rb->bytes_per_cell;
  76. if(remaining_cells == 0) {
  77. bitmap_rb->elements = vector_elements;
  78. } else {
  79. bitmap_rb->elements = vector_elements + 1;
  80. }
  81. /* Initialize the bitmap vector just right after the raw block
  82. * header */
  83. bitmap_p = (BMAP_EL_TYPE *)((char *)bitmap_rb +
  84. sizeof(bitmap_rb_t));
  85. for(size_t i = 0; i < bitmap_rb->elements; ++i) {
  86. /* If there are some remaining cells, so an extra bitmap vector
  87. * element is used, the cells that cannot be used by the
  88. * application, have to be set as already used.
  89. */
  90. if(i == bitmap_rb->elements - 1 && remaining_cells != 0) {
  91. *bitmap_p = (BMAP_EL_TYPE) 1;
  92. *bitmap_p <<= remaining_cells;
  93. *bitmap_p -= 1;
  94. } else {
  95. *bitmap_p = BMAP_EL_INIT_VAL;
  96. }
  97. bitmap_p++;
  98. }
  99. break;
  100. #endif /* BITMAP_RB_ONLY */
  101. case BIGBLOCK:
  102. #ifdef REQUEST_SIZE_INFO
  103. ptr->requested_size -= sizeof(raw_block_header_t);
  104. #endif /* REQUEST_SIZE_INFO */
  105. break;
  106. }
  107. unlock_raw_block(ptr);
  108. return ptr;
  109. }