2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
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.
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.
15 See the GNU General Public License for more details.
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.
22 // ---------------------------------------------------------------------------------------------------------------------------------
25 // _ __ ___ _ __ ___ __ _ _ __ ___ _ __ _ __
26 // | '_ ` _ \| '_ ` _ \ / _` | '__| / __| '_ \| '_ \
27 // | | | | | | | | | | | (_| | | _ | (__| |_) | |_) |
28 // |_| |_| |_|_| |_| |_|\__, |_| (_) \___| .__/| .__/
32 // Memory manager & tracking software
34 // Best viewed with 8-character tabs and (at least) 132 columns
36 // ---------------------------------------------------------------------------------------------------------------------------------
38 // Restrictions & freedoms pertaining to usage and redistribution of this software:
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.
47 // For more information, visit HTTP://www.FluidStudios.com
49 // ---------------------------------------------------------------------------------------------------------------------------------
50 // Originally created on 12/22/2000 by Paul Nettle
52 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
53 // ---------------------------------------------------------------------------------------------------------------------------------
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.
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.
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".
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.
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.
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.)
80 // 5. With MFC applications, you will need to comment out any occurance of "#define new DEBUG_NEW" from all source files.
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:
86 // #include <stdio.h> // Standard includes MUST come first
87 // #include <stdlib.h> //
88 // #include <streamio> //
90 // #include "mmgr.h" // mmgr.h MUST come next
92 // #include "myfile1.h" // Project includes MUST come last
93 // #include "myfile2.h" //
94 // #include "myfile3.h" //
96 // ---------------------------------------------------------------------------------------------------------------------------------
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
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.
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
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.
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.
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.)
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:
145 // http://www.FluidStudios.com/publications.html
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 // ---------------------------------------------------------------------------------------------------------------------------------
151 //#define TEST_MEMORY_MANAGER
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 // ---------------------------------------------------------------------------------------------------------------------------------
157 //#define STRESS_TEST
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 // ---------------------------------------------------------------------------------------------------------------------------------
164 //#define RANDOM_FAILURE 10.0
166 // ---------------------------------------------------------------------------------------------------------------------------------
167 // -DOC- Locals -- modify these flags to suit your needs
168 // ---------------------------------------------------------------------------------------------------------------------------------
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!
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;
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.
192 // The BEOS assert added by Arvid Norberg <arvid@iname.com>.
193 // ---------------------------------------------------------------------------------------------------------------------------------
197 #define m_assert(x) if ((x) == false) __asm { int 3 }
199 #define m_assert(x) {}
201 #elif defined(__BEOS__)
203 extern void debugger(const char *message);
204 #define m_assert(x) if ((x) == false) debugger("mmgr: assert failed")
206 #define m_assert(x) {}
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)
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 // ---------------------------------------------------------------------------------------------------------------------------------
224 // ---------------------------------------------------------------------------------------------------------------------------------
225 // Defaults for the constants & statics in the MemoryManager class
226 // ---------------------------------------------------------------------------------------------------------------------------------
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;
238 // ---------------------------------------------------------------------------------------------------------------------------------
239 // -DOC- Get to know these values. They represent the values that will be used to fill unused and deallocated RAM.
240 // ---------------------------------------------------------------------------------------------------------------------------------
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
247 // ---------------------------------------------------------------------------------------------------------------------------------
249 // ---------------------------------------------------------------------------------------------------------------------------------
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();
270 // ---------------------------------------------------------------------------------------------------------------------------------
271 // Local functions only
272 // ---------------------------------------------------------------------------------------------------------------------------------
274 static void log(const char *format, ...)
278 if (cleanupLogOnFirstRun) doCleanupLogOnFirstRun();
282 static char buffer[2048];
284 va_start(ap, format);
285 vsprintf(buffer, format, ap);
290 FILE *fp = fopen(memoryLogFile, "ab");
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.)
298 // Spit out the data to the log
300 fprintf(fp, "%s\r\n", buffer);
304 // ---------------------------------------------------------------------------------------------------------------------------------
306 static void doCleanupLogOnFirstRun()
308 if (cleanupLogOnFirstRun)
310 unlink(memoryLogFile);
311 cleanupLogOnFirstRun = false;
313 // Print a header for the log
315 time_t t = time(NULL);
316 log("--------------------------------------------------------------------------------");
318 log(" %s - Memory logging file created on %s", memoryLogFile, asctime(localtime(&t)));
319 log("--------------------------------------------------------------------------------");
321 log("This file contains a log of all memory operations performed during the last run.");
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.");
328 log("There is a lot of useful information here which, when used creatively, can be");
329 log("extremely helpful.");
331 log("Note that the following guides are used throughout this file:");
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");
341 log("...so, to find all errors in the file, search for \"[!]\"");
343 log("--------------------------------------------------------------------------------");
347 // ---------------------------------------------------------------------------------------------------------------------------------
349 static const char *sourceFileStripper(const char *sourceFile)
351 char *ptr = strrchr(sourceFile, '\\');
352 if (ptr) return ptr + 1;
353 ptr = strrchr(sourceFile, '/');
354 if (ptr) return ptr + 1;
358 // ---------------------------------------------------------------------------------------------------------------------------------
360 static const char *ownerString(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc)
363 memset(str, 0, sizeof(str));
364 sprintf(str, "%s(%05d)::%s", sourceFileStripper(sourceFile), sourceLine, sourceFunc);
368 // ---------------------------------------------------------------------------------------------------------------------------------
370 static const char *insertCommas(unsigned int value)
373 memset(str, 0, sizeof(str));
375 sprintf(str, "%u", value);
378 memmove(&str[strlen(str)-3], &str[strlen(str)-4], 4);
379 str[strlen(str) - 4] = ',';
383 memmove(&str[strlen(str)-7], &str[strlen(str)-8], 8);
384 str[strlen(str) - 8] = ',';
386 if (strlen(str) > 11)
388 memmove(&str[strlen(str)-11], &str[strlen(str)-12], 12);
389 str[strlen(str) - 12] = ',';
395 // ---------------------------------------------------------------------------------------------------------------------------------
397 static const char *memorySizeString(unsigned long size)
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));
406 // ---------------------------------------------------------------------------------------------------------------------------------
408 static sAllocUnit *findAllocUnit(const void *reportedAddress)
411 m_assert(reportedAddress != NULL);
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.
417 unsigned int hashIndex = (reinterpret_cast<unsigned int>(const_cast<void *>(reportedAddress)) >> 4) & (hashSize - 1);
418 sAllocUnit *ptr = hashTable[hashIndex];
421 if (ptr->reportedAddress == reportedAddress) return ptr;
428 // ---------------------------------------------------------------------------------------------------------------------------------
430 static size_t calculateActualSize(const size_t reportedSize)
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.)
436 return reportedSize + paddingSize * sizeof(long) * 2;
439 // ---------------------------------------------------------------------------------------------------------------------------------
441 static size_t calculateReportedSize(const size_t actualSize)
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.)
447 return actualSize - paddingSize * sizeof(long) * 2;
450 // ---------------------------------------------------------------------------------------------------------------------------------
452 static void *calculateReportedAddress(const void *actualAddress)
456 if (!actualAddress) return NULL;
458 // JUst account for the padding
460 return reinterpret_cast<void *>(const_cast<char *>(reinterpret_cast<const char *>(actualAddress) + sizeof(long) * paddingSize));
463 // ---------------------------------------------------------------------------------------------------------------------------------
465 static void wipeWithPattern(sAllocUnit *allocUnit, unsigned long pattern, const unsigned int originalReportedSize = 0)
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.
474 pattern = ((rand() & 0xff) << 24) | ((rand() & 0xff) << 16) | ((rand() & 0xff) << 8) | (rand() & 0xff);
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.
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.
484 // This part of the operation is optional
486 if (alwaysWipeAll && allocUnit->reportedSize > originalReportedSize)
490 long *lptr = reinterpret_cast<long *>(reinterpret_cast<char *>(allocUnit->reportedAddress) + originalReportedSize);
491 int length = static_cast<int>(allocUnit->reportedSize - originalReportedSize);
493 for (i = 0; i < (length >> 2); i++, lptr++)
498 // Fill the remainder
500 unsigned int shiftCount = 0;
501 char *cptr = reinterpret_cast<char *>(lptr);
502 for (i = 0; i < (length & 0x3); i++, cptr++, shiftCount += 8)
504 *cptr = static_cast<char>((pattern & (0xff << shiftCount)) >> shiftCount);
508 // Write in the prefix/postfix bytes
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++)
514 *pre = prefixPattern;
515 *post = postfixPattern;
519 // ---------------------------------------------------------------------------------------------------------------------------------
521 static void dumpAllocations(FILE *fp)
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");
528 for (unsigned int i = 0; i < hashSize; i++)
530 sAllocUnit *ptr = hashTable[i];
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,
538 allocationTypes[ptr->allocationType],
539 ptr->breakOnDealloc ? 'Y':'N',
540 ptr->breakOnRealloc ? 'Y':'N',
541 ownerString(ptr->sourceFile, ptr->sourceLine, ptr->sourceFunc));
547 // ---------------------------------------------------------------------------------------------------------------------------------
549 static void dumpLeakReport()
551 // Open the report file
553 FILE *fp = fopen(memoryLeakLogFile, "w+b");
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
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");
573 if (stats.totalAllocUnitCount)
575 fprintf(fp, "%d memory leak%s found:\r\n", stats.totalAllocUnitCount, stats.totalAllocUnitCount == 1 ? "":"s");
579 fprintf(fp, "Congratulations! No memory leaks found!\r\n");
581 // We can finally free up our own memory allocations
585 for (unsigned int i = 0; i < reservoirBufferSize; i++)
587 free(reservoirBuffer[i]);
589 free(reservoirBuffer);
591 reservoirBufferSize = 0;
597 if (stats.totalAllocUnitCount)
605 // ---------------------------------------------------------------------------------------------------------------------------------
606 // We use a static class to let us know when we're in the midst of static deinitialization
607 // ---------------------------------------------------------------------------------------------------------------------------------
609 class MemStaticTimeTracker
612 MemStaticTimeTracker() {doCleanupLogOnFirstRun();}
613 ~MemStaticTimeTracker() {staticDeinitTime = true; dumpLeakReport();}
615 static MemStaticTimeTracker mstt;
617 // ---------------------------------------------------------------------------------------------------------------------------------
618 // -DOC- Flags & options -- Call these routines to enable/disable the following options
619 // ---------------------------------------------------------------------------------------------------------------------------------
621 bool &m_alwaysValidateAll()
623 // Force a validation of all allocation units each time we enter this software
624 return alwaysValidateAll;
627 // ---------------------------------------------------------------------------------------------------------------------------------
629 bool &m_alwaysLogAll()
631 // Force a log of every allocation & deallocation into memory.log
635 // ---------------------------------------------------------------------------------------------------------------------------------
637 bool &m_alwaysWipeAll()
639 // Force this software to always wipe memory with a pattern when it is being allocated/dallocated
640 return alwaysWipeAll;
643 // ---------------------------------------------------------------------------------------------------------------------------------
645 bool &m_randomeWipe()
647 // Force this software to use a random pattern when wiping memory -- good for stress testing
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
654 // ---------------------------------------------------------------------------------------------------------------------------------
656 bool &m_breakOnRealloc(void *reportedAddress)
658 // Locate the existing allocation unit
660 sAllocUnit *au = findAllocUnit(reportedAddress);
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);
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);
672 return au->breakOnRealloc;
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
678 // ---------------------------------------------------------------------------------------------------------------------------------
680 bool &m_breakOnDealloc(void *reportedAddress)
682 // Locate the existing allocation unit
684 sAllocUnit *au = findAllocUnit(reportedAddress);
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);
690 return au->breakOnDealloc;
693 // ---------------------------------------------------------------------------------------------------------------------------------
694 // -DOC- When tracking down a difficult bug, use this routine to force a breakpoint on a specific allocation count
695 // ---------------------------------------------------------------------------------------------------------------------------------
697 void m_breakOnAllocation(unsigned int count)
699 breakOnAllocationCount = count;
702 // ---------------------------------------------------------------------------------------------------------------------------------
703 // Used by the macros
704 // ---------------------------------------------------------------------------------------------------------------------------------
706 void m_setOwner(const char *file, const unsigned int line, const char *func)
708 // You're probably wondering about this...
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:
714 // m_setOwner(__FILE__, __LINE__, __FUNCTION__) --> object::~object() --> delete
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):
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
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.
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.
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...
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".
748 // Thanks to J. Woznack (from Kodiak Interactive Software Studios -- www.kodiakgames.com) for pointing this out.
750 if (sourceLine && alwaysLogAll)
752 log("[I] NOTE! Possible destructor chain: previous owner is %s", ownerString(sourceFile, sourceLine, sourceFunc));
755 // Okay... save this stuff off so we can keep track of the caller
762 // ---------------------------------------------------------------------------------------------------------------------------------
764 static void resetGlobals()
771 // ---------------------------------------------------------------------------------------------------------------------------------
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 // ---------------------------------------------------------------------------------------------------------------------------------
778 void *operator new(size_t reportedSize)
780 #ifdef TEST_MEMORY_MANAGER
781 log("[D] ENTER: new");
786 const char *file = sourceFile;
787 const unsigned int line = sourceLine;
788 const char *func = sourceFunc;
790 // ANSI says: allocation requests of 0 bytes will still return a valid value
792 if (reportedSize == 0) reportedSize = 1;
794 // ANSI says: loop continuously because the error handler could possibly free up some memory
798 // Try the allocation
800 void *ptr = m_allocator(file, line, func, m_alloc_new, reportedSize);
803 #ifdef TEST_MEMORY_MANAGER
804 log("[D] EXIT : new");
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.
812 std::new_handler nh = std::set_new_handler(0);
813 std::set_new_handler(nh);
815 // If there is an error handler, call it
822 // Otherwise, throw the exception
826 #ifdef TEST_MEMORY_MANAGER
827 log("[D] EXIT : new");
829 throw std::bad_alloc();
834 // ---------------------------------------------------------------------------------------------------------------------------------
836 void *operator new[](size_t reportedSize)
838 #ifdef TEST_MEMORY_MANAGER
839 log("[D] ENTER: new[]");
844 const char *file = sourceFile;
845 const unsigned int line = sourceLine;
846 const char *func = sourceFunc;
848 // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
850 if (reportedSize == 0) reportedSize = 1;
852 // ANSI says: loop continuously because the error handler could possibly free up some memory
856 // Try the allocation
858 void *ptr = m_allocator(file, line, func, m_alloc_new_array, reportedSize);
861 #ifdef TEST_MEMORY_MANAGER
862 log("[D] EXIT : new[]");
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.
870 std::new_handler nh = std::set_new_handler(0);
871 std::set_new_handler(nh);
873 // If there is an error handler, call it
880 // Otherwise, throw the exception
884 #ifdef TEST_MEMORY_MANAGER
885 log("[D] EXIT : new[]");
887 throw std::bad_alloc();
892 // ---------------------------------------------------------------------------------------------------------------------------------
893 // Other global new/new[]
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 // ---------------------------------------------------------------------------------------------------------------------------------
900 void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine)
902 #ifdef TEST_MEMORY_MANAGER
903 log("[D] ENTER: new");
906 // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
908 if (reportedSize == 0) reportedSize = 1;
910 // ANSI says: loop continuously because the error handler could possibly free up some memory
914 // Try the allocation
916 void *ptr = m_allocator(sourceFile, sourceLine, "??", m_alloc_new, reportedSize);
919 #ifdef TEST_MEMORY_MANAGER
920 log("[D] EXIT : new");
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.
928 std::new_handler nh = std::set_new_handler(0);
929 std::set_new_handler(nh);
931 // If there is an error handler, call it
938 // Otherwise, throw the exception
942 #ifdef TEST_MEMORY_MANAGER
943 log("[D] EXIT : new");
945 throw std::bad_alloc();
950 // ---------------------------------------------------------------------------------------------------------------------------------
952 void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine)
954 #ifdef TEST_MEMORY_MANAGER
955 log("[D] ENTER: new[]");
958 // The ANSI standard says that allocation requests of 0 bytes will still return a valid value
960 if (reportedSize == 0) reportedSize = 1;
962 // ANSI says: loop continuously because the error handler could possibly free up some memory
966 // Try the allocation
968 void *ptr = m_allocator(sourceFile, sourceLine, "??", m_alloc_new_array, reportedSize);
971 #ifdef TEST_MEMORY_MANAGER
972 log("[D] EXIT : new[]");
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.
980 std::new_handler nh = std::set_new_handler(0);
981 std::set_new_handler(nh);
983 // If there is an error handler, call it
990 // Otherwise, throw the exception
994 #ifdef TEST_MEMORY_MANAGER
995 log("[D] EXIT : new[]");
997 throw std::bad_alloc();
1002 // ---------------------------------------------------------------------------------------------------------------------------------
1003 // Global delete/delete[]
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 // ---------------------------------------------------------------------------------------------------------------------------------
1009 void operator delete(void *reportedAddress)
1011 #ifdef TEST_MEMORY_MANAGER
1012 log("[D] ENTER: delete");
1015 // ANSI says: delete & delete[] allow NULL pointers (they do nothing)
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));
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.
1025 #ifdef TEST_MEMORY_MANAGER
1026 log("[D] EXIT : delete");
1030 // ---------------------------------------------------------------------------------------------------------------------------------
1032 void operator delete[](void *reportedAddress)
1034 #ifdef TEST_MEMORY_MANAGER
1035 log("[D] ENTER: delete[]");
1038 // ANSI says: delete & delete[] allow NULL pointers (they do nothing)
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));
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.
1049 #ifdef TEST_MEMORY_MANAGER
1050 log("[D] EXIT : delete[]");
1054 // ---------------------------------------------------------------------------------------------------------------------------------
1055 // Allocate memory and track it
1056 // ---------------------------------------------------------------------------------------------------------------------------------
1058 void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int allocationType, const size_t reportedSize)
1062 #ifdef TEST_MEMORY_MANAGER
1063 log("[D] ENTER: m_allocator()");
1066 // Increase our allocation count
1068 currentAllocationCount++;
1072 if (alwaysLogAll) log("[+] %05d %8s of size 0x%08X(%08d) by %s", currentAllocationCount, allocationTypes[allocationType], reportedSize, reportedSize, ownerString(sourceFile, sourceLine, sourceFunc));
1074 // If you hit this assert, you requested a breakpoint on a specific allocation count
1075 m_assert(currentAllocationCount != breakOnAllocationCount);
1077 // If necessary, grow the reservoir of unused allocation units
1081 // Allocate 256 reservoir elements
1083 reservoir = (sAllocUnit *) malloc(sizeof(sAllocUnit) * 256);
1085 // If you hit this assert, then the memory manager failed to allocate internal memory for tracking the
1087 m_assert(reservoir != NULL);
1089 // Danger Will Robinson!
1091 if (reservoir == NULL) throw "Unable to allocate RAM for internal memory tracking data";
1093 // Build a linked-list of the elements in our reservoir
1095 memset(reservoir, 0, sizeof(sAllocUnit) * 256);
1096 for (unsigned int i = 0; i < 256 - 1; i++)
1098 reservoir[i].next = &reservoir[i+1];
1101 // Add this address to our reservoirBuffer so we can free it later
1103 sAllocUnit **temp = (sAllocUnit **) realloc(reservoirBuffer, (reservoirBufferSize + 1) * sizeof(sAllocUnit *));
1107 reservoirBuffer = temp;
1108 reservoirBuffer[reservoirBufferSize++] = reservoir;
1112 // Logical flow says this should never happen...
1113 m_assert(reservoir != NULL);
1115 // Grab a new allocaton unit from the front of the reservoir
1117 sAllocUnit *au = reservoir;
1118 reservoir = au->next;
1120 // Populate it with some real data
1122 memset(au, 0, sizeof(sAllocUnit));
1123 au->actualSize = calculateActualSize(reportedSize);
1124 #ifdef RANDOM_FAILURE
1126 double b = RAND_MAX / 100.0 * RANDOM_FAILURE;
1129 au->actualAddress = malloc(au->actualSize);
1133 log("[F] Random faiure");
1134 au->actualAddress = NULL;
1137 au->actualAddress = malloc(au->actualSize);
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, "??");
1149 // We don't want to assert with random failures, because we want the application to deal with them.
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);
1157 if (au->actualAddress == NULL)
1159 throw "Request for allocation failed. Out of memory.";
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);
1166 // Insert the new allocation into the hash table
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];
1172 hashTable[hashIndex] = au;
1174 // Account for the new allocatin unit in our stats
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++;
1186 // Prepare the allocation unit for use (wipe it with recognizable garbage)
1188 wipeWithPattern(au, unusedPattern);
1190 // calloc() expects the reported memory address range to be filled with 0's
1192 if (allocationType == m_alloc_calloc)
1194 memset(au->reportedAddress, 0, au->reportedSize);
1197 // Validate every single allocated unit in memory
1199 if (alwaysValidateAll) m_validateAllAllocUnits();
1203 if (alwaysLogAll) log("[+] ----> addr 0x%08X", reinterpret_cast<unsigned int>(au->reportedAddress));
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.
1210 // Return the (reported) address of the new allocation unit
1212 #ifdef TEST_MEMORY_MANAGER
1213 log("[D] EXIT : m_allocator()");
1216 return au->reportedAddress;
1218 catch(const char *err)
1220 // Deal with the errors
1225 #ifdef TEST_MEMORY_MANAGER
1226 log("[D] EXIT : m_allocator()");
1233 // ---------------------------------------------------------------------------------------------------------------------------------
1234 // Reallocate memory and track it
1235 // ---------------------------------------------------------------------------------------------------------------------------------
1237 void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress)
1241 #ifdef TEST_MEMORY_MANAGER
1242 log("[D] ENTER: m_reallocator()");
1245 // Calling realloc with a NULL should force same operations as a malloc
1247 if (!reportedAddress)
1249 return m_allocator(sourceFile, sourceLine, sourceFunc, reallocationType, reportedSize);
1252 // Increase our allocation count
1254 currentAllocationCount++;
1256 // If you hit this assert, you requested a breakpoint on a specific allocation count
1257 m_assert(currentAllocationCount != breakOnAllocationCount);
1261 if (alwaysLogAll) log("[~] %05d %8s of size 0x%08X(%08d) by %s", currentAllocationCount, allocationTypes[reallocationType], reportedSize, reportedSize, ownerString(sourceFile, sourceLine, sourceFunc));
1263 // Locate the existing allocation unit
1265 sAllocUnit *au = findAllocUnit(reportedAddress);
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";
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));
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);
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);
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
1288 m_assert(au->breakOnRealloc == false);
1290 // Keep track of the original size
1292 unsigned int originalReportedSize = static_cast<unsigned int>(au->reportedSize);
1294 if (alwaysLogAll) log("[~] ----> from 0x%08X(%08d)", originalReportedSize, originalReportedSize);
1296 // Do the reallocation
1298 void *oldReportedAddress = reportedAddress;
1299 size_t newActualSize = calculateActualSize(reportedSize);
1300 void *newActualAddress = NULL;
1301 #ifdef RANDOM_FAILURE
1303 double b = RAND_MAX / 100.0 * RANDOM_FAILURE;
1306 newActualAddress = realloc(au->actualAddress, newActualSize);
1310 log("[F] Random faiure");
1313 newActualAddress = realloc(au->actualAddress, newActualSize);
1316 // We don't want to assert with random failures, because we want the application to deal with them.
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);
1325 if (!newActualAddress) throw "Request for reallocation failed. Out of memory.";
1327 // Remove this allocation from our stats (we'll add the new reallocation again later)
1329 stats.totalReportedMemory -= static_cast<unsigned int>(au->reportedSize);
1330 stats.totalActualMemory -= static_cast<unsigned int>(au->actualSize);
1332 // Update the allocation with the new information
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, "??");
1346 // The reallocation may cause the address to change, so we should relocate our allocation unit within the hash table
1348 unsigned int hashIndex = static_cast<unsigned int>(-1);
1349 if (oldReportedAddress != au->reportedAddress)
1351 // Remove this allocation unit from the hash table
1354 unsigned int hashIndex = (reinterpret_cast<unsigned int>(oldReportedAddress) >> 4) & (hashSize - 1);
1355 if (hashTable[hashIndex] == au)
1357 hashTable[hashIndex] = hashTable[hashIndex]->next;
1361 if (au->prev) au->prev->next = au->next;
1362 if (au->next) au->next->prev = au->prev;
1366 // Re-insert it back into the hash table
1368 hashIndex = (reinterpret_cast<unsigned int>(au->reportedAddress) >> 4) & (hashSize - 1);
1369 if (hashTable[hashIndex]) hashTable[hashIndex]->prev = au;
1370 au->next = hashTable[hashIndex];
1372 hashTable[hashIndex] = au;
1375 // Account for the new allocatin unit in our stats
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)
1384 stats.accumulatedReportedMemory += deltaReportedSize;
1385 stats.accumulatedActualMemory += deltaReportedSize;
1388 // Prepare the allocation unit for use (wipe it with recognizable garbage)
1390 wipeWithPattern(au, unusedPattern, originalReportedSize);
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));
1396 // Validate every single allocated unit in memory
1398 if (alwaysValidateAll) m_validateAllAllocUnits();
1402 if (alwaysLogAll) log("[~] ----> addr 0x%08X", reinterpret_cast<unsigned int>(au->reportedAddress));
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.
1409 // Return the (reported) address of the new allocation unit
1411 #ifdef TEST_MEMORY_MANAGER
1412 log("[D] EXIT : m_reallocator()");
1415 return au->reportedAddress;
1417 catch(const char *err)
1419 // Deal with the errors
1424 #ifdef TEST_MEMORY_MANAGER
1425 log("[D] EXIT : m_reallocator()");
1432 // ---------------------------------------------------------------------------------------------------------------------------------
1433 // Deallocate memory and track it
1434 // ---------------------------------------------------------------------------------------------------------------------------------
1436 void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, const unsigned int deallocationType, const void *reportedAddress)
1440 #ifdef TEST_MEMORY_MANAGER
1441 log("[D] ENTER: m_deallocator()");
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));
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.
1452 if (reportedAddress)
1454 // Go get the allocation unit
1456 sAllocUnit *au = findAllocUnit(reportedAddress);
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";
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));
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);
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 ) );
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);
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.
1486 wipeWithPattern(au, releasedPattern);
1488 // Do the deallocation
1490 free(au->actualAddress);
1492 // Remove this allocation unit from the hash table
1494 unsigned int hashIndex = (reinterpret_cast<unsigned int>(au->reportedAddress) >> 4) & (hashSize - 1);
1495 if (hashTable[hashIndex] == au)
1497 hashTable[hashIndex] = au->next;
1501 if (au->prev) au->prev->next = au->next;
1502 if (au->next) au->next->prev = au->prev;
1505 // Remove this allocation from our stats
1507 stats.totalReportedMemory -= static_cast<unsigned int>(au->reportedSize);
1508 stats.totalActualMemory -= static_cast<unsigned int>(au->actualSize);
1509 stats.totalAllocUnitCount--;
1511 // Add this allocation unit to the front of our reservoir of unused allocation units
1513 memset(au, 0, sizeof(sAllocUnit));
1514 au->next = reservoir;
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.
1523 // Validate every single allocated unit in memory
1525 if (alwaysValidateAll) m_validateAllAllocUnits();
1527 // If we're in the midst of static deinitialization time, track any pending memory leaks
1529 if (staticDeinitTime) dumpLeakReport();
1531 catch(const char *err)
1539 #ifdef TEST_MEMORY_MANAGER
1540 log("[D] EXIT : m_deallocator()");
1544 // ---------------------------------------------------------------------------------------------------------------------------------
1545 // -DOC- The following utilitarian allow you to become proactive in tracking your own memory, or help you narrow in on those tough
1547 // ---------------------------------------------------------------------------------------------------------------------------------
1549 bool m_validateAddress(const void *reportedAddress)
1551 // Just see if the address exists in our allocation routines
1553 return findAllocUnit(reportedAddress) != NULL;
1556 // ---------------------------------------------------------------------------------------------------------------------------------
1558 bool m_validateAllocUnit(const sAllocUnit *allocUnit)
1560 // Make sure the padding is untouched
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++)
1567 if (*pre != (long) prefixPattern)
1569 log("[!] A memory allocation unit was corrupt because of an underrun:");
1570 m_dumpAllocUnit(allocUnit, " ");
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));
1579 if (*post != static_cast<long>(postfixPattern))
1581 log("[!] A memory allocation unit was corrupt because of an overrun:");
1582 m_dumpAllocUnit(allocUnit, " ");
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));
1592 // Return the error status (we invert it, because a return of 'false' means error)
1597 // ---------------------------------------------------------------------------------------------------------------------------------
1599 bool m_validateAllAllocUnits()
1601 // Just go through each allocation unit in the hash table and count the ones that have errors
1603 unsigned int errors = 0;
1604 unsigned int allocCount = 0;
1605 for (unsigned int i = 0; i < hashSize; i++)
1607 sAllocUnit *ptr = hashTable[i];
1611 if (!m_validateAllocUnit(ptr)) errors++;
1616 // Test for hash-table correctness
1618 if (allocCount != stats.totalAllocUnitCount)
1620 log("[!] Memory tracking hash table corrupt!");
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
1629 m_assert(allocCount == stats.totalAllocUnitCount);
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);
1637 if (errors) log("[!] While validting all allocation units, %d allocation unit(s) were found to have problems", errors);
1639 // Return the error status
1644 // ---------------------------------------------------------------------------------------------------------------------------------
1645 // -DOC- Unused RAM calculation routines. Use these to determine how much of your RAM is unused (in bytes)
1646 // ---------------------------------------------------------------------------------------------------------------------------------
1648 unsigned int m_calcUnused(const sAllocUnit *allocUnit)
1650 const unsigned long *ptr = reinterpret_cast<const unsigned long *>(allocUnit->reportedAddress);
1651 unsigned int count = 0;
1653 for (unsigned int i = 0; i < allocUnit->reportedSize; i += sizeof(long), ptr++)
1655 if (*ptr == unusedPattern) count += sizeof(long);
1661 // ---------------------------------------------------------------------------------------------------------------------------------
1663 unsigned int m_calcAllUnused()
1665 // Just go through each allocation unit in the hash table and count the unused RAM
1667 unsigned int total = 0;
1668 for (unsigned int i = 0; i < hashSize; i++)
1670 sAllocUnit *ptr = hashTable[i];
1673 total += m_calcUnused(ptr);
1681 // ---------------------------------------------------------------------------------------------------------------------------------
1682 // -DOC- The following functions are for logging and statistics reporting.
1683 // ---------------------------------------------------------------------------------------------------------------------------------
1685 void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix)
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);
1696 // ---------------------------------------------------------------------------------------------------------------------------------
1698 void m_dumpMemoryReport(const char *filename, const bool overwrite)
1700 // Open the report file
1704 if (overwrite) fp = fopen(filename, "w+b");
1705 else fp = fopen(filename, "ab");
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
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");
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");
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");
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");
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");
1758 dumpAllocations(fp);
1763 // ---------------------------------------------------------------------------------------------------------------------------------
1765 sMStats m_getMemoryStatistics()
1770 // ---------------------------------------------------------------------------------------------------------------------------------
1771 // mmgr.cpp - End of file
1772 // ---------------------------------------------------------------------------------------------------------------------------------