lfs_util.h 4.8 KB

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