#![cfg_attr(feature = "sgx", no_std)]
#[cfg(feature = "sgx")]
extern crate sgx_tstd as std;
#[cfg(feature = "sgx")]
use sgx_tstd::alloc::{GlobalAlloc, Layout};
#[cfg(feature = "sgx")]
use sgx_types::*;
use super::SgxError;
#[cfg(feature = "sgx")]
pub struct SgxAllocator {
heap_base: usize,
heap_size: usize,
allocated: core::sync::atomic::AtomicUsize,
}
#[cfg(feature = "sgx")]
impl SgxAllocator {
pub const fn new(heap_base: usize, heap_size: usize) -> Self {
Self { heap_base, heap_size, allocated: core::sync::atomic::AtomicUsize::new(0) }
}
pub fn memory_usage(&self) -> usize {
self.allocated.load(core::sync::atomic::Ordering::Relaxed)
}
pub fn available_memory(&self) -> usize {
self.heap_size - self.memory_usage()
}
}
#[cfg(feature = "sgx")]
unsafe impl GlobalAlloc for SgxAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let size = layout.size();
let align = layout.align();
let current = self.allocated.fetch_add(size, core::sync::atomic::Ordering::SeqCst);
if current + size > self.heap_size {
return core::ptr::null_mut();
}
let ptr = (self.heap_base + current) as *mut u8;
let aligned_ptr = ((ptr as usize + align - 1) & !(align - 1)) as *mut u8;
aligned_ptr
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
}
}
#[cfg(feature = "sgx")]
#[global_allocator]
static SGX_ALLOCATOR: SgxAllocator = SgxAllocator::new(
0x1000_0000, 0x1000_0000, );
#[cfg(feature = "sgx")]
pub fn init_allocator() -> Result<(), SgxError> {
Ok(())
}
#[cfg(all(feature = "sgx", not(test)))]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
use sgx_trts::trts::rsgx_abort;
if let Some(location) = info.location() {
let _ = location.file();
let _ = location.line();
}
unsafe { rsgx_abort() }
}
#[cfg(feature = "sgx")]
#[alloc_error_handler]
fn oom(_layout: Layout) -> ! {
use sgx_trts::trts::rsgx_abort;
unsafe { rsgx_abort() }
}
#[cfg(not(feature = "sgx"))]
pub struct SgxAllocator;
#[cfg(not(feature = "sgx"))]
impl SgxAllocator {
pub fn new(_heap_base: usize, _heap_size: usize) -> Self {
Self
}
pub fn memory_usage(&self) -> usize {
0
}
pub fn available_memory(&self) -> usize {
usize::MAX
}
}
#[cfg(not(feature = "sgx"))]
pub fn init_allocator() -> Result<(), SgxError> {
Ok(())
}