1#![doc = include_str!("../README.md")]
2#![no_std]
3#![cfg_attr(feature = "nightly", feature(allocator_api, doc_auto_cfg))]
4mod exec_alloc;
10
11mod bad_vec;
12pub use self::bad_vec::ExecutableMemory;
13
14#[cfg(feature = "nightly")]
15pub use alloc_api::{ExecutableAllocator, Vec};
16#[cfg(feature = "nightly")]
17mod alloc_api {
18 extern crate alloc;
19
20 use crate::exec_alloc;
21 use core::alloc::AllocError;
22
23 pub type Vec<T> = alloc::vec::Vec<T, ExecutableAllocator>;
27 pub struct ExecutableAllocator;
41 unsafe impl core::alloc::Allocator for ExecutableAllocator {
42 fn allocate(
43 &self,
44 layout: core::alloc::Layout,
45 ) -> Result<core::ptr::NonNull<[u8]>, core::alloc::AllocError> {
46 assert!(layout.align() <= exec_alloc::page_size());
47 exec_alloc::alloc_executable_memory(layout.size()).or(Err(AllocError))
48 }
49
50 unsafe fn deallocate(&self, ptr: core::ptr::NonNull<u8>, layout: core::alloc::Layout) {
51 exec_alloc::dealloc_executable_memory(ptr.as_ptr(), layout.size());
52 }
53 }
54}