~ubuntu-branches/ubuntu/precise/devil/precise

« back to all changes in this revision

Viewing changes to test/Stress/mmgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bradley Smith
  • Date: 2009-01-17 15:01:18 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090117150118-4bwb6nmvbz4srsjl
Tags: 1.7.5-4
Actually fix CVE-2008-5262. Closes: #512122.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ---------------------------------------------------------------------------------------------------------------------------------
2
 
//                                                      
3
 
//                                                      
4
 
//  _ __ ___  _ __ ___   __ _ _ __      ___ _ __  _ __  
5
 
// | '_ ` _ \| '_ ` _ \ / _` | '__|    / __| '_ \| '_ \ 
6
 
// | | | | | | | | | | | (_| | |    _ | (__| |_) | |_) |
7
 
// |_| |_| |_|_| |_| |_|\__, |_|   (_) \___| .__/| .__/ 
8
 
//                       __/ |             | |   | |    
9
 
//                      |___/              |_|   |_|    
10
 
//
11
 
// Memory manager & tracking software
12
 
//
13
 
// Best viewed with 8-character tabs and (at least) 132 columns
14
 
//
15
 
// ---------------------------------------------------------------------------------------------------------------------------------
16
 
//
17
 
// Restrictions & freedoms pertaining to usage and redistribution of this software:
18
 
//
19
 
//  * This software is 100% free
20
 
//  * If you use this software (in part or in whole) you must credit the author.
21
 
//  * This software may not be re-distributed (in part or in whole) in a modified
22
 
//    form without clear documentation on how to obtain a copy of the original work.
23
 
//  * You may not use this software to directly or indirectly cause harm to others.
24
 
//  * This software is provided as-is and without warrantee. Use at your own risk.
25
 
//
26
 
// For more information, visit HTTP://www.FluidStudios.com
27
 
//
28
 
// ---------------------------------------------------------------------------------------------------------------------------------
29
 
// Originally created on 12/22/2000 by Paul Nettle
30
 
//
31
 
// Copyright 2000, Fluid Studios, Inc., all rights reserved.
32
 
// ---------------------------------------------------------------------------------------------------------------------------------
33
 
//
34
 
// !!IMPORTANT!!
35
 
//
36
 
// This software is self-documented with periodic comments. Before you start using this software, perform a search for the string
37
 
// "-DOC-" to locate pertinent information about how to use this software.
38
 
//
39
 
// You are also encouraged to read the comment blocks throughout this source file. They will help you understand how this memory
40
 
// tracking software works, so you can better utilize it within your applications.
41
 
//
42
 
// NOTES:
43
 
//
44
 
// 1. If you get compiler errors having to do with set_new_handler, then go through this source and search/replace
45
 
//    "set_new_handler" with "set_new_handler".
46
 
//
47
 
// 2. This code purposely uses no external routines that allocate RAM (other than the raw allocation routines, such as malloc). We
48
 
//    do this because we want this to be as self-contained as possible. As an example, we don't use assert, because when running
49
 
//    under WIN32, the assert brings up a dialog box, which allocates RAM. Doing this in the middle of an allocation would be bad.
50
 
//
51
 
// 3. When trying to override new/delete under MFC (which has its own version of global new/delete) the linker will complain. In
52
 
//    order to fix this error, use the compiler option: /FORCE, which will force it to build an executable even with linker errors.
53
 
//    Be sure to check those errors each time you compile, otherwise, you may miss a valid linker error.
54
 
//
55
 
// 4. If you see something that looks odd to you or seems like a strange way of going about doing something, then consider that this
56
 
//    code was carefully thought out. If something looks odd, then just assume I've got a good reason for doing it that way (an
57
 
//    example is the use of the class MemStaticTimeTracker.)
58
 
//
59
 
// 5. With MFC applications, you will need to comment out any occurance of "#define new DEBUG_NEW" from all source files.
60
 
//
61
 
// 6. Include file dependencies are _very_important_ for getting the MMGR to integrate nicely into your application. Be careful if
62
 
//    you're including standard includes from within your own project inclues; that will break this very specific dependency order. 
63
 
//    It should look like this:
64
 
//
65
 
//              #include <stdio.h>   // Standard includes MUST come first
66
 
//              #include <stdlib.h>  //
67
 
//              #include <streamio>  //
68
 
//
69
 
//              #include "mmgr.h"    // mmgr.h MUST come next
70
 
//
71
 
//              #include "myfile1.h" // Project includes MUST come last
72
 
//              #include "myfile2.h" //
73
 
//              #include "myfile3.h" //
74
 
//
75
 
// ---------------------------------------------------------------------------------------------------------------------------------
76
 
 
77
 
#include <iostream>
78
 
#include <stdio.h>
79
 
#include <stdlib.h>
80
 
#include <assert.h>
81
 
#include <string.h>
82
 
#include <time.h>
83
 
#include <stdarg.h>
84
 
#include <new>
85
 
using namespace std;
86
 
 
87
 
#ifndef WIN32
88
 
#include <unistd.h>
89
 
#endif
90
 
 
91
 
#include "mmgr.h"
92
 
 
93
 
// ---------------------------------------------------------------------------------------------------------------------------------
94
 
// -DOC- If you're like me, it's hard to gain trust in foreign code. This memory manager will try to INDUCE your code to crash (for
95
 
// very good reasons... like making bugs obvious as early as possible.) Some people may be inclined to remove this memory tracking
96
 
// software if it causes crashes that didn't exist previously. In reality, these new crashes are the BEST reason for using this
97
 
// software!
98
 
//
99
 
// Whether this software causes your application to crash, or if it reports errors, you need to be able to TRUST this software. To
100
 
// this end, you are given some very simple debugging tools.
101
 
// 
102
 
// The quickest way to locate problems is to enable the STRESS_TEST macro (below.) This should catch 95% of the crashes before they
103
 
// occur by validating every allocation each time this memory manager performs an allocation function. If that doesn't work, keep
104
 
// reading...
105
 
//
106
 
// If you enable the TEST_MEMORY_MANAGER #define (below), this memory manager will log an entry in the memory.log file each time it
107
 
// enters and exits one of its primary allocation handling routines. Each call that succeeds should place an "ENTER" and an "EXIT"
108
 
// into the log. If the program crashes within the memory manager, it will log an "ENTER", but not an "EXIT". The log will also
109
 
// report the name of the routine.
110
 
//
111
 
// Just because this memory manager crashes does not mean that there is a bug here! First, an application could inadvertantly damage
112
 
// the heap, causing malloc(), realloc() or free() to crash. Also, an application could inadvertantly damage some of the memory used
113
 
// by this memory tracking software, causing it to crash in much the same way that a damaged heap would affect the standard
114
 
// allocation routines.
115
 
//
116
 
// In the event of a crash within this code, the first thing you'll want to do is to locate the actual line of code that is
117
 
// crashing. You can do this by adding log() entries throughout the routine that crashes, repeating this process until you narrow
118
 
// in on the offending line of code. If the crash happens in a standard C allocation routine (i.e. malloc, realloc or free) don't
119
 
// bother contacting me, your application has damaged the heap. You can help find the culprit in your code by enabling the
120
 
// STRESS_TEST macro (below.)
121
 
//
122
 
// If you truely suspect a bug in this memory manager (and you had better be sure about it! :) you can contact me at
123
 
// midnight@FluidStudios.com. Before you do, however, check for a newer version at:
124
 
//
125
 
//      http://www.FluidStudios.com/publications.html
126
 
//
127
 
// When using this debugging aid, make sure that you are NOT setting the alwaysLogAll variable on, otherwise the log could be
128
 
// cluttered and hard to read.
129
 
// ---------------------------------------------------------------------------------------------------------------------------------
130
 
 
131
 
//#define       TEST_MEMORY_MANAGER
132
 
 
133
 
// ---------------------------------------------------------------------------------------------------------------------------------
134
 
// -DOC- Enable this sucker if you really want to stress-test your app's memory usage, or to help find hard-to-find bugs
135
 
// ---------------------------------------------------------------------------------------------------------------------------------
136
 
 
137
 
//#define       STRESS_TEST
138
 
 
139
 
// ---------------------------------------------------------------------------------------------------------------------------------
140
 
// -DOC- Enable this sucker if you want to stress-test your app's error-handling. Set RANDOM_FAIL to the percentage of failures you
141
 
//       want to test with (0 = none, >100 = all failures).
142
 
// ---------------------------------------------------------------------------------------------------------------------------------
143
 
 
144
 
//#define       RANDOM_FAILURE 10.0
145
 
 
146
 
// ---------------------------------------------------------------------------------------------------------------------------------
147
 
// -DOC- Locals -- modify these flags to suit your needs
148
 
// ---------------------------------------------------------------------------------------------------------------------------------
149
 
 
150
 
#ifdef  STRESS_TEST
151
 
static  const   unsigned int    hashBits               = 12;
152
 
static          bool            randomWipe             = true;
153
 
static          bool            alwaysValidateAll      = true;
154
 
static          bool            alwaysLogAll           = true;
155
 
static          bool            alwaysWipeAll          = true;
156
 
static          bool            cleanupLogOnFirstRun   = true;
157
 
static  const   unsigned int    paddingSize            = 1024; // An extra 8K per allocation!
158
 
#else
159
 
static  const   unsigned int    hashBits               = 12;
160
 
static          bool            randomWipe             = false;
161
 
static          bool            alwaysValidateAll      = false;
162
 
static          bool            alwaysLogAll           = false;
163
 
static          bool            alwaysWipeAll          = true;
164
 
static          bool            cleanupLogOnFirstRun   = true;
165
 
static  const   unsigned int    paddingSize            = 4;
166
 
#endif
167
 
 
168
 
// ---------------------------------------------------------------------------------------------------------------------------------
169
 
// We define our own assert, because we don't want to bring up an assertion dialog, since that allocates RAM. Our new assert
170
 
// simply declares a forced breakpoint.
171
 
//
172
 
// The BEOS assert added by Arvid Norberg <arvid@iname.com>.
173
 
// ---------------------------------------------------------------------------------------------------------------------------------
174
 
 
175
 
#ifdef  WIN32
176
 
        #ifdef  _DEBUG
177
 
        #define m_assert(x) if ((x) == false) __asm { int 3 }
178
 
        #else
179
 
        #define m_assert(x) {}
180
 
        #endif
181
 
#elif defined(__BEOS__)
182
 
        #ifdef DEBUG
183
 
                extern void debugger(const char *message);
184
 
                #define m_assert(x) if ((x) == false) debugger("mmgr: assert failed")
185
 
        #else
186
 
                #define m_assert(x) {}
187
 
        #endif
188
 
#else   // Linux uses assert, which we can use safely, since it doesn't bring up a dialog within the program.
189
 
        #define m_assert(cond) assert(cond)
190
 
