winnt-build-cleanup-20030825
[openafs.git] / src / WINNT / talocale / tal_alloc.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Alloc - Memory instrumentation apparatus
12  *
13  */
14
15 #ifndef ALLOC_H
16 #define ALLOC_H
17
18 /*
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.
24  *
25  * EXAMPLES ___________________________________________________________________
26  *
27  * The easiest memory allocation method is:
28  *
29  *    LPTSTR psz = Allocate (sizeof(TCHAR) * 256);
30  *    Free (psz);
31  *
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.
37  *
38  * You can construct and destroy C++ objects in the same way:
39  *
40  *    LPTSTR psz = New (TCHAR[256]);
41  *    Delete(psz);
42  *
43  * For complex constructors, you may need to use the New2 macro:
44  *
45  *    OBJECT *pObject = New2 (OBJECT,(15,242));
46  *    Delete(pObject);
47  *
48  */
49
50 /*
51  * DEFINITIONS ________________________________________________________________
52  *
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.
56  *
57  */
58
59 #ifndef DEBUG
60 #ifndef NO_DEBUG_ALLOC
61 #define NO_DEBUG_ALLOC
62 #endif
63 #endif
64
65 /*
66  * If you put this package as part of a .DLL, that .DLL will need to
67  * add these functions to its .DEF file:
68  *
69  *    ShowMemoryManager
70  *    WhileMemoryManagerShowing
71  *    MemMgr_AllocateMemory
72  *    MemMgr_FreeMemory
73  *    MemMgr_TrackNew
74  *    MemMgr_TrackDelete
75  *
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.
79  *
80  */
81
82 #ifndef MEMMGR_CALLCONV
83 #define MEMMGR_CALLCONV _cdecl
84 #endif
85
86 /*
87  * MACROS _____________________________________________________________________
88  *
89  * These definitions hide or expose instrumentation based on your
90  * compilation flags.
91  *
92  */
93
94 #ifdef NO_DEBUG_ALLOC
95
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
101
102 #else /* NO_DEBUG_ALLOC */
103
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)
109
110 #endif /* NO_DEBUG_ALLOC */
111
112
113 /*
114  * PROTOTYPES _________________________________________________________________
115  *
116  */
117
118 #ifndef DEBUG
119
120 #define ShowMemoryManager()
121 #define WhileMemoryManagerShowing()
122 #define IsMemoryManagerMessage(_pm) FALSE
123
124 #else /* DEBUG */
125
126 void MEMMGR_CALLCONV ShowMemoryManager (void);
127 void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
128 BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
129
130 #ifndef NO_DEBUG_ALLOC
131
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);
134
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);
137
138 #endif /* NO_DEBUG_ALLOC */
139
140 #endif /* DEBUG */
141
142
143 #endif /* ALLOC_H */