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.

408 lines
7.1 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. // (a) Determine the memory pointer that the handle points to
  158. // (b) Verify that the memory is of the correct BLOCK (ObjType enum is checked)
  159. // (c) Verify that the Handle associated with the memory pointer is indeed
  160. // what was passed in
  161. PVOID
  162. GetHandleObjectWithRef(
  163. IN HANDLE h,
  164. IN GPC_ENUM_OBJECT_TYPE ObjType,
  165. IN ULONG Ref
  166. )
  167. {
  168. GPC_ENUM_OBJECT_TYPE *p;
  169. KIRQL irql;
  170. TRACE(MAPHAND, h, ObjType, "GetHandleObjectWithRef ==>");
  171. READ_LOCK( &HandleLock, &irql );
  172. p = dereference_HF_handle(pMapHandles, (HFHandle)(ULONG_PTR)h);
  173. if (p != NULL) {
  174. //
  175. // we found a reference for the handle
  176. // we verify that it's the right object type
  177. //
  178. if (*p != ObjType) {
  179. //
  180. // sorry, wrong type
  181. //
  182. p = NULL;
  183. }
  184. }
  185. if (p != NULL) {
  186. switch (ObjType) {
  187. case GPC_ENUM_CFINFO_TYPE:
  188. REFADD(&((PBLOB_BLOCK)p)->RefCount, Ref);
  189. break;
  190. case GPC_ENUM_CLIENT_TYPE:
  191. REFADD(&((PCLIENT_BLOCK)p)->RefCount, Ref);
  192. break;
  193. case GPC_ENUM_PATTERN_TYPE:
  194. REFADD(&((PPATTERN_BLOCK)p)->RefCount, Ref);
  195. break;
  196. default:
  197. ASSERT(0);
  198. }
  199. }
  200. READ_UNLOCK( &HandleLock, irql );
  201. TRACE(MAPHAND, h, p, "GetHandleObjectWithRef <==");
  202. return (PVOID)p;
  203. }
  204. /*
  205. ************************************************************************
  206. InitMapHandles -
  207. The initialization handle mapping table
  208. Arguments
  209. none
  210. Returns
  211. GPC_STATUS
  212. ************************************************************************
  213. */
  214. GPC_STATUS
  215. InitMapHandles(VOID)
  216. {
  217. GPC_STATUS Status = GPC_STATUS_SUCCESS;
  218. TRACE(INIT, 0, 0, "InitMapping");
  219. INIT_LOCK(&HandleLock);
  220. NEW_HandleFactory(pMapHandles);
  221. if (pMapHandles != NULL ) {
  222. if (constructHandleFactory(pMapHandles)) {
  223. FreeHandleFactory(pMapHandles);
  224. Status = GPC_STATUS_RESOURCES;
  225. }
  226. } else {
  227. Status = GPC_STATUS_RESOURCES;
  228. }
  229. TRACE(INIT, pMapHandles, Status, "InitMapping");
  230. return Status;
  231. }
  232. /*
  233. ************************************************************************
  234. UninitMapHandles -
  235. release handle mapping table resources
  236. Arguments
  237. none
  238. Returns
  239. void
  240. ************************************************************************
  241. */
  242. VOID
  243. UninitMapHandles(VOID)
  244. {
  245. GPC_STATUS Status = GPC_STATUS_SUCCESS;
  246. TRACE(INIT, 0, 0, "UninitMapHandles");
  247. //NdisFreeSpinLock(&HandleLock);
  248. destructHandleFactory(pMapHandles);
  249. FreeHandleFactory(pMapHandles);
  250. TRACE(INIT, pMapHandles, Status, "UninitMapHandles");
  251. return;
  252. }