lfs_util.h 7.9 KB

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