Skip to content

Commit 3ff4d65

Browse files
authored
Merge pull request #63 from thomcc/cc-fixes
Build script cleanups, adding missing extended functions.
2 parents 4ac0858 + ed23bf3 commit 3ff4d65

File tree

2 files changed

+201
-30
lines changed

2 files changed

+201
-30
lines changed

libmimalloc-sys/build.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,27 @@ fn main() {
44
let mut build = cc::Build::new();
55

66
build.include("c_src/mimalloc/include");
7-
build.files(
8-
[
9-
"alloc-aligned",
10-
"alloc-posix",
11-
"alloc",
12-
"arena",
13-
"bitmap",
14-
"heap",
15-
"init",
16-
"options",
17-
"os",
18-
"page",
19-
"random",
20-
"region",
21-
"segment",
22-
"stats",
23-
]
24-
.iter()
25-
.map(|fname| format!("c_src/mimalloc/src/{}.c", fname)),
26-
);
27-
28-
build.define("MI_STATIC_LIB", None);
7+
build.include("c_src/mimalloc/src");
8+
build.file("c_src/mimalloc/src/static.c");
299

3010
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
11+
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!");
3112

32-
if cfg!(feature = "override") {
13+
if env::var_os("CARGO_FEATURE_OVERRIDE").is_some() {
3314
// Overriding malloc is only available on windows in shared mode, but we
3415
// only ever build a static lib.
35-
if target_os != "windows" {
16+
if target_family != "windows" {
3617
build.define("MI_MALLOC_OVERRIDE", None);
3718
}
3819
}
3920

40-
if cfg!(feature = "secure") {
21+
if env::var_os("CARGO_FEATURE_SECURE").is_some() {
4122
build.define("MI_SECURE", "4");
4223
}
4324

44-
let dynamic_tls = cfg!(feature = "local_dynamic_tls");
25+
let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();
4526

46-
if env::var("CARGO_CFG_TARGET_FAMILY").expect("target family not set") == "unix"
47-
&& target_os != "haiku"
48-
{
27+
if target_family == "unix" && target_os != "haiku" {
4928
if dynamic_tls {
5029
build.flag_if_supported("-ftls-model=local-dynamic");
5130
} else {

libmimalloc-sys/src/extended.rs

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,51 @@ extern "C" {
138138
offset: usize,
139139
) -> *mut c_void;
140140

141+
/// Zero initialized [re-allocation](crate::mi_realloc).
142+
///
143+
/// In general, only valid on memory originally allocated by zero
144+
/// initialization: [`mi_calloc`](crate::mi_calloc),
145+
/// [`mi_zalloc`](crate::mi_zalloc),
146+
/// [`mi_zalloc_aligned`](crate::mi_zalloc_aligned), ...
147+
pub fn mi_rezalloc(p: *mut c_void, newsize: usize) -> *mut c_void;
148+
149+
/// Zero initialized [re-allocation](crate::mi_realloc), following `calloc`
150+
/// paramater conventions.
151+
///
152+
/// In general, only valid on memory originally allocated by zero
153+
/// initialization: [`mi_calloc`](crate::mi_calloc),
154+
/// [`mi_zalloc`](crate::mi_zalloc),
155+
/// [`mi_zalloc_aligned`](crate::mi_zalloc_aligned), ...
156+
pub fn mi_recalloc(p: *mut c_void, newcount: usize, size: usize) -> *mut c_void;
157+
158+
/// Aligned version of [`mi_rezalloc`].
159+
pub fn mi_rezalloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
160+
161+
/// Offset-aligned version of [`mi_rezalloc`].
162+
pub fn mi_rezalloc_aligned_at(
163+
p: *mut c_void,
164+
newsize: usize,
165+
alignment: usize,
166+
offset: usize,
167+
) -> *mut c_void;
168+
169+
/// Aligned version of [`mi_recalloc`].
170+
pub fn mi_recalloc_aligned(
171+
p: *mut c_void,
172+
newcount: usize,
173+
size: usize,
174+
alignment: usize,
175+
) -> *mut c_void;
176+
177+
/// Offset-aligned version of [`mi_recalloc`].
178+
pub fn mi_recalloc_aligned_at(
179+
p: *mut c_void,
180+
newcount: usize,
181+
size: usize,
182+
alignment: usize,
183+
offset: usize,
184+
) -> *mut c_void;
185+
141186
/// Allocate an object of no more than [`MI_SMALL_SIZE_MAX`] bytes.
142187
///
143188
/// Does not check that `size` is indeed small.
@@ -179,6 +224,67 @@ extern "C" {
179224
/// resource usage by calling this every once in a while.
180225
pub fn mi_collect(force: bool);
181226

227+
/// Checked free: If `p` came from mimalloc's heap (as decided by
228+
/// [`mi_is_in_heap_region`]), this is [`mi_free(p)`](crate::mi_free), but
229+
/// otherwise it is a no-op.
230+
pub fn mi_cfree(p: *mut c_void);
231+
232+
/// Returns true if this is a pointer into a memory region that has been
233+
/// reserved by the mimalloc heap.
234+
///
235+
/// This function is described by the mimalloc documentation as "relatively
236+
/// fast".
237+
///
238+
/// See also [`mi_heap_check_owned`], which is (much) slower and slightly
239+
/// more precise, but only concerns a single `mi_heap`.
240+
pub fn mi_is_in_heap_region(p: *const c_void) -> bool;
241+
242+
/// Layout-aware deallocation: Like [`mi_free`](crate::mi_free), but accepts
243+
/// the size and alignment as well.
244+
///
245+
/// Note: unlike some allocators that require this information for
246+
/// performance, mimalloc doesn't need it (as of the current version,
247+
/// v2.0.0), and so it currently implements this as a (debug) assertion that
248+
/// verifies that `p` is actually aligned to `alignment` and is usable for
249+
/// at least `size` bytes, before delegating to `mi_free`.
250+
///
251+
/// However, currently there's no way to have this crate enable mimalloc's
252+
/// debug assertions, so these checks aren't particularly useful.
253+
///
254+
/// Note: It's legal to pass null to this function, and you are not required
255+
/// to use this to deallocate memory from an aligned allocation function.
256+
pub fn mi_free_size_aligned(p: *mut c_void, size: usize, alignment: usize);
257+
258+
/// Size-aware deallocation: Like [`mi_free`](crate::mi_free), but accepts
259+
/// the size and alignment as well.
260+
///
261+
/// Note: unlike some allocators that require this information for
262+
/// performance, mimalloc doesn't need it (as of the current version,
263+
/// v2.0.0), and so it currently implements this as a (debug) assertion that
264+
/// verifies that `p` is actually aligned to `alignment` and is usable for
265+
/// at least `size` bytes, before delegating to `mi_free`.
266+
///
267+
/// However, currently there's no way to have this crate enable mimalloc's
268+
/// debug assertions, so these checks aren't particularly useful.
269+
///
270+
/// Note: It's legal to pass null to this function.
271+
pub fn mi_free_size(p: *mut c_void, size: usize);
272+
273+
/// Alignment-aware deallocation: Like [`mi_free`](crate::mi_free), but
274+
/// accepts the size and alignment as well.
275+
///
276+
/// Note: unlike some allocators that require this information for
277+
/// performance, mimalloc doesn't need it (as of the current version,
278+
/// v2.0.0), and so it currently implements this as a (debug) assertion that
279+
/// verifies that `p` is actually aligned to `alignment` and is usable for
280+
/// at least `size` bytes, before delegating to `mi_free`.
281+
///
282+
/// However, currently there's no way to have this crate enable mimalloc's
283+
/// debug assertions, so these checks aren't particularly useful.
284+
///
285+
/// Note: It's legal to pass null to this function.
286+
pub fn mi_free_aligned(p: *mut c_void, alignment: usize);
287+
182288
/// Print the main statistics.
183289
///
184290
/// Ignores the passed in argument, and outputs to the registered output
@@ -222,6 +328,35 @@ extern "C" {
222328
/// process loader.
223329
pub fn mi_process_init();
224330

331+
/// Return process information (time and memory usage). All parameters are
332+
/// optional (nullable) out-params:
333+
///
334+
/// | Parameter | Description |
335+
/// | :- | :- |
336+
/// | `elapsed_msecs` | Elapsed wall-clock time of the process in milli-seconds. |
337+
/// | `user_msecs` | User time in milli-seconds (as the sum over all threads). |
338+
/// | `system_msecs` | System time in milli-seconds. |
339+
/// | `current_rss` | Current working set size (touched pages). |
340+
/// | `peak_rss` | Peak working set size (touched pages). |
341+
/// | `current_commit` | Current committed memory (backed by the page file). |
342+
/// | `peak_commit` | Peak committed memory (backed by the page file). |
343+
/// | `page_faults` | Count of hard page faults. |
344+
///
345+
/// The `current_rss` is precise on Windows and MacOSX; other systems
346+
/// estimate this using `current_commit`. The `commit` is precise on Windows
347+
/// but estimated on other systems as the amount of read/write accessible
348+
/// memory reserved by mimalloc.
349+
pub fn mi_process_info(
350+
elapsed_msecs: *mut usize,
351+
user_msecs: *mut usize,
352+
system_msecs: *mut usize,
353+
current_rss: *mut usize,
354+
peak_rss: *mut usize,
355+
current_commit: *mut usize,
356+
peak_commit: *mut usize,
357+
page_faults: *mut usize,
358+
);
359+
225360
/// Uninitialize mimalloc on a thread.
226361
///
227362
/// Should not be used as on most systems (pthreads, windows) this is done
@@ -425,6 +560,9 @@ pub const mi_option_os_tag: mi_option_t = 17;
425560
/// Experimental
426561
pub const mi_option_max_errors: mi_option_t = 18;
427562

563+
/// Experimental
564+
pub const mi_option_max_warnings: mi_option_t = 19;
565+
428566
extern "C" {
429567
// Note: mi_option_{enable,disable} aren't exposed because they're redundant
430568
// and because of https://github.com/microsoft/mimalloc/issues/266.
@@ -721,6 +859,59 @@ extern "C" {
721859
offset: usize,
722860
) -> *mut c_void;
723861

862+
/// Equivalent to [`mi_rezalloc`], but allocates out of the specific heap
863+
/// instead of the default.
864+
pub fn mi_heap_rezalloc(heap: *mut mi_heap_t, p: *mut c_void, newsize: usize) -> *mut c_void;
865+
866+
/// Equivalent to [`mi_recalloc`], but allocates out of the specific heap
867+
/// instead of the default.
868+
pub fn mi_heap_recalloc(
869+
heap: *mut mi_heap_t,
870+
p: *mut c_void,
871+
newcount: usize,
872+
size: usize,
873+
) -> *mut c_void;
874+
875+
/// Equivalent to [`mi_rezalloc_aligned`], but allocates out of the specific
876+
/// heap instead of the default.
877+
pub fn mi_heap_rezalloc_aligned(
878+
heap: *mut mi_heap_t,
879+
p: *mut c_void,
880+
newsize: usize,
881+
alignment: usize,
882+
) -> *mut c_void;
883+
884+
/// Equivalent to [`mi_rezalloc_aligned_at`], but allocates out of the
885+
/// specific heap instead of the default.
886+
pub fn mi_heap_rezalloc_aligned_at(
887+
heap: *mut mi_heap_t,
888+
p: *mut c_void,
889+
newsize: usize,
890+
alignment: usize,
891+
offset: usize,
892+
) -> *mut c_void;
893+
894+
/// Equivalent to [`mi_recalloc_aligned`], but allocates out of the
895+
/// specific heap instead of the default.
896+
pub fn mi_heap_recalloc_aligned(
897+
heap: *mut mi_heap_t,
898+
p: *mut c_void,
899+
newcount: usize,
900+
size: usize,
901+
alignment: usize,
902+
) -> *mut c_void;
903+
904+
/// Equivalent to [`mi_recalloc_aligned_at`], but allocates out of the
905+
/// specific heap instead of the default.
906+
pub fn mi_heap_recalloc_aligned_at(
907+
heap: *mut mi_heap_t,
908+
p: *mut c_void,
909+
newcount: usize,
910+
size: usize,
911+
alignment: usize,
912+
offset: usize,
913+
) -> *mut c_void;
914+
724915
/// Does a heap contain a pointer to a previously allocated block?
725916
///
726917
/// `p` must be a pointer to a previously allocated block (in any heap) -- it cannot be some
@@ -739,7 +930,8 @@ extern "C" {
739930
///
740931
/// Note: expensive function, linear in the pages in the heap.
741932
///
742-
/// See [`mi_heap_contains_block`], [`mi_heap_get_default`]
933+
/// See [`mi_heap_contains_block`], [`mi_heap_get_default`], and
934+
/// [`mi_is_in_heap_region`]
743935
pub fn mi_heap_check_owned(heap: *mut mi_heap_t, p: *const c_void) -> bool;
744936

745937
/// Check safely if any pointer is part of the default heap of this thread.

0 commit comments

Comments
 (0)