Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / talocale / tal_alloc.h
1 /*
2  * Alloc - Memory instrumentation apparatus
3  *
4  */
5
6 #ifndef ALLOC_H
7 #define ALLOC_H
8
9 /*
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.
15  *
16  * EXAMPLES ___________________________________________________________________
17  *
18  * The easiest memory allocation method is:
19  *
20  *    LPTSTR psz = Allocate (sizeof(TCHAR) * 256);
21  *    Free (psz);
22  *
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.
28  *
29  * You can construct and destroy C++ objects in the same way:
30  *
31  *    LPTSTR psz = New (TCHAR[256]);
32  *    Delete(psz);
33  *
34  * For complex constructors, you may need to use the New2 macro:
35  *
36  *    OBJECT *pObject = New2 (OBJECT,(15,242));
37  *    Delete(pObject);
38  *
39  */
40
41 /*
42  * DEFINITIONS ________________________________________________________________
43  *
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.
47  *
48  */
49
50 #ifndef DEBUG
51 #ifndef NO_DEBUG_ALLOC
52 #define NO_DEBUG_ALLOC
53 #endif
54 #endif
55
56 /*
57  * If you put this package as part of a .DLL, that .DLL will need to
58  * add these functions to its .DEF file:
59  *
60  *    ShowMemoryManager
61  *    WhileMemoryManagerShowing
62  *    MemMgr_AllocateMemory
63  *    MemMgr_FreeMemory
64  *    MemMgr_TrackNew
65  *    MemMgr_TrackDelete
66  *
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.
70  *
71  */
72
73 #ifndef MEMMGR_CALLCONV
74 #define MEMMGR_CALLCONV _cdecl
75 #endif
76
77 /*
78  * MACROS _____________________________________________________________________
79  *
80  * These definitions hide or expose instrumentation based on your
81  * compilation flags.
82  *
83  */
84
85 #ifdef NO_DEBUG_ALLOC
86
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
92
93 #else /* NO_DEBUG_ALLOC */
94
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)
100
101 #endif /* NO_DEBUG_ALLOC */
102
103
104 /*
105  * PROTOTYPES _________________________________________________________________
106  *
107  */
108
109 #ifndef DEBUG
110
111 #define ShowMemoryManager()
112 #define WhileMemoryManagerShowing()
113 #define IsMemoryManagerMessage(_pm) FALSE
114
115 #else /* DEBUG */
116
117 void MEMMGR_CALLCONV ShowMemoryManager (void);
118 void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
119 BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
120
121 #ifndef NO_DEBUG_ALLOC
122
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);
125
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);
128
129 #endif /* NO_DEBUG_ALLOC */
130
131 #endif /* DEBUG */
132
133
134 #endif /* ALLOC_H */