From 0b6cecf611379b1168e31c441f5ff49e8ae87649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20K=C3=B6nig?= <leonard.koenig@fu-berlin.de> Date: Tue, 30 Jun 2020 14:05:04 +0200 Subject: [PATCH] screw it, use builtin alloc --- Cargo.toml | 5 +-- src/allocator.rs | 89 ------------------------------------------------ src/main.rs | 2 -- 3 files changed, 3 insertions(+), 93 deletions(-) delete mode 100644 src/allocator.rs diff --git a/Cargo.toml b/Cargo.toml index edf0cba..e004879 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/allocator.rs b/src/allocator.rs deleted file mode 100644 index fd8704c..0000000 --- a/src/allocator.rs +++ /dev/null @@ -1,89 +0,0 @@ -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(); diff --git a/src/main.rs b/src/main.rs index 38ddbfe..0767a47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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")] -- GitLab