33 void dgInitMemoryGlobals();
35 void dgDestroyMemoryGlobals();
38 void dgApi dgFree(
void *
const ptr);
41 void *dgApi dgMallocStack(
size_t size);
42 void *dgApi dgMallocAligned(
size_t size, dgInt32 alignmentInBytes);
43 void dgApi dgFreeStack(
void *
const ptr);
45 typedef void *(dgApi *dgMemAlloc)(dgUnsigned32 size);
46 typedef void (dgApi *dgMemFree)(
void *
const ptr, dgUnsigned32 size);
49 typedef void (dgApi *dgSerialize)(
void *
const userData,
const void *
const buffer,
size_t size);
50 typedef void (dgApi *dgDeserialize)(
void *
const userData,
void *buffer,
size_t size);
53 void dgSetGlobalAllocators(dgMemAlloc alloc, dgMemFree free);
54 dgInt32 dgGetMemoryUsed();
57 #define DG_CLASS_ALLOCATOR_NEW(allocator) inline void *operator new (size_t size, dgMemoryAllocator* const allocator) { return dgMalloc(size, allocator);} 58 #define DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) inline void *operator new[] (size_t size, dgMemoryAllocator* const allocator) { return dgMalloc(size, allocator);} 59 #define DG_CLASS_ALLOCATOR_DELETE(allocator) inline void operator delete (void* const ptr, dgMemoryAllocator* const allocator) { dgFree(ptr); } 60 #define DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator) inline void operator delete[] (void* const ptr, dgMemoryAllocator* const allocator) { dgFree(ptr); } 61 #define DG_CLASS_ALLOCATOR_NEW_DUMMY inline void *operator new (size_t size) { NEWTON_ASSERT (0); return dgMalloc(size, NULL);} 62 #define DG_CLASS_ALLOCATOR_NEW_ARRAY_DUMMY inline void *operator new[] (size_t size) { NEWTON_ASSERT (0); return dgMalloc(size, NULL);} 63 #define DG_CLASS_ALLOCATOR_DELETE_DUMMY inline void operator delete (void* const ptr) { dgFree(ptr); } 64 #define DG_CLASS_ALLOCATOR_DELETE_ARRAY_DUMMY inline void operator delete[] (void* const ptr) { dgFree(ptr); } 67 #define DG_CLASS_ALLOCATOR(allocator) \ 68 DG_CLASS_ALLOCATOR_DELETE(allocator) \ 69 DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator) \ 70 DG_CLASS_ALLOCATOR_NEW(allocator) \ 71 DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) \ 72 DG_CLASS_ALLOCATOR_NEW_DUMMY \ 73 DG_CLASS_ALLOCATOR_NEW_ARRAY_DUMMY \ 74 DG_CLASS_ALLOCATOR_DELETE_DUMMY \ 75 DG_CLASS_ALLOCATOR_DELETE_ARRAY_DUMMY 81 static constexpr dgInt32 memoryGranularity =
sizeof(
void*) * 8;
82 static constexpr dgInt32 memorySize = 1024 - 64;
83 static constexpr dgInt32 memoryBinSize = 1024 * 16;
84 static constexpr dgInt32 memoryBinEntries = memorySize / memoryGranularity;
93 dgInt32 m_stepInBites;
98 char m_pool[memoryBinSize -
sizeof(
dgMemoryBinInfo) - memoryGranularity * 2];
116 dgInt32 m_workingSize;
119 DG_INLINE
void SaveInfo(
dgMemoryAllocator *
const allocator,
void *
const ptr, dgInt32 size, dgInt32 &enumerator, dgInt32 workingSize = 0) {
124 m_allocator = allocator;
126 m_workingSize = workingSize;
139 #ifdef __TRACK_MEMORY_LEAKS__ 140 class dgMemoryLeaksTracker {
141 #define DG_TRACK_MEMORY_LEAKS_ENTRIES (1024 * 1024 * 4) 146 dgInt32 m_allocationNumber;
150 dgMemoryLeaksTracker();
151 ~dgMemoryLeaksTracker();
152 void RemoveBlock(
void *
const ptr);
153 void InsertBlock(dgInt32 size,
void *
const ptr);
156 dgInt32 m_totalAllocatedBytes;
157 dgInt32 m_totalAllocatedCalls;
158 dgInt32 m_leakAllocationCounter;
159 Pool m_pool[DG_TRACK_MEMORY_LEAKS_ENTRIES];
166 void *
operator new (
size_t size);
167 void operator delete (
void *
const ptr);
168 dgInt32 GetMemoryUsed()
const;
169 void SetAllocatorsCallback(dgMemAlloc memAlloc, dgMemFree memFree);
170 void *MallocLow(dgInt32 size, dgInt32 alignment = memoryGranularity);
171 void FreeLow(
void *
const retPtr);
172 void *Malloc(dgInt32 memsize);
173 void Free(
void *
const retPtr);
179 dgInt32 m_emumerator;
180 dgInt32 m_memoryUsed;
185 #ifdef __TRACK_MEMORY_LEAKS__ 186 dgMemoryLeaksTracker m_leaklTracker;
Definition: dgMemory.h:87
Definition: dgMemory.h:103
Definition: dgMemory.h:89
Definition: dgMemory.h:80
Definition: dgMemory.h:131
Definition: dgMemory.h:109