malloc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 malloc.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. *
  22. * @brief Implementation of malloc() call.
  23. */
  24. #include "dmmlib/dmmlib.h"
  25. #ifdef BITMAP_RB_ONLY
  26. #include "bitmap/bitmap.h"
  27. #include "bitmap/bitmap_rb.h"
  28. #endif /* BITMAP_RB_ONLY */
  29. #ifdef FL_RB_ONLY
  30. #include "freelist/freelist.h"
  31. #include "freelist/freelist_rb.h"
  32. #endif /* FL_RB_ONLY */
  33. #ifdef WITH_ALLOCATOR_STATS
  34. #include "statistics.h"
  35. #endif /* WITH_ALLOCATOR_STATS */
  36. #include "trace.h"
  37. void * malloc(size_t size) {
  38. raw_block_header_t *raw_block, *new_raw_block;
  39. #ifdef BITMAP_RB_ONLY
  40. bitmap_rb_t
  41. #endif /* BITMAP_RB_ONLY */
  42. #ifdef FL_RB_ONLY
  43. freelist_rb_t
  44. #endif /* FL_RB_ONLY */
  45. *encapsulated_rb;
  46. void *ptr;
  47. raw_block = systemallocator.raw_block_head;
  48. ptr = NULL;
  49. if(size == 0) {
  50. return NULL;
  51. }
  52. /* Try to find a raw block available for allocation */
  53. while(raw_block != NULL) {
  54. #ifdef HAVE_LOCKS
  55. pthread_mutex_lock(&raw_block->mutex);
  56. #endif /* HAVE_LOCKS */
  57. encapsulated_rb =
  58. #ifdef BITMAP_RB_ONLY
  59. (bitmap_rb_t *)
  60. #endif /* BITMAP_RB_ONLY */
  61. #ifdef FL_RB_ONLY
  62. (freelist_rb_t *)
  63. #endif /* FL_RB_ONLY */
  64. ((char *)raw_block + sizeof(raw_block_header_t));
  65. ptr = dmmlib_malloc(encapsulated_rb, size);
  66. #ifdef HAVE_LOCKS
  67. pthread_mutex_unlock(&raw_block->mutex);
  68. #endif /* HAVE_LOCKS */
  69. if(ptr != NULL) {
  70. break;
  71. }
  72. raw_block = raw_block->next_raw_block;
  73. }
  74. if(ptr == NULL) {
  75. #ifdef BITMAP_RB_ONLY
  76. /* Check if the request would fit in a new bitmap raw block
  77. * FIXME currently the raw block size and resolution are fixed
  78. */
  79. size_t bm_vector_size = BMAP_EL_SIZE *
  80. (SYS_ALLOC_SIZE + BMAP_EL_SIZE -
  81. sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
  82. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
  83. if(2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
  84. sizeof(bitmap_rb_t) - bm_vector_size) {
  85. #endif /* BITMAP_RB_ONLY */
  86. #ifdef FL_RB_ONLY
  87. if( 2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
  88. sizeof(freelist_rb_t)) {
  89. #endif /* FL_RB_ONLY */
  90. ptr = (void *)create_raw_block(size +
  91. sizeof(raw_block_header_t), BIGBLOCK);
  92. if(ptr != NULL) {
  93. #ifdef WITH_DEBUG
  94. #ifdef HAVE_LOCKS
  95. pthread_mutex_lock(&systemallocator.creation_mutex);
  96. #endif /* HAVE_LOCKS */
  97. ((raw_block_header_t *)ptr)->next_raw_block =
  98. systemallocator.big_blocks_head;
  99. systemallocator.big_blocks_head = (raw_block_header_t *)ptr;
  100. #ifdef HAVE_LOCKS
  101. pthread_mutex_unlock(&systemallocator.creation_mutex);
  102. #endif /* HAVE_LOCKS */
  103. #endif /* WITH_DEBUG */
  104. #ifdef WITH_ALLOCATOR_STATS
  105. update_stats(&systemallocator.dmm_stats,
  106. MALLOC,
  107. #ifdef REQUEST_SIZE_INFO
  108. size,
  109. #endif /* REQUEST_SIZE_INFO */
  110. size + sizeof(raw_block_header_t));
  111. #endif /* WITH_ALLOCATOR_STATS */
  112. ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));
  113. }
  114. } else {
  115. #ifdef HAVE_LOCKS
  116. pthread_mutex_lock(&systemallocator.creation_mutex);
  117. #endif /* HAVE_LOCKS */
  118. new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE,
  119. #ifdef FL_RB_ONLY
  120. FREELIST
  121. #endif /* FL_RB_ONLY */
  122. #ifdef BITMAP_RB_ONLY
  123. BITMAP
  124. #endif /* BITMAP_RB_ONLY */
  125. );
  126. if(new_raw_block != NULL) {
  127. new_raw_block->next_raw_block = systemallocator.raw_block_head;
  128. systemallocator.raw_block_head = new_raw_block;
  129. #ifdef HAVE_LOCKS
  130. pthread_mutex_unlock(&systemallocator.creation_mutex);
  131. pthread_mutex_lock(&new_raw_block->mutex);
  132. #endif /* HAVE_LOCKS */
  133. encapsulated_rb =
  134. #ifdef BITMAP_RB_ONLY
  135. (bitmap_rb_t *)
  136. #endif /* BITMAP_RB_ONLY */
  137. #ifdef FL_RB_ONLY
  138. (freelist_rb_t *)
  139. #endif /* FL_RB_ONLY */
  140. ((char *)new_raw_block + sizeof(raw_block_header_t));
  141. ptr = dmmlib_malloc(encapsulated_rb, size);
  142. #ifdef HAVE_LOCKS
  143. pthread_mutex_unlock(&new_raw_block->mutex);
  144. #endif /* HAVE_LOCKS */
  145. }
  146. }
  147. }
  148. TRACE_1("dmmlib - m %p %zu\n", ptr, size);
  149. return ptr;
  150. }