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.

170 lines
3.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 2000
  3. Module Name:
  4. CtxColl.cxx
  5. Abstract:
  6. Implementation of ConTeXt handle COLLection.
  7. Author:
  8. Kamen Moutafov [KamenM]
  9. Revision History:
  10. KamenM Sep 2000 Created
  11. Notes:
  12. --*/
  13. #include <precomp.hxx>
  14. #include <context.hxx>
  15. #include <SWMR.hxx>
  16. #include <SContext.hxx>
  17. #include <CtxColl.hxx>
  18. ServerContextHandle *
  19. ContextCollection::Find (
  20. IN WIRE_CONTEXT *WireContext
  21. )
  22. /*++
  23. Routine Description:
  24. Finds the server context handle corresponding to the given wire context
  25. Arguments:
  26. WireContext - the context representation as it arrived on the wire
  27. Return Value:
  28. The context handle that was found.
  29. NULL if no matching context handle was found. EEInfo will be added in
  30. this case
  31. Notes:
  32. As a perf optimization, if we search more than 25 elements in the list,
  33. and find an element, we move it to the front of the list to speed up
  34. subsequent searches
  35. --*/
  36. {
  37. LIST_ENTRY *CurrentListEntry;
  38. ServerContextHandle *CurrentContextHandle;
  39. int SearchedElements = 0;
  40. CollectionMutex.VerifyOwned();
  41. CurrentListEntry = ListHead.Flink;
  42. while (CurrentListEntry != &ListHead)
  43. {
  44. CurrentContextHandle = CONTAINING_RECORD(CurrentListEntry, ServerContextHandle, ContextChain);
  45. if (RpcpMemoryCompare(&CurrentContextHandle->WireContext, WireContext, sizeof(WIRE_CONTEXT)) == 0)
  46. {
  47. if (SearchedElements > 25)
  48. {
  49. RpcpfRemoveEntryList(CurrentListEntry);
  50. RpcpfInsertHeadList(&ListHead, CurrentListEntry);
  51. }
  52. return CurrentContextHandle;
  53. }
  54. CurrentListEntry = CurrentListEntry->Flink;
  55. SearchedElements ++;
  56. }
  57. return NULL;
  58. }
  59. RPC_STATUS
  60. ContextCollection::AllocateContextCollection (
  61. OUT ContextCollection **NewColl
  62. )
  63. /*++
  64. Routine Description:
  65. Static function that allocates a new context collection.
  66. Arguments:
  67. NewCol - a placeholder for the new connection. If the function
  68. fails this parameter is undefined. On success, it contains
  69. the new collection.
  70. Return Value:
  71. RPC_S_OK for success or other codes for error.
  72. --*/
  73. {
  74. ContextCollection *NewCollection;
  75. RPC_STATUS RpcStatus = RPC_S_OK;
  76. #if defined(SCONTEXT_UNIT_TESTS)
  77. // once in a great while, fail this
  78. if ((GetRandomLong() % 9999) == 0)
  79. return RPC_S_OUT_OF_MEMORY;
  80. #endif
  81. NewCollection = new ContextCollection(&RpcStatus);
  82. if (NewCollection == NULL)
  83. {
  84. return RPC_S_OUT_OF_MEMORY;
  85. }
  86. else if (RpcStatus != RPC_S_OK)
  87. {
  88. delete NewCollection;
  89. return RpcStatus;
  90. }
  91. *NewColl = NewCollection;
  92. return RPC_S_OK;
  93. }
  94. RPC_STATUS
  95. NDRSContextInitializeCollection (
  96. IN ContextCollection **ContextCollectionPlaceholder
  97. )
  98. /*++
  99. Routine Description:
  100. Static function that can initialize the context collection of an
  101. association in a thread safe manner.
  102. Arguments:
  103. ContextCollectionPlaceholder - a pointer to the pointer to the context
  104. collection.
  105. Return Value:
  106. RPC_S_OK for success or other codes for error.
  107. --*/
  108. {
  109. ContextCollection *NewCollection;
  110. ContextCollection *OldCollection;
  111. RPC_STATUS RpcStatus = RPC_S_OK;
  112. ASSERT(ContextCollectionPlaceholder);
  113. RpcStatus = ContextCollection::AllocateContextCollection(&NewCollection);
  114. if (RpcStatus != RPC_S_OK)
  115. {
  116. return RpcStatus;
  117. }
  118. OldCollection = (ContextCollection *)
  119. InterlockedCompareExchangePointer((PVOID *)ContextCollectionPlaceholder,
  120. NewCollection,
  121. NULL);
  122. if (OldCollection != NULL)
  123. {
  124. delete NewCollection;
  125. }
  126. return RPC_S_OK;
  127. }