Bitdefender Hypervisor Memory Introspection
rbtree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _RBTREE_H_
6 #define _RBTREE_H_
7 
8 #include "introdefs.h"
9 
10 //
11 // The red-black tree is a self organizing binary search tree;
12 // Wikipedia description: http://en.wikipedia.org/wiki/Red%E2%80%93black_tree
13 // The implementation is based on the "Introduction to algorithms 3rd Ed."
14 //
15 
16 #define RBTREE_MAXIMUM_LEVEL 40
17 
18 //
19 // RbNodeColor - Color of a node can be or RED or BLACK
20 //
21 typedef enum _RbNodeColor
22 {
23  ncBlack = 0,
24  ncRed = 1,
25 } RbNodeColor;
26 
27 //
28 // RBNODE - Node representation in a Red-Black Tree;
29 // Every node beside it's child nodes must have a color;
30 // Properties:
31 // Every RED node must have two black nodes, the leafs are black
32 // Every path in the graph must contain the same number of black nodes
33 //
34 typedef struct _RBNODE
35 {
36  struct _RBNODE *Left;
37  struct _RBNODE *Right;
38  struct _RBNODE *Parent;
40 } RBNODE, *PRBNODE;
41 
42 //
43 // The free function must release the memory of a node with the same tag
44 // as it was allocated in the allocator
45 //
46 typedef void
49 
50 
51 //
52 // Compare function - compares the keys of the nodes;
53 // Return values:
54 // -1 - left.key < right.key
55 // 0 - left.key = right.key
56 // 1 - left.key > right.key
57 //
58 typedef int
61 
62 //
63 // Custom compare function - compares a node with a value
64 // Return values:
65 // -1 - left.key < right.key
66 // 0 - left.key = right.key
67 // 1 - left.key > right.key
68 //
69 typedef int
72 
73 
74 //
75 // Tree walk callback, the callback can stop the tree walk returning a FALSE
76 //
77 typedef BOOLEAN
78 FUNC_RbTreeWalkCallback(RBNODE *Node, void *WalkContext);
80 
81 //
82 // RBTREE - represents a Red-Black tree, it holds the root of the tree
83 //
84 typedef struct _RBTREE
85 {
86  RBNODE *Root; // The node what represents the root of the tree
87  RBNODE Nil; // Nil sentinel node; the pointers in it can be arbitrary
88  PFUNC_RbTreeNodeFree NodeFree; // The function which is used for cleanup
89  PFUNC_RbTreeNodeCompare NodeCompare; // The compare function
90  volatile INT32 NodeCount;
91 } RBTREE, *PBTREE;
92 
93 
94 void
95 RbPreinit(
96  _Inout_ RBTREE *Tree
97  );
98 
100 RbInit(
101  _Inout_ RBTREE *Tree,
102  _In_ PFUNC_RbTreeNodeFree NodeFree,
103  _In_ PFUNC_RbTreeNodeCompare NodeCompare
104  );
105 
106 void
107 RbUninit(
108  _Inout_ RBTREE *Tree
109  );
110 
111 INTSTATUS
113  _Inout_ RBTREE *Tree,
114  _Inout_ RBNODE *Node
115  );
116 
117 INTSTATUS
119  _In_ RBTREE *Tree,
120  _In_ RBNODE *NodeToSearch,
121  _Outptr_ RBNODE **NodeFound
122  );
123 
124 INTSTATUS
126  _In_ RBTREE *Tree,
128  _In_ void *Key,
129  _Out_ RBNODE **NodeFound
130  );
131 
132 void
134  _Inout_ RBTREE *Tree,
135  _In_ RBNODE *Node
136  );
137 
138 INTSTATUS
140  _In_ RBTREE *Tree,
142  _In_opt_ void *WalkContext
143  );
144 
145 #endif//_RBTREE_H_
#define _In_opt_
Definition: intro_sal.h:16
struct _RBTREE * PBTREE
int FUNC_RbTreeNodeCustomCompare(RBNODE *Node, void *Key)
Definition: rbtree.h:70
RbNodeColor Color
Definition: rbtree.h:39
#define _Out_
Definition: intro_sal.h:22
_Bool BOOLEAN
Definition: intro_types.h:58
struct _RBNODE * Right
Definition: rbtree.h:37
FUNC_RbTreeNodeCompare * PFUNC_RbTreeNodeCompare
Definition: rbtree.h:60
struct _RBNODE * Parent
Definition: rbtree.h:38
#define _In_
Definition: intro_sal.h:21
void FUNC_RbTreeNodeFree(RBNODE *Node)
Definition: rbtree.h:47
Definition: rbtree.h:24
Definition: rbtree.h:34
int32_t INT32
Definition: intro_types.h:44
INTSTATUS RbLookupNode(RBTREE *Tree, RBNODE *NodeToSearch, RBNODE **NodeFound)
Definition: rbtree.c:517
INTSTATUS RbWalkInorderTree(RBTREE *Tree, PFUNC_RbTreeWalkCallback Callback, void *WalkContext)
Definition: rbtree.c:806
INTSTATUS RbLookupNodeCustomCompare(RBTREE *Tree, PFUNC_RbTreeNodeCustomCompare CompareFunc, void *Key, RBNODE **NodeFound)
Definition: rbtree.c:552
RBNODE * Root
Definition: rbtree.h:86
#define _Outptr_
Definition: intro_sal.h:19
int INTSTATUS
The status data type.
Definition: introstatus.h:24
INTSTATUS RbInit(RBTREE *Tree, PFUNC_RbTreeNodeFree NodeFree, PFUNC_RbTreeNodeCompare NodeCompare)
Definition: rbtree.c:386
int FUNC_RbTreeNodeCompare(RBNODE *Left, RBNODE *Right)
Definition: rbtree.h:59
#define _Inout_
Definition: intro_sal.h:20
struct _RBNODE * Left
Definition: rbtree.h:36
_RbNodeColor
Definition: rbtree.h:21
struct _RBTREE RBTREE
void RbUninit(RBTREE *Tree)
Definition: rbtree.c:419
void RbPreinit(RBTREE *Tree)
Definition: rbtree.c:377
RBNODE Nil
Definition: rbtree.h:87
Definition: rbtree.h:23
FUNC_RbTreeWalkCallback * PFUNC_RbTreeWalkCallback
Definition: rbtree.h:79
Definition: rbtree.h:84
void RbDeleteNode(RBTREE *Tree, RBNODE *Node)
Definition: rbtree.c:710
struct _RBNODE * PRBNODE
FUNC_RbTreeNodeFree * PFUNC_RbTreeNodeFree
Definition: rbtree.h:48
INTSTATUS RbInsertNode(RBTREE *Tree, RBNODE *Node)
Definition: rbtree.c:606
PFUNC_RbTreeNodeCompare NodeCompare
Definition: rbtree.h:89
BOOLEAN FUNC_RbTreeWalkCallback(RBNODE *Node, void *WalkContext)
Definition: rbtree.h:78
struct _RBNODE RBNODE
enum _RbNodeColor RbNodeColor
PFUNC_RbTreeNodeFree NodeFree
Definition: rbtree.h:88
volatile INT32 NodeCount
Definition: rbtree.h:90
FUNC_RbTreeNodeCustomCompare * PFUNC_RbTreeNodeCustomCompare
Definition: rbtree.h:71