Dropped lfs_cmp for manual comparisons

So instead of:

  lfs_cmp(cmp) <= 0

You can do:

  cmp <= LFS_CMP_EQ

This is much simpler and still preserves the ability to use all of C's
comparison operators on the results of disk comparisons.
This commit is contained in:
Christopher Haster
2024-02-11 00:36:01 -06:00
parent 036047bbba
commit ddb86af059
2 changed files with 17 additions and 20 deletions
+13 -16
View File
@@ -21,20 +21,17 @@ enum {
LFS_OK_ORPHANED = 3, LFS_OK_ORPHANED = 3,
}; };
// a normal compare enum, but shifted up by one to allow unioning with // internally used disk-comparison enum
// negative error codes //
enum { // note LT < EQ < GT
LFS_CMP_LT = 0, enum lfs_scmp {
LFS_CMP_EQ = 1, LFS_CMP_LT = 0, // disk < query
LFS_CMP_GT = 2, LFS_CMP_EQ = 1, // disk = query
LFS_CMP_GT = 2, // disk > query
}; };
typedef int lfs_scmp_t; typedef int lfs_scmp_t;
static inline int lfs_cmp(lfs_scmp_t cmp) {
return cmp - 1;
}
/// Caching block device operations /// /// Caching block device operations ///
@@ -264,7 +261,7 @@ static int lfs_bd_flush(lfs_t *lfs,
return cmp; return cmp;
} }
if (lfs_cmp(cmp) != 0) { if (cmp != LFS_CMP_EQ) {
return LFS_ERR_CORRUPT; return LFS_ERR_CORRUPT;
} }
} }
@@ -3719,7 +3716,7 @@ static lfs_scmp_t lfsr_rbyd_namelookup(lfs_t *lfs, const lfsr_rbyd_t *rbyd,
} }
// bisect search space // bisect search space
if (lfs_cmp(cmp) > 0) { if (cmp > LFS_CMP_EQ) {
upper = rid__ - (weight__-1); upper = rid__ - (weight__-1);
// only keep track of best-match rids > our target if we haven't // only keep track of best-match rids > our target if we haven't
@@ -3739,7 +3736,7 @@ static lfs_scmp_t lfsr_rbyd_namelookup(lfs_t *lfs, const lfsr_rbyd_t *rbyd,
} }
} }
} else if (lfs_cmp(cmp) < 0) { } else if (cmp < LFS_CMP_EQ) {
lower = rid__ + 1; lower = rid__ + 1;
// keep track of best-matching rid < our target // keep track of best-matching rid < our target
@@ -6882,7 +6879,7 @@ static int lfsr_mdir_namelookup(lfs_t *lfs, const lfsr_mdir_t *mdir,
// note missing mids end up pointing to the next mid // note missing mids end up pointing to the next mid
lfsr_smid_t mid = LFSR_MID(lfs, lfsr_smid_t mid = LFSR_MID(lfs,
mdir->mid, mdir->mid,
(lfs_cmp(cmp) < 0) ? rid+1 : rid); (cmp < LFS_CMP_EQ) ? rid+1 : rid);
// intercept pending grms here and pretend they're orphaned files // intercept pending grms here and pretend they're orphaned files
// //
@@ -6899,7 +6896,7 @@ static int lfsr_mdir_namelookup(lfs_t *lfs, const lfsr_mdir_t *mdir,
if (tag_) { if (tag_) {
*tag_ = tag; *tag_ = tag;
} }
return (lfs_cmp(cmp) == 0) ? 0 : LFS_ERR_NOENT; return (cmp == LFS_CMP_EQ) ? 0 : LFS_ERR_NOENT;
} }
// lookup names in our mtree // lookup names in our mtree
@@ -7696,7 +7693,7 @@ static int lfsr_mountmroot(lfs_t *lfs, const lfsr_mdir_t *mroot) {
} }
// treat corrupted magic as no magic // treat corrupted magic as no magic
if (lfs_cmp(cmp) != 0) { if (cmp != LFS_CMP_EQ) {
LFS_ERROR("No littlefs magic found"); LFS_ERROR("No littlefs magic found");
return LFS_ERR_INVAL; return LFS_ERR_INVAL;
} }
+4 -4
View File
@@ -3896,8 +3896,8 @@ code = '''
lfs_scmp_t cmp = lfsr_btree_namelookup(&lfs, &btree, 0, name, 3, lfs_scmp_t cmp = lfsr_btree_namelookup(&lfs, &btree, 0, name, 3,
&split_bid, NULL, NULL, &split_data); &split_bid, NULL, NULL, &split_data);
assert(cmp >= 0); assert(cmp >= 0);
assert(lfs_cmp(cmp) != 0); assert(cmp != LFS_CMP_EQ);
if (lfs_cmp(cmp) > 0) { if (cmp > LFS_CMP_EQ) {
int err = lfsr_btree_commit(&lfs, &btree, int err = lfsr_btree_commit(&lfs, &btree,
split_bid, LFSR_ATTRS( split_bid, LFSR_ATTRS(
LFSR_ATTR(NAME, +1, CAT( LFSR_ATTR(NAME, +1, CAT(
@@ -4077,8 +4077,8 @@ code = '''
lfs_scmp_t cmp = lfsr_btree_namelookup(&lfs, &btree, 0, name, 3, lfs_scmp_t cmp = lfsr_btree_namelookup(&lfs, &btree, 0, name, 3,
&split_bid, NULL, &split_weight, &split_data); &split_bid, NULL, &split_weight, &split_data);
assert(cmp >= 0); assert(cmp >= 0);
assert(lfs_cmp(cmp) != 0); assert(cmp != LFS_CMP_EQ);
if (lfs_cmp(cmp) > 0) { if (cmp > LFS_CMP_EQ) {
int err = lfsr_btree_commit(&lfs, &btree, int err = lfsr_btree_commit(&lfs, &btree,
split_bid-(split_weight-1), LFSR_ATTRS( split_bid-(split_weight-1), LFSR_ATTRS(
LFSR_ATTR( LFSR_ATTR(