microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c3335c732ee0487abbcca366ce45eeaa7e9c4e7d

Branches

Tags

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

Clone

HTTPS

Download ZIP

allocator/mimalloc-sys/src/lib.rs

48lines · 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
7extern "C" {
8 /// Allocate size bytes aligned by alignment.
9 /// size: the number of bytes to allocate
10 /// alignment: the minimal alignment of the allocated memory. Must be less than MI_ALIGNMENT_MAX
11 /// returns: a pointer to the allocated memory, or null if out of memory. The returned pointer is aligned by alignment
12 pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
13 pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
14
15 /// Free previously allocated memory.
16 /// The pointer p must have been allocated before (or be nullptr).
17 /// p: the pointer to the memory to free or nullptr
18 pub fn mi_free(p: *mut c_void);
19 pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25
26 #[test]
27 fn memory_can_be_allocated_and_freed() {
28 let ptr = unsafe { mi_malloc_aligned(8, 8) }.cast::<u8>();
29 assert!(!ptr.cast::<c_void>().is_null());
30 unsafe { mi_free(ptr.cast::<c_void>()) };
31 }
32
33 #[test]
34 fn memory_can_be_allocated_zeroed_and_freed() {
35 let ptr = unsafe { mi_zalloc_aligned(8, 8) }.cast::<u8>();
36 assert!(!ptr.cast::<c_void>().is_null());
37 unsafe { mi_free(ptr.cast::<c_void>()) };
38 }
39
40 #[test]
41 fn memory_can_be_reallocated_and_freed() {
42 let ptr = unsafe { mi_malloc_aligned(8, 8) }.cast::<u8>();
43 assert!(!ptr.cast::<c_void>().is_null());
44 let realloc_ptr = unsafe { mi_realloc_aligned(ptr.cast::<c_void>(), 8, 8) }.cast::<u8>();
45 assert!(!realloc_ptr.cast::<c_void>().is_null());
46 unsafe { mi_free(ptr.cast::<c_void>()) };
47 }
48}
49