mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	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.
This commit is contained in:
		
							
								
								
									
										20
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -259,8 +259,7 @@ static int lfs_bd_sync(lfs_t *lfs) { | ||||
|  | ||||
|  | ||||
| /// Internal operations predeclared here /// | ||||
| int lfs_fs_traverse(lfs_t *lfs, | ||||
|         int (*cb)(lfs_t*, void*, lfs_block_t), void *data); | ||||
| int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); | ||||
| static int lfs_pred(lfs_t *lfs, const lfs_block_t dir[2], lfs_mdir_t *pdir); | ||||
| static int32_t lfs_parent(lfs_t *lfs, const lfs_block_t dir[2], | ||||
|         lfs_mdir_t *parent); | ||||
| @@ -272,7 +271,8 @@ int lfs_deorphan(lfs_t *lfs); | ||||
|  | ||||
|  | ||||
| /// Block allocator /// | ||||
| static int lfs_alloc_lookahead(lfs_t *lfs, void *p, lfs_block_t block) { | ||||
| static int lfs_alloc_lookahead(void *p, lfs_block_t block) { | ||||
|     lfs_t *lfs = (lfs_t*)p; | ||||
|     lfs_block_t off = ((block - lfs->free.off) | ||||
|             + lfs->cfg->block_count) % lfs->cfg->block_count; | ||||
|  | ||||
| @@ -320,7 +320,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { | ||||
|  | ||||
|         // find mask of free blocks from tree | ||||
|         memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8); | ||||
|         int err = lfs_fs_traverse(lfs, lfs_alloc_lookahead, NULL); | ||||
|         int err = lfs_fs_traverse(lfs, lfs_alloc_lookahead, lfs); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
| @@ -1768,7 +1768,7 @@ relocate: | ||||
| static int lfs_ctztraverse(lfs_t *lfs, | ||||
|         lfs_cache_t *rcache, const lfs_cache_t *pcache, | ||||
|         lfs_block_t head, lfs_size_t size, | ||||
|         int (*cb)(lfs_t*, void*, lfs_block_t), void *data) { | ||||
|         int (*cb)(void*, lfs_block_t), void *data) { | ||||
|     if (size == 0) { | ||||
|         return 0; | ||||
|     } | ||||
| @@ -1776,7 +1776,7 @@ static int lfs_ctztraverse(lfs_t *lfs, | ||||
|     lfs_off_t index = lfs_ctzindex(lfs, &(lfs_off_t){size-1}); | ||||
|  | ||||
|     while (true) { | ||||
|         int err = cb(lfs, data, head); | ||||
|         int err = cb(data, head); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
| @@ -1795,7 +1795,7 @@ static int lfs_ctztraverse(lfs_t *lfs, | ||||
|         } | ||||
|  | ||||
|         for (int i = 0; i < count-1; i++) { | ||||
|             err = cb(lfs, data, heads[i]); | ||||
|             err = cb(data, heads[i]); | ||||
|             if (err) { | ||||
|                 return err; | ||||
|             } | ||||
| @@ -3117,7 +3117,7 @@ int lfs_unmount(lfs_t *lfs) { | ||||
|  | ||||
| /// Internal filesystem filesystem operations /// | ||||
| int lfs_fs_traverse(lfs_t *lfs, | ||||
|         int (*cb)(lfs_t *lfs, void *data, lfs_block_t block), void *data) { | ||||
|         int (*cb)(void *data, lfs_block_t block), void *data) { | ||||
|     if (lfs_pairisnull(lfs->root)) { | ||||
|         return 0; | ||||
|     } | ||||
| @@ -3126,7 +3126,7 @@ int lfs_fs_traverse(lfs_t *lfs, | ||||
|     lfs_mdir_t dir = {.tail = {0, 1}}; | ||||
|     while (!lfs_pairisnull(dir.tail)) { | ||||
|         for (int i = 0; i < 2; i++) { | ||||
|             int err = cb(lfs, data, dir.tail[i]); | ||||
|             int err = cb(data, dir.tail[i]); | ||||
|             if (err) { | ||||
|                 return err; | ||||
|             } | ||||
| @@ -3542,7 +3542,7 @@ int lfs_deorphan(lfs_t *lfs) { | ||||
| //} | ||||
|  | ||||
| // TODO need lfs? | ||||
| static int lfs_fs_size_count(lfs_t *lfs, void *p, lfs_block_t block) { | ||||
| static int lfs_fs_size_count(void *p, lfs_block_t block) { | ||||
|     lfs_size_t *size = p; | ||||
|     *size += 1; | ||||
|     return 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user