freelist_malloc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "freelist/freelist.h"
  18. #ifdef HAVE_LOCKS
  19. #include <pthread.h>
  20. #endif /* HAVE_LOCKS */
  21. #include "freelist/block_header_funcs.h"
  22. #include "freelist/linked_lists/linked_lists.h"
  23. #include "freelist/linked_lists/search_algorithms.h"
  24. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  25. #include "freelist/split.h"
  26. #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  27. #if defined (BEST_FIT)
  28. #define search_on_free(size) best_fit_on_freelist(rb_header, size)
  29. #elif defined (GOOD_FIT)
  30. #define search_on_free(size) good_fit_on_freelist(rb_header, size)
  31. #elif defined (EXACT_FIT)
  32. #define search_on_free(size) exact_fit_on_freelist(rb_header, size)
  33. #elif defined (FIRST_FIT)
  34. #define search_on_free(size) first_fit_on_freelist(rb_header, size)
  35. #endif /* BEST_FIT */
  36. size_t req_padding(size_t size);
  37. size_t req_padding(size_t size) {
  38. size_t align;
  39. if(size <= 32)
  40. return 32;
  41. if(size <= 64)
  42. return 64;
  43. if(size <= 128)
  44. return 128;
  45. if(size <= 256)
  46. return 256;
  47. align = size % 4;
  48. if(align != 0) {
  49. size += 4 - align;
  50. }
  51. return size;
  52. }
  53. /** Tries to allocate memory from a specific free-list organized raw block.
  54. * @param raw_block The pointer of the raw block.
  55. * @param size The requested size.
  56. * @retval The address of the returned memory space.
  57. */
  58. void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
  59. freelist_rb_t *rb_header;
  60. block_header_t *ptr;
  61. size_t allocation_size, previous_size, previous_size_availability;
  62. rb_header = (freelist_rb_t *)((char *)raw_block +
  63. sizeof(raw_block_header_t));
  64. ptr = NULL;
  65. #ifdef HAVE_LOCKS
  66. pthread_mutex_lock(&raw_block->mutex);
  67. #endif /* HAVE_LOCKS */
  68. ptr = search_on_free(size);
  69. if(ptr != NULL) {
  70. #ifdef REQUEST_SIZE_INFO
  71. set_requested_size(ptr, size);
  72. #endif /* REQUEST_SIZE_INFO */
  73. /* Try to split */
  74. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  75. split(rb_header, ptr, size);
  76. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  77. mark_used(rb_header, ptr);
  78. } else {
  79. allocation_size = req_padding(size) + HEADER_SIZE;
  80. if(allocation_size < (char *) raw_block + raw_block->size -
  81. (char *) rb_header->border_ptr -
  82. get_size(rb_header->border_ptr)) {
  83. if(rb_header->border_ptr == (block_header_t *)((char *)rb_header +
  84. sizeof(freelist_rb_t))) {
  85. previous_size_availability = 1; // Occupied and of 0 size
  86. ptr = (block_header_t *)((char *)rb_header +
  87. sizeof(freelist_rb_t));
  88. } else {
  89. previous_size = get_size(rb_header->border_ptr);
  90. previous_size_availability =
  91. get_size_availability(rb_header->border_ptr);
  92. ptr = (block_header_t *)((char *)rb_header->border_ptr +
  93. previous_size);
  94. }
  95. rb_header->border_ptr = ptr;
  96. // Update block metadata
  97. set_size_and_used(rb_header, ptr, req_padding(size));
  98. set_previous_size_availability(ptr, previous_size_availability);
  99. #ifdef REQUEST_SIZE_INFO
  100. set_requested_size(ptr, req_padding(size));
  101. #endif /* REQUEST_SIZE_INFO */
  102. }
  103. }
  104. #ifdef WITH_RAWBLOCK_STATS
  105. if(ptr != NULL) {
  106. #if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
  107. raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
  108. #endif /* !((SPLITTING_FIXED) || (SPLITTING_VARIABLE)) */
  109. raw_block->dmm_stats.live_objects++;
  110. raw_block->dmm_stats.num_malloc++;
  111. #ifdef REQUEST_SIZE_INFO
  112. raw_block->dmm_stats.total_mem_requested += size;
  113. #endif /* REQUEST_SIZE_INFO */
  114. }
  115. #endif /* WITH_RAWBLOCK_STATS */
  116. #ifdef HAVE_LOCKS
  117. pthread_mutex_unlock(&raw_block->mutex);
  118. #endif /* HAVE_LOCKS */
  119. if(ptr != NULL) {
  120. return (void *)((char *)ptr + HEADER_SIZE);
  121. } else {
  122. return NULL;
  123. }
  124. }