|
| 1 | +// Copyright (C) 2022 Alibaba Cloud. All rights reserved. |
| 2 | +// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2 |
| 4 | + |
| 5 | +/// Policy for resource allocation. |
| 6 | +#[derive(Copy, Clone, Debug)] |
| 7 | +pub enum AllocPolicy { |
| 8 | + /// Allocate from the first matched entry. |
| 9 | + FirstMatch, |
| 10 | + /// Allocate first matched entry from the end of the range. |
| 11 | + LastMatch, |
| 12 | + // Allocate a memory slot starting with the specified addrres |
| 13 | + // if it is available. |
| 14 | + ExactMatch, |
| 15 | +} |
| 16 | + |
| 17 | +impl Default for AllocPolicy { |
| 18 | + fn default() -> Self { |
| 19 | + AllocPolicy::FirstMatch |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// Struct to describe resource allocation constraints. |
| 24 | +#[derive(Copy, Clone, Debug)] |
| 25 | +pub struct Constraint { |
| 26 | + /// Size to allocate. |
| 27 | + pub size: u64, |
| 28 | + /// Lower boundary for the allocated resource. |
| 29 | + pub min: u64, |
| 30 | + /// Upper boundary for the allocated resource. |
| 31 | + pub max: u64, |
| 32 | + /// Alignment for the allocated resource. |
| 33 | + pub align: u64, |
| 34 | + /// Resource allocation policy. |
| 35 | + pub policy: AllocPolicy, |
| 36 | +} |
| 37 | + |
| 38 | +/// A closed interval range [min, max] used to describe a |
| 39 | +/// memory slot that will be assigned to a device by the VMM. |
| 40 | +/// This structure represents the key of the Node object in |
| 41 | +/// the interval tree implementation. |
| 42 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash)] |
| 43 | +pub struct Range { |
| 44 | + pub min: u64, |
| 45 | + pub max: u64, |
| 46 | +} |
| 47 | + |
| 48 | +// Node state for interval tree nodes. |
| 49 | +/// |
| 50 | +/// Valid state transition: |
| 51 | +/// - None -> Free: IntervalTree::insert() |
| 52 | +/// - Free -> Allocated: IntervalTree::allocate() |
| 53 | +/// - Allocated -> Free: IntervalTree::free() |
| 54 | +/// - * -> None: IntervalTree::delete() |
| 55 | +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] |
| 56 | +pub enum NodeState { |
| 57 | + /// Node is free. |
| 58 | + Free, |
| 59 | + /// Node is allocated. |
| 60 | + Allocated, |
| 61 | +} |
| 62 | + |
| 63 | +/// Internal tree node to implement interval tree. |
| 64 | +#[derive(Debug, PartialEq)] |
| 65 | +pub(crate) struct InnerNode { |
| 66 | + /// Interval handled by this node. |
| 67 | + pub(crate) key: Range, |
| 68 | + /// Optional contained data, None if the node is free. |
| 69 | + pub(crate) node_state: NodeState, |
| 70 | + /// Optional left child of current node. |
| 71 | + pub(crate) left: Option<Box<InnerNode>>, |
| 72 | + /// Optional right child of current node. |
| 73 | + pub(crate) right: Option<Box<InnerNode>>, |
| 74 | + /// Cached height of the node. |
| 75 | + pub(crate) height: u64, |
| 76 | + /// Cached maximum valued covered by this node. |
| 77 | + pub(crate) max_key: u64, |
| 78 | +} |
0 commit comments