malloc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <inttypes.h>
  26. #include "dmmlib/lists.h"
  27. #include "locks.h"
  28. #include "default_rb.h"
  29. #ifdef WITH_ALLOCATOR_STATS
  30. #include "statistics.h"
  31. #endif /* WITH_ALLOCATOR_STATS */
  32. #ifdef WITH_DEBUG
  33. #include "debug.h"
  34. #endif /* WITH_DEBUG */
  35. #include "trace.h"
  36. void * malloc(size_t size) {
  37. raw_block_header_t *raw_block, *new_raw_block;
  38. DEFAULT_RB_T *encapsulated_rb;
  39. void *ptr;
  40. ptr = NULL;
  41. if(size == 0) {
  42. return NULL;
  43. }
  44. /* Try to find a raw block available for allocation */
  45. SLIST_FOREACH(raw_block, &systemallocator.rb_head, pointers) {
  46. lock_raw_block(raw_block);
  47. encapsulated_rb = (DEFAULT_RB_T *)
  48. ((uintptr_t) raw_block + sizeof(raw_block_header_t));
  49. ptr = dmmlib_malloc(encapsulated_rb, size);
  50. unlock_raw_block(raw_block);
  51. if(ptr != NULL) {
  52. break;
  53. }
  54. }
  55. if(ptr == NULL) {
  56. #ifdef BITMAP_RB_ONLY
  57. /* Check if the request would fit in a new bitmap raw block
  58. * FIXME currently the raw block size and resolution are fixed
  59. */
  60. size_t bm_vector_size = BMAP_EL_SIZE *
  61. (SYS_ALLOC_SIZE + BMAP_EL_SIZE -
  62. sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
  63. (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
  64. if(2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
  65. sizeof(bitmap_rb_t) - bm_vector_size) {
  66. #endif /* BITMAP_RB_ONLY */
  67. #ifdef FL_RB_ONLY
  68. if( 2 * size > SYS_ALLOC_SIZE - sizeof(raw_block_header_t) -
  69. sizeof(freelist_rb_t)) {
  70. #endif /* FL_RB_ONLY */
  71. lock_global();
  72. ptr = (void *)create_raw_block(size +
  73. sizeof(raw_block_header_t), BIGBLOCK);
  74. if(ptr != NULL) {
  75. #ifdef WITH_DEBUG
  76. SLIST_INSERT_HEAD(&systemallocator.bb_head,
  77. (raw_block_header_t *) ptr, pointers);
  78. #endif /* WITH_DEBUG */
  79. #ifdef WITH_ALLOCATOR_STATS
  80. update_stats(&systemallocator.dmm_stats,
  81. MALLOC,
  82. #ifdef REQUEST_SIZE_INFO
  83. size,
  84. #endif /* REQUEST_SIZE_INFO */
  85. size + sizeof(raw_block_header_t));
  86. #endif /* WITH_ALLOCATOR_STATS */
  87. unlock_global();
  88. ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
  89. }
  90. unlock_global();
  91. } else {
  92. lock_global();
  93. new_raw_block = create_raw_block((size_t) SYS_ALLOC_SIZE,
  94. DEFAULT_RB_TYPE);
  95. if(new_raw_block != NULL) {
  96. lock_raw_block(new_raw_block);
  97. SLIST_INSERT_HEAD(&systemallocator.rb_head, new_raw_block, pointers);
  98. unlock_global();
  99. encapsulated_rb = (DEFAULT_RB_T *)
  100. ((uintptr_t) new_raw_block + sizeof(raw_block_header_t));
  101. ptr = dmmlib_malloc(encapsulated_rb, size);
  102. unlock_raw_block(new_raw_block);
  103. }
  104. }
  105. }
  106. TRACE_1("dmmlib - m %p %zu\n", ptr, size);
  107. return ptr;
  108. }