Skip to content

Commit 80f19f3

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

File tree

15 files changed

+192
-48
lines changed

15 files changed

+192
-48
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["mozjs-sys", "mozjs"]
2+
members = ["mozjs-sys", "mozjs", "examples/embedder_allocator"]
33
resolver = "2"
44

55
[workspace.package]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "embedder_allocator"
3+
version = "0.1.0"
4+
repository.workspace = true
5+
license.workspace = true
6+
edition.workspace = true
7+
description = "Example usage of mozjs, with the embedder (this crate) providing a custom allocator for mozjs"
8+
publish = false
9+
10+
[dependencies]
11+
mozjs = { path = "../../mozjs", features = ["custom-alloc"] }
12+
mimalloc = "0.1.48"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Example: Using mozjs with an embedder-provided allocator
2+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
# This file
8+
9+
import os
10+
import pathlib
11+
import subprocess
12+
import sys
13+
from typing import Mapping
14+
15+
def create_env() -> Mapping[str, str]:
16+
env = os.environ.copy()
17+
mimalloc_include_dir = pathlib.Path(__file__).parent.joinpath('mimalloc/include')
18+
assert mimalloc_include_dir.is_dir(), "Could not find mimalloc include directory"
19+
env['SERVO_CUSTOM_ALLOC_INCLUDE_DIR'] = mimalloc_include_dir.as_posix()
20+
return env
21+
22+
23+
def main():
24+
completed_process = subprocess.run(sys.argv[1:], env=create_env())
25+
sys.exit(completed_process.returncode)
26+
27+
28+
if __name__ == '__main__':
29+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
// todo: Should we do this here or in the mozjs-sys build-script?
3+
println!("cargo:rustc-link-lib=mimalloc");
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
/// Defines the prefix for all malloc functions, i.e.
4+
/// mi_malloc, mi_calloc, mi_realloc, mi_free etc.
5+
#define SERVO_EMBEDDER_MALLOC_PREFIX mi_
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <mimalloc.h>
4+
#include "servo_embedder_malloc_prefix.h"
5+
6+
#define SERVO_CONCAT(x, y) x ## y
7+
#define SERVO_CONCAT2(x, y) SERVO_CONCAT(x, y)
8+
9+
#define mozmem_malloc_impl(fn) SERVO_CONCAT2(SERVO_EMBEDDER_MALLOC_PREFIX, fn)
10+
#define mozmem_dup_impl(fn) SERVO_CONCAT2(SERVO_EMBEDDER_MALLOC_PREFIX, fn)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use std::ptr;
6+
use std::sync::mpsc::channel;
7+
use std::thread;
8+
9+
use mozjs::jsapi::GCContext;
10+
use mozjs::jsapi::JSCLASS_FOREGROUND_FINALIZE;
11+
use mozjs::jsapi::{JSClass, JSClassOps, JSObject, OnNewGlobalHookOption};
12+
use mozjs::realm::AutoRealm;
13+
use mozjs::rooted;
14+
use mozjs::rust::wrappers2::{JS_NewGlobalObject, JS_NewObject};
15+
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};
16+
17+
fn main() {
18+
println!("Hello, world!");
19+
let engine = JSEngine::init().expect("Could not init JSEngine");
20+
println!("JSEngine initialized");
21+
let mut runtime = Runtime::new(engine.handle());
22+
println!("Runtime initialized");
23+
let context = runtime.cx();
24+
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
25+
let c_option = RealmOptions::default();
26+
27+
unsafe {
28+
rooted!(&in(context) let global = JS_NewGlobalObject(
29+
context,
30+
&SIMPLE_GLOBAL_CLASS,
31+
ptr::null_mut(),
32+
h_option,
33+
&*c_option,
34+
));
35+
let mut realm = AutoRealm::new_from_handle(context, global.handle());
36+
let context = &mut realm;
37+
rooted!(&in(context) let _object = JS_NewObject(context, &CLASS as *const _));
38+
}
39+
40+
let parent = runtime.prepare_for_new_child();
41+
let (sender, receiver) = channel();
42+
thread::spawn(move || {
43+
let runtime = unsafe { Runtime::create_with_parent(parent) };
44+
assert!(Runtime::get().is_some());
45+
drop(runtime);
46+
let _ = sender.send(());
47+
});
48+
let _ = receiver.recv();
49+
println!("Example ran without issues...");
50+
}
51+
52+
unsafe extern "C" fn finalize(_fop: *mut GCContext, _object: *mut JSObject) {
53+
assert!(Runtime::get().is_some());
54+
}
55+
56+
static CLASS_OPS: JSClassOps = JSClassOps {
57+
addProperty: None,
58+
delProperty: None,
59+
enumerate: None,
60+
newEnumerate: None,
61+
resolve: None,
62+
mayResolve: None,
63+
finalize: Some(finalize),
64+
call: None,
65+
construct: None,
66+
trace: None,
67+
};
68+
69+
static CLASS: JSClass = JSClass {
70+
name: c"EventTargetPrototype".as_ptr(),
71+
flags: JSCLASS_FOREGROUND_FINALIZE,
72+
cOps: &CLASS_OPS as *const JSClassOps,
73+
spec: ptr::null(),
74+
ext: ptr::null(),
75+
oOps: ptr::null(),
76+
};

mozjs-sys/Cargo.toml

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

1818
[features]
19-
default = ["custom-malloc"] # Testing only - remove me
20-
custom-malloc = []
19+
default = []
20+
custom-alloc = []
2121
debugmozjs = []
2222
profilemozjs = []
2323
jit = []
@@ -29,8 +29,6 @@ crown = []
2929
oom_with_hook = []
3030

3131
[dependencies]
32-
# remove-me
33-
mimalloc = { version = "0.1.48", optional = true}
3432
libc.workspace = true
3533
encoding_c = "0.9.8"
3634
encoding_c_mem = "0.2.6"

0 commit comments

Comments
 (0)