|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +//! Memory allocation APIs |
| 12 | +
|
11 | 13 | #![unstable(feature = "allocator_api",
|
12 | 14 | reason = "the precise API and guarantees it provides may be tweaked \
|
13 | 15 | slightly, especially to possibly take into account the \
|
@@ -37,27 +39,80 @@ extern "Rust" {
|
37 | 39 | fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
38 | 40 | }
|
39 | 41 |
|
| 42 | +/// The global memory allocator. |
| 43 | +/// |
| 44 | +/// This type implements the [`Alloc`] trait by forwarding calls |
| 45 | +/// to the allocator registered with the `#[global_allocator]` attribute |
| 46 | +/// if there is one, or the `std` crate’s default. |
40 | 47 | #[derive(Copy, Clone, Default, Debug)]
|
41 | 48 | pub struct Global;
|
42 | 49 |
|
| 50 | +/// Allocate memory with the global allocator. |
| 51 | +/// |
| 52 | +/// This function forwards calls to the [`GlobalAlloc::alloc`] method |
| 53 | +/// of the allocator registered with the `#[global_allocator]` attribute |
| 54 | +/// if there is one, or the `std` crate’s default. |
| 55 | +/// |
| 56 | +/// This function is expected to be deprecated in favor of the `alloc` method |
| 57 | +/// of the [`Global`] type when it and the [`Alloc`] trait become stable. |
| 58 | +/// |
| 59 | +/// # Safety |
| 60 | +/// |
| 61 | +/// See [`GlobalAlloc::alloc`]. |
43 | 62 | #[unstable(feature = "allocator_api", issue = "32838")]
|
44 | 63 | #[inline]
|
45 | 64 | pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
46 | 65 | __rust_alloc(layout.size(), layout.align())
|
47 | 66 | }
|
48 | 67 |
|
| 68 | +/// Deallocate memory with the global allocator. |
| 69 | +/// |
| 70 | +/// This function forwards calls to the [`GlobalAlloc::dealloc`] method |
| 71 | +/// of the allocator registered with the `#[global_allocator]` attribute |
| 72 | +/// if there is one, or the `std` crate’s default. |
| 73 | +/// |
| 74 | +/// This function is expected to be deprecated in favor of the `dealloc` method |
| 75 | +/// of the [`Global`] type when it and the [`Alloc`] trait become stable. |
| 76 | +/// |
| 77 | +/// # Safety |
| 78 | +/// |
| 79 | +/// See [`GlobalAlloc::dealloc`]. |
49 | 80 | #[unstable(feature = "allocator_api", issue = "32838")]
|
50 | 81 | #[inline]
|
51 | 82 | pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
|
52 | 83 | __rust_dealloc(ptr, layout.size(), layout.align())
|
53 | 84 | }
|
54 | 85 |
|
| 86 | +/// Reallocate memory with the global allocator. |
| 87 | +/// |
| 88 | +/// This function forwards calls to the [`GlobalAlloc::realloc`] method |
| 89 | +/// of the allocator registered with the `#[global_allocator]` attribute |
| 90 | +/// if there is one, or the `std` crate’s default. |
| 91 | +/// |
| 92 | +/// This function is expected to be deprecated in favor of the `realloc` method |
| 93 | +/// of the [`Global`] type when it and the [`Alloc`] trait become stable. |
| 94 | +/// |
| 95 | +/// # Safety |
| 96 | +/// |
| 97 | +/// See [`GlobalAlloc::realloc`]. |
55 | 98 | #[unstable(feature = "allocator_api", issue = "32838")]
|
56 | 99 | #[inline]
|
57 | 100 | pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
58 | 101 | __rust_realloc(ptr, layout.size(), layout.align(), new_size)
|
59 | 102 | }
|
60 | 103 |
|
| 104 | +/// Allocate zero-initialized memory with the global allocator. |
| 105 | +/// |
| 106 | +/// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method |
| 107 | +/// of the allocator registered with the `#[global_allocator]` attribute |
| 108 | +/// if there is one, or the `std` crate’s default. |
| 109 | +/// |
| 110 | +/// This function is expected to be deprecated in favor of the `alloc_zeroed` method |
| 111 | +/// of the [`Global`] type when it and the [`Alloc`] trait become stable. |
| 112 | +/// |
| 113 | +/// # Safety |
| 114 | +/// |
| 115 | +/// See [`GlobalAlloc::alloc_zeroed`]. |
61 | 116 | #[unstable(feature = "allocator_api", issue = "32838")]
|
62 | 117 | #[inline]
|
63 | 118 | pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
@@ -123,6 +178,16 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
123 | 178 | }
|
124 | 179 | }
|
125 | 180 |
|
| 181 | +/// Abort on memory allocation error or failure. |
| 182 | +/// |
| 183 | +/// Callers of memory allocation APIs wishing to abort computation |
| 184 | +/// in response to an allocation error are encouraged to call this function, |
| 185 | +/// rather than directly invoking `panic!` or similar. |
| 186 | +/// |
| 187 | +/// The default behavior of this function is to print a message to standard error |
| 188 | +/// and abort the process. |
| 189 | +/// It can be replaced with [`std::alloc::set_oom_hook`] |
| 190 | +/// and [`std::alloc::take_oom_hook`]. |
126 | 191 | #[rustc_allocator_nounwind]
|
127 | 192 | pub fn oom(layout: Layout) -> ! {
|
128 | 193 | #[allow(improper_ctypes)]
|
|
0 commit comments