@@ -278,7 +278,7 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
278
278
% (lfs_soff_t )(lfs -> cfg -> block_count ))
279
279
+ lfs -> cfg -> block_count ) % lfs -> cfg -> block_count ;
280
280
281
- if (off < lfs -> cfg -> lookahead ) {
281
+ if (off < lfs -> free . size ) {
282
282
lfs -> free .buffer [off / 32 ] |= 1U << (off % 32 );
283
283
}
284
284
@@ -287,18 +287,7 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
287
287
288
288
static int lfs_alloc (lfs_t * lfs , lfs_block_t * block ) {
289
289
while (true) {
290
- while (true) {
291
- // check if we have looked at all blocks since last ack
292
- if (lfs -> free .begin + lfs -> free .off == lfs -> free .end ) {
293
- LFS_WARN ("No more free space %ld" , lfs -> free .end );
294
- return LFS_ERR_NOSPC ;
295
- }
296
-
297
- if (lfs -> free .off >= lfs_min (
298
- lfs -> cfg -> lookahead , lfs -> cfg -> block_count )) {
299
- break ;
300
- }
301
-
290
+ while (lfs -> free .off != lfs -> free .size ) {
302
291
lfs_block_t off = lfs -> free .off ;
303
292
lfs -> free .off += 1 ;
304
293
@@ -309,7 +298,15 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
309
298
}
310
299
}
311
300
312
- lfs -> free .begin += lfs_min (lfs -> cfg -> lookahead , lfs -> cfg -> block_count );
301
+ // check if we have looked at all blocks since last ack
302
+ if (lfs -> free .off == lfs -> free .ack - lfs -> free .begin ) {
303
+ LFS_WARN ("No more free space %ld" , lfs -> free .off + lfs -> free .begin );
304
+ return LFS_ERR_NOSPC ;
305
+ }
306
+
307
+ lfs -> free .begin += lfs -> free .size ;
308
+ lfs -> free .size = lfs_min (lfs -> cfg -> lookahead ,
309
+ lfs -> free .ack - lfs -> free .begin );
313
310
lfs -> free .off = 0 ;
314
311
315
312
// find mask of free blocks from tree
@@ -322,7 +319,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
322
319
}
323
320
324
321
static void lfs_alloc_ack (lfs_t * lfs ) {
325
- lfs -> free .end = lfs -> free .begin + lfs -> free .off + lfs -> cfg -> block_count ;
322
+ lfs -> free .ack = lfs -> free .off - 1 + lfs -> free .begin + lfs -> cfg -> block_count ;
326
323
}
327
324
328
325
@@ -1966,11 +1963,11 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
1966
1963
// create free lookahead
1967
1964
memset (lfs -> free .buffer , 0 , lfs -> cfg -> lookahead /8 );
1968
1965
lfs -> free .begin = 0 ;
1966
+ lfs -> free .size = lfs_min (lfs -> cfg -> lookahead , lfs -> cfg -> block_count );
1969
1967
lfs -> free .off = 0 ;
1970
- lfs -> free . end = lfs -> free . begin + lfs -> free . off + lfs -> cfg -> block_count ;
1968
+ lfs_alloc_ack ( lfs ) ;
1971
1969
1972
1970
// create superblock dir
1973
- lfs_alloc_ack (lfs );
1974
1971
lfs_dir_t superdir ;
1975
1972
err = lfs_dir_alloc (lfs , & superdir );
1976
1973
if (err ) {
@@ -2042,10 +2039,11 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2042
2039
return err ;
2043
2040
}
2044
2041
2045
- // setup free lookahead, rewind so first allocation triggers a scan
2046
- lfs -> free .begin = - lfs_min (lfs -> cfg -> lookahead , lfs -> cfg -> block_count );
2047
- lfs -> free .off = - lfs -> free .begin ;
2048
- lfs -> free .end = lfs -> free .begin + lfs -> free .off + lfs -> cfg -> block_count ;
2042
+ // setup free lookahead
2043
+ lfs -> free .begin = 0 ;
2044
+ lfs -> free .size = 0 ;
2045
+ lfs -> free .off = 0 ;
2046
+ lfs_alloc_ack (lfs );
2049
2047
2050
2048
// load superblock
2051
2049
lfs_dir_t dir ;
0 commit comments