1
0

heap_realloc.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <string.h>
  2. #include <assert.h>
  3. #include "cache.h"
  4. /*--------------------------------------------------------- */
  5. lub_heap_status_t
  6. lub_heap_realloc(lub_heap_t * this,
  7. char **ptr, size_t requested_size, lub_heap_align_t alignment)
  8. {
  9. lub_heap_status_t status = LUB_HEAP_FAILED;
  10. size_t size = requested_size;
  11. /* opportunity for leak detection to do it's stuff */
  12. lub_heap_pre_realloc(this, ptr, &size);
  13. if (this->cache && (LUB_HEAP_ALIGN_NATIVE == alignment)) {
  14. /* try and get the memory from the cache */
  15. status = lub_heap_cache_realloc(this, ptr, size);
  16. } else {
  17. /* get the memory directly from the dynamic heap */
  18. status = lub_heap_raw_realloc(this, ptr, size, alignment);
  19. }
  20. /* opportunity for leak detection to do it's stuff */
  21. lub_heap_post_realloc(this, ptr);
  22. if (LUB_HEAP_OK == status) {
  23. /* update the high tide markers */
  24. if ((this->stats.alloc_bytes + this->stats.alloc_overhead)
  25. > (this->stats.alloc_hightide_bytes +
  26. this->stats.alloc_hightide_overhead)) {
  27. this->stats.alloc_hightide_blocks =
  28. this->stats.alloc_blocks;
  29. this->stats.alloc_hightide_bytes =
  30. this->stats.alloc_bytes;
  31. this->stats.alloc_hightide_overhead =
  32. this->stats.alloc_overhead;
  33. this->stats.free_hightide_blocks =
  34. this->stats.free_blocks;
  35. this->stats.free_hightide_bytes =
  36. this->stats.free_bytes;
  37. this->stats.free_hightide_overhead =
  38. this->stats.free_overhead;
  39. }
  40. } else {
  41. /* this call enables a debugger to catch failures */
  42. lub_heap_stop_here(status, *ptr, requested_size);
  43. }
  44. if ((0 == requested_size) && (NULL == *ptr) && (LUB_HEAP_OK == status)) {
  45. /* make sure that client doesn't use this (non-existant memory) */
  46. *ptr = LUB_HEAP_ZERO_ALLOC;
  47. }
  48. return status;
  49. }
  50. /*--------------------------------------------------------- */