Skip to content

Commit df1b607

Browse files
committed
Removed the implicit lfs_t parameter to lfs_traverse
This is a very minor thing but it has been bugging me. On one hand, all a callback ever needs is a single pointer for context. On the other hand, you could make the argument that in the context of littlefs, the lfs_t struct represents global state and should always be available to callbacks passed to littlefs. In the end I'm sticking with only a single context pointer, since this is satisfies the minimum requirements and has the highest chance of function reuse. If a user needs access to the lfs_t struct, it can be passed by reference in the context provided to the callback. This also matches callbacks used in other languages with more emphasis on objects and classes. Usually the callback doesn't get a reference to the caller.
1 parent 2257060 commit df1b607

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lfs.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ static int lfs_bd_sync(lfs_t *lfs) {
259259

260260

261261
/// Internal operations predeclared here ///
262-
int lfs_fs_traverse(lfs_t *lfs,
263-
int (*cb)(lfs_t*, void*, lfs_block_t), void *data);
262+
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
264263
static int lfs_pred(lfs_t *lfs, const lfs_block_t dir[2], lfs_mdir_t *pdir);
265264
static int32_t lfs_parent(lfs_t *lfs, const lfs_block_t dir[2],
266265
lfs_mdir_t *parent);
@@ -272,7 +271,8 @@ int lfs_deorphan(lfs_t *lfs);
272271

273272

274273
/// Block allocator ///
275-
static int lfs_alloc_lookahead(lfs_t *lfs, void *p, lfs_block_t block) {
274+
static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
275+
lfs_t *lfs = (lfs_t*)p;
276276
lfs_block_t off = ((block - lfs->free.off)
277277
+ lfs->cfg->block_count) % lfs->cfg->block_count;
278278

@@ -320,7 +320,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
320320

321321
// find mask of free blocks from tree
322322
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
323-
int err = lfs_fs_traverse(lfs, lfs_alloc_lookahead, NULL);
323+
int err = lfs_fs_traverse(lfs, lfs_alloc_lookahead, lfs);
324324
if (err) {
325325
return err;
326326
}
@@ -1768,15 +1768,15 @@ static int lfs_ctzextend(lfs_t *lfs,
17681768
static int lfs_ctztraverse(lfs_t *lfs,
17691769
lfs_cache_t *rcache, const lfs_cache_t *pcache,
17701770
lfs_block_t head, lfs_size_t size,
1771-
int (*cb)(lfs_t*, void*, lfs_block_t), void *data) {
1771+
int (*cb)(void*, lfs_block_t), void *data) {
17721772
if (size == 0) {
17731773
return 0;
17741774
}
17751775

17761776
lfs_off_t index = lfs_ctzindex(lfs, &(lfs_off_t){size-1});
17771777

17781778
while (true) {
1779-
int err = cb(lfs, data, head);
1779+
int err = cb(data, head);
17801780
if (err) {
17811781
return err;
17821782
}
@@ -1795,7 +1795,7 @@ static int lfs_ctztraverse(lfs_t *lfs,
17951795
}
17961796

17971797
for (int i = 0; i < count-1; i++) {
1798-
err = cb(lfs, data, heads[i]);
1798+
err = cb(data, heads[i]);
17991799
if (err) {
18001800
return err;
18011801
}
@@ -3117,7 +3117,7 @@ int lfs_unmount(lfs_t *lfs) {
31173117

31183118
/// Internal filesystem filesystem operations ///
31193119
int lfs_fs_traverse(lfs_t *lfs,
3120-
int (*cb)(lfs_t *lfs, void *data, lfs_block_t block), void *data) {
3120+
int (*cb)(void *data, lfs_block_t block), void *data) {
31213121
if (lfs_pairisnull(lfs->root)) {
31223122
return 0;
31233123
}
@@ -3126,7 +3126,7 @@ int lfs_fs_traverse(lfs_t *lfs,
31263126
lfs_mdir_t dir = {.tail = {0, 1}};
31273127
while (!lfs_pairisnull(dir.tail)) {
31283128
for (int i = 0; i < 2; i++) {
3129-
int err = cb(lfs, data, dir.tail[i]);
3129+
int err = cb(data, dir.tail[i]);
31303130
if (err) {
31313131
return err;
31323132
}
@@ -3542,7 +3542,7 @@ int lfs_deorphan(lfs_t *lfs) {
35423542
//}
35433543

35443544
// TODO need lfs?
3545-
static int lfs_fs_size_count(lfs_t *lfs, void *p, lfs_block_t block) {
3545+
static int lfs_fs_size_count(void *p, lfs_block_t block) {
35463546
lfs_size_t *size = p;
35473547
*size += 1;
35483548
return 0;

lfs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs);
625625
// blocks are in use or how much of the storage is available.
626626
//
627627
// Returns a negative error code on failure.
628-
// TODO don't pass lfs_t?
629-
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(lfs_t*, void*, lfs_block_t), void *data);
628+
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
630629

631630

632631
#endif

0 commit comments

Comments
 (0)