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.

158 lines
3.4 KiB

  1. /*++
  2. Copyright (c) 1997 - 98, Microsoft Corporation
  3. Module Name:
  4. pattrie.h
  5. Abstract:
  6. Contains interface for a best matching
  7. prefix lookup using an PATRICIA trie.
  8. Author:
  9. Chaitanya Kodeboyina (chaitk) 26-Sep-1998
  10. Revision History:
  11. --*/
  12. #ifndef __ROUTING_PATLOOKUP_H__
  13. #define __ROUTING_PATLOOKUP_H__
  14. #include "lookup.h"
  15. #define Print printf
  16. #define BITS_IN_BYTE 8
  17. #define NODE_KEY_SIZE sizeof(ULONG)
  18. //
  19. // Direction in Iterator
  20. //
  21. #define LCHILD 0
  22. #define RCHILD 1
  23. #define PARENT 2
  24. typedef INT PAT_CHILD, *PPAT_CHILD;
  25. //
  26. // A node in the PAT trie
  27. //
  28. typedef struct _PAT_NODE *PPAT_NODE;
  29. typedef struct _PAT_NODE
  30. {
  31. PPAT_NODE Child[2]; // Pointers to left & right child nodes
  32. PVOID Data; // Opaque Pointer to data in the node
  33. USHORT NumBits; // Actual number of bits in this node
  34. ULONG KeyBits; // Value of bits to match in this node
  35. }
  36. PAT_NODE;
  37. //
  38. // PAT trie for prefix matching
  39. //
  40. typedef struct _PAT_TRIE
  41. {
  42. PPAT_NODE TrieRoot; // Pointer to the PAT trie
  43. USHORT MaxKeyBytes; // Max num of bytes in key
  44. USHORT NumNodes; // Number of nodes in trie
  45. #if PROF
  46. ULONG MemoryInUse; // Total memory in use now
  47. UINT NumAllocs; // Num of total allocations
  48. UINT NumFrees; // Num of total free allocs
  49. UINT NumInsertions; // Num of total insertions
  50. UINT NumDeletions; // Num of total deletions
  51. #endif
  52. }
  53. PAT_TRIE, *PPAT_TRIE;
  54. //
  55. // Lookup context for a PAT trie
  56. //
  57. typedef struct _PAT_CONTEXT
  58. {
  59. PVOID BestNode; // Node with best the matching prefix
  60. PVOID InsPoint; // Node to which new node is attached
  61. PAT_CHILD InsChild; // Node should attached as this child
  62. }
  63. PAT_CONTEXT, *PPAT_CONTEXT;
  64. //
  65. // Linkage Info Kept in Data
  66. //
  67. typedef struct _PAT_LINKAGE
  68. {
  69. PPAT_NODE NodePtr; // Back pointer to the owning node
  70. }
  71. PAT_LINKAGE, *PPAT_LINKAGE;
  72. #define SET_NODEPTR_INTO_DATA(Data, Node) ((PPAT_LINKAGE)Data)->NodePtr = Node
  73. #define GET_NODEPTR_FROM_DATA(Data) ((PPAT_LINKAGE)Data)->NodePtr
  74. //
  75. // Macros for doing bit operations on keys
  76. //
  77. //
  78. // MaskBitsArr[i] = First 'i' bits set to 1
  79. //
  80. const ULONG MaskBitsArr[] =
  81. {
  82. 0x00000000, 0x80000000, 0xC0000000, 0xE0000000,
  83. 0xF0000000, 0xF8000000, 0xFC000000, 0xFE000000,
  84. 0xFF000000, 0xFF800000, 0xFFC00000, 0xFFE00000,
  85. 0xFFF00000, 0xFFF80000, 0xFFFC0000, 0xFFFE0000,
  86. 0xFFFF0000, 0xFFFF8000, 0xFFFFC000, 0xFFFFE000,
  87. 0xFFFFF000, 0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00,
  88. 0xFFFFFF00, 0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0,
  89. 0xFFFFFFF0, 0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE,
  90. 0xFFFFFFFF
  91. };
  92. #define PickMostSigNBits(ul, nb) ((ul) >> (NODE_KEY_SIZE - nb))
  93. #define MaskBits(nb) MaskBitsArr[nb]
  94. //
  95. // Key Compare/Copy inlines
  96. //
  97. // Disable warnings for no return value
  98. #pragma warning(disable:4035)
  99. __inline
  100. ULONG
  101. RtmUlongByteSwap(
  102. IN ULONG Value
  103. )
  104. {
  105. __asm
  106. {
  107. mov eax, Value
  108. bswap eax
  109. }
  110. }
  111. #pragma warning(default:4035)
  112. #define RtlUlongByteSwap RtmUlongByteSwap
  113. #endif //__ROUTING_PATLOOKUP_H__