microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cesarzc/ssa-panic

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

allocator/mimalloc-sys/src/lib.rs

49lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use core::ffi::c_void;
5pub static MI_ALIGNMENT_MAX: usize = 1024 * 1024; // 1 MiB
6
7// Define core functions from mimalloc needed for the allocator
8extern "C" {
9 /// Allocate size bytes aligned by alignment.
10 /// size: the number of bytes to allocate
11 /// alignment: the minimal alignment of the allocated memory. Must be less than MI_ALIGNMENT_MAX
12 /// returns: a pointer to the allocated memory, or null if out of memory. The returned pointer is aligned by alignment
13 pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
14 pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
15
16 /// Free previously allocated memory.
17 /// The pointer p must have been allocated before (or be nullptr).
18 /// p: the pointer to the memory to free or nullptr
19 pub fn mi_free(p: *mut c_void);
20 pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
21}
22
23#[cfg(test)]
24mod tests {
25 use super::*;
26
27 #[test]
28 fn memory_can_be_allocated_and_freed() {
29 let ptr = unsafe { mi_malloc_aligned(8, 8) }.cast::<u8>();
30 assert!(!ptr.cast::<c_void>().is_null());
31 unsafe { mi_free(ptr.cast::<c_void>()) };
32 }
33
34 #[test]
35 fn memory_can_be_allocated_zeroed_and_freed() {
36 let ptr = unsafe { mi_zalloc_aligned(8, 8) }.cast::<u8>();
37 assert!(!ptr.cast::<c_void>().is_null());
38 unsafe { mi_free(ptr.cast::<c_void>()) };
39 }
40
41 #[test]
42 fn memory_can_be_reallocated_and_freed() {
43 let ptr = unsafe { mi_malloc_aligned(8, 8) }.cast::<u8>();
44 assert!(!ptr.cast::<c_void>().is_null());
45 let realloc_ptr = unsafe { mi_realloc_aligned(ptr.cast::<c_void>(), 8, 8) }.cast::<u8>();
46 assert!(!realloc_ptr.cast::<c_void>().is_null());
47 unsafe { mi_free(ptr.cast::<c_void>()) };
48 }
49}
50