Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 7844905

Browse files
authored
Merge pull request #39 from ZackPierce/env_var_static_size
Add compile-time env var sizing of `static_array_backend`
2 parents c12a134 + df8ab39 commit 7844905

File tree

7 files changed

+65
-10
lines changed

7 files changed

+65
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/target/
22
**/*.rs.bk
33
Cargo.lock
4+
.idea

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
110110
runtime overhead. It is useful when debugging a use-after-free or `wee_alloc`
111111
itself.
112112

113-
- **static_array_backend**: Force the use of an OS-independent fixed-size (16 MB)
114-
backing implementation. Suitable for deploying to non-WASM/Unix/Windows
115-
`#![no_std]` environments, such as on embedded devices with esoteric or effectively
116-
absent operating systems.
113+
- **static_array_backend**: Force the use of an OS-independent backing
114+
implementation with a global maximum size fixed at compile time.
115+
Suitable for deploying to non-WASM/Unix/Windows `#![no_std]` environments,
116+
such as on embedded devices with esoteric or effectively absent operating
117+
systems. The size defaults to 32 MiB (33554432 bytes), and may be controlled
118+
at build-time by supplying an optional environment variable to cargo,
119+
`WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES`
117120

118121
### Implementation Notes and Constraints
119122

check.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cargo check --target i686-pc-windows-gnu
1111
cargo check --features size_classes
1212
cargo check --features size_classes --target wasm32-unknown-unknown
1313
cargo check --features size_classes --target i686-pc-windows-gnu
14+
cargo check --no-default-features --features "static_array_backend"
15+
cargo check --no-default-features --features "static_array_backend size_classes"
1416
cd -
1517

1618
cd ./test

wee_alloc/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extra_assertions = []
2626
size_classes = []
2727

2828
# Enable fixed-sized, OS-independent backing memory implementation
29-
static_array_backend = []
29+
static_array_backend = ["spin"]
3030

3131
# This is for internal use only.
3232
use_std_for_test_debugging = []
@@ -38,6 +38,7 @@ unreachable = "1.0.0"
3838

3939
[dependencies.spin]
4040
version = "0.4"
41+
optional = true
4142
default-features = false
4243
features = ["const_fn"]
4344

wee_alloc/build.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::env::{self, VarError};
2+
use std::fs::File;
3+
use std::io::Write;
4+
use std::path::Path;
5+
6+
const DEFAULT_STATIC_ARRAY_BACKEND_SIZE_BYTES: u32 = 1024 * 1024 * 32;
7+
const WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES: &'static str = "WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES";
8+
9+
fn main() {
10+
create_static_array_backend_size_bytes_file();
11+
export_rerun_rules();
12+
}
13+
14+
fn create_static_array_backend_size_bytes_file() {
15+
let out_dir = env::var("OUT_DIR").expect("OUT_DIR environment variable not provided");
16+
let dest_path = Path::new(&out_dir).join("wee_alloc_static_array_backend_size_bytes.txt");
17+
let size: u32 = match env::var(WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES) {
18+
Ok(s) => s.parse().expect("Could not interpret WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES as a 32 bit unsigned integer"),
19+
Err(ve) => match ve {
20+
VarError::NotPresent => { DEFAULT_STATIC_ARRAY_BACKEND_SIZE_BYTES },
21+
VarError::NotUnicode(_) => { panic!("Could not interpret WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES as a string representing a 32 bit unsigned integer")},
22+
},
23+
};
24+
let mut f = File::create(&dest_path)
25+
.expect("Could not create file to store wee_alloc static_array_backend size metadata.");
26+
write!(f, "{}", size)
27+
.expect("Could not write to wee_alloc static_array_backend size metadata file");
28+
f.flush()
29+
.expect("Could not flush write to wee_alloc static_array_backend size metadata file");
30+
}
31+
fn export_rerun_rules() {
32+
println!(
33+
"cargo:rerun-if-env-changed={}",
34+
WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES
35+
);
36+
for path in [
37+
"./Cargo.toml",
38+
"./build.rs",
39+
"./src/lib.rs",
40+
"./src/imp_static_array.rs",
41+
].iter()
42+
{
43+
println!("cargo:rerun-if-changed={}", path);
44+
}
45+
}

wee_alloc/src/imp_static_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::ptr::NonNull;
66
use memory_units::{Bytes, Pages};
77
use spin::Mutex;
88

9-
const SCRATCH_LEN_BYTES: usize = 1024 * 1024 * 32;
9+
const SCRATCH_LEN_BYTES: usize = include!(concat!(env!("OUT_DIR"), "/wee_alloc_static_array_backend_size_bytes.txt"));
1010
static mut SCRATCH_HEAP: [u8; SCRATCH_LEN_BYTES] = [0; SCRATCH_LEN_BYTES];
1111
static mut OFFSET: Mutex<usize> = Mutex::new(0);
1212

wee_alloc/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
110110
runtime overhead. It is useful when debugging a use-after-free or `wee_alloc`
111111
itself.
112112
113-
- **static_array_backend**: Force the use of an OS-independent fixed-size (16 MB)
114-
backing implementation. Suitable for deploying to non-WASM/Unix/Windows
115-
`#![no_std]` environments, such as on embedded devices with esoteric or effectively
116-
absent operating systems.
113+
- **static_array_backend**: Force the use of an OS-independent backing
114+
implementation with a global maximum size fixed at compile time.
115+
Suitable for deploying to non-WASM/Unix/Windows `#![no_std]` environments,
116+
such as on embedded devices with esoteric or effectively absent operating
117+
systems. The size defaults to 32 MiB (33554432 bytes), and may be controlled
118+
at build-time by supplying an optional environment variable to cargo,
119+
`WEE_ALLOC_STATIC_ARRAY_BACKEND_BYTES`
117120
118121
## Implementation Notes and Constraints
119122

0 commit comments

Comments
 (0)