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.

405 lines
6.4 KiB

  1. /*
  2. ************************************************************************
  3. Copyright (c) 1996-1997 Microsoft Corporation
  4. Module Name:
  5. gpcmap.c
  6. Abstract:
  7. This file contains mapping routines like user handles to
  8. kernel handles.
  9. Author:
  10. Ofer Bar - July 14, 1997
  11. Environment:
  12. Kernel mode
  13. Revision History:
  14. ************************************************************************
  15. */
  16. #include "gpcpre.h"
  17. /*
  18. /////////////////////////////////////////////////////////////////
  19. //
  20. // globals
  21. //
  22. /////////////////////////////////////////////////////////////////
  23. */
  24. static MRSW_LOCK HandleLock;
  25. static HandleFactory *pMapHandles = NULL;
  26. /*
  27. /////////////////////////////////////////////////////////////////
  28. //
  29. // prototypes
  30. //
  31. /////////////////////////////////////////////////////////////////
  32. */
  33. HANDLE
  34. AllocateHandle(
  35. OUT HANDLE *OutHandle,
  36. IN PVOID Reference
  37. )
  38. /*++
  39. Routine Description:
  40. This function creates a handle.
  41. Arguments:
  42. OutHandle - a pointer to a location to fill in the result handle
  43. Reference - to associate with the handle
  44. Return Value:
  45. The handle factory handle, or NULL in case of en error
  46. --*/
  47. {
  48. HFHandle Handle;
  49. KIRQL irql;
  50. ASSERT(OutHandle);
  51. TRACE(MAPHAND, Reference, OutHandle, "AllocateHandle <==");
  52. WRITE_LOCK( &HandleLock, &irql );
  53. *OutHandle = (HANDLE) UIntToPtr((Handle = assign_HF_handle(pMapHandles, Reference)));
  54. WRITE_UNLOCK( &HandleLock, irql );
  55. StatInc(InsertedHF);
  56. TRACE(MAPHAND, Reference, Handle, "AllocateHandle ==>");
  57. return (HANDLE) UIntToPtr(Handle);
  58. }
  59. VOID
  60. FreeHandle(
  61. IN HANDLE Handle
  62. )
  63. /*++
  64. Routine Description:
  65. This function frees the handle
  66. Arguments:
  67. Handle -
  68. Return Value:
  69. --*/
  70. {
  71. int r;
  72. KIRQL irql;
  73. TRACE(MAPHAND, Handle, 0, "FreeHandle <==");
  74. if (Handle) {
  75. WRITE_LOCK( &HandleLock, &irql );
  76. r = release_HF_handle(pMapHandles, (HFHandle)(UINT_PTR)Handle);
  77. StatInc(RemovedHF);
  78. //ASSERT(r == 0);
  79. WRITE_UNLOCK( &HandleLock, irql );
  80. }
  81. TRACE(MAPHAND, Handle, r, "FreeHandle ==>");
  82. }
  83. VOID
  84. SuspendHandle(
  85. IN HANDLE Handle
  86. )
  87. /*++
  88. Routine Description:
  89. This function suspends the handle
  90. Arguments:
  91. Handle -
  92. Return Value:
  93. --*/
  94. {
  95. int r;
  96. KIRQL irql;
  97. TRACE(MAPHAND, Handle, 0, "SuspendHandle <==");
  98. if (Handle) {
  99. WRITE_LOCK( &HandleLock, &irql );
  100. r = suspend_HF_handle(pMapHandles, (HFHandle)(UINT_PTR)Handle);
  101. //ASSERT(r == 0);
  102. WRITE_UNLOCK( &HandleLock, irql );
  103. }
  104. TRACE(MAPHAND, Handle, r, "SuspendHandle ==>");
  105. }
  106. VOID
  107. ResumeHandle(
  108. IN HANDLE Handle
  109. )
  110. /*++
  111. Routine Description:
  112. This function resumess the handle
  113. Arguments:
  114. Handle -
  115. Return Value:
  116. --*/
  117. {
  118. int r;
  119. KIRQL irql;
  120. TRACE(MAPHAND, Handle, 0, "ResumeHandle <==");
  121. if (Handle) {
  122. WRITE_LOCK( &HandleLock, &irql );
  123. r = reinstate_HF_handle(pMapHandles, (HFHandle)(UINT_PTR)Handle);
  124. //ASSERT(r == 0);
  125. WRITE_UNLOCK( &HandleLock, irql );
  126. }
  127. TRACE(MAPHAND, Handle, r, "ResumeHandle ==>");
  128. }
  129. PVOID
  130. GetHandleObject(
  131. IN HANDLE h,
  132. IN GPC_ENUM_OBJECT_TYPE ObjType
  133. )
  134. {
  135. GPC_ENUM_OBJECT_TYPE *p;
  136. KIRQL irql;
  137. TRACE(MAPHAND, h, ObjType, "GetHandleObject <==");
  138. READ_LOCK(&HandleLock, &irql);
  139. p = (GPC_ENUM_OBJECT_TYPE *)dereference_HF_handle(pMapHandles,
  140. (HFHandle)(UINT_PTR)h);
  141. if (p != NULL) {
  142. //
  143. // we found a reference for the handle
  144. // we verify that it's the right object type
  145. //
  146. if (*p != ObjType) {
  147. //
  148. // sorry, wrong type
  149. //
  150. p = NULL;
  151. }
  152. }
  153. READ_UNLOCK(&HandleLock, irql);
  154. TRACE(MAPHAND, h, p, "GetHandleObject ==>");
  155. return (PVOID)p;
  156. }
  157. PVOID
  158. GetHandleObjectWithRef(
  159. IN HANDLE h,
  160. IN GPC_ENUM_OBJECT_TYPE ObjType,
  161. IN ULONG Ref
  162. )
  163. {
  164. GPC_ENUM_OBJECT_TYPE *p;
  165. KIRQL irql;
  166. TRACE(MAPHAND, h, ObjType, "GetHandleObjectWithRef ==>");
  167. READ_LOCK( &HandleLock, &irql );
  168. p = dereference_HF_handle(pMapHandles, (HFHandle)(ULONG_PTR)h);
  169. if (p != NULL) {
  170. //
  171. // we found a reference for the handle
  172. // we verify that it's the right object type
  173. //
  174. if (*p != ObjType) {
  175. //
  176. // sorry, wrong type
  177. //
  178. p = NULL;
  179. }
  180. }
  181. if (p != NULL) {
  182. switch (ObjType) {
  183. case GPC_ENUM_CFINFO_TYPE:
  184. REFADD(&((PBLOB_BLOCK)p)->RefCount, Ref);
  185. break;
  186. case GPC_ENUM_CLIENT_TYPE:
  187. REFADD(&((PCLIENT_BLOCK)p)->RefCount, Ref);
  188. break;
  189. case GPC_ENUM_PATTERN_TYPE:
  190. REFADD(&((PPATTERN_BLOCK)p)->RefCount, Ref);
  191. break;
  192. default:
  193. ASSERT(0);
  194. }
  195. }
  196. READ_UNLOCK( &HandleLock, irql );
  197. TRACE(MAPHAND, h, p, "GetHandleObjectWithRef <==");
  198. return (PVOID)p;
  199. }
  200. /*
  201. ************************************************************************
  202. InitMapHandles -
  203. The initialization handle mapping table
  204. Arguments
  205. none
  206. Returns
  207. GPC_STATUS
  208. ************************************************************************
  209. */
  210. GPC_STATUS
  211. InitMapHandles(VOID)
  212. {
  213. GPC_STATUS Status = GPC_STATUS_SUCCESS;
  214. TRACE(INIT, 0, 0, "InitMapping");
  215. INIT_LOCK(&HandleLock);
  216. NEW_HandleFactory(pMapHandles);
  217. if (pMapHandles != NULL ) {
  218. if (constructHandleFactory(pMapHandles)) {
  219. FreeHandleFactory(pMapHandles);
  220. Status = GPC_STATUS_RESOURCES;
  221. }
  222. } else {
  223. Status = GPC_STATUS_RESOURCES;
  224. }
  225. TRACE(INIT, pMapHandles, Status, "InitMapping");
  226. return Status;
  227. }
  228. /*
  229. ************************************************************************
  230. UninitMapHandles -
  231. release handle mapping table resources
  232. Arguments
  233. none
  234. Returns
  235. void
  236. ************************************************************************
  237. */
  238. VOID
  239. UninitMapHandles(VOID)
  240. {
  241. GPC_STATUS Status = GPC_STATUS_SUCCESS;
  242. TRACE(INIT, 0, 0, "UninitMapHandles");
  243. //NdisFreeSpinLock(&HandleLock);
  244. destructHandleFactory(pMapHandles);
  245. FreeHandleFactory(pMapHandles);
  246. TRACE(INIT, pMapHandles, Status, "UninitMapHandles");
  247. return;
  248. }