+/rtos
git clone https://git.julia.blue/+/rtos
350cd22
master
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 10#define LFS_NO_ASSERT 11// #define LFS_NO_MALLOC 12// #define LFS_NO_ERROR 13// #define LFS_NO_DEBUG 14// #define LFS_NO_WARN 15 16#define NULL 0 17#define PRIu8 "u" 18#define PRIu16 "u" 19#define PRIu32 "u" 20#define PRIx16 "x" 21#define PRIx32 "x" 22 23// Users can override lfs_util.h with their own configuration by defining 24// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h). 25// 26// If LFS_CONFIG is used, none of the default utils will be emitted and must be 27// provided by the config file. To start, I would suggest copying lfs_util.h 28// and modifying as needed. 29#ifdef LFS_CONFIG 30#define LFS_STRINGIZE (x ) LFS_STRINGIZE2(x) 31#define LFS_STRINGIZE2 (x ) #x 32#include LFS_STRINGIZE (LFS_CONFIG ) 33#else 34 35// System includes 36#include "std.h" 37#include <stdbool.h> 38#include <stdint.h> 39 40#ifndef LFS_NO_MALLOC 41#include "heap.h" 42#endif 43#ifndef LFS_NO_ASSERT 44#include <assert.h> 45#endif 46#if !defined(LFS_NO_DEBUG )|| !defined(LFS_NO_WARN )|| \ 47 !defined(LFS_NO_ERROR )|| defined(LFS_YES_TRACE ) 48#include "printf.h" 49#endif 50 51#ifdef __cplusplus 52extern "C" { 53#endif 54 55// Macros, may be replaced by system specific wrappers. Arguments to these 56// macros must not have side-effects as the macros can be removed for a smaller 57// code footprint 58 59// Logging functions 60#ifdef LFS_YES_TRACE 61#define LFS_TRACE_ (fmt , ...) \ 62 printf("%s:%d:trace: " fmt "%s\n\r", __FILE__, __LINE__, __VA_ARGS__) 63#define LFS_TRACE (...) LFS_TRACE_(__VA_ARGS__, "") 64#else 65#define LFS_TRACE (...) 66#endif 67 68#ifndef LFS_NO_DEBUG 69#define LFS_DEBUG_ (fmt , ...) \ 70 printf("%s:%d:debug: " fmt "%s\n\r", __FILE__, __LINE__, __VA_ARGS__) 71#define LFS_DEBUG (...) LFS_DEBUG_(__VA_ARGS__, "") 72#else 73#define LFS_DEBUG (...) 74#endif 75 76#ifndef LFS_NO_WARN 77#define LFS_WARN_ (fmt , ...) \ 78 printf("%s:%d:warn: " fmt "%s\n\r", __FILE__, __LINE__, __VA_ARGS__) 79#define LFS_WARN (...) LFS_WARN_(__VA_ARGS__, "") 80#else 81#define LFS_WARN (...) 82#endif 83 84#ifndef LFS_NO_ERROR 85#define LFS_ERROR_ (fmt , ...) \ 86 printf("%s:%d:error: " fmt "%s\n\r", __FILE__, __LINE__, __VA_ARGS__) 87#define LFS_ERROR (...) LFS_ERROR_(__VA_ARGS__, "") 88#else 89#define LFS_ERROR (...) 90#endif 91 92// Runtime assertions 93#ifndef LFS_NO_ASSERT 94#define LFS_ASSERT (test ) assert(test) 95#else 96#define LFS_ASSERT (test ) 97#endif 98 99// Builtin functions, these may be replaced by more efficient 100// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a 101// more expensive basic C implementation for debugging purposes 102 103// Min/max functions for unsigned 32-bit numbers 104static inline uint32_t lfs_max (uint32_t a ,uint32_t b ) { 105return (a > b ) ?a :b ; 106} 107 108static inline uint32_t lfs_min (uint32_t a ,uint32_t b ) { 109return (a < b ) ?a :b ; 110} 111 112// Align to nearest multiple of a size 113static inline uint32_t lfs_aligndown (uint32_t a ,uint32_t alignment ) { 114return a - (a %alignment ); 115} 116 117static inline uint32_t lfs_alignup (uint32_t a ,uint32_t alignment ) { 118return lfs_aligndown (a + alignment - 1 ,alignment ); 119} 120 121// Find the smallest power of 2 greater than or equal to a 122static inline uint32_t lfs_npw2 (uint32_t a ) { 123#if !defined(LFS_NO_INTRINSICS )&& (defined(__GNUC__ )|| defined(__CC_ARM )) 124return 32 - __builtin_clz (a - 1 ); 125#else 126uint32_t r = 0 ; 127uint32_t s ; 128a -= 1 ; 129s = (a > 0xffff ) <<4 ; 130a >>=s ; 131r |=s ; 132s = (a > 0xff ) <<3 ; 133a >>=s ; 134r |=s ; 135s = (a > 0xf ) <<2 ; 136a >>=s ; 137r |=s ; 138s = (a > 0x3 ) <<1 ; 139a >>=s ; 140r |=s ; 141return (r | (a >>1 ))+ 1 ; 142#endif 143} 144 145// Count the number of trailing binary zeros in a 146// lfs_ctz(0) may be undefined 147static inline uint32_t lfs_ctz (uint32_t a ) { 148#if !defined(LFS_NO_INTRINSICS )&& defined(__GNUC__ ) 149return __builtin_ctz (a ); 150#else 151return lfs_npw2 ((a & - a )+ 1 )- 1 ; 152#endif 153} 154 155// Count the number of binary ones in a 156static inline uint32_t lfs_popc (uint32_t a ) { 157#if !defined(LFS_NO_INTRINSICS )&& (defined(__GNUC__ )|| defined(__CC_ARM )) 158return __builtin_popcount (a ); 159#else 160a = a - ((a >>1 )& 0x55555555 ); 161a = (a & 0x33333333 )+ ((a >>2 )& 0x33333333 ); 162return (((a + (a >>4 ))& 0xf0f0f0f )* 0x1010101 ) >>24 ; 163#endif 164} 165 166// Find the sequence comparison of a and b, this is the distance 167// between a and b ignoring overflow 168static inline int lfs_scmp (uint32_t a ,uint32_t b ) { 169return (int )(unsigned )(a - b ); 170} 171 172// Convert between 32-bit little-endian and native order 173static inline uint32_t lfs_fromle32 (uint32_t a ) { 174#if !defined(LFS_NO_INTRINSICS )&& \ 175 ((defined(BYTE_ORDER )&& defined(ORDER_LITTLE_ENDIAN )&& \ 176BYTE_ORDER == ORDER_LITTLE_ENDIAN )|| \ 177 (defined(__BYTE_ORDER )&& defined(__ORDER_LITTLE_ENDIAN )&& \ 178__BYTE_ORDER == __ORDER_LITTLE_ENDIAN )|| \ 179 (defined(__BYTE_ORDER__ )&& defined(__ORDER_LITTLE_ENDIAN__ )&& \ 180__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )) 181return a ; 182#elif !defined(LFS_NO_INTRINSICS )&& \ 183 ((defined(BYTE_ORDER )&& defined(ORDER_BIG_ENDIAN )&& \ 184BYTE_ORDER == ORDER_BIG_ENDIAN )|| \ 185 (defined(__BYTE_ORDER )&& defined(__ORDER_BIG_ENDIAN )&& \ 186__BYTE_ORDER == __ORDER_BIG_ENDIAN )|| \ 187 (defined(__BYTE_ORDER__ )&& defined(__ORDER_BIG_ENDIAN__ )&& \ 188__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )) 189return __builtin_bswap32 (a ); 190#else 191return (((uint8_t * )& a )[0 ] <<0 ) | (((uint8_t * )& a )[1 ] <<8 ) | 192 (((uint8_t * )& a )[2 ] <<16 ) | (((uint8_t * )& a )[3 ] <<24 ); 193#endif 194} 195 196static inline uint32_t lfs_tole32 (uint32_t a ) { 197return lfs_fromle32 (a ); 198} 199 200// Convert between 32-bit big-endian and native order 201static inline uint32_t lfs_frombe32 (uint32_t a ) { 202#if !defined(LFS_NO_INTRINSICS )&& \ 203 ((defined(BYTE_ORDER )&& defined(ORDER_LITTLE_ENDIAN )&& \ 204BYTE_ORDER == ORDER_LITTLE_ENDIAN )|| \ 205 (defined(__BYTE_ORDER )&& defined(__ORDER_LITTLE_ENDIAN )&& \ 206__BYTE_ORDER == __ORDER_LITTLE_ENDIAN )|| \ 207 (defined(__BYTE_ORDER__ )&& defined(__ORDER_LITTLE_ENDIAN__ )&& \ 208__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )) 209return __builtin_bswap32 (a ); 210#elif !defined(LFS_NO_INTRINSICS )&& \ 211 ((defined(BYTE_ORDER )&& defined(ORDER_BIG_ENDIAN )&& \ 212BYTE_ORDER == ORDER_BIG_ENDIAN )|| \ 213 (defined(__BYTE_ORDER )&& defined(__ORDER_BIG_ENDIAN )&& \ 214__BYTE_ORDER == __ORDER_BIG_ENDIAN )|| \ 215 (defined(__BYTE_ORDER__ )&& defined(__ORDER_BIG_ENDIAN__ )&& \ 216__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )) 217return a ; 218#else 219return (((uint8_t * )& a )[0 ] <<24 ) | (((uint8_t * )& a )[1 ] <<16 ) | 220 (((uint8_t * )& a )[2 ] <<8 ) | (((uint8_t * )& a )[3 ] <<0 ); 221#endif 222} 223 224static inline uint32_t lfs_tobe32 (uint32_t a ) { 225return lfs_frombe32 (a ); 226} 227 228// Calculate CRC-32 with polynomial = 0x04c11db7 229uint32_t lfs_crc (uint32_t crc ,const void * buffer ,size_t size ); 230 231// Allocate memory, only used if buffers are not provided to littlefs 232// Note, memory must be 64-bit aligned 233static inline void * lfs_malloc (size_t size ) { 234#ifndef LFS_NO_MALLOC 235return malloc (size ); 236#else 237 (void )size ; 238return NULL ; 239#endif 240} 241 242// Deallocate memory, only used if buffers are not provided to littlefs 243static inline void lfs_free (void * p ) { 244#ifndef LFS_NO_MALLOC 245free (p ); 246#else 247 (void )p ; 248#endif 249} 250 251#ifdef __cplusplus 252} /* extern "C" */ 253#endif 254 255#endif 256#endif