freelist_malloc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 freelist_malloc.c
  19. * @author Ioannis Koutras
  20. * @date September 2012
  21. * @brief malloc() implementation for freelist-organised raw blocks
  22. */
  23. #include "freelist/freelist.h"
  24. #include "freelist/block_header_funcs.h"
  25. #include "freelist/linked_lists/linked_lists.h"
  26. #include "freelist/linked_lists/search_algorithms.h"
  27. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  28. #include "freelist/split.h"
  29. #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  30. #include "trace.h"
  31. #ifdef WITH_ALLOCATOR_STATS
  32. #include "dmmlib/dmmlib.h"
  33. #endif /* WITH_ALLOCATOR_STATS */
  34. /**
  35. * Tries to find the best available free memory block
  36. *
  37. * @param size The requested size
  38. */
  39. #if defined (BEST_FIT)
  40. #define search_on_free(size) best_fit_on_freelist(raw_block, size)
  41. #elif defined (GOOD_FIT)
  42. #define search_on_free(size) good_fit_on_freelist(raw_block, size)
  43. #elif defined (EXACT_FIT)
  44. #define search_on_free(size) exact_fit_on_freelist(raw_block, size)
  45. #elif defined (FIRST_FIT)
  46. #define search_on_free(size) first_fit_on_freelist(raw_block, size)
  47. #endif /* BEST_FIT */
  48. size_t req_padding(size_t size);
  49. /** Tries to align the input to 32, 64, 128, 256 or to a multiple of 4 if it is
  50. * larger.
  51. *
  52. * @param size The input number.
  53. */
  54. size_t req_padding(size_t size) {
  55. size_t align;
  56. if(size <= 32)
  57. return 32;
  58. if(size <= 64)
  59. return 64;
  60. if(size <= 128)
  61. return 128;
  62. if(size <= 256)
  63. return 256;
  64. align = size % 4;
  65. if(align != 0) {
  66. size += 4 - align;
  67. }
  68. return size;
  69. }
  70. /** Tries to allocate memory from a specific free-list organized raw block.
  71. * @param raw_block The pointer of the freelist-organised raw block.
  72. * @param size The requested size.
  73. * @retval The address of the returned memory space.
  74. */
  75. void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
  76. block_header_t *ptr;
  77. size_t allocation_size, previous_size, previous_size_availability;
  78. ptr = NULL;
  79. ptr = search_on_free(size);
  80. if(ptr != NULL) {
  81. #ifdef REQUEST_SIZE_INFO
  82. set_requested_size(ptr, size);
  83. #endif /* REQUEST_SIZE_INFO */
  84. /* Try to split */
  85. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  86. split(raw_block, ptr, size);
  87. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  88. mark_used(raw_block, ptr);
  89. } else {
  90. allocation_size = req_padding(size) + HEADER_SIZE;
  91. if(allocation_size <= raw_block->remaining_size) {
  92. if(raw_block->border_ptr == NULL) {
  93. previous_size_availability = 1; // Occupied and of 0 size
  94. ptr = (block_header_t *)((char *)raw_block +
  95. sizeof(freelist_rb_t));
  96. } else {
  97. previous_size = get_size(raw_block->border_ptr);
  98. previous_size_availability =
  99. get_size_availability(raw_block->border_ptr);
  100. ptr = (block_header_t *)((char *)raw_block->border_ptr +
  101. HEADER_SIZE + previous_size);
  102. }
  103. // Update raw block metadata
  104. raw_block->remaining_size -= allocation_size;
  105. raw_block->border_ptr = ptr;
  106. // Update block metadata
  107. set_size_and_used(raw_block, ptr, req_padding(size));
  108. set_previous_size_availability(ptr, previous_size_availability);
  109. #ifdef REQUEST_SIZE_INFO
  110. set_requested_size(ptr, size);
  111. #endif /* REQUEST_SIZE_INFO */
  112. }
  113. }
  114. if(ptr != NULL) {
  115. #ifdef WITH_ALLOCATOR_STATS
  116. systemallocator.dmm_stats.total_mem_allocated +=
  117. get_size(ptr) + HEADER_SIZE;
  118. systemallocator.dmm_stats.live_objects++;
  119. systemallocator.dmm_stats.num_malloc++;
  120. TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
  121. systemallocator.dmm_stats.total_mem_allocated);
  122. #ifdef REQUEST_SIZE_INFO
  123. systemallocator.dmm_stats.total_mem_requested += size;
  124. TRACE_1("dmmlib - global requested memory: %zu bytes\n",
  125. systemallocator.dmm_stats.total_mem_requested);
  126. #endif /* REQUEST_SIZE_INFO */
  127. #endif /* WITH_ALLOCATOR_STATS */
  128. return (void *)((char *)ptr + HEADER_SIZE);
  129. } else {
  130. return NULL;
  131. }
  132. }