Skip to content

Commit ee3be04

Browse files
committed
bptr: Adopted lfs3_bptr_alloc
This just wraps up block allocation + struct initialization similarly to lfs3_rbyd_alloc. Also tweaked bptr updates in lfs3_file_crystallize__ to mutate the bptr fields directly instead of going through lfs3_bptr_init. Neither of these impacted code cost, everything ends up inlined in lfs3_file_crystallize__ anyways. Hopefully it helps with readability at least. Also LFS3_KVONLY mode is completely broken, and fixing it is not a huge priority. I don't think it makes sense to adopt lfs3_bptr_alloc in lfs3_file_flushset_ anyways, it would just lead to us initializing the lfs3_bptr_t struct twice for no real reason. No code changes.
1 parent b67bdbb commit ee3be04

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

lfs3.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ static void lfs3_bptr_init(lfs3_bptr_t *bptr,
24022402
lfs3_data_t data, lfs3_size_t cksize, uint32_t cksum) {
24032403
// make sure the bptr flag is set
24042404
LFS3_ASSERT(lfs3_data_ondisk(data));
2405-
bptr->d.size = data.size | LFS3_DATA_ONDISK | LFS3_BPTR_ISBPTR;
2405+
bptr->d.size = LFS3_DATA_ONDISK | LFS3_BPTR_ISBPTR | data.size;
24062406
bptr->d.u.disk.block = data.u.disk.block;
24072407
bptr->d.u.disk.off = data.u.disk.off;
24082408
#ifdef LFS3_CKDATACKSUMS
@@ -2581,6 +2581,22 @@ static int lfs3_data_readbptr(lfs3_t *lfs3, lfs3_data_t *data,
25812581
}
25822582
#endif
25832583

2584+
2585+
// allocate a bptr
2586+
static int lfs3_bptr_alloc(lfs3_t *lfs3, lfs3_bptr_t *bptr) {
2587+
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
2588+
if (block < 0) {
2589+
return block;
2590+
}
2591+
2592+
lfs3_bptr_init(bptr,
2593+
LFS3_DATA_DISK(block, 0, 0),
2594+
// mark as erased
2595+
LFS3_BPTR_ISERASED | 0,
2596+
0);
2597+
return 0;
2598+
}
2599+
25842600
// needed in lfs3_bptr_fetch
25852601
#ifdef LFS3_CKFETCHES
25862602
static inline bool lfs3_m_isckfetches(uint32_t flags);
@@ -12657,32 +12673,28 @@ static int lfs3_file_crystallize__(lfs3_t *lfs3, lfs3_file_t *file,
1265712673
LFS3_ASSERT(pos_ - block_pos <= lfs3->cfg->block_size);
1265812674
file->leaf.pos = block_pos + lfs3_bptr_off(&file->leaf.bptr);
1265912675
file->leaf.weight = pos_ - file->leaf.pos;
12660-
lfs3_bptr_init(&file->leaf.bptr,
12661-
LFS3_DATA_DISK(
12662-
lfs3_bptr_block(&file->leaf.bptr),
12663-
lfs3_bptr_off(&file->leaf.bptr),
12664-
pos_ - file->leaf.pos),
12665-
// mark as erased
12666-
(pos_ - block_pos) | LFS3_BPTR_ISERASED,
12667-
lfs3->pcksum);
12676+
file->leaf.bptr.d.size = LFS3_DATA_ONDISK | LFS3_BPTR_ISBPTR
12677+
| (pos_ - file->leaf.pos);
12678+
// update cksize/cksum, mark as erased
12679+
LFS3_IFDEF_CKDATACKSUMS(
12680+
file->leaf.bptr.d.u.disk.cksize,
12681+
file->leaf.bptr.cksize) = LFS3_BPTR_ISERASED
12682+
| (pos_ - block_pos);
12683+
LFS3_IFDEF_CKDATACKSUMS(
12684+
file->leaf.bptr.d.u.disk.cksum,
12685+
file->leaf.bptr.cksum) = lfs3->pcksum;
1266812686
return 0;
1266912687

1267012688
relocate:;
1267112689
// allocate a new block
1267212690
//
1267312691
// note if we relocate, we rewrite the entire block from
1267412692
// block_pos using what we can find in our tree
12675-
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
12676-
if (block < 0) {
12677-
return block;
12693+
err = lfs3_bptr_alloc(lfs3, &file->leaf.bptr);
12694+
if (err) {
12695+
return err;
1267812696
}
1267912697

12680-
lfs3_bptr_init(&file->leaf.bptr,
12681-
LFS3_DATA_DISK(block, 0, 0),
12682-
// mark as erased
12683-
LFS3_BPTR_ISERASED | 0,
12684-
0);
12685-
1268612698
// mark as uncrystallized
1268712699
file->b.o.flags |= LFS3_o_UNCRYST;
1268812700
}
@@ -14268,9 +14280,7 @@ static int lfs3_init(lfs3_t *lfs3, uint32_t flags,
1426814280
| LFS3_IFDEF_CKPROGS(LFS3_M_CKPROGS, 0)
1426914281
| LFS3_IFDEF_CKFETCHES(LFS3_M_CKFETCHES, 0)
1427014282
| LFS3_IFDEF_CKMETAPARITY(LFS3_M_CKMETAPARITY, 0)
14271-
| LFS3_IFDEF_CKDATACKSUMS(
14272-
LFS3_M_CKDATACKSUMS,
14273-
0))) == 0);
14283+
| LFS3_IFDEF_CKDATACKSUMS(LFS3_M_CKDATACKSUMS, 0))) == 0);
1427414284
// LFS3_M_REVDBG and LFS3_M_REVNOISE are incompatible
1427514285
#if defined(LFS3_REVNOISE) && defined(LFS3_REVDBG)
1427614286
LFS3_ASSERT(!lfs3_m_isrevdbg(flags) || !lfs3_m_isrevnoise(flags));

0 commit comments

Comments
 (0)