Skip to content

Commit 91589c3

Browse files
authored
Mimalloc is vendored instead of fetched (#1320)
These changes copy a snapshot of the Mimalloc source from `v2.1.2` into the mimalloc-sys folder so that we no longer need to use cmake to fetch and prepare the source. This also fixes `aarch64` Windows cross-compilation issues caused by cmake generators. I have addressed all DevSkim feedback and nothing was applicable or actionable. I've run the release build and everything compiles. It shows a red X in the checks as I cancelled the second half of the build to prevent the release from going out. Closes #1281
1 parent 3b8abfd commit 91589c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+16213
-112
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88
.vscode/
99
.vscode-test-web/
1010
.venv/
11+
/allocator/
1112
/compiler/
1213
/jupyterlab/lib/
1314
/jupyterlab/qsharp-jupyterlab/labextension/

Cargo.lock

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ To build this repository there are dependencies that need to be installed. These
2929
- Rust (<https://www.rust-lang.org/tools/install>)
3030
- Node.js (<https://nodejs.org/>)
3131
- wasm-pack (<https://rustwasm.github.io/wasm-pack/installer/>)
32-
- cmake (<https://cmake.org/>) and a C compiler
32+
- A [C compiler](https://docs.rs/cc/latest/cc/#compile-time-requirements)
3333

3434
The build script will check these dependencies and their versions and fail if not met. (Or run
3535
`python ./prereqs.py` directly to check if the minimum required versions are installed).

allocator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition.workspace = true
77
license.workspace = true
88
version.workspace = true
99

10-
[target.'cfg(not(any(target_family = "wasm", all(target_family = "windows", target_arch = "aarch64"))))'.dependencies]
10+
[target.'cfg(not(any(target_family = "wasm")))'.dependencies]
1111
mimalloc-sys = { path = "./mimalloc-sys" }
1212

1313
[lints]

allocator/mimalloc-sys/CMakeLists.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

allocator/mimalloc-sys/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ version.workspace = true
1515
workspace = true
1616

1717
[build-dependencies]
18-
cmake = "0.1"
1918
cc = "1.0"
20-

allocator/mimalloc-sys/build.rs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,41 @@ use std::boxed::Box;
55
use std::env;
66
use std::error::Error;
77
use std::fs;
8-
use std::path::{Path, PathBuf};
9-
10-
use cmake::Config;
11-
12-
// 1.8.2
13-
//static ALLOCATOR_MIMALLOC_TAG: &str = "b66e3214d8a104669c2ec05ae91ebc26a8f5ab78";
14-
// 2.1.2
15-
static ALLOCATOR_MIMALLOC_TAG: &str = "43ce4bd7fd34bcc730c1c7471c99995597415488";
8+
use std::path::PathBuf;
169

1710
fn main() -> Result<(), Box<dyn Error>> {
18-
let dst = download_mimalloc()?;
19-
compile_mimalloc(&dst);
20-
println!("cargo:rerun-if-changed=build.rs");
21-
println!("cargo:rerun-if-changed=CMakeLists.txt");
11+
compile_mimalloc();
12+
let build_dir = get_build_dir()?;
13+
println!(
14+
"cargo:rerun-if-changed={}",
15+
build_dir.join("mimalloc").display()
16+
);
2217
Ok(())
2318
}
2419

2520
// Compile mimalloc source code and link it to the crate.
2621
// The cc crate is used to compile the source code into a static library.
27-
// The cmake crate is used to download the source code and stage it in the build directory.
2822
// We don't use the cmake crate to compile the source code because the mimalloc build system
2923
// loads extra libraries, changes the name and path around, and does other things that are
3024
// difficult to handle. The cc crate is much simpler and more predictable.
31-
fn compile_mimalloc(dst: &Path) {
32-
let src_dir = dst
33-
.join("build")
34-
.join("mimalloc-prefix")
35-
.join("src")
36-
.join("mimalloc");
25+
fn compile_mimalloc() {
26+
let mimalloc_vendor_dir = PathBuf::from("mimalloc");
3727

3828
let mut build = cc::Build::new();
3929

40-
build.include(src_dir.join("include"));
41-
build.include(src_dir.join("src"));
42-
build.file(src_dir.join("src/static.c"));
30+
let include_dir = mimalloc_vendor_dir.join("include");
31+
let src_dir = mimalloc_vendor_dir.join("src");
32+
let static_file = src_dir.join("static.c");
33+
34+
assert!(include_dir.exists(), "include_dir: {include_dir:?}");
35+
assert!(src_dir.exists(), "src_dir: {src_dir:?}");
36+
assert!(static_file.exists(), "static_file: {static_file:?}");
37+
38+
build.include(include_dir);
39+
build.include(src_dir);
40+
build.file(static_file);
4341

4442
if build.get_compiler().is_like_msvc() {
45-
build.cpp(true);
4643
build.static_crt(true);
4744
}
4845
// turn off debug mode
@@ -52,28 +49,6 @@ fn compile_mimalloc(dst: &Path) {
5249
//build.opt_level(3);
5350

5451
build.compile("mimalloc");
55-
56-
println!(
57-
"cargo:rustc-link-search=native={}",
58-
dst.join("lib").display()
59-
);
60-
println!("cargo:rustc-link-lib=static=mimalloc");
61-
}
62-
63-
// Use cmake to download mimalloc source code and stage
64-
// it in the build directory.
65-
fn download_mimalloc() -> Result<PathBuf, Box<dyn Error>> {
66-
let build_dir = get_build_dir()?;
67-
let mut config = Config::new(build_dir);
68-
69-
config
70-
.no_build_target(true)
71-
.env("ALLOCATOR_MIMALLOC_TAG", ALLOCATOR_MIMALLOC_TAG)
72-
.very_verbose(true);
73-
74-
let dst = config.build();
75-
76-
Ok(dst)
7752
}
7853

7954
fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018-2021 Microsoft Corporation, Daan Leijen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Mimalloc
2+
3+
The `src`/`include` folders contain the [mimalloc](https://github.com/microsoft/mimalloc) source from [`v2.1.2`](https://github.com/microsoft/mimalloc/tree/v2.1.2)(`43ce4bd7fd34bcc730c1c7471c99995597415488`).
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* ----------------------------------------------------------------------------
2+
Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
3+
This is free software; you can redistribute it and/or modify it under the
4+
terms of the MIT license. A copy of the license can be found in the file
5+
"LICENSE" at the root of this distribution.
6+
-----------------------------------------------------------------------------*/
7+
#pragma once
8+
#ifndef MIMALLOC_NEW_DELETE_H
9+
#define MIMALLOC_NEW_DELETE_H
10+
11+
// ----------------------------------------------------------------------------
12+
// This header provides convenient overrides for the new and
13+
// delete operations in C++.
14+
//
15+
// This header should be included in only one source file!
16+
//
17+
// On Windows, or when linking dynamically with mimalloc, these
18+
// can be more performant than the standard new-delete operations.
19+
// See <https://en.cppreference.com/w/cpp/memory/new/operator_new>
20+
// ---------------------------------------------------------------------------
21+
#if defined(__cplusplus)
22+
#include <new>
23+
#include <mimalloc.h>
24+
25+
#if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
26+
// stay consistent with VCRT definitions
27+
#define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict _Ret_notnull_ _Post_writable_byte_size_(n)
28+
#define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n)
29+
#else
30+
#define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict
31+
#define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict
32+
#endif
33+
34+
void operator delete(void* p) noexcept { mi_free(p); };
35+
void operator delete[](void* p) noexcept { mi_free(p); };
36+
37+
void operator delete (void* p, const std::nothrow_t&) noexcept { mi_free(p); }
38+
void operator delete[](void* p, const std::nothrow_t&) noexcept { mi_free(p); }
39+
40+
mi_decl_new(n) void* operator new(std::size_t n) noexcept(false) { return mi_new(n); }
41+
mi_decl_new(n) void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); }
42+
43+
mi_decl_new_nothrow(n) void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
44+
mi_decl_new_nothrow(n) void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
45+
46+
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
47+
void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };
48+
void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };
49+
#endif
50+
51+
#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
52+
void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
53+
void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
54+
void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
55+
void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
56+
void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
57+
void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
58+
59+
void* operator new (std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
60+
void* operator new[](std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
61+
void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
62+
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
63+
#endif
64+
#endif
65+
66+
#endif // MIMALLOC_NEW_DELETE_H

0 commit comments

Comments
 (0)