Skip to content

Commit a1749c0

Browse files
authored
Merge pull request #1919 from ehuss/global_allocator
Update `global_allocator` to use the attribute template
2 parents bba5a87 + a551b25 commit a1749c0

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/runtime.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,43 @@ This section documents features that define some aspects of the Rust runtime.
66
r[runtime.global_allocator]
77
## The `global_allocator` attribute
88

9-
The *`global_allocator` attribute* is used on a [static item] implementing the
10-
[`GlobalAlloc`] trait to set the global allocator.
9+
r[runtime.global_allocator.intro]
10+
The *`global_allocator` [attribute][attributes]* selects a [memory allocator][std::alloc].
11+
12+
> [!EXAMPLE]
13+
> ```rust
14+
> use core::alloc::{GlobalAlloc, Layout};
15+
> use std::alloc::System;
16+
>
17+
> struct MyAllocator;
18+
>
19+
> unsafe impl GlobalAlloc for MyAllocator {
20+
> unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
21+
> unsafe { System.alloc(layout) }
22+
> }
23+
> unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24+
> unsafe { System.dealloc(ptr, layout) }
25+
> }
26+
> }
27+
>
28+
> #[global_allocator]
29+
> static GLOBAL: MyAllocator = MyAllocator;
30+
> ```
31+
32+
r[runtime.global_allocator.syntax]
33+
The `global_allocator` attribute uses the [MetaWord] syntax.
34+
35+
r[runtime.global_allocator.allowed-positions]
36+
The `global_allocator` attribute may only be applied to a [static item] whose type implements the [`GlobalAlloc`] trait.
37+
38+
r[runtime.global_allocator.duplicates]
39+
The `global_allocator` attribute may only be used once on an item.
40+
41+
r[runtime.global_allocator.single]
42+
The `global_allocator` attribute may only be used once in the crate graph.
43+
44+
r[runtime.global_allocator.stdlib]
45+
The `global_allocator` attribute is exported from the [standard library prelude][core::prelude::v1].
1146
1247
r[runtime.windows_subsystem]
1348
## The `windows_subsystem` attribute

0 commit comments

Comments
 (0)