diff --git a/bd/lfs_filebd.h b/bd/lfs_filebd.h index 0d56434..d35b133 100644 --- a/bd/lfs_filebd.h +++ b/bd/lfs_filebd.h @@ -8,7 +8,6 @@ #define LFS_FILEBD_H #include "lfs.h" -#include "lfs_util.h" #ifdef __cplusplus extern "C" diff --git a/bd/lfs_rambd.h b/bd/lfs_rambd.h index 56a45ce..2c2d121 100644 --- a/bd/lfs_rambd.h +++ b/bd/lfs_rambd.h @@ -8,7 +8,6 @@ #define LFS_RAMBD_H #include "lfs.h" -#include "lfs_util.h" #ifdef __cplusplus extern "C" diff --git a/bd/lfs_testbd.h b/bd/lfs_testbd.h index b1fb2e9..06b6190 100644 --- a/bd/lfs_testbd.h +++ b/bd/lfs_testbd.h @@ -9,7 +9,6 @@ #define LFS_TESTBD_H #include "lfs.h" -#include "lfs_util.h" #include "bd/lfs_rambd.h" #include "bd/lfs_filebd.h" diff --git a/lfs.c b/lfs.c index eb832fa..e2f90ee 100644 --- a/lfs.c +++ b/lfs.c @@ -5,7 +5,6 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include "lfs.h" -#include "lfs_util.h" #define LFS_BLOCK_NULL ((lfs_block_t)-1) #define LFS_BLOCK_INLINE ((lfs_block_t)-2) diff --git a/lfs.h b/lfs.h index 35bbbab..52e01b3 100644 --- a/lfs.h +++ b/lfs.h @@ -7,12 +7,10 @@ #ifndef LFS_H #define LFS_H -#include -#include +#include "lfs_util.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif @@ -66,26 +64,6 @@ typedef uint32_t lfs_block_t; #define LFS_ATTR_MAX 1022 #endif -// Possible error codes, these are negative to allow -// valid positive return values -enum lfs_error { - LFS_ERR_OK = 0, // No error - LFS_ERR_IO = -5, // Error during device operation - LFS_ERR_CORRUPT = -84, // Corrupted - LFS_ERR_NOENT = -2, // No directory entry - LFS_ERR_EXIST = -17, // Entry already exists - LFS_ERR_NOTDIR = -20, // Entry is not a dir - LFS_ERR_ISDIR = -21, // Entry is a dir - LFS_ERR_NOTEMPTY = -39, // Dir is not empty - LFS_ERR_BADF = -9, // Bad file number - LFS_ERR_FBIG = -27, // File too large - LFS_ERR_INVAL = -22, // Invalid parameter - LFS_ERR_NOSPC = -28, // No space left on device - LFS_ERR_NOMEM = -12, // No more memory available - LFS_ERR_NOATTR = -61, // No data/attr available - LFS_ERR_NAMETOOLONG = -36, // File name too long -}; - // File types enum lfs_type { // file types @@ -649,7 +627,7 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); #ifdef __cplusplus -} /* extern "C" */ +} #endif #endif diff --git a/lfs_util.c b/lfs_util.c index 0b60e3b..eb78bf3 100644 --- a/lfs_util.c +++ b/lfs_util.c @@ -7,7 +7,7 @@ #include "lfs_util.h" // Only compile if user does not provide custom config -#ifndef LFS_CONFIG +#ifndef LFS_UTIL // Software CRC implementation with small lookup table diff --git a/lfs_util.h b/lfs_util.h index dbb4c5b..2a7d685 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -3,20 +3,21 @@ * * Copyright (c) 2017, Arm Limited. All rights reserved. * SPDX-License-Identifier: BSD-3-Clause + * + * Can be overridden by users with their own configuration by defining + * LFS_UTIL as a header file (-DLFS_UTIL=my_lfs_util.h) + * + * If LFS_UTIL is defined, none of the default definitions will be + * emitted and must be provided by the user's header file. To start, I would + * suggest copying lfs_util.h and modifying as needed. */ #ifndef LFS_UTIL_H #define LFS_UTIL_H -// Users can override lfs_util.h with their own configuration by defining -// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h). -// -// If LFS_CONFIG is used, none of the default utils will be emitted and must be -// provided by the config file. To start, I would suggest copying lfs_util.h -// and modifying as needed. -#ifdef LFS_CONFIG +#ifdef LFS_UTIL #define LFS_STRINGIZE(x) LFS_STRINGIZE2(x) #define LFS_STRINGIZE2(x) #x -#include LFS_STRINGIZE(LFS_CONFIG) +#include LFS_STRINGIZE(LFS_UTIL) #else // System includes @@ -39,11 +40,32 @@ #endif #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif +// Possible error codes, these are negative to allow valid positive +// return values. May be redefined to system-specific error codes as long +// as they fit in a negative integer. +enum lfs_error { + LFS_ERR_OK = 0, // No error + LFS_ERR_IO = -5, // Error during device operation + LFS_ERR_CORRUPT = -84, // Corrupted + LFS_ERR_NOENT = -2, // No directory entry + LFS_ERR_EXIST = -17, // Entry already exists + LFS_ERR_NOTDIR = -20, // Entry is not a dir + LFS_ERR_ISDIR = -21, // Entry is a dir + LFS_ERR_NOTEMPTY = -39, // Dir is not empty + LFS_ERR_BADF = -9, // Bad file number + LFS_ERR_FBIG = -27, // File too large + LFS_ERR_INVAL = -22, // Invalid parameter + LFS_ERR_NOSPC = -28, // No space left on device + LFS_ERR_NOMEM = -12, // No more memory available + LFS_ERR_NOATTR = -61, // No data/attr available + LFS_ERR_NAMETOOLONG = -36, // File name too long +}; + + // Macros, may be replaced by system specific wrappers. Arguments to these // macros must not have side-effects as the macros can be removed for a smaller // code footprint @@ -150,8 +172,8 @@ static inline uint32_t lfs_popc(uint32_t a) { // Find the sequence comparison of a and b, this is the distance // between a and b ignoring overflow -static inline int lfs_scmp(uint32_t a, uint32_t b) { - return (int)(unsigned)(a - b); +static inline int32_t lfs_scmp(uint32_t a, uint32_t b) { + return (int32_t)(uint32_t)(a - b); } // Convert between 32-bit little-endian and native order @@ -227,7 +249,7 @@ static inline void lfs_free(void *p) { #ifdef __cplusplus -} /* extern "C" */ +} #endif #endif