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