raw_block.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include <sys/mman.h>
  20. #include "request_memory.h"
  21. #include "slab.h"
  22. #ifdef FL_RB_ONLY
  23. #include "dmmlib/freelist/freelist_rb.h"
  24. #include "dmmlib/freelist/initialize.h"
  25. #endif /* FL_RB_ONLY */
  26. #ifdef BITMAP_RB_ONLY
  27. #include "bitmap/bitmap_rb.h"
  28. #endif /* BITMAP_RB_ONLY */
  29. #include "locks.h"
  30. raw_block_header_t *create_raw_block(size_t raw_block_size, rb_type type) {
  31. raw_block_header_t *ptr;
  32. size_t padding;
  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. #ifdef REQUEST_SIZE_INFO
  40. size_t requested_size = raw_block_size - sizeof(raw_block_header_t);
  41. #endif /* REQUEST_SIZE_INFO */
  42. if(type == BIGBLOCK) {
  43. #ifdef PAGESIZE_ALIGN
  44. raw_block_size = (raw_block_size + SYS_PAGESIZE - 1) &
  45. (size_t) ~(SYS_PAGESIZE - 1);
  46. #else /* PAGESIZE_ALIGN */
  47. raw_block_size = (raw_block_size + SYS_ALLOC_SIZE - 1) &
  48. (size_t) ~(SYS_ALLOC_SIZE - 1);
  49. #endif /* PAGESIZE_ALIGN */
  50. }
  51. ptr = (raw_block_header_t *) request_memory(raw_block_size +
  52. SYS_ALLOC_SIZE - 1);
  53. if(ptr == NULL) {
  54. return NULL;
  55. }
  56. if((uintptr_t) ptr % SYS_ALLOC_SIZE != 0) {
  57. padding = (- (uintptr_t) ptr) & (SYS_ALLOC_SIZE - 1);
  58. munmap((void *) ptr, padding);
  59. ptr = (raw_block_header_t *) ((uintptr_t) ptr + padding);
  60. } else {
  61. padding = 0;
  62. }
  63. INIT_DEALY_LOCK(ptr->lock);
  64. DEALY_LOCK(ptr->lock);
  65. ptr->type = type;
  66. ptr->size = raw_block_size + SYS_ALLOC_SIZE - 1 - padding;
  67. switch(type) {
  68. case SLAB:
  69. initialize_slab((char *) ptr + sizeof(raw_block_header_t),
  70. raw_block_size - sizeof(raw_block_header_t));
  71. break;
  72. #ifdef FL_RB_ONLY
  73. case FREELIST:
  74. initialize_freelist((char *) ptr + sizeof(raw_block_header_t),
  75. raw_block_size - sizeof(raw_block_header_t));
  76. break;
  77. #endif /* FL_RB_ONLY */
  78. #ifdef BITMAP_RB_ONLY
  79. case BITMAP:
  80. bitmap_rb = (bitmap_rb_t *)((char *)ptr +
  81. sizeof(raw_block_header_t));
  82. bitmap_rb->bytes_per_cell = BITMAP_RESOLUTION;
  83. vector_elements =
  84. (raw_block_size - sizeof(raw_block_header_t) -
  85. sizeof(bitmap_rb_t)) /
  86. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * bitmap_rb->bytes_per_cell);
  87. remaining_cells =
  88. ((raw_block_size - sizeof(raw_block_header_t) -
  89. sizeof(bitmap_rb_t)) %
  90. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * bitmap_rb->bytes_per_cell))
  91. / bitmap_rb->bytes_per_cell;
  92. if(remaining_cells == 0) {
  93. bitmap_rb->elements = vector_elements;
  94. } else {
  95. bitmap_rb->elements = vector_elements + 1;
  96. }
  97. /* Initialize the bitmap vector just right after the raw block
  98. * header */
  99. bitmap_p = (BMAP_EL_TYPE *)((char *)bitmap_rb +
  100. sizeof(bitmap_rb_t));
  101. for(size_t i = 0; i < bitmap_rb->elements; ++i) {
  102. /* If there are some remaining cells, so an extra bitmap vector
  103. * element is used, the cells that cannot be used by the
  104. * application, have to be set as already used.
  105. */
  106. if(i == bitmap_rb->elements - 1 && remaining_cells != 0) {
  107. *bitmap_p = (BMAP_EL_TYPE) 1;
  108. *bitmap_p <<= remaining_cells;
  109. *bitmap_p -= 1;
  110. } else {
  111. *bitmap_p = BMAP_EL_INIT_VAL;
  112. }
  113. bitmap_p++;
  114. }
  115. break;
  116. #endif /* BITMAP_RB_ONLY */
  117. case BIGBLOCK:
  118. #ifdef REQUEST_SIZE_INFO
  119. ptr->requested_size = requested_size;
  120. #endif /* REQUEST_SIZE_INFO */
  121. break;
  122. }
  123. DEALY_UNLOCK(ptr->lock);
  124. return ptr;
  125. }