static_alloc/lib.rs
1//! General purpose global allocator(s) with static, inline storage.
2//!
3//! Provides an allocator for extremely resource constrained environments where the only memory
4//! guaranteed is your program's image in memory as provided by the loader. Possible use cases are
5//! OS-less development, embedded, bootloaders (even stage0/1).
6//!
7//! The allocator does not support meaningful deallocation but the whole allocator itself can be
8//! reset by mutable reference. This is useful for a local, single thread allocator. It's
9//! recommended to use the global instance to split resources _once_ at startup and then utilize
10//! multiple local allocators for actual working memory. See [`doc`] for some use case studies with
11//! examples.
12//!
13//! ## Usage – Global allocator
14//!
15//! ```rust
16//! use static_alloc::Bump;
17//!
18//! #[global_allocator]
19//! static A: Bump<[u8; 1 << 16]> = Bump::uninit();
20//!
21//! fn main() {
22//! let v = vec![0xdeadbeef_u32; 128];
23//! println!("{:x?}", v);
24//!
25//! let buffer: &'static mut [u32; 128] = A.leak([0; 128])
26//! .unwrap_or_else(|_| panic!("Runtime allocated before main"));
27//! }
28//! ```
29//!
30//! You can find more detailed examples in the `doc` module:
31//!
32//! * [The readme](doc/readme/index.html)
33//! * [Usage as a global allocator](doc/global_allocator/index.html)
34//! * [Usage as a static allocator](doc/static_allocator/index.html)
35//! * [Safe pinning of static tasks](doc/pinned/index.html)
36//!
37//! ## Why the name?
38//!
39//! This crates makes it safe to define a *static* object and to then use its memory to *allocate*
40//! dynamic values without accidentally exposing or using uninitialized memory. This allows
41//! obtaining `&'static mut T` instances which is handy if a struct requires a mutable reference
42//! but it is also required that this struct has `'static` lifetime bounds itself.
43// Copyright 2019,2022 Andreas Molzer
44#![no_std]
45#![deny(missing_docs)]
46
47#[cfg(feature = "alloc")]
48extern crate alloc;
49
50pub mod bump;
51pub use bump::Bump;
52pub mod leaked;
53
54/// An unsynchronized allocator.
55pub mod unsync;
56
57// Can't use the macro-call itself within the `doc` attribute. So force it to eval it as part of
58// the macro invocation.
59//
60// The inspiration for the macro and implementation is from
61// <https://github.com/GuillaumeGomez/doc-comment>
62//
63// MIT License
64//
65// Copyright (c) 2018 Guillaume Gomez
66#[cfg(doc)]
67macro_rules! insert_as_doc {
68 { $content:expr, $name:ident } => {
69 #[doc = $content] pub mod $name { }
70 }
71}
72
73/// A module containing extended documentation and examples.
74#[cfg(doc)]
75pub mod doc {
76 // Provides the README.md as doc, to ensure the example works!
77 insert_as_doc!(include_str!("../Readme.md"), e0_readme);
78 insert_as_doc!(include_str!("doc/global_allocator.md"), e1_global_allocator);
79 insert_as_doc!(include_str!("doc/static_allocator.md"), e2_static_allocator);
80 insert_as_doc!(include_str!("doc/pinned.md"), e3_pinned);
81}