cache_bucket.c 2.2 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, size_t bucket_size)
  26. {
  27. /* initialise the blockpool */
  28. size_t num_blocks = bucket_size / block_size;
  29. lub_blockpool_init(&this->m_blockpool,
  30. this->m_memory_start, block_size, num_blocks);
  31. this->m_memory_end = this->m_memory_start + bucket_size;
  32. this->m_cache = cache;
  33. }
  34. /*--------------------------------------------------------- */
  35. void *lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t * this)
  36. {
  37. void *ptr = 0;
  38. lub_heap_cache_bucket_t **bucket_ptr =
  39. lub_blockpool_alloc(&this->m_blockpool);
  40. if (bucket_ptr) {
  41. *bucket_ptr = this;
  42. ptr = ++bucket_ptr;
  43. /* make sure that released memory is tainted */
  44. lub_heap_taint_memory(ptr,
  45. LUB_HEAP_TAINT_ALLOC,
  46. this->m_blockpool.m_block_size -
  47. sizeof(lub_heap_cache_bucket_t *));
  48. }
  49. return ptr;
  50. }
  51. /*--------------------------------------------------------- */
  52. lub_heap_status_t
  53. lub_heap_cache_bucket_free(lub_heap_cache_bucket_t * this, void *ptr)
  54. {
  55. lub_heap_status_t status = LUB_HEAP_CORRUPTED;
  56. lub_heap_cache_bucket_t **bucket_ptr = ptr;
  57. --bucket_ptr;
  58. if (*bucket_ptr == this) {
  59. lub_blockpool_free(&this->m_blockpool, bucket_ptr);
  60. /* make sure that released memory is tainted */
  61. lub_heap_taint_memory((char *)bucket_ptr,
  62. LUB_HEAP_TAINT_FREE,
  63. this->m_blockpool.m_block_size);
  64. status = LUB_HEAP_OK;
  65. }
  66. return status;
  67. }
  68. /*--------------------------------------------------------- */