You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Idempotent: detects an already-patched file by a marker comment and
38
+
/// returns early. Panics loudly if the upstream selector block can't be
39
+
/// located — that means mimalloc changed `prim.h` and this patch needs
40
+
/// to be updated. Leaves the submodule's working tree marked dirty
41
+
/// after a fresh clone+build; reset with `git submodule update --force`
42
+
/// if needed (next build will re-apply the patch).
43
+
fnpatch_apple_tls_model_for_v3(){
44
+
let prim_h = "c_src/mimalloc3/include/mimalloc/prim.h";
45
+
let content = std::fs::read_to_string(prim_h).unwrap_or_else(|e| {
46
+
panic!(
47
+
"could not read {}: {} — is the mimalloc3 submodule initialized?",
48
+
prim_h, e
49
+
)
50
+
});
51
+
52
+
constMARKER:&str = "mimalloc-safe vendor patch: Apple uses THREAD_LOCAL";
53
+
if content.contains(MARKER){
54
+
// Already patched — idempotent fast path.
55
+
return;
56
+
}
57
+
58
+
constOLD:&str = "#if defined(_WIN32)\n #define MI_TLS_MODEL_DYNAMIC_WIN32 1 \n#elif defined(__APPLE__) && MI_HAS_TLS_SLOT && !defined(__POWERPC__) // macOS on arm64 or x64\n // #define MI_TLS_MODEL_DYNAMIC_PTHREADS 1 // also works but a bit slower\n #define MI_TLS_MODEL_FIXED_SLOT 1\n #define MI_TLS_MODEL_FIXED_SLOT_DEFAULT 108 // seems unused. @apple: it would be great to get 2 official slots for custom allocators :-)\n #define MI_TLS_MODEL_FIXED_SLOT_CACHED 109\n // we used before __PTK_FRAMEWORK_OLDGC_KEY9 (89) but that seems used now.\n // see <https://github.com/rweichler/substrate/blob/master/include/pthread_machdep.h>\n#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__ANDROID__)\n #define MI_TLS_MODEL_DYNAMIC_PTHREADS 1\n // #define MI_TLS_MODEL_DYNAMIC_PTHREADS_DEFAULT_ENTRY_IS_NULL 1\n#else\n #define MI_TLS_MODEL_THREAD_LOCAL 1\n#endif";
59
+
60
+
constNEW:&str = "#if defined(_WIN32)\n #define MI_TLS_MODEL_DYNAMIC_WIN32 1\n// --- mimalloc-safe vendor patch: Apple uses THREAD_LOCAL + RECURSE_GUARD ---\n// See libmimalloc-sys/build.rs::patch_apple_tls_model_for_v3 for rationale.\n#elif defined(__APPLE__) && !defined(__POWERPC__) // macOS on arm64 or x64\n #define MI_TLS_MODEL_THREAD_LOCAL 1\n #ifndef MI_TLS_RECURSE_GUARD\n #define MI_TLS_RECURSE_GUARD 1\n #endif\n// --- end mimalloc-safe vendor patch ---\n#elif defined(__OpenBSD__) || defined(__ANDROID__)\n #define MI_TLS_MODEL_DYNAMIC_PTHREADS 1\n // #define MI_TLS_MODEL_DYNAMIC_PTHREADS_DEFAULT_ENTRY_IS_NULL 1\n#else\n #define MI_TLS_MODEL_THREAD_LOCAL 1\n#endif";
61
+
62
+
assert!(
63
+
content.contains(OLD),
64
+
"could not locate the Apple TLS_MODEL selector block in {}; \
65
+
upstream mimalloc may have changed the file structure. \
66
+
Update libmimalloc-sys/build.rs::patch_apple_tls_model_for_v3 to match.",
67
+
prim_h
68
+
);
69
+
70
+
let patched = content.replace(OLD,NEW);
71
+
std::fs::write(prim_h, patched)
72
+
.unwrap_or_else(|e| panic!("failed to write patched {}: {}", prim_h, e));
73
+
74
+
println!(
75
+
"cargo:warning=patched mimalloc3 prim.h for Apple THREAD_LOCAL+RECURSE_GUARD \
0 commit comments