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.

260 lines
7.9 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: DdHmgr.h
  3. *
  4. * This file contains all the prototypes for the DirectDraw handle mangager.
  5. *
  6. * Added header: 30-Apr-1999 16:31:46
  7. * Author: Lindsay Steventon (linstev)
  8. *
  9. * Copyright (c) 1999 Microsoft Corporation
  10. \**************************************************************************/
  11. // <--full unique-->
  12. // +--------+------+----------------+
  13. // | unique | type | index |
  14. // +--------+------+----------------+
  15. //
  16. // TYPE - types used by DDRAW & D3D
  17. //
  18. // UNIQUE - bits that are incremented for each instance of the handle
  19. // FULLUNIQUE - bits used for comparing identical handles. This includes the TYPE
  20. //
  21. // INDEX - index into server side handle table
  22. //
  23. #define DD_TABLESIZE_DELTA ((PAGE_SIZE*4) / sizeof(DD_ENTRY)) // 4 pages of entries each time
  24. #define DD_DEF_TYPE 0
  25. #define DD_DIRECTDRAW_TYPE 1
  26. #define DD_SURFACE_TYPE 2
  27. #define D3D_HANDLE_TYPE 3
  28. #define DD_VIDEOPORT_TYPE 4
  29. #define DD_MOTIONCOMP_TYPE 5
  30. #define DD_MAX_TYPE 5
  31. #define DD_INDEX_BITS 21 // 2^21 ~ 2 million handles
  32. #define DD_TYPE_BITS 3 // 2^3 = 8, we only need 6
  33. #define DD_UNIQUE_BITS 8 // identifies each new handle
  34. #define DD_NONINDEX_BITS (32 - DD_INDEX_BITS)
  35. #define DD_INDEX_SHIFT 0
  36. #define DD_TYPE_SHIFT (DD_INDEX_BITS)
  37. #define DD_UNIQUE_SHIFT (DD_TYPE_SHIFT + DD_TYPE_BITS)
  38. // MASKS contain the bits of the handle used for the paricular field
  39. #define DD_NONINDEX_MASK(shift,cbits) ( ((1 << (cbits)) - 1) << (shift) )
  40. #define DD_INDEX_MASK ((1 << DD_INDEX_BITS) - 1)
  41. #define DD_TYPE_MASK (DD_NONINDEX_MASK(DD_TYPE_SHIFT, DD_TYPE_BITS))
  42. #define DD_UNIQUE_MASK (DD_NONINDEX_MASK(DD_UNIQUE_SHIFT, DD_UNIQUE_BITS))
  43. #define DD_FULLUNIQUE_MASK (DD_UNIQUE_MASK | DD_TYPE_MASK)
  44. #define DD_MAKE_HMGR_HANDLE(Index,Unique) LongToHandle(((((LONG) Unique) << DD_INDEX_BITS) | ((LONG) Index)))
  45. // NOTE that DD_UNIQUE_INCREMENT is based on the uniqueness being a short, not a full handle
  46. #define DD_UNIQUE_INCREMENT (1 << (DD_UNIQUE_SHIFT - DD_INDEX_BITS))
  47. #define DdHmgIfromH(h) (ULONG)((ULONG_PTR)(h) & DD_INDEX_MASK)
  48. #define DdHmgUfromH(h) ((USHORT) (((ULONG_PTR)(h) & DD_FULLUNIQUE_MASK) >> DD_TYPE_SHIFT))
  49. #define DdHmgObjtype(h) ((DD_OBJTYPE)(((ULONG_PTR)(h) & DD_TYPE_MASK) >> DD_TYPE_SHIFT))
  50. // given a usUnique and a type, modify it to contain a new type
  51. #define DD_USUNIQUE(u,t) (USHORT)((u & ((ULONG)DD_UNIQUE_MASK >> (ULONG)DD_INDEX_BITS)) | \
  52. (t << (DD_TYPE_SHIFT - DD_INDEX_BITS)))
  53. #define DD_MAX_HANDLE_COUNT (1 << (32 - DD_NONINDEX_BITS))
  54. #define DD_HMGR_HANDLE_BASE 1
  55. ULONG FASTCALL DdHmgQueryLock(HDD_OBJ hobj);
  56. BOOL DdHmgCreate();
  57. BOOL DdHmgDestroy();
  58. BOOL DdHmgCloseProcess(W32PID W32Pid);
  59. HDD_OBJ DdHmgAlloc(ULONGSIZE_T,DD_OBJTYPE,USHORT);
  60. VOID DdHmgFree(HDD_OBJ);
  61. VOID DdFreeObject(PVOID pvFree, ULONG ulType);
  62. PDD_OBJ FASTCALL DdHmgLock(HDD_OBJ,DD_OBJTYPE,BOOL);
  63. PDD_OBJ FASTCALL DdHmgNextObjt(HDD_OBJ hobj, DD_OBJTYPE objt);
  64. PVOID DdHmgRemoveObject(HDD_OBJ,LONG,LONG,BOOL,DD_OBJTYPE);
  65. VOID DdHmgAcquireHmgrSemaphore();
  66. VOID DdHmgReleaseHmgrSemaphore();
  67. // DirectDraw Handle Manager data.
  68. extern ULONG gcSizeDdHmgr;
  69. extern DD_ENTRY *gpentDdHmgr;
  70. extern HDD_OBJ ghFreeDdHmgr;
  71. extern ULONG gcMaxDdHmgr;
  72. extern PLARGE_INTEGER gpLockShortDelay;
  73. /*********************************MACRO************************************\
  74. * INC_EXCLUSIVE_REF_CNT - increment object's exclusive reference count
  75. * DEC_EXCLUSIVE_REF_CNT - decrement object's exclusive reference count
  76. *
  77. * Note that the InterlockedIncrement/Decrement treats the cExclusiveLock
  78. * as a ULONG. cExclusiveLock is declared as a USHORT and the increment
  79. * overlaps with the BASEOBJECT::BaseFlags. If the BaseFlags were ever changed,
  80. * this code may have to be changed to use an InterlockedCompareExchange loop.
  81. * See BASEOBJECT declaration.
  82. *
  83. *
  84. * Arguments:
  85. *
  86. * pObj - pointer to object
  87. *
  88. * Return Value:
  89. *
  90. * None
  91. *
  92. \**************************************************************************/
  93. #define INC_EXCLUSIVE_REF_CNT(pObj) \
  94. InterlockedIncrement((LONG *)& (((PDD_OBJ) pObj)->cExclusiveLock))
  95. #define DEC_EXCLUSIVE_REF_CNT(pObj) \
  96. InterlockedDecrement((LONG *)& (((PDD_OBJ) pObj)->cExclusiveLock))
  97. /*******************************Function***********************************\
  98. * VerifyObjectOwner
  99. *
  100. * Verifies ownership of the object passed in via the PDD_ENTRY.
  101. *
  102. * History:
  103. *
  104. * 21-Feb-1996 -by- Mark Enstrom [marke]
  105. * 12-Mar-2001 -by- Scott Mackowski [ScottMa] (taken from DDHANDLELOCK)
  106. *
  107. \**************************************************************************/
  108. inline
  109. BOOL VerifyObjectOwner(PDD_ENTRY pentry)
  110. {
  111. DD_OBJECTOWNER Obj;
  112. Obj = pentry->ObjectOwner;
  113. if ((DD_OBJECTOWNER_PID(Obj) != DD_W32GetCurrentPID()) &&
  114. (DD_OBJECTOWNER_PID(Obj) != OBJECT_OWNER_PUBLIC) )
  115. {
  116. return FALSE;
  117. }
  118. return TRUE;
  119. }
  120. // Notes on entry structure
  121. //
  122. // The internal entry in the handle manager appears as follows
  123. //
  124. // +-------------------+
  125. // | einfo.pobj, hfree | 4 bytes
  126. // +-------------------+
  127. // | ObjectOwner | 4 bytes
  128. // +-------------------+
  129. // | FullUnique | 2 bytes
  130. // +-------------------+
  131. // | Objt | 1 byte
  132. // +-------------------+
  133. // | Flags | 1 byte
  134. // +-------------------+
  135. // | dwReserved | 4 bytes
  136. // +-------------------+
  137. // 16 bytes total space
  138. #define HMGR_ALLOC_LOCK 0x0001
  139. #define HMGR_ALLOC_ALT_LOCK 0x0002
  140. #define HMGR_NO_ZERO_INIT 0x0004
  141. #define HMGR_MAKE_PUBLIC 0x0008
  142. class DD_ENTRYOBJ : public _DD_ENTRY
  143. {
  144. public:
  145. DD_ENTRYOBJ() { }
  146. ~DD_ENTRYOBJ() { }
  147. VOID vSetup(PDD_OBJ pObj, DD_OBJTYPE objt_, FSHORT fs)
  148. {
  149. DD_OBJECTOWNER ObjNew;
  150. ObjNew = ObjectOwner;
  151. einfo.pobj = (PDD_OBJ) pObj;
  152. Objt = objt_;
  153. Flags = 0;
  154. dwReserved = NULL;
  155. if (fs & HMGR_MAKE_PUBLIC)
  156. {
  157. DD_SET_OBJECTOWNER_PID(ObjNew,OBJECT_OWNER_PUBLIC);
  158. }
  159. else
  160. {
  161. DD_SET_OBJECTOWNER_PID(ObjNew,DD_W32GetCurrentPID());
  162. }
  163. if (fs & HMGR_ALLOC_LOCK)
  164. {
  165. pObj->Tid = (ULONG_PTR)PsGetCurrentThread();
  166. }
  167. pObj->cExclusiveLock = (USHORT)(fs & HMGR_ALLOC_LOCK);
  168. pObj->ulShareCount = (USHORT)0;
  169. //
  170. // Update the ObjectOwner.
  171. //
  172. ObjectOwner = ObjNew;
  173. }
  174. VOID vFree(UINT uiIndex)
  175. {
  176. //
  177. // handle must already be locked
  178. //
  179. DD_ENTRY *pentry = &gpentDdHmgr[uiIndex];
  180. DD_OBJECTOWNER ObjNew = pentry->ObjectOwner;
  181. //
  182. // Insert the specified handle in the free list.
  183. //
  184. pentry->einfo.hFree = ghFreeDdHmgr;
  185. ghFreeDdHmgr = (HDD_OBJ) (ULONG_PTR)uiIndex;
  186. //
  187. // Set the object type to the default type so all handle translations
  188. // will fail and increment the uniqueness value.
  189. //
  190. Objt = (DD_OBJTYPE) DD_DEF_TYPE;
  191. FullUnique += DD_UNIQUE_INCREMENT;
  192. //
  193. // clear user date pointer
  194. //
  195. dwReserved = NULL;
  196. //
  197. // Clear shared count, set initial pid. Caller
  198. // must unlock handle.
  199. //
  200. DD_SET_OBJECTOWNER_PID(ObjNew,0);
  201. pentry->ObjectOwner = ObjNew;
  202. }
  203. BOOL bOwnedBy(W32PID pid_)
  204. {
  205. return((Objt != DD_DEF_TYPE) && (DD_OBJECTOWNER_PID(ObjectOwner) == (pid_ & DD_PID_BITS)));
  206. }
  207. };
  208. typedef DD_ENTRYOBJ *PDD_ENTRYOBJ;