Skip to content

Commit 8a32f2c

Browse files
committed
Add usage example for the interval tree.
Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent 199beeb commit 8a32f2c

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/interval_tree.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
11
// Copyright (C) 2019 Alibaba Cloud. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! A special interval tree implementation for VMM resource management.
4+
//! An interval tree implementation specialized for VMM resource management.
55
//!
66
//! It's not designed as a generic interval tree, but specialized for VMM resource management.
77
//! In addition to the normal get/insert/delete/update operations, it also implements allocate/free.
8+
//!
9+
//! # Examples
10+
//! ```rust
11+
//! extern crate vm_allocator;
12+
//! use vm_allocator::{IntervalTree, Range, Constraint, NodeState};
13+
//!
14+
//! // Create an interval tree and add available resources.
15+
//! let mut tree = IntervalTree::<u64>::new();
16+
//! tree.insert(Range::new(0x100u32, 0x100u32), None);
17+
//! tree.insert(Range::new(0x200u16, 0x2ffu16), None);
18+
//!
19+
//! // Allocate a range with constraints.
20+
//! let mut constraint = Constraint::new(8u64);
21+
//! constraint.min = 0x211;
22+
//! constraint.max = 0x21f;
23+
//! constraint.align = 0x8;
24+
//!
25+
//! let key = tree.allocate(&constraint);
26+
//! assert_eq!(key, Some(Range::new(0x218u64, 0x21fu64)));
27+
//! let val = tree.get(&Range::new(0x218u64, 0x21fu64));
28+
//! assert_eq!(val, Some(NodeState::Allocated));
29+
//!
30+
//! // Associate data with the allocated range and mark the range as occupied.
31+
//! // Note: caller needs to protect from concurrent access between allocate() and the first call
32+
//! // to update() to mark range as occupied.
33+
//! let old = tree.update(&Range::new(0x218u32, 0x21fu32), 2);
34+
//! assert_eq!(old, None);
35+
//! let old = tree.update(&Range::new(0x218u32, 0x21fu32), 3);
36+
//! assert_eq!(old, Some(2));
37+
//! let val = tree.get(&Range::new(0x218u32, 0x21fu32));
38+
//! assert_eq!(val, Some(NodeState::Valued(&3)));
39+
//!
40+
//! // Free allocated resource.
41+
//! let old = tree.free(key.as_ref().unwrap());
42+
//! assert_eq!(old, Some(3));
43+
//!
44+
//! ```
845
946
use crate::{AllocPolicy, Constraint};
1047
use std::cmp::{max, min, Ordering};
@@ -236,7 +273,7 @@ impl<T> Node<T> {
236273
}
237274
}
238275

239-
/// Find the node covers full range of the `key`.
276+
/// Returns the node covers full range of the `key`.
240277
fn search_superset(&self, key: &Range) -> Option<&Self> {
241278
if self.0.key.contain(key) {
242279
Some(self)
@@ -501,7 +538,7 @@ fn max_key<T>(node: &Option<Node<T>>) -> u64 {
501538
node.as_ref().map_or(0, |n| n.0.max_key)
502539
}
503540

504-
/// A specialized interval tree implementation for VMM resource management.
541+
/// An interval tree implementation specialized for VMM resource management.
505542
#[derive(Debug, Default)]
506543
pub struct IntervalTree<T> {
507544
root: Option<Node<T>>,

0 commit comments

Comments
 (0)