cache_bucket.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "cache.h"
  2. /*--------------------------------------------------------- */
  3. size_t
  4. lub_heap_cache_bucket__get_block_overhead(lub_heap_cache_bucket_t *this,
  5. const char *ptr)
  6. {
  7. return sizeof(lub_heap_cache_bucket_t*);
  8. }
  9. /*--------------------------------------------------------- */
  10. size_t
  11. lub_heap_cache_bucket__get_block_size(lub_heap_cache_bucket_t *this,
  12. const char *ptr)
  13. {
  14. size_t size = 0;
  15. /* get the size from the cache */
  16. lub_blockpool_stats_t stats;
  17. lub_blockpool__get_stats(&this->m_blockpool,&stats);
  18. size = stats.block_size - sizeof(lub_heap_cache_bucket_t*);
  19. return size;
  20. }
  21. /*--------------------------------------------------------- */
  22. void
  23. lub_heap_cache_bucket_init(lub_heap_cache_bucket_t *this,
  24. lub_heap_cache_t *cache,
  25. size_t block_size,
  26. size_t bucket_size)
  27. {
  28. /* initialise the blockpool */
  29. size_t num_blocks = bucket_size/block_size;
  30. lub_blockpool_init(&this->m_blockpool,
  31. this->m_memory_start,
  32. block_size,
  33. num_blocks);
  34. this->m_memory_end = this->m_memory_start + bucket_size;
  35. this->m_cache = cache;
  36. }
  37. /*--------------------------------------------------------- */
  38. void *
  39. lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t *this)
  40. {
  41. void *ptr = 0;
  42. lub_heap_cache_bucket_t **bucket_ptr = lub_blockpool_alloc(&this->m_blockpool);
  43. if(bucket_ptr)
  44. {
  45. *bucket_ptr = this;
  46. ptr = ++bucket_ptr;
  47. /* make sure that released memory is tainted */
  48. lub_heap_taint_memory(ptr,
  49. LUB_HEAP_TAINT_ALLOC,
  50. this->m_blockpool.m_block_size-sizeof(lub_heap_cache_bucket_t*));
  51. }
  52. return ptr;
  53. }
  54. /*--------------------------------------------------------- */
  55. lub_heap_status_t
  56. lub_heap_cache_bucket_free(lub_heap_cache_bucket_t *this,
  57. void *ptr)
  58. {
  59. lub_heap_status_t status = LUB_HEAP_CORRUPTED;
  60. lub_heap_cache_bucket_t **bucket_ptr = ptr;
  61. --bucket_ptr;
  62. if(*bucket_ptr == this)
  63. {
  64. lub_blockpool_free(&this->m_blockpool,bucket_ptr);
  65. /* make sure that released memory is tainted */
  66. lub_heap_taint_memory((char*)bucket_ptr,
  67. LUB_HEAP_TAINT_FREE,
  68. this->m_blockpool.m_block_size);
  69. status = LUB_HEAP_OK;
  70. }
  71. return status;
  72. }
  73. /*--------------------------------------------------------- */