lfs_util.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LFS_UTIL_H
  19. #define LFS_UTIL_H
  20. // Users can override lfs_util.h with their own configuration by defining
  21. // LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
  22. //
  23. // If LFS_CONFIG is used, none of the default utils will be emitted and must be
  24. // provided by the config file. To start I would suggest copying lfs_util.h and
  25. // modifying as needed.
  26. #ifdef LFS_CONFIG
  27. #define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
  28. #define LFS_STRINGIZE2(x) #x
  29. #include LFS_STRINGIZE(LFS_CONFIG)
  30. #else
  31. // System includes
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. #include <string.h>
  35. #ifndef LFS_NO_MALLOC
  36. #include <stdlib.h>
  37. #endif
  38. #ifndef LFS_NO_ASSERT
  39. #include <assert.h>
  40. #endif
  41. #if !defined(LFS_NO_DEBUG) || !defined(LFS_NO_WARN) || !defined(LFS_NO_ERROR)
  42. #include <stdio.h>
  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_NO_DEBUG
  49. #define LFS_DEBUG(fmt, ...) \
  50. printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  51. #else
  52. #define LFS_DEBUG(fmt, ...)
  53. #endif
  54. #ifndef LFS_NO_WARN
  55. #define LFS_WARN(fmt, ...) \
  56. printf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  57. #else
  58. #define LFS_WARN(fmt, ...)
  59. #endif
  60. #ifndef LFS_NO_ERROR
  61. #define LFS_ERROR(fmt, ...) \
  62. printf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  63. #else
  64. #define LFS_ERROR(fmt, ...)
  65. #endif
  66. // Runtime assertions
  67. #ifndef LFS_NO_ASSERT
  68. #define LFS_ASSERT(test) assert(test)
  69. #else
  70. #define LFS_ASSERT(test)
  71. #endif
  72. // Builtin functions, these may be replaced by more efficient
  73. // toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
  74. // expensive basic C implementation for debugging purposes
  75. // Min/max functions for unsigned 32-bit numbers
  76. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  77. return (a > b) ? a : b;
  78. }
  79. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  80. return (a < b) ? a : b;
  81. }
  82. // Find the next smallest power of 2 less than or equal to a
  83. static inline uint32_t lfs_npw2(uint32_t a) {
  84. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  85. return 32 - __builtin_clz(a-1);
  86. #else
  87. uint32_t r = 0;
  88. uint32_t s;
  89. a -= 1;
  90. s = (a > 0xffff) << 4; a >>= s; r |= s;
  91. s = (a > 0xff ) << 3; a >>= s; r |= s;
  92. s = (a > 0xf ) << 2; a >>= s; r |= s;
  93. s = (a > 0x3 ) << 1; a >>= s; r |= s;
  94. return (r | (a >> 1)) + 1;
  95. #endif
  96. }
  97. // Count the number of trailing binary zeros in a
  98. // lfs_ctz(0) may be undefined
  99. static inline uint32_t lfs_ctz(uint32_t a) {
  100. #if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
  101. return __builtin_ctz(a);
  102. #else
  103. return lfs_npw2((a & -a) + 1) - 1;
  104. #endif
  105. }
  106. // Count the number of binary ones in a
  107. static inline uint32_t lfs_popc(uint32_t a) {
  108. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  109. return __builtin_popcount(a);
  110. #else
  111. a = a - ((a >> 1) & 0x55555555);
  112. a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
  113. return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
  114. #endif
  115. }
  116. // Find the sequence comparison of a and b, this is the distance
  117. // between a and b ignoring overflow
  118. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  119. return (int)(unsigned)(a - b);
  120. }
  121. // Convert between 32-bit little-endian and native order
  122. static inline uint32_t lfs_fromle32(uint32_t a) {
  123. #if !defined(LFS_NO_INTRINSICS) && ( \
  124. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  125. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  126. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  127. return a;
  128. #elif !defined(LFS_NO_INTRINSICS) && ( \
  129. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  130. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  131. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  132. return __builtin_bswap32(a);
  133. #else
  134. return (((uint8_t*)&a)[0] << 0) |
  135. (((uint8_t*)&a)[1] << 8) |
  136. (((uint8_t*)&a)[2] << 16) |
  137. (((uint8_t*)&a)[3] << 24);
  138. #endif
  139. }
  140. static inline uint32_t lfs_tole32(uint32_t a) {
  141. return lfs_fromle32(a);
  142. }
  143. // Convert between 16-bit little-endian and native order
  144. static inline uint16_t lfs_fromle16(uint16_t a) {
  145. #if !defined(LFS_NO_INTRINSICS) && ( \
  146. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  147. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  148. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  149. return a;
  150. #elif !defined(LFS_NO_INTRINSICS) && ( \
  151. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  152. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  153. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  154. return __builtin_bswap16(a);
  155. #else
  156. return (((uint8_t*)&a)[0] << 0) |
  157. (((uint8_t*)&a)[1] << 8);
  158. #endif
  159. }
  160. static inline uint16_t lfs_tole16(uint16_t a) {
  161. return lfs_fromle16(a);
  162. }
  163. // Align to nearest multiple of a size
  164. static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
  165. return a - (a % alignment);
  166. }
  167. static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
  168. return lfs_aligndown(a + alignment-1, alignment);
  169. }
  170. // Calculate CRC-32 with polynomial = 0x04c11db7
  171. void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
  172. // Allocate memory, only used if buffers are not provided to littlefs
  173. static inline void *lfs_malloc(size_t size) {
  174. #ifndef LFS_NO_MALLOC
  175. return malloc(size);
  176. #else
  177. return NULL;
  178. #endif
  179. }
  180. // Deallocate memory, only used if buffers are not provided to littlefs
  181. static inline void lfs_free(void *p) {
  182. #ifndef LFS_NO_MALLOC
  183. free(p);
  184. #endif
  185. }
  186. #endif
  187. #endif