lfs_util.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017, Arm Limited. All rights reserved.
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef LFS_UTIL_H
  8. #define LFS_UTIL_H
  9. // Users can override lfs_util.h with their own configuration by defining
  10. // LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
  11. //
  12. // If LFS_CONFIG is used, none of the default utils will be emitted and must be
  13. // provided by the config file. To start, I would suggest copying lfs_util.h
  14. // and modifying as needed.
  15. #ifdef LFS_CONFIG
  16. #define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
  17. #define LFS_STRINGIZE2(x) #x
  18. #include LFS_STRINGIZE(LFS_CONFIG)
  19. #else
  20. // System includes
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <string.h>
  24. #include <inttypes.h>
  25. #ifndef LFS_NO_MALLOC
  26. #include <stdlib.h>
  27. #endif
  28. #ifndef LFS_NO_ASSERT
  29. #ifdef __ZEPHYR__
  30. #include <sys/__assert.h>
  31. #else /* __ZEPHYR__ */
  32. #include <assert.h>
  33. #endif /* __ZEPHYR__ */
  34. #endif
  35. #if !defined(LFS_NO_DEBUG) || \
  36. !defined(LFS_NO_WARN) || \
  37. !defined(LFS_NO_ERROR) || \
  38. defined(LFS_YES_TRACE)
  39. #ifdef __ZEPHYR__
  40. #include <logging/log.h>
  41. #ifdef LFS_LOG_REGISTER
  42. LOG_MODULE_REGISTER(littlefs, CONFIG_FS_LOG_LEVEL);
  43. #else
  44. LOG_MODULE_DECLARE(littlefs, CONFIG_FS_LOG_LEVEL);
  45. #endif
  46. #endif /* __ZEPHYR__ */
  47. #include <stdio.h>
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. // Macros, may be replaced by system specific wrappers. Arguments to these
  54. // macros must not have side-effects as the macros can be removed for a smaller
  55. // code footprint
  56. // Logging functions
  57. #ifdef LFS_YES_TRACE
  58. #ifdef __ZEPHYR__
  59. #define LFS_TRACE(fmt, ...) LOG_DBG("%s:%d:trace: " fmt, __FILE__, __LINE__, __VA_ARGS__)
  60. #else /* __ZEPHYR__ */
  61. #define LFS_TRACE_(fmt, ...) \
  62. printf("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
  63. #define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
  64. #endif /* __ZEPHYR__ */
  65. #else
  66. #define LFS_TRACE(...)
  67. #endif
  68. #ifndef LFS_NO_DEBUG
  69. #ifdef __ZEPHYR__
  70. #define LFS_DEBUG(fmt, ...) LOG_DBG("%s:%d: " fmt, __FILE__, __LINE__, __VA_ARGS__)
  71. #else /* __ZEPHYR__ */
  72. #define LFS_DEBUG_(fmt, ...) \
  73. printf("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
  74. #define LFS_DEBUG(...) LFS_DEBUG_(__VA_ARGS__, "")
  75. #endif /* __ZEPHYR__ */
  76. #else
  77. #define LFS_DEBUG(...)
  78. #endif
  79. #ifndef LFS_NO_WARN
  80. #ifdef __ZEPHYR__
  81. #define LFS_WARN(fmt, ...) LOG_WRN("%s:%d: " fmt, __FILE__, __LINE__, __VA_ARGS__)
  82. #else /* __ZEPHYR__ */
  83. #define LFS_WARN_(fmt, ...) \
  84. printf("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
  85. #define LFS_WARN(...) LFS_WARN_(__VA_ARGS__, "")
  86. #endif /* __ZEPHYR__ */
  87. #else
  88. #define LFS_WARN(...)
  89. #endif
  90. #ifndef LFS_NO_ERROR
  91. #ifdef __ZEPHYR__
  92. #define LFS_ERROR(fmt, ...) LOG_ERR("%s:%d: " fmt, __FILE__, __LINE__, __VA_ARGS__)
  93. #else /* __ZEPHYR__ */
  94. #define LFS_ERROR_(fmt, ...) \
  95. printf("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
  96. #define LFS_ERROR(...) LFS_ERROR_(__VA_ARGS__, "")
  97. #endif /* __ZEPHYR__ */
  98. #else
  99. #define LFS_ERROR(...)
  100. #endif
  101. // Runtime assertions
  102. #ifndef LFS_NO_ASSERT
  103. #ifdef __ZEPHYR__
  104. #define LFS_ASSERT(test) __ASSERT_NO_MSG(test)
  105. #else /* __ZEPHYR__ */
  106. #define LFS_ASSERT(test) assert(test)
  107. #endif /* __ZEPHYR__ */
  108. #else
  109. #define LFS_ASSERT(test)
  110. #endif
  111. // Builtin functions, these may be replaced by more efficient
  112. // toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
  113. // expensive basic C implementation for debugging purposes
  114. // Min/max functions for unsigned 32-bit numbers
  115. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  116. return (a > b) ? a : b;
  117. }
  118. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  119. return (a < b) ? a : b;
  120. }
  121. // Align to nearest multiple of a size
  122. static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
  123. return a - (a % alignment);
  124. }
  125. static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
  126. return lfs_aligndown(a + alignment-1, alignment);
  127. }
  128. // Find the smallest power of 2 greater than or equal to a
  129. static inline uint32_t lfs_npw2(uint32_t a) {
  130. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  131. return 32 - __builtin_clz(a-1);
  132. #else
  133. uint32_t r = 0;
  134. uint32_t s;
  135. a -= 1;
  136. s = (a > 0xffff) << 4; a >>= s; r |= s;
  137. s = (a > 0xff ) << 3; a >>= s; r |= s;
  138. s = (a > 0xf ) << 2; a >>= s; r |= s;
  139. s = (a > 0x3 ) << 1; a >>= s; r |= s;
  140. return (r | (a >> 1)) + 1;
  141. #endif
  142. }
  143. // Count the number of trailing binary zeros in a
  144. // lfs_ctz(0) may be undefined
  145. static inline uint32_t lfs_ctz(uint32_t a) {
  146. #if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
  147. return __builtin_ctz(a);
  148. #else
  149. return lfs_npw2((a & -a) + 1) - 1;
  150. #endif
  151. }
  152. // Count the number of binary ones in a
  153. static inline uint32_t lfs_popc(uint32_t a) {
  154. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  155. return __builtin_popcount(a);
  156. #else
  157. a = a - ((a >> 1) & 0x55555555);
  158. a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
  159. return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
  160. #endif
  161. }
  162. // Find the sequence comparison of a and b, this is the distance
  163. // between a and b ignoring overflow
  164. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  165. return (int)(unsigned)(a - b);
  166. }
  167. // Convert between 32-bit little-endian and native order
  168. static inline uint32_t lfs_fromle32(uint32_t a) {
  169. #if !defined(LFS_NO_INTRINSICS) && ( \
  170. (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  171. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  172. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  173. return a;
  174. #elif !defined(LFS_NO_INTRINSICS) && ( \
  175. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  176. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  177. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  178. return __builtin_bswap32(a);
  179. #else
  180. return (((uint8_t*)&a)[0] << 0) |
  181. (((uint8_t*)&a)[1] << 8) |
  182. (((uint8_t*)&a)[2] << 16) |
  183. (((uint8_t*)&a)[3] << 24);
  184. #endif
  185. }
  186. static inline uint32_t lfs_tole32(uint32_t a) {
  187. return lfs_fromle32(a);
  188. }
  189. // Convert between 32-bit big-endian and native order
  190. static inline uint32_t lfs_frombe32(uint32_t a) {
  191. #if !defined(LFS_NO_INTRINSICS) && ( \
  192. (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  193. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  194. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  195. return __builtin_bswap32(a);
  196. #elif !defined(LFS_NO_INTRINSICS) && ( \
  197. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  198. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  199. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  200. return a;
  201. #else
  202. return (((uint8_t*)&a)[0] << 24) |
  203. (((uint8_t*)&a)[1] << 16) |
  204. (((uint8_t*)&a)[2] << 8) |
  205. (((uint8_t*)&a)[3] << 0);
  206. #endif
  207. }
  208. static inline uint32_t lfs_tobe32(uint32_t a) {
  209. return lfs_frombe32(a);
  210. }
  211. // Calculate CRC-32 with polynomial = 0x04c11db7
  212. uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
  213. // Allocate memory, only used if buffers are not provided to littlefs
  214. // Note, memory must be 64-bit aligned
  215. static inline void *lfs_malloc(size_t size) {
  216. #ifndef LFS_NO_MALLOC
  217. return malloc(size);
  218. #else
  219. (void)size;
  220. return NULL;
  221. #endif
  222. }
  223. // Deallocate memory, only used if buffers are not provided to littlefs
  224. static inline void lfs_free(void *p) {
  225. #ifndef LFS_NO_MALLOC
  226. free(p);
  227. #else
  228. (void)p;
  229. #endif
  230. }
  231. #ifdef __cplusplus
  232. } /* extern "C" */
  233. #endif
  234. #endif
  235. #endif