libmimalloc_sys/lib.rs
1#![no_std]
2// Copyright 2019 Octavian Oncescu
3
4use core::ffi::c_void;
5
6extern crate libc;
7
8#[cfg(feature = "extended")]
9mod extended;
10#[cfg(feature = "extended")]
11pub use extended::*;
12
13extern "C" {
14 /// Allocate zero-initialized `size` bytes.
15 ///
16 /// Returns a pointer to newly allocated zero-initialized memory, or null if
17 /// out of memory.
18 pub fn mi_zalloc(size: usize) -> *mut c_void;
19
20 /// Allocate `size` bytes.
21 ///
22 /// Returns pointer to the allocated memory or null if out of memory.
23 /// Returns a unique pointer if called with `size` 0.
24 pub fn mi_malloc(size: usize) -> *mut c_void;
25
26 /// Re-allocate memory to `newsize` bytes.
27 ///
28 /// Return pointer to the allocated memory or null if out of memory. If null
29 /// is returned, the pointer `p` is not freed. Otherwise the original
30 /// pointer is either freed or returned as the reallocated result (in case
31 /// it fits in-place with the new size).
32 ///
33 /// If `p` is null, it behaves as [`mi_malloc`]. If `newsize` is larger than
34 /// the original `size` allocated for `p`, the bytes after `size` are
35 /// uninitialized.
36 pub fn mi_realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
37
38 /// Allocate `size` bytes aligned by `alignment`, initialized to zero.
39 ///
40 /// Return pointer to the allocated memory or null if out of memory.
41 ///
42 /// Returns a unique pointer if called with `size` 0.
43 pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
44
45 /// Allocate `size` bytes aligned by `alignment`.
46 ///
47 /// Return pointer to the allocated memory or null if out of memory.
48 ///
49 /// Returns a unique pointer if called with `size` 0.
50 pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
51
52 /// Re-allocate memory to `newsize` bytes, aligned by `alignment`.
53 ///
54 /// Return pointer to the allocated memory or null if out of memory. If null
55 /// is returned, the pointer `p` is not freed. Otherwise the original
56 /// pointer is either freed or returned as the reallocated result (in case
57 /// it fits in-place with the new size).
58 ///
59 /// If `p` is null, it behaves as [`mi_malloc_aligned`]. If `newsize` is
60 /// larger than the original `size` allocated for `p`, the bytes after
61 /// `size` are uninitialized.
62 pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
63
64 /// Free previously allocated memory.
65 ///
66 /// The pointer `p` must have been allocated before (or be null).
67 pub fn mi_free(p: *mut c_void);
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn it_frees_memory_malloc() {
76 let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
77 unsafe { mi_free(ptr as *mut c_void) };
78 }
79
80 #[test]
81 fn it_frees_memory_zalloc() {
82 let ptr = unsafe { mi_zalloc_aligned(8, 8) } as *mut u8;
83 unsafe { mi_free(ptr as *mut c_void) };
84 }
85
86 #[test]
87 fn it_frees_memory_realloc() {
88 let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
89 let ptr = unsafe { mi_realloc_aligned(ptr as *mut c_void, 8, 8) } as *mut u8;
90 unsafe { mi_free(ptr as *mut c_void) };
91 }
92}