Skip to content

Commit df08af3

Browse files
geeknoidMartin Taillefer
andauthored
feat: Adopt custom AllocError type for Ralf-compatibility (#536)
Co-authored-by: Martin Taillefer <mataille@microsoft.com>
1 parent 75cac4d commit df08af3

33 files changed

Lines changed: 426 additions & 189 deletions

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fundle_macros = { path = "crates/fundle_macros", default-features = false, versi
7373
fundle_macros_impl = { path = "crates/fundle_macros_impl", default-features = false, version = "0.3.4" }
7474
http_extensions = { path = "crates/http_extensions", default-features = false, version = "0.6.4" }
7575
layered = { path = "crates/layered", default-features = false, version = "0.3.5" }
76-
multitude = { path = "crates/multitude", default-features = false, version = "0.5.1" }
76+
multitude = { path = "crates/multitude", default-features = false, version = "0.6.0" }
7777
ohno = { path = "crates/ohno", default-features = false, version = "0.3.8" }
7878
ohno_macros = { path = "crates/ohno_macros", default-features = false, version = "0.3.5" }
7979
plurality = { path = "crates/plurality", default-features = false, version = "0.1.0" }

crates/multitude/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[package]
55
name = "multitude"
6-
version = "0.5.1"
6+
version = "0.6.0"
77
description = "Fast and flexible arena allocator."
88
readme = "README.md"
99
keywords = ["arena", "memory", "allocator", "bump"]

crates/multitude/README.md

Lines changed: 91 additions & 91 deletions
Large diffs are not rendered by default.

crates/multitude/src/allocator_impl.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ unsafe impl<A: Allocator + Clone> Allocator for &Arena<A> {
5757
return Ok(NonNull::slice_from_raw_parts(ptr, layout.size()));
5858
}
5959
if self.is_oversized(refill_hint) {
60-
return self.alloc_oversized_shared_with(refill_hint, |mutator, chunk_ptr| {
61-
let (slot, _chunk) = mutator
62-
.try_alloc_with_chunk(layout.size(), layout.align())
63-
.expect("dedicated oversized chunk sized to fit allocation + alignment slack");
64-
let chunk_ref = acquire_chunk_ref::<A>(chunk_ptr);
65-
let ptr = slot.as_non_null();
66-
let _ = chunk_ref.forget();
67-
NonNull::slice_from_raw_parts(ptr, layout.size())
68-
});
60+
return self
61+
.alloc_oversized_shared_with(refill_hint, |mutator, chunk_ptr| {
62+
let (slot, _chunk) = mutator
63+
.try_alloc_with_chunk(layout.size(), layout.align())
64+
.expect("dedicated oversized chunk sized to fit allocation + alignment slack");
65+
let chunk_ref = acquire_chunk_ref::<A>(chunk_ptr);
66+
let ptr = slot.as_non_null();
67+
let _ = chunk_ref.forget();
68+
NonNull::slice_from_raw_parts(ptr, layout.size())
69+
})
70+
.map_err(Into::into);
6971
}
7072
self.refill(refill_hint)?;
7173
}

crates/multitude/src/arena/alloc_growable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//! All public methods are documented on [`Arena`] itself; this file
77
//! groups the family together to keep the central `mod.rs` smaller.
88
9-
use allocator_api2::alloc::{AllocError, Allocator};
9+
use allocator_api2::alloc::Allocator;
1010

1111
use super::Arena;
12+
use crate::AllocError;
1213
use crate::arena::ExpectAlloc;
1314
use crate::strings::{FromUtf16Error, String};
1415
use crate::vec::Vec;

crates/multitude/src/arena/alloc_prefixed.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
use core::mem;
1919
use core::ptr::{self, NonNull};
2020

21-
use allocator_api2::alloc::{AllocError, Allocator};
21+
use allocator_api2::alloc::Allocator;
2222

2323
use super::Arena;
2424
use super::alloc_value::acquire_chunk_ref;
25+
use crate::AllocError;
2526
use crate::internal::chunk_ref::ChunkRef;
2627
use crate::internal::thin_dst;
2728

@@ -73,8 +74,8 @@ impl<A: Allocator + Clone> Arena<A> {
7374
// pointer is strictly inside the chunk (never one-past-end at
7475
// `chunk_base + CHUNK_ALIGN`), preserving the mask-based chunk
7576
// recovery invariant used by the smart pointers' `Drop`.
76-
let payload_bytes = len.checked_mul(elem_size).ok_or(AllocError)?.max(elem_align);
77-
let total = PREFIX_BYTES.checked_add(payload_bytes).ok_or(AllocError)?;
77+
let payload_bytes = len.checked_mul(elem_size).ok_or(AllocError::CAPACITY_OVERFLOW)?.max(elem_align);
78+
let total = PREFIX_BYTES.checked_add(payload_bytes).ok_or(AllocError::CAPACITY_OVERFLOW)?;
7879
// `total` is an exact reservation size, not a worst-case hint: unlike
7980
// the slice paths (which permit over-aligned `T` and so add `elem_align`
8081
// of front-padding slack to their routing hint), the const-assert above

crates/multitude/src/arena/alloc_slice_arc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use core::mem;
99
use core::pin::Pin;
1010
use core::ptr::NonNull;
1111

12-
use allocator_api2::alloc::{AllocError, Allocator};
12+
use allocator_api2::alloc::Allocator;
1313

1414
use super::alloc_prefixed::worst_case_strong_slice_payload;
1515
use super::alloc_value::{MAX_SMART_PTR_ALIGN, acquire_chunk_ref};
1616
use super::{Arena, ExpectAlloc};
17+
use crate::AllocError;
1718
use crate::arc::Arc;
1819
use crate::internal::thin_dst::{AtomicStrong, LocalStrong, Strong};
1920
use crate::rc::Rc;
@@ -465,7 +466,7 @@ impl<A: Allocator + Clone> Arena<A> {
465466
#[inline]
466467
fn check_slice_arc_layout<T>() -> Result<(), AllocError> {
467468
if mem::align_of::<T>() >= MAX_SMART_PTR_ALIGN {
468-
return Err(AllocError);
469+
return Err(AllocError::ALIGNMENT_TOO_LARGE);
469470
}
470471
Ok(())
471472
}

crates/multitude/src/arena/alloc_slice_box.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use core::mem;
99
use core::pin::Pin;
1010
use core::ptr::{self, NonNull};
1111

12-
use allocator_api2::alloc::{AllocError, Allocator};
12+
use allocator_api2::alloc::Allocator;
1313

1414
use super::alloc_prefixed::worst_case_thin_slice_payload;
1515
use super::alloc_value::{MAX_SMART_PTR_ALIGN, acquire_chunk_ref};
1616
use super::{Arena, ExpectAlloc};
17+
use crate::AllocError;
1718
use crate::r#box::Box;
1819

1920
impl<A: Allocator + Clone> Arena<A> {
@@ -181,7 +182,7 @@ impl<A: Allocator + Clone> Arena<A> {
181182
fn impl_alloc_slice_box_with<T, F: FnMut(usize) -> T>(&self, len: usize, mut f: F) -> Result<Box<[T], A>, AllocError> {
182183
check_slice_box_layout::<T>(len)?;
183184
// Check overflow before the refill loop.
184-
let payload_bytes = mem::size_of::<T>().checked_mul(len).ok_or(AllocError)?;
185+
let payload_bytes = mem::size_of::<T>().checked_mul(len).ok_or(AllocError::CAPACITY_OVERFLOW)?;
185186
let ptr = self.reserve_slice_box::<T>(len, payload_bytes, |slot_ptr| {
186187
// SAFETY: `slot_ptr` is the reservation start; we init `len` slots
187188
// with panic-safe rollback via `InitGuard`.
@@ -293,7 +294,7 @@ impl<A: Allocator + Clone> Arena<A> {
293294
#[inline]
294295
fn check_slice_box_layout<T>(_len: usize) -> Result<(), AllocError> {
295296
if mem::align_of::<T>() >= MAX_SMART_PTR_ALIGN {
296-
return Err(AllocError);
297+
return Err(AllocError::ALIGNMENT_TOO_LARGE);
297298
}
298299
Ok(())
299300
}

crates/multitude/src/arena/alloc_slice_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use core::hint::assert_unchecked;
1616
use core::mem;
1717
use core::ptr::NonNull;
1818

19-
use allocator_api2::alloc::{AllocError, Allocator};
19+
use allocator_api2::alloc::Allocator;
2020

2121
use super::{Arena, ExpectAlloc};
22-
use crate::Alloc;
2322
use crate::internal::constants::CHUNK_ALIGN;
23+
use crate::{Alloc, AllocError};
2424

2525
/// Reject over-aligned slice element types early. Simple-reference
2626
/// slices return a plain `&mut [T]` (no header-recovery mask), so they
@@ -30,7 +30,7 @@ use crate::internal::constants::CHUNK_ALIGN;
3030
#[inline(always)]
3131
fn reject_over_aligned<T>() -> Result<(), AllocError> {
3232
if const { mem::align_of::<T>() >= CHUNK_ALIGN } {
33-
return Err(AllocError);
33+
return Err(AllocError::ALIGNMENT_TOO_LARGE);
3434
}
3535
Ok(())
3636
}

0 commit comments

Comments
 (0)