lfs_util.h 6.1 KB

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