My Project: dynamics.mod/NewtonDynamics/source/core/dgMemory.h Source File

My Project

dgMemory.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #ifndef __dgMemory__
23 #define __dgMemory__
24
25 #include "dgStdafx.h"
26
27 #ifdef _DEBUG
28 //#define __TRACK_MEMORY_LEAKS__
29 #endif
30
31 class dgMemoryAllocator;
32
33 void* dgApi dgMalloc (size_t size, dgMemoryAllocator* const allocator);
34 void dgApi dgFree (void* const ptr);
35
36
37 void* dgApi dgMallocStack (size_t size);
38 void* dgApi dgMallocAligned (size_t size, dgInt32 alignmentInBytes);
39 void dgApi dgFreeStack (void* const ptr);
40
41 typedef void* (dgApi *dgMemAlloc) (dgUnsigned32 size);
42 typedef void (dgApi *dgMemFree) (void* const ptr, dgUnsigned32 size);
43
44
45 //void dgSetMemoryDrivers (dgMemAlloc alloc, dgMemFree free);
46 void dgSetGlobalAllocators (dgMemAlloc alloc, dgMemFree free);
47 dgInt32 dgGetMemoryUsed ();
48
49
50 #define DG_CLASS_ALLOCATOR_NEW(allocator) inline void *operator new (size_t size, dgMemoryAllocator* const allocator) { return dgMalloc(size, allocator);}
51 #define DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) inline void *operator new[] (size_t size, dgMemoryAllocator* const allocator) { return dgMalloc(size, allocator);}
52 #define DG_CLASS_ALLOCATOR_DELETE(allocator) inline void operator delete (void* const ptr, dgMemoryAllocator* const allocator) { dgFree(ptr); }
53 #define DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator) inline void operator delete[] (void* const ptr, dgMemoryAllocator* const allocator) { dgFree(ptr); }
54 #define DG_CLASS_ALLOCATOR_NEW_DUMMY inline void *operator new (size_t size) { dgAssert (0); return dgMalloc(size, NULL);}
55 #define DG_CLASS_ALLOCATOR_NEW_ARRAY_DUMMY inline void *operator new[] (size_t size) { dgAssert (0); return dgMalloc(size, NULL);}
56 #define DG_CLASS_ALLOCATOR_DELETE_DUMMY inline void operator delete (void* const ptr) { dgFree(ptr); }
57 #define DG_CLASS_ALLOCATOR_DELETE_ARRAY_DUMMY inline void operator delete[] (void* const ptr) { dgFree(ptr); }
58
59
60 #define DG_CLASS_ALLOCATOR(allocator) \
61 DG_CLASS_ALLOCATOR_DELETE(allocator) \
62 DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator) \
63 DG_CLASS_ALLOCATOR_NEW(allocator) \
64 DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) \
65 DG_CLASS_ALLOCATOR_NEW_DUMMY \
66 DG_CLASS_ALLOCATOR_NEW_ARRAY_DUMMY \
67 DG_CLASS_ALLOCATOR_DELETE_DUMMY \
68 DG_CLASS_ALLOCATOR_DELETE_ARRAY_DUMMY
69
70
71
72
73 class dgMemoryAllocator
74 {
75
76 #if (defined (_WIN_64_VER) || defined (_MINGW_64_VER) || defined (_POSIX_VER_64) || defined (_MACOSX_VER))
77 #define DG_MEMORY_GRANULARITY_BITS 6
78 #else
79 #define DG_MEMORY_GRANULARITY_BITS 5
80 #endif
81 #define DG_MEMORY_GRANULARITY (1 << DG_MEMORY_GRANULARITY_BITS)
82 #define DG_MEMORY_SIZE (1024 - 64)
83 #define DG_MEMORY_BIN_SIZE (1024 * 16)
84 #define DG_MEMORY_BIN_ENTRIES (DG_MEMORY_SIZE / DG_MEMORY_GRANULARITY)
85
86 public:
87
88 class dgMemoryBin
89 {
90 public:
91 class dgMemoryBinInfo
92 {
93 public:
94 dgInt32 m_count;
95 dgInt32 m_totalCount;
96 dgInt32 m_stepInBites;
97 dgMemoryBin* m_next;
98 dgMemoryBin* m_prev;
99 };
100
101 char m_pool[DG_MEMORY_BIN_SIZE - sizeof (dgMemoryBinInfo) - DG_MEMORY_GRANULARITY * 2];
102 dgMemoryBinInfo m_info;
103 };
104
105
106 class dgMemoryCacheEntry
107 {
108 public:
109 dgMemoryCacheEntry* m_next;
110 dgMemoryCacheEntry* m_prev;
111 };
112
113 class dgMemoryInfo
114 {
115 public:
116 void *m_ptr;
117 dgMemoryAllocator* m_allocator;
118 dgInt32 m_size;
119 dgInt32 m_enum;
120 #ifdef _DEBUG
121 dgInt32 m_workingSize;
122 #endif
123
124 DG_INLINE void SaveInfo(dgMemoryAllocator* const allocator, void* const ptr, dgInt32 size, dgInt32& enumerator, dgInt32 workingSize = 0)
125 {
126 m_ptr = ptr;
127 m_size = size;
128 m_enum = enumerator;
129 enumerator ++;
130 m_allocator = allocator;
131 #ifdef _DEBUG
132 m_workingSize = workingSize;
133 #endif
134 }
135 };
136
137 class dgMemDirectory
138 {
139 public:
140 dgMemoryBin* m_first;
141 dgMemoryCacheEntry* m_cache;
142 };
143
144
145 // this is a simple memory leak tracker, it uses an flat array of two megabyte indexed by a hatch code
146 #ifdef __TRACK_MEMORY_LEAKS__
147 class dgMemoryLeaksTracker
148 {
149 #define DG_TRACK_MEMORY_LEAKS_ENTRIES (1024 * 1024 * 4)
150 class Pool
151 {
152 public:
153 void* m_ptr;
154 dgInt32 m_size;
155 dgInt32 m_allocationNumber;
156 };
157
158 public:
159 dgMemoryLeaksTracker();
160 ~dgMemoryLeaksTracker ();
161 void RemoveBlock (void* const ptr);
162 void InsertBlock (dgInt32 size, void* const ptr);
163
164 dgInt32 m_density;
165 dgInt32 m_totalAllocatedBytes;
166 dgInt32 m_totalAllocatedCalls;
167 dgInt32 m_leakAllocationCounter;
168 Pool m_pool[DG_TRACK_MEMORY_LEAKS_ENTRIES];
169
170 };
171 #endif
172
173 dgMemoryAllocator ();
174 ~dgMemoryAllocator ();
175 void *operator new (size_t size);
176 void operator delete (void* const ptr);
177 dgInt32 GetMemoryUsed() const;
178 void SetAllocatorsCallback (dgMemAlloc memAlloc, dgMemFree memFree);
179 void *MallocLow (dgInt32 size, dgInt32 alignment = DG_MEMORY_GRANULARITY);
180 void FreeLow (void* const retPtr);
181 void *Malloc (dgInt32 memsize);
182 void Free (void* const retPtr);
183
184
185 protected:
186 dgMemoryAllocator (dgMemAlloc memAlloc, dgMemFree memFree);
187
188 dgInt32 m_emumerator;
189 dgInt32 m_memoryUsed;
190 dgMemFree m_free;
191 dgMemAlloc m_malloc;
192 dgMemDirectory m_memoryDirectory[DG_MEMORY_BIN_ENTRIES + 1];
193
194 #ifdef __TRACK_MEMORY_LEAKS__
195 dgMemoryLeaksTracker m_leaklTracker;
196 #endif
197 };
198
199
200 #endif
201
dgMemoryAllocator::dgMemoryInfo::SaveInfo
DG_INLINE void SaveInfo(dgMemoryAllocator *const allocator, void *const ptr, dgInt32 size, dgInt32 &enumerator, dgInt32 workingSize=0)
Definition: dgMemory.h:124
dgApi
#define dgApi
Definition: dgTypes.h:263
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo::m_stepInBites
dgInt32 m_stepInBites
Definition: dgMemory.h:96
dgMemoryAllocator::dgMemDirectory::m_cache
dgMemoryCacheEntry * m_cache
Definition: dgMemory.h:141
dgMemoryAllocator::m_memoryUsed
dgInt32 m_memoryUsed
Definition: dgMemory.h:189
dgMemoryAllocator::~dgMemoryAllocator
~dgMemoryAllocator()
Definition: dgMemory.cpp:107
dgMemoryAllocator::dgMemoryBin::m_info
dgMemoryBinInfo m_info
Definition: dgMemory.h:102
dgMemoryAllocator::dgMemoryInfo::m_ptr
void * m_ptr
Definition: dgMemory.h:116
dgMemoryAllocator::m_emumerator
dgInt32 m_emumerator
Definition: dgMemory.h:188
DG_INLINE
#define DG_INLINE
Definition: dgTypes.h:178
dgInt32
int dgInt32
Definition: dgTypes.h:218
void
typedef void(dgApi *ConstraintsForceFeeback)(const dgConstraint &me
dgMemoryAllocator::SetAllocatorsCallback
void SetAllocatorsCallback(dgMemAlloc memAlloc, dgMemFree memFree)
Definition: dgMemory.cpp:130
DG_MEMORY_BIN_ENTRIES
#define DG_MEMORY_BIN_ENTRIES
Definition: dgMemory.h:84
dgMemoryAllocator::m_free
dgMemFree m_free
Definition: dgMemory.h:190
dgMemoryAllocator::dgMemoryCacheEntry::m_next
dgMemoryCacheEntry * m_next
Definition: dgMemory.h:109
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo::m_totalCount
dgInt32 m_totalCount
Definition: dgMemory.h:95
dgMemoryAllocator::FreeLow
void FreeLow(void *const retPtr)
Definition: dgMemory.cpp:156
dgMemoryAllocator::dgMemDirectory::m_first
dgMemoryBin * m_first
Definition: dgMemory.h:140
dgMemoryAllocator::Malloc
void * Malloc(dgInt32 memsize)
Definition: dgMemory.cpp:172
dgFreeStack
void dgApi dgFreeStack(void *const ptr)
Definition: dgMemory.cpp:430
dgMallocAligned
void *dgApi dgMallocAligned(size_t size, dgInt32 alignmentInBytes)
Definition: dgMemory.cpp:417
dgGetMemoryUsed
dgInt32 dgGetMemoryUsed()
Definition: dgMemory.cpp:400
dgMemoryAllocator::dgMemoryBin
Definition: dgMemory.h:88
dgMemFree
void(dgApi * dgMemFree)(void *const ptr, dgUnsigned32 size)
Definition: dgMemory.h:42
dgStdafx.h
dgMemoryAllocator::m_malloc
dgMemAlloc m_malloc
Definition: dgMemory.h:191
dgSetGlobalAllocators
void dgSetGlobalAllocators(dgMemAlloc alloc, dgMemFree free)
Definition: dgMemory.cpp:395
dgMemoryAllocator::Free
void Free(void *const retPtr)
Definition: dgMemory.cpp:245
dgMemoryAllocator::GetMemoryUsed
dgInt32 GetMemoryUsed() const
Definition: dgMemory.cpp:125
dgFree
void dgApi dgFree(void *const ptr)
Definition: dgMemory.cpp:457
dgMemoryAllocator::dgMemoryInfo::m_size
dgInt32 m_size
Definition: dgMemory.h:118
dgMemAlloc
void *(dgApi * dgMemAlloc)(dgUnsigned32 size)
Definition: dgMemory.h:41
dgMalloc
void *dgApi dgMalloc(size_t size, dgMemoryAllocator *const allocator)
Definition: dgMemory.cpp:439
dgUnsigned32
unsigned dgUnsigned32
Definition: dgTypes.h:219
dgMallocStack
void *dgApi dgMallocStack(size_t size)
Definition: dgMemory.cpp:409
dgMemoryAllocator::MallocLow
void * MallocLow(dgInt32 size, dgInt32 alignment=DG_MEMORY_GRANULARITY)
Definition: dgMemory.cpp:138
dgMemoryAllocator::dgMemoryCacheEntry
Definition: dgMemory.h:106
dgMemoryAllocator::dgMemoryCacheEntry::m_prev
dgMemoryCacheEntry * m_prev
Definition: dgMemory.h:110
dgMemoryAllocator::dgMemoryBin::m_pool
char m_pool[DG_MEMORY_BIN_SIZE - sizeof(dgMemoryBinInfo) - DG_MEMORY_GRANULARITY *2]
Definition: dgMemory.h:101
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo
Definition: dgMemory.h:91
dgMemoryAllocator
Definition: dgMemory.h:73
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo::m_count
dgInt32 m_count
Definition: dgMemory.h:94
dgMemoryAllocator::dgMemDirectory
Definition: dgMemory.h:137
dgMemoryAllocator::dgMemoryInfo
Definition: dgMemory.h:113
dgMemoryAllocator::dgMemoryAllocator
dgMemoryAllocator()
Definition: dgMemory.cpp:89
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo::m_next
dgMemoryBin * m_next
Definition: dgMemory.h:97
dgMemoryAllocator::dgMemoryInfo::m_allocator
dgMemoryAllocator * m_allocator
Definition: dgMemory.h:117
dgMemoryAllocator::dgMemoryInfo::m_enum
dgInt32 m_enum
Definition: dgMemory.h:119
dgMemoryAllocator::dgMemoryBin::dgMemoryBinInfo::m_prev
dgMemoryBin * m_prev
Definition: dgMemory.h:98
DG_MEMORY_GRANULARITY
#define DG_MEMORY_GRANULARITY
Definition: dgMemory.h:81
dgMemoryAllocator::m_memoryDirectory
dgMemDirectory m_memoryDirectory[DG_MEMORY_BIN_ENTRIES+1]
Definition: dgMemory.h:192
DG_MEMORY_BIN_SIZE
#define DG_MEMORY_BIN_SIZE
Definition: dgMemory.h:83