#endif
191
 
 
192
 
// ---------------------------------------------------------------------------------------------------------------------------------
193
 
// Here, we turn off our macros because any place in this source file where the word 'new' or the word 'delete' (etc.)
194
 
// appear will be expanded by the macro. So to avoid problems using them within this source file, we'll just #undef them.
195
 
// ---------------------------------------------------------------------------------------------------------------------------------
196
 
 
197
 
#undef  new
198
 
#undef  delete
199
 
#undef  malloc
200
 
#undef  calloc
201
 
#undef  realloc
202
 
#undef  free
203
 
 
204
 
// ---------------------------------------------------------------------------------------------------------------------------------
205
 
// Defaults for the constants & statics in the MemoryManager class
206
 
// ---------------------------------------------------------------------------------------------------------------------------------
207
 
 
208
 
const           unsigned int    m_alloc_unknown        = 0;
209
 
const           unsigned int    m_alloc_new            = 1;
210
 
const           unsigned int    m_alloc_new_array      = 2;
211
 
const           unsigned int    m_alloc_malloc         = 3;
212
 
const           unsigned int    m_alloc_calloc         = 4;
213
 
const           unsigned int    m_alloc_realloc        = 5;
214
 
const           unsigned int    m_alloc_delete         = 6;
215
 
const           unsigned int    m_alloc_delete_array   = 7;
216
 
const           unsigned int    m_alloc_free           = 8;
217
 
 
218
 
// ---------------------------------------------------------------------------------------------------------------------------------
219
 
// -DOC- Get to know these values. They represent the values that will be used to fill unused and deallocated RAM.
220
 
// ---------------------------------------------------------------------------------------------------------------------------------
221
 
 
222
 
static          unsigned int    prefixPattern          = 0xbaadf00d; // Fill pattern for bytes preceeding allocated blocks
223
 
static          unsigned int    postfixPattern         = 0xdeadc0de; // Fill pattern for bytes following allocated blocks
224
 
static          unsigned int    unusedPattern          = 0xfeedface; // Fill pattern for freshly allocated blocks
225
 
static          unsigned int    releasedPattern        = 0xdeadbeef; // Fill pattern for deallocated blocks
226
 
 
227
 
// ---------------------------------------------------------------------------------------------------------------------------------
228
 
// Other locals
229
 
// ---------------------------------------------------------------------------------------------------------------------------------
230
 
 
231
 
static  const   unsigned int    hashSize               = 1 << hashBits;
232
 
static  const   char            *allocationTypes[]     = {"Unknown",
233
 
                                                          "new",     "new[]",  "malloc",   "calloc",
234
 
                                                          "realloc", "delete", "delete[]", "free"};
235
 
static          sAllocUnit      *hashTable[hashSize];
236
 
static          sAllocUnit      *reservoir;
237
 
static          unsigned int    currentAllocationCount = 0;
238
 
static          unsigned int    breakOnAllocationCount = 0;
239
 
static          sMStats         stats;
240
 
static  const   char            *sourceFile            = "??";
241
 
static  const   char            *sourceFunc            = "??";
242
 
static          unsigned int    sourceLine             = 0;
243
 
static          bool            staticDeinitTime       = false;
244
 
static          sAllocUnit      **reservoirBuffer      = NULL;
245
 
static          unsigned int    reservoirBufferSize    = 0;
246
 
static const    char            *memoryLogFile         = "memory.log";
247
 
static const    char            *memoryLeakLogFile     = "memleaks.log";
248
 
static          void            doCleanupLogOnFirstRun();
249
 
 
250
 
// ---------------------------------------------------------------------------------------------------------------------------------
251
 
// Local functions only
252
 
// ---------------------------------------------------------------------------------------------------------------------------------
253
 
 
254
 
static  void    log(const char *format, ...)
255
 
{
256
 
        // Cleanup the log?
257
 
 
258
 
        if (cleanupLogOnFirstRun) doCleanupLogOnFirstRun();
259
 
 
260
 
        // Build the buffer
261
 
 
262
 
        static char buffer[2048];
263
 
        va_list ap;
264
 
        va_start(ap, format);
265
 
        vsprintf(buffer, format, ap);
266
 
        va_end(ap);
267
 
 
268
 
        // Open the log file
269
 
 
270
 
        FILE    *fp = fopen(memoryLogFile, "ab");
271
 
 
272
 
        // If you hit this assert, then the memory logger is unable to log information to a file (can't open the file for some
273
 
        // reason.) You can interrogate the variable 'buffer' to see what was supposed to be logged (but won't be.)
274
 
        m_assert(fp);
275
 
 
276
 
        if (!fp) return;
277
 
 
278
 
        // Spit out the data to the log
279
 
 
280
 
        fprintf(fp, "%s\r\n", buffer);
281
 
        fclose(fp);
282
 
}
283
 
 
284
 
// ---------------------------------------------------------------------------------------------------------------------------------
285
 
 
286
 
static  void    doCleanupLogOnFirstRun()
287
 
{
288
 
        if (cleanupLogOnFirstRun)
289
 
        {
290
 
                unlink(memoryLogFile);
291
 
                cleanupLogOnFirstRun = false;
292
 
 
293
 
                // Print a header for the log
294
 
 
295
 
                time_t  t = time(NULL);
296
 
                log("--------------------------------------------------------------------------------");
297
 
                log("");
298
 
                log("      %s - Memory logging file created on %s", memoryLogFile, asctime(localtime(&t)));
299
 
                log("--------------------------------------------------------------------------------");
300
 
                log("");
301
 
                log("This file contains a log of all memory operations performed during the last run.");
302
 
                log("");
303
 
                log("Interrogate this file to track errors or to help track down memory-related");
304
 
                log("issues. You can do this by tracing the allocations performed by a specific owner");
305
 
                log("or by tracking a specific address through a series of allocations and");
306
 
                log("reallocations.");
307
 
                log("");
308
 
                log("There is a lot of useful information here which, when used creatively, can be");
309
 
                log("extremely helpful.");
310
 
                log("");
311
 
                log("Note that the following guides are used throughout this file:");
312
 
                log("");
313
 
                log("   [!] - Error");
314
 
                log("   [+] - Allocation");
315
 
                log("   [~] - Reallocation");
316
 
                log("   [-] - Deallocation");
317
 
                log("   [I] - Generic information");
318
 
                log("   [F] - Failure induced for the purpose of stress-testing your application");
319
 
                log("   [D] - Information used for debugging this memory manager");
320
 
                log("");
321
 
                log("...so, to find all errors in the file, search for \"[!]\"");
322
 
                log("");
323
 
                log("--------------------------------------------------------------------------------");
324
 
        }
325
 
}
326
 
 
327
 
// ---------------------------------------------------------------------------------------------------------------------------------
328
 
 
329
 
static  const char      *sourceFileStripper(const char *sourceFile)
330
 
{
331
 
        char    *ptr = strrchr(sourceFile, '\\');
332
 
        if (ptr) return ptr + 1;
333
 
        ptr = strrchr(sourceFile, '/');
334
 
        if (ptr) return ptr + 1;
335
 
        return sourceFile;
336
 
}
337
 
 
338
 
// ---------------------------------------------------------------------------------------------------------------------------------
339
 
 
340
 
static  const char      *ownerString(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc)
341
 
{
342
 
        static  char    str[90];
343
 
        memset(str, 0, sizeof(str));
344
 
        sprintf(str, "%s(%05d)::%s", sourceFileStripper(sourceFile), sourceLine, sourceFunc);
345
 
        return str;
346
 
}
347
 
 
348
 
// ---------------------------------------------------------------------------------------------------------------------------------
349
 
 
350
 
static  const char      *insertCommas(unsigned int value)
351
 
{
352
 
        static  char    str[30];
353
 
        memset(str, 0, sizeof(str));
354
 
 
355
 
        sprintf(str, "%u", value);
356
 
        if (strlen(str) > 3)
357
 
        {
358
 
                memmove(&str[strlen(str)-3], &str[strlen(str)-4], 4);
359
 
                str[strlen(str) - 4] = ',';
360
 
        }
361
 
        if (strlen(str) > 7)
362
 
        {
363
 
                memmove(&str[strlen(str)-7], &str[strlen(str)-8], 8);
364
 
                str[strlen(str) - 8] = ',';
365
 
        }
366
 
        if (strlen(str) > 11)
367
 
        {
368
 
                memmove(&str[strlen(str)-11], &str[strlen(str)-12], 12);
369
 
                str[strlen(str) - 12] = ',';
370
 
        }
371
 
 
372
 
        return str;
373
 
}
374
 
 
375
 
// ---------------------------------------------------------------------------------------------------------------------------------
376
 
 
377
 
static  const char      *memorySizeString(unsigned long size)
378
 
{
379
 
        static  char    str[90];
380
 
             if (size > (1024*1024))    sprintf(str, "%10s (%7.2fM)", insertCommas(size), static_cast<float>(size) / (1024.0f * 1024.0f));
381
 
        else if (size > 1024)           sprintf(str, "%10s (%7.2fK)", insertCommas(size), static_cast<float>(size) / 1024.0f);
382
 
        else                            sprintf(str, "%10s bytes     ", insertCommas(size));
383
 
        return str;
384
 
}
385
 
 
386
 
// ---------------------------------------------------------------------------------------------------------------------------------
387
 
 
388
 
static  sAllocUnit      *findAllocUnit(const void *reportedAddress)
389
 
{
390
 
        // Just in case...
391
 
        m_assert(reportedAddress != NULL);
392
 
 
393
 
        // Use the address to locate the hash index. Note that we shift off the lower four bits. This is because most allocated
394
 
        // addresses will be on four-, eight- or even sixteen-byte boundaries. If we didn't do this, the hash index would not have
395
 
        // very good coverage.
396
 
 
397
 
        unsigned int    hashIndex = (reinterpret_cast<unsigned int>(const_cast<void *>(reportedAddress)) >> 4) & (hashSize - 1);
398
 
        sAllocUnit      *ptr = hashTable[hashIndex];
399
 
        while(ptr)
400
 
        {
401
 
                if (ptr->reportedAddress == reportedAddress) return ptr;
402
 
                ptr = ptr->next;
403
 
        }
404
 
 
405
 
        return NULL;
406
 
}
407
 
 
408
 
// ---------------------------------------------------------------------------------------------------------------------------------
409
 
 
410
 
static  size_t  calculateActualSize(const size_t reportedSize)
411
 
{
412
 
        // We use DWORDS as our padding, and a long is guaranteed to be 4 bytes, but an int is not (ANSI defines an int as
413
 
        // being the standard word size for a processor; on a 32-bit machine, that's 4 bytes, but on a 64-bit machine, it's
414
 
        // 8 bytes, which means an int can actually be larger than a long.)
415
 
 
416
 
        return reportedSize + paddingSize * sizeof(long) * 2;
417
 
}
418
 
 
419
 
