partition_realloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * partition_realloc.c
  3. */
  4. #include <string.h>
  5. #include "private.h"
  6. /*--------------------------------------------------------- */
  7. void
  8. lub_partition_stop_here(lub_heap_status_t status,
  9. char *old_ptr, size_t new_size)
  10. {
  11. /* This is provided as a debug aid */
  12. status = status;
  13. old_ptr = old_ptr;
  14. new_size = new_size;
  15. }
  16. /*--------------------------------------------------------- */
  17. void lub_partition_time_to_die(lub_partition_t * this)
  18. {
  19. bool_t time_to_die = BOOL_TRUE;
  20. lub_heap_stats_t stats;
  21. lub_heap_t *local_heap = lub_partition__get_local_heap(this);
  22. if (local_heap) {
  23. lub_heap__get_stats(local_heap, &stats);
  24. if (stats.alloc_blocks) {
  25. time_to_die = BOOL_FALSE;
  26. }
  27. }
  28. if (BOOL_FALSE == time_to_die) {
  29. lub_partition_lock(this);
  30. lub_heap__get_stats(this->m_global_heap, &stats);
  31. lub_partition_unlock(this);
  32. if (stats.alloc_blocks) {
  33. time_to_die = BOOL_FALSE;
  34. }
  35. }
  36. if (BOOL_TRUE == time_to_die) {
  37. /* and so the time has come... */
  38. lub_partition_destroy(this);
  39. }
  40. }
  41. /*--------------------------------------------------------- */
  42. lub_heap_status_t
  43. lub_partition_global_realloc(lub_partition_t * this,
  44. char **ptr,
  45. size_t size, lub_heap_align_t alignment)
  46. {
  47. lub_heap_status_t status = LUB_HEAP_FAILED;
  48. lub_partition_lock(this);
  49. if (!this->m_global_heap) {
  50. /*
  51. * Create the global heap
  52. */
  53. size_t required = size;
  54. void *segment = lub_partition_segment_alloc(this, &required);
  55. this->m_global_heap = lub_heap_create(segment, required);
  56. }
  57. if (this->m_global_heap) {
  58. status =
  59. lub_heap_realloc(this->m_global_heap, ptr, size, alignment);
  60. if (LUB_HEAP_FAILED == status) {
  61. /* expand memory and try again */
  62. if (lub_partition_extend_memory(this, size)) {
  63. status = lub_heap_realloc(this->m_global_heap,
  64. ptr, size, alignment);
  65. }
  66. }
  67. }
  68. lub_partition_unlock(this);
  69. return status;
  70. }
  71. /*--------------------------------------------------------- */
  72. lub_heap_status_t
  73. lub_partition_realloc(lub_partition_t * this,
  74. char **ptr, size_t size, lub_heap_align_t alignment)
  75. {
  76. lub_heap_status_t status = LUB_HEAP_FAILED;
  77. size_t requested_size = size;
  78. lub_heap_t *local_heap;
  79. if ((0 == size) && (0 == *ptr)) {
  80. /* simple optimisation */
  81. return LUB_HEAP_OK;
  82. }
  83. if (this->m_dying) {
  84. local_heap = lub_partition__get_local_heap(this);
  85. if (size) {
  86. /* don't allocate any new memory when dying */
  87. size = 0;
  88. }
  89. } else {
  90. local_heap = lub_partition_findcreate_local_heap(this);
  91. }
  92. if (local_heap) {
  93. /* try the fast local heap first */
  94. status = lub_heap_realloc(local_heap, ptr, size, alignment);
  95. }
  96. if ((LUB_HEAP_FAILED == status) || (LUB_HEAP_INVALID_POINTER == status)) {
  97. char *old_ptr = 0;
  98. if (local_heap && (LUB_HEAP_FAILED == status) && size) {
  99. /* we need to reallocate from the global and copy from the local */
  100. old_ptr = *ptr;
  101. *ptr = 0;
  102. }
  103. /* time to use the slower global heap */
  104. status =
  105. lub_partition_global_realloc(this, ptr, size, alignment);
  106. if (old_ptr && (LUB_HEAP_OK == status)) {
  107. /* copy from the local to the global */
  108. memcpy(*ptr, old_ptr, size);
  109. /* and release the local block */
  110. status =
  111. lub_heap_realloc(local_heap, &old_ptr, 0,
  112. alignment);
  113. }
  114. }
  115. if (LUB_HEAP_OK != status) {
  116. lub_partition_stop_here(status, *ptr, size);
  117. }
  118. /* allow the partion to destroy itself if necessary */
  119. if (this->m_dying) {
  120. lub_partition_time_to_die(this);
  121. if (requested_size) {
  122. /* we don't allocate memory whilst dying */
  123. status = LUB_HEAP_FAILED;
  124. }
  125. }
  126. return status;
  127. }
  128. /*-------------------------------------------------------- */