Skip to content
Snippets Groups Projects
Commit a7bd3c22 authored by brahms's avatar brahms
Browse files

implemented allocator

parent 111cf6f9
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ crypto = { path = "libraries/crypto" } ...@@ -16,6 +16,7 @@ crypto = { path = "libraries/crypto" }
byteorder = { version = "1", default-features = false } byteorder = { version = "1", default-features = false }
arrayref = "0.3.6" arrayref = "0.3.6"
subtle = { version = "2.2", default-features = false, features = ["nightly"] } subtle = { version = "2.2", default-features = false, features = ["nightly"] }
linked_list_allocator = { version = "0.8.1", default-features = false }
[features] [features]
#debug_allocations = ["libtock/debug_allocations"] #debug_allocations = ["libtock/debug_allocations"]
......
use core::alloc::GlobalAlloc;
use core::alloc::Layout;
use core::ptr;
use core::ptr::NonNull;
use linked_list_allocator::Heap;
pub static mut HEAP: Heap = Heap::empty();
struct TockAllocator;
unsafe impl GlobalAlloc for TockAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
HEAP.allocate_first_fit(layout)
.ok()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
HEAP.deallocate(NonNull::new_unchecked(ptr), layout)
}
}
#[global_allocator]
static ALLOCATOR: TockAllocator = TockAllocator;
#[cfg(not(feature = "custom_alloc_error_handler"))]
#[alloc_error_handler]
unsafe fn alloc_error_handler(_: Layout) -> ! {
use crate::syscalls;
// Print 0x01 using the LowLevelDebug capsule (if available).
let _ = syscalls::command1_insecure(8, 2, 0x01);
// Signal a panic using the LowLevelDebug capsule (if available).
let _ = syscalls::command1_insecure(8, 1, 0x01);
loop {
syscalls::raw::yieldk();
}
}
...@@ -30,6 +30,7 @@ extern crate crypto; ...@@ -30,6 +30,7 @@ extern crate crypto;
mod ctap; mod ctap;
mod usb_ctap_hid; mod usb_ctap_hid;
mod allocator;
use core::cell::Cell; use core::cell::Cell;
#[cfg(feature = "debug_ctap")] #[cfg(feature = "debug_ctap")]
...@@ -410,4 +411,4 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> { ...@@ -410,4 +411,4 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> {
} else { } else {
Err(Ctap2StatusCode::CTAP2_ERR_USER_ACTION_TIMEOUT) Err(Ctap2StatusCode::CTAP2_ERR_USER_ACTION_TIMEOUT)
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment