Skip to content

Commit a3fd2d4

Browse files
committed
Added more configurable utils
Note: It's still expected to modify lfs_utils.h when porting littlefs to a new target/system. There's just too much room for system-specific improvements, such as taking advantage of CRC hardware. Rather, encouraging modification of lfs_util.h and making it easy to modify and debug should result in better integration with the consuming systems. This just adds a bunch of quality-of-life improvements that should help development and integration in littlefs. - Macros that require no side-effects are all-caps - System includes are only brought in when needed - Malloc/free wrappers - LFS_NO_* checks for quickly disabling things at the command line - At least a little-bit more docs
1 parent a0a55fb commit a3fd2d4

File tree

3 files changed

+103
-43
lines changed

3 files changed

+103
-43
lines changed

lfs.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818
#include "lfs.h"
1919
#include "lfs_util.h"
2020

21-
#include <string.h>
22-
#include <stdlib.h>
23-
#include <assert.h>
24-
2521

2622
/// Caching block device operations ///
2723
static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache,
2824
const lfs_cache_t *pcache, lfs_block_t block,
2925
lfs_off_t off, void *buffer, lfs_size_t size) {
3026
uint8_t *data = buffer;
31-
assert(block < lfs->cfg->block_count);
27+
LFS_ASSERT(block < lfs->cfg->block_count);
3228

3329
while (size > 0) {
3430
if (pcache && block == pcache->block && off >= pcache->off &&
@@ -153,7 +149,7 @@ static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *pcache,
153149
lfs_cache_t *rcache, lfs_block_t block,
154150
lfs_off_t off, const void *buffer, lfs_size_t size) {
155151
const uint8_t *data = buffer;
156-
assert(block < lfs->cfg->block_count);
152+
LFS_ASSERT(block < lfs->cfg->block_count);
157153

158154
while (size > 0) {
159155
if (block == pcache->block && off >= pcache->off &&
@@ -180,7 +176,7 @@ static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *pcache,
180176

181177
// pcache must have been flushed, either by programming and
182178
// entire block or manually flushing the pcache
183-
assert(pcache->block == 0xffffffff);
179+
LFS_ASSERT(pcache->block == 0xffffffff);
184180

185181
if (off % lfs->cfg->prog_size == 0 &&
186182
size >= lfs->cfg->prog_size) {
@@ -1130,7 +1126,7 @@ static int lfs_ctz_find(lfs_t *lfs,
11301126
return err;
11311127
}
11321128

1133-
assert(head >= 2 && head <= lfs->cfg->block_count);
1129+
LFS_ASSERT(head >= 2 && head <= lfs->cfg->block_count);
11341130
current -= 1 << skip;
11351131
}
11361132

@@ -1150,7 +1146,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
11501146
if (err) {
11511147
return err;
11521148
}
1153-
assert(nblock >= 2 && nblock <= lfs->cfg->block_count);
1149+
LFS_ASSERT(nblock >= 2 && nblock <= lfs->cfg->block_count);
11541150

11551151
if (true) {
11561152
err = lfs_bd_erase(lfs, nblock);
@@ -1221,7 +1217,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
12211217
}
12221218
}
12231219

1224-
assert(head >= 2 && head <= lfs->cfg->block_count);
1220+
LFS_ASSERT(head >= 2 && head <= lfs->cfg->block_count);
12251221
}
12261222

12271223
*block = nblock;
@@ -1347,12 +1343,12 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
13471343
if (lfs->cfg->file_buffer) {
13481344
file->cache.buffer = lfs->cfg->file_buffer;
13491345
} else if ((file->flags & 3) == LFS_O_RDONLY) {
1350-
file->cache.buffer = malloc(lfs->cfg->read_size);
1346+
file->cache.buffer = lfs_malloc(lfs->cfg->read_size);
13511347
if (!file->cache.buffer) {
13521348
return LFS_ERR_NOMEM;
13531349
}
13541350
} else {
1355-
file->cache.buffer = malloc(lfs->cfg->prog_size);
1351+
file->cache.buffer = lfs_malloc(lfs->cfg->prog_size);
13561352
if (!file->cache.buffer) {
13571353
return LFS_ERR_NOMEM;
13581354
}
@@ -1378,7 +1374,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
13781374

13791375
// clean up memory
13801376
if (!lfs->cfg->file_buffer) {
1381-
free(file->cache.buffer);
1377+
lfs_free(file->cache.buffer);
13821378
}
13831379

13841380
return err;
@@ -1527,7 +1523,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
15271523
return err;
15281524
}
15291525

1530-
assert(entry.d.type == LFS_TYPE_REG);
1526+
LFS_ASSERT(entry.d.type == LFS_TYPE_REG);
15311527
entry.d.u.file.head = file->head;
15321528
entry.d.u.file.size = file->size;
15331529

@@ -1753,7 +1749,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
17531749

17541750
// flush+seek if not already at end
17551751
if (file->pos != oldsize) {
1756-
int err = lfs_file_seek(lfs, file, 0, SEEK_END);
1752+
int err = lfs_file_seek(lfs, file, 0, LFS_SEEK_END);
17571753
if (err < 0) {
17581754
return err;
17591755
}
@@ -1886,7 +1882,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
18861882
return res;
18871883
}
18881884

1889-
assert(res); // must have pred
1885+
LFS_ASSERT(res); // must have pred
18901886
cwd.d.tail[0] = dir.d.tail[0];
18911887
cwd.d.tail[1] = dir.d.tail[1];
18921888

@@ -2003,7 +1999,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
20031999
return res;
20042000
}
20052001

2006-
assert(res); // must have pred
2002+
LFS_ASSERT(res); // must have pred
20072003
newcwd.d.tail[0] = dir.d.tail[0];
20082004
newcwd.d.tail[1] = dir.d.tail[1];
20092005

@@ -2026,7 +2022,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
20262022
if (lfs->cfg->read_buffer) {
20272023
lfs->rcache.buffer = lfs->cfg->read_buffer;
20282024
} else {
2029-
lfs->rcache.buffer = malloc(lfs->cfg->read_size);
2025+
lfs->rcache.buffer = lfs_malloc(lfs->cfg->read_size);
20302026
if (!lfs->rcache.buffer) {
20312027
return LFS_ERR_NOMEM;
20322028
}
@@ -2037,30 +2033,30 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
20372033
if (lfs->cfg->prog_buffer) {
20382034
lfs->pcache.buffer = lfs->cfg->prog_buffer;
20392035
} else {
2040-
lfs->pcache.buffer = malloc(lfs->cfg->prog_size);
2036+
lfs->pcache.buffer = lfs_malloc(lfs->cfg->prog_size);
20412037
if (!lfs->pcache.buffer) {
20422038
return LFS_ERR_NOMEM;
20432039
}
20442040
}
20452041

20462042
// setup lookahead, round down to nearest 32-bits
2047-
assert(lfs->cfg->lookahead % 32 == 0);
2048-
assert(lfs->cfg->lookahead > 0);
2043+
LFS_ASSERT(lfs->cfg->lookahead % 32 == 0);
2044+
LFS_ASSERT(lfs->cfg->lookahead > 0);
20492045
if (lfs->cfg->lookahead_buffer) {
20502046
lfs->free.buffer = lfs->cfg->lookahead_buffer;
20512047
} else {
2052-
lfs->free.buffer = malloc(lfs->cfg->lookahead/8);
2048+
lfs->free.buffer = lfs_malloc(lfs->cfg->lookahead/8);
20532049
if (!lfs->free.buffer) {
20542050
return LFS_ERR_NOMEM;
20552051
}
20562052
}
20572053

20582054
// check that program and read sizes are multiples of the block size
2059-
assert(lfs->cfg->prog_size % lfs->cfg->read_size == 0);
2060-
assert(lfs->cfg->block_size % lfs->cfg->prog_size == 0);
2055+
LFS_ASSERT(lfs->cfg->prog_size % lfs->cfg->read_size == 0);
2056+
LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->prog_size == 0);
20612057

20622058
// check that the block size is large enough to fit ctz pointers
2063-
assert(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
2059+
LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
20642060
<= lfs->cfg->block_size);
20652061

20662062
// setup default state
@@ -2076,15 +2072,15 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
20762072
static int lfs_deinit(lfs_t *lfs) {
20772073
// free allocated memory
20782074
if (!lfs->cfg->read_buffer) {
2079-
free(lfs->rcache.buffer);
2075+
lfs_free(lfs->rcache.buffer);
20802076
}
20812077

20822078
if (!lfs->cfg->prog_buffer) {
2083-
free(lfs->pcache.buffer);
2079+
lfs_free(lfs->pcache.buffer);
20842080
}
20852081

20862082
if (!lfs->cfg->lookahead_buffer) {
2087-
free(lfs->free.buffer);
2083+
lfs_free(lfs->free.buffer);
20882084
}
20892085

20902086
return 0;

lfs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// Software library version
2828
// Major (top-nibble), incremented on backwards incompatible changes
2929
// Minor (bottom-nibble), incremented on feature additions
30-
#define LFS_VERSION 0x00010002
30+
#define LFS_VERSION 0x00010003
3131
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
3232
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
3333

lfs_util.h

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,60 @@
1818
#ifndef LFS_UTIL_H
1919
#define LFS_UTIL_H
2020

21-
#include <stdlib.h>
2221
#include <stdint.h>
22+
#include <stdbool.h>
23+
#include <string.h>
24+
25+
#ifndef LFS_NO_MALLOC
26+
#include <stdlib.h>
27+
#endif
28+
#ifndef LFS_NO_ASSERT
29+
#include <assert.h>
30+
#endif
31+
#if !defined(LFS_NO_DEBUG) || !defined(LFS_NO_WARN) || !defined(LFS_NO_ERROR)
2332
#include <stdio.h>
33+
#endif
34+
35+
36+
// Macros, may be replaced by system specific wrappers. Arguments to these
37+
// macros must not have side-effects as the macros can be removed for a smaller
38+
// code footprint
39+
40+
// Logging functions
41+
#ifndef LFS_NO_DEBUG
42+
#define LFS_DEBUG(fmt, ...) \
43+
printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
44+
#else
45+
#define LFS_DEBUG(fmt, ...)
46+
#endif
47+
48+
#ifndef LFS_NO_WARN
49+
#define LFS_WARN(fmt, ...) \
50+
printf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
51+
#else
52+
#define LFS_WARN(fmt, ...)
53+
#endif
54+
55+
#ifndef LFS_NO_ERROR
56+
#define LFS_ERROR(fmt, ...) \
57+
printf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
58+
#else
59+
#define LFS_ERROR(fmt, ...)
60+
#endif
61+
62+
// Runtime assertions
63+
#ifndef LFS_NO_ASSERT
64+
#define LFS_ASSERT(test) assert(test)
65+
#else
66+
#define LFS_ASSERT(test)
67+
#endif
2468

2569

26-
// Builtin functions, these may be replaced by more
27-
// efficient implementations in the system
70+
// Builtin functions, these may be replaced by more efficient
71+
// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
72+
// expensive basic C implementation for debugging purposes
73+
74+
// Min/max functions for unsigned 32-bit numbers
2875
static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
2976
return (a > b) ? a : b;
3077
}
@@ -33,8 +80,9 @@ static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
3380
return (a < b) ? a : b;
3481
}
3582

83+
// Find the next smallest power of 2 less than or equal to a
3684
static inline uint32_t lfs_npw2(uint32_t a) {
37-
#if defined(__GNUC__) || defined(__CC_ARM)
85+
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
3886
return 32 - __builtin_clz(a-1);
3987
#else
4088
uint32_t r = 0;
@@ -48,16 +96,19 @@ static inline uint32_t lfs_npw2(uint32_t a) {
4896
#endif
4997
}
5098

99+
// Count the number of trailing binary zeros in a
100+
// lfs_ctz(0) may be undefined
51101
static inline uint32_t lfs_ctz(uint32_t a) {
52-
#if defined(__GNUC__)
102+
#if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
53103
return __builtin_ctz(a);
54104
#else
55105
return lfs_npw2((a & -a) + 1) - 1;
56106
#endif
57107
}
58108

109+
// Count the number of binary ones in a
59110
static inline uint32_t lfs_popc(uint32_t a) {
60-
#if defined(__GNUC__) || defined(__CC_ARM)
111+
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
61112
return __builtin_popcount(a);
62113
#else
63114
a = a - ((a >> 1) & 0x55555555);
@@ -66,17 +117,20 @@ static inline uint32_t lfs_popc(uint32_t a) {
66117
#endif
67118
}
68119

120+
// Find the sequence comparison of a and b, this is the distance
121+
// between a and b ignoring overflow
69122
static inline int lfs_scmp(uint32_t a, uint32_t b) {
70123
return (int)(unsigned)(a - b);
71124
}
72125

126+
// Convert from 32-bit little-endian to native order
73127
static inline uint32_t lfs_fromle32(uint32_t a) {
74-
#if ( \
128+
#if !defined(LFS_NO_INTRINSICS) && ( \
75129
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
76130
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
77131
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
78132
return a;
79-
#elif ( \
133+
#elif !defined(LFS_NO_INTRINSICS) && ( \
80134
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
81135
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
82136
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
@@ -89,19 +143,29 @@ static inline uint32_t lfs_fromle32(uint32_t a) {
89143
#endif
90144
}
91145

146+
// Convert to 32-bit little-endian from native order
92147
static inline uint32_t lfs_tole32(uint32_t a) {
93148
return lfs_fromle32(a);
94149
}
95150

96-
// CRC-32 with polynomial = 0x04c11db7
151+
// Calculate CRC-32 with polynomial = 0x04c11db7
97152
void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
98153

154+
// Allocate memory, only used if buffers are not provided to littlefs
155+
static inline void *lfs_malloc(size_t size) {
156+
#ifndef LFS_NO_MALLOC
157+
return malloc(size);
158+
#else
159+
return NULL;
160+
#endif
161+
}
99162

100-
// Logging functions, these may be replaced by system-specific
101-
// logging functions
102-
#define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
103-
#define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
104-
#define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
163+
// Deallocate memory, only used if buffers are not provided to littlefs
164+
static inline void lfs_free(void *p) {
165+
#ifndef LFS_NO_MALLOC
166+
free(p);
167+
#endif
168+
}
105169

106170

107171
#endif

0 commit comments

Comments
 (0)