ScummVM API documentation
dgMemory.h
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 dgInitMemoryGlobals();
34 
35 void dgDestroyMemoryGlobals();
36 
37 void *dgApi dgMalloc(size_t size, dgMemoryAllocator *const allocator);
38 void dgApi dgFree(void *const ptr);
39 
40 
41 void *dgApi dgMallocStack(size_t size);
42 void *dgApi dgMallocAligned(size_t size, dgInt32 alignmentInBytes);
43 void dgApi dgFreeStack(void *const ptr);
44 
45 typedef void *(dgApi *dgMemAlloc)(dgUnsigned32 size);
46 typedef void (dgApi *dgMemFree)(void *const ptr, dgUnsigned32 size);
47 
48 
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);
51 
52 //void dgSetMemoryDrivers (dgMemAlloc alloc, dgMemFree free);
53 void dgSetGlobalAllocators(dgMemAlloc alloc, dgMemFree free);
54 dgInt32 dgGetMemoryUsed();
55 
56 
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); }
65 
66 
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
76 
77 
78 
79 
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;
85 public:
86 
87  class dgMemoryBin {
88  public:
90  public:
91  dgInt32 m_count;
92  dgInt32 m_totalCount;
93  dgInt32 m_stepInBites;
94  dgMemoryBin *m_next;
95  dgMemoryBin *m_prev;
96  };
97 
98  char m_pool[memoryBinSize - sizeof(dgMemoryBinInfo) - memoryGranularity * 2];
99  dgMemoryBinInfo m_info;
100  };
101 
102 
104  public:
105  dgMemoryCacheEntry *m_next;
106  dgMemoryCacheEntry *m_prev;
107  };
108 
109  class dgMemoryInfo {
110  public:
111  void *m_ptr;
112  dgMemoryAllocator *m_allocator;
113  dgInt32 m_size;
114  dgInt32 m_enum;
115 #ifdef _DEBUG
116  dgInt32 m_workingSize;
117 #endif
118 
119  DG_INLINE void SaveInfo(dgMemoryAllocator *const allocator, void *const ptr, dgInt32 size, dgInt32 &enumerator, dgInt32 workingSize = 0) {
120  m_ptr = ptr;
121  m_size = size;
122  m_enum = enumerator;
123  enumerator ++;
124  m_allocator = allocator;
125 #ifdef _DEBUG
126  m_workingSize = workingSize;
127 #endif
128  }
129  };
130 
132  public:
133  dgMemoryBin *m_first;
134  dgMemoryCacheEntry *m_cache;
135  };
136 
137 
138  // this is a simple memory leak tracker, it uses an flat array of two megabyte indexed by a hatch code
139 #ifdef __TRACK_MEMORY_LEAKS__
140  class dgMemoryLeaksTracker {
141 #define DG_TRACK_MEMORY_LEAKS_ENTRIES (1024 * 1024 * 4)
142  class Pool {
143  public:
144  void *m_ptr;
145  dgInt32 m_size;
146  dgInt32 m_allocationNumber;
147  };
148 
149  public:
150  dgMemoryLeaksTracker();
151  ~dgMemoryLeaksTracker();
152  void RemoveBlock(void *const ptr);
153  void InsertBlock(dgInt32 size, void *const ptr);
154 
155  dgInt32 m_density;
156  dgInt32 m_totalAllocatedBytes;
157  dgInt32 m_totalAllocatedCalls;
158  dgInt32 m_leakAllocationCounter;
159  Pool m_pool[DG_TRACK_MEMORY_LEAKS_ENTRIES];
160 
161  };
162 #endif
163 
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);
174 
175 
176 protected:
177  dgMemoryAllocator(dgMemAlloc memAlloc, dgMemFree memFree);
178 
179  dgInt32 m_emumerator;
180  dgInt32 m_memoryUsed;
181  dgMemFree m_free;
182  dgMemAlloc m_malloc;
183  dgMemDirectory m_memoryDirectory[memoryBinEntries + 1];
184 
185 #ifdef __TRACK_MEMORY_LEAKS__
186  dgMemoryLeaksTracker m_leaklTracker;
187 #endif
188 };
189 
190 
191 #endif
192 
Definition: dgMemory.h:87
Definition: dgMemory.h:103
Definition: dgMemory.h:80
Definition: dgMemory.h:131
Definition: dgMemory.h:109