Skip to content

Commit 8bb43ac

Browse files
committed
Dropped switch-case from lfs3_rbyd_appendrattr_
Now that lfs3_mtree_traverse_ uses a sort of state matrix, lfs3_rbyd_appendrattr_ is the only function still relying on a big switch-case statement. Replacing it with a series of if-else statements leaves the codebase switch-case free (ignoring test/bench runners, etc). Switch-case statements are extremely error prone in C, with the shared scope, implicit fallthrough, etc. And, with today's compilers, the result still ends up the same, so switch-case statements offer no benefit except maybe a more enjoyable syntax for masochists. Avoiding switch-case statements in code where we care about correctness is probably a good idea. No code changes
1 parent e9f2944 commit 8bb43ac

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

lfs3.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,30 +3551,26 @@ static int lfs3_rbyd_appendrattr_(lfs3_t *lfs3, lfs3_rbyd_t *rbyd,
35513551
} u;
35523552
} ctx;
35533553

3554-
switch (rattr.from) {
35553554
// direct buffer?
3556-
case LFS3_FROM_BUF:;
3555+
if (rattr.from == LFS3_FROM_BUF) {
35573556
ctx.u.data = LFS3_DATA_BUF(rattr.u.buffer, rattr.count);
35583557
datas = &ctx.u.data;
35593558
data_count = 1;
3560-
break;
35613559

35623560
// indirect concatenated data?
3563-
case LFS3_FROM_DATA:;
3561+
} else if (rattr.from == LFS3_FROM_DATA) {
35643562
datas = rattr.u.datas;
35653563
data_count = rattr.count;
3566-
break;
35673564

35683565
// le32?
3569-
case LFS3_FROM_LE32:;
3566+
} else if (rattr.from == LFS3_FROM_LE32) {
35703567
ctx.u.le32.data = lfs3_data_fromle32(rattr.u.le32,
35713568
ctx.u.le32.buf);
35723569
datas = &ctx.u.le32.data;
35733570
data_count = 1;
3574-
break;
35753571

35763572
// leb128?
3577-
case LFS3_FROM_LEB128:;
3573+
} else if (rattr.from == LFS3_FROM_LEB128) {
35783574
// leb128s should not exceed 31-bits
35793575
LFS3_ASSERT(rattr.u.leb128 <= 0x7fffffff);
35803576
// little-leb128s should not exceed 28-bits
@@ -3584,69 +3580,61 @@ static int lfs3_rbyd_appendrattr_(lfs3_t *lfs3, lfs3_rbyd_t *rbyd,
35843580
ctx.u.leb128.buf);
35853581
datas = &ctx.u.leb128.data;
35863582
data_count = 1;
3587-
break;
35883583

35893584
// name?
3590-
case LFS3_FROM_NAME:;
3585+
} else if (rattr.from == LFS3_FROM_NAME) {
35913586
const lfs3_name_t *name = rattr.u.etc;
35923587
ctx.u.name.datas[0] = lfs3_data_fromleb128(name->did, ctx.u.name.buf);
35933588
ctx.u.name.datas[1] = LFS3_DATA_BUF(name->name, name->name_len);
35943589
datas = ctx.u.name.datas;
35953590
data_count = 2;
3596-
break;
35973591

35983592
// bptr?
3599-
case LFS3_FROM_BPTR:;
3593+
} else if (rattr.from == LFS3_FROM_BPTR) {
36003594
ctx.u.bptr.data = lfs3_data_frombptr(rattr.u.etc,
36013595
ctx.u.bptr.buf);
36023596
datas = &ctx.u.bptr.data;
36033597
data_count = 1;
3604-
break;
36053598

36063599
// ecksum?
3607-
case LFS3_FROM_ECKSUM:;
3600+
} else if (rattr.from == LFS3_FROM_ECKSUM) {
36083601
ctx.u.ecksum.data = lfs3_data_fromecksum(rattr.u.etc,
36093602
ctx.u.ecksum.buf);
36103603
datas = &ctx.u.ecksum.data;
36113604
data_count = 1;
3612-
break;
36133605

36143606
// btree?
3615-
case LFS3_FROM_BTREE:;
3607+
} else if (rattr.from == LFS3_FROM_BTREE) {
36163608
ctx.u.btree.data = lfs3_data_frombtree(rattr.u.etc,
36173609
ctx.u.btree.buf);
36183610
datas = &ctx.u.btree.data;
36193611
data_count = 1;
3620-
break;
36213612

36223613
// shrub trunk?
3623-
case LFS3_FROM_SHRUB:;
3614+
} else if (rattr.from == LFS3_FROM_SHRUB) {
36243615
// note unlike the other lazy tags, we _need_ to lazily encode
36253616
// shrub trunks, since they change underneath us during mdir
36263617
// compactions, relocations, etc
36273618
ctx.u.shrub.data = lfs3_data_fromshrub(rattr.u.etc,
36283619
ctx.u.shrub.buf);
36293620
datas = &ctx.u.shrub.data;
36303621
data_count = 1;
3631-
break;
36323622

36333623
// mptr?
3634-
case LFS3_FROM_MPTR:;
3624+
} else if (rattr.from == LFS3_FROM_MPTR) {
36353625
ctx.u.mptr.data = lfs3_data_frommptr(rattr.u.etc,
36363626
ctx.u.mptr.buf);
36373627
datas = &ctx.u.mptr.data;
36383628
data_count = 1;
3639-
break;
36403629

36413630
// geometry?
3642-
case LFS3_FROM_GEOMETRY:;
3631+
} else if (rattr.from == LFS3_FROM_GEOMETRY) {
36433632
ctx.u.geometry.data = lfs3_data_fromgeometry(rattr.u.etc,
36443633
ctx.u.geometry.buf);
36453634
datas = &ctx.u.geometry.data;
36463635
data_count = 1;
3647-
break;
36483636

3649-
default:;
3637+
} else {
36503638
LFS3_UNREACHABLE();
36513639
}
36523640

0 commit comments

Comments
 (0)