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.

1219 lines
31 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. Gentable.c
  5. Abstract:
  6. This module implements the generic table package.
  7. Author:
  8. Gary Kimura [GaryKi] 23-May-1989
  9. Environment:
  10. Pure Utility Routines
  11. Revision History:
  12. Anthony V. Ercolano [tonye] 23-May-1990
  13. Implement package.
  14. Anthony V. Ercolano [tonye] 1-Jun-1990
  15. Added ability to get elements out in the order
  16. inserted. *NOTE* *NOTE* This depends on the implicit
  17. ordering of record fields:
  18. SPLAY_LINKS,
  19. LIST_ENTRY,
  20. USER_DATA
  21. --*/
  22. #include <nt.h>
  23. #include <ntrtl.h>
  24. #pragma pack(8)
  25. //
  26. // This structure is the header for a generic table entry.
  27. // Align this structure on a 8 byte boundary so the user
  28. // data is correctly aligned.
  29. //
  30. typedef struct _TABLE_ENTRY_HEADER {
  31. RTL_SPLAY_LINKS SplayLinks;
  32. LIST_ENTRY ListEntry;
  33. LONGLONG UserData;
  34. } TABLE_ENTRY_HEADER, *PTABLE_ENTRY_HEADER;
  35. #pragma pack()
  36. static
  37. TABLE_SEARCH_RESULT
  38. FindNodeOrParent(
  39. IN PRTL_GENERIC_TABLE Table,
  40. IN PVOID Buffer,
  41. OUT PRTL_SPLAY_LINKS *NodeOrParent
  42. )
  43. /*++
  44. Routine Description:
  45. This routine is used by all of the routines of the generic
  46. table package to locate the a node in the tree. It will
  47. find and return (via the NodeOrParent parameter) the node
  48. with the given key, or if that node is not in the tree it
  49. will return (via the NodeOrParent parameter) a pointer to
  50. the parent.
  51. Arguments:
  52. Table - The generic table to search for the key.
  53. Buffer - Pointer to a buffer holding the key. The table
  54. package doesn't examine the key itself. It leaves
  55. this up to the user supplied compare routine.
  56. NodeOrParent - Will be set to point to the node containing the
  57. the key or what should be the parent of the node
  58. if it were in the tree. Note that this will *NOT*
  59. be set if the search result is TableEmptyTree.
  60. Return Value:
  61. TABLE_SEARCH_RESULT - TableEmptyTree: The tree was empty. NodeOrParent
  62. is *not* altered.
  63. TableFoundNode: A node with the key is in the tree.
  64. NodeOrParent points to that node.
  65. TableInsertAsLeft: Node with key was not found.
  66. NodeOrParent points to what would be
  67. parent. The node would be the left
  68. child.
  69. TableInsertAsRight: Node with key was not found.
  70. NodeOrParent points to what would be
  71. parent. The node would be the right
  72. child.
  73. --*/
  74. {
  75. if (RtlIsGenericTableEmpty(Table)) {
  76. return TableEmptyTree;
  77. } else {
  78. //
  79. // Used as the iteration variable while stepping through
  80. // the generic table.
  81. //
  82. PRTL_SPLAY_LINKS NodeToExamine = Table->TableRoot;
  83. //
  84. // Just a temporary. Hopefully a good compiler will get
  85. // rid of it.
  86. //
  87. PRTL_SPLAY_LINKS Child;
  88. //
  89. // Holds the value of the comparasion.
  90. //
  91. RTL_GENERIC_COMPARE_RESULTS Result;
  92. while (TRUE) {
  93. //
  94. // Compare the buffer with the key in the tree element.
  95. //
  96. Result = Table->CompareRoutine(
  97. Table,
  98. Buffer,
  99. &((PTABLE_ENTRY_HEADER) NodeToExamine)->UserData
  100. );
  101. if (Result == GenericLessThan) {
  102. if (Child = RtlLeftChild(NodeToExamine)) {
  103. NodeToExamine = Child;
  104. } else {
  105. //
  106. // Node is not in the tree. Set the output
  107. // parameter to point to what would be its
  108. // parent and return which child it would be.
  109. //
  110. *NodeOrParent = NodeToExamine;
  111. return TableInsertAsLeft;
  112. }
  113. } else if (Result == GenericGreaterThan) {
  114. if (Child = RtlRightChild(NodeToExamine)) {
  115. NodeToExamine = Child;
  116. } else {
  117. //
  118. // Node is not in the tree. Set the output
  119. // parameter to point to what would be its
  120. // parent and return which child it would be.
  121. //
  122. *NodeOrParent = NodeToExamine;
  123. return TableInsertAsRight;
  124. }
  125. } else {
  126. //
  127. // Node is in the tree (or it better be because of the
  128. // assert). Set the output parameter to point to
  129. // the node and tell the caller that we found the node.
  130. //
  131. ASSERT(Result == GenericEqual);
  132. *NodeOrParent = NodeToExamine;
  133. return TableFoundNode;
  134. }
  135. }
  136. }
  137. }
  138. VOID
  139. RtlInitializeGenericTable (
  140. IN PRTL_GENERIC_TABLE Table,
  141. IN PRTL_GENERIC_COMPARE_ROUTINE CompareRoutine,
  142. IN PRTL_GENERIC_ALLOCATE_ROUTINE AllocateRoutine,
  143. IN PRTL_GENERIC_FREE_ROUTINE FreeRoutine,
  144. IN PVOID TableContext
  145. )
  146. /*++
  147. Routine Description:
  148. The procedure InitializeGenericTable takes as input an uninitialized
  149. generic table variable and pointers to the three user supplied routines.
  150. This must be called for every individual generic table variable before
  151. it can be used.
  152. Arguments:
  153. Table - Pointer to the generic table to be initialized.
  154. CompareRoutine - User routine to be used to compare to keys in the
  155. table.
  156. AllocateRoutine - User routine to call to allocate memory for a new
  157. node in the generic table.
  158. FreeRoutine - User routine to call to deallocate memory for
  159. a node in the generic table.
  160. TableContext - Supplies user supplied context for the table.
  161. Return Value:
  162. None.
  163. --*/
  164. {
  165. //
  166. // Initialize each field of the Table parameter.
  167. //
  168. Table->TableRoot = NULL;
  169. InitializeListHead(&Table->InsertOrderList);
  170. Table->NumberGenericTableElements = 0;
  171. Table->OrderedPointer = &Table->InsertOrderList;
  172. Table->WhichOrderedElement = 0;
  173. Table->CompareRoutine = CompareRoutine;
  174. Table->AllocateRoutine = AllocateRoutine;
  175. Table->FreeRoutine = FreeRoutine;
  176. Table->TableContext = TableContext;
  177. }
  178. PVOID
  179. RtlInsertElementGenericTable (
  180. IN PRTL_GENERIC_TABLE Table,
  181. IN PVOID Buffer,
  182. IN CLONG BufferSize,
  183. OUT PBOOLEAN NewElement OPTIONAL
  184. )
  185. /*++
  186. Routine Description:
  187. The function InsertElementGenericTable will insert a new element
  188. in a table. It does this by allocating space for the new element
  189. (this includes splay links), inserting the element in the table, and
  190. then returning to the user a pointer to the new element (which is
  191. the first available space after the splay links). If an element
  192. with the same key already exists in the table the return value is a pointer
  193. to the old element. The optional output parameter NewElement is used
  194. to indicate if the element previously existed in the table. Note: the user
  195. supplied Buffer is only used for searching the table, upon insertion its
  196. contents are copied to the newly created element. This means that
  197. pointer to the input buffer will not point to the new element.
  198. Arguments:
  199. Table - Pointer to the table in which to (possibly) insert the
  200. key buffer.
  201. Buffer - Passed to the user comparasion routine. Its contents are
  202. up to the user but one could imagine that it contains some
  203. sort of key value.
  204. BufferSize - The amount of space to allocate when the (possible)
  205. insertion is made. Note that if we actually do
  206. not find the node and we do allocate space then we
  207. will add the size of the SPLAY_LINKS to this buffer
  208. size. The user should really take care not to depend
  209. on anything in the first sizeof(SPLAY_LINKS) bytes
  210. of the memory allocated via the memory allocation
  211. routine.
  212. NewElement - Optional Flag. If present then it will be set to
  213. TRUE if the buffer was not "found" in the generic
  214. table.
  215. Return Value:
  216. PVOID - Pointer to the user defined data.
  217. --*/
  218. {
  219. //
  220. // Holds a pointer to the node in the table or what would be the
  221. // parent of the node.
  222. //
  223. PRTL_SPLAY_LINKS NodeOrParent;
  224. //
  225. // Holds the result of the table lookup.
  226. //
  227. TABLE_SEARCH_RESULT Lookup;
  228. Lookup = FindNodeOrParent(
  229. Table,
  230. Buffer,
  231. &NodeOrParent
  232. );
  233. //
  234. // Call the full routine to do the real work.
  235. //
  236. return RtlInsertElementGenericTableFull(
  237. Table,
  238. Buffer,
  239. BufferSize,
  240. NewElement,
  241. NodeOrParent,
  242. Lookup
  243. );
  244. }
  245. PVOID
  246. RtlInsertElementGenericTableFull (
  247. IN PRTL_GENERIC_TABLE Table,
  248. IN PVOID Buffer,
  249. IN CLONG BufferSize,
  250. OUT PBOOLEAN NewElement OPTIONAL,
  251. PVOID NodeOrParent,
  252. TABLE_SEARCH_RESULT SearchResult
  253. )
  254. /*++
  255. Routine Description:
  256. The function InsertElementGenericTableFull will insert a new element
  257. in a table. It does this by allocating space for the new element
  258. (this includes splay links), inserting the element in the table, and
  259. then returning to the user a pointer to the new element. If an element
  260. with the same key already exists in the table the return value is a pointer
  261. to the old element. The optional output parameter NewElement is used
  262. to indicate if the element previously existed in the table. Note: the user
  263. supplied Buffer is only used for searching the table, upon insertion its
  264. contents are copied to the newly created element. This means that
  265. pointer to the input buffer will not point to the new element.
  266. This routine is passed the NodeOrParent and SearchResult from a
  267. previous RtlLookupElementGenericTableFull.
  268. Arguments:
  269. Table - Pointer to the table in which to (possibly) insert the
  270. key buffer.
  271. Buffer - Passed to the user comparasion routine. Its contents are
  272. up to the user but one could imagine that it contains some
  273. sort of key value.
  274. BufferSize - The amount of space to allocate when the (possible)
  275. insertion is made. Note that if we actually do
  276. not find the node and we do allocate space then we
  277. will add the size of the SPLAY_LINKS to this buffer
  278. size. The user should really take care not to depend
  279. on anything in the first sizeof(SPLAY_LINKS) bytes
  280. of the memory allocated via the memory allocation
  281. routine.
  282. NewElement - Optional Flag. If present then it will be set to
  283. TRUE if the buffer was not "found" in the generic
  284. table.
  285. NodeOrParent - Result of prior RtlLookupElementGenericTableFull.
  286. SearchResult - Result of prior RtlLookupElementGenericTableFull.
  287. Return Value:
  288. PVOID - Pointer to the user defined data.
  289. --*/
  290. {
  291. //
  292. // Node will point to the splay links of what
  293. // will be returned to the user.
  294. //
  295. PRTL_SPLAY_LINKS NodeToReturn;
  296. if (SearchResult != TableFoundNode) {
  297. //
  298. // We just check that the table isn't getting
  299. // too big.
  300. //
  301. ASSERT(Table->NumberGenericTableElements != (MAXULONG-1));
  302. //
  303. // The node wasn't in the (possibly empty) tree.
  304. // Call the user allocation routine to get space
  305. // for the new node.
  306. //
  307. NodeToReturn = Table->AllocateRoutine(
  308. Table,
  309. BufferSize+FIELD_OFFSET( TABLE_ENTRY_HEADER, UserData )
  310. );
  311. //
  312. // If the return is NULL, return NULL from here to indicate that
  313. // the entry could not be added.
  314. //
  315. if (NodeToReturn == NULL) {
  316. if (ARGUMENT_PRESENT(NewElement)) {
  317. *NewElement = FALSE;
  318. }
  319. return(NULL);
  320. }
  321. RtlInitializeSplayLinks(NodeToReturn);
  322. //
  323. // Insert the new node at the end of the ordered linked list.
  324. //
  325. InsertTailList(
  326. &Table->InsertOrderList,
  327. &((PTABLE_ENTRY_HEADER) NodeToReturn)->ListEntry
  328. );
  329. Table->NumberGenericTableElements++;
  330. //
  331. // Insert the new node in the tree.
  332. //
  333. if (SearchResult == TableEmptyTree) {
  334. Table->TableRoot = NodeToReturn;
  335. } else {
  336. if (SearchResult == TableInsertAsLeft) {
  337. RtlInsertAsLeftChild(
  338. NodeOrParent,
  339. NodeToReturn
  340. );
  341. } else {
  342. RtlInsertAsRightChild(
  343. NodeOrParent,
  344. NodeToReturn
  345. );
  346. }
  347. }
  348. //
  349. // Copy the users buffer into the user data area of the table.
  350. //
  351. RtlCopyMemory(
  352. &((PTABLE_ENTRY_HEADER) NodeToReturn)->UserData,
  353. Buffer,
  354. BufferSize
  355. );
  356. } else {
  357. NodeToReturn = NodeOrParent;
  358. }
  359. //
  360. // Always splay the (possibly) new node.
  361. //
  362. Table->TableRoot = RtlSplay(NodeToReturn);
  363. if (ARGUMENT_PRESENT(NewElement)) {
  364. *NewElement = ((SearchResult == TableFoundNode)?(FALSE):(TRUE));
  365. }
  366. //
  367. // Insert the element on the ordered list;
  368. //
  369. return &((PTABLE_ENTRY_HEADER) NodeToReturn)->UserData;
  370. }
  371. BOOLEAN
  372. RtlDeleteElementGenericTable (
  373. IN PRTL_GENERIC_TABLE Table,
  374. IN PVOID Buffer
  375. )
  376. /*++
  377. Routine Description:
  378. The function DeleteElementGenericTable will find and delete an element
  379. from a generic table. If the element is located and deleted the return
  380. value is TRUE, otherwise if the element is not located the return value
  381. is FALSE. The user supplied input buffer is only used as a key in
  382. locating the element in the table.
  383. Arguments:
  384. Table - Pointer to the table in which to (possibly) delete the
  385. memory accessed by the key buffer.
  386. Buffer - Passed to the user comparasion routine. Its contents are
  387. up to the user but one could imagine that it contains some
  388. sort of key value.
  389. Return Value:
  390. BOOLEAN - If the table contained the key then true, otherwise false.
  391. --*/
  392. {
  393. //
  394. // Holds a pointer to the node in the table or what would be the
  395. // parent of the node.
  396. //
  397. PRTL_SPLAY_LINKS NodeOrParent;
  398. //
  399. // Holds the result of the table lookup.
  400. //
  401. TABLE_SEARCH_RESULT Lookup;
  402. Lookup = FindNodeOrParent(
  403. Table,
  404. Buffer,
  405. &NodeOrParent
  406. );
  407. if ((Lookup == TableEmptyTree) || (Lookup != TableFoundNode)) {
  408. return FALSE;
  409. } else {
  410. //
  411. // Delete the node from the splay tree.
  412. //
  413. Table->TableRoot = RtlDelete(NodeOrParent);
  414. //
  415. // Delete the element from the linked list.
  416. //
  417. RemoveEntryList(&((PTABLE_ENTRY_HEADER) NodeOrParent)->ListEntry);
  418. Table->NumberGenericTableElements--;
  419. Table->WhichOrderedElement = 0;
  420. Table->OrderedPointer = &Table->InsertOrderList;
  421. //
  422. // The node has been deleted from the splay table.
  423. // Now give the node to the user deletion routine.
  424. // NOTE: We are giving the deletion routine a pointer
  425. // to the splay links rather then the user data. It
  426. // is assumed that the deallocation is rather bad.
  427. //
  428. Table->FreeRoutine(Table,NodeOrParent);
  429. return TRUE;
  430. }
  431. }
  432. PVOID
  433. RtlLookupElementGenericTable (
  434. IN PRTL_GENERIC_TABLE Table,
  435. IN PVOID Buffer
  436. )
  437. /*++
  438. Routine Description:
  439. The function LookupElementGenericTable will find an element in a generic
  440. table. If the element is located the return value is a pointer to
  441. the user defined structure associated with the element, otherwise if
  442. the element is not located the return value is NULL. The user supplied
  443. input buffer is only used as a key in locating the element in the table.
  444. Arguments:
  445. Table - Pointer to the users Generic table to search for the key.
  446. Buffer - Used for the comparasion.
  447. Return Value:
  448. PVOID - returns a pointer to the user data.
  449. --*/
  450. {
  451. //
  452. // Holds a pointer to the node in the table or what would be the
  453. // parent of the node.
  454. //
  455. PRTL_SPLAY_LINKS NodeOrParent;
  456. //
  457. // Holds the result of the table lookup.
  458. //
  459. TABLE_SEARCH_RESULT Lookup;
  460. return RtlLookupElementGenericTableFull(
  461. Table,
  462. Buffer,
  463. &NodeOrParent,
  464. &Lookup
  465. );
  466. }
  467. PVOID
  468. NTAPI
  469. RtlLookupElementGenericTableFull (
  470. PRTL_GENERIC_TABLE Table,
  471. PVOID Buffer,
  472. OUT PVOID *NodeOrParent,
  473. OUT TABLE_SEARCH_RESULT *SearchResult
  474. )
  475. /*++
  476. Routine Description:
  477. The function LookupElementGenericTableFull will find an element in a generic
  478. table. If the element is located the return value is a pointer to
  479. the user defined structure associated with the element. If the element is not
  480. located then a pointer to the parent for the insert location is returned. The
  481. user must look at the SearchResult value to determine which is being returned.
  482. The user can use the SearchResult and parent for a subsequent FullInsertElement
  483. call to optimize the insert.
  484. Arguments:
  485. Table - Pointer to the users Generic table to search for the key.
  486. Buffer - Used for the comparasion.
  487. NodeOrParent - Address to store the desired Node or parent of the desired node.
  488. SearchResult - Describes the relationship of the NodeOrParent with the desired Node.
  489. Return Value:
  490. PVOID - returns a pointer to the user data.
  491. --*/
  492. {
  493. //
  494. // Lookup the element and save the result.
  495. //
  496. *SearchResult = FindNodeOrParent(
  497. Table,
  498. Buffer,
  499. (PRTL_SPLAY_LINKS *)NodeOrParent
  500. );
  501. if ((*SearchResult == TableEmptyTree) || (*SearchResult != TableFoundNode)) {
  502. return NULL;
  503. } else {
  504. //
  505. // Splay the tree with this node.
  506. //
  507. Table->TableRoot = RtlSplay(*NodeOrParent);
  508. //
  509. // Return a pointer to the user data.
  510. //
  511. return &((PTABLE_ENTRY_HEADER)*NodeOrParent)->UserData;
  512. }
  513. }
  514. PVOID
  515. RtlEnumerateGenericTable (
  516. IN PRTL_GENERIC_TABLE Table,
  517. IN BOOLEAN Restart
  518. )
  519. /*++
  520. Routine Description:
  521. The function EnumerateGenericTable will return to the caller one-by-one
  522. the elements of of a table. The return value is a pointer to the user
  523. defined structure associated with the element. The input parameter
  524. Restart indicates if the enumeration should start from the beginning
  525. or should return the next element. If the are no more new elements to
  526. return the return value is NULL. As an example of its use, to enumerate
  527. all of the elements in a table the user would write:
  528. for (ptr = EnumerateGenericTable(Table,TRUE);
  529. ptr != NULL;
  530. ptr = EnumerateGenericTable(Table, FALSE)) {
  531. :
  532. }
  533. Arguments:
  534. Table - Pointer to the generic table to enumerate.
  535. Restart - Flag that if true we should start with the least
  536. element in the tree otherwise, return we return
  537. a pointer to the user data for the root and make
  538. the real successor to the root the new root.
  539. Return Value:
  540. PVOID - Pointer to the user data.
  541. --*/
  542. {
  543. if (RtlIsGenericTableEmpty(Table)) {
  544. //
  545. // Nothing to do if the table is empty.
  546. //
  547. return NULL;
  548. } else {
  549. //
  550. // Will be used as the "iteration" through the tree.
  551. //
  552. PRTL_SPLAY_LINKS NodeToReturn;
  553. //
  554. // If the restart flag is true then go to the least element
  555. // in the tree.
  556. //
  557. if (Restart) {
  558. //
  559. // We just loop until we find the leftmost child of the root.
  560. //
  561. for (
  562. NodeToReturn = Table->TableRoot;
  563. RtlLeftChild(NodeToReturn);
  564. NodeToReturn = RtlLeftChild(NodeToReturn)
  565. ) {
  566. ;
  567. }
  568. Table->TableRoot = RtlSplay(NodeToReturn);
  569. } else {
  570. //
  571. // The assumption here is that the root of the
  572. // tree is the last node that we returned. We
  573. // find the real successor to the root and return
  574. // it as next element of the enumeration. The
  575. // node that is to be returned is splayed (thereby
  576. // making it the root of the tree). Note that we
  577. // need to take care when there are no more elements.
  578. //
  579. NodeToReturn = RtlRealSuccessor(Table->TableRoot);
  580. if (NodeToReturn) {
  581. Table->TableRoot = RtlSplay(NodeToReturn);
  582. }
  583. }
  584. //
  585. // If there actually is a next element in the enumeration
  586. // then the pointer to return is right after the list links.
  587. //
  588. return ((NodeToReturn)?
  589. ((PVOID)&((PTABLE_ENTRY_HEADER)NodeToReturn)->UserData)
  590. :((PVOID)(NULL)));
  591. }
  592. }
  593. BOOLEAN
  594. RtlIsGenericTableEmpty (
  595. IN PRTL_GENERIC_TABLE Table
  596. )
  597. /*++
  598. Routine Description:
  599. The function IsGenericTableEmpty will return to the caller TRUE if
  600. the input table is empty (i.e., does not contain any elements) and
  601. FALSE otherwise.
  602. Arguments:
  603. Table - Supplies a pointer to the Generic Table.
  604. Return Value:
  605. BOOLEAN - if enabled the tree is empty.
  606. --*/
  607. {
  608. //
  609. // Table is empty if the root pointer is null.
  610. //
  611. return ((Table->TableRoot)?(FALSE):(TRUE));
  612. }
  613. PVOID
  614. RtlGetElementGenericTable (
  615. IN PRTL_GENERIC_TABLE Table,
  616. IN ULONG I
  617. )
  618. /*++
  619. Routine Description:
  620. The function GetElementGenericTable will return the i'th element
  621. inserted in the generic table. I = 0 implies the first element,
  622. I = (RtlNumberGenericTableElements(Table)-1) will return the last element
  623. inserted into the generic table. The type of I is ULONG. Values
  624. of I > than (NumberGenericTableElements(Table)-1) will return NULL. If
  625. an arbitrary element is deleted from the generic table it will cause
  626. all elements inserted after the deleted element to "move up".
  627. Arguments:
  628. Table - Pointer to the generic table from which to get the ith element.
  629. I - Which element to get.
  630. Return Value:
  631. PVOID - Pointer to the user data.
  632. --*/
  633. {
  634. //
  635. // Current location in the table.
  636. //
  637. ULONG CurrentLocation = Table->WhichOrderedElement;
  638. //
  639. // Hold the number of elements in the table.
  640. //
  641. ULONG NumberInTable = Table->NumberGenericTableElements;
  642. //
  643. // Holds the value of I+1.
  644. //
  645. // Note that we don't care if this value overflows.
  646. // If we end up accessing it we know that it didn't.
  647. //
  648. ULONG NormalizedI = I + 1;
  649. //
  650. // Will hold distances to travel to the desired node;
  651. //
  652. ULONG ForwardDistance,BackwardDistance;
  653. //
  654. // Will point to the current element in the linked list.
  655. //
  656. PLIST_ENTRY CurrentNode = Table->OrderedPointer;
  657. //
  658. // If it's out of bounds get out quick.
  659. //
  660. if ((I == MAXULONG) || (NormalizedI > NumberInTable)) return NULL;
  661. //
  662. // If we're already at the node then return it.
  663. //
  664. if (NormalizedI == CurrentLocation) {
  665. return &((PTABLE_ENTRY_HEADER) CONTAINING_RECORD(CurrentNode, TABLE_ENTRY_HEADER, ListEntry))->UserData;
  666. }
  667. //
  668. // Calculate the forward and backward distance to the node.
  669. //
  670. if (CurrentLocation > NormalizedI) {
  671. //
  672. // When CurrentLocation is greater than where we want to go,
  673. // if moving forward gets us there quicker than moving backward
  674. // then it follows that moving forward from the listhead is
  675. // going to take fewer steps. (This is because, moving forward
  676. // in this case must move *through* the listhead.)
  677. //
  678. // The work here is to figure out if moving backward would be quicker.
  679. //
  680. // Moving backward would be quicker only if the location we wish to
  681. // go to is more than half way between the listhead and where we
  682. // currently are.
  683. //
  684. if (NormalizedI > (CurrentLocation/2)) {
  685. //
  686. // Where we want to go is more than half way from the listhead
  687. // We can traval backwards from our current location.
  688. //
  689. for (
  690. BackwardDistance = CurrentLocation - NormalizedI;
  691. BackwardDistance;
  692. BackwardDistance--
  693. ) {
  694. CurrentNode = CurrentNode->Blink;
  695. }
  696. } else {
  697. //
  698. // Where we want to go is less than halfway between the start
  699. // and where we currently are. Start from the listhead.
  700. //
  701. for (
  702. CurrentNode = &Table->InsertOrderList;
  703. NormalizedI;
  704. NormalizedI--
  705. ) {
  706. CurrentNode = CurrentNode->Flink;
  707. }
  708. }
  709. } else {
  710. //
  711. // When CurrentLocation is less than where we want to go,
  712. // if moving backwards gets us there quicker than moving forwards
  713. // then it follows that moving backwards from the listhead is
  714. // going to take fewer steps. (This is because, moving backwards
  715. // in this case must move *through* the listhead.)
  716. //
  717. ForwardDistance = NormalizedI - CurrentLocation;
  718. //
  719. // Do the backwards calculation as if we are starting from the
  720. // listhead.
  721. //
  722. BackwardDistance = (NumberInTable - NormalizedI) + 1;
  723. if (ForwardDistance <= BackwardDistance) {
  724. for (
  725. ;
  726. ForwardDistance;
  727. ForwardDistance--
  728. ) {
  729. CurrentNode = CurrentNode->Flink;
  730. }
  731. } else {
  732. for (
  733. CurrentNode = &Table->InsertOrderList;
  734. BackwardDistance;
  735. BackwardDistance--
  736. ) {
  737. CurrentNode = CurrentNode->Blink;
  738. }
  739. }
  740. }
  741. //
  742. // We're where we want to be. Save our current location and return
  743. // a pointer to the data to the user.
  744. //
  745. Table->OrderedPointer = CurrentNode;
  746. Table->WhichOrderedElement = I+1;
  747. return &((PTABLE_ENTRY_HEADER) CONTAINING_RECORD(CurrentNode, TABLE_ENTRY_HEADER, ListEntry))->UserData;
  748. }
  749. ULONG
  750. RtlNumberGenericTableElements(
  751. IN PRTL_GENERIC_TABLE Table
  752. )
  753. /*++
  754. Routine Description:
  755. The function NumberGenericTableElements returns a ULONG value
  756. which is the number of generic table elements currently inserted
  757. in the generic table.
  758. Arguments:
  759. Table - Pointer to the generic table from which to find out the number
  760. of elements.
  761. Return Value:
  762. ULONG - The number of elements in the generic table.
  763. --*/
  764. {
  765. return Table->NumberGenericTableElements;
  766. }
  767. PVOID
  768. RtlEnumerateGenericTableWithoutSplaying (
  769. IN PRTL_GENERIC_TABLE Table,
  770. IN PVOID *RestartKey
  771. )
  772. /*++
  773. Routine Description:
  774. The function EnumerateGenericTableWithoutSplaying will return to the
  775. caller one-by-one the elements of of a table. The return value is a
  776. pointer to the user defined structure associated with the element.
  777. The input parameter RestartKey indicates if the enumeration should
  778. start from the beginning or should return the next element. If the
  779. are no more new elements to return the return value is NULL. As an
  780. example of its use, to enumerate all of the elements in a table the
  781. user would write:
  782. PVOID RestartKey = NULL;
  783. for (ptr = RtlEnumerateGenericTableWithoutSplaying(Table, &RestartKey);
  784. ptr != NULL;
  785. ptr = RtlEnumerateGenericTableWithoutSplaying(Table, &RestartKey)) {
  786. :
  787. }
  788. Arguments:
  789. Table - Pointer to the generic table to enumerate.
  790. RestartKey - Pointer that indicates if we should restart or return the next
  791. element. If the contents of RestartKey is NULL, the search
  792. will be started from the beginning.
  793. Return Value:
  794. PVOID - Pointer to the user data.
  795. --*/
  796. {
  797. if (RtlIsGenericTableEmpty(Table)) {
  798. //
  799. // Nothing to do if the table is empty.
  800. //
  801. return NULL;
  802. } else {
  803. //
  804. // Will be used as the "iteration" through the tree.
  805. //
  806. PRTL_SPLAY_LINKS NodeToReturn;
  807. //
  808. // If the restart flag is true then go to the least element
  809. // in the tree.
  810. //
  811. if (*RestartKey == NULL) {
  812. //
  813. // We just loop until we find the leftmost child of the root.
  814. //
  815. for (
  816. NodeToReturn = Table->TableRoot;
  817. RtlLeftChild(NodeToReturn);
  818. NodeToReturn = RtlLeftChild(NodeToReturn)
  819. ) {
  820. ;
  821. }
  822. *RestartKey = NodeToReturn;
  823. } else {
  824. //
  825. // The caller has passed in the previous entry found
  826. // in the table to enable us to continue the search. We call
  827. // RtlRealSuccessor to step to the next element in the tree.
  828. //
  829. NodeToReturn = RtlRealSuccessor(*RestartKey);
  830. if (NodeToReturn) {
  831. *RestartKey = NodeToReturn;
  832. }
  833. }
  834. //
  835. // If there actually is a next element in the enumeration
  836. // then the pointer to return is right after the list links.
  837. //
  838. return ((NodeToReturn)?
  839. ((PVOID)&((PTABLE_ENTRY_HEADER)NodeToReturn)->UserData)
  840. :((PVOID)(NULL)));
  841. }
  842. }