Skip to content

Commit 1b083d1

Browse files
authored
Merge pull request #57 from tweedegolf/queue-size-report
Add function for queue space left
2 parents 61194c5 + dc340b4 commit 1b083d1

File tree

6 files changed

+189
-39
lines changed

6 files changed

+189
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Unreleased
66

7+
- Added `space_left` function for queue
8+
79
# 2.0.2 07-05-24
810

911
- Added check for too big items that won't ever fit in flash so it returns a good clear error.

fuzz/fuzz_targets/map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
8686

8787
for op in ops.ops.into_iter() {
8888
#[cfg(fuzzing_repro)]
89-
eprintln!("{}", flash.print_items());
89+
eprintln!("{}", block_on(flash.print_items()));
9090
#[cfg(fuzzing_repro)]
9191
eprintln!("{:?}", cache);
9292
#[cfg(fuzzing_repro)]
@@ -120,14 +120,14 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
120120
)) {
121121
Ok(Some(check_item)) if check_item == value => {
122122
#[cfg(fuzzing_repro)]
123-
eprintln!("Early shutoff when storing {item:?}! (but it still stored fully). Originated from:\n{_backtrace:#}");
123+
eprintln!("Early shutoff when storing key: {key}, value: {value:?}! (but it still stored fully). Originated from:\n{_backtrace:#}");
124124
// Even though we got a shutoff, it still managed to store well
125125
map.insert(key, value);
126126
}
127127
_ => {
128128
// Could not fetch the item we stored...
129129
#[cfg(fuzzing_repro)]
130-
eprintln!("Early shutoff when storing {item:?}! Originated from:\n{_backtrace:#}");
130+
eprintln!("Early shutoff when storing key: {key}, value: {value:?}! Originated from:\n{_backtrace:#}");
131131
}
132132
}
133133
}

