pool_allocator.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. size_t allocated_size();
  28. private:
  29. Allocator& _backing;
  30. void* _start;
  31. void* _freelist;
  32. size_t _block_size;
  33. size_t _block_align;
  34. uint32_t _num_allocations;
  35. size_t _allocated_size;
  36. };
  37. } // namespace crown