Skip to main content

hashbrown/
alloc.rs

1#[cfg(test)]
2pub(crate) use self::inner::AllocError;
3pub(crate) use self::inner::{Allocator, Global, do_alloc};
4
5// Nightly-case.
6// Use unstable `allocator_api` feature.
7// This is compatible with `allocator-api2` which can be enabled or not.
8// This is used when building for `std`.
9#[cfg(feature = "nightly")]
10mod inner {
11    use core::alloc::Layout;
12    use core::ptr::NonNull;
13    #[cfg(test)]
14    pub(crate) use stdalloc::alloc::AllocError;
15    pub(crate) use stdalloc::alloc::{Allocator, Global};
16
17    pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
18        match alloc.allocate(layout) {
19            Ok(ptr) => Ok(ptr),
20            Err(_) => Err(()),
21        }
22    }
23}
24
25// Basic non-nightly case.
26// This uses `allocator-api2` enabled by default.
27// If any crate enables "nightly" in `allocator-api2`,
28// this will be equivalent to the nightly case,
29// since `allocator_api2::alloc::Allocator` would be re-export of
30// `core::alloc::Allocator`.
31#[cfg(all(not(feature = "nightly"), feature = "allocator-api2"))]
32mod inner {
33    #[cfg(test)]
34    pub(crate) use allocator_api2::alloc::AllocError;
35    pub(crate) use allocator_api2::alloc::{Allocator, Global};
36    use core::alloc::Layout;
37    use core::ptr::NonNull;
38
39    pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
40        match alloc.allocate(layout) {
41            Ok(ptr) => Ok(ptr),
42            Err(_) => Err(()),
43        }
44    }
45}
46
47// No-defaults case.
48// When building with default-features turned off and
49// neither `nightly` nor `allocator-api2` is enabled,
50// this will be used.
51// Making it impossible to use any custom allocator with collections defined
52// in this crate.
53// Any crate in build-tree can enable `allocator-api2`,
54// or `nightly` without disturbing users that don't want to use it.
55#[cfg(not(any(feature = "nightly", feature = "allocator-api2")))]
56mod inner {
57    use core::alloc::Layout;
58    use core::ptr::NonNull;
59    use stdalloc::alloc::{alloc, dealloc};
60
61    #[expect(clippy::missing_safety_doc)] // not exposed outside of this crate
62    pub unsafe trait Allocator {
63        fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()>;
64        unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
65    }
66
67    #[derive(Copy, Clone)]
68    pub struct Global;
69
70    unsafe impl Allocator for Global {
71        #[inline]
72        fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> {
73            match unsafe { NonNull::new(alloc(layout)) } {
74                Some(data) => {
75                    // SAFETY: this is NonNull::slice_from_raw_parts.
76                    Ok(unsafe {
77                        NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(
78                            data.as_ptr(),
79                            layout.size(),
80                        ))
81                    })
82                }
83                None => Err(()),
84            }
85        }
86        #[inline]
87        unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
88            unsafe {
89                dealloc(ptr.as_ptr(), layout);
90            }
91        }
92    }
93
94    impl Default for Global {
95        #[inline]
96        fn default() -> Self {
97            Global
98        }
99    }
100
101    pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
102        alloc.allocate(layout)
103    }
104}