Skip to content

Commit

Permalink
Merge pull request #7 from embassy-rs/configurable-max-chunk-size
Browse files Browse the repository at this point in the history
Allow configuring max chunk size
  • Loading branch information
Dirbaio committed Apr 24, 2024
2 parents 3ce370e + 804bc21 commit 0f8b292
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,10 @@ branching-factor-2 = [] # Default
branching-factor-3 = []
branching-factor-4 = []

max-chunk-size-256 = []
max-chunk-size-512 = []
max-chunk-size-1024 = []
max-chunk-size-2048 = []
max-chunk-size-4096 = [] # Default

# END AUTOGENERATED CONFIG FEATURES
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static CONFIGS: &[(&str, usize)] = &[
("MAX_VALUE_SIZE", 1024),
("SCRATCH_PAGE_COUNT", 4),
("BRANCHING_FACTOR", 2),
("MAX_CHUNK_SIZE", 4096),
// END AUTOGENERATED CONFIG FEATURES
];

Expand Down
1 change: 1 addition & 0 deletions gen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def feature(name, default, min=None, max=None, pow2=None, vals=None, factors=[])

feature("scratch_page_count", default=4, min=0, max=65536, pow2=True)
feature("branching_factor", default=2, min=2, max=4)
feature("max_chunk_size", default=4096, vals=[256, 512, 1024, 2048, 4096])

# ========= Update Cargo.toml

Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ pub const ERASE_VALUE: u8 = match raw::ERASE_VALUE {
_ => core::panic!("invalid ERASE_VALUE"),
};

/// Maximum chunk size supported.
///
/// The chunk size controls how big chunks of data is read and written from/to flash. A low
/// value reduces the memory usage of EKV at the expense of more reads/writes.
///
/// Default: 4096
pub const MAX_CHUNK_SIZE: usize = raw::MAX_CHUNK_SIZE;

pub(crate) const MAX_HEADER_SIZE: usize = {
let a = size_of::<MetaHeader>();
let b = size_of::<DataHeader>();
Expand Down
4 changes: 2 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct MetaHeader {
}

unsafe impl page::Header for MetaHeader {
const MAGIC: u32 = 0x1d81accb;
const MAGIC: u32 = 0x1d81bccc;
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand All @@ -48,7 +48,7 @@ pub struct DataHeader {
}

unsafe impl page::Header for DataHeader {
const MAGIC: u32 = 0x7fccf25b;
const MAGIC: u32 = 0x7fcbf25c;
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down
10 changes: 7 additions & 3 deletions src/page.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::marker::PhantomData;
use core::mem::size_of;

use crate::config::*;
use crate::config::{self, ALIGN, ERASE_VALUE, MAX_HEADER_SIZE, PAGE_SIZE};
use crate::errors::Error;
use crate::flash::Flash;
use crate::types::PageID;

const CHUNK_MAGIC: u16 = 0x58A4;
const CHUNK_MAGIC: u16 = 0x59B4;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(C)]
Expand Down Expand Up @@ -53,7 +53,11 @@ pub unsafe trait Header: Sized {
const MAGIC: u32;
}

pub const MAX_CHUNK_SIZE: usize = PAGE_SIZE - PageHeader::SIZE - ChunkHeader::SIZE;
const MAX_CHUNK_SIZE: usize = if config::MAX_CHUNK_SIZE > (PAGE_SIZE - PageHeader::SIZE - ChunkHeader::SIZE) {
PAGE_SIZE - PageHeader::SIZE - ChunkHeader::SIZE
} else {
config::MAX_CHUNK_SIZE
};

async fn write_header<F: Flash, H: Header>(flash: &mut F, page_id: PageID, header: H) -> Result<(), F::Error> {
assert!(size_of::<H>() <= MAX_HEADER_SIZE);
Expand Down

0 comments on commit 0f8b292

Please sign in to comment.