-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_block.cpp
559 lines (465 loc) · 19.6 KB
/
dl_block.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*===========================================================================
*
* File: DL_Block.CPP
* Author: Dave Humphrey ([email protected])
* Created On: Tuesday, April 03, 2001
*
* Implements the memory logging routines for debug builds.
* If allocated memory blocks are stored in the block info structure
* upon creation and removed upon deletion, the logging routines
* can trace memory blocks that are deleted more than once or blocks that
* are never freed.
*
* The code is primarily from "Writing Solid Code" by Steve Maguire.
*
* 4 September 2002
* - Changed from a linked-list to a resizable array of pointers to
* increase the speed when dealing with a large number of allocated
* pointers.
*
*=========================================================================*/
#if defined(_DEBUG)
/* Include Files */
#include "dl_block.h"
#include "dl_log.h"
#include <string.h>
/*===========================================================================
*
* Begin Local Variables
*
*=========================================================================*/
DEFINE_FILE("DL_Block.cpp");
/* Allocated array of blocks */
static blockinfo_t** l_ppBlocks = NULL;
static int l_NumBlocks = 0;
static int l_AllocatedBlocks = 0;
/*===========================================================================
* End of Local Variables
*=========================================================================*/
/*===========================================================================
*
* Begin Pointer Comparison Macros
*
* The functions in this file must compare arbitrary pointers (ie, they
* could point to anything), which is not always portable according to ANSI.
* Thus, the following macros are used to define pointer comparisons to
* allow the correct comparisons to be made across various platforms.
* Ensure the comparisons are correct for your implemenation.
*
*=========================================================================*/
#define IsPtrLess(pLeft, pRight) ((pLeft) < (pRight))
#define IsPtrGreater(pLeft, pRight) ((pLeft) > (pRight))
#define IsPtrEqual(pLeft, pRight) ((pLeft) == (pRight))
#define IsPtrLessEq(pLeft, pRight) ((pLeft) <= (pRight))
#define IsPtrGreaterEq(pLeft, pRight) ((pLeft) >= (pRight))
/*===========================================================================
* End of Pointer Comparison Macros
*=========================================================================*/
/*===========================================================================
*
* Begin Private File Variables
*
*=========================================================================*/
/* Points to the start of a singly linked list of memory block
* information structures */
//static blockinfo_t* pBIHead = NULL;
/*===========================================================================
* End of Private File Variables
*=========================================================================*/
/*===========================================================================
*
* Function - void ResizeBlockArray (void);
*
* Local function to increase the size of the block array. The current
* block information is copied into the new block array.
*
*=========================================================================*/
void ResizeBlockArray (void) {
DEFINE_FUNCTION("ResizeBlockArray()");
blockinfo_t** ppNewArray;
/* Determine the new size of the array */
if (l_ppBlocks == NULL)
l_AllocatedBlocks = BLOCK_INITIAL_ARRAYSIZE;
else {
ASSERT(l_AllocatedBlocks < INT_MAX);
l_AllocatedBlocks *= BLOCK_RESIZEARRAY_FACTOR;
}
/* Allocate the new array */
ppNewArray = new blockinfo_t*[l_AllocatedBlocks];
ASSERT(ppNewArray != NULL);
/* Copy existing blocks if required */
if (l_ppBlocks != NULL) {
memcpy(ppNewArray, l_ppBlocks, sizeof(blockinfo_t*)*l_NumBlocks);
delete[] l_ppBlocks;
}
l_ppBlocks = ppNewArray;
}
/*===========================================================================
* End of Function ResizeBlockArray()
*=========================================================================*/
/*===========================================================================
*
* Function - static blockinfo_t* GetBlockInfo (pBlock);
*
* A local function which attempts to return the block information structure
* for the given memory block. If the block does not exist, the function
* will ASSERT. The function always returns a valid block.
*
*=========================================================================*/
static blockinfo_t* GetBlockInfo (void* pBlock) {
DEFINE_FUNCTION("GetBlockInfo()");
int BlockIndex;
byte* pStart;
byte* pEnd;
byte* pByteBlock = (byte *) pBlock;
/* Look through the array for a match */
for (BlockIndex = 0; BlockIndex < l_NumBlocks; BlockIndex++) {
pStart = l_ppBlocks[BlockIndex]->pPointer;
pEnd = pStart + l_ppBlocks[BlockIndex]->Size - 1;
if (IsPtrGreaterEq(pByteBlock, pStart) && IsPtrLessEq(pByteBlock, pEnd)) break;
}
/* Ensure a valid block was found */
ASSERT(BlockIndex < l_NumBlocks);
return (l_ppBlocks[BlockIndex]);
}
/*===========================================================================
* End of Function blockinfo_t* GetBlockInfo()
*=========================================================================*/
/*===========================================================================
*
* Function - void CheckMemoryRefs (void);
*
* Checks the reference member of all memory block logs looking for blocks
* that have not been referenced with NoteMemoryRef(). The function
* ASSERTs if any unflagged block is found. Use this function to check
* for dangling pointers in your application.
*
*=========================================================================*/
void CheckMemoryRefs (void) {
DEFINE_FUNCTION("CheckMemoryRefs()");
int BlockIndex;
blockinfo_t* pBlockInfo;
for (BlockIndex = 0; BlockIndex < l_NumBlocks; BlockIndex++) {
pBlockInfo = l_ppBlocks[BlockIndex];
/* Simple check for the integrity of the block. If this ASSERT
* fires it indicates that the blockinfo code has caused an
* error or its memory has been trashed. */
ASSERT(pBlockInfo->pPointer != NULL && pBlockInfo->Size != 0);
/* Ensure that the block has been referenced by a call to
* NoteMemoryRef(). If this ASSERT fires it indicates either
* a dangling pointer or that not all pointers have been
* accounted for. */
ASSERT(pBlockInfo->Referenced);
}
}
/*===========================================================================
* End of Function CheckMemoryRefs()
*=========================================================================*/
/*===========================================================================
*
* Function - void ClearMemoryRefs (void);
*
* Clears all the reference flags of the current memory block logs.
* Use this function before NoteMemoryRefs() to check for dangling pointers
* in your application.
*
*=========================================================================*/
void ClearMemoryRefs (void) {
//DEFINE_FUNCTION("ClearMemoryRefs()");
int BlockIndex;
for (BlockIndex = 0; BlockIndex < l_NumBlocks; BlockIndex++) {
l_ppBlocks[BlockIndex]->Referenced = FALSE;
}
}
/*===========================================================================
* End of Function ClearMemoryRefs()
*=========================================================================*/
/*===========================================================================
*
* Function - boolean CreateBlockInfo (pNewBlock, NewSize);
*
* Creates a new memory block information structure for the given memory.
* Returns TRUE on success or FALSE if the new block log could not be
* created. On failure the newly allocated block should be freed.
*
*=========================================================================*/
boolean CreateBlockInfo (void* pNewBlock, const size_t NewSize) {
DEFINE_FUNCTION("CreateBlockInfo(void*, size_t)");
blockinfo_t* pBlockInfo;
/* Ensure valid input */
ASSERT(pNewBlock != NULL && NewSize != 0);
/* Create the memory log structure */
pBlockInfo = new blockinfo_t;
if (pBlockInfo == NULL) return (FALSE);
/* Initialize the new memory block */
pBlockInfo->pPointer = (byte*) pNewBlock;
pBlockInfo->Size = NewSize;
pBlockInfo->Referenced = TRUE;
pBlockInfo->pName = NULL;
/* Resize blocks array if required */
if (l_ppBlocks == NULL || l_NumBlocks >= l_AllocatedBlocks) ResizeBlockArray();
/* Add the block info to the array */
l_ppBlocks[l_NumBlocks] = pBlockInfo;
l_NumBlocks++;
return (TRUE);
}
/*===========================================================================
* End of Function CreateBlockInfo()
*=========================================================================*/
/*===========================================================================
*
* Function - boolean CreateBlockInfo (pNewBlock, NewSize, pName, pFunc);
*
* Creates a new memory block information structure for the given memory.
* Returns TRUE on success or FALSE if the new block log could not be
* created. On failure the newly allocated block should be freed.
* Accepts the given valid name string for thje pointer name. ASSERTs if
* the input pNname pointer is not valid.
*
*=========================================================================*/
boolean CreateBlockInfo (void* pNewBlock, const size_t NewSize, const TCHAR* pName, const TCHAR* pFunc) {
DEFINE_FUNCTION("CreateBlockInfo(void*, size_t, char*)");
blockinfo_t* pBlockInfo;
/* Ensure valid input */
ASSERT(pNewBlock != NULL && NewSize != 0 && pName != NULL);
/* Create the memory log structure */
pBlockInfo = new blockinfo_t;
if (pBlockInfo == NULL) return (FALSE);
/* Initialize the new memory block */
pBlockInfo->pPointer = (byte*) pNewBlock;
pBlockInfo->Size = NewSize;
pBlockInfo->Referenced = TRUE;
/* Attempt to create the name string */
pBlockInfo->pName = new TCHAR[TSTRLEN(pName)+1];
if (pBlockInfo->pName == NULL) {
memset (pBlockInfo, (int)GARBAGE_CHAR, sizeof(blockinfo_t));
delete pBlockInfo;
return (FALSE);
}
TSTRCPY(pBlockInfo->pName, pName);
/* Attempt to create the function string if required */
if (pFunc == NULL) {
pBlockInfo->pFunc = NULL;
}
else {
pBlockInfo->pFunc = new TCHAR[TSTRLEN(pFunc)+1];
if (pBlockInfo->pFunc == NULL) {
memset (pBlockInfo, (int)GARBAGE_CHAR, sizeof(blockinfo_t));
delete pBlockInfo;
return (FALSE);
}
TSTRCPY(pBlockInfo->pFunc, pFunc);
}
/* Resize blocks array if required */
if (l_ppBlocks == NULL || l_NumBlocks >= l_AllocatedBlocks) ResizeBlockArray();
/* Add the block info to the array */
l_ppBlocks[l_NumBlocks] = pBlockInfo;
l_NumBlocks++;
return (TRUE);
}
/*===========================================================================
* End of Function CreateBlockInfo()
*=========================================================================*/
/*===========================================================================
*
* Function - void FreeBlockInfo (void* pBlock)
*
* Attempts to remove the block info for the given memory. The function
* will ASSERT if given an invalid memory block.
*
*=========================================================================*/
void FreeBlockInfo (void* pBlock) {
DEFINE_FUNCTION("FreeBlockInfo()");
int BlockIndex;
blockinfo_t* pBlockInfo = NULL;
byte* pByteBlock = (byte *) pBlock;
/* Ensure valid input */
ASSERT(pBlock != NULL);
/* Search the block linked list for a match, from head to tail */
for (BlockIndex = 0; BlockIndex < l_NumBlocks; BlockIndex++) {
pBlockInfo = l_ppBlocks[BlockIndex];
/* Check for a block match */
if (IsPtrEqual(pBlockInfo->pPointer, pByteBlock)) {
memmove(l_ppBlocks + BlockIndex, l_ppBlocks + BlockIndex + 1, sizeof(blockinfo_t*)*(l_NumBlocks - BlockIndex));
l_NumBlocks--;
break;
}
}
/* Ensure a valid block was found to be deleted */
ASSERT(pBlockInfo != NULL);
//ASSERT(BlockIndex < l_NumBlocks);
/* Delete the pointer name, if any */
if (pBlockInfo->pName != NULL) {
memset(pBlockInfo->pName, (int)GARBAGE_CHAR, (TSTRLEN(pBlockInfo->pName) + 1)*sizeof(TCHAR));
delete[] pBlockInfo->pName;
}
/* Delete the function name, if any */
if (pBlockInfo->pFunc != NULL) {
memset(pBlockInfo->pFunc, (int)GARBAGE_CHAR, (TSTRLEN(pBlockInfo->pFunc) + 1)*sizeof(TCHAR));
delete[] pBlockInfo->pFunc;
}
/* Clear the blockinfo structure and delete */
memset(pBlockInfo, (int)GARBAGE_CHAR, sizeof(blockinfo_t));
delete pBlockInfo;
}
/*===========================================================================
* End of Function FreeBlockInfo()
*=========================================================================*/
/*===========================================================================
*
* Function - size_t GetNumBlocks (void);
*
* Returns the total number of allocated blocks.
*
*=========================================================================*/
size_t GetNumBlocks (void) {
//DEFINE_FUNCTION("GetNumBlocks()");
return (l_NumBlocks);
}
/*===========================================================================
* End of Function GetNumBlocks()
*=========================================================================*/
/*===========================================================================
*
* Function - boolean IsValidPointer (pBlock, MinSize);
*
* Ensures that the given block is a valid memory block and that it is
* at least MinSize bytes in size (from the given pointer to the
* end of the block). The passed block does not have to be the very start
* of an allocated block, but can be anywhere in the block.
*
* The function will ASSERT if the block pointer or size is invalid, and
* never actually returns NULL. The value returned is to allow the
* function to be used in an ASSERT statement easily, such as
* ASSERT(IsValidPointer(pSomeObject, size));
*
*=========================================================================*/
boolean IsValidPointer (void* pBlock, const size_t MinSize) {
DEFINE_FUNCTION("IsValidPointer(void*, size_t)");
blockinfo_t* pBlockInfo;
byte* pByteBlock = (byte*) pBlock;
/* Ensure valid input */
ASSERT(pBlock != NULL && MinSize != 0);
pBlockInfo = GetBlockInfo(pBlock);
/* Ensure that the block size is valid */
ASSERT(IsPtrLessEq(pByteBlock + MinSize, pBlockInfo->pPointer + pBlockInfo->Size));
return (TRUE);
}
/*===========================================================================
* End of Function IsValidPointer()
*=========================================================================*/
/*===========================================================================
*
* Function - boolean IsValidPointer (pBlock);
*
* Same as IsValidPointer(void*, size_t) except that it just checks if
* the given pointer is valid without requiring a minimum size.
*
* The function will ASSERT if the block pointer or size is invalid, and
* never actually returns NULL. The value returned is to allow the
* function to be used in an ASSERT statement easily, such as
* ASSERT(IsValidPointer(pSomeObject));
*
*=========================================================================*/
boolean IsValidPointer (void* pBlock) {
DEFINE_FUNCTION("IsValidPointer(void*)");
blockinfo_t* pBlockInfo;
/* Ensure valid input */
ASSERT(pBlock != NULL);
/* Attempt to get the block info */
pBlockInfo = GetBlockInfo(pBlock);
if (pBlockInfo == NULL) return (FALSE);
return (TRUE);
}
/*===========================================================================
* End of Function IsValidPointer()
*=========================================================================*/
/*===========================================================================
*
* Function - void NoteMemoryRef (pBlock);
*
* Sets the reference flag for the log associated with the given memory
* block. The function ASSERTs if the given block is not valid. Note that
* the input block does not have to be the start of the allocated block,
* but anywhere within the block.
*
*=========================================================================*/
void NoteMemoryRef (void* pBlock) {
//DEFINE_FUNCTION("NoteMemoryRef()");
blockinfo_t* pBlockInfo;
pBlockInfo = GetBlockInfo(pBlock);
pBlockInfo->Referenced = TRUE;
}
/*===========================================================================
* End of Function NoteMemoryRef()
*=========================================================================*/
/*===========================================================================
*
* Function - void OutputBlockInfo (void);
*
* Outputs the current block information to the system log file.
*
*=========================================================================*/
void OutputBlockInfo (void) {
//DEFINE_FUNCTION("OutputBlockInfo()");
blockinfo_t* pBlockInfo;
int BlockIndex;
long Index = 1;
long MemorySize = 0l;
SystemLog.Printf (_T("Outputting custom allocation blocks..."));
for (BlockIndex = 0; BlockIndex < l_NumBlocks; BlockIndex++, Index++) {
pBlockInfo = l_ppBlocks[BlockIndex];
MemorySize += pBlockInfo->Size;
SystemLog.Printf (_T("\t\t%3ld) %p (%s, Func %s, %8u bytes), %s."), Index,
pBlockInfo->pPointer,
pBlockInfo->pName == NULL ? _T("NULL") : pBlockInfo->pName,
pBlockInfo->pFunc == NULL ? _T("NULL") : pBlockInfo->pFunc,
pBlockInfo->Size, pBlockInfo->Referenced ? _T("is Referenced") : _T("NOT Referenced"));
}
SystemLog.Printf (_T("\tOutput %ld bytes in %ld blocks..."), MemorySize, Index - 1);
}
/*===========================================================================
* End of Function OutputBlockInfo()
*=========================================================================*/
/*===========================================================================
*
* Function - size_t SizeOfBlock (pBlock);
*
* Attempts to get the allocated size of the given block. The function
* will ASSERT if the passed pointer is invalid.
*
*=========================================================================*/
size_t SizeOfBlock (void* pBlock) {
DEFINE_FUNCTION("SizeOfBlock()");
blockinfo_t* pBlockInfo;
pBlockInfo = GetBlockInfo(pBlock);
ASSERT((byte *) pBlock == pBlockInfo->pPointer);
return (pBlockInfo->Size);
}
/*===========================================================================
* End of Function SizeOfBlock()
*=========================================================================*/
/*===========================================================================
*
* Function - void UpdateBlockInfo (pOldBlock, pNewBlock, NewSize);
*
* Attempts to update a log structure for a memory block. If any of the
* input parameters are invalid, the function will ASSERT.
*
*=========================================================================*/
void UpdateBlockInfo (void* pOldBlock, void* pNewBlock, const size_t NewSize) {
DEFINE_FUNCTION("UpdateBlockInfo()");
blockinfo_t* pBlockInfo;
/* Ensure valid input */
ASSERT(pNewBlock != NULL && NewSize != 0);
/* Attempt to retrieve the block information */
pBlockInfo = GetBlockInfo(pOldBlock);
ASSERT((byte *) pOldBlock == pBlockInfo->pPointer);
/* Update the log information */
pBlockInfo->pPointer = (byte *) pNewBlock;
pBlockInfo->Size = NewSize;
}
/*===========================================================================
* End of Function UpdateBlockInfo()
*=========================================================================*/
#endif /* End of if defined(_DEBUG) */