cMemoryManager.h

00001 // ---------------------------------------------------------------------------------------------------------------------------------
00002 //                                     _     
00003 //                                    | |    
00004 //  _ __ ___  _ __ ___   __ _ _ __    | |__  
00005 // | '_ ` _ \| '_ ` _ \ / _` | '__|   | '_ \ 
00006 // | | | | | | | | | | | (_| | |    _ | | | |
00007 // |_| |_| |_|_| |_| |_|\__, |_|   (_)|_| |_|
00008 //                       __/ |               
00009 //                      |___/                
00010 //
00011 // Memory manager & tracking software
00012 //
00013 // Best viewed with 8-character tabs and (at least) 132 columns
00014 //
00015 // ---------------------------------------------------------------------------------------------------------------------------------
00016 //
00017 // Restrictions & freedoms pertaining to usage and redistribution of this software:
00018 //
00019 //  * This software is 100% free
00020 //  * If you use this software (in part or in whole) you must credit the author.
00021 //  * This software may not be re-distributed (in part or in whole) in a modified
00022 //    form without clear documentation on how to obtain a copy of the original work.
00023 //  * You may not use this software to directly or indirectly cause harm to others.
00024 //  * This software is provided as-is and without warrantee. Use at your own risk.
00025 //
00026 // For more information, visit HTTP://www.FluidStudios.com
00027 //
00028 // ---------------------------------------------------------------------------------------------------------------------------------
00029 // Originally created on 12/22/2000 by Paul Nettle
00030 //
00031 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
00032 // ---------------------------------------------------------------------------------------------------------------------------------
00033 
00034 #ifndef _H_MMGR
00035 #define _H_MMGR
00036 
00037 // ---------------------------------------------------------------------------------------------------------------------------------
00038 // For systems that don't have the __FUNCTION__ variable, we can just define it here
00039 // ---------------------------------------------------------------------------------------------------------------------------------
00040 
00041 #define __FUNCTION__ "??"
00042 
00043 // ---------------------------------------------------------------------------------------------------------------------------------
00044 // Types
00045 // ---------------------------------------------------------------------------------------------------------------------------------
00046 
00047 typedef struct tag_au
00048 {
00049         size_t          actualSize;
00050         size_t          reportedSize;
00051         void            *actualAddress;
00052         void            *reportedAddress;
00053         char            sourceFile[40];
00054         char            sourceFunc[40];
00055         unsigned int    sourceLine;
00056         unsigned int    allocationType;
00057         bool            breakOnDealloc;
00058         bool            breakOnRealloc;
00059         unsigned int    allocationNumber;
00060         struct tag_au   *next;
00061         struct tag_au   *prev;
00062 } sAllocUnit;
00063 
00064 typedef struct
00065 {
00066         unsigned int    totalReportedMemory;
00067         unsigned int    totalActualMemory;
00068         unsigned int    peakReportedMemory;
00069         unsigned int    peakActualMemory;
00070         unsigned int    accumulatedReportedMemory;
00071         unsigned int    accumulatedActualMemory;
00072         unsigned int    accumulatedAllocUnitCount;
00073         unsigned int    totalAllocUnitCount;
00074         unsigned int    peakAllocUnitCount;
00075 } sMStats;
00076 
00077 // ---------------------------------------------------------------------------------------------------------------------------------
00078 // External constants
00079 // ---------------------------------------------------------------------------------------------------------------------------------
00080 
00081 extern  const   unsigned int    m_alloc_unknown;
00082 extern  const   unsigned int    m_alloc_new;
00083 extern  const   unsigned int    m_alloc_new_array;
00084 extern  const   unsigned int    m_alloc_malloc;
00085 extern  const   unsigned int    m_alloc_calloc;
00086 extern  const   unsigned int    m_alloc_realloc;
00087 extern  const   unsigned int    m_alloc_delete;
00088 extern  const   unsigned int    m_alloc_delete_array;
00089 extern  const   unsigned int    m_alloc_free;
00090 
00091 // ---------------------------------------------------------------------------------------------------------------------------------
00092 // Used by the macros
00093 // ---------------------------------------------------------------------------------------------------------------------------------
00094 
00095 void            m_setOwner(const char *file, const unsigned int line, const char *func);
00096 
00097 // ---------------------------------------------------------------------------------------------------------------------------------
00098 // Allocation breakpoints
00099 // ---------------------------------------------------------------------------------------------------------------------------------
00100 
00101 bool            &m_breakOnRealloc(void *reportedAddress);
00102 bool            &m_breakOnDealloc(void *reportedAddress);
00103 
00104 // ---------------------------------------------------------------------------------------------------------------------------------
00105 // The meat of the memory tracking software
00106 // ---------------------------------------------------------------------------------------------------------------------------------
00107 
00108 void            *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
00109                              const unsigned int allocationType, const size_t reportedSize);
00110 void            *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
00111                                const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
00112 void            m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
00113                               const unsigned int deallocationType, const void *reportedAddress);
00114 
00115 // ---------------------------------------------------------------------------------------------------------------------------------
00116 // Utilitarian functions
00117 // ---------------------------------------------------------------------------------------------------------------------------------
00118 
00119 bool            m_validateAddress(const void *reportedAddress);
00120 bool            m_validateAllocUnit(const sAllocUnit *allocUnit);
00121 bool            m_validateAllAllocUnits();
00122 
00123 // ---------------------------------------------------------------------------------------------------------------------------------
00124 // Unused RAM calculations
00125 // ---------------------------------------------------------------------------------------------------------------------------------
00126 
00127 unsigned int    m_calcUnused(const sAllocUnit *allocUnit);
00128 unsigned int    m_calcAllUnused();
00129 
00130 // ---------------------------------------------------------------------------------------------------------------------------------
00131 // Logging and reporting
00132 // ---------------------------------------------------------------------------------------------------------------------------------
00133 
00134 void            m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
00135 void            m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
00136 sMStats         m_getMemoryStatistics();
00137 
00138 // ---------------------------------------------------------------------------------------------------------------------------------
00139 // Variations of global operators new & delete
00140 // ---------------------------------------------------------------------------------------------------------------------------------
00141 
00142 void    *operator new(size_t reportedSize);
00143 void    *operator new[](size_t reportedSize);
00144 void    *operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
00145 void    *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);
00146 void    operator delete(void *reportedAddress);
00147 void    operator delete[](void *reportedAddress);
00148 
00149 #endif // _H_MMGR
00150 
00151 // ---------------------------------------------------------------------------------------------------------------------------------
00152 // Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
00153 // ---------------------------------------------------------------------------------------------------------------------------------
00154 
00155 #include "cNonMemoryManager.h"
00156 #define new             (m_setOwner  (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
00157 #define delete          (m_setOwner  (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
00158 #define malloc(sz)      m_allocator  (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
00159 #define calloc(sz)      m_allocator  (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
00160 #define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
00161 #define free(ptr)       m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
00162 
00163 // ---------------------------------------------------------------------------------------------------------------------------------
00164 // mmgr.h - End of file
00165 // ---------------------------------------------------------------------------------------------------------------------------------
 All Classes Namespaces Functions Variables Enumerations

Generated on Sat Feb 20 22:55:07 2010 for cAudio by  doxygen 1.6.2