00001 #ifndef _MERCURY_MEMORY_H 00002 #define _MERCURY_MEMORY_H 00003 00004 /* 00005 MercuryMemory is a very strange collection of yucky code. 00006 It's extremely dirty and is really only designed to work on 00007 Visual Studio under DEBUG mode. It's extremely dirty and 00008 breaks virtually every coding standard. 00009 */ 00010 00011 //We have to do this so C++ doesn't define a couple of functions 00012 // in stdlib.h. 00013 #define _INC_MALLOC 00014 00015 //We have to do this in VC.NET 2003 so it doesn't try doing xdebug. 00016 #define _XDEBUG_ 00017 00018 #define _NEW_CRT new 00019 #define _DELETE_CRT(_P) delete (_P) 00020 #define _DELETE_CRT_VEC(_P) delete[] (_P) 00021 #define _STRING_CRT string 00022 00023 //Get the main libary that has malloc and such, but won't have that compiled 00024 #include <stdlib.h> 00025 00026 //These functions are internal and should not get called. 00027 //Each represents their normal functions. 00028 void * MercuryMalloc( size_t x, const char * file, const int ); 00029 void * MercuryCalloc( size_t x, size_t y, const char * file, const int ); 00030 void * MercuryRealloc( void * ptr, size_t x, const char * file, const int ); 00031 //This will not actually malloc anyting, only put in a notice that something was already malloc'd 00032 void MercuryAMalloc( void * ptr, const char * file, const int ); 00033 00034 //Actually free ram 00035 void MercuryFree( void * ptr ); 00036 //Put in a notice that the ram was free'd 00037 void MercuryAFree( void * ptr ); 00038 00039 //Include the C++ memory here, before we macro out new. 00040 #include <memory> 00041 00043 class MercuryMemoryThrough 00044 { 00045 public: 00046 MercuryMemoryThrough() {} 00047 MercuryMemoryThrough( const char * fn, int ln ) { FILENAME=fn; LINENUMBER=ln; } 00048 //<< notifies a malloc 00049 template< class G > 00050 G * operator << ( G * t ) { MercuryAMalloc( (void*)t, FILENAME, LINENUMBER ); return t; } 00051 //>> notifies a free 00052 template< class G > 00053 G * operator >> ( G * t ) { MercuryAFree( (void*)t ); return t; } 00054 //File Name and Line Number of where the malloc started. 00055 int LINENUMBER; 00056 const char * FILENAME; 00057 }; 00058 00059 //This is a singleton, used in deletion. We don't need another one since we don't care about 00060 //line numbers or filenames. 00061 extern MercuryMemoryThrough OVERALLFREE; 00062 00064 static class MercuryMemoryThroughMaker 00065 { 00066 public: 00067 MercuryMemoryThrough Make( const char * s, int l ) { return MercuryMemoryThrough( s, l ); } 00068 } MTHRM; 00069 00070 //macro out new. This way we call the normal new, and send the address, filename and line number 00071 //into a temporarly made memory though class. 00072 #define new MTHRM.Make(__FILE__,__LINE__)<<new 00073 00074 //override a notification for normal deletion 00075 inline void operator delete(void* memblock) 00076 { 00077 MercuryAFree( memblock ); 00078 free( memblock ); 00079 } 00080 00081 //override a notification for [] deletion 00082 inline void operator delete[](void* memblock) 00083 { 00084 MercuryAFree( memblock ); 00085 free( memblock ); 00086 } 00087 00088 //macro overrides for malloc, realloc, calloc and free 00089 #define malloc( x ) MercuryMalloc( x, __FILE__, __LINE__ ) 00090 #define realloc( x, y ) MercuryRealloc( x, y, __FILE__, __LINE__ ) 00091 #define calloc( x, y ) MercuryCalloc( x, y, __FILE__, __LINE__ ) 00092 #define free( x ) MercuryFree( x ) 00093 00094 //user accessable functions 00095 void ResetAllLeakInfo(); 00096 void PrintAllLeakInfo(); 00097 00098 //If you call this with "true" MercuryMemory will print all leaks 00099 // after main. 00100 void SetPrintOnEnd( bool POD ); 00101 00102 #endif 00103 00104 /* 00105 * Copyright (c) 2006 Charles Lohr 00106 * All rights reserved. 00107 * 00108 * Redistribution and use in source and binary forms, with or 00109 * without modification, are permitted provided that the following 00110 * conditions are met: 00111 * - Redistributions of source code must retain the above 00112 * copyright notice, this list of conditions and the following disclaimer. 00113 * - Redistributions in binary form must reproduce the above copyright 00114 * notice, this list of conditions and the following disclaimer in 00115 * the documentation and/or other materials provided with the distribution. 00116 * - Neither the name of the Mercury Engine nor the names of its 00117 * contributors may be used to endorse or promote products derived from 00118 * this software without specific prior written permission. 00119 * 00120 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00121 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00122 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00123 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00124 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00125 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00126 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00127 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00128 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00129 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00130 */