Skip to content

Commit 3e47304

Browse files
committed
btree: Adopted LFS3_ERR_EXIST for terminating at shrubs
A bit of an abuse of this error code, but this is more explicit than the previous rattr_count > 0 condition. Forgetting to set rattr_count=0 on a normal exit has introduced bugs before. --- Though I'm not sure why this adds code. Somehow, _removing_ the rattr_count=0 statements when lfs3_btree_commit_ collapses the root added code? code stack ctx before: 36996 2392 652 after: 37020 (+0.1%) 2392 (+0.0%) 652 (+0.0%) Seriously, add bcommit->rattr_count = 0 to lfs3_btree_commit_ and the lfs3_btree_commit_'s code cost shrinks by 8 bytes. Is the compiler hiding stuff in bcommit? I'm just going to chalk this up to compiler noise for now...
1 parent 6d00354 commit 3e47304

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

lfs3.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5457,8 +5457,8 @@ static inline uint32_t lfs3_rev_btree(lfs3_t *lfs3);
54575457
// core btree algorithm
54585458
//
54595459
// this commits up to the root, but stops if:
5460-
// 1. we need a new root
5461-
// 2. we have a shrub root
5460+
// 1. we need a new root => LFS3_ERR_RANGE
5461+
// 2. we have a shrub root => LFS3_ERR_EXIST
54625462
//
54635463
// ---
54645464
//
@@ -5531,7 +5531,9 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
55315531
if (!lfs3_rbyd_trunk(&child)
55325532
|| lfs3_rbyd_isshrub(btree)) {
55335533
bcommit->bid = rid;
5534-
return (!lfs3_rbyd_trunk(&child)) ? LFS3_ERR_RANGE : 0;
5534+
return (!lfs3_rbyd_trunk(&child))
5535+
? LFS3_ERR_RANGE
5536+
: LFS3_ERR_EXIST;
55355537
}
55365538

55375539
// mark btree as unerased in case of failure, our btree rbyd and
@@ -5584,17 +5586,13 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
55845586
if (!lfs3_rbyd_trunk(&parent)) {
55855587
// update the root
55865588
// (note btree_ == child_)
5587-
// no new root needed
5588-
bcommit->rattr_count = 0;
55895589
return 0;
55905590
}
55915591

55925592
// is our parent the root and is the root degenerate?
55935593
if (child.weight == btree->weight) {
55945594
// collapse the root, decreasing the height of the tree
55955595
// (note btree_ == child_)
5596-
// no new root needed
5597-
bcommit->rattr_count = 0;
55985596
return 0;
55995597
}
56005598

@@ -6066,8 +6064,6 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
60666064
if (child.weight+sibling.weight == btree->weight) {
60676065
// collapse the root, decreasing the height of the tree
60686066
// (note btree_ == child_)
6069-
// no new root needed
6070-
bcommit->rattr_count = 0;
60716067
return 0;
60726068
}
60736069

@@ -6165,13 +6161,12 @@ static int lfs3_btree_commit(lfs3_t *lfs3, lfs3_btree_t *btree,
61656161
int err = lfs3_btree_commit_(lfs3, &btree_, btree,
61666162
&bcommit);
61676163
if (err && err != LFS3_ERR_RANGE) {
6164+
LFS3_ASSERT(err != LFS3_ERR_EXIST);
61686165
return err;
61696166
}
61706167

61716168
// needs a new root?
61726169
if (err == LFS3_ERR_RANGE) {
6173-
LFS3_ASSERT(bcommit.rattr_count > 0);
6174-
61756170
err = lfs3_btree_commitroot_(lfs3, &btree_, btree, true,
61766171
bcommit.bid, bcommit.rattrs, bcommit.rattr_count);
61776172
if (err) {
@@ -6913,15 +6908,16 @@ static int lfs3_bshrub_commit(lfs3_t *lfs3, lfs3_bshrub_t *bshrub,
69136908
bcommit.rattr_count = rattr_count;
69146909
int err = lfs3_btree_commit_(lfs3, &bshrub->shrub_, &bshrub->shrub,
69156910
&bcommit);
6916-
if (err && err != LFS3_ERR_RANGE) {
6911+
if (err && err != LFS3_ERR_EXIST
6912+
&& err != LFS3_ERR_RANGE) {
69176913
return err;
69186914
}
6919-
LFS3_ASSERT(!err || bcommit.rattr_count > 0);
69206915
bool split = (err == LFS3_ERR_RANGE);
69216916

69226917
// when btree is shrubbed, lfs3_btree_commit_ stops at the root
69236918
// and returns with pending rattrs
6924-
if (bcommit.rattr_count > 0) {
6919+
if (err == LFS3_ERR_EXIST
6920+
|| err == LFS3_ERR_RANGE) {
69256921
// try to commit to shrub root
69266922
err = lfs3_bshrub_commitroot_(lfs3, bshrub, split,
69276923
bcommit.bid, bcommit.rattrs, bcommit.rattr_count);
@@ -9609,9 +9605,9 @@ static inline bool lfs3_path_isdir(const char *path) {
96099605
//
96109606
// the errors get a bit subtle here, and rely on what ends up in the
96119607
// path/mdir:
9612-
// - 0 => file found
9613-
// - 0, lfs3_path_isdir(path) => dir found
9614-
// - 0, mdir.mid=-1 => root found
9608+
// - 0 => file found
9609+
// - 0, lfs3_path_isdir(path) => dir found
9610+
// - 0, mdir.mid=-1 => root found
96159611
// - LFS3_ERR_NOENT, lfs3_path_islast(path) => file not found
96169612
// - LFS3_ERR_NOENT, !lfs3_path_islast(path) => parent not found
96179613
// - LFS3_ERR_NOTDIR => parent not a dir

0 commit comments

Comments
 (0)