cyo_mimalloc_sys/lib.rs
1//! Low-level FFI bindings to [mimalloc](https://github.com/microsoft/mimalloc) v3.
2//!
3//! The build script compiles the bundled mimalloc sources into a static
4//! library and links it. For a safe wrapper and the global allocator, use the
5//! `cyo-mimalloc` crate.
6//!
7//! Each function has the name and the signature of its declaration in
8//! `mimalloc.h` or `mimalloc-profile.h`, and those headers document it.
9//! [`mi_option_t`] documents every runtime option and how to set it.
10
11#![no_std]
12#![allow(non_camel_case_types)]
13#![warn(missing_docs)]
14
15// ── Type aliases ────────────────────────────────────────────────────────────
16
17pub use core::ffi::{c_char, c_int, c_long, c_void};
18
19/// The C `size_t` type.
20pub type size_t = usize;
21
22// ── Constants ──────────────────────────────────────────────────────────────
23
24/// The largest number of bytes of profiler data that mimalloc stores with a
25/// sampled allocation.
26pub const MI_PROFILE_SAMPLE_DATA_MAX_SIZE: size_t = 1024;
27
28// ── Opaque types ────────────────────────────────────────────────────────────
29
30/// A mimalloc heap.
31///
32/// You only handle it through pointers that mimalloc returns.
33#[repr(C)]
34pub struct mi_heap_t {
35 _opaque: [u8; 0],
36}
37
38/// The identifier of a mimalloc subprocess.
39///
40/// Its field is private. Pass the value only to mimalloc functions.
41#[repr(C)]
42#[derive(Clone, Copy)]
43pub struct mi_subproc_id_t {
44 _id: *mut c_void,
45}
46
47/// The identifier of an arena.
48///
49/// Pass the value only to mimalloc functions.
50pub type mi_arena_id_t = *mut c_void;
51
52// ── Option enum ─────────────────────────────────────────────────────────────
53//
54// Kept in sync with `mi_option_e` in `mimalloc.h`.
55
56/// A mimalloc runtime option.
57///
58/// You can set an option in three places. A later one takes precedence over an
59/// earlier one:
60///
61/// 1. At build time, through a `MI_DEFAULT_*` variable in the build
62/// environment. Only the options whose entry below names a variable accept
63/// one.
64/// 2. In the environment of the running process, as `MIMALLOC_<NAME>`.
65/// `<NAME>` is the variant name without the `mi_option_` prefix, in upper
66/// case. mimalloc reads these variables once, when the process starts.
67/// 3. From code, with `mi_option_set` (or `MiMalloc::option_set` in
68/// `cyo-mimalloc`).
69///
70/// Setting an option from code only has an effect if mimalloc reads it again
71/// afterwards. Each variant says when mimalloc reads it:
72///
73/// - **on use**: every time mimalloc needs the value. You can change it at any
74/// time.
75/// - **per thread**: when a thread first allocates. A change applies to threads
76/// that start afterwards.
77/// - **at startup**: once, before `main`. Setting it from code has no effect.
78/// Use the environment variable or the build-time default instead.
79/// - **at exit**: when the process ends.
80///
81/// In the environment, an option measured in KiB accepts a `K`, `M`, `G` or `T`
82/// suffix, as in `MIMALLOC_ARENA_RESERVE=4GiB`. Through `mi_option_set` and
83/// `MI_DEFAULT_*`, the value is a plain number of KiB. `mi_option_get_size`
84/// returns the value in bytes. A boolean option accepts `1`/`0`,
85/// `true`/`false`, `on`/`off` and `yes`/`no`.
86#[repr(C)]
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub enum mi_option_t {
89 /// Whether mimalloc prints error messages to stderr.
90 ///
91 /// The default is 0, or 1 in a build with `MI_DEBUG`. mimalloc reads it on
92 /// use.
93 mi_option_show_errors = 0,
94 /// Whether mimalloc prints statistics to stderr when the process exits.
95 ///
96 /// The default is 0. mimalloc reads it at exit.
97 mi_option_show_stats = 1,
98 /// Whether mimalloc prints verbose messages. These include the option
99 /// values at startup and the statistics at exit.
100 ///
101 /// The default is 0. `MI_DEFAULT_VERBOSE` sets the default at build time.
102 /// mimalloc reads it on use.
103 mi_option_verbose = 2,
104 /// Whether mimalloc commits all the memory of a new arena when it creates
105 /// the arena.
106 ///
107 /// 0 commits the memory on demand instead. 1 commits all of it, and 2 does
108 /// so only on a system that overcommits, such as Linux. Committing memory
109 /// does not raise the resident set. The default is 2.
110 /// `MI_DEFAULT_ARENA_EAGER_COMMIT` sets the default at build time.
111 /// mimalloc reads it on use, when it creates an arena.
112 mi_option_arena_eager_commit = 4,
113 /// How mimalloc gives unused memory back to the OS: 1 decommits it, and 0
114 /// resets it.
115 ///
116 /// On Linux, decommitting uses `MADV_DONTNEED` and lowers the resident set
117 /// immediately. Resetting uses `MADV_FREE`, and the kernel reclaims the
118 /// memory when it needs it. The default is 1. mimalloc reads it on use.
119 mi_option_purge_decommits = 5,
120 /// Whether mimalloc uses explicit large OS pages (2 MiB, `MAP_HUGETLB`)
121 /// when they are available.
122 ///
123 /// The kernel only has these pages if `vm.nr_hugepages` is set, and
124 /// mimalloc cannot purge them. The default is 0.
125 /// `MI_DEFAULT_ALLOW_LARGE_OS_PAGES` sets the default at build time.
126 /// mimalloc reads it on use.
127 mi_option_allow_large_os_pages = 6,
128 /// The number of 1 GiB huge pages that mimalloc reserves at startup,
129 /// spread over the NUMA nodes.
130 ///
131 /// The default is 0. `MI_DEFAULT_RESERVE_HUGE_OS_PAGES` sets the default at
132 /// build time. mimalloc reads it at startup. From code, call
133 /// `mi_reserve_huge_os_pages_interleave` instead.
134 mi_option_reserve_huge_os_pages = 7,
135 /// The NUMA node on which mimalloc reserves all the huge pages of
136 /// `mi_option_reserve_huge_os_pages`, or -1 to spread them over all nodes.
137 ///
138 /// The default is -1. mimalloc reads it at startup. From code, call
139 /// `mi_reserve_huge_os_pages_at` instead.
140 mi_option_reserve_huge_os_pages_at = 8,
141 /// The amount of memory, in KiB, that mimalloc reserves as an arena at
142 /// startup.
143 ///
144 /// The default is 0. `MI_DEFAULT_RESERVE_OS_MEMORY` sets the default at
145 /// build time. mimalloc reads it at startup. From code, call
146 /// `mi_reserve_os_memory_ex` instead.
147 mi_option_reserve_os_memory = 9,
148 /// The delay in milliseconds before mimalloc purges unused memory (see
149 /// `mi_option_purge_decommits`).
150 ///
151 /// 0 purges immediately, and -1 never purges. The default is 1000. mimalloc
152 /// reads it on use.
153 mi_option_purge_delay = 15,
154 /// The largest number of NUMA nodes that mimalloc uses, or 0 to detect
155 /// them.
156 ///
157 /// Setting 1 can help in a virtual machine that reports NUMA incorrectly.
158 /// The default is 0. mimalloc reads it at startup. It has no build-time
159 /// default, so set `MIMALLOC_USE_NUMA_NODES` instead.
160 mi_option_use_numa_nodes = 16,
161 /// Whether mimalloc allocates only from arenas reserved in advance, and
162 /// never from the OS.
163 ///
164 /// The default is 0. mimalloc reads it on use.
165 mi_option_disallow_os_alloc = 17,
166 /// The memory tag that mimalloc gives its memory on macOS.
167 ///
168 /// Only macOS uses it.
169 mi_option_os_tag = 18,
170 /// The largest number of error messages that mimalloc prints.
171 ///
172 /// The default is 32. mimalloc reads it at startup.
173 mi_option_max_errors = 19,
174 /// The largest number of warning messages that mimalloc prints.
175 ///
176 /// The default is 32. mimalloc reads it at startup.
177 mi_option_max_warnings = 20,
178 /// Whether mimalloc releases all its OS memory when the process exits,
179 /// even memory that other code may still use afterwards.
180 ///
181 /// The default is 0. mimalloc reads it at exit.
182 mi_option_destroy_on_exit = 22,
183 /// The size, in KiB, of each arena that mimalloc reserves from the OS.
184 ///
185 /// The default is 1 GiB. `MI_DEFAULT_ARENA_RESERVE` sets the default at
186 /// build time. mimalloc reads it on use, when it creates an arena.
187 mi_option_arena_reserve = 23,
188 /// The multiplier that mimalloc applies to `mi_option_purge_delay` when it
189 /// purges arena memory.
190 ///
191 /// The default is 4. mimalloc reads it on use.
192 mi_option_arena_purge_mult = 24,
193 /// Whether mimalloc allocates from an arena only if you request that arena
194 /// by its id.
195 ///
196 /// The default is 0. `MI_DEFAULT_DISALLOW_ARENA_ALLOC` sets the default at
197 /// build time. mimalloc reads it on use.
198 mi_option_disallow_arena_alloc = 26,
199 /// How many milliseconds mimalloc retries an allocation after the OS runs
200 /// out of memory, or 0 to not retry.
201 ///
202 /// Only Windows uses it. The default is 400.
203 mi_option_retry_on_oom = 27,
204 /// The smallest object size that guarded allocation guards.
205 ///
206 /// It only has an effect in a build with `MI_GUARDED`, which this crate
207 /// does not enable.
208 mi_option_guarded_min = 29,
209 /// The largest object size that guarded allocation guards.
210 ///
211 /// It only has an effect in a build with `MI_GUARDED`, which this crate
212 /// does not enable.
213 mi_option_guarded_max = 30,
214 /// Whether guarded allocation places each block directly against its guard
215 /// page.
216 ///
217 /// It only has an effect in a build with `MI_GUARDED`, which this crate
218 /// does not enable.
219 mi_option_guarded_precise = 31,
220 /// The rate N at which guarded allocation guards 1 in N allocations.
221 ///
222 /// It only has an effect in a build with `MI_GUARDED`, which this crate
223 /// does not enable.
224 mi_option_guarded_sample_rate = 32,
225 /// The random seed that guarded allocation uses for sampling.
226 ///
227 /// It only has an effect in a build with `MI_GUARDED`, which this crate
228 /// does not enable.
229 mi_option_guarded_sample_seed = 33,
230 /// The number of allocations on the slow path after which mimalloc
231 /// collects a thread's heap.
232 ///
233 /// The default is 10000. mimalloc reads it on use.
234 mi_option_generic_collect = 34,
235 /// Whether mimalloc reclaims an abandoned page when a block in it is freed.
236 ///
237 /// -1 never reclaims it. 0 reclaims it only into the thread that owned it,
238 /// and 1 reclaims it into any thread. The default is 0. mimalloc reads it
239 /// per thread and on use.
240 mi_option_page_reclaim_on_free = 35,
241 /// The number of empty pages that each thread keeps in its free queues, or
242 /// -1 to never abandon pages.
243 ///
244 /// The default is 2. mimalloc reads it per thread.
245 mi_option_page_full_retain = 36,
246 /// The largest number of pages that mimalloc searches for the best fit.
247 ///
248 /// The default is 4. mimalloc reads it on use.
249 mi_option_page_max_candidates = 37,
250 /// The largest number of usable virtual address bits, or 0 to detect it.
251 ///
252 /// The default is 0. mimalloc reads it at startup.
253 mi_option_max_vabits = 38,
254 /// Whether mimalloc commits the whole page map at startup, instead of on
255 /// demand.
256 ///
257 /// The default is 0. `MI_DEFAULT_PAGEMAP_COMMIT` sets the default at build
258 /// time. mimalloc reads it at startup.
259 mi_option_pagemap_commit = 39,
260 /// Whether mimalloc commits pages on demand: 0 = no, 1 = yes, 2 = only on a
261 /// system that does not overcommit.
262 ///
263 /// The default is 0. mimalloc reads it on use.
264 mi_option_page_commit_on_demand = 40,
265 /// A limit on reclaiming a thread's own abandoned pages, or -1 for no
266 /// limit.
267 ///
268 /// A thread that already owns this many pages of a size class no longer
269 /// reclaims its abandoned pages of that class. The default is -1. `MI_DEFAULT_PAGE_MAX_RECLAIM` sets the default at
270 /// build time. mimalloc reads it on use.
271 mi_option_page_max_reclaim = 41,
272 /// A limit on reclaiming pages that other threads abandoned.
273 ///
274 /// A thread that already owns this many pages of a size class no longer
275 /// reclaims pages of that class from other threads. The default is 32. `MI_DEFAULT_PAGE_CROSS_THREAD_MAX_RECLAIM` sets the
276 /// default at build time. mimalloc reads it on use.
277 mi_option_page_cross_thread_max_reclaim = 42,
278 /// Whether mimalloc uses transparent huge pages (THP).
279 ///
280 /// 0 disables THP for the whole process, and 1 allows it. 2 allows it and
281 /// raises `mi_option_minimal_purge_size` to 2 MiB, so that purging does not
282 /// split huge pages. The default is 2. `MI_DEFAULT_ALLOW_THP` sets the
283 /// default at build time. mimalloc disables THP for the process at startup,
284 /// and reads the option on use to decide whether to ask for huge pages.
285 mi_option_allow_thp = 43,
286 /// The smallest amount of memory, in KiB, that mimalloc purges at once.
287 ///
288 /// 0 picks 64 KiB, or 2 MiB when THP is available and
289 /// `mi_option_allow_thp` is 2. The default is 0. mimalloc reads it on use.
290 mi_option_minimal_purge_size = 44,
291 /// The size, in KiB, of the largest object that mimalloc allocates from an
292 /// arena.
293 ///
294 /// mimalloc allocates a larger object directly from the OS. The default is
295 /// 2 GiB. `MI_DEFAULT_ARENA_MAX_OBJECT_SIZE` sets the default at build
296 /// time. mimalloc reads it on use.
297 mi_option_arena_max_object_size = 45,
298 /// Whether mimalloc ties a new arena to the NUMA node of the thread that
299 /// creates it.
300 ///
301 /// The default is 0. mimalloc reads it on use.
302 mi_option_arena_is_numa_local = 46,
303 /// Whether mimalloc merges a thread heap's statistics into its parent heap
304 /// on every collection.
305 ///
306 /// The default is 1. `MI_DEFAULT_COLLECT_MERGES_STATS` sets the default at
307 /// build time. mimalloc reads it on use.
308 mi_option_collect_merges_stats = 47,
309}
310
311// ── Heap area (for visiting blocks) ─────────────────────────────────────────
312
313/// An area of a heap that holds blocks of a single size.
314#[repr(C)]
315pub struct mi_heap_area_t {
316 /// The start of the area.
317 pub blocks: *mut c_void,
318 /// The number of bytes of address space reserved for the area.
319 pub reserved: size_t,
320 /// The number of bytes of the area that are committed.
321 pub committed: size_t,
322 /// The number of allocated blocks in the area.
323 pub used: size_t,
324 /// The size of each block in bytes.
325 pub block_size: size_t,
326 /// The size of each block in bytes, including padding and metadata.
327 pub full_block_size: size_t,
328 /// Reserved for mimalloc.
329 pub reserved1: *mut c_void,
330}
331
332// ── Callback types ──────────────────────────────────────────────────────────
333
334/// A function that receives mimalloc's text output.
335///
336/// mimalloc passes each piece of output as a NUL-terminated string in `msg`,
337/// and passes back the `arg` that you registered.
338pub type mi_output_fun = unsafe extern "C" fn(msg: *const c_char, arg: *mut c_void);
339/// A function that mimalloc calls when an error occurs, with the `errno`
340/// value of the error in `err`.
341pub type mi_error_fun = unsafe extern "C" fn(err: c_int, arg: *mut c_void);
342/// A function that mimalloc calls regularly, so that you can free memory
343/// that you deferred freeing.
344pub type mi_deferred_free_fun = unsafe extern "C" fn(force: bool, heartbeat: u64, arg: *mut c_void);
345/// A function that mimalloc calls on each sampled allocation.
346pub type mi_profiler_on_alloc_fun = unsafe extern "C" fn(
347 profiler: *mut mi_profiler_t,
348 profiler_data: *mut mi_profiler_sample_data_t,
349 ptr: *mut c_void,
350 requested_size: size_t,
351 bytes_sample_rate: size_t,
352 bytes_since_last_sample: u64,
353 heap: *const mi_heap_t,
354) -> size_t;
355/// A function that mimalloc calls when it reallocates a sampled allocation in
356/// place.
357pub type mi_profiler_on_realloc_inplace_fun = unsafe extern "C" fn(
358 profiler: *mut mi_profiler_t,
359 profiler_data: *mut mi_profiler_sample_data_t,
360 ptr: *mut c_void,
361 old_size: size_t,
362 heap: *const mi_heap_t,
363) -> size_t;
364/// A function that mimalloc calls when a sampled allocation is freed.
365pub type mi_profiler_on_free_fun = unsafe extern "C" fn(
366 profiler: *mut mi_profiler_t,
367 profiler_data: *mut mi_profiler_sample_data_t,
368 ptr: *mut c_void,
369 heap: *const mi_heap_t,
370);
371/// A function that mimalloc calls for each area of a heap, and for each
372/// block when you ask it to visit blocks.
373///
374/// Return `false` to stop the visit.
375pub type mi_block_visit_fun = unsafe extern "C" fn(
376 heap: *const mi_heap_t,
377 area: *const mi_heap_area_t,
378 block: *mut c_void,
379 block_size: size_t,
380 arg: *mut c_void,
381) -> bool;
382/// A function that mimalloc calls for each heap of a subprocess.
383///
384/// Return `false` to stop the visit.
385pub type mi_heap_visit_fun = unsafe extern "C" fn(heap: *mut mi_heap_t, arg: *mut c_void) -> bool;
386
387// ── Profiling ───────────────────────────────────────────────────────────────
388
389/// The profiler data that mimalloc stores with a sampled allocation.
390///
391/// mimalloc stores it only if the profiler's `on_free` is set.
392#[repr(C)]
393pub struct mi_profiler_sample_data_t {
394 /// The size of `user_data` in bytes, equal to the profiler's
395 /// `sample_data_size`.
396 pub user_data_size: size_t,
397 /// The start of the user data. The data can be longer than this array,
398 /// up to [`MI_PROFILE_SAMPLE_DATA_MAX_SIZE`] bytes.
399 pub user_data: [*mut c_void; 1],
400}
401
402/// A table of profiling callbacks. mimalloc's profiling support is
403/// experimental.
404///
405/// mimalloc may copy the table and read it from several threads at once, so
406/// do not change it after you register it. Every field can be null or 0.
407#[repr(C)]
408pub struct mi_profiler_t {
409 /// Reserved for mimalloc.
410 pub reserved: *mut c_void,
411 /// The number of bytes of data that mimalloc stores with each sampled
412 /// allocation, or 0 for none.
413 pub sample_data_size: size_t,
414 /// The initial sample rate in bytes, at least 1. `on_alloc` can change it.
415 pub initial_sample_rate: size_t,
416 /// The function that mimalloc calls on a sampled allocation, possibly
417 /// from several threads at once.
418 pub on_alloc: Option<mi_profiler_on_alloc_fun>,
419 /// The function that mimalloc calls when a sampled allocation is freed,
420 /// possibly from several threads at once.
421 pub on_free: Option<mi_profiler_on_free_fun>,
422 /// The function that mimalloc calls when it reallocates a sampled
423 /// allocation in place. mimalloc v3.5 does not call it yet.
424 pub on_realloc_inplace: Option<mi_profiler_on_realloc_inplace_fun>,
425}
426
427// ── Standard malloc interface ───────────────────────────────────────────────
428
429#[allow(missing_docs)]
430unsafe extern "C" {
431 pub fn mi_malloc(size: size_t) -> *mut c_void;
432 pub fn mi_calloc(count: size_t, size: size_t) -> *mut c_void;
433 pub fn mi_realloc(p: *mut c_void, newsize: size_t) -> *mut c_void;
434 pub fn mi_free(p: *mut c_void);
435 pub fn mi_strdup(s: *const c_char) -> *mut c_char;
436}
437
438// ── Extended allocation ─────────────────────────────────────────────────────
439
440#[allow(missing_docs)]
441unsafe extern "C" {
442 pub fn mi_malloc_small(size: size_t) -> *mut c_void;
443 pub fn mi_zalloc_small(size: size_t) -> *mut c_void;
444 pub fn mi_wmalloc_small(wsize: size_t) -> *mut c_void;
445 pub fn mi_wzalloc_small(wsize: size_t) -> *mut c_void;
446 pub fn mi_zalloc(size: size_t) -> *mut c_void;
447 pub fn mi_mallocn(count: size_t, size: size_t) -> *mut c_void;
448 pub fn mi_reallocn(p: *mut c_void, count: size_t, size: size_t) -> *mut c_void;
449 pub fn mi_usable_size(p: *const c_void) -> size_t;
450 pub fn mi_good_size(size: size_t) -> size_t;
451 pub fn mi_free_size(p: *mut c_void, size: size_t);
452 pub fn mi_free_small(p: *mut c_void);
453 pub fn mi_free_small_nonnull(p: *mut c_void);
454}
455
456// ── Aligned allocation ──────────────────────────────────────────────────────
457
458#[allow(missing_docs)]
459unsafe extern "C" {
460 pub fn mi_malloc_aligned(size: size_t, alignment: size_t) -> *mut c_void;
461 pub fn mi_zalloc_aligned(size: size_t, alignment: size_t) -> *mut c_void;
462 pub fn mi_calloc_aligned(count: size_t, size: size_t, alignment: size_t) -> *mut c_void;
463 pub fn mi_realloc_aligned(p: *mut c_void, newsize: size_t, alignment: size_t) -> *mut c_void;
464}
465
466// ── Process & thread lifecycle ──────────────────────────────────────────────
467
468#[allow(missing_docs)]
469unsafe extern "C" {
470 pub fn mi_collect(force: bool);
471 pub fn mi_thread_set_in_threadpool();
472 pub fn mi_version() -> c_int;
473 pub fn mi_process_info_print_out(out: Option<mi_output_fun>, arg: *mut c_void);
474 pub fn mi_process_info(
475 elapsed_msecs: *mut size_t,
476 user_msecs: *mut size_t,
477 system_msecs: *mut size_t,
478 current_rss: *mut size_t,
479 peak_rss: *mut size_t,
480 current_commit: *mut size_t,
481 peak_commit: *mut size_t,
482 page_faults: *mut size_t,
483 );
484}
485
486// ── Heaps ───────────────────────────────────────────────────────────────────
487
488#[allow(missing_docs)]
489unsafe extern "C" {
490 pub fn mi_heap_new() -> *mut mi_heap_t;
491 pub fn mi_heap_delete(heap: *mut mi_heap_t);
492 pub fn mi_heap_destroy(heap: *mut mi_heap_t);
493 pub fn mi_heap_collect(heap: *mut mi_heap_t, force: bool);
494 pub fn mi_heap_main() -> *mut mi_heap_t;
495 pub fn mi_heap_of(p: *const c_void) -> *mut mi_heap_t;
496 pub fn mi_heap_contains(heap: *const mi_heap_t, p: *const c_void) -> bool;
497
498 pub fn mi_heap_malloc(heap: *mut mi_heap_t, size: size_t) -> *mut c_void;
499 pub fn mi_heap_zalloc(heap: *mut mi_heap_t, size: size_t) -> *mut c_void;
500 pub fn mi_heap_calloc(heap: *mut mi_heap_t, count: size_t, size: size_t) -> *mut c_void;
501 pub fn mi_heap_realloc(heap: *mut mi_heap_t, p: *mut c_void, newsize: size_t) -> *mut c_void;
502 pub fn mi_heap_malloc_aligned(
503 heap: *mut mi_heap_t,
504 size: size_t,
505 alignment: size_t,
506 ) -> *mut c_void;
507}
508
509// ── Arena management ────────────────────────────────────────────────────────
510
511#[allow(missing_docs)]
512unsafe extern "C" {
513 pub fn mi_reserve_os_memory_ex(
514 size: size_t,
515 commit: bool,
516 allow_large: bool,
517 exclusive: bool,
518 arena_id: *mut mi_arena_id_t,
519 ) -> c_int;
520 pub fn mi_manage_os_memory_ex(
521 start: *mut c_void,
522 size: size_t,
523 is_committed: bool,
524 is_pinned: bool,
525 is_zero: bool,
526 numa_node: c_int,
527 exclusive: bool,
528 arena_id: *mut mi_arena_id_t,
529 ) -> bool;
530 pub fn mi_reserve_huge_os_pages_interleave(
531 pages: size_t,
532 numa_nodes: size_t,
533 timeout_msecs: size_t,
534 ) -> c_int;
535 pub fn mi_reserve_huge_os_pages_at(
536 pages: size_t,
537 numa_node: c_int,
538 timeout_msecs: size_t,
539 ) -> c_int;
540 pub fn mi_arena_min_alignment() -> size_t;
541 pub fn mi_arena_min_size() -> size_t;
542 pub fn mi_arena_max_object_size() -> size_t;
543 pub fn mi_heap_new_in_arena(arena_id: mi_arena_id_t) -> *mut mi_heap_t;
544}
545
546// ── Options ─────────────────────────────────────────────────────────────────
547
548#[allow(missing_docs)]
549unsafe extern "C" {
550 pub fn mi_option_is_enabled(option: mi_option_t) -> bool;
551 pub fn mi_option_enable(option: mi_option_t);
552 pub fn mi_option_disable(option: mi_option_t);
553 pub fn mi_option_get(option: mi_option_t) -> c_long;
554 pub fn mi_option_get_size(option: mi_option_t) -> size_t;
555 pub fn mi_option_set(option: mi_option_t, value: c_long);
556}
557
558// ── Experimental profiling ─────────────────────────────────────────────────
559
560#[allow(missing_docs)]
561unsafe extern "C" {
562 pub fn mi_heap_profile(heap: *mut mi_heap_t, profiler: *mut mi_profiler_t) -> bool;
563 pub fn mi_heap_profile_disable(heap: *mut mi_heap_t);
564 pub fn mi_subproc_profile(subproc_id: mi_subproc_id_t, profiler: *mut mi_profiler_t) -> bool;
565 pub fn mi_profile(profiler: *mut mi_profiler_t) -> bool;
566 pub fn mi_profiler_start(profiler: *mut mi_profiler_t) -> bool;
567 pub fn mi_profiler_stop(profiler: *mut mi_profiler_t) -> bool;
568}
569
570// ── POSIX-compatible ────────────────────────────────────────────────────────
571
572#[allow(missing_docs)]
573unsafe extern "C" {
574 pub fn mi_posix_memalign(p: *mut *mut c_void, alignment: size_t, size: size_t) -> c_int;
575 pub fn mi_memalign(alignment: size_t, size: size_t) -> *mut c_void;
576 pub fn mi_malloc_size(p: *const c_void) -> size_t;
577 pub fn mi_malloc_usable_size(p: *const c_void) -> size_t;
578}
579
580// ── Statistics ──────────────────────────────────────────────────────────────
581
582#[allow(missing_docs)]
583unsafe extern "C" {
584 pub fn mi_stats_get_json(buf_size: size_t, buf: *mut c_char) -> *mut c_char;
585 pub fn mi_stats_print_out(out: Option<mi_output_fun>, arg: *mut c_void);
586 pub fn mi_stats_reset();
587 pub fn mi_heap_stats_get_json(
588 heap: *mut mi_heap_t,
589 buf_size: size_t,
590 buf: *mut c_char,
591 ) -> *mut c_char;
592 pub fn mi_heap_stats_print_out(
593 heap: *mut mi_heap_t,
594 out: Option<mi_output_fun>,
595 arg: *mut c_void,
596 );
597}