From 8cfb4bfeb951da9ddceefc711bf897ae3cad7d61 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 29 Jun 2023 21:46:56 +0200 Subject: [PATCH] add optional logging --- mips-mcu-alloc/.cargo/config | 5 ----- mips-mcu-alloc/Cargo.toml | 3 ++- mips-mcu-alloc/README.md | 2 ++ mips-mcu-alloc/src/lib.rs | 15 ++++++++++++--- 4 files changed, 16 insertions(+), 9 deletions(-) delete mode 100644 mips-mcu-alloc/.cargo/config diff --git a/mips-mcu-alloc/.cargo/config b/mips-mcu-alloc/.cargo/config deleted file mode 100644 index 8a80fe9..0000000 --- a/mips-mcu-alloc/.cargo/config +++ /dev/null @@ -1,5 +0,0 @@ -[build] -target = "mipsel-unknown-none" - -[unstable] -build-std = ["core", "compiler_builtins", "alloc"] diff --git a/mips-mcu-alloc/Cargo.toml b/mips-mcu-alloc/Cargo.toml index 5c04a29..ca487a1 100644 --- a/mips-mcu-alloc/Cargo.toml +++ b/mips-mcu-alloc/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "mips-mcu-alloc" description = "A heap allocator for MIPS based microcontrollers" -version = "0.6.0" +version = "0.6.1" authors = ["Stephan "] repository = "https://github.com/kiffie/pic32-rs/tree/master/mips-mcu-alloc" license = "MIT OR Apache-2.0" @@ -15,6 +15,7 @@ include = ["README.md", "/src", "LICENSE"] mips-mcu = "0.3.0" mips-rt = "0.3.0" critical-section = "1.0.0" +log = { version = "0.4.19", optional = true } [dependencies.linked_list_allocator] default-features = false diff --git a/mips-mcu-alloc/README.md b/mips-mcu-alloc/README.md index 25a4819..77b6211 100644 --- a/mips-mcu-alloc/README.md +++ b/mips-mcu-alloc/README.md @@ -9,6 +9,8 @@ The heap is placed at a location determined by the linker and automatically exte to fullfil allocation requests. Automatic heap extension fails if the heap would collide with the stack. +Memory allocation and heap extension can be traced via logging by activating the `log` feature. + Example: ```rust diff --git a/mips-mcu-alloc/src/lib.rs b/mips-mcu-alloc/src/lib.rs index a1c9327..a1fdc75 100644 --- a/mips-mcu-alloc/src/lib.rs +++ b/mips-mcu-alloc/src/lib.rs @@ -6,7 +6,7 @@ //! //! # Example //! -//! ``` +//! ```ignore //! #![feature(global_allocator)] //! #![feature(alloc_error_handler)] //! @@ -19,8 +19,6 @@ //! #[global_allocator] //! static ALLOCATOR: MipsMcuHeap = MipsMcuHeap::empty(); //! -//! entry!(main); -//! //! #[entry] //! fn main() -> ! { //! ALLOCATOR.init(); @@ -50,6 +48,9 @@ use critical_section::{self, Mutex}; use linked_list_allocator::Heap; use mips_rt::heap_start; +#[cfg(feature = "log")] +use log::{debug, trace}; + /// Heap extension is performed stepwise. This constant defines the size of one extension step. const EXTEND_INCREMENT: usize = 1024; @@ -71,6 +72,8 @@ impl MipsMcuHeap { /// Initialize heap with heap start location from linker and a defined initial size pub fn init(&self) { let bottom = heap_start() as *mut u8; + #[cfg(feature = "log")] + debug!("heap init, bottom = {bottom:?}, size = {EXTEND_INCREMENT}"); critical_section::with(|cs| { unsafe { self.heap @@ -109,6 +112,8 @@ unsafe impl GlobalAlloc for MipsMcuHeap { if let Ok(p) = critical_section::with(|cs| { self.heap.borrow(cs).borrow_mut().allocate_first_fit(layout) }) { + #[cfg(feature = "log")] + trace!("alloc at {:?}, {:?}, stack_pointer = {:?}", p.as_ptr(), layout, stack_pointer()); break p.as_ptr(); } else { // this must be a u8 pointer @@ -120,6 +125,8 @@ unsafe impl GlobalAlloc for MipsMcuHeap { critical_section::with(|cs| { self.heap.borrow(cs).borrow_mut().extend(EXTEND_INCREMENT) }); + #[cfg(feature = "log")] + debug!("extended heap, top = {:?}, stack_pointer = {:?}", self.top(), stack_pointer()); } else { break ptr::null_mut(); } @@ -128,6 +135,8 @@ unsafe impl GlobalAlloc for MipsMcuHeap { } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + #[cfg(feature = "log")] + trace!("dealloc at {ptr:?}, {layout:?}"); critical_section::with(|cs| { self.heap .borrow(cs)