lfs_util.h 7.2 KB

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