Skip to content

Commit 69f2f5c

Browse files
committed
inline helpers.rs
1 parent 3658252 commit 69f2f5c

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ _no versions before 0.13.2 have a changelog as I started the changelog in that v
3838

3939
## Version 0.15.0
4040

41+
### Commit 6 (2025-8-05)
42+
43+
- Inline alloc_ext.rs, resize_in_place.rs, and helpers.rs
44+
4145
### Commit 5 (2025-8-05)
4246

4347
- Inline jemalloc.rs and mimalloc.rs

src/external_alloc/jemalloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl Alloc for Jemalloc {
155155
///
156156
/// - `ptr` must point to a block previously allocated with this allocator.
157157
/// - `old_layout` must describe exactly that block.
158+
#[inline]
158159
unsafe fn realloc(
159160
&self,
160161
ptr: NonNull<u8>,

src/external_alloc/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub(crate) const REALLOC_DIFF_ALIGN: &str =
1111
"unsupported operation: attempted to reallocate with a different alignment";
1212

1313
#[cfg(any(feature = "jemalloc", feature = "mimalloc"))]
14-
#[cfg_attr(miri, track_caller)]
1514
pub(crate) unsafe fn resize<F: Fn() -> *mut libc::c_void>(
1615
ralloc: F,
1716
ptr: core::ptr::NonNull<u8>,
@@ -99,8 +98,6 @@ pub mod ffi {
9998
// /// # Safety
10099
// ///
101100
// /// `ptr` must have been allocated by jemalloc and must not have been freed yet.
102-
103-
#[cfg_attr(miri, track_caller)]
104101
pub(crate) unsafe fn raw_ralloc(
105102
ptr: *mut c_void,
106103
old_layout: Layout,

src/features/alloc_ext.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait AllocExt: Alloc {
2424
/// - [`AllocError::AllocFailed`] if allocation fails.
2525
/// - [`AllocError::ZeroSizedLayout`] if `T::SZ == 0`.
2626
#[track_caller]
27+
#[inline]
2728
fn alloc_init<T, I: Fn(NonNull<T>)>(&self, init: I) -> Result<NonNull<T>, AllocError> {
2829
let guard = AllocGuard::new(self.alloc(T::LAYOUT)?.cast::<T>(), self);
2930
init(*guard);
@@ -37,6 +38,7 @@ pub trait AllocExt: Alloc {
3738
/// - [`AllocError::AllocFailed`] if allocation fails.
3839
/// - [`AllocError::ZeroSizedLayout`] if `T::SZ == 0`.
3940
#[track_caller]
41+
#[inline]
4042
fn alloc_default<T: Default>(&self) -> Result<NonNull<T>, AllocError> {
4143
self.alloc_write(T::default())
4244
}
@@ -48,6 +50,7 @@ pub trait AllocExt: Alloc {
4850
/// - [`AllocError::AllocFailed`] if allocation fails.
4951
/// - [`AllocError::ZeroSizedLayout`] if `T::SZ == 0`.
5052
#[cfg_attr(miri, track_caller)]
53+
#[inline]
5154
fn alloc_write<T>(&self, data: T) -> Result<NonNull<T>, AllocError> {
5255
alloc_then::<NonNull<T>, Self, T, _>(self, T::LAYOUT, data, |p, data| unsafe {
5356
let ptr: NonNull<T> = p.cast();
@@ -64,6 +67,7 @@ pub trait AllocExt: Alloc {
6467
/// - [`AllocError::AllocFailed`] if allocation fails.
6568
/// - [`AllocError::ZeroSizedLayout`] if `T::SZ == 0`.
6669
#[track_caller]
70+
#[inline]
6771
fn alloc_clone_to<T: Clone>(&self, data: &T) -> Result<NonNull<T>, AllocError> {
6872
alloc_then::<NonNull<T>, Self, &T, _>(self, T::LAYOUT, data, |p, data| {
6973
let mut guard = AllocGuard::new(p.cast(), self);
@@ -172,6 +176,7 @@ pub trait AllocExt: Alloc {
172176
/// - `ptr` must point to a block of memory allocated using this allocator, be valid for reads
173177
/// and writes, aligned, and a valid `T`.
174178
#[cfg_attr(miri, track_caller)]
179+
#[inline]
175180
unsafe fn drop_and_dealloc<T: ?Sized>(&self, ptr: NonNull<T>) {
176181
ptr::drop_in_place(ptr.as_ptr());
177182
self.dealloc(ptr.cast::<u8>(), ptr.layout());
@@ -184,6 +189,7 @@ pub trait AllocExt: Alloc {
184189
/// - `ptr` must point to a block of memory allocated using this allocator.
185190
/// - `layout` must describe exactly the same block.
186191
#[cfg_attr(miri, track_caller)]
192+
#[inline]
187193
unsafe fn zero_and_dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
188194
ptr::write_bytes(ptr.as_ptr(), 0, layout.size());
189195
self.dealloc(ptr, layout);
@@ -195,6 +201,7 @@ pub trait AllocExt: Alloc {
195201
///
196202
/// - `ptr` must point to a block of memory allocated using this allocator.
197203
#[cfg_attr(miri, track_caller)]
204+
#[inline]
198205
unsafe fn dealloc_typed<T: ?Sized>(&self, ptr: NonNull<T>) {
199206
self.dealloc(ptr.cast::<u8>(), ptr.layout());
200207
}
@@ -205,6 +212,7 @@ pub trait AllocExt: Alloc {
205212
///
206213
/// - `ptr` must point to a block of memory allocated using this allocator.
207214
#[cfg_attr(miri, track_caller)]
215+
#[inline]
208216
unsafe fn zero_and_dealloc_typed<T: ?Sized>(&self, ptr: NonNull<T>) {
209217
ptr::write_bytes(ptr.as_ptr().cast::<u8>(), 0, ptr.size());
210218
self.dealloc_typed(ptr);
@@ -217,6 +225,7 @@ pub trait AllocExt: Alloc {
217225
/// - `ptr` must point to a block of memory allocated using this allocator, be valid for reads
218226
/// and writes, aligned, and a valid `T`.
219227
#[cfg_attr(miri, track_caller)]
228+
#[inline]
220229
unsafe fn drop_zero_and_dealloc<T: ?Sized>(&self, ptr: NonNull<T>) {
221230
ptr::drop_in_place(ptr.as_ptr());
222231
self.zero_and_dealloc_typed(ptr);

src/features/resize_in_place.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,16 @@ impl ResizeInPlace for crate::external_alloc::mimalloc::MiMalloc {
375375
}
376376
}
377377

378-
/// Shrinking in-place is not supported by mimalloc. This is a no-op.
378+
/// Shrinking in-place is not supported by mimalloc.
379+
///
380+
/// This is a no-op and always returns an error.
379381
///
380382
/// # Errors
381383
///
382384
/// - [`AllocError::Other`]`("unsupported operation: attempted to shrink in place")`.
385+
// it just returns an error, no reason not to inline it.
386+
#[allow(clippy::inline_always)]
387+
#[inline(always)]
383388
unsafe fn shrink_in_place(
384389
&self,
385390
_: NonNull<u8>,

src/helpers.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use crate::{
2+
error::AllocError,
23
type_props::{
3-
varsized_nonnull_from_raw_parts,
4-
varsized_pointer_from_raw_parts,
5-
VarSized,
6-
PtrProps,
7-
SizedProps,
8-
USIZE_MAX_NO_HIGH_BIT
4+
varsized_nonnull_from_raw_parts, varsized_pointer_from_raw_parts, PtrProps, SizedProps,
5+
VarSized, USIZE_MAX_NO_HIGH_BIT,
96
},
10-
error::AllocError,
11-
Alloc
7+
Alloc,
128
};
139
use core::{
1410
alloc::Layout,
@@ -96,12 +92,6 @@ pub(crate) fn zsl_check<Ret, F: Fn(Layout) -> Result<Ret, AllocError>>(
9692
}
9793
}
9894

99-
/// Checks equality between two [`NonNull`] pointers.
100-
#[must_use]
101-
pub fn nonnull_eq<T: ?Sized>(a: NonNull<T>, b: NonNull<T>) -> bool {
102-
peq(a.as_ptr() as *const T, b.as_ptr() as *const T)
103-
}
104-
10595
/// Aligns the given value up to a non-zero alignment.
10696
#[must_use]
10797
pub const fn align_up(n: usize, align: NonZeroUsize) -> usize {
@@ -131,6 +121,7 @@ pub const fn dangling_nonnull_for(layout: Layout) -> NonNull<u8> {
131121
///
132122
/// The caller must ensure the `alignment` is a valid power of two.
133123
#[must_use]
124+
#[inline]
134125
pub const unsafe fn dangling_nonnull(align: usize) -> NonNull<u8> {
135126
transmute::<NonZeroUsize, NonNull<u8>>(NonZeroUsize::new_unchecked(align))
136127
}
@@ -200,6 +191,7 @@ impl<'a, T: ?Sized, A: Alloc + ?Sized> AllocGuard<'a, T, A> {
200191
const_if! {
201192
"extra_const",
202193
"Creates a new guard from a pointer and a reference to an allocator.",
194+
#[inline]
203195
pub const fn new(ptr: NonNull<T>, alloc: &'a A) -> AllocGuard<'a, T, A> {
204196
AllocGuard { ptr, alloc }
205197
}
@@ -209,6 +201,7 @@ impl<'a, T: ?Sized, A: Alloc + ?Sized> AllocGuard<'a, T, A> {
209201
"extra_extra_const",
210202
"Initializes the value by writing to the contained pointer.",
211203
#[cfg_attr(miri, track_caller)]
204+
#[inline]
212205
pub const fn init(&mut self, elem: T)
213206
where
214207
T: Sized
@@ -224,6 +217,7 @@ impl<'a, T: ?Sized, A: Alloc + ?Sized> AllocGuard<'a, T, A> {
224217
"Releases ownership of the allocation, preventing deallocation, and returns the raw \
225218
pointer.",
226219
#[must_use]
220+
#[inline]
227221
pub const fn release(self) -> NonNull<T> {
228222
let ptr = self.ptr;
229223
forget(self);
@@ -244,6 +238,7 @@ impl<T: ?Sized, A: Alloc + ?Sized> Drop for AllocGuard<'_, T, A> {
244238
impl<T: ?Sized, A: Alloc + ?Sized> Deref for AllocGuard<'_, T, A> {
245239
type Target = NonNull<T>;
246240

241+
#[inline]
247242
fn deref(&self) -> &NonNull<T> {
248243
&self.ptr
249244
}
@@ -299,6 +294,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
299294
const_if! {
300295
"extra_const",
301296
"Creates a new slice guard for `full` elements at `ptr` in the given allocator.",
297+
#[inline]
302298
pub const fn new(ptr: NonNull<T>, alloc: &'a A, full: usize)
303299
-> SliceAllocGuard<'a, T, A> {
304300
SliceAllocGuard {
@@ -315,6 +311,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
315311
"Release ownership of the slice without deallocating memory, returning a `NonNull<T>` \
316312
pointer to the slice.",
317313
#[must_use]
314+
#[inline]
318315
pub const fn release(self) -> NonNull<[T]> {
319316
let ret = self.get_init_part();
320317
forget(self);
@@ -327,6 +324,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
327324
"Release ownership of the slice without deallocating memory, returning a `NonNull<T>` \
328325
pointer to the slice's first element.",
329326
#[must_use]
327+
#[inline]
330328
pub const fn release_first(self) -> NonNull<T> {
331329
let ret = self.ptr;
332330
forget(self);
@@ -370,6 +368,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
370368
"extra_extra_const",
371369
"Sets the initialized element count.\n\n# Safety\n\nThe caller must ensure the new \
372370
count is correct.",
371+
#[inline]
373372
pub const unsafe fn set_init(&mut self, init: usize) {
374373
self.init = init;
375374
}
@@ -379,6 +378,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
379378
"extra_extra_const",
380379
"Initializes the next element of the slice with `elem`.\n\n# Errors\n\nReturns \
381380
`Err(elem)` if the slice is at capacity.",
381+
#[inline]
382382
pub const fn init(&mut self, elem: T) -> Result<(), T> {
383383
if self.init == self.full {
384384
return Err(elem);
@@ -394,6 +394,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
394394
"extra_extra_const",
395395
"Initializes the next element of the slice with `elem`.\n\n# Safety\n\nThe caller must \
396396
ensure that the slice is not at capacity. (`initialized() < full()`)",
397+
#[inline]
397398
pub const unsafe fn init_unchecked(&mut self, elem: T) {
398399
ptr::write(self.ptr.as_ptr().add(self.init), elem);
399400
self.init += 1;
@@ -426,6 +427,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
426427
"extra_const",
427428
"Returns how many elements have been initialized.",
428429
#[must_use]
430+
#[inline]
429431
pub const fn initialized(&self) -> usize {
430432
self.init
431433
}
@@ -435,6 +437,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
435437
"extra_const",
436438
"Returns the total number of elements in the slice.",
437439
#[must_use]
440+
#[inline]
438441
pub const fn full(&self) -> usize {
439442
self.full
440443
}
@@ -444,6 +447,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
444447
"extra_const",
445448
"Returns `true` if every element in the slice has been initialized.",
446449
#[must_use]
450+
#[inline]
447451
pub const fn is_full(&self) -> bool {
448452
self.init == self.full
449453
}
@@ -453,6 +457,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
453457
"extra_const",
454458
"Returns `true` if no elements have been initialized.",
455459
#[must_use]
460+
#[inline]
456461
pub const fn is_empty(&self) -> bool {
457462
self.init == 0
458463
}
@@ -508,6 +513,7 @@ impl<T, A: Alloc + ?Sized> Drop for SliceAllocGuard<'_, T, A> {
508513
impl<T, A: Alloc + ?Sized> Deref for SliceAllocGuard<'_, T, A> {
509514
type Target = NonNull<T>;
510515

516+
#[inline]
511517
fn deref(&self) -> &NonNull<T> {
512518
&self.ptr
513519
}

src/unstable_util.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{error::ArithOp, AllocError};
1+
use crate::{
2+
error::ArithOp, helpers::align_up_unchecked, type_props::USIZE_MAX_NO_HIGH_BIT, AllocError,
3+
};
24
use alloc::alloc::Layout;
35

46
#[cfg(feature = "metadata")]
@@ -24,7 +26,7 @@ pub const fn pad_layout_for(layout: Layout, align: usize) -> usize {
2426
}
2527

2628
let sz = layout.size();
27-
size_rounded_up_to_align(sz, align) - sz
29+
unsafe { align_up_unchecked(sz, align) - sz }
2830
}
2931

3032
/// Creates a layout by rounding the size of this layout up to a multiple of the layout's alignment.
@@ -33,11 +35,12 @@ pub const fn pad_layout_for(layout: Layout, align: usize) -> usize {
3335
#[must_use]
3436
#[inline]
3537
pub const fn pad_layout_to_align(layout: Layout, align: usize) -> Layout {
38+
if !align.is_power_of_two() {
39+
return unsafe { Layout::from_size_align_unchecked(USIZE_MAX_NO_HIGH_BIT, 1) };
40+
}
41+
3642
unsafe {
37-
Layout::from_size_align_unchecked(
38-
size_rounded_up_to_align(layout.size(), align),
39-
layout.align(),
40-
)
43+
Layout::from_size_align_unchecked(align_up_unchecked(layout.size(), align), layout.align())
4144
}
4245
}
4346

@@ -90,10 +93,3 @@ pub const fn repeat_layout_packed(layout: Layout, count: usize) -> Result<Layout
9093
))
9194
}
9295
}
93-
94-
// TODO: unchecked ops
95-
#[inline]
96-
const fn size_rounded_up_to_align(sz: usize, align: usize) -> usize {
97-
let sub1 = align - 1;
98-
(sz + sub1) & !sub1
99-
}

0 commit comments

Comments
 (0)