Skip to content

Commit 6d4248c

Browse files
committed
util: Cleaned up lfs_util.h
This one was a bit more involved. Removes utils that are no longer useful, and made sure some of the name/API changes over time are adopted consistently: - lfs_npw2 -> lfs_nlog2 - lfs_tole32_ -> lfs_tole32 - lfs_fromle32_ -> lfs_fromle32 Also did another pass for lfs_ prefixes on mem/str functions. The habit to use the naked variants of these is hard to break!
1 parent 31aafe0 commit 6d4248c

5 files changed

Lines changed: 21 additions & 122 deletions

File tree

lfs.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ static int lfsr_data_readle32(lfs_t *lfs, lfsr_data_t *data,
17231723
return LFS_ERR_CORRUPT;
17241724
}
17251725

1726-
*word = lfs_fromle32_(buf);
1726+
*word = lfs_fromle32(buf);
17271727
return 0;
17281728
}
17291729

@@ -1904,7 +1904,7 @@ static int lfsr_bd_progdata(lfs_t *lfs,
19041904

19051905
static inline lfsr_data_t lfsr_data_fromle32(uint32_t word,
19061906
uint8_t buffer[static LFSR_LE32_DSIZE]) {
1907-
lfs_tole32_(word, buffer);
1907+
lfs_tole32(word, buffer);
19081908
return LFSR_DATA_BUF(buffer, LFSR_LE32_DSIZE);
19091909
}
19101910

@@ -2329,7 +2329,7 @@ static lfsr_data_t lfsr_data_fromecksum(const lfsr_ecksum_t *ecksum,
23292329
}
23302330
d += d_;
23312331

2332-
lfs_tole32_(ecksum->cksum, &buffer[d]);
2332+
lfs_tole32(ecksum->cksum, &buffer[d]);
23332333
d += 4;
23342334

23352335
return LFSR_DATA_BUF(buffer, d);
@@ -2473,7 +2473,7 @@ static lfsr_data_t lfsr_data_frombptr(const lfsr_bptr_t *bptr,
24732473
}
24742474
d += d_;
24752475

2476-
lfs_tole32_(lfsr_bptr_cksum(bptr), &buffer[d]);
2476+
lfs_tole32(lfsr_bptr_cksum(bptr), &buffer[d]);
24772477
d += 4;
24782478

24792479
return LFSR_DATA_BUF(buffer, d);
@@ -2779,7 +2779,7 @@ static int lfsr_rbyd_fetch_(lfs_t *lfs,
27792779
}
27802780
return err;
27812781
}
2782-
cksum__ = lfs_fromle32_(&cksum__);
2782+
cksum__ = lfs_fromle32(&cksum__);
27832783

