/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.17.1/src/alloc.rs
Line | Count | Source |
1 | | #[cfg(test)] |
2 | | pub(crate) use self::inner::AllocError; |
3 | | pub(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")] |
10 | | mod inner { |
11 | | use core::ptr::NonNull; |
12 | | #[cfg(test)] |
13 | | pub(crate) use stdalloc::alloc::AllocError; |
14 | | use stdalloc::alloc::Layout; |
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"))] |
32 | | mod inner { |
33 | | #[cfg(test)] |
34 | | pub(crate) use allocator_api2::alloc::AllocError; |
35 | | pub(crate) use allocator_api2::alloc::{Allocator, Global}; |
36 | | use core::ptr::NonNull; |
37 | | use stdalloc::alloc::Layout; |
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")))] |
56 | | mod inner { |
57 | | use core::ptr::NonNull; |
58 | | use stdalloc::alloc::{Layout, alloc, dealloc}; |
59 | | |
60 | | #[expect(clippy::missing_safety_doc)] // not exposed outside of this crate |
61 | | pub unsafe trait Allocator { |
62 | | fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()>; |
63 | | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout); |
64 | | } |
65 | | |
66 | | #[derive(Copy, Clone)] |
67 | | pub struct Global; |
68 | | |
69 | | unsafe impl Allocator for Global { |
70 | | #[inline] |
71 | 388k | fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> { |
72 | 388k | match unsafe { NonNull::new(alloc(layout)) } { |
73 | 388k | Some(data) => { |
74 | | // SAFETY: this is NonNull::slice_from_raw_parts. |
75 | 388k | Ok(unsafe { |
76 | 388k | NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut( |
77 | 388k | data.as_ptr(), |
78 | 388k | layout.size(), |
79 | 388k | )) |
80 | 388k | }) |
81 | | } |
82 | 0 | None => Err(()), |
83 | | } |
84 | 388k | } <hashbrown::alloc::inner::Global as hashbrown::alloc::inner::Allocator>::allocate Line | Count | Source | 71 | 388k | fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> { | 72 | 388k | match unsafe { NonNull::new(alloc(layout)) } { | 73 | 388k | Some(data) => { | 74 | | // SAFETY: this is NonNull::slice_from_raw_parts. | 75 | 388k | Ok(unsafe { | 76 | 388k | NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut( | 77 | 388k | data.as_ptr(), | 78 | 388k | layout.size(), | 79 | 388k | )) | 80 | 388k | }) | 81 | | } | 82 | 0 | None => Err(()), | 83 | | } | 84 | 388k | } |
Unexecuted instantiation: <hashbrown::alloc::inner::Global as hashbrown::alloc::inner::Allocator>::allocate |
85 | | #[inline] |
86 | 388k | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { |
87 | 388k | unsafe { |
88 | 388k | dealloc(ptr.as_ptr(), layout); |
89 | 388k | } |
90 | 388k | } <hashbrown::alloc::inner::Global as hashbrown::alloc::inner::Allocator>::deallocate Line | Count | Source | 86 | 388k | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { | 87 | 388k | unsafe { | 88 | 388k | dealloc(ptr.as_ptr(), layout); | 89 | 388k | } | 90 | 388k | } |
Unexecuted instantiation: <hashbrown::alloc::inner::Global as hashbrown::alloc::inner::Allocator>::deallocate |
91 | | } |
92 | | |
93 | | impl Default for Global { |
94 | | #[inline] |
95 | 0 | fn default() -> Self { |
96 | 0 | Global |
97 | 0 | } |
98 | | } |
99 | | |
100 | 388k | pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> { |
101 | 388k | alloc.allocate(layout) |
102 | 388k | } hashbrown::alloc::inner::do_alloc::<hashbrown::alloc::inner::Global> Line | Count | Source | 100 | 388k | pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> { | 101 | 388k | alloc.allocate(layout) | 102 | 388k | } |
Unexecuted instantiation: hashbrown::alloc::inner::do_alloc::<_> |
103 | | } |