ialloc/allocator/adapt/
alloc_zst.rs1use crate::*;
2use crate::error::ExcessiveAlignmentRequestedError;
3use crate::meta::*;
4
5use core::alloc::Layout;
6use core::mem::MaybeUninit;
7use core::ptr::NonNull;
8
9
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct AllocZst<A>(pub A);
14
15impl<A> core::ops::Deref for AllocZst<A> { fn deref(&self) -> &Self::Target { &self.0 } type Target = A; }
16
17impl<A: Meta> AllocZst<A> {
18 fn fix_thin_size(size: usize) -> usize {
19 if A::ZST_SUPPORTED { size }
20 else { size.max(1) }
21 }
22
23 fn fix_layout(layout: Layout) -> Result<Layout, A::Error> {
24 if A::ZST_SUPPORTED || layout.size() != 0 {
25 Ok(layout)
26 } else {
27 const ALIGN_HALF_MAX : Alignment = Alignment::constant(Alignment::MAX.as_usize()/2);
28 Layout::from_size_align(1, layout.align()).map_err(|_| ExcessiveAlignmentRequestedError {
29 requested: Alignment::from(layout),
30 supported: ALIGN_HALF_MAX, }.into())
32 }
33 }
34}
35
36
37
38impl<A: Meta> Meta for AllocZst<A> {
41 type Error = A::Error;
42 const MAX_ALIGN : Alignment = A::MAX_ALIGN;
43 const MAX_SIZE : usize = A::MAX_SIZE;
44 const ZST_SUPPORTED : bool = true;
45}
46
47impl<A: Meta> ZstSupported for AllocZst<A> {}
48
49unsafe impl<A: Stateless> Stateless for AllocZst<A> {}
50
51
52
53#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: thin::Alloc> thin::Alloc for AllocZst<A> {
57 fn alloc_uninit(&self, size: usize) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
58 let size = Self::fix_thin_size(size);
59 self.0.alloc_uninit(size)
60 }
61
62 fn alloc_zeroed(&self, size: usize) -> Result<AllocNN0, Self::Error> {
63 let size = Self::fix_thin_size(size);
64 self.0.alloc_zeroed(size)
65 }
66}
67
68#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: thin::Free> thin::Free for AllocZst<A> {
70 unsafe fn free(&self, ptr: NonNull<MaybeUninit<u8>>) { unsafe { self.0.free(ptr) } }
71 unsafe fn free_nullable(&self, ptr: *mut MaybeUninit<u8>) { unsafe { self.0.free_nullable(ptr) } }
72}
73
74#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: thin::Realloc> thin::Realloc for AllocZst<A> {
76 const CAN_REALLOC_ZEROED : bool = A::CAN_REALLOC_ZEROED;
77
78 unsafe fn realloc_uninit(&self, ptr: NonNull<MaybeUninit<u8>>, new_size: usize) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
79 let new_size = Self::fix_thin_size(new_size);
80 unsafe { self.0.realloc_uninit(ptr, new_size) }
81 }
82
83 unsafe fn realloc_zeroed(&self, ptr: NonNull<MaybeUninit<u8>>, new_size: usize) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
84 let new_size = Self::fix_thin_size(new_size);
85 unsafe { self.0.realloc_zeroed(ptr, new_size) }
86 }
87}
88
89unsafe impl<A: thin::SizeOf> thin::SizeOf for AllocZst<A> {
90 unsafe fn size_of(&self, ptr: NonNull<MaybeUninit<u8>>) -> usize {
91 unsafe { self.0.size_of(ptr) }
92 }
93}
94
95unsafe impl<A: thin::SizeOfDebug> thin::SizeOfDebug for AllocZst<A> {
96 unsafe fn size_of_debug(&self, ptr: NonNull<MaybeUninit<u8>>) -> Option<usize> {
97 unsafe { self.0.size_of_debug(ptr) }
98 }
99}
100
101
102
103#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: fat::Alloc> fat::Alloc for AllocZst<A> {
107 fn alloc_uninit(&self, layout: Layout) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
108 let layout = Self::fix_layout(layout)?;
109 self.0.alloc_uninit(layout)
110 }
111
112 fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<u8>, Self::Error> {
113 let layout = Self::fix_layout(layout)?;
114 self.0.alloc_zeroed(layout)
115 }
116}
117
118#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: fat::Free> fat::Free for AllocZst<A> {
120 unsafe fn free(&self, ptr: NonNull<MaybeUninit<u8>>, layout: Layout) {
121 let layout = Self::fix_layout(layout).expect("bug: undefined behavior: invalid old_layout");
122 unsafe { self.0.free(ptr, layout) }
123 }
124}
125
126#[allow(clippy::undocumented_unsafe_blocks)] unsafe impl<A: fat::Realloc> fat::Realloc for AllocZst<A> {
128 unsafe fn realloc_uninit(&self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
129 let old_layout = Self::fix_layout(old_layout).expect("bug: undefined behavior: invalid old_layout");
130 let new_layout = Self::fix_layout(new_layout)?;
131 unsafe { self.0.realloc_uninit(ptr, old_layout, new_layout) }
132 }
133
134 unsafe fn realloc_zeroed(&self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> {
135 let old_layout = Self::fix_layout(old_layout).expect("bug: undefined behavior: invalid old_layout");
136 let new_layout = Self::fix_layout(new_layout)?;
137 unsafe { self.0.realloc_zeroed(ptr, old_layout, new_layout) }
138 }
139}
140
141#[no_implicit_prelude] mod cleanroom {
142 #[allow(unused_imports)] use super::{impls, fat, AllocZst};
143
144 impls! {
145 unsafe impl[A: ::core::alloc::GlobalAlloc ] core::alloc::GlobalAlloc for AllocZst<A> => core::ops::Deref;
146 }
147
148 #[cfg(allocator_api = "1.50")] impls! {
149 unsafe impl[A: fat::Realloc ] core::alloc::Allocator(unstable 1.50) for AllocZst<A> => ialloc::fat::Realloc;
150 }
151}