lfs_util.h 7.1 KB

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