33#ifndef DART_COMMON_DETAIL_MEMORYALLOCATORDEBUGGER_IMPL_HPP_
34#define DART_COMMON_DETAIL_MEMORYALLOCATORDEBUGGER_IMPL_HPP_
44template <
typename... Args>
46 : mInternalAllocator(std::forward<Args>(args)...)
56 std::lock_guard<std::mutex> lock(mMutex);
58 if (!mMapPointerToSize.empty())
61 for (
auto it : mMapPointerToSize)
63 void* pointer = it.first;
64 size_t size = it.second;
66 dtdbg <<
"Found potential memory leak at " << pointer <<
" (" << size
72 dtdbg <<
"Found potential memory leak of total " << totalSize
73 <<
" bytes. The internal allocator will try to forcefully "
74 <<
"deallocate it but it's is not guaranteed.\n";
84 static const std::string
type
85 =
"MemoryAllocatorDebugger<" + T::getStaticType() +
">";
93 return getStaticType();
100 void* newPtr = mInternalAllocator.allocate(bytes);
104 std::lock_guard<std::mutex> lock(mMutex);
106 mPeak = std::max(mPeak, mSize);
107 mMapPointerToSize[newPtr] = bytes;
117 std::lock_guard<std::mutex> lock(mMutex);
119 auto it = mMapPointerToSize.find(pointer);
120 if (it == mMapPointerToSize.end())
123 "Cannot deallocate memory {} not allocated by this allocator.",
128 auto allocatedSize = it->second;
129 if (bytes != allocatedSize)
132 "Cannot deallocate memory at {} of {} bytes that is different from the "
133 "allocated size {}, which is a critical bug.",
140 mInternalAllocator.deallocate(pointer, bytes);
141 mMapPointerToSize.erase(it);
149 std::lock_guard<std::mutex> lock(mMutex);
150 return mMapPointerToSize.empty();
157 std::lock_guard<std::mutex> lock(mMutex);
159 const auto it = mMapPointerToSize.find(pointer);
160 if (it == mMapPointerToSize.end())
165 const auto& allocatedSize = it->second;
166 if (size != allocatedSize)
178 return mInternalAllocator;
185 return mInternalAllocator;
194 os <<
"[" << getType() <<
"]\n";
196 const std::string spaces(indent,
' ');
199 os << spaces <<
"type: " << getType() <<
"\n";
201 std::lock_guard<std::mutex> lock(mMutex);
202 os << spaces <<
"size_in_bytes: " << mSize <<
"\n";
203 os << spaces <<
"peak: " << mPeak <<
"\n";
204 os << spaces <<
"internal_allocator:\n";
205 mInternalAllocator.print(os, indent + 2);
#define dtdbg
Output a debug message.
Definition Console.hpp:43
#define DART_DEBUG(...)
Definition Logging.hpp:60
std::string type
Definition SdfParser.cpp:82
void * allocate(size_t bytes) noexcept override
Allocates size bytes of uninitialized storage.
Definition MemoryAllocatorDebugger-impl.hpp:98
bool hasAllocated(void *pointer, size_t size) const
Returns true if a pointer is allocated by the internal allocator.
Definition MemoryAllocatorDebugger-impl.hpp:155
void deallocate(void *pointer, size_t bytes) override
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition MemoryAllocatorDebugger-impl.hpp:115
const std::string & getType() const override
Returns type string.
Definition MemoryAllocatorDebugger-impl.hpp:91
MemoryAllocatorDebugger(Args &&... args)
Constructor.
Definition MemoryAllocatorDebugger-impl.hpp:45
bool isEmpty() const
Returns true if there is no memory allocated by the internal allocator.
Definition MemoryAllocatorDebugger-impl.hpp:147
const T & getInternalAllocator() const
Returns the internal allocator.
Definition MemoryAllocatorDebugger-impl.hpp:176
~MemoryAllocatorDebugger()
Destructor.
Definition MemoryAllocatorDebugger-impl.hpp:53
static const std::string & getStaticType()
Returns type string.
Definition MemoryAllocatorDebugger-impl.hpp:82
void print(std::ostream &os=std::cout, int indent=0) const override
Prints state of the memory allocator.
Definition MemoryAllocatorDebugger-impl.hpp:190