proxy_allocator.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include "macros.h"
  8. namespace crown
  9. {
  10. /// Offers the facility to tag allocators by a string identifier.
  11. /// Proxy allocator is appended to a global linked list when instantiated
  12. /// so that it is possible to later visit that list for debugging purposes.
  13. ///
  14. /// @ingroup Memory
  15. class ProxyAllocator : public Allocator
  16. {
  17. public:
  18. /// Tag all allocations made with @a allocator by the given @a name
  19. ProxyAllocator(const char* name, Allocator& allocator);
  20. /// @copydoc Allocator::allocate()
  21. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  22. /// @copydoc Allocator::deallocate()
  23. void deallocate(void* data);
  24. /// @copydoc Allocator::allocated_size()
  25. size_t allocated_size();
  26. /// Returns the name of the proxy allocator
  27. const char* name() const;
  28. public:
  29. /// Returns the total number of proxy allocators.
  30. /// in the global list.
  31. static uint32_t count();
  32. /// Returns the proxy allocator @name or NULL if not found.
  33. static ProxyAllocator* find(const char* name);
  34. /// Returns the first proxy allocator in the global list or
  35. /// NULL if the list is empty.
  36. static ProxyAllocator* begin();
  37. /// Returns the next proxy allocator to @a a in the global list
  38. /// or NULL if end-of-list is reached.
  39. static ProxyAllocator* next(ProxyAllocator* a);
  40. private:
  41. Allocator& _allocator;
  42. const char* _name;
  43. size_t _total_allocated;
  44. ProxyAllocator* _next;
  45. };
  46. } // namespace crown