mimalloc_rust_sys/
heap.rs

1use cty::{c_char, c_void};
2
3use crate::types::mi_heap_t;
4
5// Doc: https://microsoft.github.io/mimalloc/group__heap.html
6
7#[repr(C)]
8#[derive(Debug, Clone, Copy)]
9pub struct mi_heap_area_t {
10    pub blocks: *mut c_void,
11    pub reserved: usize,
12    pub committed: usize,
13    pub used: usize,
14    pub block_size: usize,
15}
16
17pub type mi_block_visit_fun = Option<
18    unsafe extern "C" fn(
19        heap: *const mi_heap_t,
20        area: *const mi_heap_area_t,
21        block: *mut c_void,
22        block_size: usize,
23        arg: *mut c_void,
24    ) -> bool,
25>;
26
27extern "C" {
28    pub fn mi_heap_new() -> *mut mi_heap_t;
29    pub fn mi_heap_delete(heap: *mut mi_heap_t);
30    pub fn mi_heap_destroy(heap: *mut mi_heap_t);
31    pub fn mi_heap_set_default(heap: *mut mi_heap_t) -> *mut mi_heap_t;
32    pub fn mi_heap_get_default() -> *mut mi_heap_t;
33    pub fn mi_heap_get_backing() -> *mut mi_heap_t;
34    pub fn mi_heap_collect(heap: *mut mi_heap_t, force: bool);
35    pub fn mi_heap_malloc(heap: *mut mi_heap_t, size: usize) -> *mut c_void;
36    pub fn mi_heap_malloc_small(heap: *mut mi_heap_t, size: usize) -> *mut c_void;
37    pub fn mi_heap_zalloc(heap: *mut mi_heap_t, size: usize) -> *mut c_void;
38    pub fn mi_heap_calloc(heap: *mut mi_heap_t, count: usize, size: usize) -> *mut c_void;
39    pub fn mi_heap_mallocn(heap: *mut mi_heap_t, count: usize, size: usize) -> *mut c_void;
40    pub fn mi_heap_strdup(heap: *mut mi_heap_t, s: *const c_char) -> *mut c_char;
41    pub fn mi_heap_strndup(heap: *mut mi_heap_t, s: *const c_char, n: usize) -> *mut c_char;
42    pub fn mi_heap_realpath(
43        heap: *mut mi_heap_t,
44        fname: *const c_char,
45        resolved_name: *mut c_char,
46    ) -> *mut c_char;
47    pub fn mi_heap_realloc(heap: *mut mi_heap_t, p: *mut c_void, newsize: usize) -> *mut c_void;
48    pub fn mi_heap_reallocn(
49        heap: *mut mi_heap_t,
50        p: *mut c_void,
51        count: usize,
52        size: usize,
53    ) -> *mut c_void;
54    pub fn mi_heap_reallocf(heap: *mut mi_heap_t, p: *mut c_void, newsize: usize) -> *mut c_void;
55    pub fn mi_heap_malloc_aligned(
56        heap: *mut mi_heap_t,
57        size: usize,
58        alignment: usize,
59    ) -> *mut c_void;
60    pub fn mi_heap_malloc_aligned_at(
61        heap: *mut mi_heap_t,
62        size: usize,
63        alignment: usize,
64        offset: usize,
65    ) -> *mut c_void;
66    pub fn mi_heap_zalloc_aligned(
67        heap: *mut mi_heap_t,
68        size: usize,
69        alignment: usize,
70    ) -> *mut c_void;
71    pub fn mi_heap_zalloc_aligned_at(
72        heap: *mut mi_heap_t,
73        size: usize,
74        alignment: usize,
75        offset: usize,
76    ) -> *mut c_void;
77    pub fn mi_heap_calloc_aligned(
78        heap: *mut mi_heap_t,
79        count: usize,
80        size: usize,
81        alignment: usize,
82    ) -> *mut c_void;
83    pub fn mi_heap_calloc_aligned_at(
84        heap: *mut mi_heap_t,
85        count: usize,
86        size: usize,
87        alignment: usize,
88        offset: usize,
89    ) -> *mut c_void;
90    pub fn mi_heap_realloc_aligned(
91        heap: *mut mi_heap_t,
92        p: *mut c_void,
93        newsize: usize,
94        alignment: usize,
95    ) -> *mut c_void;
96    pub fn mi_heap_realloc_aligned_at(
97        heap: *mut mi_heap_t,
98        p: *mut c_void,
99        newsize: usize,
100        alignment: usize,
101        offset: usize,
102    ) -> *mut c_void;
103
104    // Zero initialized re-allocation
105    // Doc: https://microsoft.github.io/mimalloc/group__zeroinit.html
106
107    pub fn mi_heap_rezalloc(heap: *mut mi_heap_t, p: *mut c_void, newsize: usize) -> *mut c_void;
108    pub fn mi_heap_recalloc(
109        heap: *mut mi_heap_t,
110        p: *mut c_void,
111        newcount: usize,
112        size: usize,
113    ) -> *mut c_void;
114    pub fn mi_heap_rezalloc_aligned(
115        heap: *mut mi_heap_t,
116        p: *mut c_void,
117        newsize: usize,
118        alignment: usize,
119    ) -> *mut c_void;
120    pub fn mi_heap_rezalloc_aligned_at(
121        heap: *mut mi_heap_t,
122        p: *mut c_void,
123        newsize: usize,
124        alignment: usize,
125        offset: usize,
126    ) -> *mut c_void;
127    pub fn mi_heap_recalloc_aligned(
128        heap: *mut mi_heap_t,
129        p: *mut c_void,
130        newcount: usize,
131        size: usize,
132        alignment: usize,
133    ) -> *mut c_void;
134    pub fn mi_heap_recalloc_aligned_at(
135        heap: *mut mi_heap_t,
136        p: *mut c_void,
137        newcount: usize,
138        size: usize,
139        alignment: usize,
140        offset: usize,
141    ) -> *mut c_void;
142
143    // Heap Introspection
144    // Doc: https://microsoft.github.io/mimalloc/group__analysis.html
145
146    pub fn mi_heap_contains_block(heap: *mut mi_heap_t, p: *const c_void) -> bool;
147    pub fn mi_heap_check_owned(heap: *mut mi_heap_t, p: *const c_void) -> bool;
148    pub fn mi_check_owned(p: *const c_void) -> bool;
149    pub fn mi_heap_visit_blocks(
150        heap: *const mi_heap_t,
151        visit_all_blocks: bool,
152        visitor: mi_block_visit_fun,
153        arg: *mut c_void,
154    ) -> bool;
155}