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.

491 lines
15 KiB

  1. /*
  2. * glheap.h
  3. *
  4. * Implementation of global and local heaps
  5. *
  6. * Copyright (C) 1994 Microsoft Corporation
  7. */
  8. #ifndef __GLHEAP_H_
  9. #define __GLHEAP_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /* Windows 95 Implementation -------------------------------------------------- */
  14. #ifdef CHICAGO
  15. #define GH_POINTERS_VALID
  16. typedef DWORD GHNAME, * PGHNAME, ** PPGHNAME;
  17. typedef DWORD GHID, * PGHID, ** PPGHID;
  18. typedef struct GHDR GHDR, * PGHDR, ** PPGHDR;
  19. typedef struct GH GH, * PGH, ** PPGH;
  20. typedef PGH _HGH;
  21. typedef HANDLE _HLH;
  22. struct GHDR {
  23. PGHDR pghdrNext; // Pointer to next heap
  24. HANDLE hHeap; // Handle to the heap
  25. GHNAME ghname; // Name of the heap
  26. GHID ghidRoot; // Client root heap block
  27. ULONG cRef; // Number of active clients
  28. };
  29. struct GH {
  30. HANDLE hHeap; // Handle to the heap
  31. HANDLE hMutex; // Handle to mutex for this heap
  32. PGHDR pghdr; // Pointer to the heap header block
  33. #ifdef DEBUG
  34. UINT cMutex; // Mutex entry count
  35. #endif
  36. };
  37. __inline void HeapFreeZ(HANDLE hHeap, LPVOID pv)
  38. {
  39. if (pv)
  40. HeapFree(hHeap, 0, pv);
  41. }
  42. _HGH _GH_Open(BOOL fCreate, GHNAME ghname, DWORD dwMaxHeap);
  43. void _GH_Close(_HGH hgh);
  44. #define _GH_GetRoot(hgh) ((hgh)->pghdr->ghidRoot)
  45. #define _GH_SetRoot(hgh, ghid) ((hgh)->pghdr->ghidRoot = (ghid))
  46. #define _GH_GetName(hgh) ((hgh)->pghdr->ghname)
  47. #define _GH_GetPv(hgh, ghid) ((LPVOID)(ghid))
  48. #define _GH_GetId(hgh, pv) ((GHID)(pv))
  49. #define _GH_GetSize(hgh, ghid) HeapSize((hgh)->hHeap, 0, (LPVOID)(ghid))
  50. #define _GH_Alloc(hgh, cb) ((GHID)HeapAlloc((hgh)->hHeap, 0, cb))
  51. #define _GH_Realloc(hgh, ghid, cb) ((GHID)HeapReAlloc((hgh)->hHeap, 0, (LPVOID)(ghid), cb))
  52. #define _GH_Free(hgh, ghid) HeapFreeZ((hgh)->hHeap, (LPVOID)(ghid))
  53. #ifdef DEBUG
  54. BOOL _GH_WaitForMutex(_HGH hgh, ULONG ulTimeout);
  55. void _GH_ReleaseMutex(_HGH hgh);
  56. #else
  57. #define _GH_WaitForMutex(hgh, ulT) GH_WaitForSingleObject(hgh->hMutex, ulT)
  58. #ifdef __cplusplus
  59. #define _GH_ReleaseMutex(hgh) ::ReleaseMutex((hgh)->hMutex)
  60. #else
  61. #define _GH_ReleaseMutex(hgh) ReleaseMutex((hgh)->hMutex)
  62. #endif
  63. #endif
  64. #define _LH_Open(dwMaxHeap) HeapCreate(0, 0, dwMaxHeap)
  65. #define _LH_Close(hlh) HeapDestroy(hlh)
  66. #define _LH_Alloc(hlh, cb) HeapAlloc(hlh, 0, cb)
  67. #define _LH_Realloc(hlh, pv, cb) HeapReAlloc(hlh, 0, pv, cb)
  68. #define _LH_GetSize(hlh, pv) HeapSize(hlh, 0, pv)
  69. #define _LH_Free(hlh, pv) HeapFreeZ(hlh, pv)
  70. #endif
  71. /* Win16 Implementation ---------------------------------------------------- */
  72. #ifdef WIN16
  73. #define GH_POINTERS_VALID
  74. typedef __segment HPH, * PHPH, ** PPHPH;
  75. typedef DWORD HPID, * PHPID, ** PPHPID;
  76. typedef DWORD GHNAME, * PGHNAME, ** PPGHNAME;
  77. typedef HPID GHID, * PGHID, ** PPGHID;
  78. typedef HPH _HGH;
  79. typedef HPH _HLH;
  80. typedef struct HP {
  81. HPH hphRoot; // Pointer to root heap
  82. HPH hphPrev; // Pointer to the previous heap (fShared)
  83. HPH hphNext; // Pointer to next heap (fShared)
  84. HPH hphChild; // Pointer to extended heaps
  85. BOOL fShared; // TRUE if heap is shared across processes
  86. GHNAME ghname; // Name of the shared heap (fShared)
  87. GHID ghidRoot; // Client root heap block
  88. DWORD dwCurHeap; // Current size of the heap
  89. DWORD dwMaxHeap; // Maximum size of the heap
  90. UINT cRef; // Number of active clients
  91. UINT cbHeap; // Size of this heap
  92. UINT cbFree; // Maximum contiguous free bytes in heap
  93. } HP, * PHP;
  94. #define HphToPhp(hph) ((PHP)((ULONG)(hph) << 16))
  95. #define HP_CREATE 0x0001
  96. #define HP_SHARED 0x0002
  97. HPH HP_Open(UINT uiFlags, GHNAME ghname, DWORD dwMaxHeap);
  98. void HP_Close(HPH hph);
  99. HPID HP_Alloc(HPH hph, UINT cb);
  100. HPID HP_Realloc(HPH hph, HPID hpid, UINT cb);
  101. void HP_Free(HPH hph, HPID hpid);
  102. #define HP_GetSize(hgh, hpid) (*((UINT *)(hpid) - 2))
  103. #define _GH_Open(fCreate, ghname, dwMaxHeap) \
  104. HP_Open(HP_SHARED | !!(fCreate), ghname, dwMaxHeap)
  105. #define _GH_Close(hgh) HP_Close(hgh)
  106. #define _GH_GetRoot(hgh) (HphToPhp(hgh)->ghidRoot)
  107. #define _GH_SetRoot(hgh, ghid) (HphToPhp(hgh)->ghidRoot = (ghid))
  108. #define _GH_GetName(hgh) (HphToPhp(hgh)->ghname)
  109. #define _GH_GetPv(hgh, ghid) ((LPVOID)(ghid))
  110. #define _GH_GetId(hgh, pv) ((GHID)(pv))
  111. #define _GH_GetSize(hgh, ghid) HP_GetSize(hgh, ghid)
  112. #define _GH_Alloc(hgh, cb) ((GHID)HP_Alloc(hgh, cb))
  113. #define _GH_Realloc(hgh, ghid, cb) ((GHID)HP_Realloc(hgh, ghid, cb))
  114. #define _GH_Free(hgh, ghid) HP_Free(hgh, ghid)
  115. #define _GH_WaitForMutex(hgh, ul) (TRUE)
  116. #define _GH_ReleaseMutex(hgh)
  117. #define HeapFreeZ(hHeap, pv) \
  118. if (pv) \
  119. HeapFree(hHeap, 0, pv);
  120. #define _LH_Open(dwMaxHeap) HeapCreate(0, 0, dwMaxHeap)
  121. #define _LH_Close(_hlh) HeapDestroy(_hlh)
  122. #define _LH_Alloc(_hlh, cb) HeapAlloc(_hlh, 0, cb)
  123. #define _LH_Realloc(_hlh, pv, cb) HeapReAlloc(_hlh, 0, pv, cb)
  124. #define _LH_GetSize(_hlh, pv) HeapSize(_hlh, 0, pv)
  125. #define _LH_Free(_hlh, pv) HeapFreeZ(_hlh, pv)
  126. #endif
  127. /* NT Implementation ------------------------------------------------------- */
  128. #if defined(WIN32) && !defined(CHICAGO) && !defined(MAC)
  129. typedef DWORD GHNAME, * PGHNAME, ** PPGHNAME;
  130. typedef DWORD GHID, * PGHID, ** PPGHID;
  131. typedef struct GROOT GROOT, * PGROOT, ** PPGROOT;
  132. typedef struct GH GH, * PGH, ** PPGH;
  133. typedef PGH _HGH;
  134. typedef HANDLE _HLH;
  135. struct GROOT
  136. {
  137. DWORD dwBLK; // Block header
  138. GHNAME ghname; // Name of the heap
  139. GHID ghidRoot; // Client root heap block
  140. DWORD dwCurHeap; // Current size of the heap
  141. DWORD dwMaxHeap; // Maximum size of the heap
  142. WORD rgcbFree[1]; // Maximum contiguous free bytes per page
  143. };
  144. struct GH
  145. {
  146. PGROOT pgroot; // Pointer to the first byte of the heap
  147. HANDLE hMutex; // Handle to public mutex for this heap
  148. HANDLE hMutexHeap; // Handle to private mutex for this heap
  149. HANDLE hMapping; // Handle to file mapping object
  150. };
  151. typedef struct GH_SECURITY_ATTRIBUTES {
  152. SECURITY_ATTRIBUTES sa;
  153. BYTE rgbSd[SECURITY_DESCRIPTOR_MIN_LENGTH];
  154. } GH_SECURITY_ATTRIBUTES, * PGH_SECURITY_ATTRIBUTES;
  155. BOOL GH_InitializeSecurityAttributes(PGH_SECURITY_ATTRIBUTES pghsa);
  156. __inline void HeapFreeZ(HANDLE hHeap, LPVOID pv)
  157. {
  158. if (pv)
  159. HeapFree(hHeap, 0, pv);
  160. }
  161. BOOL _GH_WaitForMutex(_HGH hgh, ULONG ulTimeout);
  162. _HGH _GH_Open(BOOL fCreate, GHNAME ghname, DWORD dwMaxHeap);
  163. void _GH_Close(_HGH hgh);
  164. GHID _GH_Alloc(_HGH hgh, UINT cb);
  165. GHID _GH_Realloc(_HGH hgh, GHID ghid, UINT cb);
  166. void _GH_Free(_HGH hgh, GHID ghid);
  167. #define _GH_GetPv(hgh, ghid) ((LPVOID)((BYTE *)(hgh)->pgroot + (ghid)))
  168. #define _GH_GetId(hgh, pv) ((BYTE *)(pv) - (BYTE *)(hgh)->pgroot)
  169. #define _GH_GetSize(hgh, ghid) ((UINT)*((WORD *)_GH_GetPv(hgh, ghid) - 2))
  170. #define _GH_GetRoot(hgh) ((hgh)->pgroot->ghidRoot)
  171. #define _GH_SetRoot(hgh, ghid) ((hgh)->pgroot->ghidRoot = (ghid))
  172. #define _GH_GetName(hgh) ((hgh)->pgroot->ghname)
  173. #define _GH_WaitForMutex(hgh, ul) GH_WaitForSingleObject((hgh)->hMutex, ul)
  174. #ifdef __cplusplus
  175. #define _GH_ReleaseMutex(hgh) ::ReleaseMutex((hgh)->hMutex)
  176. #else
  177. #define _GH_ReleaseMutex(hgh) ReleaseMutex((hgh)->hMutex)
  178. #endif /* __cplusplus */
  179. #define _LH_Open(dwMaxHeap) HeapCreate(0, 0, dwMaxHeap)
  180. #define _LH_Close(_hlh) HeapDestroy(_hlh)
  181. #define _LH_Alloc(_hlh, cb) HeapAlloc(_hlh, 0, cb)
  182. #define _LH_Realloc(_hlh, pv, cb) HeapReAlloc(_hlh, 0, pv, cb)
  183. #define _LH_GetSize(_hlh, pv) HeapSize(_hlh, 0, pv)
  184. #define _LH_Free(_hlh, pv) HeapFreeZ(_hlh, pv)
  185. #endif /* WIN32 */
  186. /* Mac Implementation ------------------------------------------------------ */
  187. #ifdef MAC
  188. #define GH_POINTERS_VALID
  189. typedef DWORD GHNAME, * PGHNAME, ** PPGHNAME;
  190. typedef DWORD GHID, * PGHID, ** PPGHID;
  191. typedef struct GROOT GROOT, * PGROOT, ** PPGROOT;
  192. typedef struct GH GH, * PGH, ** PPGH;
  193. typedef PGH _HGH;
  194. typedef HANDLE _HLH;
  195. struct GROOT
  196. {
  197. GHID ghidRoot; // Root heap block
  198. GHNAME ghname; // Name of the heap
  199. DWORD dwCurHeap; // Current size of the heap
  200. DWORD dwMaxHeap; // Maximum size of the heap
  201. PGROOT next; // Pointer to next shared heap
  202. };
  203. struct GH
  204. {
  205. LPMALLOC lpMalloc; // OLE shared heap allocator (IMalloc interface)
  206. PGROOT pgroot; // Pointer to the first byte of the heap
  207. };
  208. #define _GH_GetRoot(hgh) ((hgh)->pgroot->ghidRoot)
  209. #define _GH_SetRoot(hgh, ghid) ((hgh)->pgroot->ghidRoot = (ghid))
  210. #define _GH_GetName(hgh) ((hgh)->pgroot->ghname)
  211. #define _GH_WaitForMutex(hgh, ul) (TRUE)
  212. #define _GH_ReleaseMutex(hgh)
  213. #define _GH_GetPv(hgh, ghid) ((LPVOID)(ghid))
  214. #define _GH_GetId(hgh, pv) ((GHID)(pv))
  215. #define _GH_Close(hgh) ((void)0)
  216. _HGH _GH_Open(BOOL fCreate, GHNAME ghname, DWORD dwMaxHeap);
  217. __inline GHID _GH_Alloc(_HGH hgh, UINT cb)
  218. {
  219. #ifdef __cplusplus
  220. return((GHID)hgh->lpMalloc->Alloc(cb));
  221. #else
  222. return((GHID)hgh->lpMalloc->lpVtbl->Alloc(hgh->lpMalloc, cb));
  223. #endif
  224. }
  225. __inline GHID _GH_Realloc(_HGH hgh, GHID ghid, UINT cb)
  226. {
  227. #ifdef __cplusplus
  228. return((GHID)hgh->lpMalloc->Realloc((LPVOID)ghid, cb));
  229. #else
  230. return((GHID)hgh->lpMalloc->lpVtbl->Realloc(hgh->lpMalloc, (LPVOID)ghid, cb));
  231. #endif
  232. }
  233. __inline UINT _GH_GetSize(_HGH hgh, GHID ghid)
  234. {
  235. #ifdef __cplusplus
  236. return((UINT)hgh->lpMalloc->GetSize((PVOID)ghid));
  237. #else
  238. return((UINT)hgh->lpMalloc->lpVtbl->GetSize(hgh->lpMalloc, (PVOID)ghid));
  239. #endif
  240. }
  241. __inline void _GH_Free(_HGH hgh, GHID ghid)
  242. {
  243. #ifdef __cplusplus
  244. hgh->lpMalloc->Free((LPVOID)ghid);
  245. #else
  246. hgh->lpMalloc->lpVtbl->Free(hgh->lpMalloc, (LPVOID)ghid);
  247. #endif
  248. }
  249. // -----------
  250. __inline LPVOID _LH_Open(DWORD dwMaxHeap)
  251. {
  252. LPMALLOC lpMalloc;
  253. (void) CoGetMalloc(MEMCTX_TASK, &lpMalloc);
  254. return (void *)lpMalloc;
  255. }
  256. #define _LH_Close(hlh) ((void)0)
  257. __inline LPVOID _LH_Alloc(LPMALLOC hlh, UINT cb)
  258. {
  259. #ifdef __cplusplus
  260. return((hlh)->Alloc(cb));
  261. #else
  262. return((hlh)->lpVtbl->Alloc(hlh, cb));
  263. #endif
  264. }
  265. __inline LPVOID _LH_Realloc(LPMALLOC hlh, LPVOID pv, UINT cb)
  266. {
  267. #ifdef __cplusplus
  268. return(hlh->Realloc(pv, cb));
  269. #else
  270. return(hlh->lpVtbl->Realloc(hlh, pv, cb));
  271. #endif
  272. }
  273. __inline UINT _LH_GetSize(LPMALLOC hlh, LPVOID pv)
  274. {
  275. #ifdef __cplusplus
  276. return((UINT)hlh->GetSize(pv));
  277. #else
  278. return((UINT)hlh->lpVtbl->GetSize(hlh, pv));
  279. #endif
  280. }
  281. __inline void _LH_Free(LPMALLOC hlh, LPVOID pv)
  282. {
  283. #ifdef __cplusplus
  284. hlh->Free(pv);
  285. #else
  286. hlh->lpVtbl->Free(hlh, pv);
  287. #endif
  288. }
  289. #endif /* MAC */
  290. /* DOS Implementation ------------------------------------------------------ */
  291. #ifdef DOS
  292. typedef DWORD GHID, * PGHID, ** PPGHID;
  293. typedef LPMALLOC _HLH;
  294. __inline LPVOID _LH_Alloc(_HLH hlh, UINT cb)
  295. {
  296. #ifdef __cplusplus
  297. return((hlh)->Alloc(cb));
  298. #else
  299. return((hlh)->lpVtbl->Alloc(hlh, cb));
  300. #endif
  301. }
  302. __inline LPVOID _LH_Realloc(_HLH hlh, LPVOID pv, UINT cb)
  303. {
  304. #ifdef __cplusplus
  305. return(hlh->Realloc(pv, cb));
  306. #else
  307. return(hlh->lpVtbl->Realloc(hlh, pv, cb));
  308. #endif
  309. }
  310. __inline void _LH_Free(_HLH hlh, LPVOID pv)
  311. {
  312. #ifdef __cplusplus
  313. hlh->Free(pv);
  314. #else
  315. hlh->lpVtbl->Free(hlh, pv);
  316. #endif
  317. }
  318. __inline UINT _LH_GetSize(_HLH hlh, LPVOID pv)
  319. {
  320. #ifdef __cplusplus
  321. return((UINT)hlh->GetSize(pv));
  322. #else
  323. return((UINT)hlh->lpVtbl->GetSize(hlh, pv));
  324. #endif
  325. }
  326. #endif
  327. // LH External API ------------------------------------------------------------
  328. #if defined(DEBUG) && (defined(WIN16) || defined(WIN32))
  329. #define IFHEAPNAME(x) x
  330. typedef struct LH * HLH;
  331. HLH WINAPI LH_Open(DWORD dwMaxHeap);
  332. void WINAPI LH_Close(HLH hlh);
  333. LPVOID WINAPI LH_Alloc(HLH hlh, UINT cb);
  334. LPVOID WINAPI LH_Realloc(HLH hlh, LPVOID pv, UINT cb);
  335. UINT WINAPI LH_GetSize(HLH hlh, LPVOID pv);
  336. void WINAPI LH_Free(HLH hlh, LPVOID pv);
  337. BOOL
  338. #ifdef MAC
  339. WINAPI
  340. #endif
  341. LH_DidAlloc(HLH hlh, LPVOID pv);
  342. void __cdecl LH_SetHeapNameFn(HLH hlh, TCHAR *pszFormat, ...);
  343. void __cdecl LH_SetNameFn(HLH hlh, LPVOID pv, TCHAR *pszFormat, ...);
  344. TCHAR * LH_GetName(HLH hlh, LPVOID pv);
  345. #else
  346. #define IFHEAPNAME(x) 0
  347. typedef _HLH HLH;
  348. #define LH_Open(dwMaxHeap) _LH_Open(dwMaxHeap)
  349. #define LH_Close(hlh) _LH_Close(hlh)
  350. #define LH_Alloc(hlh, cb) _LH_Alloc(hlh, cb)
  351. #define LH_Realloc(hlh, pv, cb) _LH_Realloc(hlh, pv, cb)
  352. #define LH_GetSize(hlh, pv) _LH_GetSize(hlh, pv)
  353. #define LH_Free(hlh, pv) _LH_Free(hlh, pv)
  354. #endif
  355. #define LH_SetHeapName(hlh,psz) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz))
  356. #define LH_SetHeapName1(hlh,psz,a1) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz,a1))
  357. #define LH_SetHeapName2(hlh,psz,a1,a2) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz,a1,a2))
  358. #define LH_SetHeapName3(hlh,psz,a1,a2,a3) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz,a1,a2,a3))
  359. #define LH_SetHeapName4(hlh,psz,a1,a2,a3,a4) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz,a1,a2,a3,a4))
  360. #define LH_SetHeapName5(hlh,psz,a1,a2,a3,a4,a5) IFHEAPNAME(LH_SetHeapNameFn(hlh,psz,a1,a2,a3,a4,a5))
  361. #define LH_SetName(hlh,pv,psz) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz))
  362. #define LH_SetName1(hlh,pv,psz,a1) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz,a1))
  363. #define LH_SetName2(hlh,pv,psz,a1,a2) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz,a1,a2))
  364. #define LH_SetName3(hlh,pv,psz,a1,a2,a3) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz,a1,a2,a3))
  365. #define LH_SetName4(hlh,pv,psz,a1,a2,a3,a4) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz,a1,a2,a3,a4))
  366. #define LH_SetName5(hlh,pv,psz,a1,a2,a3,a4,a5) IFHEAPNAME(LH_SetNameFn(hlh,pv,psz,a1,a2,a3,a4,a5))
  367. // GH External API ------------------------------------------------------------
  368. #if !defined(DOS)
  369. typedef _HGH HGH;
  370. #define GH_Open(fCreate, ghname, dwMaxHeap) _GH_Open(fCreate, ghname, \
  371. dwMaxHeap)
  372. #define GH_Close(hgh) _GH_Close(hgh)
  373. #define GH_GetRoot(hgh) _GH_GetRoot(hgh)
  374. #define GH_SetRoot(hgh, ghid) _GH_SetRoot(hgh, ghid)
  375. #define GH_GetName(hgh) _GH_GetName(hgh)
  376. #define GH_GetPv(hgh, ghid) _GH_GetPv(hgh, ghid)
  377. #define GH_GetId(hgh, pv) _GH_GetId(hgh, pv)
  378. #define GH_GetSize(hgh, ghid) _GH_GetSize(hgh, ghid)
  379. #define GH_Alloc(hgh, cb) _GH_Alloc(hgh, cb)
  380. #define GH_Realloc(hgh, ghid, cb) _GH_Realloc(hgh, ghid, cb)
  381. #define GH_Free(hgh, ghid) _GH_Free(hgh, ghid)
  382. #define GH_WaitForMutex(hgh, ulT) _GH_WaitForMutex(hgh, ulT)
  383. #define GH_ReleaseMutex(hgh) _GH_ReleaseMutex(hgh)
  384. #define GH_GetObjectName(pszName, ghname, bTag) _GH_GetObjectName(pszName, \
  385. ghname, bTag);
  386. #define GH_WaitForSingleObject(hMutex, ulTO) _GH_WaitForSingleObject(hMutex,\
  387. ulTO)
  388. #endif
  389. #ifdef WIN32
  390. #define GH_NAME_CCH 17
  391. #define GH_NAME_MUTEX_1 '*' /* reserved for internal use */
  392. #define GH_NAME_MUTEX_2 '+' /* reserved for internal use */
  393. #define GH_NAME_MUTEX_3 '^'
  394. #define GH_NAME_FILE_MAPPING '!'
  395. void _GH_GetObjectName(CHAR *pszName, GHNAME ghname, BYTE bTag);
  396. BOOL _GH_WaitForSingleObject(HANDLE hMutex, ULONG ulTimeout);
  397. #endif
  398. // ----------------------------------------------------------------------------
  399. #ifdef __cplusplus
  400. }
  401. #endif
  402. #endif // __GLHEAP_H_