Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.3 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // File: TRANSMEM.H
  4. //
  5. // Copyright Microsoft Corporation 1997, All Rights Reserved.
  6. //
  7. // Owner: NIKOS
  8. //
  9. // Description: This file contains memory routines and macros for using
  10. // EXCHMEM as a dynamic memory allocator. If your object can
  11. // be made fixed in size, it may be more appropriate to use
  12. // CPool especially if your object is allocated/freed often.
  13. //
  14. // Note: CPool never releases (frees) objects, so some sort of
  15. // free such objects may also be needed.
  16. //
  17. // Modified 2/98 by mikeswa - Added Multi-heap support
  18. //-----------------------------------------------------------------------------
  19. #ifndef __TRANSMEM_H__
  20. #define __TRANSMEM_H__
  21. #include <exchmem.h>
  22. #include <cpool.h>
  23. #define HEAP_LOW_MEMORY_RESERVE 65536 // to be freed when we're low on memory
  24. //define number of exchmem heaps if not already defined
  25. #ifndef NUM_EXCHMEM_HEAPS
  26. #define NUM_EXCHMEM_HEAPS 0
  27. #endif //NUM_EXCHMEM_HEAPS
  28. //
  29. // These three globals:
  30. //
  31. // HANDLE g_hTransHeap = NULL;
  32. //
  33. // must be declared somewhere in a C file so things will link properly. The macros
  34. // declared use these to store heap handles, etc. to make things work.
  35. //
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. extern HANDLE g_hTransHeap;
  40. #ifdef __cplusplus
  41. }
  42. #endif
  43. //
  44. // TrHeapCreate needs to be called once at startup time to initialize Exchmem and create
  45. // the heap.
  46. //
  47. #ifdef __cplusplus
  48. __inline BOOL TrHeapCreate(DWORD dwFlags=0, DWORD dwInitialSize=1024000, DWORD dwMaxSize=0)
  49. #else
  50. __inline BOOL TrHeapCreate(DWORD dwFlags, DWORD dwInitialSize, DWORD dwMaxSize)
  51. #endif
  52. {
  53. if (g_hTransHeap)
  54. return FALSE;
  55. g_hTransHeap = ExchMHeapCreate(NUM_EXCHMEM_HEAPS, dwFlags, dwInitialSize, dwMaxSize);
  56. if (g_hTransHeap)
  57. return TRUE;
  58. else
  59. return FALSE;
  60. }
  61. //
  62. // TrHeapDestroy() needs to be called once at shutdown time to free the heap and it's contents.
  63. //
  64. // Note: Because the heap is destroyed before the module is finished unloading, all objects that
  65. // allocated memory must be destroyed (with delete) before the module is unloaded. If not
  66. // done, nasty crashes will result. This is a BAD thing to do:
  67. //
  68. // CObject g_Object;
  69. //
  70. // CObject::~CObject()
  71. // {
  72. // if (NULL != m_pBuffer)
  73. // {
  74. // TrFree(m_pBuffer);
  75. // m_pBuffer = NULL;
  76. // }
  77. // }
  78. //
  79. // since ~CObject() will be called AFTER TrHeapDestroy, and TrFree will be called on a (destroyed) heap.
  80. //
  81. __inline BOOL TrHeapDestroy(void)
  82. {
  83. BOOL b = TRUE;
  84. if (g_hTransHeap)
  85. {
  86. b = ExchMHeapDestroy();
  87. g_hTransHeap = NULL;
  88. }
  89. return b;
  90. }
  91. //
  92. // TrCalloc: replacement for calloc()
  93. //
  94. __inline void * TrCalloc(unsigned int x, unsigned int y, char * szFile = __FILE__, unsigned int uiLine = __LINE__)
  95. {
  96. return g_hTransHeap ? ExchMHeapAllocDebug(x*y, szFile, uiLine) : NULL;
  97. }
  98. //
  99. // TrFree: replacement for free()
  100. __inline void TrFree(void *pv)
  101. {
  102. if (g_hTransHeap)
  103. {
  104. ExchMHeapFree(pv);
  105. }
  106. else
  107. {
  108. // Our allocs / frees are out of sync.
  109. #ifdef DEBUG
  110. DebugBreak();
  111. #endif
  112. }
  113. }
  114. // TrMalloc: replacement for malloc()
  115. __inline void * TrMalloc(unsigned int size, char * szFile = __FILE__, unsigned int uiLine = __LINE__)
  116. {
  117. return g_hTransHeap ? ExchMHeapAllocDebug(size, szFile, uiLine) : NULL;
  118. }
  119. // TrRealloc: replacement for realloc()
  120. __inline void * TrRealloc(void *pv, unsigned int size, char * szFile = __FILE__, unsigned int uiLine = __LINE__)
  121. {
  122. return g_hTransHeap ? ExchMHeapReAllocDebug(pv, size, szFile, uiLine) : NULL;
  123. }
  124. #ifdef __cplusplus
  125. #define TransCONST const
  126. #else
  127. #define TransCONST
  128. #endif
  129. // TrStrdupW: replacement for wcsdup()
  130. __inline LPWSTR TrStrdupW(TransCONST LPWSTR pwszString)
  131. {
  132. LPWSTR pwszTmp = NULL;
  133. if (NULL == g_hTransHeap || NULL == pwszString)
  134. return NULL;
  135. pwszTmp = (LPWSTR) ExchMHeapAlloc((wcslen(pwszString) + 1) * sizeof(WCHAR));
  136. if (NULL != pwszTmp)
  137. wcscpy(pwszTmp,pwszString);
  138. return pwszTmp;
  139. }
  140. // TrStrdupA: replacement for strdup()
  141. __inline LPSTR TrStrdupA(TransCONST LPSTR pszString)
  142. {
  143. LPSTR pszTmp = NULL;
  144. if (NULL == g_hTransHeap || NULL == pszString)
  145. return NULL;
  146. pszTmp = (LPSTR) ExchMHeapAlloc((strlen(pszString) + 1) * sizeof(CHAR));
  147. if (NULL != pszTmp)
  148. strcpy(pszTmp,pszString);
  149. return pszTmp;
  150. }
  151. #ifdef _UNICODE
  152. #define TrStrdup(x) TrStrdupW(x)
  153. #else
  154. #define TrStrdup(x) TrStrdupA(x)
  155. #endif
  156. //
  157. // Please use the pv* macros... defined here allocators may change over time and this will
  158. // make it easy to change when needed.
  159. //
  160. #define pvMalloc(x) TrMalloc(x, __FILE__, __LINE__)
  161. #define FreePv(x) TrFree(x)
  162. #define pvCalloc(x,y) TrCalloc(x,y, __FILE__, __LINE__)
  163. #define pszStrdup(x) TrStrdup(x)
  164. #define pvRealloc(pv,size) TrRealloc(pv, size, __FILE__, __LINE__)
  165. #ifdef __cplusplus
  166. // Replacement for the default new() operator
  167. __inline void * __cdecl operator new(size_t stAllocateBlock)
  168. {
  169. return TrMalloc( stAllocateBlock );
  170. }
  171. // Replacement for the default new() operator that allows
  172. //specification of file and line #
  173. // To use this allocator as your default allocator, simply use the following:
  174. //#define new TRANSMEM_NEW
  175. //NOTE: You must be careful when you redefine this macro... it may cause
  176. //problems with overloaded new operators (a la CPOOL or STL).
  177. #define TRANSMEM_NEW new(__FILE__, __LINE)
  178. __inline void * __cdecl operator new(size_t stAllocateBlock, char * szFile, unsigned int uiLine)
  179. {
  180. return TrMalloc( stAllocateBlock, szFile, uiLine );
  181. }
  182. // Replacement for the default delete() operator
  183. __inline void __cdecl operator delete( void *pvMem )
  184. {
  185. FreePv( pvMem );
  186. }
  187. #endif
  188. // Convenient macro to set the pointer you freed to NULL as well
  189. #define TRFREE(x) \
  190. if (NULL != x) \
  191. { \
  192. FreePv(x); \
  193. x = NULL; \
  194. }
  195. // Convenient macro to set the pointer to the object to NULL as well
  196. #define TRDELETE(x) \
  197. if (NULL != x) \
  198. { \
  199. delete x; \
  200. x = NULL; \
  201. }
  202. #endif /* __TRANSMEM_H__ */