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 #define EXPORTED __declspec(dllexport)
91 * MACROS _____________________________________________________________________
93 * These definitions hide or expose instrumentation based on your
100 #define Allocate(_s) ((PVOID)GlobalAlloc(GMEM_FIXED,_s))
101 #define Free(_p) GlobalFree((HGLOBAL)(_p))
102 #define New(_t) new _t
103 #define New2(_t,_a) new _t _a
104 #define Delete(_p) delete _p
106 #else /* NO_DEBUG_ALLOC */
108 #define Allocate(_s) MemMgr_AllocateMemory (_s, #_s, __FILE__, __LINE__)
109 #define Free(_p) MemMgr_FreeMemory (_p, __FILE__, __LINE__)
110 #define New(_t) (_t*)MemMgr_TrackNew (new _t, sizeof(_t), #_t, __FILE__, __LINE__)
111 #define New2(_t,_a) (_t*)MemMgr_TrackNew (new _t _a, sizeof(_t), #_t, __FILE__, __LINE__)
112 #define Delete(_p) do { MemMgr_TrackDelete (_p, __FILE__, __LINE__); delete _p; } while(0)
114 #endif /* NO_DEBUG_ALLOC */
118 * PROTOTYPES _________________________________________________________________
124 #define ShowMemoryManager()
125 #define WhileMemoryManagerShowing()
126 #define IsMemoryManagerMessage(_pm) FALSE
130 EXPORTED void MEMMGR_CALLCONV ShowMemoryManager (void);
131 EXPORTED void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
132 EXPORTED BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
134 #ifndef NO_DEBUG_ALLOC
136 EXPORTED PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
137 EXPORTED void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPSTR pszFile, DWORD dwLine);
139 EXPORTED PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine);
140 EXPORTED void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPSTR pszFile, DWORD dwLine);
142 #endif /* NO_DEBUG_ALLOC */