Windows: fix checked UNICODE build of talocale
[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 #ifndef EXPORTED
87 #define EXPORTED __declspec(dllexport)
88 #endif
89
90 /*
91  * MACROS _____________________________________________________________________
92  *
93  * These definitions hide or expose instrumentation based on your
94  * compilation flags.
95  *
96  */
97
98 #ifdef NO_DEBUG_ALLOC
99
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
105
106 #else /* NO_DEBUG_ALLOC */
107
108 #define Allocate(_s)   MemMgr_AllocateMemory (_s, TEXT(#_s), TEXT(__FILE__), __LINE__)
109 #define Free(_p)       MemMgr_FreeMemory (_p, TEXT(__FILE__), __LINE__)
110 #define New(_t)        (_t*)MemMgr_TrackNew (new _t, sizeof(_t), TEXT(#_t), TEXT(__FILE__), __LINE__)
111 #define New2(_t,_a)    (_t*)MemMgr_TrackNew (new _t _a, sizeof(_t), TEXT(#_t), TEXT(__FILE__), __LINE__)
112 #define Delete(_p)     do { MemMgr_TrackDelete (_p, TEXT(__FILE__), __LINE__); delete _p; } while(0)
113
114 #endif /* NO_DEBUG_ALLOC */
115
116
117 /*
118  * PROTOTYPES _________________________________________________________________
119  *
120  */
121
122 #ifndef DEBUG
123
124 #define ShowMemoryManager()
125 #define WhileMemoryManagerShowing()
126 #define IsMemoryManagerMessage(_pm) FALSE
127
128 #else /* DEBUG */
129
130 EXPORTED void MEMMGR_CALLCONV ShowMemoryManager (void);
131 EXPORTED void MEMMGR_CALLCONV WhileMemoryManagerShowing (void);
132 EXPORTED BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg);
133
134 #ifndef NO_DEBUG_ALLOC
135
136 EXPORTED PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPTSTR pszExpr, LPTSTR pszFile, DWORD dwLine);
137 EXPORTED void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPTSTR pszFile, DWORD dwLine);
138
139 EXPORTED PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPTSTR pszExpr, LPTSTR pszFile, DWORD dwLine);
140 EXPORTED void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPTSTR pszFile, DWORD dwLine);
141
142 #endif /* NO_DEBUG_ALLOC */
143
144 #endif /* DEBUG */
145
146
147 #endif /* ALLOC_H */