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.

189 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. ftrie.h
  5. Abstract:
  6. This module contains support definitions for
  7. an F-trie data stucture, that forms the fast
  8. path in a fast IP route lookup implementation.
  9. Author:
  10. Chaitanya Kodeboyina (chaitk) 26-Nov-1997
  11. Revision History:
  12. --*/
  13. #ifndef FTRIE_H_INCLUDED
  14. #define FTRIE_H_INCLUDED
  15. #include "trie.h"
  16. //
  17. // Constants
  18. //
  19. // State of the trie
  20. #define NORMAL 0
  21. #define PARTIAL 1
  22. //
  23. // Structs
  24. //
  25. // A Node in an F-Trie
  26. typedef struct _FTrieNode FTrieNode;
  27. struct _FTrieNode
  28. {
  29. LIST_ENTRY linkage; // Linkage into list of nodes on the F-Trie
  30. Dest *comDest; // Dest for this subtree's common prefix
  31. UINT numDests; // Number of dests in this node's subtree
  32. UINT numBits; // Number of addr bits to get to children
  33. FTrieNode *child[0]; // Child Node (Or) Info Ptr Array [Var Len]
  34. };
  35. // An FTrie Data Structure
  36. typedef struct _FTrie FTrie;
  37. struct _FTrie
  38. {
  39. FTrieNode *trieRoot; // Pointer to the root of the FTrie
  40. ULONG availMemory; // Memory available for allocation
  41. LIST_ENTRY listofNodes; // List of nodes allocated on FTrie
  42. UINT numLevels; // Total num of levels in the FTrie
  43. UINT *bitsInLevel; // Num of index bits in each level
  44. UINT *numDestsInOrigLevel; // Num of dests in each original level
  45. UINT *numNodesInExpnLevel; // Num of nodes in each expanded level
  46. UINT *numDestsInExpnLevel; // Num of dests in each expanded level
  47. };
  48. // Specific Dest Macros
  49. #define StoreDestPtr(_p_) (FTrieNode *) ((ULONG_PTR) _p_ + 1)
  50. #define RestoreDestPtr(_p_) (Dest *) ((ULONG_PTR) _p_ - 1)
  51. #define IsPtrADestPtr(_p_) (BOOLEAN) ((ULONG_PTR) _p_ & 1)
  52. #define ReplaceDestPtr(_pNewDest_, _pOldDest_, _ppDest_) \
  53. if (*_ppDest_ == _pOldDest_) \
  54. { \
  55. *_ppDest_ = _pNewDest_; \
  56. } \
  57. // Specific FTrieNode Macros
  58. #define NewFTrieNode(_pFTrie_, _pFTrieNode_, _numBits_, _pDest_) \
  59. { \
  60. UINT __i; \
  61. UINT __numChild = 1 << _numBits_; \
  62. UINT __numBytes = sizeof(FTrieNode) + \
  63. __numChild * sizeof(PVOID); \
  64. \
  65. if (_numBits_ > 7*sizeof(PVOID)) \
  66. { \
  67. Recover("Unable to Allocate Memory", \
  68. ERROR_TRIE_RESOURCES); \
  69. } \
  70. \
  71. AllocMemory2(_pFTrieNode_, \
  72. __numBytes, \
  73. _pFTrie_->availMemory); \
  74. \
  75. InsertHeadList(&_pFTrie_->listofNodes, \
  76. &_pFTrieNode_->linkage); \
  77. /* \
  78. DbgPrint("Allocating FTNode @ %08x\n", \
  79. _pFTrieNode_); \
  80. */ \
  81. _pFTrieNode_->numDests = 0; \
  82. \
  83. _pFTrieNode_->comDest = _pDest_; \
  84. \
  85. _pFTrieNode_->numBits = _numBits_; \
  86. \
  87. for (__i = 0; __i < __numChild; __i++) \
  88. { \
  89. _pFTrieNode_->child[__i] = \
  90. StoreDestPtr(NULL); \
  91. } \
  92. } \
  93. #define FreeFTrieNode(_pFTrie_, _pFTrieNode_) \
  94. { \
  95. UINT __numChild = 1 << _pFTrieNode_->numBits;\
  96. UINT __numBytes = sizeof(FTrieNode) + \
  97. __numChild * sizeof(PVOID); \
  98. \
  99. RemoveEntryList(&_pFTrieNode_->linkage); \
  100. /* \
  101. DbgPrint("Freeing FTNode @ %08x\n", \
  102. _pFTrieNode_); \
  103. */ \
  104. FreeMemory1(_pFTrieNode_, \
  105. __numBytes, \
  106. _pFTrie_->availMemory); \
  107. } \
  108. // Prototypes
  109. UINT
  110. CALLCONV
  111. InitFTrie (IN FTrie *pFTrie,
  112. IN ULONG levels,
  113. IN ULONG maxMemory);
  114. UINT
  115. CALLCONV
  116. InsertIntoFTrie (IN FTrie *pFTrie,
  117. IN Route *pInsRoute,
  118. IN Dest *pInsDest,
  119. IN Dest *pOldDest);
  120. UINT
  121. CALLCONV
  122. DeleteFromFTrie (IN FTrie *pFTrie,
  123. IN Route *pDelRoute,
  124. IN Dest *pDelDest,
  125. IN Dest *pNewDest,
  126. IN BOOLEAN trieState);
  127. UINT
  128. CALLCONV
  129. SearchDestInFTrie (IN FTrie *pFTrie,
  130. IN Dest *pSerDest,
  131. OUT UINT *pNumPtrs,
  132. OUT Dest **pStartPtr);
  133. Dest *
  134. CALLCONV
  135. SearchAddrInFTrie (IN FTrie *pFTrie,
  136. IN ULONG Addr);
  137. UINT
  138. CALLCONV
  139. CleanupFTrie (IN FTrie *pFTrie);
  140. #if DBG
  141. VOID
  142. CALLCONV
  143. PrintFTrie (IN FTrie *pFTrie,
  144. IN UINT fPrintAll);
  145. VOID
  146. CALLCONV
  147. PrintFTrieNode (IN FTrieNode *pFTrieNode,
  148. IN UINT levelNumber);
  149. #endif // DBG
  150. #endif // FTRIE_H_INCLUDED