linear_allocator.h 1.2 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 memory linearly from a predefined chunk
  10. /// and frees all the allocations with a single call to clear()
  11. ///
  12. /// @ingroup Memory
  13. class LinearAllocator : public Allocator
  14. {
  15. public:
  16. LinearAllocator(Allocator& backing, size_t size);
  17. LinearAllocator(void* start, size_t size);
  18. ~LinearAllocator();
  19. /// @copydoc Allocator::allocate()
  20. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  21. /// @copydoc Allocator::deallocate()
  22. /// @note
  23. /// The linear allocator does not support deallocating
  24. /// individual allocations, rather you have to call
  25. /// clear() to free all allocated memory at once.
  26. void deallocate(void* data);
  27. /// Frees all the allocations made by allocate()
  28. void clear();
  29. /// @copydoc Allocator::allocated_size()
  30. uint32_t allocated_size(const void* /*ptr*/) { return SIZE_NOT_TRACKED; }
  31. /// @copydoc Allocator::total_allocated()
  32. uint32_t total_allocated() { return _offset; }
  33. private:
  34. Allocator* _backing;
  35. void* _physical_start;
  36. size_t _total_size;
  37. size_t _offset;
  38. };
  39. } // namespace crown