// ---------------------------------------------------------------------------------------------------------------------------------
420
 
 
421
 
static  size_t  calculateReportedSize(const size_t actualSize)
422
 
{
423
 
        // We use DWORDS as our padding, and a long is guaranteed to be 4 bytes, but an int is not (ANSI defines an int as
424
 
        // being the standard word size for a processor; on a 32-bit machine, that's 4 bytes, but on a 64-bit machine, it's
425
 
        // 8 bytes, which means an int can actually be larger than a long.)
426
 
 
427
 
        return actualSize - paddingSize * sizeof(long) * 2;
428
 
}
429
 
 
430
 
// ---------------------------------------------------------------------------------------------------------------------------------
431
 
 
432
 
static  void    *calculateReportedAddress(const void *actualAddress)
433
 
{
434
 
        // We allow this...
435
 
 
436
 
        if (!actualAddress) return NULL;
437
 
 
438
 
        // JUst account for the padding
439
 
 
440
 
        return reinterpret_cast<void *>(const_cast<char *>(reinterpret_cast<const char *>(actualAddress) + sizeof(long) * paddingSize));
441
 
}
442
 
 
443
 
// ---------------------------------------------------------------------------------------------------------------------------------
444
 
 
445
 
static  void    wipeWithPattern(sAllocUnit *allocUnit, unsigned long pattern, const unsigned int originalReportedSize = 0)
446
 
{
447
 
        // For a serious test run, we use wipes of random a random value. However, if this causes a crash, we don't want it to
448
 
        // crash in a differnt place each time, so we specifically DO NOT call srand. If, by chance your program calls srand(),
449
 
        // you may wish to disable that when running with a random wipe test. This will make any crashes more consistent so they
450
 
        // can be tracked down easier.
451
 
 
452
 
        if (randomWipe)
453
 
        {
454
 
                pattern = ((rand() & 0xff) << 24) | ((rand() & 0xff) << 16) | ((rand() & 0xff) << 8) | (rand() & 0xff);
455
 
        }
456
 
 
457
 
        // -DOC- We should wipe with 0's if we're not in debug mode, so we can help hide bugs if possible when we release the
458
 
        // product. So uncomment the following line for releases.
459
 
        //
460
 
        // Note that the "alwaysWipeAll" should be turned on for this to have effect, otherwise it won't do much good. But we'll
461
 
        // leave it this way (as an option) because this does slow things down.
462
 
//      pattern = 0;
463
 
 
464
 
        // This part of the operation is optional
465
 
 
466
 
        if (alwaysWipeAll && allocUnit->reportedSize > originalReportedSize)
467
 
        {
468
 
                // Fill the bulk
469
 
 
470
 
                long    *lptr = reinterpret_cast<long *>(reinterpret_cast<char *>(allocUnit->reportedAddress) + originalReportedSize);
471
 
                int     length = static_cast<int>(allocUnit->reportedSize - originalReportedSize);
472
 
                int     i;
473
 
                for (i = 0; i < (length >> 2); i++, lptr++)
474
 
                {
475
 
                        *lptr = pattern;
476
 
                }
477
 
 
478
 
                // Fill the remainder
479
 
 
480
 
                unsigned int    shiftCount = 0;
481
 
                char            *cptr = reinterpret_cast<char *>(lptr);
482
 
                for (i = 0; i < (length & 0x3); i++, cptr++, shiftCount += 8)
483
 
                {
484
 
                        *cptr = static_cast<char>((pattern & (0xff << shiftCount)) >> shiftCount);
485
 
                }
486
 
        }
487
 
 
488
 
        // Write in the prefix/postfix bytes
489
 
 
490
 
        long            *pre = reinterpret_cast<long *>(allocUnit->actualAddress);
491
 
        long            *post = reinterpret_cast<long *>(reinterpret_cast<char *>(allocUnit->actualAddress) + allocUnit->actualSize - paddingSize * sizeof(long));
492
 
        for (unsigned int i = 0; i < paddingSize; i++, pre++, post++)
493
 
        {
494
 
                *pre = prefixPattern;
495
 
                *post = postfixPattern;
496
 
        }
497
 
}
498
 
 
499
 
// ---------------------------------------------------------------------------------------------------------------------------------
500
 
 
501
 
static  void    dumpAllocations(FILE *fp)
502
 
{
503
 
        fprintf(fp, "Alloc.   Addr       Size       Addr       Size                        BreakOn BreakOn              \r\n");
504
 
        fprintf(fp, "Number Reported   Reported    Actual     Actual     Unused    Method  Dealloc Realloc Allocated by \r\n");
505
 
        fprintf(fp, "------ ---------- ---------- ---------- ---------- ---------- -------- ------- ------- --------------------------------------------------- \r\n");
506
 
 
507
 
 
508
 
        for (unsigned int i = 0; i < hashSize; i++)
509
 
        {
510
 
                sAllocUnit *ptr = hashTable[i];
511
 
                while(ptr)
512
 
                {
513
 
                        fprintf(fp, "%06d 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X %-8s    %c       %c    %s\r\n",
514
 
                                ptr->allocationNumber,
515
 
                                reinterpret_cast<unsigned int>(ptr->reportedAddress), ptr->reportedSize,
516
 
                                reinterpret_cast<unsigned int>(ptr->actualAddress), ptr->actualSize,
517
 
                                m_calcUnused(ptr),
518
 
                                allocationTypes[ptr->allocationType],
519
 
                                ptr->breakOnDealloc ? 'Y':'N',
520
 
                                ptr->breakOnRealloc ? 'Y':'N',
521
 
                                ownerString(ptr->sourceFile, ptr->sourceLine, ptr->sourceFunc));
522
 
                        ptr = ptr->next;
523
 
                }
524
 
        }
525
 
}
526
 
 
527
 
// ---------------------------------------------------------------------------------------------------------------------------------
528
 
 
529
 
static  void    dumpLeakReport()
530
 
{
531
 
        // Open the report file
532
 
 
533
 
        FILE    *fp = fopen(memoryLeakLogFile, "w+b");
534
 
 
535
 
        // If you hit this assert, then the memory report generator is unable to log information to a file (can't open the file for
536
 
        // some reason.)
537
 
        m_assert(fp);
538
 
        if (!fp) return;
539
 
 
540
 
        // Any leaks?
541
 
 
542
 
        // Header
543
 
 
544
 
        static  char    timeString[25];
545
 
        memset(timeString, 0, sizeof(timeString));
546
 
        time_t  t = time(NULL);
547
 
        struct  tm *tme = localtime(&t);
548
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
549
 
        fprintf(fp, "|                                          Memory leak report for:  %02d/%02d/%04d %02d:%02d:%02d                                            |\r\n", tme->tm_mon + 1, tme->tm_mday, tme->tm_year + 1900, tme->tm_hour, tme->tm_min, tme->tm_sec);
550
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
551
 
        fprintf(fp, "\r\n");
552
 
        fprintf(fp, "\r\n");
553
 
        if (stats.totalAllocUnitCount)
554
 
        {
555
 
                fprintf(fp, "%d memory leak%s found:\r\n", stats.totalAllocUnitCount, stats.totalAllocUnitCount == 1 ? "":"s");
556
 
        }
557
 
        else
558
 
        {
559
 
                fprintf(fp, "Congratulations! No memory leaks found!\r\n");
560
 
 
561
 
                // We can finally free up our own memory allocations
562
 
 
563
 
                if (reservoirBuffer)
564
 
                {
565
 
                        for (unsigned int i = 0; i < reservoirBufferSize; i++)
566
 
                        {
567
 
                                free(reservoirBuffer[i]);
568
 
                        }
569
 
                        free(reservoirBuffer);
570
 
                        reservoirBuffer = 0;
571
 
                        reservoirBufferSize = 0;
572
 
                        reservoir = NULL;
573
 
                }
574
 
        }
575
 
        fprintf(fp, "\r\n");
576
 
 
577
 
        if (stats.totalAllocUnitCount)
578
 
        {
579
 
                dumpAllocations(fp);
580
 
        }
581
 
 
582
 
        fclose(fp);
583
 
}
584
 
 
585
 
// ---------------------------------------------------------------------------------------------------------------------------------
586
 
// We use a static class to let us know when we're in the midst of static deinitialization
587
 
// ---------------------------------------------------------------------------------------------------------------------------------
588
 
 
589
 
class   MemStaticTimeTracker
590
 
{
591
 
public:
592
 
        MemStaticTimeTracker() {doCleanupLogOnFirstRun();}
593
 
        ~MemStaticTimeTracker() {staticDeinitTime = true; dumpLeakReport();}
594
 
};
595
 
static  MemStaticTimeTracker    mstt;
596
 
 
597
 
// ---------------------------------------------------------------------------------------------------------------------------------
598
 
// -DOC- Flags & options -- Call these routines to enable/disable the following options
599
 
// ---------------------------------------------------------------------------------------------------------------------------------
600
 
 
601
 
bool    &m_alwaysValidateAll()
602
 
{
603
 
        // Force a validation of all allocation units each time we enter this software
604
 
        return alwaysValidateAll;
605
 
}
606
 
 
607
 
// ---------------------------------------------------------------------------------------------------------------------------------
608
 
 
609
 
bool    &m_alwaysLogAll()
610
 
{
611
 
        // Force a log of every allocation & deallocation into memory.log
612
 
        return alwaysLogAll;
613
 
}
614
 
 
615
 
// ---------------------------------------------------------------------------------------------------------------------------------
616
 
 
617
 
bool    &m_alwaysWipeAll()
618
 
{
619
 
        // Force this software to always wipe memory with a pattern when it is being allocated/dallocated
620
 
        return alwaysWipeAll;
621
 
}
622
 
 
623
 
// ---------------------------------------------------------------------------------------------------------------------------------
624
 
 
625
 
bool    &m_randomeWipe()
626
 
{
627
 
        // Force this software to use a random pattern when wiping memory -- good for stress testing
628
 
        return randomWipe;
629
 
}
630
 
 
631
 
// ---------------------------------------------------------------------------------------------------------------------------------
632
 
// -DOC- Simply call this routine with the address of an allocated block of RAM, to cause it to force a breakpoint when it is
633
 
// reallocated.
634
 
// ---------------------------------------------------------------------------------------------------------------------------------
635
 
 
636
 
bool    &m_breakOnRealloc(void *reportedAddress)
637
 
