Skip to content

Commit

Permalink
add optional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kiffie committed Jun 29, 2023
1 parent d1229b0 commit 8cfb4bf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
5 changes: 0 additions & 5 deletions mips-mcu-alloc/.cargo/config

This file was deleted.

3 changes: 2 additions & 1 deletion mips-mcu-alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <kiffie@mailbox.org>"]
repository = "https://github.com/kiffie/pic32-rs/tree/master/mips-mcu-alloc"
license = "MIT OR Apache-2.0"
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mips-mcu-alloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions mips-mcu-alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! # Example
//!
//! ```
//! ```ignore
//! #![feature(global_allocator)]
//! #![feature(alloc_error_handler)]
//!
Expand All @@ -19,8 +19,6 @@
//! #[global_allocator]
//! static ALLOCATOR: MipsMcuHeap = MipsMcuHeap::empty();
//!
//! entry!(main);
//!
//! #[entry]
//! fn main() -> ! {
//! ALLOCATOR.init();
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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)
Expand Down

0 comments on commit 8cfb4bf

Please sign in to comment.