initialize_allocator.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef WITH_MEMORY_SPACE_AWARENESS
  18. #include <unistd.h>
  19. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  20. #include <dmmlib/initialize_allocator.h>
  21. #ifdef HAVE_LOCKS
  22. #include "posix_lock.h"
  23. #endif /* HAVE_LOCKS */
  24. #include "sys_alloc.h"
  25. #if defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT)
  26. #include <string.h> /* for strcmp() */
  27. #include "linked_lists/search_algorithms.h"
  28. #endif /* defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT) */
  29. #ifdef WITH_MEMORY_SPACE_AWARENESS
  30. void initialize_allocator(allocator_t *allocator, void *starting_address,
  31. size_t size) {
  32. #else
  33. void initialize_allocator(allocator_t *allocator) {
  34. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  35. int i;
  36. #ifdef WITH_FIXED_LISTS
  37. heap_t *current_heap;
  38. maptable_node_t *maptablenode;
  39. #endif /* WITH_FIXED_LISTS */
  40. #ifdef HAVE_LOCKS
  41. sbrk_lock();
  42. #endif /* HAVE_LOCKS */
  43. for(i = 0; i < NUM_HEAPS; i++) {
  44. #ifdef HEAP_VAR_FIT
  45. if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) {
  46. allocator->heaps[i].dmm_knobs.search_policy = &best_fit_on_freelist;
  47. } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) {
  48. allocator->heaps[i].dmm_knobs.search_policy = &good_fit_on_freelist;
  49. } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) {
  50. allocator->heaps[i].dmm_knobs.search_policy = &exact_fit_on_freelist;
  51. } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) {
  52. allocator->heaps[i].dmm_knobs.search_policy = &first_fit_on_freelist;
  53. }
  54. #endif /* HEAP_VAR_FIT */
  55. #ifdef WITH_FIXED_LISTS
  56. allocator->heaps[i].maptable_head = NULL;
  57. #endif /* WITH_FIXED_LISTS */
  58. allocator->heaps[i].free_list_head = NULL;
  59. #ifdef FUTURE_FEATURES
  60. allocator->heaps[i].used_blocks_head = NULL;
  61. allocator->heaps[i].rov_ptr = NULL;
  62. #endif /* FUTURE_FEATURES */
  63. allocator->heaps[i].num_objects = 0;
  64. #ifdef WITH_STATS
  65. allocator->heaps[i].dmm_stats.mem_allocated = 0;
  66. allocator->heaps[i].dmm_stats.mem_requested = 0;
  67. allocator->heaps[i].dmm_stats.live_objects = 0;
  68. #ifdef COUNT_ACCESSES
  69. allocator->heaps[i].dmm_stats.read_mem_accesses = 0;
  70. allocator->heaps[i].dmm_stats.write_mem_accesses = 0;
  71. #endif /* COUNT_ACCESSES */
  72. allocator->heaps[i].dmm_stats.num_malloc = 0;
  73. allocator->heaps[i].dmm_stats.num_free = 0;
  74. #endif /* WITH_STATS */
  75. #ifdef WITH_KNOBS
  76. /* Knobs initialization */
  77. #ifdef GOOD_FIT
  78. allocator->heaps[i].dmm_knobs.fit_percentage = FIT_PERCENTAGE;
  79. #endif /* GOOD_FIT */
  80. #ifdef COALESCING_VARIABLE
  81. allocator->heaps[i].dmm_knobs.max_coalesce_size = MAX_COALESCE_SIZE;
  82. #endif /* COALESCING_VARIABLE */
  83. #ifdef SPLITTING_VARIABLE
  84. allocator->heaps[i].dmm_knobs.min_split_size = MIN_SPLITTING_SIZE;
  85. #endif /* SPLITTING_VARIABLE */
  86. /* FIXME Create a constant for the initial value of the next
  87. * variables
  88. */
  89. allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
  90. allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
  91. #endif /* WITH_KNOBS */
  92. #ifdef HAVE_LOCKS
  93. pthread_mutex_init(&allocator->heaps[i].mutex, NULL);
  94. #endif /* HAVE_LOCKS */
  95. }
  96. #ifndef WITH_MEMORY_SPACE_AWARENESS
  97. allocator->border_ptr = NULL;
  98. allocator->remaining_size = 0;
  99. #else
  100. allocator->border_ptr = starting_address;
  101. allocator->remaining_size = size;
  102. #endif /* WITH_MEMORY_SPACE_AWARENESS */
  103. allocator->initialized = false;
  104. #ifdef ALLOC_VAR_FIT
  105. if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) {
  106. allocator->search_policy = &best_fit_on_freelist;
  107. } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) {
  108. allocator->search_policy = &good_fit_on_freelist;
  109. } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) {
  110. allocator->search_policy = &exact_fit_on_freelist;
  111. } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) {
  112. allocator->search_policy = &first_fit_on_freelist;
  113. }
  114. #endif /* ALLOC_VAR_FIT */
  115. #ifdef WITH_FIXED_LISTS
  116. /* Custom number of fixed lists and their initialization
  117. * Fixed lists of 32, 64, 128 and 256 bytes (4 fixed lists per heap)
  118. */
  119. current_heap = &allocator->heaps[0];
  120. maptablenode = (maptable_node_t *) sys_alloc(allocator, &allocator->heaps[0], 4*(sizeof(maptable_node_t)));
  121. maptablenode->size = 32;
  122. maptablenode->fixed_list_head = NULL;
  123. maptablenode->next = maptablenode+1;
  124. current_heap->maptable_head = maptablenode;
  125. (maptablenode+1)->size = 64;
  126. (maptablenode+1)->fixed_list_head = NULL;
  127. (maptablenode+1)->next = maptablenode+2;
  128. (maptablenode+2)->size = 128;
  129. (maptablenode+2)->fixed_list_head = NULL;
  130. (maptablenode+2)->next = maptablenode+3;
  131. (maptablenode+3)->size = 256;
  132. (maptablenode+3)->fixed_list_head = NULL;
  133. (maptablenode+3)->next = NULL;
  134. #endif /* WITH_FIXED_LISTS */
  135. #ifdef HAVE_LOCKS
  136. sbrk_unlock();
  137. #endif /* HAVE_LOCKS */
  138. }