{
638
 
        // Locate the existing allocation unit
639
 
 
640
 
        sAllocUnit      *au = findAllocUnit(reportedAddress);
641
 
 
642
 
        // If you hit this assert, you tried to set a breakpoint on reallocation for an address that doesn't exist. Interrogate the
643
 
        // stack frame or the variable 'au' to see which allocation this is.
644
 
        m_assert(au != NULL);
645
 
 
646
 
        // If you hit this assert, you tried to set a breakpoint on reallocation for an address that wasn't allocated in a way that
647
 
        // is compatible with reallocation.
648
 
        m_assert(au->allocationType == m_alloc_malloc ||
649
 
                 au->allocationType == m_alloc_calloc ||
650
 
                 au->allocationType == m_alloc_realloc);
651
 
 
652
 
        return au->breakOnRealloc;
653
 
}
654
 
 
655
 
// ---------------------------------------------------------------------------------------------------------------------------------
656
 
// -DOC- Simply call this routine with the address of an allocated block of RAM, to cause it to force a breakpoint when it is
657
 
// deallocated.
658
 
// ---------------------------------------------------------------------------------------------------------------------------------
659
 
 
660
 
bool    &m_breakOnDealloc(void *reportedAddress)
661
 
{
662
 
        // Locate the existing allocation unit
663
 
 
664
 
        sAllocUnit      *au = findAllocUnit(reportedAddress);
665
 
 
666
 
        // If you hit this assert, you tried to set a breakpoint on deallocation for an address that doesn't exist. Interrogate the
667
 
        // stack frame or the variable 'au' to see which allocation this is.
668
 
        m_assert(au != NULL);
669
 
 
670
 
        return au->breakOnDealloc;
671
 
}
672
 
 
673
 
// ---------------------------------------------------------------------------------------------------------------------------------
674
 
// -DOC- When tracking down a difficult bug, use this routine to force a breakpoint on a specific allocation count
675
 
// ---------------------------------------------------------------------------------------------------------------------------------
676
 
 
677
 
void    m_breakOnAllocation(unsigned int count)
678
 
{
679
 
        breakOnAllocationCount = count;
680
 
}
681
 
 
682
 
// ---------------------------------------------------------------------------------------------------------------------------------
683
 
// Used by the macros
684
 
// ---------------------------------------------------------------------------------------------------------------------------------
685
 
 
686
 
void    m_setOwner(const char *file, const unsigned int line, const char *func)
687
 
{
688
 
        // You're probably wondering about this...
689
 
        //
690
 
        // It's important for this memory manager to primarily work with global new/delete in their original forms (i.e. with
691
 
        // no extra parameters.) In order to do this, we use macros that call this function prior to operators new & delete. This
692
 
        // is fine... usually. Here's what actually happens when you use this macro to delete an object:
693
 
        //
694
 
        // m_setOwner(__FILE__, __LINE__, __FUNCTION__) --> object::~object() --> delete
695
 
        //
696
 
        // Note that the compiler inserts a call to the object's destructor just prior to calling our overridden operator delete.
697
 
        // But what happens when we delete an object whose destructor deletes another object, whose desctuctor deletes another
698
 
        // object? Here's a diagram (indentation follows stack depth):
699
 
        //
700
 
        // m_setOwner(...) -> ~obj1()                          // original call to delete obj1
701
 
        //     m_setOwner(...) -> ~obj2()                      // obj1's destructor deletes obj2
702
 
        //         m_setOwner(...) -> ~obj3()                  // obj2's destructor deletes obj3
703
 
        //             ...                                     // obj3's destructor just does some stuff
704
 
        //         delete                                      // back in obj2's destructor, we call delete
705
 
        //     delete                                          // back in obj1's destructor, we call delete
706
 
        // delete                                              // back to our original call, we call delete
707
 
        //
708
 
        // Because m_setOwner() just sets up some static variables (below) it's important that each call to m_setOwner() and
709
 
        // successive calls to new/delete alternate. However, in this case, three calls to m_setOwner() happen in succession
710
 
        // followed by three calls to delete in succession (with a few calls to destructors mixed in for fun.) This means that
711
 
        // only the final call to delete (in this chain of events) will have the proper reporting, and the first two in the chain
712
 
        // will not have ANY owner-reporting information. The deletes will still work fine, we just won't know who called us.
713
 
        //
714
 
        // "Then build a stack, my friend!" you might think... but it's a very common thing that people will be working with third-
715
 
        // party libraries (including MFC under Windows) which is not compiled with this memory manager's macros. In those cases,
716
 
        // m_setOwner() is never called, and rightfully should not have the proper trace-back information. So if one of the
717
 
        // destructors in the chain ends up being a call to a delete from a non-mmgr-compiled library, the stack will get confused.
718
 
        //
719
 
        // I've been unable to find a solution to this problem, but at least we can detect it and report the data before we
720
 
        // lose it. That's what this is all about. It makes it somewhat confusing to read in the logs, but at least ALL the
721
 
        // information is present...
722
 
        //
723
 
        // There's a caveat here... The compiler is not required to call operator delete if the value being deleted is NULL.
724
 
        // In this case, any call to delete with a NULL will sill call m_setOwner(), which will make m_setOwner() think that
725
 
        // there is a destructor chain becuase we setup the variables, but nothing gets called to clear them. Because of this
726
 
        // we report a "Possible destructor chain".
727
 
        //
728
 
        // Thanks to J. Woznack (from Kodiak Interactive Software Studios -- www.kodiakgames.com) for pointing this out.
729
 
 
730
 
        if (sourceLine && alwaysLogAll)
731
 
        {
732
 
                log("[I] NOTE! Possible destructor chain: previous owner is %s", ownerString(sourceFile, sourceLine, sourceFunc));
733
 
        }
734
 
 
735
 
        // Okay... save this stuff off so we can keep track of the caller
736
 
 
737
 
        sourceFile = file;
738
 
        sourceLine = line;
739
 
        sourceFunc = func;
740
 
}
741
 
 
742
 
// ---------------------------------------------------------------------------------------------------------------------------------
743
 
 
744
 
static  void    resetGlobals()
745
 
{
746
 
        sourceFile = "??";
747
 
        sourceLine = 0;
748
 
        sourceFunc = "??";
749
 
}
750
 
 
751
 
// ---------------------------------------------------------------------------------------------------------------------------------
752
 
// Global new/new[]
753
 
//
754
 
// These are the standard new/new[] operators. They are merely interface functions that operate like normal new/new[], but use our
755
 
// memory tracking routines.
756
 
// ---------------------------------------------------------------------------------------------------------------------------------
757
 
 
758
 
void    *operator new(size_t reportedSize)
759
 
{
760
 
        #ifdef TEST_MEMORY_MANAGER
761
 
        log("[D] ENTER: new");
762
 
        #endif
763
 
 
764
 
        // Save these off...
765
 
 
766
 
        const   char            *file = sourceFile;
767
 
        const   unsigned int    line = sourceLine;
768
 
        const   char            *func = sourceFunc;
769
 
 
770
 
        // ANSI says: allocation requests of 0 bytes will still return a valid value
771
 
 
772
 
        if (reportedSize == 0) reportedSize = 1;
773
 
 
774
 
        // ANSI says: loop continuously because the error handler could possibly free up some memory
775
 
 
776
 
        for(;;)
777
 
        {
778
 
                // Try the allocation
779
 
 
780
 
                void    *ptr = m_allocator(file, line, func, m_alloc_new, reportedSize);
781
 
                if (ptr)
782
 
                {
783
 
                        #ifdef TEST_MEMORY_MANAGER
784
 
                        log("[D] EXIT : new");
785
 
                        #endif
786
 
                        return ptr;
787
 
                }
788
 
 
789
 
                // There isn't a way to determine the new handler, except through setting it. So we'll just set it to NULL, then
790
 
                // set it back again.
791
 
 
792
 
                new_handler     nh = set_new_handler(0);
793
 
                set_new_handler(nh);
794
 
 
795
 
                // If there is an error handler, call it
796
 
 
797
 
                if (nh)
798
 
                {
799
 
                        (*nh)();
800
 
                }
801
 
 
802
 
                // Otherwise, throw the exception
803
 
 
804
 
                else
805
 
                {
806
 
                        #ifdef TEST_MEMORY_MANAGER
807
 
                        log("[D] EXIT : new");
808
 
                        #endif
809
 
                        throw std::bad_alloc();
810
 
                }
811
 
        }
812
 
}
813
 
 
814
 
// ---------------------------------------------------------------------------------------------------------------------------------
815
 
 
816
 
void    *operator new[](size_t reportedSize)
817
 
{
818
 
        #ifdef TEST_MEMORY_MANAGER
819
 
        log("[D] ENTER: new[]");
820
 
        #endif
821
 
 
822
 
        // Save these off...
823
 
 
824
 
        const   char            *file = sourceFile;
825
 
        const   unsigned int    line = sourceLine;
826
 
        const   char            *func = sourceFunc;
827
 
 
828
 
        // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
829
 
 
830
 
        if (reportedSize == 0) reportedSize = 1;
831
 
 
832
 
        // ANSI says: loop continuously because the error handler could possibly free up some memory
833
 
 
834
 
        for(;;)
835
 
        {
836
 
                // Try the allocation
837
 
 
838
 
                void    *ptr = m_allocator(file, line, func, m_alloc_new_array, reportedSize);
839
 
                if (ptr)
840
 
                {
841
 
                        #ifdef TEST_MEMORY_MANAGER
842
 
                        log("[D] EXIT : new[]");
843
 
                        #endif
844
 
                        return ptr;
845
 
                }
846
 
 
847
 
                // There isn't a way to determine the new handler, except through setting it. So we'll just set it to NULL, then
848
 
                // set it back again.
849
 
 
850
 
                new_handler     nh = set_new_handler(0);
851
 
                set_new_handler(nh);
852
 
 
853
 
                // If there is an error handler, call it
854
 
 
855
 
                if (nh)
856
 
                {
857
 
                        (*nh)();
858
 
                }
859
 
 
860
 
                // Otherwise, throw the exception
861
 
 
862
 
                else
863
 
                {
864
 
                        #ifdef TEST_MEMORY_MANAGER
865
 
                        log("[D] EXIT : new[]");
866
 
                        #endif
867
 
                        throw std::bad_alloc();
868
 
                }
869
 
        }
870
 
}
871
 
 
872
 
// ---------------------------------------------------------------------------------------------------------------------------------
873
 
// Other global new/new[]
874
 
//
875
 
// These are the standard new/new[] operators as used by Microsoft's memory tracker. We don't want them interfering with our memory
876
 
// tracking efforts. Like the previous versions, these are merely interface functions that operate like normal new/new[], but use
877
 
// our memory tracking routines.
878
 
// ---------------------------------------------------------------------------------------------------------------------------------
879
 
 
880
 
void    *operator new(size_t reportedSize, const char *sourceFile, int sourceLine)
881
 
