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.

272 lines
5.6 KiB

  1. #include "nwcompat.hxx"
  2. #pragma hdrstop
  3. NWC_CONTEXT BindCache ;
  4. DWORD BindCacheCount = 0 ;
  5. //
  6. // Initializes a cache entry
  7. //
  8. HRESULT BindCacheAllocEntry(NWC_CONTEXT **ppCacheEntry)
  9. {
  10. NWC_CONTEXT *pCacheEntry ;
  11. *ppCacheEntry = NULL ;
  12. if (!(pCacheEntry = (PNWC_CONTEXT)AllocADsMem(sizeof(NWC_CONTEXT)))) {
  13. RRETURN(E_OUTOFMEMORY);
  14. }
  15. pCacheEntry->RefCount = 0;
  16. pCacheEntry->Flags = 0;
  17. pCacheEntry->List.Flink = NULL ;
  18. pCacheEntry->List.Blink = NULL ;
  19. pCacheEntry->pCredentials = NULL;
  20. pCacheEntry->pszBinderyName = NULL;
  21. *ppCacheEntry = pCacheEntry ;
  22. RRETURN(S_OK);
  23. }
  24. //
  25. // Frees a cache entry.
  26. //
  27. // This is not a way to remove entries from the cache --- use BindCacheDef
  28. // for that. It is a way to either free a entry alloc'ed with
  29. // BindCacheAllocEntry that was never added to the cache, or to dealloc
  30. // the entry after it has been removed from the cache (BindCacheDefer
  31. // returned 0).
  32. HRESULT BindCacheFreeEntry(NWC_CONTEXT *pCacheEntry)
  33. {
  34. if (pCacheEntry)
  35. {
  36. if (pCacheEntry->pszBinderyName)
  37. FreeADsMem(pCacheEntry->pszBinderyName) ;
  38. delete pCacheEntry->pCredentials;
  39. FreeADsMem(pCacheEntry);
  40. }
  41. RRETURN(S_OK);
  42. }
  43. //
  44. // Invalidates a cache entry so it will not be used.
  45. //
  46. VOID BindCacheInvalidateEntry(NWC_CONTEXT *pCacheEntry)
  47. {
  48. pCacheEntry->Flags |= NWC_CACHE_INVALID ;
  49. }
  50. //
  51. // Lookup an entry in the cache. Does not take into account timeouts.
  52. // Increments ref count if found.
  53. //
  54. PNWC_CONTEXT
  55. BindCacheLookupByConn(
  56. NWCONN_HANDLE hConn
  57. )
  58. {
  59. DWORD i ;
  60. PNWC_CONTEXT pEntry = (PNWC_CONTEXT) BindCache.List.Flink ;
  61. //
  62. // Loop thru looking for match. A match is defined as:
  63. // hConn, and it is NOT invalid.
  64. //
  65. while (pEntry != &BindCache) {
  66. if (!(pEntry->Flags & NWC_CACHE_INVALID) &&
  67. (pEntry->hConn == hConn)) {
  68. ++pEntry->RefCount ;
  69. return(pEntry) ;
  70. }
  71. pEntry = (PNWC_CONTEXT)pEntry->List.Flink ;
  72. }
  73. return NULL ;
  74. }
  75. //
  76. // Lookup an entry in the cache. Does not take into account timeouts.
  77. // Increments ref count if found.
  78. //
  79. PNWC_CONTEXT
  80. BindCacheLookup(
  81. LPWSTR pszBinderyName,
  82. CCredentials& Credentials
  83. )
  84. {
  85. DWORD i ;
  86. PNWC_CONTEXT pEntry = (PNWC_CONTEXT) BindCache.List.Flink ;
  87. //
  88. // Loop thru looking for match. A match is defined as:
  89. // tree name and credentials, and it is NOT invalid.
  90. //
  91. while (pEntry != &BindCache) {
  92. if (!(pEntry->Flags & NWC_CACHE_INVALID) &&
  93. (((pszBinderyName != NULL) && (pEntry->pszBinderyName != NULL) &&
  94. (_wcsicmp(pEntry->pszBinderyName, pszBinderyName) == 0)) ||
  95. (pEntry->pszBinderyName == NULL && pszBinderyName == NULL)) &&
  96. (*(pEntry->pCredentials) == Credentials)) {
  97. ++pEntry->RefCount ;
  98. return(pEntry) ;
  99. }
  100. pEntry = (PNWC_CONTEXT)pEntry->List.Flink ;
  101. }
  102. return NULL ;
  103. }
  104. //
  105. // Add entry to cache. Returns S_FALSE if there are too many
  106. // entries already in the cache.
  107. //
  108. HRESULT
  109. BindCacheAdd(
  110. LPWSTR pszBinderyName,
  111. CCredentials& Credentials,
  112. BOOL fLoggedIn,
  113. PNWC_CONTEXT pCacheEntry)
  114. {
  115. LPWSTR pszBindery = (LPWSTR) AllocADsMem(
  116. (wcslen(pszBinderyName)+1)*sizeof(WCHAR)) ;
  117. if (!pszBindery) {
  118. RRETURN(E_OUTOFMEMORY);
  119. }
  120. CCredentials * pCredentials = new CCredentials(Credentials);
  121. if (!pCredentials) {
  122. FreeADsMem(pszBindery);
  123. RRETURN(E_OUTOFMEMORY);
  124. }
  125. //
  126. // setup the data
  127. //
  128. wcscpy(pszBindery,pszBinderyName) ;
  129. pCacheEntry->pszBinderyName = pszBindery;
  130. pCacheEntry->pCredentials = pCredentials;
  131. pCacheEntry->RefCount = 1 ;
  132. pCacheEntry->fLoggedIn = fLoggedIn;
  133. //
  134. // insert into list
  135. //
  136. InsertHeadList(&BindCache.List, &pCacheEntry->List) ;
  137. ++BindCacheCount ;
  138. RRETURN(S_OK);
  139. }
  140. //
  141. // Dereference an entry in the cache. Removes if ref count is zero.
  142. // Returns the final ref count or zero if not there. If zero, caller
  143. // should close the handle.
  144. //
  145. DWORD BindCacheDeref(NWC_CONTEXT *pCacheEntry)
  146. {
  147. DWORD i=0;
  148. ENTER_BIND_CRITSECT() ;
  149. if ((pCacheEntry->List.Flink == NULL) &&
  150. (pCacheEntry->List.Blink == NULL) &&
  151. (pCacheEntry->RefCount == NULL)) {
  152. //
  153. // this is one of the entries that never got into the cache.
  154. //
  155. LEAVE_BIND_CRITSECT() ;
  156. return(0) ;
  157. }
  158. ADsAssert(pCacheEntry->List.Flink) ;
  159. ADsAssert(pCacheEntry->RefCount > 0) ;
  160. //
  161. // Dereference by one. If result is non zero, just return.
  162. //
  163. --pCacheEntry->RefCount ;
  164. if (pCacheEntry->RefCount) {
  165. LEAVE_BIND_CRITSECT() ;
  166. return(pCacheEntry->RefCount) ;
  167. }
  168. //
  169. // This entry can be cleaned up.
  170. //
  171. --BindCacheCount ;
  172. RemoveEntryList(&pCacheEntry->List) ;
  173. LEAVE_BIND_CRITSECT() ;
  174. return 0 ;
  175. }
  176. VOID
  177. BindCacheInit(
  178. VOID
  179. )
  180. {
  181. InitializeCriticalSection(&BindCacheCritSect) ;
  182. InitializeListHead(&BindCache.List) ;
  183. }
  184. VOID
  185. BindCacheCleanup(
  186. VOID
  187. )
  188. {
  189. PNWC_CONTEXT pEntry = (PNWC_CONTEXT) BindCache.List.Flink ;
  190. while (pEntry != &BindCache) {
  191. PNWC_CONTEXT pNext = (PNWC_CONTEXT) pEntry->List.Flink;
  192. if (pEntry->pszBinderyName)
  193. FreeADsMem(pEntry->pszBinderyName) ;
  194. pEntry->pszBinderyName = NULL ;
  195. delete pEntry->pCredentials;
  196. pEntry->pCredentials = NULL;
  197. RemoveEntryList(&pEntry->List) ;
  198. BindCacheFreeEntry(pEntry);
  199. pEntry = pNext;
  200. }
  201. DeleteCriticalSection(&BindCacheCritSect);
  202. }