17 #define LFS_STRINGIZE(x) LFS_STRINGIZE2(x) 18 #define LFS_STRINGIZE2(x) #x 19 #include LFS_STRINGIZE(LFS_CONFIG) 33 #if !defined(LFS_NO_INFO) || !defined(LFS_NO_DEBUG) || !defined(LFS_NO_WARN) || !defined(LFS_NO_ERROR) 48 #include "mbed_debug.h" 49 #include "mbed_assert.h" 50 #include "cmsis_compiler.h" 52 #define MBED_LFS_ENABLE_INFO false 53 #define MBED_LFS_ENABLE_DEBUG true 54 #define MBED_LFS_ENABLE_WARN true 55 #define MBED_LFS_ENABLE_ERROR true 56 #define MBED_LFS_ENABLE_ASSERT true 57 #define MBED_LFS_INTRINSICS true 61 #if !defined(LFS_NO_INFO) && MBED_LFS_ENABLE_INFO 62 #define LFS_INFO(fmt, ...) printf("lfs info:%d: " fmt "\n", __LINE__, __VA_ARGS__) 63 #elif !defined(LFS_NO_INFO) && !defined(MBED_LFS_ENABLE_INFO) 64 #define LFS_INFO(fmt, ...) debug("lfs info:%d: " fmt "\n", __LINE__, __VA_ARGS__) 66 #define LFS_INFO(fmt, ...) 69 #if !defined(LFS_NO_DEBUG) && MBED_LFS_ENABLE_DEBUG 70 #define LFS_DEBUG(fmt, ...) printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__) 71 #elif !defined(LFS_NO_DEBUG) && !defined(MBED_LFS_ENABLE_DEBUG) 72 #define LFS_DEBUG(fmt, ...) debug("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__) 74 #define LFS_DEBUG(fmt, ...) 77 #if !defined(LFS_NO_WARN) && MBED_LFS_ENABLE_WARN 78 #define LFS_WARN(fmt, ...) printf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__) 79 #elif !defined(LFS_NO_WARN) && !defined(MBED_LFS_ENABLE_WARN) 80 #define LFS_WARN(fmt, ...) debug("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__) 82 #define LFS_WARN(fmt, ...) 85 #if !defined(LFS_NO_ERROR) && MBED_LFS_ENABLE_ERROR 86 #define LFS_ERROR(fmt, ...) printf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__) 87 #elif !defined(LFS_NO_ERROR) && !defined(MBED_LFS_ENABLE_ERROR) 88 #define LFS_ERROR(fmt, ...) debug("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__) 90 #define LFS_ERROR(fmt, ...) 94 #if !defined(LFS_NO_ASSERT) && MBED_LFS_ENABLE_ASSERT 95 #define LFS_ASSERT(test) assert(test) 96 #elif !defined(LFS_NO_ASSERT) && !defined(MBED_LFS_ENABLE_ASSERT) 97 #define LFS_ASSERT(test) MBED_ASSERT(test) 99 #define LFS_ASSERT(test) 108 static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
109 return (a > b) ? a : b;
112 static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
113 return (a < b) ? a : b;
117 static inline uint32_t lfs_npw2(uint32_t a) {
118 #if !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && \ 120 return 32 - __builtin_clz(a-1);
125 s = (a > 0xffff) << 4; a >>= s; r |= s;
126 s = (a > 0xff ) << 3; a >>= s; r |= s;
127 s = (a > 0xf ) << 2; a >>= s; r |= s;
128 s = (a > 0x3 ) << 1; a >>= s; r |= s;
129 return (r | (a >> 1)) + 1;
135 static inline uint32_t lfs_ctz(uint32_t a) {
136 #if !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && \ 138 return __builtin_ctz(a);
140 return lfs_npw2((a & -a) + 1) - 1;
145 static inline uint32_t lfs_popc(uint32_t a) {
146 #if !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && \ 148 return __builtin_popcount(a);
150 a = a - ((a >> 1) & 0x55555555);
151 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
152 return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
158 static inline int lfs_scmp(uint32_t a, uint32_t b) {
159 return (
int)(unsigned)(a - b);
163 static inline uint32_t lfs_fromle32(uint32_t a) {
164 #if !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && ( \ 165 (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \ 166 (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \ 167 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) 169 #elif !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && ( \ 170 (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \ 171 (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \ 172 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) 173 return __builtin_bswap32(a);
175 return (((uint8_t*)&a)[0] << 0) |
176 (((uint8_t*)&a)[1] << 8) |
177 (((uint8_t*)&a)[2] << 16) |
178 (((uint8_t*)&a)[3] << 24);
183 static inline uint32_t lfs_tole32(uint32_t a) {
184 return lfs_fromle32(a);
188 static inline uint32_t lfs_rbit(uint32_t a) {
189 #if !defined(LFS_NO_INTRINSICS) && MBED_LFS_INTRINSICS && \ 193 a = ((a & 0xaaaaaaaa) >> 1) | ((a & 0x55555555) << 1);
194 a = ((a & 0xcccccccc) >> 2) | ((a & 0x33333333) << 2);
195 a = ((a & 0xf0f0f0f0) >> 4) | ((a & 0x0f0f0f0f) << 4);
196 a = ((a & 0xff00ff00) >> 8) | ((a & 0x00ff00ff) << 8);
197 a = (a >> 16) | (a << 16);
203 void lfs_crc(uint32_t *crc,
const void *buffer,
size_t size);
206 static inline void *lfs_malloc(
size_t size) {
207 #ifndef LFS_NO_MALLOC 216 static inline void lfs_free(
void *p) {
217 #ifndef LFS_NO_MALLOC