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.

304 lines
12 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: hmgr.h
  3. *
  4. * This file contains all the prototypes for the handle mangager.
  5. *
  6. * Added nifty header: 29-Jun-1991 16:31:46
  7. * Author: Patrick Haluptzok patrickh
  8. *
  9. * Copyright (c) 1990-1999 Microsoft Corporation
  10. \**************************************************************************/
  11. //#include "hmgshare.h"
  12. // <-fulltype->
  13. // <---full unique--->
  14. // +-+------+-+--+-----+----------------+
  15. // |u|unique|s|al|type | index |
  16. // +-+------+-+--+-----+----------------+
  17. // ^ ^ ^^
  18. // +user | ||
  19. // | ||
  20. // | ++alternate type for client (pen, metafile, dibsection)
  21. // |
  22. // +stock object
  23. //
  24. // USER - bit(s) reserved for USER, not used for comparing identical handles
  25. //
  26. // TYPE - types used by GRE
  27. // ALTTYPE - extra type bits used by client
  28. // STOCK - bit indicating stockobject
  29. // FULLTYPE - all bits related to type that are per object (includes STOCK bit)
  30. //
  31. // UNIQUE - bits that are incremented for each instance of the handle
  32. // FULLUNIQUE - bits used for comparing identical handles. This includes FULLTYPE
  33. //
  34. // INDEX - index into server side handle table
  35. //
  36. // The handle index points into a big array of entries. This index is
  37. // broken down into 'page' and 'entry' fields.
  38. // This is to prevent having to have the entire handle table created at
  39. // once. If all the handles in a page are in use and no free handles are
  40. // available, a new page is faulted in for use.
  41. // all the commented defines below live in ntgdistr.h.
  42. #define LOTYPE_BITS (TYPE_BITS + ALTTYPE_BITS)
  43. #define FULLTYPE_BITS (TYPE_BITS + ALTTYPE_BITS + STOCK_BITS)
  44. #define FULLUNIQUE_BITS (TYPE_BITS + ALTTYPE_BITS + STOCK_BITS + UNIQUE_BITS)
  45. #define NONINDEX_BITS (32 - INDEX_BITS)
  46. #define INDEX_SHIFT 0
  47. #define UNIQUE_SHIFT (STOCK_SHIFT + STOCK_BITS)
  48. #define LOTYPE_SHIFT (TYPE_SHIFT)
  49. #define FULLTYPE_SHIFT (TYPE_SHIFT)
  50. #define FULLUNIQUE_SHIFT (TYPE_SHIFT)
  51. //MASKS contain the bits of the handle used for the paricular field
  52. #define NONINDEX_MASK(shift,cbits) ( ((1 << (cbits)) - 1) << (shift) )
  53. #define INDEX_MASK ((1 << INDEX_BITS) - 1)
  54. #define TYPE_MASK (NONINDEX_MASK(TYPE_SHIFT, TYPE_BITS))
  55. #define ALTTYPE_MASK (NONINDEX_MASK(ALTTYPE_SHIFT, ALTTYPE_BITS))
  56. #define STOCK_MASK (NONINDEX_MASK(STOCK_SHIFT, STOCK_BITS))
  57. #define UNIQUE_MASK (NONINDEX_MASK(UNIQUE_SHIFT, UNIQUE_BITS))
  58. #define LOTYPE_MASK (NONINDEX_MASK(LOTYPE_SHIFT, LOTYPE_BITS))
  59. #define FULLTYPE_MASK (NONINDEX_MASK(FULLTYPE_SHIFT, FULLTYPE_BITS))
  60. // NOTE that UNIQUE_INCREMENT is based on the uniqueness beeing a short, not a full handle
  61. #define UNIQUE_INCREMENT (1 << (UNIQUE_SHIFT - INDEX_BITS))
  62. #define MODIFY_HMGR_TYPE(h,t) ((HANDLE)((ULONG_PTR)(h) | (t)))
  63. #define HmgIfromH(h) (ULONG)((ULONG_PTR)(h) & INDEX_MASK)
  64. #define HmgUfromH(h) ((USHORT) (((ULONG_PTR)(h) & FULLUNIQUE_MASK) >> TYPE_SHIFT))
  65. #define HmgObjtype(h) ((OBJTYPE)(((ULONG_PTR)(h) & TYPE_MASK) >> TYPE_SHIFT))
  66. #define HmgStockObj(hobj) ((ULONG_PTR)(hobj) & STOCK_MASK)
  67. // given a usUnique and a type, modify it to contain a new type
  68. #define USUNIQUE(u,t) (USHORT)((u & (UNIQUE_MASK >> INDEX_BITS)) | \
  69. (t << (TYPE_SHIFT - INDEX_BITS)))
  70. //
  71. // WOW has a dependency on the index portion of returned 32-bit GDI
  72. // handles being greater than (COLOR_ENDCOLORS >> 2)
  73. // (COLOR_ENDCOLORS is defined in winuserp.h).
  74. //
  75. // This dependency allows WOW to distinguish between a 16-bit HBRUSH
  76. // and a COLOR_* constant.
  77. //
  78. // Therefore, we will will reserve the first few entries of the handle
  79. // table, disallowing the use of any index less than HMGR_HANDLE_BASE.
  80. //
  81. // hideyukn, 1997.Oct.27
  82. //
  83. #define HMGR_HANDLE_BASE 0x000a
  84. ULONG FASTCALL HmgQueryLock(HOBJ hobj);
  85. BOOL FASTCALL HmgSetLock(HOBJ hobj, ULONG cLock);
  86. ULONG FASTCALL HmgQueryAltLock(HOBJ hobj);
  87. BOOL HmgCreate();
  88. HOBJ HmgAlloc(ULONGSIZE_T,OBJTYPE,USHORT);
  89. POBJ HmgReplace(HOBJ,POBJ,FLONG,LONG,OBJTYPE);
  90. VOID HmgFree(HOBJ);
  91. POBJ FASTCALL HmgLock(HOBJ,OBJTYPE);
  92. POBJ FASTCALL HmgLockAllOwners(HOBJ,OBJTYPE);
  93. POBJ FASTCALL HmgShareLock(HOBJ,OBJTYPE);
  94. POBJ FASTCALL HmgShareCheckLock(HOBJ,OBJTYPE);
  95. POBJ FASTCALL HmgShareLockIgnoreStockBit(HOBJ,OBJTYPE);
  96. POBJ FASTCALL HmgShareCheckLockIgnoreStockBit(HOBJ,OBJTYPE);
  97. BOOL HmgSetOwner(HOBJ,W32PID,OBJTYPE);
  98. BOOL HmgSwapHandleContents(HOBJ,ULONG,HOBJ,ULONG,OBJTYPE);
  99. BOOL HmgSwapLockedHandleContents(HOBJ,ULONG,HOBJ,ULONG,OBJTYPE);
  100. POBJ FASTCALL HmgReferenceCheckLock(HOBJ h,OBJTYPE ot, BOOL bDebugPrint);
  101. HOBJ FASTCALL HmgNextOwned(HOBJ,W32PID);
  102. POBJ FASTCALL HmgSafeNextObjt(HOBJ hobj, OBJTYPE objt);
  103. BOOL HmgValidHandle(HOBJ, OBJTYPE);
  104. HOBJ FASTCALL HmgSafeNextOwned(HOBJ,W32PID);
  105. BOOL FASTCALL HmgMarkUndeletable(HOBJ,OBJTYPE);
  106. BOOL FASTCALL HmgMarkDeletable(HOBJ,OBJTYPE);
  107. HOBJ HmgInsertObject(PVOID,FLONG,OBJTYPE);
  108. PVOID HmgRemoveObject(HOBJ,LONG,LONG,BOOL,OBJTYPE);
  109. OBJTYPE * HmgSetNULLType(HOBJ,LONG,LONG,OBJTYPE);
  110. HOBJ HmgModifyHandleType(HOBJ h);
  111. BOOL HmgLockAndModifyHandleType(HOBJ h);
  112. PVOID HmgAllocateSecureUserMemory();
  113. PDC_ATTR HmgAllocateDcAttr();
  114. POBJECTATTR HmgAllocateObjectAttr();
  115. VOID HmgFreeDcAttr(PDC_ATTR);
  116. VOID HmgFreeObjectAttr(POBJECTATTR);
  117. BOOL bPEBCacheHandle(HANDLE,HANDLECACHETYPE,POBJECTATTR,PENTRY);
  118. BOOL bLoadProcessHandleQuota();
  119. HOBJ HmgIncUniqueness(HOBJ hobj, OBJTYPE objt);
  120. VOID FASTCALL HmgIncrementShareReferenceCount(POBJ);
  121. ULONG FASTCALL HmgDecrementShareReferenceCount(POBJ);
  122. VOID HmgShareUnlock(POBJ pobj);
  123. BOOL FASTCALL HmgInterlockedCompareAndSwap(PULONG,ULONG,ULONG);
  124. PVOID HmgForceRemoveObject(HOBJ hobj,BOOL bIgnoreUndeletable, OBJTYPE objt);
  125. BOOL HmgIncProcessHandleCount(W32PID,OBJTYPE);
  126. VOID HmgDecProcessHandleCount(W32PID);
  127. #define HMGR_ALLOC_LOCK 0x0001
  128. #define HMGR_ALLOC_ALT_LOCK 0x0002
  129. #define HMGR_NO_ZERO_INIT 0x0004
  130. #define HMGR_MAKE_PUBLIC 0x0008
  131. #define MAXIMUM_POOL_ALLOC (PAGE_SIZE * 10000)
  132. // Global Handle Manager data.
  133. extern HSEMAPHORE ghsemHmgr;
  134. extern ENTRY *gpentHmgr;
  135. extern HOBJ ghFreeHmgr;
  136. extern ULONG gcMaxHmgr;
  137. extern PLARGE_INTEGER gpLockShortDelay;
  138. extern ULONG gCacheHandleEntries[GDI_CACHED_HADNLE_TYPES];
  139. extern LONG gProcessHandleQuota;
  140. // DirectDraw Handle Manager data: externed here for debugger extensions
  141. extern ULONG gcSizeDdHmgr;
  142. extern ENTRY *gpentDdHmgr;
  143. extern HOBJ ghFreeDdHmgr;
  144. extern ULONG gcMaxDdHmgr;
  145. //
  146. // SAMEHANDLE and DIFFHANDLE have moved to wingdip.h so other server-side
  147. // components can safely compare engine handles. They validate all but USER bits
  148. //
  149. #define SAMEINDEX(H,K) (((((ULONG_PTR) (H)) ^ ((ULONG_PTR) (K))) & INDEX_MASK) == 0)
  150. /*********************************MACRO************************************\
  151. * INC_EXCLUSIVE_REF_CNT - increment object's exclusive reference count
  152. * DEC_EXCLUSIVE_REF_CNT - decrement object's exclusive reference count
  153. *
  154. * Note that the InterlockedIncrement/Decrement treats the cExclusiveLock
  155. * as a ULONG. cExclusiveLock is declared as a USHORT and the increment
  156. * overlaps with the BASEOBJECT::BaseFlags. If the BaseFlags were ever changed,
  157. * this code may have to be changed to use an InterlockedCompareExchange loop.
  158. * See BASEOBJECT declaration.
  159. *
  160. *
  161. * Arguments:
  162. *
  163. * pObj - pointer to object
  164. *
  165. * Return Value:
  166. *
  167. * None
  168. *
  169. \**************************************************************************/
  170. #define INC_EXCLUSIVE_REF_CNT(pObj) InterlockedIncrement((LONG *)& (((POBJ) pObj)->cExclusiveLock))
  171. #define DEC_EXCLUSIVE_REF_CNT(pObj) InterlockedDecrement((LONG *)& (((POBJ) pObj)->cExclusiveLock))
  172. /********************************MACRO*************************************\
  173. * INC_SHARE_REF_CNT - do an interlocked increment of the
  174. * shared reference count of the given object.
  175. *
  176. * DEC_SHARE_REF_CNT - do an interlocked decrement of the
  177. * shared reference count of the given object.
  178. *
  179. * Arguments:
  180. *
  181. * pObj - pointer to OBJECT
  182. *
  183. * Return Value:
  184. *
  185. * HmgIncrementShareReferenceCount : None
  186. * HmgDecrementShareReferenceCount : Original shared reference count
  187. *
  188. \**************************************************************************/
  189. #define PENTRY_FROM_POBJ(pObj) \
  190. (&gpentHmgr[(UINT)HmgIfromH(((POBJ)(pObj))->hHmgr)])
  191. #define INC_SHARE_REF_CNT(pObj) \
  192. HmgIncrementShareReferenceCount((POBJ) (pObj))
  193. #define DEC_SHARE_REF_CNT(pObj) \
  194. HmgDecrementShareReferenceCount((POBJ) (pObj))
  195. /*********************************MACRO************************************\
  196. *
  197. * DEC_SHARE_REF_CNT_LAZY0 - Interlocked decrement the shared reference
  198. * count of the given object. If the original count was 1, and the
  199. * object's TO_BE_DELETED flag is set, then call the object deletion
  200. * routine
  201. *
  202. * Arguments:
  203. *
  204. * pObj - pointer to object
  205. *
  206. * Return Value:
  207. *
  208. * None
  209. *
  210. \**************************************************************************/
  211. #define DEC_SHARE_REF_CNT_LAZY0(pObj) \
  212. { \
  213. PBRUSHATTR pBrushattr = ((PBRUSH)pObj)->pBrushattr(); \
  214. \
  215. if (1 == (DEC_SHARE_REF_CNT(pObj) & 0xffff)) \
  216. { \
  217. if ((pBrushattr->AttrFlags) & ATTR_TO_BE_DELETED) \
  218. { \
  219. bDeleteBrush ((HBRUSH) pObj->hHmgr,FALSE); \
  220. } \
  221. } \
  222. }
  223. /*********************************MACRO************************************\
  224. *
  225. * DEC_SHARE_REF_CNT_LAZY_DEL_LOGFONT
  226. * count of the given object. If the original count was 1, and the
  227. * object's TO_BE_DELETED flag is set, then call the object deletion
  228. * routine
  229. *
  230. * Arguments:
  231. *
  232. * pObj - pointer to object
  233. *
  234. * Return Value:
  235. *
  236. * None
  237. *
  238. \**************************************************************************/
  239. #define DEC_SHARE_REF_CNT_LAZY_DEL_LOGFONT(pObj) \
  240. { \
  241. if (1 == (DEC_SHARE_REF_CNT(pObj) & 0xffff)) \
  242. { \
  243. if (PENTRY_FROM_POBJ(pObj)->Flags & HMGR_ENTRY_LAZY_DEL) \
  244. { \
  245. bDeleteFont ((HLFONT) pObj->hHmgr,FALSE); \
  246. } \
  247. } \
  248. }
  249. #define DEC_SHARE_REF_CNT_LAZY_DEL_COLORSPACE(pObj) \
  250. { \
  251. DEC_SHARE_REF_CNT(pObj); \
  252. }