Skip to content

Commit 83ba3cb

Browse files
committed
Change Region to require [MaybeUninit<u8>] rather than [u8]
Closes #11
1 parent 375d762 commit 83ba3cb

File tree

7 files changed

+71
-50
lines changed

7 files changed

+71
-50
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## [v0.5](https://timdiekmann.github.io/alloc-compose/alloc_compose/index.html) (Unreleased)
22

3-
- **Breaking Change** Add `AllocAll` trait and move some methods from `Region` into that trait
4-
- **Breaking Change** Remove `MemoryMarker`
3+
**Breaking Changes:**
4+
- Add `AllocAll` trait and move some methods from `Region` into that trait
5+
- Change `Region` to require `[MaybeUninit<u8>]` rather than `[u8]`
6+
- Remove `MemoryMarker`
57

68
## [v0.4](https://docs.rs/alloc-compose/0.4)
79

src/affix.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ mod tests {
546546
Affix::<System, Prefix, Suffix>::prefix(memory.ptr, layout).as_ref(),
547547
&prefix
548548
);
549-
assert_eq!(memory.as_slice(), &vec![0_u8; memory.size][..]);
549+
assert_eq!(MaybeUninit::slice_get_ref(memory.as_slice()), &vec![0_u8; memory.size][..]);
550550
assert_eq!(
551551
Affix::<System, Prefix, Suffix>::suffix(memory.ptr, layout).as_ref(),
552552
&suffix
@@ -569,7 +569,7 @@ mod tests {
569569
&prefix
570570
);
571571
assert_eq!(
572-
growed_memory.as_slice(),
572+
MaybeUninit::slice_get_ref(growed_memory.as_slice()),
573573
&vec![0_u8; growed_memory.size][..]
574574
);
575575
assert_eq!(
@@ -590,7 +590,7 @@ mod tests {
590590
Affix::<System, Prefix, Suffix>::prefix(memory.ptr, layout).as_ref(),
591591
&prefix
592592
);
593-
assert_eq!(memory.as_slice(), &vec![0_u8; memory.size][..]);
593+
assert_eq!(MaybeUninit::slice_get_ref(memory.as_slice()), &vec![0_u8; memory.size][..]);
594594
assert_eq!(
595595
Affix::<System, Prefix, Suffix>::suffix(memory.ptr, layout).as_ref(),
596596
&suffix

src/fallback.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use core::{
2323
///
2424
/// use alloc_compose::{Fallback, Owns, Region};
2525
/// use std::alloc::{AllocInit, AllocRef, Layout, System};
26+
/// use std::mem::MaybeUninit;
2627
///
27-
/// let mut data = [0; 32];
28+
/// let mut data = [MaybeUninit::new(0); 32];
2829
/// let mut alloc = Fallback {
2930
/// primary: Region::new(&mut data),
3031
/// secondary: System,
@@ -137,10 +138,11 @@ mod tests {
137138
use super::Fallback;
138139
use crate::{helper, Owns, Region};
139140
use std::alloc::{AllocInit, AllocRef, Layout, ReallocPlacement, System};
141+
use std::mem::MaybeUninit;
140142

141143
#[test]
142144
fn alloc() {
143-
let mut data = [0; 32];
145+
let mut data = [MaybeUninit::new(0); 32];
144146
let mut alloc = Fallback {
145147
primary: helper::tracker(Region::new(&mut data)),
146148
secondary: helper::tracker(System),
@@ -163,7 +165,7 @@ mod tests {
163165

164166
#[test]
165167
fn grow() {
166-
let mut data = [0; 80];
168+
let mut data = [MaybeUninit::new(0); 80];
167169
let mut alloc = Fallback {
168170
primary: helper::tracker(Region::new(&mut data)),
169171
secondary: helper::tracker(System),
@@ -226,7 +228,7 @@ mod tests {
226228

227229
#[test]
228230
fn shrink() {
229-
let mut data = [0; 80];
231+
let mut data = [MaybeUninit::new(0); 80];
230232
let mut alloc = Fallback {
231233
primary: helper::tracker(Region::new(&mut data)),
232234
secondary: helper::tracker(System),
@@ -275,8 +277,8 @@ mod tests {
275277

276278
#[test]
277279
fn owns() {
278-
let mut data_1 = [0; 32];
279-
let mut data_2 = [0; 64];
280+
let mut data_1 = [MaybeUninit::new(0); 32];
281+
let mut data_2 = [MaybeUninit::new(0); 64];
280282
let mut alloc = Fallback {
281283
primary: Region::new(&mut data_1),
282284
secondary: Region::new(&mut data_2),

src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![cfg_attr(not(test), no_std)]
22
#![cfg_attr(doc, feature(doc_cfg, external_doc))]
33
#![cfg_attr(feature = "intrinsics", feature(core_intrinsics))]
4+
#![cfg_attr(test, feature(maybe_uninit_slice_assume_init))]
45
#![cfg_attr(doc, doc(include = "../README.md"))]
56
#![feature(
67
allocator_api,
@@ -157,6 +158,7 @@ pub(crate) mod helper {
157158
use std::{
158159
alloc::{AllocErr, AllocInit, AllocRef, Layout, MemoryBlock, ReallocPlacement, System},
159160
collections::HashMap,
161+
mem::MaybeUninit,
160162
ptr::NonNull,
161163
slice,
162164
sync::{Mutex, PoisonError},
@@ -320,16 +322,16 @@ pub(crate) mod helper {
320322
}
321323

322324
pub trait AsSlice {
323-
unsafe fn as_slice<'a>(self) -> &'a [u8];
324-
unsafe fn as_slice_mut<'a>(self) -> &'a mut [u8];
325+
unsafe fn as_slice<'a>(self) -> &'a [MaybeUninit<u8>];
326+
unsafe fn as_slice_mut<'a>(self) -> &'a mut [MaybeUninit<u8>];
325327
}
326328

327329
impl AsSlice for MemoryBlock {
328-
unsafe fn as_slice<'a>(self) -> &'a [u8] {
329-
slice::from_raw_parts(self.ptr.as_ptr(), self.size)
330+
unsafe fn as_slice<'a>(self) -> &'a [MaybeUninit<u8>] {
331+
slice::from_raw_parts(self.ptr.cast().as_ptr(), self.size)
330332
}
331-
unsafe fn as_slice_mut<'a>(self) -> &'a mut [u8] {
332-
slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size)
333+
unsafe fn as_slice_mut<'a>(self) -> &'a mut [MaybeUninit<u8>] {
334+
slice::from_raw_parts_mut(self.ptr.cast().as_ptr(), self.size)
333335
}
334336
}
335337

src/proxy.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ use core::{
4444
/// stats::{AllocInitFilter, ResultFilter},
4545
/// Region,
4646
/// };
47+
/// use core::mem::MaybeUninit;
4748
///
4849
/// let counter = stats::FilteredCounter::default();
49-
/// let mut data = [0; 32];
50+
/// let mut data = [MaybeUninit::new(0); 32];
5051
/// let mut alloc = Proxy {
5152
/// alloc: Region::new(&mut data),
5253
/// callbacks: counter.by_ref(),

src/region.rs

+44-31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{unlikely, AllocAll, Owns};
22
use core::{
33
alloc::{AllocErr, AllocInit, AllocRef, Layout, MemoryBlock, ReallocPlacement},
44
fmt,
5+
mem::MaybeUninit,
56
ptr::{self, NonNull},
67
};
78

@@ -14,8 +15,9 @@ use core::{
1415
///
1516
/// use alloc_compose::{Owns, Region};
1617
/// use core::alloc::{AllocInit, AllocRef, Layout};
18+
/// use core::mem::MaybeUninit;
1719
///
18-
/// let mut data = [0; 64];
20+
/// let mut data = [MaybeUninit::new(0); 64];
1921
/// let mut region = Region::new(&mut data);
2022
///
2123
/// let memory = region.alloc(Layout::new::<u32>(), AllocInit::Uninitialized)?;
@@ -27,22 +29,22 @@ use core::{
2729
/// ```rust
2830
/// # #![feature(allocator_api)]
2931
/// # use alloc_compose::{Owns, Region};
30-
/// # use core::alloc::{AllocInit, AllocRef, Layout};
31-
/// # let mut data = [0; 64];
32+
/// # use core::{alloc::{AllocInit, AllocRef, Layout}, mem::MaybeUninit};
33+
/// # let mut data = [MaybeUninit::new(0); 64];
3234
/// # let mut region = Region::new(&mut data);
3335
/// # let memory = region.alloc(Layout::new::<u32>(), AllocInit::Uninitialized)?;
3436
/// unsafe { region.dealloc(memory.ptr, Layout::new::<u32>()) };
3537
/// assert!(!region.owns(memory));
3638
/// # Ok::<(), core::alloc::AllocErr>(())
3739
/// ```
3840
pub struct Region<'a> {
39-
data: &'a mut [u8],
41+
data: &'a mut [MaybeUninit<u8>],
4042
ptr: usize,
4143
}
4244

4345
impl<'a> Region<'a> {
4446
#[inline]
45-
pub fn new(data: &'a mut [u8]) -> Self {
47+
pub fn new(data: &'a mut [MaybeUninit<u8>]) -> Self {
4648
let ptr = data.as_ptr() as usize + data.len();
4749
let region = Self { data, ptr };
4850
debug_assert!(region.is_empty());
@@ -252,23 +254,43 @@ mod tests {
252254

253255
#[test]
254256
fn alloc_zero() {
255-
let mut data = [1; 32];
257+
let mut data = [MaybeUninit::new(1); 32];
256258
let mut region = Region::new(&mut data);
257259

258260
assert_eq!(region.capacity(), 32);
259261
assert!(region.is_empty());
260262

261263
region
262-
.alloc(Layout::new::<[u8; 0]>(), AllocInit::Zeroed)
264+
.alloc(Layout::new::<[u8; 0]>(), AllocInit::Uninitialized)
263265
.expect("Could not allocated 0 bytes");
264266
assert!(region.is_empty());
265267

266-
assert_eq!(data, [1; 32]);
268+
unsafe {
269+
assert_eq!(MaybeUninit::slice_get_ref(&data), &[1; 32][..]);
270+
}
271+
}
272+
273+
#[test]
274+
fn alloc_zeroed() {
275+
let mut data = [MaybeUninit::new(1); 32];
276+
let mut region = Region::new(&mut data);
277+
278+
assert_eq!(region.capacity(), 32);
279+
assert!(region.is_empty());
280+
281+
region
282+
.alloc(Layout::new::<[u8; 32]>(), AllocInit::Zeroed)
283+
.expect("Could not allocated 32 bytes");
284+
assert!(!region.is_empty());
285+
286+
unsafe {
287+
assert_eq!(MaybeUninit::slice_get_ref(&data), &[0; 32][..]);
288+
}
267289
}
268290

269291
#[test]
270292
fn alloc_all() {
271-
let mut data = [1; 32];
293+
let mut data = [MaybeUninit::new(1); 32];
272294
let mut region = Region::new(&mut data);
273295

274296
assert_eq!(region.capacity(), 32);
@@ -299,7 +321,7 @@ mod tests {
299321

300322
#[test]
301323
fn alloc_small() {
302-
let mut data = [1; 32];
324+
let mut data = [MaybeUninit::new(1); 32];
303325
let mut region = Region::new(&mut data);
304326

305327
assert_eq!(region.capacity(), 32);
@@ -310,39 +332,30 @@ mod tests {
310332
.expect("Could not allocated 16 bytes");
311333
assert_eq!(region.capacity_left(), 16);
312334

313-
assert_eq!(&data[0..16], &[1; 16][..]);
314-
assert_eq!(&data[16..], &[0; 16][..]);
315-
}
316-
317-
#[test]
318-
fn alloc_full() {
319-
let mut data = [1; 32];
320-
let mut region = Region::new(&mut data);
321-
322-
region
323-
.alloc(Layout::new::<[u8; 32]>(), AllocInit::Zeroed)
324-
.expect("Could not allocated 32 bytes");
325-
assert_eq!(region.capacity_left(), 0);
326-
327-
assert_eq!(data, [0; 32]);
335+
unsafe {
336+
assert_eq!(MaybeUninit::slice_get_ref(&data[0..16]), &[1; 16][..]);
337+
assert_eq!(MaybeUninit::slice_get_ref(&data[16..]), &[0; 16][..]);
338+
}
328339
}
329340

330341
#[test]
331342
fn alloc_uninitialzed() {
332-
let mut data = [1; 32];
343+
let mut data = [MaybeUninit::new(1); 32];
333344
let mut region = Region::new(&mut data);
334345

335346
region
336347
.alloc(Layout::new::<[u8; 32]>(), AllocInit::Uninitialized)
337348
.expect("Could not allocated 32 bytes");
338349
assert_eq!(region.capacity_left(), 0);
339350

340-
assert_eq!(data, [1; 32]);
351+
unsafe {
352+
assert_eq!(MaybeUninit::slice_get_ref(&data), &[1; 32][..]);
353+
}
341354
}
342355

343356
#[test]
344357
fn alloc_fail() {
345-
let mut data = [1; 32];
358+
let mut data = [MaybeUninit::new(1); 32];
346359
let mut region = Region::new(&mut data);
347360

348361
region
@@ -380,7 +393,7 @@ mod tests {
380393

381394
#[test]
382395
fn dealloc() {
383-
let mut data = [1; 32];
396+
let mut data = [MaybeUninit::new(1); 32];
384397
let mut region = Region::new(&mut data);
385398
let layout = Layout::from_size_align(8, 1).expect("Invalid layout");
386399

@@ -417,7 +430,7 @@ mod tests {
417430

418431
#[test]
419432
fn realloc() {
420-
let mut data = [1; 32];
433+
let mut data = [MaybeUninit::new(1); 32];
421434
let mut region = Region::new(&mut data);
422435
let layout = Layout::from_size_align(8, 1).expect("Invalid layout");
423436

@@ -479,7 +492,7 @@ mod tests {
479492
)
480493
};
481494

482-
let mut data = [1; 32];
495+
let mut data = [MaybeUninit::new(1); 32];
483496
let mut region = Region::new(&mut data);
484497
test_output(&region);
485498

src/stats.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,11 @@ mod tests {
626626
use super::{AtomicCounter, Counter, FilteredAtomicCounter, FilteredCounter};
627627
use crate::{helper, CallbackRef, Owns, Proxy, Region};
628628
use std::alloc::{AllocInit, AllocRef, Layout, ReallocPlacement};
629+
use std::mem::MaybeUninit;
629630

630631
#[allow(clippy::too_many_lines)]
631632
fn run_suite(callbacks: &impl CallbackRef) {
632-
let mut region = [0; 32];
633+
let mut region = [MaybeUninit::new(0); 32];
633634
let mut alloc = Proxy {
634635
alloc: helper::tracker(Region::new(&mut region)),
635636
callbacks,

0 commit comments

Comments
 (0)