27842784
if (cksum_ != cksum__) {
27852785
// uh oh, checksums don't match
@@ -3140,7 +3140,7 @@ static int lfsr_rbyd_appendrev(lfs_t *lfs, lfsr_rbyd_t *rbyd, uint32_t rev) {
31403140
// revision count stored as le32, we don't use a leb128 encoding as we
31413141
// intentionally allow the revision count to overflow
31423142
uint8_t rev_buf[sizeof(uint32_t)];
3143-
lfs_tole32_(rev, &rev_buf);
3143+
lfs_tole32(rev, &rev_buf);
31443144

31453145
int err = lfsr_bd_prog(lfs,
31463146
rbyd->blocks[0], lfsr_rbyd_eoff(rbyd),
@@ -4264,7 +4264,7 @@ static int lfsr_rbyd_appendcksum_(lfs_t *lfs, lfsr_rbyd_t *rbyd,
42644264
// note the odd-parity zero preserves our position in the crc32c
42654265
// ring while only changing the parity
42664266
cksum_ ^= (lfsr_rbyd_isperturb(rbyd)) ? LFS_CRC32C_ODDZERO : 0;
4267-
lfs_tole32_(cksum_, &cksum_buf[2+1+4]);
4267+
lfs_tole32(cksum_, &cksum_buf[2+1+4]);
42684268

42694269
// prog, when this lands on disk commit is committed
42704270
int err = lfsr_bd_prog(lfs, rbyd->blocks[0], lfsr_rbyd_eoff(rbyd),
@@ -4847,7 +4847,7 @@ static lfsr_data_t lfsr_data_frombranch(const lfsr_rbyd_t *branch,
48474847
}
48484848
d += d_;
48494849

4850-
lfs_tole32_(branch->cksum, &buffer[d]);
4850+
lfs_tole32(branch->cksum, &buffer[d]);
48514851
d += 4;
48524852

48534853
return LFSR_DATA_BUF(buffer, d);
@@ -7284,7 +7284,7 @@ static int lfsr_mdir_fetch(lfs_t *lfs, lfsr_mdir_t *mdir,
72847284
if (err && err != LFS_ERR_CORRUPT) {
72857285
return err;
72867286
}
7287-
revs[i] = lfs_fromle32_(&revs[i]);
7287+
revs[i] = lfs_fromle32(&revs[i]);
72887288

72897289
if (i == 0
72907290
|| err == LFS_ERR_CORRUPT
@@ -7539,7 +7539,7 @@ static int lfsr_mdir_alloc__(lfs_t *lfs, lfsr_mdir_t *mdir,
75397539
return err;
75407540
}
75417541
// note we allow corrupt errors here, as long as they are consistent
7542-
rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32_(&rev) : 0;
7542+
rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32(&rev) : 0;
75437543
// reset recycle bits in revision count and increment
75447544
rev = lfsr_rev_init(lfs, mdir, rev);
75457545

@@ -7583,7 +7583,7 @@ static int lfsr_mdir_swap__(lfs_t *lfs, lfsr_mdir_t *mdir_,
75837583
return err;
75847584
}
75857585
// note we allow corrupt errors here, as long as they are consistent
7586-
rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32_(&rev) : 0;
7586+
rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32(&rev) : 0;
75877587
// increment our revision count
75887588
rev = lfsr_rev_inc(lfs, rev);
75897589

@@ -10889,7 +10889,7 @@ int lfsr_setattr(lfs_t *lfs, const char *path, uint8_t type,
1088910889
}
1089010890

1089110891
lfs_size_t d = lfs_min(size, file->cfg->attrs[i].buffer_size);
10892-
memcpy(file->cfg->attrs[i].buffer, buffer, d);
10892+
lfs_memcpy(file->cfg->attrs[i].buffer, buffer, d);
1089310893
if (file->cfg->attrs[i].size) {
1089410894
*file->cfg->attrs[i].size = d;
1089510895
}
@@ -13018,7 +13018,7 @@ int lfsr_file_sync(lfs_t *lfs, lfsr_file_t *file) {
1301813018
lfs_size_t d = lfs_min(
1301913019
lfsr_attr_size(&file->cfg->attrs[i]),
1302013020
file_->cfg->attrs[j].buffer_size);
13021-
memcpy(file_->cfg->attrs[j].buffer,
13021+
lfs_memcpy(file_->cfg->attrs[j].buffer,
1302213022
file->cfg->attrs[i].buffer,
1302313023
d);
1302413024
if (file_->cfg->attrs[j].size) {
@@ -13825,7 +13825,7 @@ static int lfsr_data_readcompat(lfs_t *lfs, lfsr_data_t *data,
1382513825
if (d < 0) {
1382613826
return d;
1382713827
}
13828-
*compat = lfs_fromle32_(buf);
13828+
*compat = lfs_fromle32(buf);
1382913829

1383013830
// if any out-of-range flags are set, set the internal overflow bit,
1383113831
// this is a compromise in correctness and and compat-flag complexity

lfs_util.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,6 @@ ssize_t lfs_fromleb128(uint32_t *word, const void *buffer, size_t size) {
5757
}
5858

5959

60-
//// Software CRC implementation with small lookup table
61-
//uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) {
62-
// static const uint32_t rtable[16] = {
63-
// 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
64-
// 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
65-
// 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
66-
// 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c,
67-
// };
68-
//
69-
// const uint8_t *data = buffer;
70-
//
71-
// for (size_t i = 0; i < size; i++) {
72-
// crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 0)) & 0xf];
73-
// crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 4)) & 0xf];
74-
// }
75-
//
76-
// return crc;
77-
//}
78-
79-
8060
// crc32c tables (see lfs_crc32c for more info)
8161
#if !defined(LFS_SMALLER_CRC32C) \
8262
&& !defined(LFS_FASTER_CRC32C) \
@@ -301,4 +281,5 @@ uint32_t lfs_crc32c_mul(uint32_t a, uint32_t b) {
301281
return (uint32_t)r;
302282
}
303283

284+
304285
#endif

lfs_util.h

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,6 @@
189189
#endif
190190
#endif
191191

192-
// We need to know the endianness of the system for some struct packing
193-
#if (defined(BYTE_ORDER) \
194-
&& defined(ORDER_LITTLE_ENDIAN) \
195-
&& BYTE_ORDER == ORDER_LITTLE_ENDIAN) \
196-
|| (defined(__BYTE_ORDER) \
197-
&& defined(__ORDER_LITTLE_ENDIAN) \
198-
&& __BYTE_ORDER == __ORDER_LITTLE_ENDIAN) \
199-
|| (defined(__BYTE_ORDER__) \
200-
&& defined(__ORDER_LITTLE_ENDIAN__) \
201-
&& __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
202-
#define LFS_LITTLE_ENDIAN
203-
#elif (defined(BYTE_ORDER) \
204-
&& defined(ORDER_BIG_ENDIAN) \
205-
&& BYTE_ORDER == ORDER_BIG_ENDIAN) \
206-
|| (defined(__BYTE_ORDER) \
207-
&& defined(__ORDER_BIG_ENDIAN) \
208-
&& __BYTE_ORDER == __ORDER_BIG_ENDIAN) \
209-
|| (defined(__BYTE_ORDER__) \
210-
&& defined(__ORDER_BIG_ENDIAN__) \
211-
&& __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
212-
#define LFS_BIG_ENDIAN
213-
#else
214-
#error "lfs: Unknown endianness?"
215-
#endif
216-
217192

218193
// Some ifdef conveniences
219194
#ifdef LFS_REVDBG
@@ -331,7 +306,7 @@ static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
331306
}
332307

333308
// Find the smallest power of 2 greater than or equal to a
334-
static inline uint32_t lfs_npw2(uint32_t a) {
309+
static inline uint32_t lfs_nlog2(uint32_t a) {
335310
// __builtin_clz of zero is undefined, so treat both 0 and 1 specially
336311
if (a <= 1) {
337312
return a;
@@ -351,19 +326,13 @@ static inline uint32_t lfs_npw2(uint32_t a) {
351326
#endif
352327
}
353328

354-
// TODO we should eventually adopt this as the new name for npw2
355-
// Find the ceiling of log base 2 of the given number
356-
static inline uint32_t lfs_nlog2(uint32_t a) {
357-
return lfs_npw2(a);
358-
}
359-
360329
// Count the number of trailing binary zeros in a
361330
// lfs_ctz(0) may be undefined
362331
static inline uint32_t lfs_ctz(uint32_t a) {
363332
#if !defined(LFS_NO_BUILTINS) && defined(__GNUC__)
364333
return __builtin_ctz(a);
365334
#else
366-
return lfs_npw2((a & -a) + 1) - 1;
335+
return lfs_nlog2((a & -a) + 1) - 1;
367336
#endif
368337
}
369338

@@ -416,62 +385,15 @@ static inline uint64_t lfs_pmul(uint32_t a, uint32_t b) {
416385
}
417386

418387

419-
// Convert between 32-bit little-endian and native order
420-
static inline uint32_t lfs_fromle32(uint32_t a) {
421-
#if !defined(LFS_NO_BUILTINS) && defined(LFS_LITTLE_ENDIAN)
422-
return a;
423-
#elif !defined(LFS_NO_BUILTINS)
424-
return __builtin_bswap32(a);
425-
#else
426-
return (((uint8_t*)&a)[0] << 0) |
427-
(((uint8_t*)&a)[1] << 8) |
428-
(((uint8_t*)&a)[2] << 16) |
429-
(((uint8_t*)&a)[3] << 24);
430-
#endif
431-
}
432-
433-
static inline uint32_t lfs_tole32(uint32_t a) {
434-
return lfs_fromle32(a);
435-
}
436-
437-
// Convert between 32-bit big-endian and native order
438-
static inline uint32_t lfs_frombe32(uint32_t a) {
439-
#if !defined(LFS_NO_BUILTINS) && defined(LFS_LITTLE_ENDIAN)
440-
return __builtin_bswap32(a);
441-
#elif !defined(LFS_NO_BUILTINS)
442-
return a;
443-
#else
444-
return (((uint8_t*)&a)[0] << 24) |
445-
(((uint8_t*)&a)[1] << 16) |
446-
(((uint8_t*)&a)[2] << 8) |
447-
(((uint8_t*)&a)[3] << 0);
448-
#endif
449-
}
450-
451-
static inline uint32_t lfs_tobe32(uint32_t a) {
452-
return lfs_frombe32(a);
453-
}
454-
455-
// Convert to/from 16-bit little-endian
456-
static inline void lfs_tole16_(uint16_t word, void *buffer) {
457-
((uint8_t*)buffer)[0] = word >> 0;
458-
((uint8_t*)buffer)[1] = word >> 8;
459-
}
460-
461-
static inline uint16_t lfs_fromle16_(const void *buffer) {
462-
return (((uint8_t*)buffer)[0] << 0)
463-
| (((uint8_t*)buffer)[1] << 8);
464-
}
465-
466388
// Convert to/from 32-bit little-endian
467-
static inline void lfs_tole32_(uint32_t word, void *buffer) {
389+
static inline void lfs_tole32(uint32_t word, void *buffer) {
468390
((uint8_t*)buffer)[0] = word >> 0;
469391
((uint8_t*)buffer)[1] = word >> 8;
470392
((uint8_t*)buffer)[2] = word >> 16;
471393
((uint8_t*)buffer)[3] = word >> 24;
472394
}
473395

474-
static inline uint32_t lfs_fromle32_(const void *buffer) {
396+
static inline uint32_t lfs_fromle32(const void *buffer) {
475397
return (((uint8_t*)buffer)[0] << 0)
476398
| (((uint8_t*)buffer)[1] << 8)
477399
| (((uint8_t*)buffer)[2] << 16)
@@ -733,9 +655,6 @@ static inline size_t lfs_strcspn(const char *a, const char *cs) {
733655
#endif
734656

735657

736-
//// Calculate CRC-32 with polynomial = 0x04c11db7
737-
//uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
738-
739658
// Odd-parity and even-parity zeros in our crc32c ring
740659
#define LFS_CRC32C_ODDZERO 0xfca42daf
741660
#define LFS_CRC32C_EVENZERO 0x00000000
@@ -758,7 +677,6 @@ static inline uint32_t lfs_crc32c_cube(uint32_t a) {
758677

759678

760679
// Allocate memory, only used if buffers are not provided to littlefs
761-
// Note, memory must be 64-bit aligned
762680
#ifndef LFS_NO_MALLOC
763681
#define lfs_malloc malloc
764682
#else

runners/bench_runner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ getopt_done: ;
19201920

19211921
if (d >= define_count) {
19221922
// align to power of two to avoid any superlinear growth
1923-
size_t ncount = 1 << lfs_npw2(d+1);
1923+
size_t ncount = 1 << lfs_nlog2(d+1);
19241924
defines = realloc(defines,
19251925
ncount*sizeof(bench_define_t));
19261926
memset(defines+define_count, 0,

runners/test_runner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2538,7 +2538,7 @@ getopt_done:;
25382538

25392539
if (d >= define_count) {
25402540
// align to power of two to avoid any superlinear growth
2541-
size_t ncount = 1 << lfs_npw2(d+1);
2541+
size_t ncount = 1 << lfs_nlog2(d+1);
25422542
defines = realloc(defines,
25432543
ncount*sizeof(test_define_t));
25442544
memset(defines+define_count, 0,

0 commit comments

Comments
 (0)