From 8b183dc85605a5a71393c8cf75fed738b63848c7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 16:58:51 -0700 Subject: [PATCH 1/2] Update global_allocator to use the attribute template This also adds a little more detail. This is still a little light, and to some degree that is intentional because there is significant overlap with the standard library, and the standard library docs do contain a little more detail. --- src/runtime.md | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/runtime.md b/src/runtime.md index 87b348cd0..f48e6da48 100644 --- a/src/runtime.md +++ b/src/runtime.md @@ -6,8 +6,43 @@ This section documents features that define some aspects of the Rust runtime. r[runtime.global_allocator] ## The `global_allocator` attribute -The *`global_allocator` attribute* is used on a [static item] implementing the -[`GlobalAlloc`] trait to set the global allocator. +r[runtime.global_allocator.intro] +The *`global_allocator` [attribute][attributes]* is used to define a [memory allocator][std::alloc]. + +> [!EXAMPLE] +> ```rust +> use std::alloc::{GlobalAlloc, System, Layout}; +> +> struct MyAllocator; +> +> unsafe impl GlobalAlloc for MyAllocator { +> unsafe fn alloc(&self, layout: Layout) -> *mut u8 { +> unsafe { System.alloc(layout) } +> } +> +> unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { +> unsafe { System.dealloc(ptr, layout) } +> } +> } +> +> #[global_allocator] +> static GLOBAL: MyAllocator = MyAllocator; +> ``` + +r[runtime.global_allocator.syntax] +The `global_allocator` attribute uses the [MetaWord] syntax and thus does not take any inputs. + +r[runtime.global_allocator.allowed-positions] +The `global_allocator` attribute may only be applied to a [static item] that implements the [`GlobalAlloc`] trait. + +r[runtime.global_allocator.duplicates] +The `global_allocator` attribute may only be specified once on an item. + +r[runtime.global_allocator.single] +At most one `global_allocator` may be specified in the crate graph. + +r[runtime.global_allocator.stdlib] +The `global_allocator` attribute is exported in the [standard library prelude][core::prelude::v1]. r[runtime.windows_subsystem] ## The `windows_subsystem` attribute From a551b2546aee6589f8f8838b9047b55ec0ad953c Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 10 Aug 2025 14:36:30 +0000 Subject: [PATCH 2/2] Revise `global_allocator` text --- src/runtime.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/runtime.md b/src/runtime.md index f48e6da48..566985c53 100644 --- a/src/runtime.md +++ b/src/runtime.md @@ -7,11 +7,12 @@ r[runtime.global_allocator] ## The `global_allocator` attribute r[runtime.global_allocator.intro] -The *`global_allocator` [attribute][attributes]* is used to define a [memory allocator][std::alloc]. +The *`global_allocator` [attribute][attributes]* selects a [memory allocator][std::alloc]. > [!EXAMPLE] > ```rust -> use std::alloc::{GlobalAlloc, System, Layout}; +> use core::alloc::{GlobalAlloc, Layout}; +> use std::alloc::System; > > struct MyAllocator; > @@ -19,7 +20,6 @@ The *`global_allocator` [attribute][attributes]* is used to define a [memory all > unsafe fn alloc(&self, layout: Layout) -> *mut u8 { > unsafe { System.alloc(layout) } > } -> > unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { > unsafe { System.dealloc(ptr, layout) } > } @@ -30,19 +30,19 @@ The *`global_allocator` [attribute][attributes]* is used to define a [memory all > ``` r[runtime.global_allocator.syntax] -The `global_allocator` attribute uses the [MetaWord] syntax and thus does not take any inputs. +The `global_allocator` attribute uses the [MetaWord] syntax. r[runtime.global_allocator.allowed-positions] -The `global_allocator` attribute may only be applied to a [static item] that implements the [`GlobalAlloc`] trait. +The `global_allocator` attribute may only be applied to a [static item] whose type implements the [`GlobalAlloc`] trait. r[runtime.global_allocator.duplicates] -The `global_allocator` attribute may only be specified once on an item. +The `global_allocator` attribute may only be used once on an item. r[runtime.global_allocator.single] -At most one `global_allocator` may be specified in the crate graph. +The `global_allocator` attribute may only be used once in the crate graph. r[runtime.global_allocator.stdlib] -The `global_allocator` attribute is exported in the [standard library prelude][core::prelude::v1]. +The `global_allocator` attribute is exported from the [standard library prelude][core::prelude::v1]. r[runtime.windows_subsystem] ## The `windows_subsystem` attribute