Skip to content

Commit 0f03b3f

Browse files
allow to configure file/read/write cache separate size
1 parent 75fed00 commit 0f03b3f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lfs.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
9494

9595
static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
9696
// zero to avoid information leak
97-
memset(pcache->buffer, 0xff, lfs->cfg->cache_size);
97+
memset(pcache->buffer, 0xff, lfs->prog_cache_size);
9898
pcache->block = LFS_BLOCK_NULL;
9999
}
100100

@@ -178,7 +178,7 @@ static int lfs_bd_read_raw(lfs_t *lfs,
178178
if (reverse) {
179179
lfs_size_t msize;
180180
moff = off + size;
181-
msize = lfs_min(hint, lfs->cfg->cache_size);
181+
msize = lfs_min(hint, lfs->read_cache_size);
182182
if (moff > msize)
183183
moff = lfs_alignup(moff - msize, lfs->cfg->read_size);
184184
else
@@ -190,7 +190,7 @@ static int lfs_bd_read_raw(lfs_t *lfs,
190190
lfs_alignup(moff+hint, lfs->cfg->read_size),
191191
lfs->cfg->block_size)
192192
- rcache->off,
193-
lfs->cfg->cache_size);
193+
lfs->read_cache_size);
194194
int err = lfs->cfg->read(lfs->cfg, rcache->block,
195195
rcache->off, rcache->buffer, rcache->size);
196196
LFS_ASSERT(err <= 0);
@@ -331,18 +331,18 @@ static int lfs_bd_prog(lfs_t *lfs,
331331
while (size > 0) {
332332
if (block == pcache->block &&
333333
off >= pcache->off &&
334-
off < pcache->off + lfs->cfg->cache_size) {
334+
off < pcache->off + lfs->prog_cache_size) {
335335
// already fits in pcache?
336336
lfs_size_t diff = lfs_min(size,
337-
lfs->cfg->cache_size - (off-pcache->off));
337+
lfs->prog_cache_size - (off-pcache->off));
338338
memcpy(&pcache->buffer[off-pcache->off], data, diff);
339339

340340
data += diff;
341341
off += diff;
342342
size -= diff;
343343

344344
pcache->size = lfs_max(pcache->size, off - pcache->off);
345-
if (pcache->size == lfs->cfg->cache_size) {
345+
if (pcache->size == lfs->prog_cache_size) {
346346
// eagerly flush out pcache if we fill up
347347
int err = lfs_bd_flush(lfs, pcache, rcache, validate);
348348
if (err) {
@@ -357,7 +357,7 @@ static int lfs_bd_prog(lfs_t *lfs,
357357
// entire block or manually flushing the pcache
358358
LFS_ASSERT(pcache->block == LFS_BLOCK_NULL);
359359

360-
if (size >= lfs->cfg->cache_size) {
360+
if (size >= lfs->prog_cache_size) {
361361
/* data do not fit in cache, direct write */
362362
int err = lfs_bd_flush_direct(lfs, rcache, block, off,
363363
data, size, validate);
@@ -931,7 +931,7 @@ static int lfs_dir_getread(lfs_t *lfs, const lfs_mdir_t *dir,
931931
rcache->block = LFS_BLOCK_INLINE;
932932
rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
933933
rcache->size = lfs_min(lfs_alignup(off+hint, lfs->cfg->read_size),
934-
lfs->cfg->cache_size);
934+
lfs->read_cache_size);
935935
int err = lfs_dir_getslice(lfs, dir, gmask, gtag,
936936
rcache->off, rcache->buffer, rcache->size);
937937
if (err < 0) {
@@ -1845,7 +1845,7 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
18451845

18461846
// manually flush here since we don't prog the padding, this confuses
18471847
// the caching layer
1848-
if (noff >= end || noff >= lfs->pcache.off + lfs->cfg->cache_size) {
1848+
if (noff >= end || noff >= lfs->pcache.off + lfs->prog_cache_size) {
18491849
// flush buffers
18501850
int err = lfs_bd_sync(lfs, &lfs->pcache, &lfs->rcache, false);
18511851
if (err) {
@@ -4279,17 +4279,30 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
42794279
// which littlefs currently does not support
42804280
LFS_ASSERT((bool)0x80000000);
42814281

4282+
if (!lfs->cfg->read_cache_size)
4283+
lfs->read_cache_size = lfs->cfg->cache_size;
4284+
else
4285+
lfs->read_cache_size = lfs->cfg->read_cache_size;
4286+
if (!lfs->cfg->prog_cache_size)
4287+
lfs->prog_cache_size = lfs->cfg->cache_size;
4288+
else
4289+
lfs->prog_cache_size = lfs->cfg->prog_cache_size;
4290+
42824291
// validate that the lfs-cfg sizes were initiated properly before
42834292
// performing any arithmetic logics with them
42844293
LFS_ASSERT(lfs->cfg->read_size != 0);
42854294
LFS_ASSERT(lfs->cfg->prog_size != 0);
4295+
LFS_ASSERT(lfs->read_cache_size != 0);
4296+
LFS_ASSERT(lfs->prog_cache_size != 0);
42864297
LFS_ASSERT(lfs->cfg->cache_size != 0);
42874298

42884299
// check that block size is a multiple of cache size is a multiple
42894300
// of prog and read sizes
4290-
LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->read_size == 0);
4291-
LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->prog_size == 0);
4301+
LFS_ASSERT(lfs->read_cache_size % lfs->cfg->read_size == 0);
4302+
LFS_ASSERT(lfs->prog_cache_size % lfs->cfg->prog_size == 0);
42924303
LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->cache_size == 0);
4304+
LFS_ASSERT(lfs->cfg->block_size % lfs->read_cache_size == 0);
4305+
LFS_ASSERT(lfs->cfg->block_size % lfs->prog_cache_size == 0);
42934306

42944307
// check that the block size is large enough to fit all ctz pointers
42954308
LFS_ASSERT(lfs->cfg->block_size >= 128);
@@ -4319,7 +4332,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
43194332
if (lfs->cfg->read_buffer) {
43204333
lfs->rcache.buffer = lfs->cfg->read_buffer;
43214334
} else {
4322-
lfs->rcache.buffer = lfs_malloc(lfs->cfg->cache_size);
4335+
lfs->rcache.buffer = lfs_malloc(lfs->read_cache_size);
43234336
if (!lfs->rcache.buffer) {
43244337
err = LFS_ERR_NOMEM;
43254338
goto cleanup;
@@ -4330,7 +4343,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
43304343
if (lfs->cfg->prog_buffer) {
43314344
lfs->pcache.buffer = lfs->cfg->prog_buffer;
43324345
} else {
4333-
lfs->pcache.buffer = lfs_malloc(lfs->cfg->cache_size);
4346+
lfs->pcache.buffer = lfs_malloc(lfs->prog_cache_size);
43344347
if (!lfs->pcache.buffer) {
43354348
err = LFS_ERR_NOMEM;
43364349
goto cleanup;

lfs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ struct lfs_config {
222222
// data and reducing the number of disk accesses. Must be a multiple of the
223223
// read and program sizes, and a factor of the block size.
224224
lfs_size_t cache_size;
225+
lfs_size_t read_cache_size;
226+
lfs_size_t prog_cache_size;
225227

226228
// Size of the lookahead buffer in bytes. A larger lookahead buffer
227229
// increases the number of blocks found during an allocation pass. The
@@ -435,6 +437,8 @@ typedef struct lfs_gstate {
435437
typedef struct lfs {
436438
lfs_cache_t rcache;
437439
lfs_cache_t pcache;
440+
lfs_size_t read_cache_size;
441+
lfs_size_t prog_cache_size;
438442

439443
lfs_block_t root[2];
440444
struct lfs_mlist {

0 commit comments

Comments
 (0)