forked from POV-Ray/povray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvmem.h
364 lines (323 loc) · 10.1 KB
/
pvmem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//******************************************************************************
///
/// @file windows/pvmem.h
///
/// Definitions for windows-specific memory handling routines.
///
/// @author Christopher J. Cason
///
/// @copyright
/// @parblock
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.8.
/// Copyright 1991-2019 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by David K. Buck.
/// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
///
/// @endparblock
///
//******************************************************************************
#ifndef POVRAY_WINDOWS_PVMEM_H
#define POVRAY_WINDOWS_PVMEM_H
#undef new
#undef delete
namespace povwin
{
class WinMemStats final
{
public:
WinMemStats() { Clear(); }
~WinMemStats() {}
void Clear() { currentAllocated = peakAllocated = callsToAlloc = callsToFree = smallestAlloc = largestAlloc = 0; }
void * operator new(size_t count) { return (WinMemStats *) HeapAlloc(GetProcessHeap(), 0, count); }
void operator delete (void *p) { HeapFree(GetProcessHeap(), 0, p); }
void RecordAlloc(size_t count)
{
callsToAlloc++;
currentAllocated += count;
if (currentAllocated > 0)
if (peakAllocated < currentAllocated)
peakAllocated = currentAllocated;
if (count > 1)
if ((smallestAlloc == 0) || (count < smallestAlloc))
smallestAlloc = count;
if (count > largestAlloc)
largestAlloc = count;
}
void RecordFree(size_t count)
{
callsToFree++;
currentAllocated -= count;
}
void InterlockedRecordAlloc(size_t count)
{
}
void InterlockedRecordFree(size_t count)
{
}
void Report(unsigned __int64& allocs, unsigned __int64& frees, __int64& current,
unsigned __int64& peak, unsigned __int64& smallest, unsigned __int64& largest)
{
allocs = callsToAlloc;
frees = callsToFree;
current = currentAllocated;
peak = peakAllocated;
smallest = smallestAlloc;
largest = largestAlloc;
}
WinMemStats& operator=(const WinMemStats& rhs)
{
// not interlocked, should only be used when thread is not performing memory allocations
currentAllocated = rhs.currentAllocated;
peakAllocated = rhs.peakAllocated;
callsToAlloc = rhs.callsToAlloc;
callsToFree = rhs.callsToFree;
smallestAlloc = rhs.smallestAlloc;
largestAlloc = rhs.largestAlloc;
return *this;
}
WinMemStats operator+(const WinMemStats& rhs) const
{
// not interlocked, should only be used when thread is not performing memory allocations
WinMemStats result = *this;
result.currentAllocated += rhs.currentAllocated;
result.callsToAlloc += rhs.callsToAlloc;
result.callsToFree += rhs.callsToFree;
result.peakAllocated = 0;
result.smallestAlloc = 0;
result.largestAlloc = 0;
return result;
}
WinMemStats& operator+=(const WinMemStats& rhs)
{
// not interlocked, should only be used when threads are not performing memory allocations
currentAllocated += rhs.currentAllocated;
callsToAlloc += rhs.callsToAlloc;
callsToFree += rhs.callsToFree;
peakAllocated = 0;
smallestAlloc = 0;
largestAlloc = 0;
return *this;
}
private:
volatile __int64 currentAllocated;
volatile unsigned __int64 peakAllocated;
volatile unsigned __int64 callsToAlloc;
volatile unsigned __int64 callsToFree;
volatile unsigned __int64 smallestAlloc;
volatile unsigned __int64 largestAlloc;
};
typedef WinMemStats PovMemStats;
class WinHeap final
{
public:
WinHeap()
{
InitializeCriticalSectionAndSpinCount(&m_CritSec, 4000);
m_LowFragHeap = false;
SYSTEM_INFO si;
GetSystemInfo(&si);
// allocate heap
m_Heap = HeapCreate (HEAP_NO_SERIALIZE, si.dwPageSize, 0);
if (m_Heap == NULL)
return;
#if 0
// enable low-fragmentation heap. NB this only works on XP or later.
// TODO: make sure this isn't an unresolved symbol in W2K or earlier.
ULONG val = 2;
HeapSetInformation(m_Heap, HeapCompatibilityInformation, &val, sizeof(val));
if (HeapQueryInformation(m_Heap, HeapCompatibilityInformation, &val, sizeof(val), NULL))
if (val == 2)
m_LowFragHeap = true;
#endif
}
~WinHeap()
{
if (m_Heap != NULL)
{
Lock();
HeapDestroy(m_Heap);
Unlock();
}
DeleteCriticalSection(&m_CritSec);
}
void * operator new(size_t count) { return (WinHeap *) HeapAlloc(GetProcessHeap(), 0, count); }
bool Validate(const void *p = NULL) const { return m_Heap != NULL && HeapValidate(m_Heap, 0, p); }
void *Alloc(size_t count) { return HeapAlloc(m_Heap, 0, count ? count : 1); }
bool Free(void *p) { return HeapFree(m_Heap, 0, p); }
void *ReAlloc(void *p, size_t count) { return HeapReAlloc(m_Heap, 0, p, count); }
void Compact() { HeapCompact(m_Heap, 0); }
void Lock() { EnterCriticalSection(&m_CritSec); }
void Unlock() { LeaveCriticalSection(&m_CritSec); }
size_t BlockSize(const void *p) const { return HeapSize(m_Heap, 0, p); }
void *LockedAlloc(size_t count)
{
Lock();
void *p = HeapAlloc(m_Heap, 0, count ? count : 1);
Unlock();
return p;
}
void *LockedReAlloc(void *p, size_t count)
{
Lock();
p = HeapReAlloc(m_Heap, 0, p, count);
Unlock();
return p;
}
bool LockedFree(void *p)
{
Lock();
bool result = HeapFree(m_Heap, 0, p);
Unlock();
return result;
}
bool LockedValidate(const void *p = NULL)
{
if (m_Heap == NULL)
return false;
Lock();
bool result = HeapValidate(m_Heap, 0, p);
Unlock();
return result;
}
private:
bool m_LowFragHeap;
HANDLE m_Heap;
CRITICAL_SECTION m_CritSec;
};
class HeapLock final
{
public:
HeapLock(WinHeap *wh) : m_WinHeap(wh) { m_WinHeap->Lock(); }
~HeapLock() { m_WinHeap->Unlock(); }
private:
WinHeap *m_WinHeap;
};
class WinMemBlock final
{
public:
__declspec(nothrow) WinMemBlock(const void *data, int line)
{
m_Sanity = 'PM';
m_Line = line;
m_Data = data;
# if EXTRA_VALIDATION
m_Hash = ptrdiff_t(m_Sanity) ^ ptrdiff_t(m_Line) ^ ptrdiff_t(m_Data) ^ ptrdiff_t(m_Length) ^ ptrdiff_t(this);
memcpy((char *) this + m_Length - sizeof(m_Hash), &m_Hash, sizeof(m_Hash));
# endif
}
// must be called with the lock held
void __declspec(nothrow) * operator new (size_t size, size_t len)
{
# if EXTRA_VALIDATION
WinMemBlock *block = (WinMemBlock *) m_Heap->Alloc(len + size + BlockPadding);
if (block == NULL)
return NULL;
block->m_Length = len + size + BlockPadding;
return block;
# else
return m_Heap->Alloc(len + size);
# endif
}
// must be called with the lock held
void __declspec(nothrow) operator delete (void *p)
{
m_Heap->Free(p);
}
bool Validate(bool full = EXTRA_VALIDATION) const
{
if (m_Sanity != 'PM')
return false;
# if EXTRA_VALIDATION
ptrdiff_t hash = ptrdiff_t(m_Sanity) ^ ptrdiff_t(m_Line) ^ ptrdiff_t(m_Data) ^ ptrdiff_t(m_Length) ^ ptrdiff_t(this);
if (m_Hash != hash || memcmp(&m_Hash, (char *) this + m_Length - sizeof(m_Hash), sizeof(m_Hash)) != 0)
return false;
# endif
if (!full)
return true;
if (m_Heap->Validate(this) == false)
return false;
# if EXTRA_VALIDATION
if (m_Heap->BlockSize(this) < m_Length)
return false;
# endif
return true;
}
size_t Size() const
{
size_t len = m_Heap->BlockSize(this);
if (len == (size_t) -1 || len < sizeof(WinMemBlock) + BlockPadding)
return (size_t) -1;
# if EXTRA_VALIDATION
if (len < m_Length)
return (size_t) -1;
# endif
return len;
}
size_t Size(size_t size) const
{
return size + sizeof(WinMemBlock) + BlockPadding;
}
void *GetMem()
{
return this + 1;
}
WinMemBlock *Realloc(size_t size, const void *data, int line)
{
WinMemBlock *newBlock = (WinMemBlock *) m_Heap->ReAlloc(this, size + sizeof(WinMemBlock) + BlockPadding);
if (newBlock == NULL)
return NULL;
// note that *this is no longer valid here!
newBlock->m_Line = line;
newBlock->m_Data = data;
# if EXTRA_VALIDATION
newBlock->m_Length = size + sizeof(WinMemBlock) + BlockPadding;
newBlock->m_Hash = ptrdiff_t(newBlock->m_Sanity) ^ ptrdiff_t(newBlock->m_Line) ^ ptrdiff_t(newBlock->m_Data) ^
ptrdiff_t(newBlock->m_Length) ^ ptrdiff_t(newBlock);
memcpy((char *) newBlock + newBlock->m_Length - sizeof(m_Hash), &newBlock->m_Hash, sizeof(m_Hash));
# endif
return newBlock;
}
static WinMemBlock *GetBlock(void *p, bool full = EXTRA_VALIDATION)
{
WinMemBlock *block = (WinMemBlock *) p - 1;
if (!block->Validate(full))
return NULL;
return block;
}
static void SetAllocator(WinHeap *alloc) { m_Heap = alloc; }
private:
unsigned short m_Sanity;
short m_Line;
const void *m_Data;
static WinHeap *m_Heap;
# if EXTRA_VALIDATION
size_t m_Length;
ptrdiff_t m_Hash;
static const size_t BlockPadding = sizeof(ptrdiff_t);
# else
static const size_t BlockPadding = 0;
# endif
WinMemBlock() = delete;
void *operator new (size_t len) = delete;
};
}
// end of namespace povwin
#endif // POVRAY_WINDOWS_PVMEM_H