Skip to content

Commit 8241ebc

Browse files
committed
feat: Map implementation based on the ngx_rbtree_t
1 parent c6fe06d commit 8241ebc

File tree

4 files changed

+467
-0
lines changed

4 files changed

+467
-0
lines changed

nginx-sys/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod event;
77
#[cfg(ngx_feature = "http")]
88
mod http;
99
mod queue;
10+
mod rbtree;
1011
#[cfg(ngx_feature = "stream")]
1112
mod stream;
1213
mod string;
@@ -33,6 +34,7 @@ pub use event::*;
3334
#[cfg(ngx_feature = "http")]
3435
pub use http::*;
3536
pub use queue::*;
37+
pub use rbtree::*;
3638
#[cfg(ngx_feature = "stream")]
3739
pub use stream::*;
3840

nginx-sys/src/rbtree.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use core::ptr;
2+
3+
use crate::bindings::{ngx_rbtree_insert_pt, ngx_rbtree_node_t, ngx_rbtree_t};
4+
5+
/// Get a reference to the beginning of a tree element data structure,
6+
/// considering the link field offset in it.
7+
///
8+
/// # Safety
9+
///
10+
/// `$node` must be a valid pointer to the field `$link` in the struct `$type`
11+
#[macro_export]
12+
macro_rules! ngx_rbtree_data {
13+
($node:expr, $type:path, $link:ident) => {
14+
$node
15+
.byte_sub(::core::mem::offset_of!($type, $link))
16+
.cast::<$type>()
17+
};
18+
}
19+
20+
/// Initializes the RbTree with specified sentinel and insert function.
21+
///
22+
/// # Safety
23+
///
24+
/// All of the pointers passed must be valid.
25+
/// `sentinel` is expected to be valid for the whole lifetime of the `tree`.
26+
///
27+
pub unsafe fn ngx_rbtree_init(
28+
tree: *mut ngx_rbtree_t,
29+
sentinel: *mut ngx_rbtree_node_t,
30+
insert: ngx_rbtree_insert_pt,
31+
) {
32+
ngx_rbtree_sentinel_init(sentinel);
33+
(*tree).root = sentinel;
34+
(*tree).sentinel = sentinel;
35+
(*tree).insert = insert;
36+
}
37+
38+
/// Marks the tree node as red.
39+
///
40+
/// # Safety
41+
///
42+
/// `node` must be a valid pointer to a [ngx_rbtree_node_t].
43+
#[inline]
44+
pub unsafe fn ngx_rbt_red(node: *mut ngx_rbtree_node_t) {
45+
(*node).color = 1
46+
}
47+
48+
/// Marks the tree node as black.
49+
///
50+
/// # Safety
51+
///
52+
/// `node` must be a valid pointer to a [ngx_rbtree_node_t].
53+
#[inline]
54+
pub unsafe fn ngx_rbt_black(node: *mut ngx_rbtree_node_t) {
55+
(*node).color = 0
56+
}
57+
58+
/// Initializes the sentinel node.
59+
///
60+
/// # Safety
61+
///
62+
/// `node` must be a valid pointer to a [ngx_rbtree_node_t].
63+
#[inline]
64+
pub unsafe fn ngx_rbtree_sentinel_init(node: *mut ngx_rbtree_node_t) {
65+
ngx_rbt_black(node)
66+
}
67+
68+
/// Returns the least (leftmost) node of the tree.
69+
///
70+
/// # Safety
71+
///
72+
/// `node` must be a valid pointer to a [ngx_rbtree_node_t].
73+
/// `sentinel` must be a valid pointer to the sentinel node in the same Red-Black tree.
74+
#[inline]
75+
pub unsafe fn ngx_rbtree_min(
76+
mut node: *mut ngx_rbtree_node_t,
77+
sentinel: *mut ngx_rbtree_node_t,
78+
) -> *mut ngx_rbtree_node_t {
79+
while !ptr::addr_eq((*node).left, sentinel) {
80+
node = (*node).left;
81+
}
82+
83+
node
84+
}

src/core/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
mod buffer;
22
mod pool;
3+
pub mod rbtree;
34
pub mod slab;
45
mod status;
56
mod string;
67

78
pub use buffer::*;
89
pub use pool::*;
10+
pub use rbtree::RbTreeMap;
911
pub use slab::SlabPool;
1012
pub use status::*;
1113
pub use string::*;

0 commit comments

Comments
 (0)