proxy_allocator.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "macros.h"
  26. namespace crown
  27. {
  28. /// Offers the facility to tag allocators by a string identifier.
  29. /// Proxy allocator is appended to a global linked list when instantiated
  30. /// so that it is possible to later visit that list for debugging purposes.
  31. ///
  32. /// @ingroup Memory
  33. class ProxyAllocator : public Allocator
  34. {
  35. public:
  36. /// Tag all allocations made with @a allocator by the given @a name
  37. ProxyAllocator(const char* name, Allocator& allocator);
  38. /// @copydoc Allocator::allocate()
  39. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  40. /// @copydoc Allocator::deallocate()
  41. void deallocate(void* data);
  42. /// @copydoc Allocator::allocated_size()
  43. size_t allocated_size();
  44. /// Returns the name of the proxy allocator
  45. const char* name() const;
  46. public:
  47. /// Returns the total number of proxy allocators.
  48. /// in the global list.
  49. static uint32_t count();
  50. /// Returns the proxy allocator @name or NULL if not found.
  51. static ProxyAllocator* find(const char* name);
  52. /// Returns the first proxy allocator in the global list or
  53. /// NULL if the list is empty.
  54. static ProxyAllocator* begin();
  55. /// Returns the next proxy allocator to @a a in the global list
  56. /// or NULL if end-of-list is reached.
  57. static ProxyAllocator* next(ProxyAllocator* a);
  58. private:
  59. Allocator& m_allocator;
  60. const char* m_name;
  61. size_t m_total_allocated;
  62. ProxyAllocator* m_next;
  63. };
  64. } // namespace crown