2
0

pool_allocator.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "allocator.h"
  25. namespace crown
  26. {
  27. /// Allocates fixed-size memory blocks from a fixed memory pool.
  28. /// The backing allocator is used to allocate the memory pool.
  29. ///
  30. /// @ingroup Memory
  31. class PoolAllocator : public Allocator
  32. {
  33. public:
  34. /// Uses @a backing to allocate the memory pool for containing exactly
  35. /// @a num_blocks blocks of @a block_size size each aligned to @a block_align.
  36. PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align = Allocator::DEFAULT_ALIGN);
  37. ~PoolAllocator();
  38. /// Allocates a block of memory from the memory pool.
  39. /// @note
  40. /// The @a size and @a align must match those passed to PoolAllocator::PoolAllocator()
  41. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  42. /// @copydoc Allocator::deallocate()
  43. void deallocate(void* data);
  44. /// @copydoc Allocator::allocated_size()
  45. size_t allocated_size();
  46. private:
  47. Allocator& _backing;
  48. void* _start;
  49. void* _freelist;
  50. size_t _block_size;
  51. size_t _block_align;
  52. uint32_t _num_allocations;
  53. size_t _allocated_size;
  54. };
  55. } // namespace crown