Skip to content

Commit 148e2cd

Browse files
committed
Document memory allocation APIs
Add some docs where they were missing, attempt to fix them where they were out of date.
1 parent f507dc3 commit 148e2cd

File tree

4 files changed

+245
-50
lines changed

4 files changed

+245
-50
lines changed

src/liballoc/alloc.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Memory allocation APIs
12+
1113
#![unstable(feature = "allocator_api",
1214
reason = "the precise API and guarantees it provides may be tweaked \
1315
slightly, especially to possibly take into account the \
@@ -37,27 +39,80 @@ extern "Rust" {
3739
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
3840
}
3941

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.
4047
#[derive(Copy, Clone, Default, Debug)]
4148
pub struct Global;
4249

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`].
4362
#[unstable(feature = "allocator_api", issue = "32838")]
4463
#[inline]
4564
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
4665
__rust_alloc(layout.size(), layout.align())
4766
}
4867

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`].
4980
#[unstable(feature = "allocator_api", issue = "32838")]
5081
#[inline]
5182
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
5283
__rust_dealloc(ptr, layout.size(), layout.align())
5384
}
5485

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`].
5598
#[unstable(feature = "allocator_api", issue = "32838")]
5699
#[inline]
57100
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
58101
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
59102
}
60103

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`].
61116
#[unstable(feature = "allocator_api", issue = "32838")]
62117
#[inline]
63118
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
@@ -123,6 +178,16 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
123178
}
124179
}
125180

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`].
126191
#[rustc_allocator_nounwind]
127192
pub fn oom(layout: Layout) -> ! {
128193
#[allow(improper_ctypes)]

src/liballoc_system/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const MIN_ALIGN: usize = 16;
4444
use core::alloc::{Alloc, GlobalAlloc, AllocErr, Layout};
4545
use core::ptr::NonNull;
4646

47+
/// The default memory allocator provided by the operating system.
4748
#[unstable(feature = "allocator_api", issue = "32838")]
4849
pub struct System;
4950

0 commit comments

Comments
 (0)