fuzz/fuzz_targets/queue.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn fuzz(ops: Input, mut cache: impl CacheImpl + Debug) {
7171

7272
for mut op in ops.ops.into_iter() {
7373
#[cfg(fuzzing_repro)]
74-
eprintln!("{}", flash.print_items());
74+
eprintln!("{}", block_on(flash.print_items()));
7575
#[cfg(fuzzing_repro)]
7676
eprintln!("{:?}", cache);
7777
#[cfg(fuzzing_repro)]
@@ -128,7 +128,7 @@ fn fuzz(ops: Input, mut cache: impl CacheImpl + Debug) {
128128
backtrace: _backtrace,
129129
}) => {
130130
// We need to check if it managed to write
131-
if let Some(true) = flash.get_item_presence(address) {
131+
if let Some(true) = block_on(flash.get_item_presence(address)) {
132132
#[cfg(fuzzing_repro)]
133133
eprintln!("Early shutoff when pushing {val:?}! (but it still stored fully). Originated from:\n{_backtrace:#}");
134134
order.push_back(val);
@@ -172,7 +172,7 @@ fn fuzz(ops: Input, mut cache: impl CacheImpl + Debug) {
172172
"Early shutoff when popping (single)! Originated from:\n{_backtrace:#}"
173173
);
174174

175-
if !matches!(flash.get_item_presence(address), Some(true)) {
175+
if !matches!(block_on(flash.get_item_presence(address)), Some(true)) {
176176
// The item is no longer readable here
177177
order.pop_front();
178178
}
@@ -276,7 +276,10 @@ fn fuzz(ops: Input, mut cache: impl CacheImpl + Debug) {
276276
"Early shutoff when popping iterator entry! Originated from:\n{_backtrace:#}"
277277
);
278278

279-
if !matches!(flash.get_item_presence(address), Some(true)) {
279+
if !matches!(
280+
block_on(flash.get_item_presence(address)),
281+
Some(true)
282+
) {
280283
// The item is no longer readable here
281284
order.remove(i - popped_items).unwrap();
282285
}

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ pub async fn erase_all<S: NorFlash>(
4949
})
5050
}
5151

52+
/// Get the minimal overhead size per stored item for the given flash type.
53+
///
54+
/// The associated data of each item is additionally padded to a full flash word size, but that's not part of this number.
55+
/// This means the full item length is `returned number + (data length).next_multiple_of(S::WORD_SIZE)`.
56+
pub const fn item_overhead_size<S: NorFlash>() -> u32 {
57+
item::ItemHeader::data_address::<S>(0)
58+
}
59+
5260
// Type representing buffer aligned to 4 byte boundary.
5361
#[repr(align(4))]
5462
pub(crate) struct AlignedBuf<const SIZE: usize>(pub(crate) [u8; SIZE]);

src/mock_flash.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
113113
self.current_stats
114114
}
115115

116-
#[cfg(feature = "_test")]
116+
#[cfg(any(test, feature = "_test"))]
117117
/// Print all items in flash to the returned string
118-
pub fn print_items(&mut self) -> String {
118+
pub async fn print_items(&mut self) -> String {
119119
use crate::cache::NoCache;
120120
use crate::NorFlashExt;
121-
use futures::executor::block_on;
122121
use std::fmt::Write;
123122

124123
let mut buf = [0; 1024 * 16];
@@ -132,12 +131,14 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
132131
writeln!(
133132
s,
134133
" Page {page_index} ({}):",
135-
match block_on(crate::get_page_state(
134+
match crate::get_page_state(
136135
self,
137136
Self::FULL_FLASH_RANGE,
138137
&mut NoCache::new(),
139138
page_index
140-
)) {
139+
)
140+
.await
141+
{
141142
Ok(value) => format!("{value:?}"),
142143
Err(e) => format!("Error ({e:?})"),
143144
}
@@ -151,13 +152,13 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
151152
- Self::WORD_SIZE as u32;
152153

153154
let mut it = crate::item::ItemHeaderIter::new(page_data_start, page_data_end);
154-
while let (Some(header), item_address) =
155-
block_on(it.traverse(self, |_, _| false)).unwrap()
155+
while let (Some(header), item_address) = it.traverse(self, |_, _| false).await.unwrap()
156156
{
157157
let next_item_address = header.next_item_address::<Self>(item_address);
158-
let maybe_item =
159-
block_on(header.read_item(self, &mut buf, item_address, page_data_end))
160-
.unwrap();
158+
let maybe_item = header
159+
.read_item(self, &mut buf, item_address, page_data_end)
160+
.await
161+
.unwrap();
161162
writeln!(
162163
s,
163164
" Item {maybe_item:?} at {item_address}..{next_item_address}"
@@ -169,15 +170,14 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
169170
s
170171
}
171172

172-
#[cfg(feature = "_test")]
173+
#[cfg(any(test, feature = "_test"))]
173174
/// Get the presence of the item at the given address.
174175
///
175176
/// - If some, the item is there.
176177
/// - If true, the item is present and fine.
177178
/// - If false, the item is corrupt or erased.
178-
pub fn get_item_presence(&mut self, target_item_address: u32) -> Option<bool> {
179+
pub async fn get_item_presence(&mut self, target_item_address: u32) -> Option<bool> {
179180
use crate::NorFlashExt;
180-
use futures::executor::block_on;
181181

182182
if !Self::FULL_FLASH_RANGE.contains(&target_item_address) {
183183
return None;
@@ -197,14 +197,14 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
197197

198198
let mut found_item = None;
199199
let mut it = crate::item::ItemHeaderIter::new(page_data_start, page_data_end);
200-
while let (Some(header), item_address) = block_on(it.traverse(self, |_, _| false)).unwrap()
201-
{
200+
while let (Some(header), item_address) = it.traverse(self, |_, _| false).await.unwrap() {
202201
let next_item_address = header.next_item_address::<Self>(item_address);
203202

204203
if (item_address..next_item_address).contains(&target_item_address) {
205-
let maybe_item =
206-
block_on(header.read_item(self, &mut buf, item_address, page_data_end))
207-
.unwrap();
204+
let maybe_item = header
205+
.read_item(self, &mut buf, item_address, page_data_end)
206+
.await
207+
.unwrap();
208208

209209
match maybe_item {
210210
crate::item::MaybeItem::Corrupted(_, _)

0 commit comments

Comments
 (0)