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.

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