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.

303 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. rsvphndls.c
  5. Abstract:
  6. This file contains the code to create and release handles
  7. Author:
  8. Jim Stewart (JStew) June 10, 1996
  9. Environment:
  10. Revision History:
  11. Ofer Bar (oferbar) Oct 1, 1997 - Revision II
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. PVOID
  16. GetHandleObject(
  17. IN HANDLE h,
  18. IN ENUM_OBJECT_TYPE ObjType
  19. )
  20. {
  21. ENUM_OBJECT_TYPE *p;
  22. GetLock(pGlobals->Lock);
  23. p = (ENUM_OBJECT_TYPE *)dereference_HF_handle(pGlobals->pHandleTbl,
  24. PtrToUlong(h));
  25. if (p != NULL) {
  26. //
  27. // we found a reference for the handle
  28. // we verify that it's the right object type
  29. //
  30. if ((*p & ObjType) == 0) {
  31. //
  32. // sorry, wrong type
  33. //
  34. p = NULL;
  35. }
  36. } else {
  37. IF_DEBUG(HANDLES) {
  38. WSPRINT(("The handle (%x) is invalid\n", h));
  39. DEBUGBREAK();
  40. }
  41. }
  42. FreeLock(pGlobals->Lock);
  43. return (PVOID)p;
  44. }
  45. PVOID
  46. GetHandleObjectWithRef(
  47. IN HANDLE h,
  48. IN ENUM_OBJECT_TYPE ObjType,
  49. IN ULONG RefType
  50. )
  51. {
  52. ENUM_OBJECT_TYPE *p, *p1;
  53. PCLIENT_STRUC pClient;
  54. PFILTER_STRUC pFilter;
  55. PFLOW_STRUC pFlow;
  56. PINTERFACE_STRUC pInterface;
  57. GetLock(pGlobals->Lock);
  58. p = (ENUM_OBJECT_TYPE *) dereference_HF_handle(pGlobals->pHandleTbl,
  59. PtrToUlong(h));
  60. if (p != NULL) {
  61. //
  62. // we found a reference for the handle
  63. // we verify that it's the right object type
  64. //
  65. if ((*p & ObjType) == 0) {
  66. //
  67. // sorry, wrong type
  68. //
  69. p = NULL;
  70. }
  71. }
  72. if (p != NULL) {
  73. p1 = p;
  74. switch (ObjType) {
  75. case ENUM_CLIENT_TYPE:
  76. pClient = (PCLIENT_STRUC)p;
  77. GetLock(pClient->Lock);
  78. if (QUERY_STATE(pClient->State) == OPEN) {
  79. REFADD(&pClient->RefCount, RefType);
  80. } else {
  81. p = NULL; // we can deref a struct that is not open for business
  82. }
  83. FreeLock(pClient->Lock);
  84. break;
  85. case ENUM_FILTER_TYPE:
  86. pFilter = (PFILTER_STRUC)p;
  87. GetLock(pFilter->Lock);
  88. if (QUERY_STATE(pFilter->State) == OPEN) {
  89. REFADD(&pFilter->RefCount, RefType);
  90. } else {
  91. p = NULL;
  92. }
  93. FreeLock(pFilter->Lock);
  94. break;
  95. case ENUM_INTERFACE_TYPE:
  96. pInterface = (PINTERFACE_STRUC)p;
  97. GetLock(pInterface->Lock);
  98. if (QUERY_STATE(pInterface->State) == OPEN) {
  99. REFADD(&pInterface->RefCount, RefType);
  100. } else {
  101. p = NULL;
  102. }
  103. FreeLock(pInterface->Lock);
  104. break;
  105. case ENUM_GEN_FLOW_TYPE:
  106. pFlow = (PFLOW_STRUC)p;
  107. GetLock(pFlow->Lock);
  108. // Return a HANDLE only if it is in OPEN state
  109. // Otherwise return INVALID_HANDLE_VALUE so the
  110. // caller will know that the Flow is not in the
  111. // correct state
  112. if (QUERY_STATE(pFlow->State) == OPEN)
  113. {
  114. REFADD(&pFlow->RefCount, RefType);
  115. } else
  116. {
  117. p = INVALID_HANDLE_VALUE;
  118. }
  119. FreeLock(pFlow->Lock);
  120. break;
  121. case ENUM_CLASS_MAP_FLOW_TYPE:
  122. pFlow = (PFLOW_STRUC)p;
  123. GetLock(pFlow->Lock);
  124. if (QUERY_STATE(pFlow->State) == OPEN) {
  125. REFADD(&pFlow->RefCount, RefType);
  126. } else {
  127. p = NULL;
  128. }
  129. FreeLock(pFlow->Lock);
  130. break;
  131. default:
  132. ASSERT(0);
  133. }
  134. //
  135. // random debug code - please delete
  136. //
  137. IF_DEBUG(HANDLES) {
  138. if (p1 != p) {
  139. WSPRINT(("The object being derefed is NOT in OPEN state p1=%x and p=%x\n", p1, p));
  140. DEBUGBREAK();
  141. }
  142. }
  143. } else {
  144. IF_DEBUG(HANDLES) {
  145. WSPRINT(("The handle (%x) is invalid\n", h));
  146. DEBUGBREAK();
  147. }
  148. }
  149. FreeLock(pGlobals->Lock);
  150. return (PVOID)p;
  151. }
  152. HANDLE
  153. AllocateHandle(
  154. IN PVOID Context
  155. )
  156. /*++
  157. Routine Description:
  158. This function creates a handle.
  159. Arguments:
  160. Context - the context value to store with the handle
  161. Return Value:
  162. The handle factory handle, or NULL in case of en error
  163. --*/
  164. {
  165. HFHandle Handle;
  166. PVOID VerifyCtx;
  167. GetLock( pGlobals->Lock );
  168. Handle = assign_HF_handle(pGlobals->pHandleTbl, Context);
  169. //
  170. // verify the handle is valid
  171. //
  172. if (Handle) {
  173. VerifyCtx = dereference_HF_handle(pGlobals->pHandleTbl, Handle);
  174. ASSERT(VerifyCtx == Context);
  175. IF_DEBUG(HANDLES) {
  176. WSPRINT(("AllocHandle: (%x) being allocated\n", Handle ));
  177. }
  178. }
  179. FreeLock(pGlobals->Lock);
  180. return UlongToPtr(Handle);
  181. }
  182. VOID
  183. FreeHandle(
  184. IN HANDLE Handle
  185. )
  186. /*++
  187. Routine Description:
  188. This function frees the handle
  189. Arguments:
  190. Handle -
  191. Return Value:
  192. --*/
  193. {
  194. int r;
  195. GetLock( pGlobals->Lock );
  196. IF_DEBUG(HANDLES) {
  197. WSPRINT(("FreeHandle (%x) being freed\n", PtrToUlong(Handle) ));
  198. }
  199. r = release_HF_handle(pGlobals->pHandleTbl, PtrToUlong(Handle));
  200. ASSERT(r == 0);
  201. FreeLock(pGlobals->Lock);
  202. }