]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/alloc.rs
SGX target: fix cfg(test) build
[rust.git] / src / libstd / sys / sgx / alloc.rs
1 use crate::alloc::{GlobalAlloc, Layout, System};
2
3 use super::waitqueue::SpinMutex;
4
5 // Using a SpinMutex because we never want to exit the enclave waiting for the
6 // allocator.
7 #[cfg_attr(test, linkage = "available_externally")]
8 #[export_name = "_ZN16__rust_internals3std3sys3sgx5alloc8DLMALLOCE"]
9 static DLMALLOC: SpinMutex<dlmalloc::Dlmalloc> = SpinMutex::new(dlmalloc::DLMALLOC_INIT);
10
11 #[stable(feature = "alloc_system_type", since = "1.28.0")]
12 unsafe impl GlobalAlloc for System {
13     #[inline]
14     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
15         DLMALLOC.lock().malloc(layout.size(), layout.align())
16     }
17
18     #[inline]
19     unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
20         DLMALLOC.lock().calloc(layout.size(), layout.align())
21     }
22
23     #[inline]
24     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
25         DLMALLOC.lock().free(ptr, layout.size(), layout.align())
26     }
27
28     #[inline]
29     unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
30         DLMALLOC.lock().realloc(ptr, layout.size(), layout.align(), new_size)
31     }
32 }
33
34 // The following functions are needed by libunwind. These symbols are named
35 // in pre-link args for the target specification, so keep that in sync.
36 #[cfg(not(test))]
37 #[no_mangle]
38 pub unsafe extern "C" fn __rust_c_alloc(size: usize, align: usize) -> *mut u8 {
39     crate::alloc::alloc(Layout::from_size_align_unchecked(size, align))
40 }
41
42 #[cfg(not(test))]
43 #[no_mangle]
44 pub unsafe extern "C" fn __rust_c_dealloc(ptr: *mut u8, size: usize, align: usize) {
45     crate::alloc::dealloc(ptr, Layout::from_size_align_unchecked(size, align))
46 }