lfs_util.h 4.9 KB

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