| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | pub mod projection;
|
| 6 | pub use crate::project_pointer as project;
|
| 7 |
|
| 8 | use core::mem::{
|
| 9 | align_of,
|
| 10 | size_of, |
| 11 | };
|
| 12 | use core::num::NonZero;
|
| 13 |
|
| 14 | use crate::const_assert;
|
| 15 |
|
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #[repr(transparent)]
|
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
| 31 | pub struct Alignment(NonZero<usize>);
|
| 32 |
|
| 33 | impl Alignment {
|
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | #[inline(always)]
|
| 48 | pub const fn new<const ALIGN: usize>() -> Self {
|
| 49 | const_assert!(
|
| 50 | ALIGN.is_power_of_two(),
|
| 51 | "Provided alignment is not a power of two."
|
| 52 | );
|
| 53 |
|
| 54 | |
| 55 | |
| 56 | Self(unsafe { NonZero::new_unchecked(ALIGN) })
|
| 57 | }
|
| 58 |
|
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | #[inline(always)]
|
| 75 | pub const fn new_checked(align: usize) -> Option<Self> {
|
| 76 | if align.is_power_of_two() {
|
| 77 | |
| 78 | |
| 79 | Some(Self(unsafe { NonZero::new_unchecked(align) }))
|
| 80 | } else {
|
| 81 | None
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | |
| 86 | |
| 87 | |
| 88 | #[inline(always)]
|
| 89 | pub const fn of<T>() -> Self {
|
| 90 | |
| 91 | |
| 92 | |
| 93 | const { Alignment::new_checked(align_of::<T>()).unwrap() }
|
| 94 | }
|
| 95 |
|
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | #[inline(always)]
|
| 108 | pub const fn as_usize(self) -> usize {
|
| 109 | self.as_nonzero().get()
|
| 110 | }
|
| 111 |
|
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | #[inline(always)]
|
| 124 | pub const fn as_nonzero(self) -> NonZero<usize> {
|
| 125 | |
| 126 | |
| 127 | if !self.0.is_power_of_two() {
|
| 128 | |
| 129 | |
| 130 | unsafe { core::hint::unreachable_unchecked() }
|
| 131 | }
|
| 132 | self.0
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | #[inline(always)]
|
| 146 | pub const fn log2(self) -> u32 {
|
| 147 | self.0.ilog2()
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | #[inline(always)]
|
| 162 | pub const fn mask(self) -> usize {
|
| 163 | |
| 164 | |
| 165 | !(self.as_usize() - 1)
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | |
| 170 | pub trait Alignable: Sized {
|
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | fn align_down(self, alignment: Alignment) -> Self;
|
| 183 |
|
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | fn align_up(self, alignment: Alignment) -> Option<Self>;
|
| 199 | }
|
| 200 |
|
| 201 | |
| 202 | macro_rules! impl_alignable_uint {
|
| 203 | ($($t:ty),*) => {
|
| 204 | $(
|
| 205 | impl Alignable for $t {
|
| 206 | #[inline(always)]
|
| 207 | fn align_down(self, alignment: Alignment) -> Self {
|
| 208 | |
| 209 | |
| 210 | ::core::num::NonZero::<Self>::try_from(alignment.as_nonzero())
|
| 211 | .map(|align| self & !(align.get() - 1))
|
| 212 | |
| 213 | .unwrap_or(0)
|
| 214 | }
|
| 215 |
|
| 216 | #[inline(always)]
|
| 217 | fn align_up(self, alignment: Alignment) -> Option<Self> {
|
| 218 | let aligned_down = self.align_down(alignment);
|
| 219 | if self == aligned_down {
|
| 220 | Some(aligned_down)
|
| 221 | } else {
|
| 222 | Self::try_from(alignment.as_usize())
|
| 223 | .ok()
|
| 224 | .and_then(|align| aligned_down.checked_add(align))
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 | )*
|
| 229 | };
|
| 230 | }
|
| 231 |
|
| 232 | impl_alignable_uint!(u8, u16, u32, u64, usize);
|
| 233 |
|
| 234 | |
| 235 | |
| 236 | |
| 237 | pub trait KnownSize {
|
| 238 | |
| 239 | const MIN_SIZE: usize;
|
| 240 |
|
| 241 | |
| 242 | const MIN_ALIGN: Alignment;
|
| 243 |
|
| 244 | |
| 245 | fn size(p: *const Self) -> usize;
|
| 246 | }
|
| 247 |
|
| 248 | impl<T> KnownSize for T {
|
| 249 | const MIN_SIZE: usize = size_of::<T>();
|
| 250 | const MIN_ALIGN: Alignment = Alignment::of::<T>();
|
| 251 |
|
| 252 | #[inline(always)]
|
| 253 | fn size(_: *const Self) -> usize {
|
| 254 | size_of::<T>()
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | impl<T> KnownSize for [T] {
|
| 259 | const MIN_SIZE: usize = 0;
|
| 260 | const MIN_ALIGN: Alignment = Alignment::of::<T>();
|
| 261 |
|
| 262 | #[inline(always)]
|
| 263 | fn size(p: *const Self) -> usize {
|
| 264 | p.len() * size_of::<T>()
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | #[inline(always)]
|
| 290 | pub const fn const_align_up(value: usize, align: Alignment) -> Option<usize> {
|
| 291 | match value.checked_add(align.as_usize() - 1) {
|
| 292 | Some(v) => Some(v & align.mask()),
|
| 293 | None => None,
|
| 294 | }
|
| 295 | }
|