Skip to content

Commit 4faef03

Browse files
authored
Merge 26c0306 into 038a613
2 parents 038a613 + 26c0306 commit 4faef03

Some content is hidden

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

46 files changed

+16191
-86
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.

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: 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
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_OVERRIDE_H
9+
#define MIMALLOC_OVERRIDE_H
10+
11+
/* ----------------------------------------------------------------------------
12+
This header can be used to statically redirect malloc/free and new/delete
13+
to the mimalloc variants. This can be useful if one can include this file on
14+
each source file in a project (but be careful when using external code to
15+
not accidentally mix pointers from different allocators).
16+
-----------------------------------------------------------------------------*/
17+
18+
#include <mimalloc.h>
19+
20+
// Standard C allocation
21+
#define malloc(n) mi_malloc(n)
22+
#define calloc(n,c) mi_calloc(n,c)
23+
#define realloc(p,n) mi_realloc(p,n)
24+
#define free(p) mi_free(p)
25+
26+
#define strdup(s) mi_strdup(s)
27+
#define strndup(s,n) mi_strndup(s,n)
28+
#define realpath(f,n) mi_realpath(f,n)
29+
30+
// Microsoft extensions
31+
#define _expand(p,n) mi_expand(p,n)
32+
#define _msize(p) mi_usable_size(p)
33+
#define _recalloc(p,n,c) mi_recalloc(p,n,c)
34+
35+
#define _strdup(s) mi_strdup(s)
36+
#define _strndup(s,n) mi_strndup(s,n)
37+
#define _wcsdup(s) (wchar_t*)mi_wcsdup((const unsigned short*)(s))
38+
#define _mbsdup(s) mi_mbsdup(s)
39+
#define _dupenv_s(b,n,v) mi_dupenv_s(b,n,v)
40+
#define _wdupenv_s(b,n,v) mi_wdupenv_s((unsigned short*)(b),n,(const unsigned short*)(v))
41+
42+
// Various Posix and Unix variants
43+
#define reallocf(p,n) mi_reallocf(p,n)
44+
#define malloc_size(p) mi_usable_size(p)
45+
#define malloc_usable_size(p) mi_usable_size(p)
46+
#define cfree(p) mi_free(p)
47+
48+
#define valloc(n) mi_valloc(n)
49+
#define pvalloc(n) mi_pvalloc(n)
50+
#define reallocarray(p,s,n) mi_reallocarray(p,s,n)
51+
#define reallocarr(p,s,n) mi_reallocarr(p,s,n)
52+
#define memalign(a,n) mi_memalign(a,n)
53+
#define aligned_alloc(a,n) mi_aligned_alloc(a,n)
54+
#define posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
55+
#define _posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
56+
57+
// Microsoft aligned variants
58+
#define _aligned_malloc(n,a) mi_malloc_aligned(n,a)
59+
#define _aligned_realloc(p,n,a) mi_realloc_aligned(p,n,a)
60+
#define _aligned_recalloc(p,s,n,a) mi_aligned_recalloc(p,s,n,a)
61+
#define _aligned_msize(p,a,o) mi_usable_size(p)
62+
#define _aligned_free(p) mi_free(p)
63+
#define _aligned_offset_malloc(n,a,o) mi_malloc_aligned_at(n,a,o)
64+
#define _aligned_offset_realloc(p,n,a,o) mi_realloc_aligned_at(p,n,a,o)
65+
#define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o)
66+
67+
#endif // MIMALLOC_OVERRIDE_H

0 commit comments

Comments
 (0)