2 * Alloc - Memory instrumentation apparatus
10 * The first step in instrumenting memory management code is to replace
11 * your allocation and deallocation statements, and augment "new" and "delete"
12 * commands. Once this instrumentation is in place, your application can
13 * call the ShowMemoryManager() routine, which provides (on a managed
14 * background thread) an interactive window showing current memory usage.
16 * EXAMPLES ___________________________________________________________________
18 * The easiest memory allocation method is:
20 * LPTSTR psz = Allocate (sizeof(TCHAR) * 256);
23 * Internally, Allocate() uses GlobalAlloc() to actually allocate the
24 * requested chunk of memory (plus additional space at the end of the chunk,
25 * for a trailing signature). Free() calls GlobalFree() to release the chunk,
26 * after validating the trailing signature. The two routines also allow
27 * the memory manager to keep track of where memory is allocated and freed.
29 * You can construct and destroy C++ objects in the same way:
31 * LPTSTR psz = New (TCHAR[256]);
34 * For complex constructors, you may need to use the New2 macro:
36 * OBJECT *pObject = New2 (OBJECT,(15,242));
42 * DEFINITIONS ________________________________________________________________
44 * You can turn off instrumentation for a particular source file by
45 * undefining DEBUG for that file. If you want -DDEBUG but still don't
46 * want instrumentation, you can explicitly define NO_DEBUG_ALLOC.
51 #ifndef NO_DEBUG_ALLOC
52 #define NO_DEBUG_ALLOC
57 * If you put this package as part of a .DLL, that .DLL will need to
58 * add these functions to its .DEF file:
61 * WhileMemoryManagerShowing
62 * MemMgr_AllocateMemory
67 * You can change MEMMGR_CALLCONV in your .DLL to be _declspec(dllexport),
68 * and in your other packages to _declspec(dllimport), to ensure that all
69 * modules use the same instance of this package.
73 #ifndef MEMMGR_CALLCONV
74 #define MEMMGR_CALLCONV _cdecl
78 * MACROS _____________________________________________________________________
80 * These definitions hide or expose instrumentation based on your
87 #define Allocate(_s) ((PVOID)GlobalAlloc(GMEM_FIXED,_s))
88 #define Free(_p) GlobalFree((HGLOBAL)(_p))
89 #define New(_t) new _t
90 #define New2(_t,_a) new _t _a
91 #define Delete(_p) delete _p
93 #else /* NO_DEBUG_ALLOC */
95 #define Allocate(_s) MemMgr_AllocateMemory (_s, #_s, __FILE__, __LINE__)
96 #define Free(_p) MemMgr_FreeMemory (_p, __FILE__, __LINE__)
97 #define New(_t) (_t*)MemMgr_TrackNew (new _t, sizeof(_t), #_t, __FILE__, __LINE__)
98 #define New2(_t,_a) (_t*)MemMgr_TrackNew (new _t _a, sizeof(_t), #_t, __FILE__, __LINE__)
99 #define Delete(_p) do { MemMgr_TrackDelete (_p, __FILE__, __LINE__); delete _p; } while(0)
101 #endif /* NO_DEBUG_ALLOC */
105 * PROTOTYPES _________________________________________________________________
111 #define ShowMemoryManager()
112 #define WhileMemoryManagerShowing()
113 #define IsMemoryManagerMessage(_pm) FALSE
117 void MEMMGR_CALLCONV ShowMemoryManager (void);
118 void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
119 BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
121 #ifndef NO_DEBUG_ALLOC
123 PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
124 void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPSTR pszFile, DWORD dwLine);
126 PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
127 void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPSTR pszFile, DWORD dwLine);
129 #endif /* NO_DEBUG_ALLOC */