Skip to content

Commit

Permalink
Implement defmt::Format for HeapStats and RegionStats
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyGrondin committed Sep 17, 2024
1 parent e9da803 commit 62b1953
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions esp-alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ default-target = "riscv32imc-unknown-none-elf"
features = ["nightly"]

[dependencies]
defmt = { version = "0.3.8", optional = true }
cfg-if = "1.0.0"
critical-section = "1.1.3"
enumset = "1.1.5"
Expand All @@ -33,6 +34,9 @@ document-features = "0.2.10"
default = []
nightly = []

## Implement `defmt::Format` on certain types.
defmt = ["dep:defmt"]

## Enable this feature if you want to keep stats about the internal heap usage such as:
## - Max memory usage since initialization of the heap
## - Total allocated memory since initialization of the heap
Expand Down
14 changes: 14 additions & 0 deletions esp-alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ impl Display for RegionStats {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for RegionStats {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{}", defmt::Display2Format(self))
}
}

/// A memory region to be used as heap memory
pub struct HeapRegion {
heap: Heap,
Expand Down Expand Up @@ -253,6 +260,13 @@ impl Display for HeapStats {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for HeapStats {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{}", defmt::Display2Format(self))
}
}

/// Internal stats to keep track across multiple regions.
#[cfg(feature = "internal-heap-stats")]
struct InternalHeapStats {
Expand Down

0 comments on commit 62b1953

Please sign in to comment.