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.

119 lines
3.2 KiB

  1. /*
  2. redblack.h
  3. Prototypes and node structure definition for red-black binary trees.
  4. See redblack.c for details and implementation.
  5. Author: Tom McGuire (tommcg) 1/98
  6. Copyright (C) Microsoft, 1998.
  7. 2/98, modified this version of redblack.h for debug symbol lookups.
  8. */
  9. #ifndef _REDBLACK_H_
  10. #define _REDBLACK_H_
  11. typedef struct _SYMBOL_NODE SYMBOL_NODE, *PSYMBOL_NODE;
  12. typedef struct _SYMBOL_TREE SYMBOL_TREE, *PSYMBOL_TREE;
  13. struct _SYMBOL_NODE {
  14. PSYMBOL_NODE Left;
  15. PSYMBOL_NODE Right;
  16. ULONG Hash;
  17. union {
  18. ULONG RvaWithStatusBits;
  19. struct {
  20. ULONG Rva:30;
  21. ULONG Hit:1;
  22. ULONG Red:1;
  23. };
  24. };
  25. CHAR SymbolName[ 0 ];
  26. };
  27. struct _SYMBOL_TREE {
  28. PSYMBOL_NODE Root;
  29. HANDLE SubAllocator;
  30. #if defined( DEBUG ) || defined( DBG ) || defined( TESTCODE )
  31. ULONG CountNodes;
  32. BOOL DeletedAny;
  33. #endif
  34. };
  35. #define RBNIL ((PSYMBOL_NODE)&SymRBEmptyNode)
  36. extern const SYMBOL_NODE SymRBEmptyNode;
  37. //
  38. // Although "Red" can be stored in its own 1-byte or 4-byte field, keeping the
  39. // nodes smaller by encoding "Red" as a one-bit field with another value
  40. // provides better performance (more nodes tend to stay in the cache). To
  41. // provide flexibility in storage of the RED property, all references to RED
  42. // and BLACK are made through the following macros which can be changed as
  43. // necessary:
  44. //
  45. #define IS_RED( Node ) ( (Node)->Red )
  46. #define IS_BLACK( Node ) ( ! (Node)->Red )
  47. #define MARK_RED( Node ) ( (Node)->Red = 1 )
  48. #define MARK_BLACK( Node ) ( (Node)->Red = 0 )
  49. //
  50. // The maximum tree depth is 2*Lg(N). Since we could never have more than
  51. // 2^X nodes with X-bit pointers, we can safely say the absolute maximum
  52. // depth will be 2*Lg(2^X) which is 2*X. The size of a pointer in bits is
  53. // its size in bytes times 8 bits, so 2*(sizeof(p)*8) is our maximum depth.
  54. // So for 32-bit pointers, our maximum depth is 64.
  55. //
  56. // If you know the maximum possible number of nodes in advance (like the size
  57. // of the address space divided by the size of a node), you can tweak this
  58. // value a bit smaller to 2*Lg(N). Note that it's important for this max
  59. // depth be evalutated to a constant value at compile time.
  60. //
  61. // For this implementation, we'll assume the maximum number of nodes is
  62. // 128 million, so the max depth is 54 (2*Lg(2^27)). Note that no runtime
  63. // checks are made to ensure we don't exceed this number, but since our
  64. // minimum node allocation size is 32 bytes, that would be a maximum of
  65. // 100 million nodes in a 3GB address space.
  66. //
  67. #define MAX_DEPTH 54
  68. //
  69. // The following prototypes are the red-black tree interface.
  70. //
  71. VOID
  72. SymRBInitTree(
  73. IN OUT PSYMBOL_TREE Tree,
  74. IN HANDLE SubAllocator
  75. );
  76. PSYMBOL_NODE
  77. SymRBInsert(
  78. IN OUT PSYMBOL_TREE Tree,
  79. IN LPSTR SymbolName,
  80. IN ULONG Rva
  81. );
  82. PSYMBOL_NODE
  83. SymRBFind(
  84. IN PSYMBOL_TREE Tree,
  85. IN LPSTR SymbolName
  86. );
  87. PSYMBOL_NODE
  88. SymRBFindAndDelete(
  89. IN OUT PSYMBOL_TREE Tree,
  90. IN LPSTR SymbolName
  91. );
  92. #endif // _REDBLACK_H_