pool_allocator.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "allocator.h"
  7. namespace crown
  8. {
  9. /// Allocates fixed-size memory blocks from a fixed memory pool.
  10. /// The backing allocator is used to allocate the memory pool.
  11. ///
  12. /// @ingroup Memory
  13. class PoolAllocator : public Allocator
  14. {
  15. public:
  16. /// Uses @a backing to allocate the memory pool for containing exactly
  17. /// @a num_blocks blocks of @a block_size size each aligned to @a block_align.
  18. PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align = Allocator::DEFAULT_ALIGN);
  19. ~PoolAllocator();
  20. /// Allocates a block of memory from the memory pool.
  21. /// @note
  22. /// The @a size and @a align must match those passed to PoolAllocator::PoolAllocator()
  23. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  24. /// @copydoc Allocator::deallocate()
  25. void deallocate(void* data);
  26. /// @copydoc Allocator::allocated_size()
  27. uint32_t allocated_size(const void* /*ptr*/) { return SIZE_NOT_TRACKED; }
  28. /// @copydoc Allocator::total_allocated()
  29. uint32_t total_allocated();
  30. private:
  31. Allocator& _backing;
  32. void* _start;
  33. void* _freelist;
  34. size_t _block_size;
  35. size_t _block_align;
  36. uint32_t _num_allocations;
  37. size_t _allocated_size;
  38. };
  39. } // namespace crown