From a0c6c54345921cb98a4feb39651939122f99fa19 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 19 Dec 2023 13:57:17 -0600 Subject: [PATCH 1/2] Added LFS_MALLOC/FREE, easier overrides for lfs_malloc/free Now you can override littlefs's malloc with some simple defines: -DLFS_MALLOC=my_malloc -DLFS_FREE=my_free This is probably what most users expected when wanting to override malloc/free in littlefs, but it hasn't been available, since instead littlefs provides a file-level override of builtin utils. The thinking was that there's just too many builtins that could be overriden, lfs_max/min/alignup/npw2/etc/etc/etc, so allowing users to just override the util file provides the best flexibility without a ton of ifdefs. But it's become clear this is awkward for users that just want to replace malloc. Maybe the original goal was too optimistic, maybe there's a better way to structure this file, or maybe the best API is just a bunch of ifdefs, I have no idea! This will hopefully continue to evolve. --- lfs_util.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lfs_util.h b/lfs_util.h index 13e93961..30f96121 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -217,7 +217,9 @@ uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size); // Allocate memory, only used if buffers are not provided to littlefs // Note, memory must be 64-bit aligned static inline void *lfs_malloc(size_t size) { -#ifndef LFS_NO_MALLOC +#if defined(LFS_MALLOC) + return LFS_MALLOC(size); +#elif !defined(LFS_NO_MALLOC) return malloc(size); #else (void)size; @@ -227,7 +229,9 @@ static inline void *lfs_malloc(size_t size) { // Deallocate memory, only used if buffers are not provided to littlefs static inline void lfs_free(void *p) { -#ifndef LFS_NO_MALLOC +#if defined(LFS_FREE) + LFS_FREE(p); +#elif !defined(LFS_NO_MALLOC) free(p); #else (void)p; From 9a620c730c097a4ddccebca983e35625c1f4fb2e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 19 Dec 2023 14:12:10 -0600 Subject: [PATCH 2/2] Added LFS_CRC, easier override for lfs_crc Now you can override littlefs's CRC implementation with some simple defines: -DLFS_CRC=lfs_crc The motivation for this is the same for LFS_MALLOC/LFS_FREE. I think these are the main "system-level" utils that users want to override. Don't override with this something that's not CRC32! Your filesystem will no longer be compatible with other tools! This is only intended for provided hardware acceleration! --- lfs_util.c | 3 +++ lfs_util.h | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lfs_util.c b/lfs_util.c index 9cdd1c60..dac72abc 100644 --- a/lfs_util.c +++ b/lfs_util.c @@ -11,6 +11,8 @@ #ifndef LFS_CONFIG +// If user provides their own CRC impl we don't need this +#ifndef LFS_CRC // 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] = { @@ -29,6 +31,7 @@ uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) { return crc; } +#endif #endif diff --git a/lfs_util.h b/lfs_util.h index 30f96121..45cefc95 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -212,7 +212,13 @@ static inline uint32_t lfs_tobe32(uint32_t a) { } // Calculate CRC-32 with polynomial = 0x04c11db7 +#ifdef LFS_CRC +uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) { + return LFS_CRC(crc, buffer, size) +} +#else uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size); +#endif // Allocate memory, only used if buffers are not provided to littlefs // Note, memory must be 64-bit aligned