Debian Codegraph @

debian/rust-vendor/bitvec/doc/ptr/BitSpan.md

raw
1 # Encoded Bit-Span Descriptor
2
3 This structure is used as the actual in-memory value of `BitSlice` pointers
4 (including both `*{const,mut} BitSlice` and `&/mut BitSlice`). It is **not**
5 public API, and the encoding scheme does not support external modification.
6
7 Rust slices encode a base element address and an element count into a single
8 `&[T]` two-word value. `BitSpan` encodes a third value, the index of the base
9 bit within the base element, into unused bits of the address and length counter.
10
11 The slice reference has the ABI `(*T, usize)`, which is exactly two processor
12 words in size. `BitSpan` matches this ABI so that it can be cast into
13 `&/mut BitSlice` and used in reference-demanding APIs.
14
15 ## Layout
16
17 This structure is a more complex version of the `(*const T, usize)` tuple that
18 Rust uses to represent slices throughout the language. It breaks the pointer and
19 counter fundamentals into sub-field components. Rust does not have bitfield
20 syntax, so the below description of the structure layout is in C++.
21
22 ```cpp
23 template <typename T>
24 struct BitSpan {
25 uintptr_t ptr_head : __builtin_ctzll(alignof(T));
26 uintptr_t ptr_addr : sizeof(uintptr_T) * 8 - __builtin_ctzll(alignof(T));
27
28 size_t len_head : 3;
29 size_t len_bits : sizeof(size_t) * 8 - 3;
30 };
31 ```
32
33 This means that the `BitSpan<O, T>` has three *logical* fields, stored in four
34 segments, across the two *structural* fields of the type. The widths and
35 placements of each segment are functions of the size of `*const T`, `usize`, and
36 of the alignment of the `T` referent buffer element type.
37
38 ## Fields
39
40 ### Base Address
41
42 The address of the base element in a memory region is stored in all but the
43 lowest bits of the `ptr` field. An aligned pointer to `T` will always have its
44 lowest log<sub>2</sub>(byte width) bits zeroed, so those bits can be used to
45 store other information, as long as they are erased before dereferencing the
46 address as a pointer to `T`.
47
48 ### Head Bit Index
49
50 For any referent element type `T`, the selection of a single bit within the
51 element requires log<sub>2</sub>(byte width) bits to select a byte within the
52 element `T`, and another three bits to select a bit within the selected byte.
53
54 |Type |Alignment|Trailing Zeros|Count Bits|
55 |:----|--------:|-------------:|---------:|
56 |`u8` | 1| 0| 3|
57 |`u16`| 2| 1| 4|
58 |`u32`| 4| 2| 5|
59 |`u64`| 8| 3| 6|
60
61 The index of the first live bit in the base element is split to have its three
62 least significant bits stored in the least significant edge of the `len` field,
63 and its remaining bits stored in the least significant edge of the `ptr` field.
64
65 ### Length Counter
66
67 All but the lowest three bits of the `len` field are used to store a counter of
68 live bits in the referent region. When this is zero, the region is empty.
69 Because it is missing three bits, a `BitSpan` has only ⅛ of the index space of
70 a `usize` value.
71
72 ## Significant Values
73
74 The following values represent significant instances of the `BitSpan` type.
75
76 ### Null Slice
77
78 The fully-zeroed slot is not a valid member of the `BitSpan<O, T>` type; it is
79 reserved instead as the sentinel value for `Option::<BitSpan<O, T>>::None`.
80
81 ### Canonical Empty Slice
82
83 All pointers with a `bits: 0` logical field are empty. Pointers that are used to
84 maintain ownership of heap buffers are not permitted to erase their `addr`
85 field. The canonical form of the empty slice has an `addr` value of
86 [`NonNull::<T>::dangling()`], but all pointers to an empty region are equivalent
87 regardless of address.
88
89 #### Uninhabited Slices
90
91 Any empty pointer with a non-[`dangling()`] base address is considered to be an
92 uninhabited region. `BitSpan` never discards its address information, even as
93 operations may alter or erase its head-index or length values.
94
95 ## Type Parameters
96
97 - `T`: The memory type of the referent region. `BitSpan<O, T>` is a specialized
98 `*[T]` slice pointer, and operates on memory in terms of the `T` type for
99 access instructions and pointer calculation.
100 - `O`: The ordering within the register type. The bit-ordering used within a
101 region colors all pointers to the region, and orderings can never mix.
102
103 ## Safety
104
105 `BitSpan` values may only be constructed from pointers provided by the
106 surrounding program.
107
108 ## Undefined Behavior
109
110 Values of this type are binary-incompatible with slice pointers. Transmutation
111 of these values into any other type will result in an incorrect program, and
112 permit the program to begin illegal or undefined behaviors. This type may never
113 be manipulated in any way by user code outside of the APIs it offers to this
114 `bitvec`; it certainly may not be seen or observed by other crates.
115
116 ## Design Notes
117
118 Accessing the `.head` logical field would be faster if it inhabited the least
119 significant byte of `.len`, and was not partitioned into `.ptr` as well.
120 This implementation was chosen against in order to minimize the loss of bits in
121 the length counter; if user studies indicate that bit-slices do not **ever**
122 require more than 2<sup>24</sup> bits on 32-bit systems, this may be revisited.
123
124 The `ptr_metadata` feature, tracked in [Issue #81513], defines a trait `Pointee`
125 that regions such as `BitSlice` can implement and define a `Metadata` type that
126 carries all information other than a dereferenceable memory address. For regular
127 slices, this would be `impl<T> Pointee for [T] { type Metadata = usize; }`. For
128 `BitSlice`, it would be `(usize, BitIdx<T::Mem>)` and obviate this module
129 entirely. But until it stabilizes, this remains.
130
131 [Issue #81513]: https://github.com/rust-lang/rust/issues/81513
132
133 [`NonNull::<T>::dangling()`]: core::ptr::NonNull::dangling
134 [`dangling()`]: core::ptr::NonNull::dangling