| 1 |
# Storage Memory Description |
| 2 |
|
| 3 |
This module defines the `bitvec` memory model used to interface bit-slice |
| 4 |
regions to raw memory, and manage type-state changes as demanded by the region |
| 5 |
descriptor. |
| 6 |
|
| 7 |
The [`BitStore`] trait is the primary type-level description of `bitvec` views |
| 8 |
of the memory space and provides the runtime system that drives the crate memory |
| 9 |
model. |
| 10 |
|
| 11 |
## Memory Model |
| 12 |
|
| 13 |
`bitvec` considers all memory within [`BitSlice`] regions as if it were composed |
| 14 |
of discrete bits, each divisible and independent from its neighbors, just as the |
| 15 |
Rust memory model considers elements `T` in a slice `[T]`. Much as ordinary byte |
| 16 |
slices `[u8]` provide an API where each byte is distinct and independent from |
| 17 |
its neighbors, but the underlying processor silicon clusters them in words and |
| 18 |
cachelines, both the processor silicon *and* the Rust compiler require that bits |
| 19 |
in a `BitSlice` be grouped into memory elements, and collectively subjected to |
| 20 |
aliasing rules within their batch. |
| 21 |
|
| 22 |
`bitvec` manages this through the `BitStore` trait. It is implemented on three |
| 23 |
type families available from the Rust standard libraries: |
| 24 |
|
| 25 |
- [unsigned integers] |
| 26 |
- [atomic] unsigned integers |
| 27 |
- [`Cell`] wrappers of unsigned integers |
| 28 |
|
| 29 |
`bitvec` receives memory regions typed with one of these families and wraps it |
| 30 |
in one of its data structures based on the `BitSlice` region. The target |
| 31 |
processor is responsible for handling any contention between `T: BitStore` |
| 32 |
memory elements; this is irrelevant to the `bitvec` model. `bitvec` is solely |
| 33 |
responsible for proving to the Rust compiler that all memory accesses through |
| 34 |
its types are correctly managed according to the `&`/`&mut` shared/exclusion |
| 35 |
reference model, and the [`UnsafeCell`] shared-mutation model. |
| 36 |
|
| 37 |
Through `BitStore`, `bitvec` is able to demonstrate that `&mut BitSlice` |
| 38 |
references to a region of *bits* have no other `BitSlice` references capable of |
| 39 |
viewing those bits. However, multiple `&mut BitSlice` references may view the |
| 40 |
same underlying memory element, which is undefined behavior in the Rust compiler |
| 41 |
unless additional synchronization and mutual exclusion is provided to prevent |
| 42 |
racing writes and unsynchronized reads. |
| 43 |
|
| 44 |
As such, `BitStore` provides a closed type-system graph that the `BitSlice` |
| 45 |
region API uses to mark events that can induce aliasing over memory locations. |
| 46 |
When a `&mut BitSlice<_, T>` typed with an ordinary unsigned integer uses any of |
| 47 |
the APIs that call [`.split_at_mut()`], it transitions its `BitStore` parameter |
| 48 |
to `&mut BitSlice<_, T::Alias>`. The [`::Alias`] associated type is always a |
| 49 |
type that manages aliasing references to a single memory location: either an |
| 50 |
[atomic] unsigned integer `T` or a [`Cell<T>`][`Cell`]. The Rust standard |
| 51 |
library guarantees that these types will behave correctly when multiple |
| 52 |
references to a single location attempt to perform memory transactions. |
| 53 |
|
| 54 |
The atomic and `Cell` types stay as themselves when [`BitSlice`] introduces |
| 55 |
aliasing conditions, as they are already alias-aware. |
| 56 |
|
| 57 |
Foreign implementations of `BitStore` are required to follow the conventions |
| 58 |
used here: unsynchronized storage types must create marker newtypes over an |
| 59 |
appropriate synchronized type for `::Alias` and uphold the “only `&mut` has |
| 60 |
write permission” rule, while synchronized storage types do not need to perform |
| 61 |
these transitions, but may never transition to an unsynchronized type either. |
| 62 |
|
| 63 |
The `bitvec` memory description model as implemented in the [`domain`] module is |
| 64 |
able to perform the inverse transition: where a `BitSlice` can demonstrate a |
| 65 |
static awareness that the `&`/`&mut` exclusion rules are satisfied for a |
| 66 |
particular element slice `[T]`, it may apply the [`::Unalias`] marker to undo |
| 67 |
any `::Alias`ing, and present a type that has no more aliasing protection than |
| 68 |
that with which the memory region was initially declared. |
| 69 |
|
| 70 |
Namely, this means that the [atomic] and [`Cell`] wrappers will *never* be |
| 71 |
removed from a region that had them before it was given to `bitvec`, while a |
| 72 |
region of ordinary integers may regain the ability to be viewed without |
| 73 |
synchrony guards if `bitvec` can prove safety in the `domain` module. |
| 74 |
|
| 75 |
In order to retain `bitvec`’s promise that an `&mut BitSlice<_, T>` has the sole |
| 76 |
right of observation for all bits in its region, the unsigned integers alias to |
| 77 |
a crate-internal wrapper over the alias-capable standard-library types. This |
| 78 |
wrapper forbids mutation through shared references, so two [`BitSlice`] |
| 79 |
references that alias a memory location, but do not overlap in bits, may not be |
| 80 |
coërced to interfere with each other. |
| 81 |
|
| 82 |
[atomic]: core::sync::atomic |
| 83 |
[unsigned integers]: core::primitive |
| 84 |
[`BitSlice`]: crate::slice::BitSlice |
| 85 |
[`BitStore`]: self::BitStore |
| 86 |
[`Cell`]: core::cell::Cell |
| 87 |
[`UnsafeCell`]: core::cell::UnsafeCell |
| 88 |
[`domain`]: crate::domain |
| 89 |
[`::Alias`]: self::BitStore::Alias |
| 90 |
[`::Unalias`]: self::BitStore::Unalias |