Skip to content

Commit

Permalink
counters: add "counters-disabled" feature flag
Browse files Browse the repository at this point in the history
The `ringbuf` crate has a `"disabled` feature flag, which disables ring
buffers, intended for use on memory-constrained targets. Currently,
setting the `"disabled"` feature flag still enables counters for ring
buffers declared using the `counted_ringbuf!` macro, because counters
generally require less RAM than full ringbufs. However, on some targets,
even counters are too large, especially when a ringbuf's entry type has
too many variants (e.g. see [this comment][1] from @bcantrill --- the
`i2c_driver` ringbuf must disable counters in order to fit in Donglet's
RAM).

Therefore, this commit adds an additional `"counters-disabled"` feature
flag to the `ringbuf` crate, to allow disabling counters as well as ring
buffers.

[1]: #1657 (comment)
  • Loading branch information
hawkw committed Mar 14, 2024
1 parent d226a73 commit 42b8b1e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/ringbuf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ edition = "2021"
# To disable a ring buffer (but leave it otherwise present), enable the
# "disabled" feature
disabled = []
# To disable counters as well as ring buffers, enable the "counters-disabled"
# feature.
counters-disabled = []
default = ["counters"]

[dependencies]
Expand Down
54 changes: 51 additions & 3 deletions lib/ringbuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@
//!
//! Entry variant counts are recorded even when this crate is compiled with the
//! "disabled" feature flag set. This allows targets which lack the memory for
//! an entire ring buffer to still record event counts.
//! an entire ring buffer to still record event counts. To disable counting ring
//! buffer entries, set the "counters-disabled" feature flag. This feature may
//! be set independently of the "disabled" feature: if both are set, neither
//! ring buffer entries nor counters will be recorded; if *only*
//! "counters-disabled" is set, the [`counted_ringbuf!`] macro will behave
//! identically to the [`ringbuf!`] macro, recording ringbuf entries but not
//! counters.
//!
//! To use the [`counted_ringbuf!`] macro, the entry type must be
//! an `enum`, and it must implement the [`counters::Count`] trait, which
Expand Down Expand Up @@ -264,7 +270,11 @@ macro_rules! ringbuf {
/// To support the common case of having one quickly-installed ringbuffer per
/// module, if you omit the name, it will default to `__RINGBUF`.
///
#[cfg(all(not(feature = "disabled"), feature = "counters"))]
#[cfg(all(
not(feature = "disabled"),
not(feature = "counters-disabled"),
feature = "counters"
))]
#[macro_export]
macro_rules! counted_ringbuf {
($name:ident, $t:ident, $n:expr, $init:expr) => {
Expand All @@ -287,7 +297,11 @@ macro_rules! counted_ringbuf {
};
}

#[cfg(all(feature = "counters", feature = "disabled"))]
#[cfg(all(
feature = "counters",
not(feature = "counters-disabled"),
feature = "disabled"
))]
#[macro_export]
macro_rules! counted_ringbuf {
($name:ident, $t:ident, $n:expr, $init:expr) => {
Expand All @@ -301,6 +315,40 @@ macro_rules! counted_ringbuf {
};
}

#[cfg(all(
feature = "counters",
feature = "counters-disabled",
not(feature = "disabled")
))]
#[macro_export]
macro_rules! counted_ringbuf {
($name:ident, $t:ident, $n:expr, $init:expr) => {
#[allow(dead_code)]
const _: $t = $init;
static $name: () = ();
};
($t:ident, $n:expr, $init:expr) => {
$crate::ringbuf!(__RINGBUF, $t, $n, $init);
};
}

#[cfg(all(
feature = "counters",
feature = "counters-disabled",
feature = "disabled"
))]
#[macro_export]
macro_rules! counted_ringbuf {
($name:ident, $t:ident, $n:expr, $init:expr) => {
#[allow(dead_code)]
const _: $t = $init;
static $name: () = ();
};
($t:ident, $n:expr, $init:expr) => {
$crate::counted_ringbuf!(__RINGBUF, $t, $n, $init);
};
}

/// Inserts data into a named ringbuffer (which should have been declared with
/// the [`ringbuf!`] or [`counted_ringbuf!`] macro).
///
Expand Down

0 comments on commit 42b8b1e

Please sign in to comment.