realloc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "other.h"
  29. #include "release_memory.h"
  30. #include <sys/mman.h>
  31. #include <inttypes.h>
  32. #include "memcpy.h"
  33. #include "trace.h"
  34. void * realloc(void *ptr, size_t size) {
  35. raw_block_header_t *owner_raw_block;
  36. void *return_ptr = NULL;
  37. if(ptr == NULL) {
  38. return malloc(size);
  39. }
  40. if(size == 0) {
  41. free(ptr);
  42. return NULL;
  43. }
  44. owner_raw_block = find_raw_block_owner(systemallocator.rb_head, ptr);
  45. if(owner_raw_block != NULL) {
  46. DEFAULT_RB_T *encapsulated_rb = (DEFAULT_RB_T *)
  47. ((uintptr_t) owner_raw_block + sizeof(raw_block_header_t));
  48. return_ptr = dmmlib_realloc(encapsulated_rb, ptr, size);
  49. goto done;
  50. } else { /* This has to be a big block */
  51. // The new size is checked and if it is smaller than the one from the
  52. // original request, the remaining memory is simply released. If it is
  53. // greater, a new big block is initialized, data is copied there and the
  54. // old big block gets de-allocated.
  55. owner_raw_block = (raw_block_header_t *)
  56. ((uintptr_t) ptr - sizeof(raw_block_header_t));
  57. size_t full_size = sizeof(raw_block_header_t) + size;
  58. if(full_size <= owner_raw_block->size) {
  59. size_t remaining_size = owner_raw_block->size - full_size;
  60. owner_raw_block->size = full_size;
  61. // FIXME This is mmap-specific
  62. munmap((void *)((uintptr_t) owner_raw_block +
  63. owner_raw_block->size), remaining_size);
  64. #ifdef WITH_ALLOCATOR_STATS
  65. LOCK_GLOBAL();
  66. systemallocator.dmm_stats.total_mem_allocated -=
  67. remaining_size;
  68. STATS_TRACE("dmmlib - ms all %zu\n",
  69. systemallocator.dmm_stats.total_mem_allocated);
  70. #ifdef REQUEST_SIZE_INFO
  71. systemallocator.dmm_stats.total_mem_requested -=
  72. remaining_size;
  73. STATS_TRACE("dmmlib - ms req %zu\n",
  74. systemallocator.dmm_stats.total_mem_requested);
  75. #endif /* REQUEST_SIZE_INFO */
  76. systemallocator.dmm_stats.num_realloc++;
  77. UNLOCK_GLOBAL();
  78. #endif /* WITH_ALLOCATOR_STATS */
  79. return_ptr = ptr;
  80. goto done;
  81. } else { /* We have to create a new big block */
  82. #ifdef WITH_ALLOCATOR_STATS
  83. size_t size_diff = full_size - owner_raw_block->size;
  84. #endif /* WITH_ALLOCATOR_STATS */
  85. raw_block_header_t *new_block = create_raw_block(full_size,
  86. BIGBLOCK);
  87. if(new_block == NULL) {
  88. return_ptr = NULL;
  89. goto done;
  90. } else {
  91. return_ptr = (void *)
  92. ((uintptr_t) new_block + sizeof(raw_block_header_t));
  93. memcpy(return_ptr, ptr,
  94. owner_raw_block->size - sizeof(raw_block_header_t));
  95. #ifdef WITH_DEBUG
  96. LOCK_GLOBAL();
  97. LOCK_RAW_BLOCK(owner_raw_block);
  98. SLIST_REMOVE(&systemallocator.bb_head, owner_raw_block,
  99. raw_block_header_s, pointers);
  100. UNLOCK_RAW_BLOCK(owner_raw_block);
  101. LOCK_RAW_BLOCK(new_block);
  102. SLIST_INSERT_HEAD(&systemallocator.bb_head,
  103. new_block, pointers);
  104. UNLOCK_RAW_BLOCK(new_block);
  105. UNLOCK_GLOBAL();
  106. #endif /* WITH_DEBUG */
  107. release_memory(owner_raw_block);
  108. #ifdef WITH_ALLOCATOR_STATS
  109. LOCK_GLOBAL();
  110. systemallocator.dmm_stats.total_mem_allocated +=
  111. size_diff;
  112. STATS_TRACE("dmmlib - ms all %zu\n",
  113. systemallocator.dmm_stats.total_mem_allocated);
  114. #ifdef REQUEST_SIZE_INFO
  115. systemallocator.dmm_stats.total_mem_requested +=
  116. size_diff;
  117. STATS_TRACE("dmmlib - ms req %zu\n",
  118. systemallocator.dmm_stats.total_mem_requested);
  119. #endif /* REQUEST_SIZE_INFO */
  120. systemallocator.dmm_stats.num_realloc++;
  121. UNLOCK_GLOBAL();
  122. #endif /* WITH_ALLOCATOR_STATS */
  123. goto done;
  124. }
  125. }
  126. }
  127. done:
  128. MEM_TRACE("dmmlib - r %p %p %zu\n", ptr, return_ptr, size);
  129. return return_ptr;
  130. }