|
18 | 18 | //! Defines memory-related functions, such as allocate/deallocate/reallocate memory
|
19 | 19 | //! regions, cache and allocation alignments.
|
20 | 20 |
|
21 |
| -use std::alloc::{handle_alloc_error, Layout}; |
| 21 | +use std::alloc::Layout; |
22 | 22 | use std::fmt::{Debug, Formatter};
|
23 | 23 | use std::panic::RefUnwindSafe;
|
24 |
| -use std::ptr::NonNull; |
25 | 24 | use std::sync::Arc;
|
26 | 25 |
|
27 | 26 | mod alignment;
|
28 | 27 |
|
29 | 28 | pub use alignment::ALIGNMENT;
|
30 | 29 |
|
31 |
| -/// Returns an aligned non null pointer similar to [`NonNull::dangling`] |
32 |
| -/// |
33 |
| -/// Note that the pointer value may potentially represent a valid pointer, which means |
34 |
| -/// this must not be used as a "not yet initialized" sentinel value. |
35 |
| -/// |
36 |
| -/// Types that lazily allocate must track initialization by some other means. |
37 |
| -#[inline] |
38 |
| -fn dangling_ptr() -> NonNull<u8> { |
39 |
| - // SAFETY: ALIGNMENT is a non-zero usize which is then casted |
40 |
| - // to a *mut T. Therefore, `ptr` is not null and the conditions for |
41 |
| - // calling new_unchecked() are respected. |
42 |
| - unsafe { NonNull::new_unchecked(ALIGNMENT as *mut u8) } |
43 |
| -} |
44 |
| - |
45 |
| -/// Allocates a cache-aligned memory region of `size` bytes with uninitialized values. |
46 |
| -/// This is more performant than using [allocate_aligned_zeroed] when all bytes will have |
47 |
| -/// an unknown or non-zero value and is semantically similar to `malloc`. |
48 |
| -#[deprecated(note = "Use Vec")] |
49 |
| -pub fn allocate_aligned(size: usize) -> NonNull<u8> { |
50 |
| - unsafe { |
51 |
| - if size == 0 { |
52 |
| - dangling_ptr() |
53 |
| - } else { |
54 |
| - let layout = Layout::from_size_align_unchecked(size, ALIGNMENT); |
55 |
| - let raw_ptr = std::alloc::alloc(layout); |
56 |
| - NonNull::new(raw_ptr).unwrap_or_else(|| handle_alloc_error(layout)) |
57 |
| - } |
58 |
| - } |
59 |
| -} |
60 |
| - |
61 |
| -/// Allocates a cache-aligned memory region of `size` bytes with `0` on all of them. |
62 |
| -/// This is more performant than using [allocate_aligned] and setting all bytes to zero |
63 |
| -/// and is semantically similar to `calloc`. |
64 |
| -#[deprecated(note = "Use Vec")] |
65 |
| -pub fn allocate_aligned_zeroed(size: usize) -> NonNull<u8> { |
66 |
| - unsafe { |
67 |
| - if size == 0 { |
68 |
| - dangling_ptr() |
69 |
| - } else { |
70 |
| - let layout = Layout::from_size_align_unchecked(size, ALIGNMENT); |
71 |
| - let raw_ptr = std::alloc::alloc_zeroed(layout); |
72 |
| - NonNull::new(raw_ptr).unwrap_or_else(|| handle_alloc_error(layout)) |
73 |
| - } |
74 |
| - } |
75 |
| -} |
76 |
| - |
77 |
| -/// # Safety |
78 |
| -/// |
79 |
| -/// This function is unsafe because undefined behavior can result if the caller does not ensure all |
80 |
| -/// of the following: |
81 |
| -/// |
82 |
| -/// * ptr must denote a block of memory currently allocated via this allocator, |
83 |
| -/// |
84 |
| -/// * size must be the same size that was used to allocate that block of memory, |
85 |
| -#[deprecated(note = "Use Vec")] |
86 |
| -pub unsafe fn free_aligned(ptr: NonNull<u8>, size: usize) { |
87 |
| - if size != 0 { |
88 |
| - std::alloc::dealloc( |
89 |
| - ptr.as_ptr() as *mut u8, |
90 |
| - Layout::from_size_align_unchecked(size, ALIGNMENT), |
91 |
| - ); |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 |
| -/// # Safety |
96 |
| -/// |
97 |
| -/// This function is unsafe because undefined behavior can result if the caller does not ensure all |
98 |
| -/// of the following: |
99 |
| -/// |
100 |
| -/// * ptr must be currently allocated via this allocator, |
101 |
| -/// |
102 |
| -/// * new_size must be greater than zero. |
103 |
| -/// |
104 |
| -/// * new_size, when rounded up to the nearest multiple of [ALIGNMENT], must not overflow (i.e., |
105 |
| -/// the rounded value must be less than usize::MAX). |
106 |
| -#[deprecated(note = "Use Vec")] |
107 |
| -#[allow(deprecated)] |
108 |
| -pub unsafe fn reallocate( |
109 |
| - ptr: NonNull<u8>, |
110 |
| - old_size: usize, |
111 |
| - new_size: usize, |
112 |
| -) -> NonNull<u8> { |
113 |
| - if old_size == 0 { |
114 |
| - return allocate_aligned(new_size); |
115 |
| - } |
116 |
| - |
117 |
| - if new_size == 0 { |
118 |
| - free_aligned(ptr, old_size); |
119 |
| - return dangling_ptr(); |
120 |
| - } |
121 |
| - |
122 |
| - let raw_ptr = std::alloc::realloc( |
123 |
| - ptr.as_ptr() as *mut u8, |
124 |
| - Layout::from_size_align_unchecked(old_size, ALIGNMENT), |
125 |
| - new_size, |
126 |
| - ); |
127 |
| - NonNull::new(raw_ptr).unwrap_or_else(|| { |
128 |
| - handle_alloc_error(Layout::from_size_align_unchecked(new_size, ALIGNMENT)) |
129 |
| - }) |
130 |
| -} |
131 |
| - |
132 | 30 | /// The owner of an allocation.
|
133 | 31 | /// The trait implementation is responsible for dropping the allocations once no more references exist.
|
134 | 32 | pub trait Allocation: RefUnwindSafe + Send + Sync {}
|
|
0 commit comments