{
882
 
        #ifdef TEST_MEMORY_MANAGER
883
 
        log("[D] ENTER: new");
884
 
        #endif
885
 
 
886
 
        // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
887
 
 
888
 
        if (reportedSize == 0) reportedSize = 1;
889
 
 
890
 
        // ANSI says: loop continuously because the error handler could possibly free up some memory
891
 
 
892
 
        for(;;)
893
 
        {
894
 
                // Try the allocation
895
 
 
896
 
                void    *ptr = m_allocator(sourceFile, sourceLine, "??", m_alloc_new, reportedSize);
897
 
                if (ptr)
898
 
                {
899
 
                        #ifdef TEST_MEMORY_MANAGER
900
 
                        log("[D] EXIT : new");
901
 
                        #endif
902
 
                        return ptr;
903
 
                }
904
 
 
905
 
                // There isn't a way to determine the new handler, except through setting it. So we'll just set it to NULL, then
906
 
                // set it back again.
907
 
 
908
 
                new_handler     nh = set_new_handler(0);
909
 
                set_new_handler(nh);
910
 
 
911
 
                // If there is an error handler, call it
912
 
 
913
 
                if (nh)
914
 
                {
915
 
                        (*nh)();
916
 
                }
917
 
 
918
 
                // Otherwise, throw the exception
919
 
 
920
 
                else
921
 
                {
922
 
                        #ifdef TEST_MEMORY_MANAGER
923
 
                        log("[D] EXIT : new");
924
 
                        #endif
925
 
                        throw std::bad_alloc();
926
 
                }
927
 
        }
928
 
}
929
 
 
930
 
// ---------------------------------------------------------------------------------------------------------------------------------
931
 
 
932
 
void    *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine)
933
 
{
934
 
        #ifdef TEST_MEMORY_MANAGER
935
 
        log("[D] ENTER: new[]");
936
 
        #endif
937
 
 
938
 
        // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
939
 
 
940
 
        if (reportedSize == 0) reportedSize = 1;
941
 
 
942
 
        // ANSI says: loop continuously because the error handler could possibly free up some memory
943
 
 
944
 
        for(;;)
945
 
        {
946
 
                // Try the allocation
947
 
 
948
 
                void    *ptr = m_allocator(sourceFile, sourceLine, "??", m_alloc_new_array, reportedSize);
949
 
                if (ptr)
950
 
                {
951
 
                        #ifdef TEST_MEMORY_MANAGER
952
 
                        log("[D] EXIT : new[]");
953
 
                        #endif
954
 
                        return ptr;
955
 
                }
956
 
 
957
 
                // There isn't a way to determine the new handler, except through setting it. So we'll just set it to NULL, then
958
 
                // set it back again.
959
 
 
960
 
                new_handler     nh = set_new_handler(0);
961
 
                set_new_handler(nh);
962
 
 
963
 
                // If there is an error handler, call it
964
 
 
965
 
                if (nh)
966
 
                {
967
 
                        (*nh)();
968
 
                }
969
 
 
970
 
                // Otherwise, throw the exception
971
 
 
972
 
                else
973
 
                {
974
 
                        #ifdef TEST_MEMORY_MANAGER
975
 
                        log("[D] EXIT : new[]");
976
 
                        #endif
977
 
                        throw std::bad_alloc();
978
 
                }
979
 
        }
980
 
}
981
 
 
982
 
// ---------------------------------------------------------------------------------------------------------------------------------
983
 
// Global delete/delete[]
984
 
//
985
 
// These are the standard delete/delete[] operators. They are merely interface functions that operate like normal delete/delete[],
986
 
// but use our memory tracking routines.
987
 
// ---------------------------------------------------------------------------------------------------------------------------------
988
 
 
989
 
void    operator delete(void *reportedAddress)
990
 
{
991
 
        #ifdef TEST_MEMORY_MANAGER
992
 
        log("[D] ENTER: delete");
993
 
        #endif
994
 
 
995
 
        // ANSI says: delete & delete[] allow NULL pointers (they do nothing)
996
 
 
997
 
        if (reportedAddress) m_deallocator(sourceFile, sourceLine, sourceFunc, m_alloc_delete, reportedAddress);
998
 
        else if (alwaysLogAll) log("[-] ----- %8s of NULL                      by %s", allocationTypes[m_alloc_delete], ownerString(sourceFile, sourceLine, sourceFunc));
999
 
 
1000
 
        // Resetting the globals insures that if at some later time, somebody calls our memory manager from an unknown
1001
 
        // source (i.e. they didn't include our H file) then we won't think it was the last allocation.
1002
 
 
1003
 
        resetGlobals();
1004
 
 
1005
 
        #ifdef TEST_MEMORY_MANAGER
1006
 
        log("[D] EXIT : delete");
1007
 
        #endif
1008
 
}
1009
 
 
1010
 
// ---------------------------------------------------------------------------------------------------------------------------------
1011
 
 
1012
 
void    operator delete[](void *reportedAddress)
1013
 
{
1014
 
        #ifdef TEST_MEMORY_MANAGER
1015
 
        log("[D] ENTER: delete[]");
1016
 
        #endif
1017
 
 
1018
 
        // ANSI says: delete & delete[] allow NULL pointers (they do nothing)
1019
 
 
1020
 
        if (reportedAddress) m_deallocator(sourceFile, sourceLine, sourceFunc, m_alloc_delete_array, reportedAddress);
1021
 
        else if (alwaysLogAll)
1022
 
                log("[-] ----- %8s of NULL                      by %s", allocationTypes[m_alloc_delete_array], ownerString(sourceFile, sourceLine, sourceFunc));
1023
 
 
1024
 
        // Resetting the globals insures that if at some later time, somebody calls our memory manager from an unknown
1025
 
        // source (i.e. they didn't include our H file) then we won't think it was the last allocation.
1026
 
 
1027
 
        resetGlobals();
1028
 
 
1029
 
        #ifdef TEST_MEMORY_MANAGER
1030
 
        log("[D] EXIT : delete[]");
1031
 
        #endif
1032
 
}
1033
 
 
1034
 
// ---------------------------------------------------------------------------------------------------------------------------------
1035
 
// Allocate memory and track it
1036
 
// ---------------------------------------------------------------------------------------------------------------------------------
1037
 
 
1038
 
void    *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int allocationType, const size_t reportedSize)
1039
 
{
1040
 
        try
1041
 
        {
1042
 
                #ifdef TEST_MEMORY_MANAGER
1043
 
                log("[D] ENTER: m_allocator()");
1044
 
                #endif
1045
 
 
1046
 
                // Increase our allocation count
1047
 
 
1048
 
                currentAllocationCount++;
1049
 
 
1050
 
                // Log the request
1051
 
 
1052
 
                if (alwaysLogAll) log("[+] %05d %8s of size 0x%08X(%08d) by %s", currentAllocationCount, allocationTypes[allocationType], reportedSize, reportedSize, ownerString(sourceFile, sourceLine, sourceFunc));
1053
 
 
1054
 
                // If you hit this assert, you requested a breakpoint on a specific allocation count
1055
 
                m_assert(currentAllocationCount != breakOnAllocationCount);
1056
 
 
1057
 
                // If necessary, grow the reservoir of unused allocation units
1058
 
 
1059
 
                if (!reservoir)
1060
 
                {
1061
 
                        // Allocate 256 reservoir elements
1062
 
 
1063
 
                        reservoir = (sAllocUnit *) malloc(sizeof(sAllocUnit) * 256);
1064
 
 
1065
 
                        // If you hit this assert, then the memory manager failed to allocate internal memory for tracking the
1066
 
                        // allocations
1067
 
                        m_assert(reservoir != NULL);
1068
 
 
1069
 
                        // Danger Will Robinson!
1070
 
 
1071
 
                        if (reservoir == NULL) throw "Unable to allocate RAM for internal memory tracking data";
1072
 
 
1073
 
                        // Build a linked-list of the elements in our reservoir
1074
 
 
1075
 
                        memset(reservoir, 0, sizeof(sAllocUnit) * 256);
1076
 
                        for (unsigned int i = 0; i < 256 - 1; i++)
1077
 
                        {
1078
 
                                reservoir[i].next = &reservoir[i+1];
1079
 
                        }
1080
 
 
1081
 
                        // Add this address to our reservoirBuffer so we can free it later
1082
 
 
1083
 
                        sAllocUnit      **temp = (sAllocUnit **) realloc(reservoirBuffer, (reservoirBufferSize + 1) * sizeof(sAllocUnit *));
1084
 
                        m_assert(temp);
1085
 
                        if (temp)
1086
 
                        {
1087
 
                                reservoirBuffer = temp;
1088
 
                                reservoirBuffer[reservoirBufferSize++] = reservoir;
1089
 
                        }
1090
 
                }
1091
 
 
1092
 
                // Logical flow says this should never happen...
1093
 
                m_assert(reservoir != NULL);
1094
 
 
1095
 
                // Grab a new allocaton unit from the front of the reservoir
1096
 
 
1097
 
                sAllocUnit      *au = reservoir;
1098
 
                reservoir = au->next;
1099
 
 
1100
 
                // Populate it with some real data
1101
 
 
1102
 
                memset(au, 0, sizeof(sAllocUnit));
1103
 
                au->actualSize        = calculateActualSize(reportedSize);
1104
 
                #ifdef RANDOM_FAILURE
1105
 
                double  a = rand();
1106
 
                double  b = RAND_MAX / 100.0 * RANDOM_FAILURE;
1107
 
                if (a > b)
1108
 
                {
1109
 
                        au->actualAddress = malloc(au->actualSize);
1110
 
                }
1111
 
                else
1112
 
                {
1113
 
                        log("[F] Random faiure");
1114
 
                        au->actualAddress = NULL;
1115
 
                }
1116
 
                #else
1117
 
                au->actualAddress     = malloc(au->actualSize);
1118
 
                #endif
1119
 
                au->reportedSize      = reportedSize;
1120
 
                au->reportedAddress   = calculateReportedAddress(au->actualAddress);
1121
 
                au->allocationType    = allocationType;
1122
 
                au->sourceLine        = sourceLine;
1123
 
                au->allocationNumber  = currentAllocationCount;
1124
 
                if (sourceFile) strncpy(au->sourceFile, sourceFileStripper(sourceFile), sizeof(au->sourceFile) - 1);
1125
 
                else            strcpy (au->sourceFile, "??");
1126
 
                if (sourceFunc) strncpy(au->sourceFunc, sourceFunc, sizeof(au->sourceFunc) - 1);
1127
 
                else            strcpy (au->sourceFunc, "??");
1128
 
 
1129
 
                // We don't want to assert with random failures, because we want the application to deal with them.
1130
 
 
1131
 
                #ifndef RANDOM_FAILURE
1132
 
                // If you hit this assert, then the requested allocation simply failed (you're out of memory.) Interrogate the
1133
 
                // variable 'au' or the stack frame to see what you were trying to do.
1134
 
                m_assert(au->actualAddress != NULL);
1135
 
                #endif
1136
 
 
1137
 
                if (au->actualAddress == NULL)
1138
 
                {
1139
 
                        throw "Request for allocation failed. Out of memory.";
1140
 
                }
1141
 
 
1142
 
                // If you hit this assert, then this allocation was made from a source that isn't setup to use this memory tracking
1143
 
                // software, use the stack frame to locate the source and include our H file.
1144
 
                m_assert(allocationType != m_alloc_unknown);
1145
 
 
1146
 
                // Insert the new allocation into the hash table
1147
 
 
1148
 
                unsigned int    hashIndex = (reinterpret_cast<unsigned int>(au->reportedAddress) >> 4) & (hashSize - 1);
1149
 
                if (hashTable[hashIndex]) hashTable[hashIndex]->prev = au;
1150
 
                au->next = hashTable[hashIndex];
1151
 
                au->prev = NULL;
1152
 
                hashTable[hashIndex] = au;
1153
 
 
1154
 
                // Account for the new allocatin unit in our stats
1155
 
 
1156
 
                stats.totalReportedMemory += static_cast<unsigned int>(au->reportedSize);
1157
 
                stats.totalActualMemory   += static_cast<unsigned int>(au->actualSize);
1158
 
                stats.totalAllocUnitCount++;
1159
 
                if (stats.totalReportedMemory > stats.peakReportedMemory) stats.peakReportedMemory = stats.totalReportedMemory;
1160
 
                if (stats.totalActualMemory   > stats.peakActualMemory)   stats.peakActualMemory   = stats.totalActualMemory;
1161
 
                if (stats.totalAllocUnitCount > stats.peakAllocUnitCount) stats.peakAllocUnitCount = stats.totalAllocUnitCount;
1162
 
                stats.accumulatedReportedMemory += static_cast<unsigned int>(au->reportedSize);
1163
 
                stats.accumulatedActualMemory += static_cast<unsigned int>(au->actualSize);
1164
 
                stats.accumulatedAllocUnitCount++;
1165
 
 
1166
 
                // Prepare the allocation unit for use (wipe it with recognizable garbage)
1167
 
 
1168
 
                wipeWithPattern(au, unusedPattern);
1169
 
 
1170
 
                // calloc() expects the reported memory address range to be filled with 0's
1171
 
 
1172
 
                if (allocationType == m_alloc_calloc)
1173
 
                {
1174
 
                        memset(au->reportedAddress, 0, au->reportedSize);
1175
 
                }
1176
 
 
1177
 
                // Validate every single allocated unit in memory
1178
 
 
1179
 
                if (alwaysValidateAll) m_validateAllAllocUnits();
1180
 
 
1181
 
                // Log the result
1182
 
 
1183
 
                if (alwaysLogAll) log("[+] ---->             addr 0x%08X", reinterpret_cast<unsigned int>(au->reportedAddress));
1184
 
 
1185
 
                // Resetting the globals insures that if at some later time, somebody calls our memory manager from an unknown
1186
 
                // source (i.e. they didn't include our H file) then we won't think it was the last allocation.
1187
 
 
1188
 
                resetGlobals();
1189
 
 
1190
 
                // Return the (reported) address of the new allocation unit
1191
 
 
1192
 
                #ifdef TEST_MEMORY_MANAGER
1193
 
                log("[D] EXIT : m_allocator()");
1194
 
                #endif
1195
 
 
1196
 
                return au->reportedAddress;
1197
 
        }
1198
 
        catch(const char *err)
1199
 
        {
1200
 
                // Deal with the errors
1201
 
 
1202
 
                log("[!] %s", err);
1203
 
                resetGlobals();
1204
 
 
1205
 
                #ifdef TEST_MEMORY_MANAGER
1206
 
                log("[D] EXIT : m_allocator()");
1207
 
                #endif
1208
 
 
1209
 
                return NULL;
1210
 
        }
1211
 
}
1212
 
 
1213
 
