realloc.c 4.9 KB

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