diff --git a/lfs.c b/lfs.c index fe52b2b4..5e3d2a2c 100644 --- a/lfs.c +++ b/lfs.c @@ -1723,7 +1723,7 @@ static int lfsr_data_readle32(lfs_t *lfs, lfsr_data_t *data, return LFS_ERR_CORRUPT; } - *word = lfs_fromle32_(buf); + *word = lfs_fromle32(buf); return 0; } @@ -1904,7 +1904,7 @@ static int lfsr_bd_progdata(lfs_t *lfs, static inline lfsr_data_t lfsr_data_fromle32(uint32_t word, uint8_t buffer[static LFSR_LE32_DSIZE]) { - lfs_tole32_(word, buffer); + lfs_tole32(word, buffer); return LFSR_DATA_BUF(buffer, LFSR_LE32_DSIZE); } @@ -2329,7 +2329,7 @@ static lfsr_data_t lfsr_data_fromecksum(const lfsr_ecksum_t *ecksum, } d += d_; - lfs_tole32_(ecksum->cksum, &buffer[d]); + lfs_tole32(ecksum->cksum, &buffer[d]); d += 4; return LFSR_DATA_BUF(buffer, d); @@ -2473,7 +2473,7 @@ static lfsr_data_t lfsr_data_frombptr(const lfsr_bptr_t *bptr, } d += d_; - lfs_tole32_(lfsr_bptr_cksum(bptr), &buffer[d]); + lfs_tole32(lfsr_bptr_cksum(bptr), &buffer[d]); d += 4; return LFSR_DATA_BUF(buffer, d); @@ -2779,7 +2779,7 @@ static int lfsr_rbyd_fetch_(lfs_t *lfs, } return err; } - cksum__ = lfs_fromle32_(&cksum__); + cksum__ = lfs_fromle32(&cksum__); if (cksum_ != cksum__) { // 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) { // revision count stored as le32, we don't use a leb128 encoding as we // intentionally allow the revision count to overflow uint8_t rev_buf[sizeof(uint32_t)]; - lfs_tole32_(rev, &rev_buf); + lfs_tole32(rev, &rev_buf); int err = lfsr_bd_prog(lfs, rbyd->blocks[0], lfsr_rbyd_eoff(rbyd), @@ -4264,7 +4264,7 @@ static int lfsr_rbyd_appendcksum_(lfs_t *lfs, lfsr_rbyd_t *rbyd, // note the odd-parity zero preserves our position in the crc32c // ring while only changing the parity cksum_ ^= (lfsr_rbyd_isperturb(rbyd)) ? LFS_CRC32C_ODDZERO : 0; - lfs_tole32_(cksum_, &cksum_buf[2+1+4]); + lfs_tole32(cksum_, &cksum_buf[2+1+4]); // prog, when this lands on disk commit is committed 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, } d += d_; - lfs_tole32_(branch->cksum, &buffer[d]); + lfs_tole32(branch->cksum, &buffer[d]); d += 4; return LFSR_DATA_BUF(buffer, d); @@ -7284,7 +7284,7 @@ static int lfsr_mdir_fetch(lfs_t *lfs, lfsr_mdir_t *mdir, if (err && err != LFS_ERR_CORRUPT) { return err; } - revs[i] = lfs_fromle32_(&revs[i]); + revs[i] = lfs_fromle32(&revs[i]); if (i == 0 || err == LFS_ERR_CORRUPT @@ -7539,7 +7539,7 @@ static int lfsr_mdir_alloc__(lfs_t *lfs, lfsr_mdir_t *mdir, return err; } // note we allow corrupt errors here, as long as they are consistent - rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32_(&rev) : 0; + rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32(&rev) : 0; // reset recycle bits in revision count and increment rev = lfsr_rev_init(lfs, mdir, rev); @@ -7583,7 +7583,7 @@ static int lfsr_mdir_swap__(lfs_t *lfs, lfsr_mdir_t *mdir_, return err; } // note we allow corrupt errors here, as long as they are consistent - rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32_(&rev) : 0; + rev = (err != LFS_ERR_CORRUPT) ? lfs_fromle32(&rev) : 0; // increment our revision count rev = lfsr_rev_inc(lfs, rev); @@ -10889,7 +10889,7 @@ int lfsr_setattr(lfs_t *lfs, const char *path, uint8_t type, } lfs_size_t d = lfs_min(size, file->cfg->attrs[i].buffer_size); - memcpy(file->cfg->attrs[i].buffer, buffer, d); + lfs_memcpy(file->cfg->attrs[i].buffer, buffer, d); if (file->cfg->attrs[i].size) { *file->cfg->attrs[i].size = d; } @@ -13018,7 +13018,7 @@ int lfsr_file_sync(lfs_t *lfs, lfsr_file_t *file) { lfs_size_t d = lfs_min( lfsr_attr_size(&file->cfg->attrs[i]), file_->cfg->attrs[j].buffer_size); - memcpy(file_->cfg->attrs[j].buffer, + lfs_memcpy(file_->cfg->attrs[j].buffer, file->cfg->attrs[i].buffer, d); if (file_->cfg->attrs[j].size) { @@ -13825,7 +13825,7 @@ static int lfsr_data_readcompat(lfs_t *lfs, lfsr_data_t *data, if (d < 0) { return d; } - *compat = lfs_fromle32_(buf); + *compat = lfs_fromle32(buf); // if any out-of-range flags are set, set the internal overflow bit, // this is a compromise in correctness and and compat-flag complexity diff --git a/lfs_util.c b/lfs_util.c index 51550c99..60abdab6 100644 --- a/lfs_util.c +++ b/lfs_util.c @@ -57,26 +57,6 @@ ssize_t lfs_fromleb128(uint32_t *word, const void *buffer, size_t size) { } -//// Software CRC implementation with small lookup table -//uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) { -// static const uint32_t rtable[16] = { -// 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, -// 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, -// 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, -// 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c, -// }; -// -// const uint8_t *data = buffer; -// -// for (size_t i = 0; i < size; i++) { -// crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 0)) & 0xf]; -// crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 4)) & 0xf]; -// } -// -// return crc; -//} - - // crc32c tables (see lfs_crc32c for more info) #if !defined(LFS_SMALLER_CRC32C) \ && !defined(LFS_FASTER_CRC32C) \ @@ -301,4 +281,5 @@ uint32_t lfs_crc32c_mul(uint32_t a, uint32_t b) { return (uint32_t)r; } + #endif diff --git a/lfs_util.h b/lfs_util.h index d6345044..49977e27 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -189,31 +189,6 @@ #endif #endif -// We need to know the endianness of the system for some struct packing -#if (defined(BYTE_ORDER) \ - && defined(ORDER_LITTLE_ENDIAN) \ - && BYTE_ORDER == ORDER_LITTLE_ENDIAN) \ - || (defined(__BYTE_ORDER) \ - && defined(__ORDER_LITTLE_ENDIAN) \ - && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN) \ - || (defined(__BYTE_ORDER__) \ - && defined(__ORDER_LITTLE_ENDIAN__) \ - && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) -#define LFS_LITTLE_ENDIAN -#elif (defined(BYTE_ORDER) \ - && defined(ORDER_BIG_ENDIAN) \ - && BYTE_ORDER == ORDER_BIG_ENDIAN) \ - || (defined(__BYTE_ORDER) \ - && defined(__ORDER_BIG_ENDIAN) \ - && __BYTE_ORDER == __ORDER_BIG_ENDIAN) \ - || (defined(__BYTE_ORDER__) \ - && defined(__ORDER_BIG_ENDIAN__) \ - && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -#define LFS_BIG_ENDIAN -#else -#error "lfs: Unknown endianness?" -#endif - // Some ifdef conveniences #ifdef LFS_REVDBG @@ -331,7 +306,7 @@ static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) { } // Find the smallest power of 2 greater than or equal to a -static inline uint32_t lfs_npw2(uint32_t a) { +static inline uint32_t lfs_nlog2(uint32_t a) { // __builtin_clz of zero is undefined, so treat both 0 and 1 specially if (a <= 1) { return a; @@ -351,19 +326,13 @@ static inline uint32_t lfs_npw2(uint32_t a) { #endif } -// TODO we should eventually adopt this as the new name for npw2 -// Find the ceiling of log base 2 of the given number -static inline uint32_t lfs_nlog2(uint32_t a) { - return lfs_npw2(a); -} - // Count the number of trailing binary zeros in a // lfs_ctz(0) may be undefined static inline uint32_t lfs_ctz(uint32_t a) { #if !defined(LFS_NO_BUILTINS) && defined(__GNUC__) return __builtin_ctz(a); #else - return lfs_npw2((a & -a) + 1) - 1; + return lfs_nlog2((a & -a) + 1) - 1; #endif } @@ -416,62 +385,15 @@ static inline uint64_t lfs_pmul(uint32_t a, uint32_t b) { } -// Convert between 32-bit little-endian and native order -static inline uint32_t lfs_fromle32(uint32_t a) { -#if !defined(LFS_NO_BUILTINS) && defined(LFS_LITTLE_ENDIAN) - return a; -#elif !defined(LFS_NO_BUILTINS) - return __builtin_bswap32(a); -#else - return (((uint8_t*)&a)[0] << 0) | - (((uint8_t*)&a)[1] << 8) | - (((uint8_t*)&a)[2] << 16) | - (((uint8_t*)&a)[3] << 24); -#endif -} - -static inline uint32_t lfs_tole32(uint32_t a) { - return lfs_fromle32(a); -} - -// Convert between 32-bit big-endian and native order -static inline uint32_t lfs_frombe32(uint32_t a) { -#if !defined(LFS_NO_BUILTINS) && defined(LFS_LITTLE_ENDIAN) - return __builtin_bswap32(a); -#elif !defined(LFS_NO_BUILTINS) - return a; -#else - return (((uint8_t*)&a)[0] << 24) | - (((uint8_t*)&a)[1] << 16) | - (((uint8_t*)&a)[2] << 8) | - (((uint8_t*)&a)[3] << 0); -#endif -} - -static inline uint32_t lfs_tobe32(uint32_t a) { - return lfs_frombe32(a); -} - -// Convert to/from 16-bit little-endian -static inline void lfs_tole16_(uint16_t word, void *buffer) { - ((uint8_t*)buffer)[0] = word >> 0; - ((uint8_t*)buffer)[1] = word >> 8; -} - -static inline uint16_t lfs_fromle16_(const void *buffer) { - return (((uint8_t*)buffer)[0] << 0) - | (((uint8_t*)buffer)[1] << 8); -} - // Convert to/from 32-bit little-endian -static inline void lfs_tole32_(uint32_t word, void *buffer) { +static inline void lfs_tole32(uint32_t word, void *buffer) { ((uint8_t*)buffer)[0] = word >> 0; ((uint8_t*)buffer)[1] = word >> 8; ((uint8_t*)buffer)[2] = word >> 16; ((uint8_t*)buffer)[3] = word >> 24; } -static inline uint32_t lfs_fromle32_(const void *buffer) { +static inline uint32_t lfs_fromle32(const void *buffer) { return (((uint8_t*)buffer)[0] << 0) | (((uint8_t*)buffer)[1] << 8) | (((uint8_t*)buffer)[2] << 16) @@ -733,9 +655,6 @@ static inline size_t lfs_strcspn(const char *a, const char *cs) { #endif -//// Calculate CRC-32 with polynomial = 0x04c11db7 -//uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size); - // Odd-parity and even-parity zeros in our crc32c ring #define LFS_CRC32C_ODDZERO 0xfca42daf #define LFS_CRC32C_EVENZERO 0x00000000 @@ -758,7 +677,6 @@ static inline uint32_t lfs_crc32c_cube(uint32_t a) { // Allocate memory, only used if buffers are not provided to littlefs -// Note, memory must be 64-bit aligned #ifndef LFS_NO_MALLOC #define lfs_malloc malloc #else diff --git a/runners/bench_runner.c b/runners/bench_runner.c index 17447147..0d8f5441 100644 --- a/runners/bench_runner.c +++ b/runners/bench_runner.c @@ -1920,7 +1920,7 @@ getopt_done: ; if (d >= define_count) { // align to power of two to avoid any superlinear growth - size_t ncount = 1 << lfs_npw2(d+1); + size_t ncount = 1 << lfs_nlog2(d+1); defines = realloc(defines, ncount*sizeof(bench_define_t)); memset(defines+define_count, 0, diff --git a/runners/test_runner.c b/runners/test_runner.c index f5752860..1050ef85 100644 --- a/runners/test_runner.c +++ b/runners/test_runner.c @@ -2538,7 +2538,7 @@ getopt_done:; if (d >= define_count) { // align to power of two to avoid any superlinear growth - size_t ncount = 1 << lfs_npw2(d+1); + size_t ncount = 1 << lfs_nlog2(d+1); defines = realloc(defines, ncount*sizeof(test_define_t)); memset(defines+define_count, 0,