2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Alloc - Memory instrumentation apparatus
19 * The first step in instrumenting memory management code is to replace
20 * your allocation and deallocation statements, and augment "new" and "delete"
21 * commands. Once this instrumentation is in place, your application can
22 * call the ShowMemoryManager() routine, which provides (on a managed
23 * background thread) an interactive window showing current memory usage.
25 * EXAMPLES ___________________________________________________________________
27 * The easiest memory allocation method is:
29 * LPTSTR psz = Allocate (sizeof(TCHAR) * 256);
32 * Internally, Allocate() uses GlobalAlloc() to actually allocate the
33 * requested chunk of memory (plus additional space at the end of the chunk,
34 * for a trailing signature). Free() calls GlobalFree() to release the chunk,
35 * after validating the trailing signature. The two routines also allow
36 * the memory manager to keep track of where memory is allocated and freed.
38 * You can construct and destroy C++ objects in the same way:
40 * LPTSTR psz = New (TCHAR[256]);
43 * For complex constructors, you may need to use the New2 macro:
45 * OBJECT *pObject = New2 (OBJECT,(15,242));
51 * DEFINITIONS ________________________________________________________________
53 * You can turn off instrumentation for a particular source file by
54 * undefining DEBUG for that file. If you want -DDEBUG but still don't
55 * want instrumentation, you can explicitly define NO_DEBUG_ALLOC.
60 #ifndef NO_DEBUG_ALLOC
61 #define NO_DEBUG_ALLOC
66 * If you put this package as part of a .DLL, that .DLL will need to
67 * add these functions to its .DEF file:
70 * WhileMemoryManagerShowing
71 * MemMgr_AllocateMemory
76 * You can change MEMMGR_CALLCONV in your .DLL to be _declspec(dllexport),
77 * and in your other packages to _declspec(dllimport), to ensure that all
78 * modules use the same instance of this package.
82 #ifndef MEMMGR_CALLCONV
83 #define MEMMGR_CALLCONV _cdecl
87 * MACROS _____________________________________________________________________
89 * These definitions hide or expose instrumentation based on your
96 #define Allocate(_s) ((PVOID)GlobalAlloc(GMEM_FIXED,_s))
97 #define Free(_p) GlobalFree((HGLOBAL)(_p))
98 #define New(_t) new _t
99 #define New2(_t,_a) new _t _a
100 #define Delete(_p) delete _p
102 #else /* NO_DEBUG_ALLOC */
104 #define Allocate(_s) MemMgr_AllocateMemory (_s, #_s, __FILE__, __LINE__)
105 #define Free(_p) MemMgr_FreeMemory (_p, __FILE__, __LINE__)
106 #define New(_t) (_t*)MemMgr_TrackNew (new _t, sizeof(_t), #_t, __FILE__, __LINE__)
107 #define New2(_t,_a) (_t*)MemMgr_TrackNew (new _t _a, sizeof(_t), #_t, __FILE__, __LINE__)
108 #define Delete(_p) do { MemMgr_TrackDelete (_p, __FILE__, __LINE__); delete _p; } while(0)
110 #endif /* NO_DEBUG_ALLOC */
114 * PROTOTYPES _________________________________________________________________
120 #define ShowMemoryManager()
121 #define WhileMemoryManagerShowing()
122 #define IsMemoryManagerMessage(_pm) FALSE
126 void MEMMGR_CALLCONV ShowMemoryManager (void);
127 void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
128 BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
130 #ifndef NO_DEBUG_ALLOC
132 PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
133 void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPSTR pszFile, DWORD dwLine);
135 PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
136 void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPSTR pszFile, DWORD dwLine);
138 #endif /* NO_DEBUG_ALLOC */