realloc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 src/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 "dmmlib/debug.h"
  26. #include "locks.h"
  27. #include "default_rb.h"
  28. #include "dmmlib_trace.h"
  29. #include "other.h"
  30. #include "padding.h"
  31. #include "statistics.h"
  32. #include "release_memory.h"
  33. #include <sys/mman.h>
  34. #include <inttypes.h>
  35. #ifdef PAGESIZE_ALIGN
  36. #include <unistd.h> /* for pagesize */
  37. #endif /* PAGESIZE_ALIGN */
  38. #include "memcpy.h"
  39. void * realloc(void *ptr, size_t size) {
  40. raw_block_header_t *owner_raw_block;
  41. void *return_ptr = NULL;
  42. if(ptr == NULL) {
  43. return malloc(size);
  44. }
  45. if(size == 0) {
  46. free(ptr);
  47. return NULL;
  48. }
  49. owner_raw_block = find_raw_block_owner(systemallocator.rb_head, ptr);
  50. if(owner_raw_block != NULL) {
  51. DEFAULT_RB_T *encapsulated_rb = (DEFAULT_RB_T *)
  52. ((uintptr_t) owner_raw_block + sizeof(raw_block_header_t));
  53. return_ptr = dmmlib_realloc(encapsulated_rb, ptr, size);
  54. } else { /* This has to be a big block */
  55. // The new size is checked and if it is smaller than the one from the
  56. // original request, the remaining memory is simply released. If it is
  57. // greater, a new big block is initialized, data is copied there and the
  58. // old big block gets de-allocated.
  59. owner_raw_block = (raw_block_header_t *)
  60. ((uintptr_t) ptr - sizeof(raw_block_header_t));
  61. size_t full_size = sizeof(raw_block_header_t) + req_padding(size);
  62. #ifdef PAGESIZE_ALIGN
  63. size_t pagesize = (size_t) sysconf(_SC_PAGESIZE);
  64. full_size = pagesize * ((full_size + pagesize - 1) / pagesize);
  65. #endif /* PAGESIZE_ALIGN */
  66. if(full_size < owner_raw_block->size) {
  67. /* The re-allocation can take place inside the same big block */
  68. size_t remaining_size = owner_raw_block->size - full_size;
  69. #ifdef REQUEST_SIZE_INFO
  70. UPDATE_GLOBAL_STATS(REALLOC_LT, owner_raw_block->requested_size -
  71. size);
  72. #else /* REQUEST_SIZE_INFO */
  73. UPDATE_GLOBAL_STATS(REALLOC_LT);
  74. #endif /* REQUEST_SIZE_INFO */
  75. LOCK_RAW_BLOCK(owner_raw_block);
  76. #ifdef REQUEST_SIZE_INFO
  77. owner_raw_block->requested_size = size;
  78. #endif /* REQUEST_SIZE_INFO */
  79. UNLOCK_RAW_BLOCK(owner_raw_block);
  80. // FIXME This is pagesize and mmap-specific
  81. #ifdef PAGESIZE_ALIGN
  82. if(remaining_size > pagesize) {
  83. remaining_size = pagesize * ((remaining_size + pagesize - 1) /
  84. pagesize);
  85. LOCK_RAW_BLOCK(owner_raw_block);
  86. owner_raw_block->size = full_size;
  87. UNLOCK_RAW_BLOCK(owner_raw_block);
  88. #ifdef WITH_ALLOCATOR_STATS
  89. LOCK_GLOBAL();
  90. systemallocator.dmm_stats.total_mem_allocated -= remaining_size;
  91. UNLOCK_GLOBAL();
  92. #endif /* WITH_ALLOCATOR_STATS */
  93. munmap((void *)((uintptr_t) owner_raw_block +
  94. owner_raw_block->size), remaining_size);
  95. }
  96. #endif /* PAGESIZE_ALIGN */
  97. return_ptr = ptr;
  98. } else if(full_size == owner_raw_block->size) {
  99. /* no need to make modifications */
  100. return_ptr = ptr;
  101. } else { /* We have to create a new big block */
  102. raw_block_header_t *new_block = create_raw_block(full_size,
  103. BIGBLOCK);
  104. if(new_block == NULL) {
  105. return_ptr = NULL;
  106. } else {
  107. return_ptr = (void *)
  108. ((uintptr_t) new_block + sizeof(raw_block_header_t));
  109. memcpy(return_ptr, ptr,
  110. owner_raw_block->size - sizeof(raw_block_header_t));
  111. #ifdef WITH_DEBUG
  112. LOCK_GLOBAL();
  113. LOCK_RAW_BLOCK(owner_raw_block);
  114. SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
  115. raw_block_header_s, pointers);
  116. UNLOCK_RAW_BLOCK(owner_raw_block);
  117. LOCK_RAW_BLOCK(new_block);
  118. SLIST_INSERT_HEAD(&systemallocator.bb_head,
  119. new_block, pointers);
  120. UNLOCK_RAW_BLOCK(new_block);
  121. UNLOCK_GLOBAL();
  122. #endif /* WITH_DEBUG */
  123. #ifdef REQUEST_SIZE_INFO
  124. UPDATE_GLOBAL_STATS(REALLOC_GT, size -
  125. owner_raw_block->requested_size);
  126. #else /* REQUEST_SIZE_INFO */
  127. UPDATE_GLOBAL_STATS(REALLOC_GT);
  128. #endif /* REQUEST_SIZE_INFO */
  129. #ifdef WITH_ALLOCATOR_STATS
  130. LOCK_GLOBAL();
  131. systemallocator.dmm_stats.total_mem_allocated += new_block->size
  132. - owner_raw_block->size;
  133. UNLOCK_GLOBAL();
  134. #endif /* WITH_ALLOCATOR_STATS */
  135. release_memory(owner_raw_block);
  136. }
  137. }
  138. }
  139. MEM_TRACE("dmmlib - r %p %p %zu\n", ptr, return_ptr, size);
  140. return return_ptr;
  141. }