CTS-SAT-1-OBC-Firmware
Loading...
Searching...
No Matches
lfs_util.h
Go to the documentation of this file.
1/*
2 * lfs utility functions
3 *
4 * Copyright (c) 2022, The littlefs authors.
5 * Copyright (c) 2017, Arm Limited. All rights reserved.
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8#ifndef LFS_UTIL_H
9#define LFS_UTIL_H
10
11// ================ Configuration =================
12#define LFS_NO_MALLOC 1
13
14// TODO: try setting these macros to write to debug UART or logs, maybe
15#define LFS_NO_DEBUG 1
16#define LFS_NO_WARN 1
17#define LFS_NO_ERROR 1
18#define LFS_NO_ASSERT 1
19// #define LFS_NO_INTRINSICS // Use the default for this.
20
21// ============= END Configuration ================
22
23#define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
24#define LFS_STRINGIZE2(x) #x
25
26// Users can override lfs_util.h with their own configuration by defining
27// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
28//
29// If LFS_CONFIG is used, none of the default utils will be emitted and must be
30// provided by the config file. To start, I would suggest copying lfs_util.h
31// and modifying as needed.
32#ifdef LFS_CONFIG
33#include LFS_STRINGIZE(LFS_CONFIG)
34#else
35
36// Alternatively, users can provide a header file which defines
37// macros and other things consumed by littlefs.
38//
39// For example, provide my_defines.h, which contains
40// something like:
41//
42// #include <stddef.h>
43// extern void *my_malloc(size_t sz);
44// #define LFS_MALLOC(sz) my_malloc(sz)
45//
46// And build littlefs with the header by defining LFS_DEFINES.
47// (-DLFS_DEFINES=my_defines.h)
48
49#ifdef LFS_DEFINES
50#include LFS_STRINGIZE(LFS_DEFINES)
51#endif
52
53// System includes
54#include <stdint.h>
55#include <stdbool.h>
56#include <string.h>
57#include <inttypes.h>
58
59#ifndef LFS_NO_MALLOC
60#include <stdlib.h>
61#endif
62#ifndef LFS_NO_ASSERT
63#include <assert.h>
64#endif
65#if !defined(LFS_NO_DEBUG) || \
66 !defined(LFS_NO_WARN) || \
67 !defined(LFS_NO_ERROR) || \
68 defined(LFS_YES_TRACE)
69#include <stdio.h>
70#endif
71
72#ifdef __cplusplus
73extern "C"
74{
75#endif
76
77
78// Macros, may be replaced by system specific wrappers. Arguments to these
79// macros must not have side-effects as the macros can be removed for a smaller
80// code footprint
81
82// Logging functions
83#ifndef LFS_TRACE
84#ifdef LFS_YES_TRACE
85#define LFS_TRACE_(fmt, ...) \
86 printf("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
87#define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
88#else
89#define LFS_TRACE(...)
90#endif
91#endif
92
93#ifndef LFS_DEBUG
94#ifndef LFS_NO_DEBUG
95#define LFS_DEBUG_(fmt, ...) \
96 printf("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
97#define LFS_DEBUG(...) LFS_DEBUG_(__VA_ARGS__, "")
98#else
99#define LFS_DEBUG(...)
100#endif
101#endif
102
103#ifndef LFS_WARN
104#ifndef LFS_NO_WARN
105#define LFS_WARN_(fmt, ...) \
106 printf("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
107#define LFS_WARN(...) LFS_WARN_(__VA_ARGS__, "")
108#else
109#define LFS_WARN(...)
110#endif
111#endif
112
113#ifndef LFS_ERROR
114#ifndef LFS_NO_ERROR
115#define LFS_ERROR_(fmt, ...) \
116 printf("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
117#define LFS_ERROR(...) LFS_ERROR_(__VA_ARGS__, "")
118#else
119#define LFS_ERROR(...)
120#endif
121#endif
122
123// Runtime assertions
124#ifndef LFS_ASSERT
125#ifndef LFS_NO_ASSERT
126#define LFS_ASSERT(test) assert(test)
127#else
128#define LFS_ASSERT(test)
129#endif
130#endif
131
132
133// Builtin functions, these may be replaced by more efficient
134// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
135// expensive basic C implementation for debugging purposes
136
137// Min/max functions for unsigned 32-bit numbers
138static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
139 return (a > b) ? a : b;
140}
141
142static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
143 return (a < b) ? a : b;
144}
145
146// Align to nearest multiple of a size
147static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
148 return a - (a % alignment);
149}
150
151static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
152 return lfs_aligndown(a + alignment-1, alignment);
153}
154
155// Find the smallest power of 2 greater than or equal to a
156static inline uint32_t lfs_npw2(uint32_t a) {
157#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
158 return 32 - __builtin_clz(a-1);
159#else
160 uint32_t r = 0;
161 uint32_t s;
162 a -= 1;
163 s = (a > 0xffff) << 4; a >>= s; r |= s;
164 s = (a > 0xff ) << 3; a >>= s; r |= s;
165 s = (a > 0xf ) << 2; a >>= s; r |= s;
166 s = (a > 0x3 ) << 1; a >>= s; r |= s;
167 return (r | (a >> 1)) + 1;
168#endif
169}
170
171// Count the number of trailing binary zeros in a
172// lfs_ctz(0) may be undefined
173static inline uint32_t lfs_ctz(uint32_t a) {
174#if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
175 return __builtin_ctz(a);
176#else
177 return lfs_npw2((a & -a) + 1) - 1;
178#endif
179}
180
181// Count the number of binary ones in a
182static inline uint32_t lfs_popc(uint32_t a) {
183#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
184 return __builtin_popcount(a);
185#else
186 a = a - ((a >> 1) & 0x55555555);
187 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
188 return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
189#endif
190}
191
192// Find the sequence comparison of a and b, this is the distance
193// between a and b ignoring overflow
194static inline int lfs_scmp(uint32_t a, uint32_t b) {
195 return (int)(unsigned)(a - b);
196}
197
198// Convert between 32-bit little-endian and native order
199static inline uint32_t lfs_fromle32(uint32_t a) {
200#if (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
201 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
202 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
203 return a;
204#elif !defined(LFS_NO_INTRINSICS) && ( \
205 (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
206 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
207 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
208 return __builtin_bswap32(a);
209#else
210 return (((uint8_t*)&a)[0] << 0) |
211 (((uint8_t*)&a)[1] << 8) |
212 (((uint8_t*)&a)[2] << 16) |
213 (((uint8_t*)&a)[3] << 24);
214#endif
215}
216
217static inline uint32_t lfs_tole32(uint32_t a) {
218 return lfs_fromle32(a);
219}
220
221// Convert between 32-bit big-endian and native order
222static inline uint32_t lfs_frombe32(uint32_t a) {
223#if !defined(LFS_NO_INTRINSICS) && ( \
224 (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
225 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
226 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
227 return __builtin_bswap32(a);
228#elif (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
229 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
230 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
231 return a;
232#else
233 return (((uint8_t*)&a)[0] << 24) |
234 (((uint8_t*)&a)[1] << 16) |
235 (((uint8_t*)&a)[2] << 8) |
236 (((uint8_t*)&a)[3] << 0);
237#endif
238}
239
240static inline uint32_t lfs_tobe32(uint32_t a) {
241 return lfs_frombe32(a);
242}
243
244// Calculate CRC-32 with polynomial = 0x04c11db7
245#ifdef LFS_CRC
246uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) {
247 return LFS_CRC(crc, buffer, size)
248}
249#else
250uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
251#endif
252
253// Allocate memory, only used if buffers are not provided to littlefs
254//
255// littlefs current has no alignment requirements, as it only allocates
256// byte-level buffers.
257static inline void *lfs_malloc(size_t size) {
258#if defined(LFS_MALLOC)
259 return LFS_MALLOC(size);
260#elif !defined(LFS_NO_MALLOC)
261 return malloc(size);
262#else
263 (void)size;
264 return NULL;
265#endif
266}
267
268// Deallocate memory, only used if buffers are not provided to littlefs
269static inline void lfs_free(void *p) {
270#if defined(LFS_FREE)
271 LFS_FREE(p);
272#elif !defined(LFS_NO_MALLOC)
273 free(p);
274#else
275 (void)p;
276#endif
277}
278
279
280#ifdef __cplusplus
281} /* extern "C" */
282#endif
283
284#endif
285#endif
static int lfs_scmp(uint32_t a, uint32_t b)
Definition lfs_util.h:194
static uint32_t lfs_popc(uint32_t a)
Definition lfs_util.h:182
static uint32_t lfs_ctz(uint32_t a)
Definition lfs_util.h:173
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size)
Definition lfs_util.c:17
static uint32_t lfs_npw2(uint32_t a)
Definition lfs_util.h:156
static uint32_t lfs_frombe32(uint32_t a)
Definition lfs_util.h:222
static uint32_t lfs_tole32(uint32_t a)
Definition lfs_util.h:217
static void lfs_free(void *p)
Definition lfs_util.h:269
static void * lfs_malloc(size_t size)
Definition lfs_util.h:257
static uint32_t lfs_min(uint32_t a, uint32_t b)
Definition lfs_util.h:142
static uint32_t lfs_alignup(uint32_t a, uint32_t alignment)
Definition lfs_util.h:151
static uint32_t lfs_tobe32(uint32_t a)
Definition lfs_util.h:240
static uint32_t lfs_fromle32(uint32_t a)
Definition lfs_util.h:199
static uint32_t lfs_max(uint32_t a, uint32_t b)
Definition lfs_util.h:138
static uint32_t lfs_aligndown(uint32_t a, uint32_t alignment)
Definition lfs_util.h:147