Skip to content

Commit f65b4b2

Browse files
committed
Draft: Add custom allocator support
Signed-off-by: Jonathan Schwender <[email protected]>
1 parent e1f9c3a commit f65b4b2

File tree

10 files changed

+727
-7
lines changed

10 files changed

+727
-7
lines changed

mozjs-sys/Cargo.toml

Lines changed: 4 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 = ["custom-malloc"] # Testing only - remove me
20+
custom-malloc = []
1921
debugmozjs = []
2022
profilemozjs = []
2123
jit = []
@@ -27,6 +29,8 @@ crown = []
2729
oom_with_hook = []
2830

2931
[dependencies]
32+
# remove-me
33+
mimalloc = { version = "0.1.48", optional = true}
3034
libc.workspace = true
3135
encoding_c = "0.9.8"
3236
encoding_c_mem = "0.2.6"

mozjs-sys/build.rs

Lines changed: 38 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,42 @@ 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_CUSTOM_MALLOC").is_some() {
222+
let mut flags = vec![];
223+
// let include_dir = env::var("SERVO_CUSTOM_MALLOC_INCLUDE_DIR").expect("Required variable not set with feature custom-malloc");
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+
cppflags.extend(flags.iter().map(|s| OsString::from(s)));
230+
cxxflags.extend(flags);
231+
// Todo: from embedder
232+
cppflags.push("-DSERVO_EMBEDDER_MALLOC_PREFIX=mi_");
233+
cppflags.push("-DSERVO_EMBEDDER_MEMORY");
234+
cxxflags.push("-DSERVO_EMBEDDER_MALLOC_PREFIX=mi_".to_string());
235+
cxxflags.push("-DSERVO_EMBEDDER_MEMORY".to_string());
236+
237+
}
238+
221239
if target.contains("apple") || target.contains("freebsd") || target.contains("ohos") {
222240
cxxflags.push(String::from("-stdlib=libc++"));
223241
}
224242

243+
cmd.env("CPPFLAGS", cppflags);
244+
225245
let base_cxxflags = env::var("CXXFLAGS").unwrap_or_default();
226246
let mut cxxflags = cxxflags.join(" ");
227247
cxxflags.push_str(&base_cxxflags);
228248
cmd.env("CXXFLAGS", cxxflags);
229249

230-
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
231250
let result = cmd
232251
.args(&["-R", "-f"])
233252
.arg(cargo_manifest_dir.join("makefile.cargo"))
@@ -292,6 +311,13 @@ fn build(build_dir: &Path, target: BuildTarget) {
292311

293312
build.flag(include_file_flag(build.get_compiler().is_like_msvc()));
294313
build.flag(&js_config_path(build_dir));
314+
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
315+
316+
let header_dir = cargo_manifest_dir.join("src/custom_alloc").to_str().expect("utf-8").to_string();
317+
build.include(&header_dir);
318+
// fixme
319+
build.flag("-DSERVO_EMBEDDER_MALLOC_PREFIX=mi_");
320+
println!("cargo:rerun-if-changed={}", header_dir);
295321

296322
for path in target.include_paths(build_dir) {
297323
build.include(path);
@@ -313,6 +339,10 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
313339
config &= !CodegenConfig::DESTRUCTORS;
314340
config &= !CodegenConfig::METHODS;
315341

342+
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
343+
344+
let header_dir = cargo_manifest_dir.join("src/custom_alloc").to_str().expect("utf-8").to_string();
345+
316346
let mut builder = bindgen::builder()
317347
.rust_target(minimum_rust_target())
318348
.header(target.path())
@@ -324,6 +354,7 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
324354
.size_t_is_usize(true)
325355
.enable_cxx_namespaces()
326356
.with_codegen_config(config)
357+
.clang_arg(format!("-I{}", header_dir))
327358
.clang_args(cc_flags(true));
328359

329360
if env::var("TARGET").unwrap().contains("wasi") {
@@ -432,6 +463,8 @@ fn link_static_lib_binaries(build_dir: &Path) {
432463
// needing to use the WASI-SDK's clang for linking, which is annoying.
433464
println!("cargo:rustc-link-lib=stdc++")
434465
}
466+
// TODO: link against lib from env
467+
// println!("cargo:rustc-link-lib=mimalloc");
435468

436469
if target.contains("wasi") {
437470
println!("cargo:rustc-link-lib=wasi-emulated-getpid");
@@ -463,6 +496,9 @@ fn should_build_from_source() -> bool {
463496
} else if env::var_os("CARGO_FEATURE_INTL").is_none() {
464497
println!("intl feature is disabled. Building from source directly.");
465498
true
499+
} else if env::var_os("CARGO_FEATURE_CUSTOM_ALLOC").is_some() {
500+
println!("custom-alloc feature is enabled. Building from source directly.");
501+
true
466502
} else if !env::var_os("CARGO_FEATURE_JIT").is_some() {
467503
println!("jit feature is NOT enabled. Building from source directly.");
468504
true

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: 39 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: 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/build/mozmemory_wrap.h

Lines changed: 8 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)