Moved erase into lfs_alloc, mostly

This doesn't really help us all that much right now, but will be useful
for the future-planned block map and being able to cache pre-erased
blocks.

Though the lack of erasing when allocating new mdirs raises some
questions... Oh well, future problems.

Code changes:

           code          stack
  before: 33856           2880
  after:  33864 (+0.0%)   2880 (+0.0%)
This commit is contained in:
Christopher Haster
2024-02-25 11:10:11 -06:00
parent 788a9d0129
commit 5005db2b4e
2 changed files with 17 additions and 20 deletions
+12 -17
View File
@@ -2120,7 +2120,7 @@ static int lfsr_data_readgrm(lfs_t *lfs, lfsr_data_t *data,
// predeclare block allocator functions
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block);
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block, bool erase);
static void lfs_alloc_ckpoint(lfs_t *lfs);
@@ -2159,13 +2159,7 @@ static inline int lfsr_rbyd_cmp(
// allocate an rbyd block
static int lfsr_rbyd_alloc(lfs_t *lfs, lfsr_rbyd_t *rbyd) {
*rbyd = (lfsr_rbyd_t){.weight=0, .trunk=0, .eoff=0, .cksum=0};
int err = lfs_alloc(lfs, &rbyd->blocks[0]);
if (err) {
return err;
}
// TODO should erase be implicit in alloc eventually?
err = lfsr_bd_erase(lfs, rbyd->blocks[0]);
int err = lfs_alloc(lfs, &rbyd->blocks[0], true);
if (err) {
return err;
}
@@ -5609,7 +5603,7 @@ static int lfsr_mdir_alloc__(lfs_t *lfs, lfsr_mdir_t *mdir, lfsr_smid_t mid) {
// allocate two blocks
for (int i = 0; i < 2; i++) {
int err = lfs_alloc(lfs, &mdir->rbyd.blocks[i]);
int err = lfs_alloc(lfs, &mdir->rbyd.blocks[i], false);
if (err) {
return err;
}
@@ -8253,7 +8247,7 @@ static inline void lfs_alloc_setinuse(lfs_t *lfs, lfs_block_t block) {
}
}
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block, bool erase) {
while (true) {
// scan our lookahead buffer for free blocks
while (lfs->lookahead.next < lfs->lookahead.size) {
@@ -8262,6 +8256,13 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
// found a free block
*block = (lfs->lookahead.start + lfs->lookahead.next)
% lfs->cfg->block_count;
// erase requested?
if (erase) {
int err = lfsr_bd_erase(lfs, *block);
if (err) {
return err;
}
}
// eagerly find next free block to maximize how many blocks
// lfs_alloc_ckpoint makes available for scanning
@@ -10519,13 +10520,7 @@ static int lfsr_file_flush_(lfs_t *lfs, lfsr_file_t *file,
}
// allocate a new block
int err = lfs_alloc(lfs, &bptr.data.u.disk.block);
if (err) {
return err;
}
// TODO should lfs_alloc handle erase?
err = lfsr_bd_erase(lfs, bptr.data.u.disk.block);
int err = lfs_alloc(lfs, &bptr.data.u.disk.block, true);
if (err) {
return err;
}
+5 -3
View File
@@ -17,6 +17,7 @@ after = ['test_mtree', 'test_dirs', 'test_files']
# test that we can alloc
[cases.test_alloc_alloc]
in = 'lfs.c'
defines.ERASE = [false, true]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
@@ -27,7 +28,7 @@ code = '''
lfs_size_t alloced = 0;
while (true) {
lfs_block_t block;
int err = lfs_alloc(&lfs, &block);
int err = lfs_alloc(&lfs, &block, ERASE);
assert(!err || err == LFS_ERR_NOSPC);
if (err == LFS_ERR_NOSPC) {
@@ -50,6 +51,7 @@ code = '''
# test that we can realloc after an ack
[cases.test_alloc_reuse]
in = 'lfs.c'
defines.ERASE = [false, true]
code = '''
lfs_t lfs;
lfsr_format(&lfs, CFG) => 0;
@@ -60,7 +62,7 @@ code = '''
lfs_size_t alloced = 0;
while (true) {
lfs_block_t block;
int err = lfs_alloc(&lfs, &block);
int err = lfs_alloc(&lfs, &block, ERASE);
assert(!err || err == LFS_ERR_NOSPC);
if (err == LFS_ERR_NOSPC) {
@@ -82,7 +84,7 @@ code = '''
alloced = 0;
while (true) {
lfs_block_t block;
int err = lfs_alloc(&lfs, &block);
int err = lfs_alloc(&lfs, &block, ERASE);
assert(!err || err == LFS_ERR_NOSPC);
if (err == LFS_ERR_NOSPC) {