musli/alloc/stack.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
#[cfg(test)]
mod tests;
use core::cell::UnsafeCell;
use core::marker::PhantomData;
use core::mem::{align_of, forget, replace, size_of, MaybeUninit};
use core::num::NonZeroU16;
use core::ops::{Deref, DerefMut};
use core::ptr;
use super::{Allocator, RawVec};
// We keep max bytes to 2^31, since that ensures that addition between two
// magnitutes never overflow.
const MAX_BYTES: usize = i32::MAX as usize;
/// A no-std compatible slice-based allocator that can be used with the `musli`
/// crate.
///
/// It is geared towards handling few allocations, but they can be arbitrarily
/// large. It is optimized to work best when allocations are short lived and
/// "merged back" into one previously allocated region through
/// `Buffer::extend`.
///
/// It's also optimized to write to one allocation "at a time". So once an
/// allocation has been grown once, it will be put in a region where it is
/// unlikely to need to be moved again, usually the last region which has access
/// to the remainder of the provided buffer.
///
/// For the moment, this allocator only supports 65535 unique allocations, which
/// is fine for use with the `musli` crate, but might be a limitation for other
/// use-cases.
///
/// ## Examples
///
/// ```
/// use musli::alloc::{ArrayBuffer, Slice, Vec};
///
/// let mut buf = ArrayBuffer::new();
/// let alloc = Slice::new(&mut buf);
///
/// let mut a = Vec::new_in(&alloc);
/// let mut b = Vec::new_in(&alloc);
///
/// b.write(b"He11o");
/// a.write(b.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o");
/// assert_eq!(a.len(), 5);
///
/// a.write(b" W0rld");
///
/// assert_eq!(a.as_slice(), b"He11o W0rld");
/// assert_eq!(a.len(), 11);
///
/// let mut c = Vec::new_in(&alloc);
/// c.write(b"!");
/// a.write(c.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o W0rld!");
/// assert_eq!(a.len(), 12);
/// ```
///
/// ## Design
///
/// The allocator takes a buffer of contiguous memory. This is dynamically
/// diviced into two parts:
///
/// * One part which grows upwards from the base, constituting the memory being
/// allocated.
/// * Its metadata growing downward from the end of the buffer, containing
/// headers for all allocated region.
///
/// By designing the allocator so that the memory allocated and its metadata is
/// separate, neighbouring regions can efficiently be merged as they are written
/// or freed.
///
/// Each allocation is sparse, meaning it does not try to over-allocate memory.
/// This ensures that subsequent regions with initialized memory can be merged
/// efficiently, but degrades performance for many small writes performed across
/// multiple allocations concurrently.
///
/// Below is an illustration of this, where `a` and `b` are two allocations
/// where we write one byte at a time to each. Here `x` below indicates an
/// occupied `gap` in memory regions.
///
/// ```text
/// a
/// ab
/// # a moved to end
/// xbaa
/// # b moved to 0
/// bbaa
/// # aa not moved
/// bbaaa
/// # bb moved to end
/// xxaaabbb
/// # aaa moved to 0
/// aaaaxbbb
/// # bbb not moved
/// aaaaxbbbb
/// # aaaa not moved
/// aaaaabbbb
/// # bbbbb not moved
/// aaaaabbbbb
/// # aaaaa moved to end
/// xxxxxbbbbbaaaaaa
/// # bbbbb moved to 0
/// bbbbbbxxxxaaaaaa
/// ```
pub struct Slice<'a> {
// This must be an unsafe cell, since it's mutably accessed through an
// immutable pointers. We simply make sure that those accesses do not
// clobber each other, which we can do since the API is restricted through
// the `RawVec` trait.
internal: UnsafeCell<Internal>,
// The underlying vector being borrowed.
_marker: PhantomData<&'a mut [MaybeUninit<u8>]>,
}
impl<'a> Slice<'a> {
/// Construct a new slice allocator.
///
/// See [type-level documentation][Slice] for more information.
///
/// # Panics
///
/// This panics if called with a buffer larger than `2^31` bytes.
pub fn new(buffer: &'a mut [MaybeUninit<u8>]) -> Self {
let size = buffer.len();
assert!(
size <= MAX_BYTES,
"Buffer of {size} bytes is larger than the maximum {MAX_BYTES}"
);
let mut data = Range::new(buffer.as_mut_ptr_range());
// The region of allocated headers grows downwards from the end of the
// buffer, so in order to ensure headers are aligned, we simply align
// the end pointer of the buffer preemptively here. Then we don't have
// to worry about it.
let align = data.end.align_offset(align_of::<Header>());
if align != 0 {
let sub = align_of::<Header>() - align;
if sub <= size {
// SAFETY: We've ensured that the adjustment is less than the
// size of the buffer.
unsafe {
data.end = data.end.sub(sub);
}
} else {
data.end = data.start;
}
}
Self {
internal: UnsafeCell::new(Internal {
free_head: None,
tail: None,
occupied: None,
full: data,
free: data,
}),
_marker: PhantomData,
}
}
}
impl Allocator for Slice<'_> {
type RawVec<'this, T> = SliceBuf<'this, T> where Self: 'this, T: 'this;
#[inline]
fn new_raw_vec<'a, T>(&'a self) -> Self::RawVec<'a, T>
where
T: 'a,
{
// SAFETY: We have exclusive access to the internal state, and it's only
// held for the duration of this call.
let region = if size_of::<T>() == 0 {
None
} else {
unsafe {
(*self.internal.get())
.alloc(0, align_of::<T>())
.map(|r| r.id)
}
};
SliceBuf {
region,
internal: &self.internal,
_marker: PhantomData,
}
}
}
/// A slice allocated buffer.
///
/// See [`Slice`].
pub struct SliceBuf<'a, T> {
region: Option<HeaderId>,
internal: &'a UnsafeCell<Internal>,
_marker: PhantomData<T>,
}
impl<'a, T> RawVec<T> for SliceBuf<'a, T> {
#[inline]
fn resize(&mut self, len: usize, additional: usize) -> bool {
if additional == 0 || size_of::<T>() == 0 {
return true;
}
let Some(region_id) = self.region else {
return false;
};
let Some(len) = len.checked_mul(size_of::<T>()) else {
return false;
};
let Some(additional) = additional.checked_mul(size_of::<T>()) else {
return false;
};
let Some(requested) = len.checked_add(additional) else {
return false;
};
if requested > MAX_BYTES {
return false;
}
// SAFETY: Due to invariants in the Buffer trait we know that these
// cannot be used incorrectly.
unsafe {
let i = &mut *self.internal.get();
let region = i.region(region_id);
// Region can already fit in the requested bytes.
if region.capacity() >= requested {
return true;
};
let Some(region) = i.realloc(region_id, len, requested, align_of::<T>()) else {
return false;
};
self.region = Some(region.id);
true
}
}
#[inline]
fn as_ptr(&self) -> *const T {
let Some(region) = self.region else {
return ptr::NonNull::dangling().as_ptr();
};
unsafe {
let i = &*self.internal.get();
let this = i.header(region);
this.range.start.cast_const().cast()
}
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
let Some(region) = self.region else {
return ptr::NonNull::dangling().as_ptr();
};
unsafe {
let i = &*self.internal.get();
let this = i.header(region);
this.range.start.cast()
}
}
#[inline]
fn try_merge<B>(&mut self, this_len: usize, buf: B, other_len: usize) -> Result<(), B>
where
B: RawVec<T>,
{
let Some(region) = self.region else {
return Err(buf);
};
let this_len = this_len * size_of::<T>();
let other_len = other_len * size_of::<T>();
// NB: Placing this here to make miri happy, since accessing the
// slice will mean mutably accessing the internal state.
let other_ptr = buf.as_ptr().cast();
unsafe {
let i = &mut *self.internal.get();
let mut this = i.region(region);
debug_assert!(this.capacity() >= this_len);
// If this region immediately follows the other region, we can
// optimize the write by simply growing the current region and
// de-allocating the second since the only conclusion is that
// they share the same allocator.
if !ptr::eq(this.range.end.cast_const(), other_ptr) {
return Err(buf);
}
let Some(next) = this.next else {
return Err(buf);
};
// Prevent the other buffer from being dropped, since we're
// taking care of the allocation in here directly instead.
forget(buf);
let next = i.region(next);
let to = this.range.start.wrapping_add(this_len);
// Data needs to be shuffle back to the end of the initialized
// region.
if this.range.end != to {
this.range.end.copy_to(to, other_len);
}
let old = i.free_region(next);
this.range.end = old.range.end;
Ok(())
}
}
}
impl<T> Drop for SliceBuf<'_, T> {
fn drop(&mut self) {
if let Some(region) = self.region.take() {
// SAFETY: We have exclusive access to the internal state.
unsafe {
(*self.internal.get()).free(region);
}
}
}
}
struct Region {
id: HeaderId,
ptr: *mut Header,
}
impl Deref for Region {
type Target = Header;
#[inline]
fn deref(&self) -> &Self::Target {
// SAFETY: Construction of the region is unsafe, so the caller must
// ensure that it's used correctly after that.
unsafe { &*self.ptr }
}
}
impl DerefMut for Region {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: Construction of the region is unsafe, so the caller must
// ensure that it's used correctly after that.
unsafe { &mut *self.ptr }
}
}
/// The identifier of a region.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(test, derive(PartialOrd, Ord, Hash))]
#[repr(transparent)]
struct HeaderId(NonZeroU16);
impl HeaderId {
#[cfg(test)]
const unsafe fn new_unchecked(value: u16) -> Self {
Self(NonZeroU16::new_unchecked(value))
}
/// Create a new region identifier.
///
/// # Safety
///
/// The given value must be non-zero.
#[inline]
fn new(value: isize) -> Option<Self> {
Some(Self(NonZeroU16::new(u16::try_from(value).ok()?)?))
}
/// Get the value of the region identifier.
#[inline]
fn get(self) -> usize {
self.0.get() as usize
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Range {
start: *mut MaybeUninit<u8>,
end: *mut MaybeUninit<u8>,
}
impl Range {
fn new(range: core::ops::Range<*mut MaybeUninit<u8>>) -> Self {
Self {
start: range.start,
end: range.end,
}
}
fn head(self) -> Range {
Self {
start: self.start,
end: self.start,
}
}
}
struct Internal {
// The first free region.
free_head: Option<HeaderId>,
// Pointer to the tail region.
tail: Option<HeaderId>,
// The occupied header region
occupied: Option<HeaderId>,
// The full range used by the allocator.
full: Range,
// The free range available to the allocator.
free: Range,
}
impl Internal {
// Return the number of allocates bytes.
#[cfg(test)]
#[inline]
fn bytes(&self) -> usize {
// SAFETY: It is guaranteed that free_end >= free_start inside of the provided region.
unsafe { self.free.start.byte_offset_from(self.full.start) as usize }
}
#[cfg(test)]
#[inline]
fn headers(&self) -> usize {
unsafe {
self.full
.end
.cast::<Header>()
.offset_from(self.free.end.cast()) as usize
}
}
// Return the number of remaining bytes.
#[inline]
fn remaining(&self) -> usize {
// SAFETY: It is guaranteed that free_end >= free_start inside of the provided region.
unsafe { self.free.end.byte_offset_from(self.free.start) as usize }
}
/// Get the header pointer corresponding to the given id.
#[inline]
fn header(&self, at: HeaderId) -> &Header {
// SAFETY: Once we've coerced to `&self`, then we guarantee that we can
// get a header immutably.
unsafe { &*self.full.end.cast::<Header>().wrapping_sub(at.get()) }
}
/// Get the mutable header pointer corresponding to the given id.
#[inline]
fn header_mut(&mut self, at: HeaderId) -> *mut Header {
self.full.end.cast::<Header>().wrapping_sub(at.get())
}
/// Get the mutable region corresponding to the given id.
#[inline]
fn region(&mut self, id: HeaderId) -> Region {
Region {
id,
ptr: self.header_mut(id),
}
}
unsafe fn unlink(&mut self, header: &Header) {
if let Some(next) = header.next {
(*self.header_mut(next)).prev = header.prev;
} else {
self.tail = header.prev;
}
if let Some(prev) = header.prev {
(*self.header_mut(prev)).next = header.next;
}
}
unsafe fn replace_back(&mut self, region: &mut Region) {
let prev = region.prev.take();
let next = region.next.take();
if let Some(prev) = prev {
(*self.header_mut(prev)).next = next;
}
if let Some(next) = next {
(*self.header_mut(next)).prev = prev;
}
self.push_back(region);
}
unsafe fn push_back(&mut self, region: &mut Region) {
if let Some(tail) = self.tail.replace(region.id) {
region.prev = Some(tail);
(*self.header_mut(tail)).next = Some(region.id);
}
}
/// Free a region.
unsafe fn free_region(&mut self, region: Region) -> Header {
self.unlink(®ion);
region.ptr.replace(Header {
range: self.full.head(),
next: self.free_head.replace(region.id),
prev: None,
})
}
/// Allocate a new header.
///
/// # Safety
///
/// The caller msut ensure that there is enough space for the region and
/// that the end pointer has been aligned to the requirements of `Header`.
unsafe fn alloc_header(&mut self, end: *mut MaybeUninit<u8>) -> Option<Region> {
if let Some(region) = self.free_head.take() {
let mut region = self.region(region);
region.range.start = self.free.start;
region.range.end = end;
return Some(region);
}
debug_assert_eq!(
self.free.end.align_offset(align_of::<Header>()),
0,
"End pointer should be aligned to header"
);
let ptr = self.free.end.cast::<Header>().wrapping_sub(1);
if ptr < self.free.start.cast() || ptr >= self.free.end.cast() {
return None;
}
let id = HeaderId::new(self.full.end.cast::<Header>().offset_from(ptr))?;
ptr.write(Header {
range: Range::new(self.free.start..end),
prev: None,
next: None,
});
self.free.end = ptr.cast();
Some(Region { id, ptr })
}
/// Allocate a region.
///
/// # Safety
///
/// The caller must ensure that `this` is exclusively available.
unsafe fn alloc(&mut self, requested: usize, align: usize) -> Option<Region> {
if let Some(occupied) = self.occupied {
let region = self.region(occupied);
if region.capacity() >= requested && region.is_aligned(align) {
self.occupied = None;
return Some(region);
}
}
self.align(align)?;
if self.remaining() < requested {
return None;
}
let end = self.free.start.wrapping_add(requested);
let mut region = self.alloc_header(end)?;
self.free.start = end;
debug_assert!(self.free.start <= self.free.end);
self.push_back(&mut region);
Some(region)
}
/// Align the free region by the specified alignment.
///
/// This might require either expanding the tail region, or introducing an
/// occupied region which matches the number of bytes needed to fulfill the
/// specified alignment.
unsafe fn align(&mut self, align: usize) -> Option<()> {
let align = self.free.start.align_offset(align);
if align == 0 {
return Some(());
}
if self.remaining() < align {
return None;
}
let aligned_start = self.free.start.wrapping_add(align);
if let Some(tail) = self.tail {
// Simply expand the tail region to fill the gap created.
self.region(tail).range.end = aligned_start;
} else {
// We need to construct a new occupied header to fill in the gap
// which we just aligned from since there is no previous region to
// expand.
let mut region = self.alloc_header(aligned_start)?;
self.push_back(&mut region);
}
self.free.start = aligned_start;
Some(())
}
unsafe fn free(&mut self, region: HeaderId) {
let region = self.region(region);
// Just free up the last region in the slab.
if region.next.is_none() {
self.free_tail(region);
return;
}
// If there is no previous region, then mark this region as occupy.
let Some(prev) = region.prev else {
debug_assert!(
self.occupied.is_none(),
"There can only be one occupied region"
);
self.occupied = Some(region.id);
return;
};
let mut prev = self.region(prev);
// Move allocation to the previous region.
let region = self.free_region(region);
prev.range.end = region.range.end;
// The current header being freed is the last in the list.
if region.next.is_none() {
self.free.start = region.range.start;
}
}
/// Free the tail starting at the `current` region.
unsafe fn free_tail(&mut self, current: Region) {
debug_assert_eq!(self.tail, Some(current.id));
let current = self.free_region(current);
debug_assert_eq!(current.next, None);
self.free.start = match current.prev {
// The prior region is occupied, so we can free that as well.
Some(prev) if self.occupied == Some(prev) => {
self.occupied = None;
let prev = self.region(prev);
self.free_region(prev).range.start
}
_ => current.range.start,
};
}
unsafe fn reserve(&mut self, additional: usize, align: usize) -> Option<*mut MaybeUninit<u8>> {
self.align(align)?;
let free_start = self.free.start.wrapping_add(additional);
if free_start > self.free.end || free_start < self.free.start {
return None;
}
Some(free_start)
}
unsafe fn realloc(
&mut self,
from: HeaderId,
len: usize,
requested: usize,
align: usize,
) -> Option<Region> {
let mut from = self.region(from);
// This is the last region in the slab, so we can just expand it.
if from.next.is_none() {
// Before we call realloc, we check the capacity of the current
// region. So we know that it is <= requested.
let additional = requested - from.capacity();
self.free.start = self.reserve(additional, align)?;
from.range.end = from.range.end.add(additional);
return Some(from);
}
// There is no data allocated in the current region, so we can simply
// re-link it to the end of the chain of allocation.
if from.range.start == from.range.end {
let free_start = self.reserve(requested, align)?;
from.range.start = replace(&mut self.free.start, free_start);
from.range.end = free_start;
self.replace_back(&mut from);
return Some(from);
}
// Try to merge with a preceeding region, if the requested memory can
// fit in it.
'bail: {
// Check if the immediate prior region can fit the requested allocation.
let Some(prev) = from.prev else {
break 'bail;
};
if self.occupied != Some(prev) {
break 'bail;
}
let mut prev = self.region(prev);
if prev.capacity() + from.capacity() < requested {
break 'bail;
}
if !prev.is_aligned(align) {
break 'bail;
}
let from = self.free_region(from);
from.range.start.copy_to(prev.range.start, len);
prev.range.end = from.range.end;
self.occupied = None;
return Some(prev);
}
let to = self.alloc(requested, align)?;
from.range.start.copy_to_nonoverlapping(to.range.start, len);
self.free(from.id);
Some(to)
}
}
/// The header of a region.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Header {
// The range of the allocated region.
range: Range,
// The previous region.
prev: Option<HeaderId>,
// The next region.
next: Option<HeaderId>,
}
impl Header {
#[inline]
fn capacity(&self) -> usize {
// SAFETY: Both pointers are defined within the region.
unsafe { self.range.end.byte_offset_from(self.range.start) as usize }
}
/// Test if region is aligned to `align`.
#[inline]
fn is_aligned(&self, align: usize) -> bool {
self.range.start.align_offset(align) == 0
}
}