// ---------------------------------------------------------------------------------------------------------------------------------
1214
 
// Reallocate memory and track it
1215
 
// ---------------------------------------------------------------------------------------------------------------------------------
1216
 
 
1217
 
void    *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress)
1218
 
{
1219
 
        try
1220
 
        {
1221
 
                #ifdef TEST_MEMORY_MANAGER
1222
 
                log("[D] ENTER: m_reallocator()");
1223
 
                #endif
1224
 
 
1225
 
                // Calling realloc with a NULL should force same operations as a malloc
1226
 
 
1227
 
                if (!reportedAddress)
1228
 
                {
1229
 
                        return m_allocator(sourceFile, sourceLine, sourceFunc, reallocationType, reportedSize);
1230
 
                }
1231
 
 
1232
 
                // Increase our allocation count
1233
 
 
1234
 
                currentAllocationCount++;
1235
 
 
1236
 
                // If you hit this assert, you requested a breakpoint on a specific allocation count
1237
 
                m_assert(currentAllocationCount != breakOnAllocationCount);
1238
 
 
1239
 
                // Log the request
1240
 
 
1241
 
                if (alwaysLogAll) log("[~] %05d %8s of size 0x%08X(%08d) by %s", currentAllocationCount, allocationTypes[reallocationType], reportedSize, reportedSize, ownerString(sourceFile, sourceLine, sourceFunc));
1242
 
 
1243
 
                // Locate the existing allocation unit
1244
 
 
1245
 
                sAllocUnit      *au = findAllocUnit(reportedAddress);
1246
 
 
1247
 
                // If you hit this assert, you tried to reallocate RAM that wasn't allocated by this memory manager.
1248
 
                m_assert(au != NULL);
1249
 
                if (au == NULL) throw "Request to reallocate RAM that was never allocated";
1250
 
 
1251
 
                // If you hit this assert, then the allocation unit that is about to be reallocated is damaged. But you probably
1252
 
                // already know that from a previous assert you should have seen in validateAllocUnit() :)
1253
 
                m_assert(m_validateAllocUnit(au));
1254
 
 
1255
 
                // If you hit this assert, then this reallocation was made from a source that isn't setup to use this memory
1256
 
                // tracking software, use the stack frame to locate the source and include our H file.
1257
 
                m_assert(reallocationType != m_alloc_unknown);
1258
 
 
1259
 
                // If you hit this assert, you were trying to reallocate RAM that was not allocated in a way that is compatible with
1260
 
                // realloc. In other words, you have a allocation/reallocation mismatch.
1261
 
                m_assert(au->allocationType == m_alloc_malloc ||
1262
 
                         au->allocationType == m_alloc_calloc ||
1263
 
                         au->allocationType == m_alloc_realloc);
1264
 
 
1265
 
                // If you hit this assert, then the "break on realloc" flag for this allocation unit is set (and will continue to be
1266
 
                // set until you specifically shut it off. Interrogate the 'au' variable to determine information about this
1267
 
                // allocation unit.
1268
 
                m_assert(au->breakOnRealloc == false);
1269
 
 
1270
 
                // Keep track of the original size
1271
 
 
1272
 
                unsigned int    originalReportedSize = static_cast<unsigned int>(au->reportedSize);
1273
 
 
1274
 
                if (alwaysLogAll) log("[~] ---->             from 0x%08X(%08d)", originalReportedSize, originalReportedSize);
1275
 
 
1276
 
                // Do the reallocation
1277
 
 
1278
 
                void    *oldReportedAddress = reportedAddress;
1279
 
                size_t  newActualSize = calculateActualSize(reportedSize);
1280
 
                void    *newActualAddress = NULL;
1281
 
                #ifdef RANDOM_FAILURE
1282
 
                double  a = rand();
1283
 
                double  b = RAND_MAX / 100.0 * RANDOM_FAILURE;
1284
 
                if (a > b)
1285
 
                {
1286
 
                        newActualAddress = realloc(au->actualAddress, newActualSize);
1287
 
                }
1288
 
                else
1289
 
                {
1290
 
                        log("[F] Random faiure");
1291
 
                }
1292
 
                #else
1293
 
                newActualAddress = realloc(au->actualAddress, newActualSize);
1294
 
                #endif
1295
 
 
1296
 
                // We don't want to assert with random failures, because we want the application to deal with them.
1297
 
 
1298
 
                #ifndef RANDOM_FAILURE
1299
 
                // If you hit this assert, then the requested allocation simply failed (you're out of memory) Interrogate the
1300
 
                // variable 'au' to see the original allocation. You can also query 'newActualSize' to see the amount of memory
1301
 
                // trying to be allocated. Finally, you can query 'reportedSize' to see how much memory was requested by the caller.
1302
 
                m_assert(newActualAddress);
1303
 
                #endif
1304
 
 
1305
 
                if (!newActualAddress) throw "Request for reallocation failed. Out of memory.";
1306
 
 
1307
 
                // Remove this allocation from our stats (we'll add the new reallocation again later)
1308
 
 
1309
 
                stats.totalReportedMemory -= static_cast<unsigned int>(au->reportedSize);
1310
 
                stats.totalActualMemory   -= static_cast<unsigned int>(au->actualSize);
1311
 
 
1312
 
                // Update the allocation with the new information
1313
 
 
1314
 
                au->actualSize        = newActualSize;
1315
 
                au->actualAddress     = newActualAddress;
1316
 
                au->reportedSize      = calculateReportedSize(newActualSize);
1317
 
                au->reportedAddress   = calculateReportedAddress(newActualAddress);
1318
 
                au->allocationType    = reallocationType;
1319
 
                au->sourceLine        = sourceLine;
1320
 
                au->allocationNumber  = currentAllocationCount;
1321
 
                if (sourceFile) strncpy(au->sourceFile, sourceFileStripper(sourceFile), sizeof(au->sourceFile) - 1);
1322
 
                else            strcpy (au->sourceFile, "??");
1323
 
                if (sourceFunc) strncpy(au->sourceFunc, sourceFunc, sizeof(au->sourceFunc) - 1);
1324
 
                else            strcpy (au->sourceFunc, "??");
1325
 
 
1326
 
                // The reallocation may cause the address to change, so we should relocate our allocation unit within the hash table
1327
 
 
1328
 
                unsigned int    hashIndex = static_cast<unsigned int>(-1);
1329
 
                if (oldReportedAddress != au->reportedAddress)
1330
 
                {
1331
 
                        // Remove this allocation unit from the hash table
1332
 
 
1333
 
                        {
1334
 
                                unsigned int    hashIndex = (reinterpret_cast<unsigned int>(oldReportedAddress) >> 4) & (hashSize - 1);
1335
 
                                if (hashTable[hashIndex] == au)
1336
 
                                {
1337
 
                                        hashTable[hashIndex] = hashTable[hashIndex]->next;
1338
 
                                }
1339
 
                                else
1340
 
                                {
1341
 
                                        if (au->prev)   au->prev->next = au->next;
1342
 
                                        if (au->next)   au->next->prev = au->prev;
1343
 
                                }
1344
 
                        }
1345
 
 
1346
 
                        // Re-insert it back into the hash table
1347
 
 
1348
 
                        hashIndex = (reinterpret_cast<unsigned int>(au->reportedAddress) >> 4) & (hashSize - 1);
1349
 
                        if (hashTable[hashIndex]) hashTable[hashIndex]->prev = au;
1350
 
                        au->next = hashTable[hashIndex];
1351
 
                        au->prev = NULL;
1352
 
                        hashTable[hashIndex] = au;
1353
 
                }
1354
 
 
1355
 
                // Account for the new allocatin unit in our stats
1356
 
 
1357
 
                stats.totalReportedMemory += static_cast<unsigned int>(au->reportedSize);
1358
 
                stats.totalActualMemory   += static_cast<unsigned int>(au->actualSize);
1359
 
                if (stats.totalReportedMemory > stats.peakReportedMemory) stats.peakReportedMemory = stats.totalReportedMemory;
1360
 
                if (stats.totalActualMemory   > stats.peakActualMemory)   stats.peakActualMemory   = stats.totalActualMemory;
1361
 
                int     deltaReportedSize = static_cast<int>(reportedSize - originalReportedSize);
1362
 
                if (deltaReportedSize > 0)
1363
 
                {
1364
 
                        stats.accumulatedReportedMemory += deltaReportedSize;
1365
 
                        stats.accumulatedActualMemory += deltaReportedSize;
1366
 
                }
1367
 
 
1368
 
                // Prepare the allocation unit for use (wipe it with recognizable garbage)
1369
 
 
1370
 
                wipeWithPattern(au, unusedPattern, originalReportedSize);
1371
 
 
1372
 
                // If you hit this assert, then something went wrong, because the allocation unit was properly validated PRIOR to
1373
 
                // the reallocation. This should not happen.
1374
 
                m_assert(m_validateAllocUnit(au));
1375
 
 
1376
 
                // Validate every single allocated unit in memory
1377
 
 
1378
 
                if (alwaysValidateAll) m_validateAllAllocUnits();
1379
 
 
1380
 
                // Log the result
1381
 
 
1382
 
                if (alwaysLogAll) log("[~] ---->             addr 0x%08X", reinterpret_cast<unsigned int>(au->reportedAddress));
1383
 
 
1384
 
                // Resetting the globals insures that if at some later time, somebody calls our memory manager from an unknown
1385
 
                // source (i.e. they didn't include our H file) then we won't think it was the last allocation.
1386
 
 
1387
 
                resetGlobals();
1388
 
 
1389
 
                // Return the (reported) address of the new allocation unit
1390
 
 
1391
 
                #ifdef TEST_MEMORY_MANAGER
1392
 
                log("[D] EXIT : m_reallocator()");
1393
 
                #endif
1394
 
 
1395
 
                return au->reportedAddress;
1396
 
        }
1397
 
        catch(const char *err)
1398
 
        {
1399
 
                // Deal with the errors
1400
 
 
1401
 
                log("[!] %s", err);
1402
 
                resetGlobals();
1403
 
 
1404
 
                #ifdef TEST_MEMORY_MANAGER
1405
 
                log("[D] EXIT : m_reallocator()");
1406
 
                #endif
1407
 
 
1408
 
                return NULL;
1409
 
        }
1410
 
}
1411
 
 
1412
 
