lfs_util.h 7.7 KB

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