| 1 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 2 |
// _ |
| 3 |
// | | |
| 4 |
// _ __ ___ _ __ ___ __ _ _ __ | |__ |
| 5 |
// | '_ ` _ \| '_ ` _ \ / _` | '__| | '_ \ |
| 6 |
// | | | | | | | | | | | (_| | | _ | | | | |
| 7 |
// |_| |_| |_|_| |_| |_|\__, |_| (_)|_| |_| |
| 8 |
// __/ | |
| 9 |
// |___/ |
| 10 |
// |
| 11 |
// Memory manager & tracking software |
| 12 |
// |
| 13 |
// Best viewed with 8-character tabs and (at least) 132 columns |
| 14 |
// |
| 15 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 16 |
// |
| 17 |
// Restrictions & freedoms pertaining to usage and redistribution of this software: |
| 18 |
// |
| 19 |
// * This software is 100% free |
| 20 |
// * If you use this software (in part or in whole) you must credit the author. |
| 21 |
// * This software may not be re-distributed (in part or in whole) in a modified |
| 22 |
// form without clear documentation on how to obtain a copy of the original work. |
| 23 |
// * You may not use this software to directly or indirectly cause harm to others. |
| 24 |
// * This software is provided as-is and without warrantee. Use at your own risk. |
| 25 |
// |
| 26 |
// For more information, visit HTTP://www.FluidStudios.com |
| 27 |
// |
| 28 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 29 |
// Originally created on 12/22/2000 by Paul Nettle |
| 30 |
// |
| 31 |
// Copyright 2000, Fluid Studios, Inc., all rights reserved. |
| 32 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 33 |
|
| 34 |
#ifndef _H_MMGR |
| 35 |
#define _H_MMGR |
| 36 |
|
| 37 |
|
| 38 |
#ifdef __APPLE_CC__ |
| 39 |
//MAC hjm 7/11/2000 |
| 40 |
typedef unsigned long size_t; |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 45 |
// For systems that don't have the __FUNCTION__ variable, we can just define it here |
| 46 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 47 |
|
| 48 |
#define __FUNCTION__ "??" |
| 49 |
|
| 50 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 51 |
// Types |
| 52 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 53 |
|
| 54 |
typedef struct tag_au |
| 55 |
{ |
| 56 |
size_t actualSize; |
| 57 |
size_t reportedSize; |
| 58 |
void *actualAddress; |
| 59 |
void *reportedAddress; |
| 60 |
char sourceFile[40]; |
| 61 |
char sourceFunc[40]; |
| 62 |
unsigned int sourceLine; |
| 63 |
unsigned int allocationType; |
| 64 |
bool breakOnDealloc; |
| 65 |
bool breakOnRealloc; |
| 66 |
unsigned int allocationNumber; |
| 67 |
struct tag_au *next; |
| 68 |
struct tag_au *prev; |
| 69 |
} sAllocUnit; |
| 70 |
|
| 71 |
typedef struct |
| 72 |
{ |
| 73 |
unsigned int totalReportedMemory; |
| 74 |
unsigned int totalActualMemory; |
| 75 |
unsigned int peakReportedMemory; |
| 76 |
unsigned int peakActualMemory; |
| 77 |
unsigned int accumulatedReportedMemory; |
| 78 |
unsigned int accumulatedActualMemory; |
| 79 |
unsigned int accumulatedAllocUnitCount; |
| 80 |
unsigned int totalAllocUnitCount; |
| 81 |
unsigned int peakAllocUnitCount; |
| 82 |
} sMStats; |
| 83 |
|
| 84 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 85 |
// External constants |
| 86 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 87 |
|
| 88 |
extern const unsigned int m_alloc_unknown; |
| 89 |
extern const unsigned int m_alloc_new; |
| 90 |
extern const unsigned int m_alloc_new_array; |
| 91 |
extern const unsigned int m_alloc_malloc; |
| 92 |
extern const unsigned int m_alloc_calloc; |
| 93 |
extern const unsigned int m_alloc_realloc; |
| 94 |
extern const unsigned int m_alloc_delete; |
| 95 |
extern const unsigned int m_alloc_delete_array; |
| 96 |
extern const unsigned int m_alloc_free; |
| 97 |
|
| 98 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 99 |
// Used by the macros |
| 100 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 101 |
|
| 102 |
void m_setOwner(const char *file, const unsigned int line, const char *func); |
| 103 |
|
| 104 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 105 |
// Allocation breakpoints |
| 106 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 107 |
|
| 108 |
bool &m_breakOnRealloc(void *reportedAddress); |
| 109 |
bool &m_breakOnDealloc(void *reportedAddress); |
| 110 |
|
| 111 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 112 |
// The meat of the memory tracking software |
| 113 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 114 |
|
| 115 |
void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, |
| 116 |
const unsigned int allocationType, const size_t reportedSize); |
| 117 |
void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, |
| 118 |
const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress); |
| 119 |
void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc, |
| 120 |
const unsigned int deallocationType, const void *reportedAddress); |
| 121 |
|
| 122 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 123 |
// Utilitarian functions |
| 124 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 125 |
|
| 126 |
bool m_validateAddress(const void *reportedAddress); |
| 127 |
bool m_validateAllocUnit(const sAllocUnit *allocUnit); |
| 128 |
bool m_validateAllAllocUnits(); |
| 129 |
|
| 130 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 131 |
// Unused RAM calculations |
| 132 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 133 |
|
| 134 |
unsigned int m_calcUnused(const sAllocUnit *allocUnit); |
| 135 |
unsigned int m_calcAllUnused(); |
| 136 |
|
| 137 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 138 |
// Logging and reporting |
| 139 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 140 |
|
| 141 |
void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = ""); |
| 142 |
void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true); |
| 143 |
sMStats m_getMemoryStatistics(); |
| 144 |
|
| 145 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 146 |
// Variations of global operators new & delete |
| 147 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 148 |
|
| 149 |
void *operator new(size_t reportedSize); |
| 150 |
void *operator new[](size_t reportedSize); |
| 151 |
void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine); |
| 152 |
void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine); |
| 153 |
void operator delete(void *reportedAddress); |
| 154 |
void operator delete[](void *reportedAddress); |
| 155 |
|
| 156 |
#endif // _H_MMGR |
| 157 |
|
| 158 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 159 |
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :) |
| 160 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 161 |
|
| 162 |
#include "nommgr.h" |
| 163 |
#define new (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new |
| 164 |
#define delete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete |
| 165 |
#define malloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz) |
| 166 |
#define calloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz) |
| 167 |
#define realloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr) |
| 168 |
#define free(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr) |
| 169 |
|
| 170 |
// --------------------------------------------------------------------------------------------------------------------------------- |
| 171 |
// mmgr.h - End of file |
| 172 |
// --------------------------------------------------------------------------------------------------------------------------------- |