allocator.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "types.h"
  7. namespace crown
  8. {
  9. /// Base class for memory allocators.
  10. ///
  11. /// @ingroup Memory
  12. class Allocator
  13. {
  14. public:
  15. Allocator() {}
  16. virtual ~Allocator() {}
  17. /// Allocates @a size bytes of memory aligned to the specified
  18. /// @a align byte and returns a pointer to the first allocated byte.
  19. virtual void* allocate(size_t size, size_t align = DEFAULT_ALIGN) = 0;
  20. /// Deallocates a previously allocated block of memory pointed by @a data.
  21. virtual void deallocate(void* data) = 0;
  22. /// Returns the size of the memory block pointed by @a ptr or SIZE_NOT_TRACKED
  23. /// if the allocator does not support memory tracking.
  24. /// @a ptr must be a pointer returned by Allocator::allocate().
  25. virtual uint32_t allocated_size(const void* ptr) = 0;
  26. /// Returns the total number of bytes allocated.
  27. virtual uint32_t total_allocated() = 0;
  28. /// Default memory alignment in bytes.
  29. static const size_t DEFAULT_ALIGN = 4;
  30. static const uint32_t SIZE_NOT_TRACKED = 0xffffffffu;
  31. private:
  32. // Disable copying
  33. Allocator(const Allocator&);
  34. Allocator& operator=(const Allocator&);
  35. };
  36. } // namespace crown