From 448bc43e47aa65fe351d355cd2bd8f534019472a Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Sun, 29 Jun 2025 08:34:11 +0300 Subject: [PATCH 1/6] lib: Expose SlabAlloc and SlabAllocError With these one can now store the SlabAlloc in a struct. Signed-off-by: Jon Doron --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3d47565..3516daf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,4 +3,4 @@ #![feature(allocator_api, maybe_uninit_slice)] mod slab; -pub use slab::SlabAllocator; +pub use slab::{SlabAlloc, SlabAllocError, SlabAllocator}; From f34e8a263c8d18daafda5123b18c8d123b5f924e Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Sun, 29 Jun 2025 08:35:27 +0300 Subject: [PATCH 2/6] Lib: Allocator API is not required An external user of the lib can use Allocator API if they want to, there is no dependency here for the Allocator API. Signed-off-by: Jon Doron --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3516daf..96cabc7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![doc = include_str!("../README.md")] #![no_std] -#![feature(allocator_api, maybe_uninit_slice)] +#![feature(maybe_uninit_slice)] mod slab; pub use slab::{SlabAlloc, SlabAllocError, SlabAllocator}; From b3619c6cf1d184687996d30bdda2c952e53a51f0 Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Sun, 29 Jun 2025 08:35:57 +0300 Subject: [PATCH 3/6] maybe_uninit_slice is deprecated use the old ways building a safe slice Signed-off-by: Jon Doron --- src/lib.rs | 1 - src/slab.rs | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 96cabc7..61e3f9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ #![doc = include_str!("../README.md")] #![no_std] -#![feature(maybe_uninit_slice)] mod slab; pub use slab::{SlabAlloc, SlabAllocError, SlabAllocator}; diff --git a/src/slab.rs b/src/slab.rs index 8ab3be3..97fe37a 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -90,7 +90,10 @@ impl SlabAllocator { b.write(AtomicU8::new(0)); } - unsafe { MaybeUninit::slice_assume_init_mut(bitmap) } + // The memory is now initialized. + unsafe { + core::slice::from_raw_parts_mut(bitmap.as_mut_ptr() as *mut AtomicU8, bitmap.len()) + } }; Ok(Self { From 6d4c338b620195bf8feebcc461d3adcbe327ce20 Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Mon, 30 Jun 2025 17:11:17 +0300 Subject: [PATCH 4/6] Fix alignment verification Signed-off-by: Jon Doron --- src/slab.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/slab.rs b/src/slab.rs index 97fe37a..8cc4456 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -58,9 +58,9 @@ impl SlabAllocator { /// Create a new instance of this allocator. /// If the input parameters are invalid, this will return a [SlabError]. pub fn new(mem: *mut MaybeUninit, size: usize) -> Result { - // Ensure the size is aligned to the size of the contained objects. - if size & (core::mem::size_of::() - 1) != 0 { - Err(SlabError::BadBaseAlignment)?; + // Verify the base address is aligned properly. + if (mem as usize) & (align_of::() - 1) != 0 { + return Err(SlabError::BadBaseAlignment); } let element_size = core::cmp::max(core::mem::size_of::(), core::mem::align_of::()); From b5b1b8ae33ba1a268d1ac1f7c547b0afa293db5d Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Sun, 6 Jul 2025 12:11:53 +0300 Subject: [PATCH 5/6] size_of already holds the size including the alignment padding Signed-off-by: Jon Doron --- src/slab.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slab.rs b/src/slab.rs index 8cc4456..3c9a4d7 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -63,7 +63,7 @@ impl SlabAllocator { return Err(SlabError::BadBaseAlignment); } - let element_size = core::cmp::max(core::mem::size_of::(), core::mem::align_of::()); + let element_size = core::mem::size_of::(); // Calculate the size of the data segment, subtracting out the ideal // bitmap size. From ceedd09dfde3148269e267d4d40f682b38b8ee66 Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Sun, 6 Jul 2025 12:48:12 +0300 Subject: [PATCH 6/6] Use explicit u8::BITS Signed-off-by: Jon Doron --- src/slab.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slab.rs b/src/slab.rs index 3c9a4d7..dba359b 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -67,7 +67,7 @@ impl SlabAllocator { // Calculate the size of the data segment, subtracting out the ideal // bitmap size. - let data_size = size - div_ceil(size / element_size, 8); + let data_size = size - div_ceil(size / element_size, u8::BITS as usize); // Partition off the data first. // FIXME: Does this ensure the alignment of elements? @@ -75,7 +75,7 @@ impl SlabAllocator { // Calculate the actual number of elements that can be stored in the data segment. let num_elems = data_size / element_size; - let bitmap_size = div_ceil(num_elems, 8); + let bitmap_size = div_ceil(num_elems, u8::BITS as usize); // Slice off the bitmap, taking care to initialize it. let bitmap = {