custom_malloc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2011 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/dmmlib.h>
  18. #include "dmm_config.h"
  19. #ifdef HAVE_LOCKS
  20. #include "posix_lock.h"
  21. #endif /* HAVE_LOCKS */
  22. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  23. #include "split.h"
  24. #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  25. #include <dmmlib/initialize_allocator.h>
  26. #include "other.h"
  27. #include "linked_lists/linked_lists.h"
  28. #include "linked_lists/search_algorithms.h"
  29. #include "sys_alloc.h"
  30. #include "block_header.h"
  31. #ifdef WITH_ADAPTIVITY
  32. #include "dmm_adaptor.h"
  33. #endif /* WITH_ADAPTIVITY */
  34. void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
  35. void *ptr;
  36. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  37. size_t new_size;
  38. size_t min_split_size;
  39. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  40. #ifndef WITH_MEMORY_SPACE_AWARENESS
  41. int heap_id;
  42. /* Go to the system allocator if none was given */
  43. if(allocator == NULL) {
  44. allocator = &systemallocator;
  45. if(allocator->initialized != true) {
  46. initialize_allocator(allocator);
  47. }
  48. }
  49. if(heap == NULL) {
  50. heap_id = map_thread_heap();
  51. heap = &allocator->heaps[heap_id];
  52. }
  53. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  54. ptr = NULL;
  55. #ifdef HAVE_LOCKS
  56. posix_lock(heap);
  57. #endif /* HAVE_LOCKS */
  58. #ifdef WITH_FIXED_LISTS
  59. ptr = search_on_fixed(heap, req_padding(size));
  60. if(ptr == NULL) {
  61. #endif /* WITH_FIXED_LISTS */
  62. ptr = best_fit_on_freelist(heap, size);
  63. #ifdef WITH_FIXED_LISTS
  64. }
  65. #endif /* WITH_FIXED_LISTS */
  66. if(ptr != NULL) {
  67. #ifdef FUTURE_FEATURES
  68. set_requested_size(ptr, size);
  69. #endif /* FUTURE_FEATURES */
  70. /* Try to split */
  71. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  72. /* Check what would be the size of the new block if we split the current
  73. * one.
  74. * Note: new_size is a size_t, so compare first in order to prevent an
  75. * underflow.
  76. */
  77. if(get_size(ptr) > size + HEADER_SIZE) {
  78. new_size = get_size(ptr) - size - HEADER_SIZE;
  79. } else {
  80. new_size = 0;
  81. }
  82. #ifdef SPLITTING_FIXED
  83. min_split_size = MIN_SPLITTING_SIZE;
  84. #endif /* SPLITTING_FIXED */
  85. #ifdef SPLITTING_VARIABLE
  86. min_split_size = heap->dmm_knobs.min_split_size;
  87. #endif /* SPLITTING_VARIABLE */
  88. if(new_size >= min_split_size) {
  89. split(allocator, heap, ptr, size);
  90. }
  91. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  92. mark_used(allocator, ptr);
  93. #ifdef FUTURE_FEATURES
  94. /* Update the used blocks list */
  95. push_block(&ptr, &heap->used_blocks_head);
  96. #endif /* FUTURE_FEATURES */
  97. #ifdef WITH_STATS
  98. /* Update statistics */
  99. heap->dmm_stats.mem_allocated += req_padding(size);
  100. heap->dmm_stats.mem_requested += size;
  101. heap->dmm_stats.live_objects += 1;
  102. heap->dmm_stats.num_malloc += 1;
  103. #endif /* WITH_STATS */
  104. /* FIXME To be refactored - END */
  105. } else {
  106. ptr = sys_alloc(allocator, heap, size);
  107. }
  108. #ifdef WITH_ADAPTIVITY
  109. /* Refresh the state of the heap allocator if a certain number of
  110. * malloc's has been served already
  111. */
  112. /* TODO Define 50 as a constant */
  113. if(heap->dmm_stats.num_malloc % 50) {
  114. malloc_state_refresh(heap);
  115. }
  116. #endif /* WITH_ADAPTIVITY */
  117. #ifdef HAVE_LOCKS
  118. posix_unlock(heap);
  119. #endif /* HAVE_LOCKS */
  120. return ptr;
  121. }
  122. /* Currently all the memory space aware allocators are pre-initialized, so
  123. * we do not expect any custom_ahmalloc call without knowing which allocator
  124. * and heap are to be used.
  125. */
  126. #ifndef WITH_MEMORY_SPACE_AWARENESS
  127. void * custom_malloc(size_t size) {
  128. return custom_ahmalloc(NULL, NULL, size);
  129. }
  130. #endif /* WITH_MEMORY_SPACE_AWARENESS */