Skip to content

Commit ad7c039

Browse files
committed
Draft: Add custom allocator support
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
1 parent b4c210b commit ad7c039

File tree

11 files changed

+719
-8
lines changed

11 files changed

+719
-8
lines changed

mozjs-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ name = "mozjs_sys"
1616
doctest = false
1717

1818
[features]
19+
default = ["mimalloc"] # Testingl only - remove me
20+
mimalloc = [ "dep:mimalloc"]
1921
debugmozjs = []
2022
profilemozjs = []
2123
jitspew = []
@@ -26,6 +28,7 @@ crown = []
2628
oom_with_hook = []
2729

2830
[dependencies]
31+
mimalloc = { version = "0.1.48", optional = true}
2932
libc.workspace = true
3033
encoding_c = "0.9.8"
3134
encoding_c_mem = "0.2.6"

mozjs-sys/build.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ fn main() {
138138

139139
fn build_spidermonkey(build_dir: &Path) {
140140
let target = env::var("TARGET").unwrap();
141+
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
141142
let make;
142143

143144
#[cfg(windows)]
@@ -210,24 +211,37 @@ fn build_spidermonkey(build_dir: &Path) {
210211
}
211212

212213
cppflags.push(get_cc_rs_env_os("CPPFLAGS").unwrap_or_default());
213-
cmd.env("CPPFLAGS", cppflags);
214214

215215
if let Some(makeflags) = env::var_os("CARGO_MAKEFLAGS") {
216216
cmd.env("MAKEFLAGS", makeflags);
217217
}
218218

219219
let mut cxxflags = vec![];
220220

221+
if env::var_os("CARGO_FEATURE_MIMALLOC").is_some() {
222+
let mut flags = vec![];
223+
// let include_dir = env::var("DEP_DFMALLOC_INCLUDE_DIR").expect("Required variable not set with feature dfmalloc");
224+
// flags.push(format!("-I{}", &include_dir.replace("\\", "/")));
225+
let header_dir = cargo_manifest_dir.join("src/custom_alloc").to_str().expect("utf-8").to_string();
226+
flags.push(format!("-I{}", header_dir));
227+
println!("cargo:rerun-if-changed={}", header_dir);
228+
229+
// flags.push("-DJS_USE_CUSTOM_ALLOCATOR".to_string());
230+
cppflags.extend(flags.iter().map(|s| OsString::from(s)));
231+
cxxflags.extend(flags);
232+
}
233+
221234
if target.contains("apple") || target.contains("freebsd") || target.contains("ohos") {
222235
cxxflags.push(String::from("-stdlib=libc++"));
223236
}
224237

238+
cmd.env("CPPFLAGS", cppflags);
239+
225240
let base_cxxflags = env::var("CXXFLAGS").unwrap_or_default();
226241
let mut cxxflags = cxxflags.join(" ");
227242
cxxflags.push_str(&base_cxxflags);
228243
cmd.env("CXXFLAGS", cxxflags);
229244

230-
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
231245
let result = cmd
232246
.args(&["-R", "-f"])
233247
.arg(cargo_manifest_dir.join("makefile.cargo"))
@@ -292,6 +306,11 @@ fn build(build_dir: &Path, target: BuildTarget) {
292306

293307
build.flag(include_file_flag(build.get_compiler().is_like_msvc()));
294308
build.flag(&js_config_path(build_dir));
309+
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
310+
311+
let header_dir = cargo_manifest_dir.join("src/custom_alloc").to_str().expect("utf-8").to_string();
312+
build.include(&header_dir);
313+
println!("cargo:rerun-if-changed={}", header_dir);
295314

296315
for path in target.include_paths(build_dir) {
297316
build.include(path);
@@ -313,6 +332,10 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
313332
config &= !CodegenConfig::DESTRUCTORS;
314333
config &= !CodegenConfig::METHODS;
315334

335+
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
336+
337+
let header_dir = cargo_manifest_dir.join("src/custom_alloc").to_str().expect("utf-8").to_string();
338+
316339
let mut builder = bindgen::builder()
317340
.rust_target(minimum_rust_target())
318341
.header(target.path())
@@ -324,6 +347,7 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
324347
.size_t_is_usize(true)
325348
.enable_cxx_namespaces()
326349
.with_codegen_config(config)
350+
.clang_arg(format!("-I{}", header_dir))
327351
.clang_args(cc_flags(true));
328352

329353
if env::var("TARGET").unwrap().contains("wasi") {
@@ -432,6 +456,8 @@ fn link_static_lib_binaries(build_dir: &Path) {
432456
// needing to use the WASI-SDK's clang for linking, which is annoying.
433457
println!("cargo:rustc-link-lib=stdc++")
434458
}
459+
// FIXME: guard me, or remove me or whatever.
460+
// println!("cargo:rustc-link-lib=mimalloc");
435461

436462
if target.contains("wasi") {
437463
println!("cargo:rustc-link-lib=wasi-emulated-getpid");
@@ -463,6 +489,9 @@ fn should_build_from_source() -> bool {
463489
} else if env::var_os("CARGO_FEATURE_INTL").is_none() {
464490
println!("intl feature is disabled. Building from source directly.");
465491
true
492+
} else if env::var_os("CARGO_FEATURE_MIMALLOC").is_some() {
493+
println!("mimalloc feature is enabled. Building from source directly.");
494+
true
466495
} else {
467496
false
468497
}

mozjs-sys/mozjs/js/public/Utility.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mozjs-sys/mozjs/memory/build/embedder_fallback.cpp

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mozjs-sys/mozjs/memory/build/moz.build

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mozjs-sys/mozjs/memory/build/mozmemory_wrap.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mozjs-sys/mozjs/memory/mozalloc/mozalloc.cpp

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mozjs-sys/mozjs/mfbt/StringBuffer.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)