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.

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