mc_sgx_alloc/
allocator.rs

1// Copyright (c) 2022 The MobileCoin Foundation
2
3use core::alloc::{GlobalAlloc, Layout};
4use core::ffi::c_void;
5use core::ptr::null_mut;
6use mc_sgx_tservice_sys::{sgx_aligned_free, sgx_aligned_malloc};
7
8/// Allocator that works in an SGX enclave
9#[derive(Debug)]
10pub struct Allocator;
11
12unsafe impl GlobalAlloc for Allocator {
13    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
14        let size = layout.size();
15        let align = layout.align();
16        let memory = sgx_aligned_malloc(size, align, null_mut(), 0);
17        memory as *mut u8
18    }
19    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
20        sgx_aligned_free(ptr as *mut c_void);
21    }
22}