// ---------------------------------------------------------------------------------------------------------------------------------
1413
 
// Deallocate memory and track it
1414
 
// ---------------------------------------------------------------------------------------------------------------------------------
1415
 
 
1416
 
void    m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int deallocationType, const void *reportedAddress)
1417
 
{
1418
 
        try
1419
 
        {
1420
 
                #ifdef TEST_MEMORY_MANAGER
1421
 
                log("[D] ENTER: m_deallocator()");
1422
 
                #endif
1423
 
 
1424
 
                // Log the request
1425
 
 
1426
 
                if (alwaysLogAll) log("[-] ----- %8s of addr 0x%08X           by %s", allocationTypes[deallocationType], reinterpret_cast<unsigned int>(const_cast<void *>(reportedAddress)), ownerString(sourceFile, sourceLine, sourceFunc));
1427
 
 
1428
 
                // We should only ever get here with a null pointer if they try to do so with a call to free() (delete[] and delete will
1429
 
                // both bail before they get here.) So, since ANSI allows free(NULL), we'll not bother trying to actually free the allocated
1430
 
                // memory or track it any further.
1431
 
 
1432
 
                if (reportedAddress)
1433
 
                {
1434
 
                        // Go get the allocation unit
1435
 
 
1436
 
                        sAllocUnit      *au = findAllocUnit(reportedAddress);
1437
 
 
1438
 
                        // If you hit this assert, you tried to deallocate RAM that wasn't allocated by this memory manager.
1439
 
                        m_assert(au != NULL);
1440
 
                        if (au == NULL) throw "Request to deallocate RAM that was never allocated";
1441
 
 
1442
 
                        // If you hit this assert, then the allocation unit that is about to be deallocated is damaged. But you probably
1443
 
                        // already know that from a previous assert you should have seen in validateAllocUnit() :)
1444
 
                        m_assert(m_validateAllocUnit(au));
1445
 
 
1446
 
                        // If you hit this assert, then this deallocation was made from a source that isn't setup to use this memory
1447
 
                        // tracking software, use the stack frame to locate the source and include our H file.
1448
 
                        m_assert(deallocationType != m_alloc_unknown);
1449
 
 
1450
 
                        // If you hit this assert, you were trying to deallocate RAM that was not allocated in a way that is compatible with
1451
 
                        // the deallocation method requested. In other words, you have a allocation/deallocation mismatch.
1452
 
                        m_assert((deallocationType == m_alloc_delete       && au->allocationType == m_alloc_new      ) ||
1453
 
                                (deallocationType == m_alloc_delete_array && au->allocationType == m_alloc_new_array) ||
1454
 
                                (deallocationType == m_alloc_free         && au->allocationType == m_alloc_malloc   ) ||
1455
 
                                (deallocationType == m_alloc_free         && au->allocationType == m_alloc_calloc   ) ||
1456
 
                                (deallocationType == m_alloc_free         && au->allocationType == m_alloc_realloc  ) ||
1457
 
                                (deallocationType == m_alloc_unknown                                                ) );
1458
 
 
1459
 
                        // If you hit this assert, then the "break on dealloc" flag for this allocation unit is set. Interrogate the 'au'
1460
 
                        // variable to determine information about this allocation unit.
1461
 
                        m_assert(au->breakOnDealloc == false);
1462
 
 
1463
 
                        // Wipe the deallocated RAM with a new pattern. This doen't actually do us much good in debug mode under WIN32,
1464
 
                        // because Microsoft's memory debugging & tracking utilities will wipe it right after we do. Oh well.
1465
 
 
1466
 
                        wipeWithPattern(au, releasedPattern);
1467
 
 
1468
 
                        // Do the deallocation
1469
 
 
1470
 
                        free(au->actualAddress);
1471
 
 
1472
 
                        // Remove this allocation unit from the hash table
1473
 
 
1474
 
                        unsigned int    hashIndex = (reinterpret_cast<unsigned int>(au->reportedAddress) >> 4) & (hashSize - 1);
1475
 
                        if (hashTable[hashIndex] == au)
1476
 
                        {
1477
 
                                hashTable[hashIndex] = au->next;
1478
 
                        }
1479
 
                        else
1480
 
                        {
1481
 
                                if (au->prev)   au->prev->next = au->next;
1482
 
                                if (au->next)   au->next->prev = au->prev;
1483
 
                        }
1484
 
 
1485
 
                        // Remove this allocation from our stats
1486
 
 
1487
 
                        stats.totalReportedMemory -= static_cast<unsigned int>(au->reportedSize);
1488
 
                        stats.totalActualMemory   -= static_cast<unsigned int>(au->actualSize);
1489
 
                        stats.totalAllocUnitCount--;
1490
 
 
1491
 
                        // Add this allocation unit to the front of our reservoir of unused allocation units
1492
 
 
1493
 
                        memset(au, 0, sizeof(sAllocUnit));
1494
 
                        au->next = reservoir;
1495
 
                        reservoir = au;
1496
 
                }
1497
 
 
1498
 
                // Resetting the globals insures that if at some later time, somebody calls our memory manager from an unknown
1499
 
                // source (i.e. they didn't include our H file) then we won't think it was the last allocation.
1500
 
 
1501
 
                resetGlobals();
1502
 
 
1503
 
                // Validate every single allocated unit in memory
1504
 
 
1505
 
                if (alwaysValidateAll) m_validateAllAllocUnits();
1506
 
 
1507
 
                // If we're in the midst of static deinitialization time, track any pending memory leaks
1508
 
 
1509
 
                if (staticDeinitTime) dumpLeakReport();
1510
 
        }
1511
 
        catch(const char *err)
1512
 
        {
1513
 
                // Deal with errors
1514
 
 
1515
 
                log("[!] %s", err);
1516
 
                resetGlobals();
1517
 
        }
1518
 
 
1519
 
        #ifdef TEST_MEMORY_MANAGER
1520
 
        log("[D] EXIT : m_deallocator()");
1521
 
        #endif
1522
 
}
1523
 
 
1524
 
// ---------------------------------------------------------------------------------------------------------------------------------
1525
 
// -DOC- The following utilitarian allow you to become proactive in tracking your own memory, or help you narrow in on those tough
1526
 
// bugs.
1527
 
// ---------------------------------------------------------------------------------------------------------------------------------
1528
 
 
1529
 
bool    m_validateAddress(const void *reportedAddress)
1530
 
{
1531
 
        // Just see if the address exists in our allocation routines
1532
 
 
1533
 
        return findAllocUnit(reportedAddress) != NULL;
1534
 
}
1535
 
 
1536
 
// ---------------------------------------------------------------------------------------------------------------------------------
1537
 
 
1538
 
bool    m_validateAllocUnit(const sAllocUnit *allocUnit)
1539
 
