Source code of Windows XP (NT5)
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.

310 lines
7.5 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: MemUtil.h
  6. // Author: Charles Ma, 10/13/2000
  7. //
  8. // Revision History:
  9. //
  10. //
  11. //
  12. // Description:
  13. //
  14. // IU memory utility library
  15. //
  16. //=======================================================================
  17. #ifndef __MEM_UTIL_HEADER__
  18. #include <ole2.h>
  19. //
  20. // declare a class that handles the heap memory smartly, free itself.
  21. // you should not use this class directly. Use the macros defined
  22. // below this class instead.
  23. //
  24. class CSmartHeapMem
  25. {
  26. public:
  27. //
  28. // constructor/destructor
  29. //
  30. CSmartHeapMem();
  31. ~CSmartHeapMem();
  32. LPVOID Alloc(
  33. size_t nBytes,
  34. DWORD dwFlags = HEAP_ZERO_MEMORY
  35. );
  36. LPVOID ReAlloc(
  37. LPVOID lpMem,
  38. size_t nBytes,
  39. DWORD dwFlags = HEAP_ZERO_MEMORY
  40. );
  41. size_t Size(
  42. LPVOID lpMem
  43. );
  44. void FreeAllocatedMem(
  45. LPVOID lpMem
  46. );
  47. private:
  48. HANDLE m_Heap;
  49. LPVOID* m_lppMems;
  50. size_t m_ArraySize;
  51. int GetUnusedArraySlot();
  52. inline int FindIndex(LPVOID pMem);
  53. };
  54. // *******************************************************************************
  55. //
  56. // MACROs to utlize class CSmartHeapMem to provide you a "smart pointer"
  57. // type of memory management based on Heap memory.
  58. //
  59. // Restriction:
  60. // HEAP_GENERATE_EXCEPTIONS and HEAP_NO_SERIALIZE flags are ignored
  61. //
  62. // *******************************************************************************
  63. //
  64. // similar to ATL USES_CONVERSION, this macro declares
  65. // that within this block you want to use CSmartHeapMem feature
  66. //
  67. #define USES_MY_MEMORY CSmartHeapMem mem;
  68. //
  69. // allocate a pc of memory, e.g.:
  70. // LPTSTR t = (LPTSTR) MemAlloc(30*sizeof(TCHAR));
  71. //
  72. #define MemAlloc mem.Alloc
  73. //
  74. // re-allocate a pc of memory, e.g.:
  75. // t = (LPTSTR) MemReAlloc(t, MemSize(t) * 2, HEAP_REALLOC_IN_PLACE_ONLY);
  76. //
  77. #define MemReAlloc mem.ReAlloc
  78. //
  79. // macro to return the memory size allocated:
  80. // size_t nBytes = MemSize(t);
  81. //
  82. #define MemSize mem.Size
  83. //
  84. // macro to free a pc of memory allocated by MemAlloc or MemReAlloc, e.g.:
  85. // MemFree(t);
  86. // You only need to do this when you want to re-use this pointer to
  87. // call MemAlloc() repeatedly, such as, in a loop. In normal cases,
  88. // memory allocated by these two macros will be freed automatically
  89. // when control goes out of the current scope.
  90. //
  91. #define MemFree mem.FreeAllocatedMem
  92. #define SafeMemFree(p) if (NULL != p) { MemFree(p); p = NULL; }
  93. // *******************************************************************************
  94. //
  95. // Duplicate USES_CONVERSION, but remove dependency on
  96. // CRT memory function_alloca()
  97. //
  98. // *******************************************************************************
  99. #define USES_IU_CONVERSION int _convert = 0; \
  100. _convert; UINT _acp = CP_ACP; _acp; \
  101. USES_MY_MEMORY; \
  102. LPCWSTR _lpw = NULL; _lpw; LPCSTR _lpa = NULL; _lpa
  103. //
  104. // NTRAID#NTBUG9-260079-2001/03/08-waltw PREFIX: Dereferencing NULL lpw.
  105. // NTRAID#NTBUG9-260080-2001/03/08-waltw PREFIX: Dereferencing NULL lpw.
  106. //
  107. inline LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars, UINT acp)
  108. {
  109. //
  110. // verify that no illegal character present
  111. // since lpw was allocated based on the size of lpa
  112. // don't worry about the number of chars
  113. //
  114. if (lpw)
  115. {
  116. lpw[0] = '\0';
  117. MultiByteToWideChar(acp, 0, lpa, -1, lpw, nChars);
  118. }
  119. return lpw;
  120. }
  121. //
  122. // NTRAID#NTBUG9-260083-2001/03/08-waltw PREFIX: Dereferencing NULL lpa.
  123. //
  124. inline LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, UINT acp)
  125. {
  126. //
  127. // verify that no illegal character present
  128. // since lpa was allocated based on the size of lpw
  129. // don't worry about the number of chars
  130. //
  131. if (lpa)
  132. {
  133. lpa[0] = '\0';
  134. WideCharToMultiByte(acp, 0, lpw, -1, lpa, nChars, NULL, NULL);
  135. }
  136. return lpa;
  137. }
  138. inline LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  139. {
  140. return AtlA2WHelper(lpw, lpa, nChars, CP_ACP);
  141. }
  142. inline LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars)
  143. {
  144. return AtlW2AHelper(lpa, lpw, nChars, CP_ACP);
  145. }
  146. #ifdef _CONVERSION_USES_THREAD_LOCALE
  147. #ifdef ATLA2WHELPER
  148. #undef ATLA2WHELPER
  149. #undef ATLW2AHELPER
  150. #endif
  151. #define ATLA2WHELPER AtlA2WHelper
  152. #define ATLW2AHELPER AtlW2AHelper
  153. #else
  154. #ifndef ATLA2WHELPER
  155. #define ATLA2WHELPER AtlA2WHelper
  156. #define ATLW2AHELPER AtlW2AHelper
  157. #endif
  158. #endif
  159. #define A2W(lpa) (\
  160. ((_lpa = lpa) == NULL) ? NULL : (\
  161. _convert = (lstrlenA(_lpa)+1),\
  162. AtlA2WHelper((LPWSTR)MemAlloc(_convert*2), _lpa, _convert, CP_ACP)))
  163. #define W2A(lpw) (\
  164. ((_lpw = lpw) == NULL) ? NULL : (\
  165. _convert = (lstrlenW(_lpw)+1)*2,\
  166. AtlW2AHelper((LPSTR)MemAlloc(_convert), _lpw, _convert, CP_ACP)))
  167. #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
  168. #define W2CA(lpw) ((LPCSTR)W2A(lpw))
  169. #ifdef OLE2ANSI
  170. inline LPOLESTR A2OLE(LPSTR lp) { return lp;}
  171. inline LPSTR OLE2A(LPOLESTR lp) { return lp;}
  172. #define W2OLE W2A
  173. #define OLE2W A2W
  174. inline LPCOLESTR A2COLE(LPCSTR lp) { return lp;}
  175. inline LPCSTR OLE2CA(LPCOLESTR lp) { return lp;}
  176. #define W2COLE W2CA
  177. #define OLE2CW A2CW
  178. #else
  179. inline LPOLESTR W2OLE(LPWSTR lp) { return lp; }
  180. inline LPWSTR OLE2W(LPOLESTR lp) { return lp; }
  181. #define A2OLE A2W
  182. #define OLE2A W2A
  183. inline LPCOLESTR W2COLE(LPCWSTR lp) { return lp; }
  184. inline LPCWSTR OLE2CW(LPCOLESTR lp) { return lp; }
  185. #define A2COLE A2CW
  186. #define OLE2CA W2CA
  187. #endif
  188. #ifdef _UNICODE
  189. #define T2A W2A
  190. #define A2T A2W
  191. inline LPWSTR T2W(LPTSTR lp) { return lp; }
  192. inline LPTSTR W2T(LPWSTR lp) { return lp; }
  193. #define T2CA W2CA
  194. #define A2CT A2CW
  195. inline LPCWSTR T2CW(LPCTSTR lp) { return lp; }
  196. inline LPCTSTR W2CT(LPCWSTR lp) { return lp; }
  197. #else
  198. #define T2W A2W
  199. #define W2T W2A
  200. inline LPSTR T2A(LPTSTR lp) { return lp; }
  201. inline LPTSTR A2T(LPSTR lp) { return lp; }
  202. #define T2CW A2CW
  203. #define W2CT W2CA
  204. inline LPCSTR T2CA(LPCTSTR lp) { return lp; }
  205. inline LPCTSTR A2CT(LPCSTR lp) { return lp; }
  206. #endif
  207. #define OLE2T W2T
  208. #define OLE2CT W2CT
  209. #define T2OLE T2W
  210. #define T2COLE T2CW
  211. inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
  212. {
  213. USES_IU_CONVERSION;
  214. BSTR str = NULL;
  215. int nConvertedLen = MultiByteToWideChar(_acp, 0, lp,
  216. nLen, NULL, NULL)-1;
  217. str = ::SysAllocStringLen(NULL, nConvertedLen);
  218. if (str != NULL)
  219. {
  220. MultiByteToWideChar(_acp, 0, lp, -1,
  221. str, nConvertedLen);
  222. }
  223. return str;
  224. }
  225. inline BSTR OLE2BSTR(LPCOLESTR lp) {return ::SysAllocString(lp);}
  226. #if defined(_UNICODE)
  227. // in these cases the default (TCHAR) is the same as OLECHAR
  228. inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  229. inline BSTR A2BSTR(LPCSTR lp) {USES_IU_CONVERSION; return A2WBSTR(lp);}
  230. inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  231. #elif defined(OLE2ANSI)
  232. // in these cases the default (TCHAR) is the same as OLECHAR
  233. inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  234. inline BSTR A2BSTR(LPCSTR lp) {return ::SysAllocString(lp);}
  235. inline BSTR W2BSTR(LPCWSTR lp) {USES_IU_CONVERSION; return ::SysAllocString(W2COLE(lp));}
  236. #else
  237. inline BSTR T2BSTR(LPCTSTR lp) {USES_IU_CONVERSION; return A2WBSTR(lp);}
  238. inline BSTR A2BSTR(LPCSTR lp) {USES_IU_CONVERSION; return A2WBSTR(lp);}
  239. inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  240. #endif
  241. // *******************************************************************************
  242. //
  243. // Other memory related functions
  244. //
  245. // *******************************************************************************
  246. //
  247. // implemenation of CRT memcpy() function
  248. //
  249. LPVOID MyMemCpy(LPVOID dest, const LPVOID src, size_t nBytes);
  250. //
  251. // allocate heap mem and copy
  252. //
  253. LPVOID HeapAllocCopy(LPVOID src, size_t nBytes);
  254. #define __MEM_UTIL_HEADER__
  255. #endif //__MEM_UTIL_HEADER__