From 747b5b151432ffb671e99856874e2a7e34414fb3 Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Thu, 31 Jul 2025 09:40:01 +0200 Subject: [PATCH 1/8] Introduce feature flag to switch between mimalloc major versions --- .gitmodules | 7 +++++-- Cargo.toml | 1 + libmimalloc-sys/Cargo.toml | 1 + libmimalloc-sys/build.rs | 12 +++++++++--- libmimalloc-sys/c_src/mimalloc | 1 - libmimalloc-sys/c_src/mimalloc/v2 | 1 + libmimalloc-sys/c_src/mimalloc/v3 | 1 + libmimalloc-sys/sys-test/build.rs | 14 +++++++++++--- 8 files changed, 29 insertions(+), 9 deletions(-) delete mode 160000 libmimalloc-sys/c_src/mimalloc create mode 160000 libmimalloc-sys/c_src/mimalloc/v2 create mode 160000 libmimalloc-sys/c_src/mimalloc/v3 diff --git a/.gitmodules b/.gitmodules index cf61ca1..01d2c74 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ -[submodule "libmimalloc-sys/c_src/mimalloc"] - path = libmimalloc-sys/c_src/mimalloc +[submodule "libmimalloc-sys/c_src/mimalloc/v2"] + path = libmimalloc-sys/c_src/mimalloc/v2 + url = https://github.com/microsoft/mimalloc +[submodule "libmimalloc-sys/c_src/mimalloc/v3"] + path = libmimalloc-sys/c_src/mimalloc/v3 url = https://github.com/microsoft/mimalloc diff --git a/Cargo.toml b/Cargo.toml index 3e6fd1b..410a949 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ debug_in_debug = ["libmimalloc-sys/debug_in_debug"] local_dynamic_tls = ["libmimalloc-sys/local_dynamic_tls"] no_thp = ["libmimalloc-sys/no_thp"] extended = ["libmimalloc-sys/extended"] +v3 = ["libmimalloc-sys/v3"] diff --git a/libmimalloc-sys/Cargo.toml b/libmimalloc-sys/Cargo.toml index c42b21c..f862a07 100644 --- a/libmimalloc-sys/Cargo.toml +++ b/libmimalloc-sys/Cargo.toml @@ -34,6 +34,7 @@ extended = ["cty"] arena = [] local_dynamic_tls = [] no_thp = [] +v3 = [] # Show `extended` on docs.rs since it's the full API surface. [package.metadata.docs.rs] diff --git a/libmimalloc-sys/build.rs b/libmimalloc-sys/build.rs index cd1b76e..e059bfd 100644 --- a/libmimalloc-sys/build.rs +++ b/libmimalloc-sys/build.rs @@ -3,9 +3,15 @@ use std::env; fn main() { let mut build = cc::Build::new(); - build.include("c_src/mimalloc/include"); - build.include("c_src/mimalloc/src"); - build.file("c_src/mimalloc/src/static.c"); + let version = if env::var("CARGO_FEATURE_V3").is_ok() { + "v3" + } else { + "v2" + }; + + build.include(format!("c_src/mimalloc/{version}/include")); + build.include(format!("c_src/mimalloc/{version}/src")); + build.file(format!("c_src/mimalloc/{version}/src/static.c")); let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!"); diff --git a/libmimalloc-sys/c_src/mimalloc b/libmimalloc-sys/c_src/mimalloc deleted file mode 160000 index 09a2709..0000000 --- a/libmimalloc-sys/c_src/mimalloc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 09a27098aa6e9286518bd9c74e6ffa7199c3f04e diff --git a/libmimalloc-sys/c_src/mimalloc/v2 b/libmimalloc-sys/c_src/mimalloc/v2 new file mode 160000 index 0000000..fbd8b99 --- /dev/null +++ b/libmimalloc-sys/c_src/mimalloc/v2 @@ -0,0 +1 @@ +Subproject commit fbd8b99c2b828428947d70fdc046bb55609be93e diff --git a/libmimalloc-sys/c_src/mimalloc/v3 b/libmimalloc-sys/c_src/mimalloc/v3 new file mode 160000 index 0000000..dfa50c3 --- /dev/null +++ b/libmimalloc-sys/c_src/mimalloc/v3 @@ -0,0 +1 @@ +Subproject commit dfa50c37d951128b1e77167dd9291081aa88eea4 diff --git a/libmimalloc-sys/sys-test/build.rs b/libmimalloc-sys/sys-test/build.rs index 5aabf83..c3a6a2d 100644 --- a/libmimalloc-sys/sys-test/build.rs +++ b/libmimalloc-sys/sys-test/build.rs @@ -1,9 +1,17 @@ +use std::env; + fn main() { + let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR"); + let version = if env::var("CARGO_FEATURE_V3").is_ok() { + "v3" + } else { + "v2" + }; + let mut cfg = ctest2::TestGenerator::new(); cfg.header("mimalloc.h") - .include(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../c_src/mimalloc/include" + .include(format!( + "{cargo_manifest_dir}/../c_src/mimalloc/{version}/include" )) .cfg("feature", Some("extended")) .fn_cname(|rust, link_name| link_name.unwrap_or(rust).to_string()) From d84e46ef770dd97acb468d8ea9833377c39d207c Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Thu, 31 Jul 2025 11:01:00 +0200 Subject: [PATCH 2/8] Fix excludes in Cargo manifest --- libmimalloc-sys/Cargo.toml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/libmimalloc-sys/Cargo.toml b/libmimalloc-sys/Cargo.toml index f862a07..cb3ff72 100644 --- a/libmimalloc-sys/Cargo.toml +++ b/libmimalloc-sys/Cargo.toml @@ -10,12 +10,18 @@ description = "Sys crate wrapping the mimalloc allocator" license = "MIT" links = "mimalloc" exclude = [ - "/c_src/mimalloc/bin", - "/c_src/mimalloc/cmake", - "/c_src/mimalloc/doc", - "/c_src/mimalloc/docs", - "/c_src/mimalloc/ide", - "/c_src/mimalloc/test", + "/c_src/mimalloc/v2/bin", + "/c_src/mimalloc/v2/cmake", + "/c_src/mimalloc/v2/doc", + "/c_src/mimalloc/v2/docs", + "/c_src/mimalloc/v2/ide", + "/c_src/mimalloc/v2/test", + "/c_src/mimalloc/v3/bin", + "/c_src/mimalloc/v3/cmake", + "/c_src/mimalloc/v3/doc", + "/c_src/mimalloc/v3/docs", + "/c_src/mimalloc/v3/ide", + "/c_src/mimalloc/v3/test", ] [dependencies] From af52306816930ba3b5dae99538c026931e07043c Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Mon, 25 Aug 2025 08:33:56 +0200 Subject: [PATCH 3/8] Add support for testing v3 in CI --- libmimalloc-sys/src/extended.rs | 4 ++++ libmimalloc-sys/sys-test/Cargo.toml | 3 +++ libmimalloc-sys/sys-test/build.rs | 1 + 3 files changed, 8 insertions(+) diff --git a/libmimalloc-sys/src/extended.rs b/libmimalloc-sys/src/extended.rs index 985e2e4..4de01ea 100644 --- a/libmimalloc-sys/src/extended.rs +++ b/libmimalloc-sys/src/extended.rs @@ -542,11 +542,15 @@ pub const mi_option_max_errors: mi_option_t = 19; /// Option (experimental) pub const mi_option_max_warnings: mi_option_t = 20; +#[cfg(not(feature = "v3"))] /// Option (experimental) pub const mi_option_max_segment_reclaim: mi_option_t = 21; /// Last option. +#[cfg(not(feature = "v3"))] pub const _mi_option_last: mi_option_t = 37; +#[cfg(feature = "v3")] +pub const _mi_option_last: mi_option_t = 43; extern "C" { // Note: mi_option_{enable,disable} aren't exposed because they're redundant diff --git a/libmimalloc-sys/sys-test/Cargo.toml b/libmimalloc-sys/sys-test/Cargo.toml index 89fb748..d31f7d9 100644 --- a/libmimalloc-sys/sys-test/Cargo.toml +++ b/libmimalloc-sys/sys-test/Cargo.toml @@ -13,3 +13,6 @@ libc = "0.2" [build-dependencies] ctest2 = "0.4" + +[features] +v3 = ["libmimalloc-sys/v3"] diff --git a/libmimalloc-sys/sys-test/build.rs b/libmimalloc-sys/sys-test/build.rs index c3a6a2d..82229d5 100644 --- a/libmimalloc-sys/sys-test/build.rs +++ b/libmimalloc-sys/sys-test/build.rs @@ -14,6 +14,7 @@ fn main() { "{cargo_manifest_dir}/../c_src/mimalloc/{version}/include" )) .cfg("feature", Some("extended")) + .cfg("feature", (version == "v3").then(|| "v3")) .fn_cname(|rust, link_name| link_name.unwrap_or(rust).to_string()) // ignore whether or not the option enum is signed. .skip_signededness(|c| c.ends_with("_t") || c.ends_with("_e")) From 29c44c222412fa9d7ba66f9d6bbe03dc6cb9dcae Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Mon, 25 Aug 2025 08:40:37 +0200 Subject: [PATCH 4/8] Add workflows for v3 --- .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a931ec0..86ea25b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,6 +60,33 @@ jobs: - name: Test libmimalloc-sys crate bindings (extended) run: cargo run --features extended -p libmimalloc-sys-test + - name: Build (v3, secure) + run: cargo build --features v3,secure + + - name: Test (v3, secure) + run: cargo test --features v3,secure + + - name: Test libmimalloc-sys crate bindings (v3, secure) + run: cargo run --features v3,secure -p libmimalloc-sys-test + + - name: Build (v3, no secure) + run: cargo build --features v3 + + - name: Test (v3, no secure) + run: cargo test --features v3 + + - name: Test libmimalloc-sys crate bindings (v3, no secure) + run: cargo run --features v3 -p libmimalloc-sys-test + + - name: Build (v3, extended) + run: cargo build --features v3,extended + + - name: Test (v3, extended) + run: cargo test --features v3,extended + + - name: Test libmimalloc-sys crate bindings (v3, extended) + run: cargo run --features v3,extended -p libmimalloc-sys-test + lint: name: Rustfmt / Clippy runs-on: ubuntu-latest From edee487dfadb23b31b4f6b82a89a61f4383c08d0 Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Mon, 25 Aug 2025 16:01:23 +0200 Subject: [PATCH 5/8] Fix clippy lints --- libmimalloc-sys/sys-test/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmimalloc-sys/sys-test/build.rs b/libmimalloc-sys/sys-test/build.rs index 82229d5..66f18f6 100644 --- a/libmimalloc-sys/sys-test/build.rs +++ b/libmimalloc-sys/sys-test/build.rs @@ -14,7 +14,7 @@ fn main() { "{cargo_manifest_dir}/../c_src/mimalloc/{version}/include" )) .cfg("feature", Some("extended")) - .cfg("feature", (version == "v3").then(|| "v3")) + .cfg("feature", (version == "v3").then_some("v3")) .fn_cname(|rust, link_name| link_name.unwrap_or(rust).to_string()) // ignore whether or not the option enum is signed. .skip_signededness(|c| c.ends_with("_t") || c.ends_with("_e")) From 1f527f13bb560d8e08d526f0e5ddbc6bece83b80 Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Mon, 25 Aug 2025 21:18:39 +0200 Subject: [PATCH 6/8] Proper feature flag propagation in binding tests --- .github/workflows/ci.yml | 10 +++++----- libmimalloc-sys/sys-test/Cargo.toml | 4 +++- libmimalloc-sys/sys-test/build.rs | 13 ++++++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86ea25b..de9e80e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: run: cargo test --features secure - name: Test libmimalloc-sys crate bindings (secure) - run: cargo run --features secure -p libmimalloc-sys-test + run: cargo run --features libmimalloc-sys-test/secure -p libmimalloc-sys-test - name: Build (no secure) run: cargo build @@ -58,7 +58,7 @@ jobs: run: cargo test --features extended - name: Test libmimalloc-sys crate bindings (extended) - run: cargo run --features extended -p libmimalloc-sys-test + run: cargo run --features libmimalloc-sys-test/extended -p libmimalloc-sys-test - name: Build (v3, secure) run: cargo build --features v3,secure @@ -67,7 +67,7 @@ jobs: run: cargo test --features v3,secure - name: Test libmimalloc-sys crate bindings (v3, secure) - run: cargo run --features v3,secure -p libmimalloc-sys-test + run: cargo run --features libmimalloc-sys-test/v3,libmimalloc-sys-test/secure -p libmimalloc-sys-test - name: Build (v3, no secure) run: cargo build --features v3 @@ -76,7 +76,7 @@ jobs: run: cargo test --features v3 - name: Test libmimalloc-sys crate bindings (v3, no secure) - run: cargo run --features v3 -p libmimalloc-sys-test + run: cargo run --features libmimalloc-sys-test/v3 -p libmimalloc-sys-test - name: Build (v3, extended) run: cargo build --features v3,extended @@ -85,7 +85,7 @@ jobs: run: cargo test --features v3,extended - name: Test libmimalloc-sys crate bindings (v3, extended) - run: cargo run --features v3,extended -p libmimalloc-sys-test + run: cargo run --features libmimalloc-sys-test/v3,libmimalloc-sys-test/extended -p libmimalloc-sys-test lint: name: Rustfmt / Clippy diff --git a/libmimalloc-sys/sys-test/Cargo.toml b/libmimalloc-sys/sys-test/Cargo.toml index d31f7d9..5148cec 100644 --- a/libmimalloc-sys/sys-test/Cargo.toml +++ b/libmimalloc-sys/sys-test/Cargo.toml @@ -8,11 +8,13 @@ license = "MIT" publish = false [dependencies] -libmimalloc-sys = { path = "..", features = ["extended"] } +libmimalloc-sys = { path = ".." } libc = "0.2" [build-dependencies] ctest2 = "0.4" [features] +secure = ["libmimalloc-sys/secure"] +extended = ["libmimalloc-sys/extended"] v3 = ["libmimalloc-sys/v3"] diff --git a/libmimalloc-sys/sys-test/build.rs b/libmimalloc-sys/sys-test/build.rs index 66f18f6..383f0dc 100644 --- a/libmimalloc-sys/sys-test/build.rs +++ b/libmimalloc-sys/sys-test/build.rs @@ -2,6 +2,16 @@ use std::env; fn main() { let cargo_manifest_dir = env!("CARGO_MANIFEST_DIR"); + let secure = if env::var("CARGO_FEATURE_SECURE").is_ok() { + Some("secure") + } else { + None + }; + let extended = if env::var("CARGO_FEATURE_EXTENDED").is_ok() { + Some("extended") + } else { + None + }; let version = if env::var("CARGO_FEATURE_V3").is_ok() { "v3" } else { @@ -13,7 +23,8 @@ fn main() { .include(format!( "{cargo_manifest_dir}/../c_src/mimalloc/{version}/include" )) - .cfg("feature", Some("extended")) + .cfg("feature", secure) + .cfg("feature", extended) .cfg("feature", (version == "v3").then_some("v3")) .fn_cname(|rust, link_name| link_name.unwrap_or(rust).to_string()) // ignore whether or not the option enum is signed. From 54d6262e0ec1fa10c65ee502904ce62557c74461 Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Tue, 26 Aug 2025 07:58:30 +0200 Subject: [PATCH 7/8] Allow unused imports in generated test code --- libmimalloc-sys/sys-test/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmimalloc-sys/sys-test/src/main.rs b/libmimalloc-sys/sys-test/src/main.rs index d637386..d9f44a8 100644 --- a/libmimalloc-sys/sys-test/src/main.rs +++ b/libmimalloc-sys/sys-test/src/main.rs @@ -1,4 +1,4 @@ -#![allow(bad_style, clippy::all)] +#![allow(bad_style, unused_imports, clippy::all)] use libmimalloc_sys::*; From aaa0114619926ce59471edaa80bf94b0d881b41e Mon Sep 17 00:00:00 2001 From: Gunnar Schulze Date: Tue, 26 Aug 2025 10:22:08 +0200 Subject: [PATCH 8/8] Allow unused macros in generated test code --- libmimalloc-sys/sys-test/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmimalloc-sys/sys-test/src/main.rs b/libmimalloc-sys/sys-test/src/main.rs index d9f44a8..385a34d 100644 --- a/libmimalloc-sys/sys-test/src/main.rs +++ b/libmimalloc-sys/sys-test/src/main.rs @@ -1,4 +1,4 @@ -#![allow(bad_style, unused_imports, clippy::all)] +#![allow(bad_style, unused_imports, unused_macros, clippy::all)] use libmimalloc_sys::*;