{
1540
 
        // Make sure the padding is untouched
1541
 
 
1542
 
        long    *pre = reinterpret_cast<long *>(allocUnit->actualAddress);
1543
 
        long    *post = reinterpret_cast<long *>((char *)allocUnit->actualAddress + allocUnit->actualSize - paddingSize * sizeof(long));
1544
 
        bool    errorFlag = false;
1545
 
        for (unsigned int i = 0; i < paddingSize; i++, pre++, post++)
1546
 
        {
1547
 
                if (*pre != (long) prefixPattern)
1548
 
                {
1549
 
                        log("[!] A memory allocation unit was corrupt because of an underrun:");
1550
 
                        m_dumpAllocUnit(allocUnit, "  ");
1551
 
                        errorFlag = true;
1552
 
                }
1553
 
 
1554
 
                // If you hit this assert, then you should know that this allocation unit has been damaged. Something (possibly the
1555
 
                // owner?) has underrun the allocation unit (modified a few bytes prior to the start). You can interrogate the
1556
 
                // variable 'allocUnit' to see statistics and information about this damaged allocation unit.
1557
 
                m_assert(*pre == static_cast<long>(prefixPattern));
1558
 
 
1559
 
                if (*post != static_cast<long>(postfixPattern))
1560
 
                {
1561
 
                        log("[!] A memory allocation unit was corrupt because of an overrun:");
1562
 
                        m_dumpAllocUnit(allocUnit, "  ");
1563
 
                        errorFlag = true;
1564
 
                }
1565
 
 
1566
 
                // If you hit this assert, then you should know that this allocation unit has been damaged. Something (possibly the
1567
 
                // owner?) has overrun the allocation unit (modified a few bytes after the end). You can interrogate the variable
1568
 
                // 'allocUnit' to see statistics and information about this damaged allocation unit.
1569
 
                m_assert(*post == static_cast<long>(postfixPattern));
1570
 
        }
1571
 
 
1572
 
        // Return the error status (we invert it, because a return of 'false' means error)
1573
 
 
1574
 
        return !errorFlag;
1575
 
}
1576
 
 
1577
 
// ---------------------------------------------------------------------------------------------------------------------------------
1578
 
 
1579
 
bool    m_validateAllAllocUnits()
1580
 
{
1581
 
        // Just go through each allocation unit in the hash table and count the ones that have errors
1582
 
 
1583
 
        unsigned int    errors = 0;
1584
 
        unsigned int    allocCount = 0;
1585
 
        for (unsigned int i = 0; i < hashSize; i++)
1586
 
        {
1587
 
                sAllocUnit      *ptr = hashTable[i];
1588
 
                while(ptr)
1589
 
                {
1590
 
                        allocCount++;
1591
 
                        if (!m_validateAllocUnit(ptr)) errors++;
1592
 
                        ptr = ptr->next;
1593
 
                }
1594
 
        }
1595
 
 
1596
 
        // Test for hash-table correctness
1597
 
 
1598
 
        if (allocCount != stats.totalAllocUnitCount)
1599
 
        {
1600
 
                log("[!] Memory tracking hash table corrupt!");
1601
 
                errors++;
1602
 
        }
1603
 
 
1604
 
        // If you hit this assert, then the internal memory (hash table) used by this memory tracking software is damaged! The
1605
 
        // best way to track this down is to use the alwaysLogAll flag in conjunction with STRESS_TEST macro to narrow in on the
1606
 
        // offending code. After running the application with these settings (and hitting this assert again), interrogate the
1607
 
        // memory.log file to find the previous successful operation. The corruption will have occurred between that point and this
1608
 
        // assertion.
1609
 
        m_assert(allocCount == stats.totalAllocUnitCount);
1610
 
 
1611
 
        // If you hit this assert, then you've probably already been notified that there was a problem with a allocation unit in a
1612
 
        // prior call to validateAllocUnit(), but this assert is here just to make sure you know about it. :)
1613
 
        m_assert(errors == 0);
1614
 
 
1615
 
        // Log any errors
1616
 
 
1617
 
        if (errors) log("[!] While validting all allocation units, %d allocation unit(s) were found to have problems", errors);
1618
 
 
1619
 
        // Return the error status
1620
 
 
1621
 
        return errors != 0;
1622
 
}
1623
 
 
1624
 
// ---------------------------------------------------------------------------------------------------------------------------------
1625
 
// -DOC- Unused RAM calculation routines. Use these to determine how much of your RAM is unused (in bytes)
1626
 
// ---------------------------------------------------------------------------------------------------------------------------------
1627
 
 
1628
 
unsigned int    m_calcUnused(const sAllocUnit *allocUnit)
1629
 
{
1630
 
        const unsigned long     *ptr = reinterpret_cast<const unsigned long *>(allocUnit->reportedAddress);
1631
 
        unsigned int            count = 0;
1632
 
 
1633
 
        for (unsigned int i = 0; i < allocUnit->reportedSize; i += sizeof(long), ptr++)
1634
 
        {
1635
 
                if (*ptr == unusedPattern) count += sizeof(long);
1636
 
        }
1637
 
 
1638
 
        return count;
1639
 
}
1640
 
 
1641
 
// ---------------------------------------------------------------------------------------------------------------------------------
1642
 
 
1643
 
unsigned int    m_calcAllUnused()
1644
 
{
1645
 
        // Just go through each allocation unit in the hash table and count the unused RAM
1646
 
 
1647
 
        unsigned int    total = 0;
1648
 
        for (unsigned int i = 0; i < hashSize; i++)
1649
 
        {
1650
 
                sAllocUnit      *ptr = hashTable[i];
1651
 
                while(ptr)
1652
 
                {
1653
 
                        total += m_calcUnused(ptr);
1654
 
                        ptr = ptr->next;
1655
 
                }
1656
 
        }
1657
 
 
1658
 
        return total;
1659
 
}
1660
 
 
1661
 
// ---------------------------------------------------------------------------------------------------------------------------------
1662
 
// -DOC- The following functions are for logging and statistics reporting.
1663
 
// ---------------------------------------------------------------------------------------------------------------------------------
1664
 
 
1665
 
void    m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix)
1666
 
{
1667
 
        log("[I] %sAddress (reported): %010p",       prefix, allocUnit->reportedAddress);
1668
 
        log("[I] %sAddress (actual)  : %010p",       prefix, allocUnit->actualAddress);
1669
 
        log("[I] %sSize (reported)   : 0x%08X (%s)", prefix, static_cast<unsigned int>(allocUnit->reportedSize), memorySizeString(static_cast<unsigned int>(allocUnit->reportedSize)));
1670
 
        log("[I] %sSize (actual)     : 0x%08X (%s)", prefix, static_cast<unsigned int>(allocUnit->actualSize), memorySizeString(static_cast<unsigned int>(allocUnit->actualSize)));
1671
 
        log("[I] %sOwner             : %s(%d)::%s",  prefix, allocUnit->sourceFile, allocUnit->sourceLine, allocUnit->sourceFunc);
1672
 
        log("[I] %sAllocation type   : %s",          prefix, allocationTypes[allocUnit->allocationType]);
1673
 
        log("[I] %sAllocation number : %d",          prefix, allocUnit->allocationNumber);
1674
 
}
1675
 
 
1676
 
// ---------------------------------------------------------------------------------------------------------------------------------
1677
 
 
1678
 
void    m_dumpMemoryReport(const char *filename, const bool overwrite)
1679
 
{
1680
 
        // Open the report file
1681
 
 
1682
 
        FILE    *fp = NULL;
1683
 
        
1684
 
        if (overwrite)  fp = fopen(filename, "w+b");
1685
 
        else            fp = fopen(filename, "ab");
1686
 
 
1687
 
        // If you hit this assert, then the memory report generator is unable to log information to a file (can't open the file for
1688
 
        // some reason.)
1689
 
        m_assert(fp);
1690
 
        if (!fp) return;
1691
 
 
1692
 
        // Header
1693
 
 
1694
 
        static  char    timeString[25];
1695
 
        memset(timeString, 0, sizeof(timeString));
1696
 
        time_t  t = time(NULL);
1697
 
        struct  tm *tme = localtime(&t);
1698
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1699
 
        fprintf(fp, "|                                             Memory report for: %02d/%02d/%04d %02d:%02d:%02d                                               |\r\n", tme->tm_mon + 1, tme->tm_mday, tme->tm_year + 1900, tme->tm_hour, tme->tm_min, tme->tm_sec);
1700
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1701
 
        fprintf(fp, "\r\n");
1702
 
        fprintf(fp, "\r\n");
1703
 
 
1704
 
        // Report summary
1705
 
 
1706
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1707
 
        fprintf(fp, "|                                                           T O T A L S                                                            |\r\n");
1708
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1709
 
        fprintf(fp, "              Allocation unit count: %10s\r\n", insertCommas(stats.totalAllocUnitCount));
1710
 
        fprintf(fp, "            Reported to application: %s\r\n", memorySizeString(stats.totalReportedMemory));
1711
 
        fprintf(fp, "         Actual total memory in use: %s\r\n", memorySizeString(stats.totalActualMemory));
1712
 
        fprintf(fp, "           Memory tracking overhead: %s\r\n", memorySizeString(stats.totalActualMemory - stats.totalReportedMemory));
1713
 
        fprintf(fp, "\r\n");
1714
 
 
1715
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1716
 
        fprintf(fp, "|                                                            P E A K S                                                             |\r\n");
1717
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1718
 
        fprintf(fp, "              Allocation unit count: %10s\r\n", insertCommas(stats.peakAllocUnitCount));
1719
 
        fprintf(fp, "            Reported to application: %s\r\n", memorySizeString(stats.peakReportedMemory));
1720
 
        fprintf(fp, "                             Actual: %s\r\n", memorySizeString(stats.peakActualMemory));
1721
 
        fprintf(fp, "           Memory tracking overhead: %s\r\n", memorySizeString(stats.peakActualMemory - stats.peakReportedMemory));
1722
 
        fprintf(fp, "\r\n");
1723
 
 
1724
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1725
 
        fprintf(fp, "|                                                      A C C U M U L A T E D                                                       |\r\n");
1726
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1727
 
        fprintf(fp, "              Allocation unit count: %s\r\n", memorySizeString(stats.accumulatedAllocUnitCount));
1728
 
        fprintf(fp, "            Reported to application: %s\r\n", memorySizeString(stats.accumulatedReportedMemory));
1729
 
        fprintf(fp, "                             Actual: %s\r\n", memorySizeString(stats.accumulatedActualMemory));
1730
 
        fprintf(fp, "\r\n");
1731
 
 
1732
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1733
 
        fprintf(fp, "|                                                           U N U S E D                                                            |\r\n");
1734
 
        fprintf(fp, " ---------------------------------------------------------------------------------------------------------------------------------- \r\n");
1735
 
        fprintf(fp, "    Memory allocated but not in use: %s\r\n", memorySizeString(m_calcAllUnused()));
1736
 
        fprintf(fp, "\r\n");
1737
 
 
1738
 
        dumpAllocations(fp);
1739
 
 
1740
 
        fclose(fp);
1741
 
}
1742
 
 
1743
 
// ---------------------------------------------------------------------------------------------------------------------------------
1744
 
 
1745
 
sMStats m_getMemoryStatistics()
1746
 
{
1747
 
        return stats;
1748
 
}
1749
 
 
1750
 
// ---------------------------------------------------------------------------------------------------------------------------------
1751
 
// mmgr.cpp - End of file
1752
 
// ---------------------------------------------------------------------------------------------------------------------------------