|
| 1 | +# Design |
| 2 | + |
| 3 | +## Allocation engine |
| 4 | + |
| 5 | +This implementation uses an interval tree that is specialised for allocation of |
| 6 | +memory-mapped I/O and port I/O address space. The fields of the strctures |
| 7 | +defined will have semantic meaning for this context (e.g node state to indicate |
| 8 | +if a node in the tree is assigned or not to a device). |
| 9 | + |
| 10 | +We offer three options for placing a memory slot in the managed address space: |
| 11 | + |
| 12 | +1. `LastMatch` -> When using this allocation policy the allocator will try to |
| 13 | +insert the range described by the constraint at the first available position |
| 14 | +starting from the end of the managed address space. |
| 15 | +2. `FirstMatch` -> When using this allocation policy the allocator will try to |
| 16 | +insert the range described by the constraint at the first available position |
| 17 | +starting from the begining of the managed address space. |
| 18 | +3. `ExactMatch(u64)` -> When using this allocation policy the allocator will try |
| 19 | +to insert the range at the exact position described by the constraint, otherwise |
| 20 | +it will return an error. |
| 21 | + |
| 22 | +Struct `Constraint` is used to describe the overall information of the resource |
| 23 | +needed to be allocated. This structure is also used by IntervalTree to know where |
| 24 | +and how to allocate the resource. The fields that are mandatory for allocating |
| 25 | +a new memory slot are size of the slot, alignment of the slot and allocation policy. |
| 26 | +Optionally the user can specify a range where the allocator will place the allocated |
| 27 | +memory slot. |
| 28 | + |
| 29 | +## Interval tree |
| 30 | + |
| 31 | +An interval tree is a tree data structure used for storing information about intervals. |
| 32 | +Specifically, it allows one to efficiently identify intervals that are overlaping |
| 33 | +with a given point, or another interval. We considered that this characteristic |
| 34 | +makes this data structure appropriate to be used as an allocation engine for |
| 35 | +memory slots inside an address space. The time complexity of an interval tree, |
| 36 | +namely O(log n+m) for queries, O(log n) for creation and O(log n) for insertion |
| 37 | +and deletion of nodes. The key of each node of the tree is represented using a |
| 38 | +struct named `Range` that contains the bounds of the address space. Each node in |
| 39 | +the tree can have two states, either `Free` or `Allocated`. Beside the information |
| 40 | +presented above the representation of a node also contains references two the |
| 41 | +children node of the current node. |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +To use the `IntervalTree` implementation as an address allocator one should first |
| 46 | +create an interval tree object and give an address space as a root node. After, |
| 47 | +the user should create a constraint with the size for the resource. Optionally |
| 48 | +the constraint could also contain the maximum, minimum and alignment for the |
| 49 | +constraint. |
| 50 | + |
| 51 | +## State transition |
| 52 | + |
| 53 | +At the beggining, the interval tree will contain just one node that will represent |
| 54 | +the whole address space, the state of this node will be `NodeState::Free`. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +When we allocate a memory slot, one of the nodes that have the state `NodeState::Free` |
| 59 | +will be split accordingly. A new node that has as the key a range representing the |
| 60 | +allocated memory slot will be inserted in the tree. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +When one of the allocated nodes is freed its state will be changed from `NodeState::Allocated` |
| 65 | +to `NodeState::Free` if there are two adjacent nodes that are not allocated then |
| 66 | +they will be merged in a single node. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +**!!!NOTICE**: The BSD-3-Clause license is not included in this template. |
| 73 | +The license needs to be manually added because the text of the license file |
| 74 | +also includes the copyright. The copyright can be different for different |
| 75 | +crates. If the crate contains code from CrosVM, the crate must add the |
| 76 | +CrosVM copyright which can be found |
| 77 | +[here](https://chromium.googlesource.com/chromiumos/platform/crosvm/+/master/LICENSE). |
| 78 | +For crates developed from scratch, the copyright is different and depends on |
| 79 | +the contributors. |
0 commit comments