Skip to content

Commit 421271a

Browse files
committed
alloc: Added the concept of block allocator flags
Currently this just has one flag the replaces the previous `erase` argument: LFS3_ALLOC_ERASE 0x00000001 Please erase the block Benefits include: - Slightly better readability at lfs3_alloc call sites. - Possibility of more allocator flags in the future: - LFS3_ALLOC_EMERGENCY - Use reserved blocks - Uh, that's all I can think of right now No code changes.
1 parent 37df79f commit 421271a

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

lfs3.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,13 @@ static lfs3_scmp_t lfs3_attr_cmp(lfs3_t *lfs3, const struct lfs3_attr *attr,
23582358
// to avoid the mess of redeclaring flags and things, just declare
23592359
// everything we need here
23602360

2361+
// block allocator flags
2362+
#define LFS3_ALLOC_ERASE 0x000000001 // Please erase the block
2363+
2364+
static inline bool lfs3_alloc_iserase(uint32_t flags) {
2365+
return flags & LFS3_ALLOC_ERASE;
2366+
}
2367+
23612368
// checkpoint the allocator
23622369
//
23632370
// operations that need to alloc should call this when all in-use blocks
@@ -2376,7 +2383,7 @@ static inline void lfs3_alloc_discard(lfs3_t *lfs3);
23762383

23772384
// allocate a block
23782385
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY)
2379-
static lfs3_sblock_t lfs3_alloc(lfs3_t *lfs3, bool erase);
2386+
static lfs3_sblock_t lfs3_alloc(lfs3_t *lfs3, uint32_t flags);
23802387
#endif
23812388

23822389

@@ -2764,7 +2771,7 @@ static inline int lfs3_rbyd_cmp(
27642771
// allocate an rbyd block
27652772
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY)
27662773
static int lfs3_rbyd_alloc(lfs3_t *lfs3, lfs3_rbyd_t *rbyd) {
2767-
lfs3_sblock_t block = lfs3_alloc(lfs3, true);
2774+
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
27682775
if (block < 0) {
27692776
return block;
27702777
}
@@ -8045,7 +8052,7 @@ static int lfs3_mdir_alloc__(lfs3_t *lfs3, lfs3_mdir_t *mdir,
80458052

80468053
if (!partial) {
80478054
// allocate one block without an erase
8048-
lfs3_sblock_t block = lfs3_alloc(lfs3, false);
8055+
lfs3_sblock_t block = lfs3_alloc(lfs3, 0);
80498056
if (block < 0) {
80508057
return block;
80518058
}
@@ -8069,7 +8076,7 @@ static int lfs3_mdir_alloc__(lfs3_t *lfs3, lfs3_mdir_t *mdir,
80698076

80708077
relocate:;
80718078
// allocate another block with an erase
8072-
lfs3_sblock_t block = lfs3_alloc(lfs3, true);
8079+
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
80738080
if (block < 0) {
80748081
return block;
80758082
}
@@ -10418,7 +10425,7 @@ static inline lfs3_size_t lfs3_graft_count(lfs3_size_t graft_count);
1041810425

1041910426
// allocate a block
1042010427
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY)
10421-
static lfs3_sblock_t lfs3_alloc(lfs3_t *lfs3, bool erase) {
10428+
static lfs3_sblock_t lfs3_alloc(lfs3_t *lfs3, uint32_t flags) {
1042210429
while (true) {
1042310430
// scan our lookahead buffer for free blocks
1042410431
lfs3_sblock_t block = lfs3_alloc_findfree(lfs3);
@@ -10431,7 +10438,7 @@ static lfs3_sblock_t lfs3_alloc(lfs3_t *lfs3, bool erase) {
1043110438
LFS3_ASSERT(block != 0 && block != 1);
1043210439

1043310440
// erase requested?
10434-
if (erase) {
10441+
if (lfs3_alloc_iserase(flags)) {
1043510442
int err = lfs3_bd_erase(lfs3, block);
1043610443
if (err) {
1043710444
// bad erase? try another block
@@ -12666,7 +12673,7 @@ static int lfs3_file_crystallize__(lfs3_t *lfs3, lfs3_file_t *file,
1266612673
//
1266712674
// note if we relocate, we rewrite the entire block from
1266812675
// block_pos using what we can find in our tree
12669-
lfs3_sblock_t block = lfs3_alloc(lfs3, true);
12676+
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
1267012677
if (block < 0) {
1267112678
return block;
1267212679
}
@@ -12770,7 +12777,7 @@ static int lfs3_file_flushset_(lfs3_t *lfs3, lfs3_file_t *file,
1277012777

1277112778
relocate:;
1277212779
// allocate a new block
12773-
lfs3_sblock_t block = lfs3_alloc(lfs3, true);
12780+
lfs3_sblock_t block = lfs3_alloc(lfs3, LFS3_ALLOC_ERASE);
1277412781
if (block < 0) {
1277512782
return block;
1277612783
}

scripts/dbgflags.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
('--gc', 'GC', "Filter by LFS3_GC_* flags."),
1616
(['--i', '--info'], 'I', "Filter by LFS3_I_* flags."),
1717
(['--t', '--traversal'], 'T', "Filter by LFS3_T_* flags."),
18+
('--alloc', 'ALLOC', "Filter by LFS3_ALLOC_* flags."),
1819
(['--rc', '--rcompat'], 'RCOMPAT', "Filter by LFS3_RCOMPAT_* flags."),
1920
(['--wc', '--wcompat'], 'WCOMPAT', "Filter by LFS3_WCOMPAT_* flags."),
2021
(['--oc', '--ocompat'], 'OCOMPAT', "Filter by LFS3_OCOMPAT_* flags."),
@@ -155,6 +156,9 @@
155156
('t_DIRTY', 0x02000000, "Filesystem modified during traversal" ),
156157
('t_MUTATED', 0x01000000, "Filesystem modified by traversal" ),
157158

159+
# Block allocator flags
160+
('alloc_ERASE', 0x00000001, "Please erase the block" ),
161+
158162
# Read-compat flags
159163
('RCOMPAT_NONSTANDARD',
160164
0x00000001, "Non-standard filesystem format" ),

0 commit comments

Comments
 (0)