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

Commit 4282ef5

Browse files
committed
Add compile-time env var sizing of static_array_backend
1 parent c12a134 commit 4282ef5

File tree

7 files changed

+68
-10
lines changed

7 files changed

+68
-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 MB (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: 5 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

@@ -48,3 +49,6 @@ version = "0.2"
4849
[target.'cfg(target_os = "windows")'.dependencies.winapi]
4950
version = "0.3"
5051
features = ["memoryapi", "synchapi", "winbase"]
52+
53+
[build-dependencies]
54+
globwalk = "0.3"

wee_alloc/build.rs

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