|
1 | 1 | // Copyright (C) 2019 Alibaba Cloud. All rights reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -//! A special interval tree implementation for VMM resource management. |
| 4 | +//! An interval tree implementation specialized for VMM resource management. |
5 | 5 | //! |
6 | 6 | //! It's not designed as a generic interval tree, but specialized for VMM resource management. |
7 | 7 | //! 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 | +//! ``` |
8 | 45 |
|
9 | 46 | use crate::{AllocPolicy, Constraint}; |
10 | 47 | use std::cmp::{max, min, Ordering}; |
@@ -236,7 +273,7 @@ impl<T> Node<T> { |
236 | 273 | } |
237 | 274 | } |
238 | 275 |
|
239 | | - /// Find the node covers full range of the `key`. |
| 276 | + /// Returns the node covers full range of the `key`. |
240 | 277 | fn search_superset(&self, key: &Range) -> Option<&Self> { |
241 | 278 | if self.0.key.contain(key) { |
242 | 279 | Some(self) |
@@ -501,7 +538,7 @@ fn max_key<T>(node: &Option<Node<T>>) -> u64 { |
501 | 538 | node.as_ref().map_or(0, |n| n.0.max_key) |
502 | 539 | } |
503 | 540 |
|
504 | | -/// A specialized interval tree implementation for VMM resource management. |
| 541 | +/// An interval tree implementation specialized for VMM resource management. |
505 | 542 | #[derive(Debug, Default)] |
506 | 543 | pub struct IntervalTree<T> { |
507 | 544 | root: Option<Node<T>>, |
|
0 commit comments