realloc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2012 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 freelist/realloc.c
  19. * @author Ioannis Koutras
  20. * @date September, 2012
  21. * @brief realloc() implementation for freelist-organized raw blocks
  22. */
  23. #include "dmmlib/freelist/freelist.h"
  24. #include "dmmlib/freelist/block_header.h"
  25. #include "freelist/block_header_funcs.h"
  26. /* for freelist_free() functionality */
  27. #include "freelist/ordering_policy.h"
  28. #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
  29. #include "freelist/coalesce.h"
  30. #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
  31. #include "dmmlib/dmmlib.h"
  32. #include "memcpy.h"
  33. #ifdef HAVE_LOCKS
  34. #include "dmmlib/raw_block.h"
  35. #include <inttypes.h>
  36. #endif /* HAVE_LOCKS */
  37. #include "locks.h"
  38. #include "other.h"
  39. #include "padding.h"
  40. #include "statistics.h"
  41. /**
  42. * Re-allocates a memory block from a freelist-organized raw block
  43. *
  44. * @param raw_block The pointer of the freelist raw block.
  45. * @param ptr The pointer of the memory block to be re-allocated.
  46. * @param req_size The requested memory size.
  47. * @retval The address to serve the request.
  48. * @retval NULL No available memory space.
  49. */
  50. void * freelist_realloc(freelist_rb_t *raw_block, void *ptr,
  51. size_t req_size) {
  52. block_header_t *block;
  53. void *ret;
  54. #if defined (WITH_ALLOCATOR_STATS) && defined (REQUEST_SIZE_INFO)
  55. size_t unmodified_req_size = req_size;
  56. #endif /* WITH_ALLOCATOR_STATS && REQUEST_SIZE_INFO */
  57. req_size = req_padding(req_size);
  58. block = get_header(ptr);
  59. #if defined (WITH_ALLOCATOR_STATS) && defined (REQUEST_SIZE_INFO)
  60. size_t original_req_size = get_requested_size(block);
  61. #endif /* WITH_ALLOCATOR_STATS && REQUEST_SIZE_INFO */
  62. if(get_size(block) > req_size) {
  63. /* TODO Maybe create a memory block in the unneeded space */
  64. return ptr;
  65. }
  66. #ifdef HAVE_LOCKS
  67. raw_block_header_t *rb;
  68. rb = (raw_block_header_t *)
  69. ((uintptr_t) raw_block - sizeof(raw_block_header_t));
  70. #endif /* HAVE_LOCKS */
  71. /* First try to allocate space from the same raw block.
  72. * If it fails, try a more generic malloc. */
  73. /* TODO check if the next memory block is free and try to coalesce if
  74. * possible */
  75. LOCK_RAW_BLOCK(rb);
  76. ret = freelist_malloc(raw_block, req_size);
  77. UNLOCK_RAW_BLOCK(rb);
  78. if(ret != NULL) {
  79. #ifdef REQUEST_SIZE_INFO
  80. UPDATE_GLOBAL_STATS(REALLOC_GT,
  81. unmodified_req_size - original_req_size);
  82. #else /* REQUEST_SIZE_INFO */
  83. UPDATE_GLOBAL_STATS(REALLOC_GT);
  84. #endif /* REQUEST_SIZE_INFO */
  85. } else {
  86. /* FIXME stats leak from this malloc() call */
  87. ret = malloc(req_size);
  88. }
  89. if(ret == NULL) {
  90. return NULL;
  91. }
  92. memcpy(ret, ptr, get_size(block));
  93. LOCK_RAW_BLOCK(rb);
  94. /* start of copy from freelist_free() */
  95. #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
  96. coalesce(raw_block, block);
  97. #else
  98. mark_free(raw_block, block);
  99. ADD_BLOCK(block);
  100. #endif /* COALESCING_FIXED || COALESCING_VARIABLE */
  101. /* end of copy from freelist_free() */
  102. UNLOCK_RAW_BLOCK(rb);
  103. return ret;
  104. }