1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! [![github]](https://github.com/debuti/prealloc) [![crates-io]](https://crates.io/crates/prealloc) [![docs-rs]](https://docs.rs/prealloc)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! <br>
//!
//! Some design patterns would request memory only at the initialization stage. The usage of these patterns,
//! specially on embedded and real-time systems where the heap usage is forbidden, is hard to implement on
//! constrained languages like Rust.
//!
//! This crate provides a macro to generate all the boilerplate needed to provide heap-like storage
//! while using static memory as the storage backend. The exposed interface allows you to manage objects
//! in this preallocated static memory, mimicking the behavior of dynamic memory allocation
//! without relying on a heap. You can safely dispatch and use items from this preallocated pool, ensuring
//! that you have tight control over memory usage with zero runtime allocation overhead.
//!
//! <br>
//!
//! # Defining the configuration
//!
//! Create a JSON configuration file (ex. `config.json`) that defines the items, their types, and counts:
//! ```json
//! [
//! {
//! "name": "ExampleUsage",
//! "type": "MyEnum",
//! "size": 5
//! }
//! ]
//! ```
//!
//! # Accesing the preallocated memory
//!
//! ```ignore
//! use prealloc::prealloc_from_config;
//!
//! prealloc_from_config!("path/to/config.json");
//!
//! #[derive(Debug)]
//! #[allow(dead_code)]
//! pub enum MyEnum {
//! L(u32),
//! R,
//! }
//!
//! impl Drop for MyEnum {
//! fn drop(&mut self) {
//! println!("Dropping MyEnum located at {self:p}");
//! }
//! }
//!
//! fn main() {
//! for idx in 0..=5 {
//! // The dispatch_static macro retrieves one preallocated element and initializes it.
//! if let Some(item) = dispatch_static!(ExampleUsage, MyEnum::L(33)) {
//! println!("Retrieved {idx}: {item:p}");
//! } else {
//! println!("ExampleUsage depleted");
//! }
//! }
//! }
//! ```
pub use prealloc_from_config;
pub use *;
pub use *;