heap_realloc.c 2.0 KB

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