stack_allocator.h 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 in a stack-like fashion from a
  10. /// predefined chunk. All deallocations must occur in LIFO
  11. /// order.
  12. ///
  13. /// @ingroup Memory
  14. class StackAllocator : public Allocator
  15. {
  16. public:
  17. StackAllocator(void* start, size_t size);
  18. ~StackAllocator();
  19. /// @copydoc Allocator::allocate()
  20. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  21. /// @copydoc Allocator::deallocate()
  22. /// @note
  23. /// Deallocations must occur in LIFO order i.e. the
  24. /// last allocation must be freed for first.
  25. void deallocate(void* data);
  26. /// @copydoc Allocator::allocated_size()
  27. size_t allocated_size();
  28. private:
  29. struct Header
  30. {
  31. uint32_t offset;
  32. uint32_t alloc_id;
  33. };
  34. void* _physical_start;
  35. size_t _total_size;
  36. void* _top;
  37. uint32_t _allocation_count;
  38. };
  39. } // namespace crown