Skip to content

Commit

Permalink
New API function lfs_find_free_blocks. It performs free block search …
Browse files Browse the repository at this point in the history
…which later will not slow down a write operation.
  • Loading branch information
opilat committed Nov 8, 2021
1 parent ead5080 commit f82f34c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,25 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
}
}
}

int lfs_find_free_blocks(lfs_t *lfs){
// check if we have looked at all blocks since last ack
if (lfs->free.ack == 0) {
LFS_ERROR("No more free space %"PRIu32,
lfs->free.i + lfs->free.off);
return LFS_ERR_NOSPC;
}

lfs->free.off = (lfs->free.off + lfs->free.size)
% lfs->cfg->block_count;
lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size, lfs->free.ack);
lfs->free.i = 0;

// find mask of free blocks from tree
memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size);
int err = lfs_fs_traverse(lfs, lfs_alloc_lookahead, lfs);
return err;
}
#endif

/// Metadata pair and directory operations ///
Expand Down
4 changes: 4 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs);
// Returns a negative error code on failure.
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);

// Use Traverse function and try to find free blocks. LittleFS free blocks search is unpredictable.
// Search is costly operation which may delay write. In realtime write scenarios can be better to find them before a write.
int lfs_find_free_blocks(lfs_t *lfs);

#ifndef LFS_READONLY
#ifdef LFS_MIGRATE
// Attempts to migrate a previous version of littlefs
Expand Down

0 comments on commit f82f34c

Please sign in to comment.