Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Revise notes for example std_global_allocator.rs #36

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions stable_examples/examples/std_global_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use talc::*;

// note: miri thinks this violates stacked borrows upon program termination.
// This only occurs if #[global_allocator] is used.
// Use the allocator API if you can't have that. Perhaps checck out `std_allocator_api.rs`?
// note:
// - Miri thinks this violates stacked borrows upon program termination.
// - This only occurs with `#[global_allocator]`.
// - Consider using the allocator API if you can't have that (see: `examples/stable_allocator_api.rs`)
// - `spin::Mutex<()>`
// The `spin` crate provides a mutex that is a sensible choice to use.
// - `ClaimOnOom`
// An OOM handler with support for claiming memory on-demand is required, as allocations may
// occur prior to the execution of `main()`.

static mut START_ARENA: [u8; 10000] = [0; 10000];

// The mutex provided by the `spin` crate is used here as it's a sensible choice

// Allocations may occur prior to the execution of `main()`, thus support for
// claiming memory on-demand is required, such as the ClaimOnOom OOM handler.

#[global_allocator]
static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> = Talc::new(unsafe {
ClaimOnOom::new(Span::from_const_array(std::ptr::addr_of!(START_ARENA)))
}).lock();
ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA)))
}).lock();

fn main() {
let mut vec = Vec::with_capacity(100);
Expand Down