realloc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 realloc.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. *
  22. * @brief Implementation of realloc() call.
  23. */
  24. #include "dmmlib/dmmlib.h"
  25. #include <stdbool.h>
  26. #ifdef BITMAP_RB_ONLY
  27. #include "bitmap/bitmap.h"
  28. #include "bitmap/bitmap_rb.h"
  29. #endif /* BITMAP_RB_ONLY */
  30. #ifdef FL_RB_ONLY
  31. #include "freelist/freelist.h"
  32. #include "freelist/freelist_rb.h"
  33. #endif /* FL_RB_ONLY */
  34. #include "release_memory.h"
  35. #include <sys/mman.h>
  36. #include <string.h>
  37. #include "trace.h"
  38. void * realloc(void *ptr, size_t size) {
  39. raw_block_header_t *current_raw_block;
  40. bool found;
  41. void *return_ptr;
  42. if(ptr == NULL) {
  43. return malloc(size);
  44. }
  45. if(size == 0) {
  46. free(ptr);
  47. return_ptr = malloc((size_t) 32); // FIXME 32 <- minimum size
  48. goto done;
  49. }
  50. current_raw_block = systemallocator.raw_block_head;
  51. found = false;
  52. while(current_raw_block) {
  53. if(((char *)ptr > (char *)current_raw_block) &&
  54. ((char *)ptr < (char *)(current_raw_block) +
  55. current_raw_block->size)) {
  56. found = true;
  57. break;
  58. }
  59. current_raw_block = current_raw_block->next_raw_block;
  60. }
  61. if(found == true) {
  62. #ifdef BITMAP_RB_ONLY
  63. bitmap_rb_t
  64. #endif /* BITMAP_RB_ONLY */
  65. #ifdef FL_RB_ONLY
  66. freelist_rb_t
  67. #endif /* FL_RB_ONLY */
  68. *encapsulated_rb =
  69. #ifdef BITMAP_RB_ONLY
  70. (bitmap_rb_t *)
  71. #endif /* BITMAP_RB_ONLY */
  72. #ifdef FL_RB_ONLY
  73. (freelist_rb_t *)
  74. #endif /* FL_RB_ONLY */
  75. ((char *)current_raw_block + sizeof(raw_block_header_t));
  76. return_ptr = dmmlib_realloc(encapsulated_rb, ptr, size);
  77. goto done;
  78. } else { /* This has to be a big block */
  79. // The new size is checked and if it is smaller than the one from the
  80. // original request, the remaining memory is simply released. If it is
  81. // greater, a new big block is initialized, data is copied there and the
  82. // old big block gets de-allocated.
  83. current_raw_block = (raw_block_header_t *)((char *)ptr -
  84. sizeof(raw_block_header_t));
  85. size_t full_size = sizeof(raw_block_header_t) + size;
  86. if(full_size <= current_raw_block->size) {
  87. size_t remaining_size = current_raw_block->size - full_size;
  88. current_raw_block->size = full_size;
  89. // FIXME This is mmap-specific
  90. munmap((void *)((char *)current_raw_block +
  91. current_raw_block->size), remaining_size);
  92. #ifdef WITH_ALLOCATOR_STATS
  93. systemallocator.dmm_stats.total_mem_allocated -=
  94. remaining_size;
  95. TRACE_2("dmmlib - ms all %zu\n",
  96. systemallocator.dmm_stats.total_mem_allocated);
  97. #ifdef REQUEST_SIZE_INFO
  98. systemallocator.dmm_stats.total_mem_requested -=
  99. remaining_size;
  100. TRACE_2("dmmlib - ms req %zu\n",
  101. systemallocator.dmm_stats.total_mem_requested);
  102. #endif /* REQUEST_SIZE_INFO */
  103. systemallocator.dmm_stats.num_realloc++;
  104. #endif /* WITH_ALLOCATOR_STATS */
  105. return_ptr = ptr;
  106. goto done;
  107. } else {
  108. size_t size_diff = full_size - current_raw_block->size;
  109. raw_block_header_t *new_block = create_raw_block(full_size,
  110. BIGBLOCK);
  111. if(new_block == NULL) {
  112. return_ptr = NULL;
  113. goto done;
  114. } else {
  115. memcpy((void *)((char *)new_block +
  116. sizeof(raw_block_header_t)), ptr,
  117. current_raw_block->size - sizeof(raw_block_header_t));
  118. release_memory(current_raw_block);
  119. #ifdef WITH_ALLOCATOR_STATS
  120. systemallocator.dmm_stats.total_mem_allocated +=
  121. size_diff;
  122. TRACE_2("dmmlib - ms all %zu\n",
  123. systemallocator.dmm_stats.total_mem_allocated);
  124. #ifdef REQUEST_SIZE_INFO
  125. systemallocator.dmm_stats.total_mem_requested +=
  126. size_diff;
  127. TRACE_2("dmmlib - ms req %zu\n",
  128. systemallocator.dmm_stats.total_mem_requested);
  129. #endif /* REQUEST_SIZE_INFO */
  130. systemallocator.dmm_stats.num_realloc++;
  131. #endif /* WITH_ALLOCATOR_STATS */
  132. return_ptr = (void *)((char *)new_block +
  133. sizeof(raw_block_header_t));
  134. goto done;
  135. }
  136. }
  137. }
  138. done:
  139. TRACE_1("dmmlib - r %p %p %zu\n", ptr, return_ptr, size);
  140. return return_ptr;
  141. }