Skip to content

Commit 65ff247

Browse files
committed
rust bindings
1 parent 0064c01 commit 65ff247

File tree

11 files changed

+444
-6
lines changed

11 files changed

+444
-6
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114
path: "~/.cache/bazel"
115115
key: bazel
116116
- run: bazel build -c opt //:snmalloc
117-
- run: bazel build -c opt //:snmalloc-rs
117+
- run: bazel build -c opt //:snmalloc-rust-support
118118

119119
# If this looks remarkably familiar, that's because it is. Sigh.
120120
macos:
@@ -166,7 +166,7 @@ jobs:
166166
path: "~/.cache/bazel"
167167
key: bazel
168168
- run: bazel build -c opt //:snmalloc
169-
- run: bazel build -c opt //:snmalloc-rs
169+
- run: bazel build -c opt //:snmalloc-rust-support
170170

171171

172172
# GitHub doesn't natively support *BSD, but we can run them in VMs on Mac /

BUILD.bazel

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
12
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
3+
load("@rules_cc//cc:defs.bzl", "cc_import")
4+
load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain")
25

36
filegroup(
47
name = "srcs",
@@ -38,7 +41,6 @@ CMAKE_FLAGS = {
3841
"SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE": "ON",
3942
"SNMALLOC_USE_SELF_VENDORED_STL": "OFF",
4043
"SNMALLOC_IPO": "ON",
41-
"USE_SNMALLOC_STATS": "ON",
4244
} | select({
4345
":release_with_debug": {"CMAKE_BUILD_TYPE": "RelWithDebInfo"},
4446
":release": {"CMAKE_BUILD_TYPE": "Release"},
@@ -65,10 +67,11 @@ cmake(
6567
],
6668
postfix_script = "ninja",
6769
visibility = ["//visibility:public"],
70+
alwayslink = True,
6871
)
6972

7073
cmake(
71-
name = "snmalloc-rs",
74+
name = "snmalloc-rust-support",
7275
cache_entries = CMAKE_FLAGS | {
7376
"SNMALLOC_RUST_SUPPORT": "ON",
7477
},
@@ -87,5 +90,39 @@ cmake(
8790
"libsnmalloc-new-override.a",
8891
],
8992
postfix_script = "ninja",
93+
visibility = ["//src/snmalloc_rs:__pkg__"],
94+
alwayslink = True,
95+
)
96+
97+
alias(
98+
name = "snmalloc_rs",
99+
actual = "//src/snmalloc_rs",
90100
visibility = ["//visibility:public"],
91101
)
102+
103+
native_binary(
104+
name = "clang",
105+
src = "@llvm_toolchain_llvm//:bin/clang",
106+
visibility = ["//snmalloc_rs:__subpackages__"],
107+
)
108+
109+
cc_import(
110+
name = "libclang",
111+
shared_library = "@llvm_toolchain_llvm//:libclang",
112+
visibility = ["//snmalloc_rs:__subpackages__"],
113+
)
114+
115+
rust_bindgen_toolchain(
116+
name = "rust_bindgen_toolchain",
117+
bindgen = "@rules_rust_bindgen//3rdparty:bindgen",
118+
clang = ":clang",
119+
libclang = ":libclang",
120+
visibility = ["//snmalloc_rs:__subpackages__"],
121+
)
122+
123+
toolchain(
124+
name = "default_bindgen_toolchain",
125+
toolchain = ":rust_bindgen_toolchain",
126+
toolchain_type = "@rules_rust_bindgen//:toolchain_type",
127+
visibility = ["//snmalloc_rs:__subpackages__"],
128+
)

MODULE.bazel

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
module(name = "snmalloc")
22

3+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
34
bazel_dep(name = "rules_cc", version = "0.1.1")
45
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
56
bazel_dep(name = "fuzztest", version = "20250214.0")
67
bazel_dep(name = "googletest", version = "1.16.0")
8+
bazel_dep(name = "toolchains_llvm", version = "1.4.0")
9+
bazel_dep(name = "rules_rust", version = "0.61.0")
10+
bazel_dep(name = "rules_rust_bindgen", version = "0.61.0")
11+
12+
# Configure and register the toolchain.
13+
# https://github.com/bazel-contrib/toolchains_llvm/blob/master/toolchain/internal/llvm_distributions.bzl
14+
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)
15+
16+
# LLVM toolchain.
17+
llvm.toolchain(
18+
name = "llvm_toolchain",
19+
cxx_standard = {"": "c++20"},
20+
llvm_version = "17.0.6",
21+
)
22+
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")
23+
24+
#register_toolchains("@llvm_toolchain//:all")
25+
26+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust", dev_dependency = True)
27+
rust.toolchain(
28+
edition = "2021",
29+
versions = [
30+
"1.86.0",
31+
],
32+
)
33+
use_repo(rust, "rust_toolchains")
34+
35+
register_toolchains(
36+
"@rust_toolchains//:all",
37+
dev_dependency = True,
38+
)
39+
40+
register_toolchains(
41+
"//:default_bindgen_toolchain",
42+
dev_dependency = True,
43+
)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
snmalloc is a high-performance allocator.
44
snmalloc can be used directly in a project as a header-only C++ library,
55
it can be `LD_PRELOAD`ed on Elf platforms (e.g. Linux, BSD),
6-
and there is a [crate](https://crates.io/crates/snmalloc-rs) to use it from Rust.
6+
and there is a [crate](https://crates.io/crates/snmalloc_rs) to use it from Rust.
77

88
Its key design features are:
99

fuzzing/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cc_test(
99
copts = [
1010
"-fsanitize=address",
1111
] + select({
12-
"@bazel_tools//tools/cpp:clang-cl": ["-fexperimental-library"], # needed for std::execution::unseq,
12+
"@bazel_tools//src/conditions:darwin": ["-fexperimental-library"],
1313
"//conditions:default": ["-mcx16"],
1414
}),
1515
defines = [

src/snmalloc_rs/BUILD.bazel

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
3+
load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_library")
4+
load("@rules_cc//cc:defs.bzl", "cc_library")
5+
6+
cc_library(
7+
name = "wrapper",
8+
hdrs = ["wrapper.h"],
9+
defines = ["SNMALLOC_USE_WAIT_ON_ADDRESS"],
10+
linkstatic = True,
11+
visibility = ["//visibility:private"],
12+
deps = ["//:snmalloc-rust-support"],
13+
)
14+
15+
rust_bindgen_library(
16+
name = "snmalloc_sys",
17+
bindgen_flags = [
18+
"--allowlist-function=aligned_alloc",
19+
"--allowlist-function=calloc",
20+
"--allowlist-function=get_malloc_info_v1", # getting mangles
21+
"--allowlist-function=malloc",
22+
"--allowlist-function=malloc_usable_size", # getting mangles
23+
"--allowlist-function=realloc",
24+
"--allowlist-function=free",
25+
# comment out the above and uncomment the two lines below to generate bindings for everything
26+
# "--opaque-type=std::.*",
27+
# "--blocklist-item=std::value",
28+
"--use-core", # don't compile with std, allows baremetal envs
29+
],
30+
cc_lib = ":wrapper",
31+
clang_flags = [
32+
# "-v", # enable for debugging
33+
"-x",
34+
"c++",
35+
"-std=c++20",
36+
"-I",
37+
],
38+
header = ":wrapper.h",
39+
)
40+
41+
rust_library(
42+
name = "snmalloc_rs",
43+
srcs = ["lib.rs"],
44+
visibility = ["//visibility:public"],
45+
deps = [":snmalloc_sys"],
46+
)
47+
48+
# bazel test //src/snmalloc_rs/... --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
49+
rust_test(
50+
name = "test",
51+
size = "small",
52+
crate = ":snmalloc_rs",
53+
)
54+
55+
# bazel run //snmalloc_rs:main --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
56+
rust_binary(
57+
name = "main",
58+
srcs = ["main.rs"],
59+
rustc_flags = [
60+
"-Ccodegen-units=1",
61+
"-Copt-level=3",
62+
"-Cstrip=symbols",
63+
],
64+
deps = [":snmalloc_rs"],
65+
)

src/snmalloc_rs/Cargo.lock

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

src/snmalloc_rs/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "snmalloc_rs"
3+
version = "0.0.0"
4+
edition = "2024"
5+
publish = false
6+
homepage = "https://github.com/microsoft/snmalloc"
7+
repository = "https://github.com/microsoft/snmalloc"
8+
9+
10+
[lib]
11+
path = "lib.rs"
12+
13+
[dependencies]
14+
libc = "0.2.172"

0 commit comments

Comments
 (0)