malloc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 src/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. #include <inttypes.h>
  26. #include <assert.h>
  27. #include "tls_allocator.h"
  28. #include "dmmlib/debug.h"
  29. #include "dmmlib/lists.h"
  30. #include "locks.h"
  31. #include "default_rb.h"
  32. #include "dmmlib_trace.h"
  33. #include "statistics.h"
  34. void * malloc(size_t size) {
  35. size_t sys_alloc_size;
  36. raw_block_header_t *raw_block, *new_raw_block;
  37. DEFAULT_RB_T *encapsulated_rb;
  38. void *ptr;
  39. if(size == 0) {
  40. return (void *) 0xD34D;
  41. }
  42. get_tls_allocator();
  43. #ifdef WITH_KNOBS
  44. sys_alloc_size = (size_t) tls_allocator->dmm_knobs.sys_alloc_size;
  45. #else /* WITH_KNOBS */
  46. sys_alloc_size = SYS_ALLOC_SIZE;
  47. #endif /* WITH_KNOBS */
  48. /* Check if the size is appropriate for the use of a freelist-organized or
  49. * bitmap-organized raw block. */
  50. #ifdef BITMAP_RB_ONLY
  51. /* FIXME currently the raw block size and resolution are fixed */
  52. size_t bm_vector_size = BMAP_EL_SIZE * (sys_alloc_size + BMAP_EL_SIZE -
  53. sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
  54. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
  55. if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
  56. sizeof(bitmap_rb_t) - bm_vector_size) {
  57. #endif /* BITMAP_RB_ONLY */
  58. #ifdef FL_RB_ONLY
  59. if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
  60. sizeof(freelist_rb_t)) {
  61. #endif /* FL_RB_ONLY */
  62. ptr = NULL;
  63. /* Try to find a raw block available for allocation */
  64. SLIST_FOREACH(raw_block, &tls_allocator->rb_head, pointers) {
  65. #ifdef TRYLOCK_ON_MALLOC
  66. if(DEALY_TRYLOCK(raw_block->lock) == 0) {
  67. #else /* TRYLOCK_ON_MALLOC */
  68. DEALY_LOCK(raw_block->lock);
  69. #endif /* TRYLOCK_ON_MALLOC */
  70. encapsulated_rb = (DEFAULT_RB_T *)
  71. ((uintptr_t) raw_block + sizeof(raw_block_header_t));
  72. ptr = dmmlib_malloc(encapsulated_rb, size);
  73. DEALY_UNLOCK(raw_block->lock);
  74. if(ptr != NULL) {
  75. /* Check that a valid pointer has been returned */
  76. assert(((uintptr_t) raw_block < (uintptr_t) ptr) &&
  77. ((uintptr_t) ptr < (uintptr_t) raw_block +
  78. raw_block->size + sizeof(raw_block_header_t)));
  79. break;
  80. }
  81. #ifdef TRYLOCK_ON_MALLOC
  82. }
  83. #endif /* TRYLOCK_ON_MALLOC */
  84. }
  85. if(ptr == NULL) {
  86. /* No raw block was found, try to create a new one and allocate a
  87. * memory block from there. */
  88. new_raw_block = create_raw_block(sys_alloc_size, DEFAULT_RB_TYPE);
  89. if(new_raw_block != NULL) {
  90. SLIST_INSERT_HEAD(&tls_allocator->rb_head, new_raw_block,
  91. pointers);
  92. encapsulated_rb = (DEFAULT_RB_T *)
  93. ((uintptr_t) new_raw_block + sizeof(raw_block_header_t));
  94. ptr = dmmlib_malloc(encapsulated_rb, size);
  95. }
  96. }
  97. } else {
  98. /* The memory request size is too large to serve it inside a
  99. * freelist-organized or bitmap-organized raw block, so a big raw block
  100. * shall be used. */
  101. ptr = create_raw_block(size + sizeof(raw_block_header_t), BIGBLOCK);
  102. if(ptr != NULL) {
  103. ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
  104. }
  105. }
  106. MEM_TRACE("dmmlib - m %p %zu\n", ptr, size);
  107. if(ptr != NULL) {
  108. #ifdef REQUEST_SIZE_INFO
  109. UPDATE_GLOBAL_STATS(MALLOC, size);
  110. #else /* REQUEST_SIZE_INFO */
  111. UPDATE_GLOBAL_STATS(MALLOC);
  112. #endif /* REQUEST_SIZE_INFO */
  113. }
  114. return ptr;
  115. }