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.

194 lines
8.2 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. #pragma warning(push)
  28. #pragma warning(disable:4200) // nonstandard extension used: zero sized array
  29. struct _FTrieNode
  30. {
  31. LIST_ENTRY linkage; // Linkage into list of nodes on the F-Trie
  32. Dest *comDest; // Dest for this subtree's common prefix
  33. UINT numDests; // Number of dests in this node's subtree
  34. UINT numBits; // Number of addr bits to get to children
  35. FTrieNode *child[0]; // Child Node (Or) Info Ptr Array [Var Len]
  36. };
  37. #pragma warning(pop)
  38. // An FTrie Data Structure
  39. typedef struct _FTrie FTrie;
  40. struct _FTrie
  41. {
  42. FTrieNode *trieRoot; // Pointer to the root of the FTrie
  43. ULONG availMemory; // Memory available for allocation
  44. LIST_ENTRY listofNodes; // List of nodes allocated on FTrie
  45. UINT numLevels; // Total num of levels in the FTrie
  46. UINT *bitsInLevel; // Num of index bits in each level
  47. UINT *numDestsInOrigLevel; // Num of dests in each original level
  48. UINT *numNodesInExpnLevel; // Num of nodes in each expanded level
  49. UINT *numDestsInExpnLevel; // Num of dests in each expanded level
  50. };
  51. // Specific Dest Macros
  52. #define StoreDestPtr(_p_) (FTrieNode *) ((ULONG_PTR) _p_ + 1)
  53. #define RestoreDestPtr(_p_) (Dest *) ((ULONG_PTR) _p_ - 1)
  54. #define IsPtrADestPtr(_p_) (BOOLEAN) ((ULONG_PTR) _p_ & 1)
  55. #define ReplaceDestPtr(_pNewDest_, _pOldDest_, _ppDest_) \
  56. if (*_ppDest_ == _pOldDest_) \
  57. { \
  58. *_ppDest_ = _pNewDest_; \
  59. } \
  60. // Specific FTrieNode Macros
  61. #define NewFTrieNode(_pFTrie_, _pFTrieNode_, _numBits_, _pDest_) \
  62. { \
  63. UINT __i; \
  64. UINT __numChild = 1 << _numBits_; \
  65. UINT __numBytes = sizeof(FTrieNode) + \
  66. __numChild * sizeof(PVOID); \
  67. \
  68. if (_numBits_ > 7*sizeof(PVOID)) \
  69. { \
  70. Recover("Unable to Allocate Memory", \
  71. (UINT) ERROR_TRIE_RESOURCES); \
  72. } \
  73. \
  74. AllocMemory2(_pFTrieNode_, \
  75. __numBytes, \
  76. _pFTrie_->availMemory); \
  77. \
  78. InsertHeadList(&_pFTrie_->listofNodes, \
  79. &_pFTrieNode_->linkage); \
  80. /* \
  81. DbgPrint("Allocating FTNode @ %08x\n", \
  82. _pFTrieNode_); \
  83. */ \
  84. _pFTrieNode_->numDests = 0; \
  85. \
  86. _pFTrieNode_->comDest = _pDest_; \
  87. \
  88. _pFTrieNode_->numBits = _numBits_; \
  89. \
  90. for (__i = 0; __i < __numChild; __i++) \
  91. { \
  92. _pFTrieNode_->child[__i] = \
  93. StoreDestPtr(NULL); \
  94. } \
  95. } \
  96. #define FreeFTrieNode(_pFTrie_, _pFTrieNode_) \
  97. { \
  98. UINT __numChild = 1 << _pFTrieNode_->numBits;\
  99. UINT __numBytes = sizeof(FTrieNode) + \
  100. __numChild * sizeof(PVOID); \
  101. \
  102. RemoveEntryList(&_pFTrieNode_->linkage); \
  103. /* \
  104. DbgPrint("Freeing FTNode @ %08x\n", \
  105. _pFTrieNode_); \
  106. */ \
  107. FreeMemory1(_pFTrieNode_, \
  108. __numBytes, \
  109. _pFTrie_->availMemory); \
  110. } \
  111. // Prototypes
  112. UINT
  113. CALLCONV
  114. InitFTrie (IN FTrie *pFTrie,
  115. IN ULONG levels,
  116. IN ULONG maxMemory);
  117. UINT
  118. CALLCONV
  119. InsertIntoFTrie (IN FTrie *pFTrie,
  120. IN Route *pInsRoute,
  121. IN Dest *pInsDest,
  122. IN Dest *pOldDest);
  123. UINT
  124. CALLCONV
  125. DeleteFromFTrie (IN FTrie *pFTrie,
  126. IN Route *pDelRoute,
  127. IN Dest *pDelDest,
  128. IN Dest *pNewDest,
  129. IN BOOLEAN trieState);
  130. UINT
  131. CALLCONV
  132. SearchDestInFTrie (IN FTrie *pFTrie,
  133. IN Dest *pSerDest,
  134. OUT UINT *pNumPtrs,
  135. OUT Dest **pStartPtr);
  136. Dest *
  137. CALLCONV
  138. SearchAddrInFTrie (IN FTrie *pFTrie,
  139. IN ULONG Addr);
  140. UINT
  141. CALLCONV
  142. CleanupFTrie (IN FTrie *pFTrie);
  143. #if DBG
  144. VOID
  145. CALLCONV
  146. PrintFTrie (IN FTrie *pFTrie,
  147. IN UINT fPrintAll);
  148. VOID
  149. CALLCONV
  150. PrintFTrieNode (IN FTrieNode *pFTrieNode,
  151. IN UINT levelNumber);
  152. #endif // DBG
  153. #endif // FTRIE_H_INCLUDED