Skip to content
Snippets Groups Projects
Commit 0b6cecf6 authored by koenigl's avatar koenigl
Browse files

screw it, use builtin alloc

parent cd8d0881
No related branches found
No related tags found
No related merge requests found
......@@ -10,16 +10,17 @@ license = "Apache-2.0"
edition = "2018"
[dependencies]
libtock = { path = "third_party/libtock-rs" }
libtock = { path = "third_party/libtock-rs", features = ["alloc"]}
cbor = { path = "libraries/cbor" }
crypto = { path = "libraries/crypto" }
byteorder = { version = "1", default-features = false }
arrayref = "0.3.6"
subtle = { version = "2.2", default-features = false, features = ["nightly"] }
linked_list_allocator = { version = "0.8.1", default-features = false }
#linked_list_allocator = { version = "0.8.1", default-features = false }
[features]
#debug_allocations = ["libtock/debug_allocations"]
#alloc = ["libtock/alloc"]
debug_ctap = ["crypto/derive_debug"]
#panic_console = ["libtock/panic_console"]
std = ["cbor/std", "crypto/std", "crypto/derive_debug"]
......
use core::alloc::GlobalAlloc;
use core::alloc::Layout;
use core::ptr;
use core::ptr::NonNull;
use linked_list_allocator::Heap;
#[cfg(feature = "debug_allocations")]
use core::fmt::Write;
#[cfg(feature = "debug_allocations")]
use core::sync::atomic;
#[cfg(feature = "debug_allocations")]
use core::sync::atomic::AtomicUsize;
pub static mut HEAP: Heap = Heap::empty();
// With the "debug_allocations" feature, we use `AtomicUsize` to store the
// statistics because:
// - it is `Sync`, so we can use it in a static object (the allocator),
// - it implements interior mutability, so we can use it in the allocator
// methods (that take an immutable `&self` reference).
struct TockAllocator {
#[cfg(feature = "debug_allocations")]
count: AtomicUsize,
#[cfg(feature = "debug_allocations")]
size: AtomicUsize,
}
impl TockAllocator {
const fn new() -> TockAllocator {
TockAllocator {
#[cfg(feature = "debug_allocations")]
count: AtomicUsize::new(0),
#[cfg(feature = "debug_allocations")]
size: AtomicUsize::new(0),
}
}
}
unsafe impl GlobalAlloc for TockAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = HEAP
.allocate_first_fit(layout)
.ok()
.map_or(ptr::null_mut(), NonNull::as_ptr);
#[cfg(feature = "debug_allocations")]
{
self.count.fetch_add(1, atomic::Ordering::SeqCst);
self.size.fetch_add(layout.size(), atomic::Ordering::SeqCst);
writeln!(
crate::console::Console::new(),
"alloc[{}, {}] = {:?} ({} ptrs, {} bytes)",
layout.size(),
layout.align(),
ptr,
self.count.load(atomic::Ordering::SeqCst),
self.size.load(atomic::Ordering::SeqCst)
)
.unwrap();
}
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
#[cfg(feature = "debug_allocations")]
{
self.count.fetch_sub(1, atomic::Ordering::SeqCst);
self.size.fetch_sub(layout.size(), atomic::Ordering::SeqCst);
writeln!(
crate::console::Console::new(),
"dealloc[{}, {}] = {:?} ({} ptrs, {} bytes)",
layout.size(),
layout.align(),
ptr,
self.count.load(atomic::Ordering::SeqCst),
self.size.load(atomic::Ordering::SeqCst)
)
.unwrap();
}
HEAP.deallocate(NonNull::new_unchecked(ptr), layout)
}
}
#[global_allocator]
static ALLOCATOR: TockAllocator = TockAllocator::new();
......@@ -26,11 +26,9 @@ extern crate subtle;
#[macro_use]
extern crate cbor;
extern crate crypto;
extern crate linked_list_allocator;
mod ctap;
mod usb_ctap_hid;
mod allocator;
use core::cell::Cell;
#[cfg(feature = "debug_ctap")]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment