Skip to content

Commit 362b0bb

Browse files
committed
Minor improvement to from-memory commits
Tweaked the commit callback to pass the arguments for from-memory commits explicitly, with non-from-memory commits still being able to hijack the opaque data pointer for additional state. The from-memory commits make up the vast majority of commits in littlefs, so this small change has a noticable impact.
1 parent e4a0cd9 commit 362b0bb

File tree

1 file changed

+35
-53
lines changed

1 file changed

+35
-53
lines changed

lfs.c

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ struct lfs_commit {
490490
lfs_off_t off;
491491
};
492492

493-
static int lfs_commit(lfs_t *lfs, struct lfs_commit *c, const void *data, lfs_size_t size) {
493+
static int lfs_commit(lfs_t *lfs, struct lfs_commit *c,
494+
const void *data, lfs_size_t size) {
494495
lfs_crc(&c->crc, data, size);
495496
int err = lfs_bd_prog(lfs, c->block, c->off, data, size);
496497
c->off += size;
@@ -500,46 +501,44 @@ static int lfs_commit(lfs_t *lfs, struct lfs_commit *c, const void *data, lfs_si
500501
struct lfs_region {
501502
lfs_off_t off;
502503
lfs_ssize_t diff;
503-
int (*commit)(lfs_t *lfs, struct lfs_commit *c, const void *p);
504-
const void *data;
505-
struct lfs_region *next;
506-
};
507504

508-
struct lfs_commit_mem {
505+
int (*commit)(lfs_t *lfs, struct lfs_commit *c,
506+
const void *data, lfs_size_t size);
509507
const void *data;
510508
lfs_size_t size;
509+
struct lfs_region *next;
511510
};
512511

513-
static int lfs_commit_mem(lfs_t *lfs, struct lfs_commit *c, const void *p) {
514-
const struct lfs_commit_mem *m = p;
515-
return lfs_commit(lfs, c, m->data, m->size);
512+
static int lfs_commit_mem(lfs_t *lfs, struct lfs_commit *c,
513+
const void *data, lfs_size_t size) {
514+
return lfs_commit(lfs, c, data, size);
516515
}
517516

518517
struct lfs_commit_disk {
519518
lfs_block_t block;
520519
lfs_off_t off;
521-
lfs_size_t size;
522520
struct lfs_region *regions;
523521
};
524522

525-
static int lfs_commit_disk(lfs_t *lfs, struct lfs_commit *c, const void *p) {
526-
const struct lfs_commit_disk *u = p;
523+
static int lfs_commit_disk(lfs_t *lfs, struct lfs_commit *c,
524+
const void *p, lfs_size_t size) {
525+
const struct lfs_commit_disk *d = p;
527526

528-
struct lfs_region *r = u->regions;
527+
struct lfs_region *r = d->regions;
529528
lfs_off_t off = 0;
530529
while (true) {
531530
if (r && r->off == off) {
532531
lfs_off_t orig = c->off;
533-
int err = r->commit(lfs, c, r->data);
532+
int err = r->commit(lfs, c, r->data, r->size);
534533
if (err) {
535534
return err;
536535
}
537536

538537
off += (c->off - orig) - r->diff;
539538
r = r->next;
540-
} else if (off < u->size) {
539+
} else if (off < size) {
541540
uint8_t data;
542-
int err = lfs_bd_read(lfs, u->block, u->off + off, &data, 1);
541+
int err = lfs_bd_read(lfs, d->block, d->off + off, &data, 1);
543542
if (err) {
544543
return err;
545544
}
@@ -590,12 +589,11 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
590589

591590
lfs_dir_tole32(&dir->d);
592591
err = lfs_commit_disk(lfs, &c, &(struct lfs_commit_disk){
593-
oldpair[1], 0, oldsize,
592+
oldpair[1], 0,
594593
&(struct lfs_region){
595594
0, 0,
596-
lfs_commit_mem, &(struct lfs_commit_mem){
597-
&dir->d, sizeof(dir->d)},
598-
regions}});
595+
lfs_commit_mem, &dir->d, sizeof(dir->d),
596+
regions}}, oldsize);
599597
lfs_dir_fromle32(&dir->d);
600598
if (err) {
601599
if (err == LFS_ERR_CORRUPT) {
@@ -754,8 +752,7 @@ static int lfs_dir_update(lfs_t *lfs, lfs_dir_t *dir,
754752
int err = lfs_dir_commit(lfs, &olddir,
755753
&(struct lfs_region){
756754
oldoff, 0,
757-
lfs_commit_mem, &(struct lfs_commit_mem){
758-
&entry->d.type, 1}});
755+
lfs_commit_mem, &entry->d.type, 1});
759756
if (err) {
760757
return err;
761758
}
@@ -766,7 +763,7 @@ static int lfs_dir_update(lfs_t *lfs, lfs_dir_t *dir,
766763
&(struct lfs_region){
767764
0, +lfs_entry_size(entry),
768765
lfs_commit_disk, &(struct lfs_commit_disk){
769-
olddir.pair[0], entry->off, oldsize, regions}});
766+
olddir.pair[0], entry->off, regions}, oldsize});
770767
if (err) {
771768
return err;
772769
}
@@ -775,8 +772,7 @@ static int lfs_dir_update(lfs_t *lfs, lfs_dir_t *dir,
775772
err = lfs_dir_commit(lfs, &olddir,
776773
&(struct lfs_region){
777774
oldoff, -oldsize,
778-
lfs_commit_mem, &(struct lfs_commit_mem){
779-
NULL, 0}});
775+
lfs_commit_mem, NULL, 0});
780776
if (err) {
781777
return err;
782778
}
@@ -829,8 +825,7 @@ static int lfs_dir_remove(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
829825
int err = lfs_dir_commit(lfs, dir,
830826
&(struct lfs_region){
831827
entry->off, -lfs_entry_size(entry),
832-
lfs_commit_mem, &(struct lfs_commit_mem){
833-
NULL, 0}});
828+
lfs_commit_mem, NULL, 0});
834829
if (err) {
835830
return err;
836831
}
@@ -1053,12 +1048,10 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
10531048
err = lfs_dir_append(lfs, &cwd, &entry,
10541049
&(struct lfs_region){
10551050
0, +sizeof(entry.d),
1056-
lfs_commit_mem, &(struct lfs_commit_mem){
1057-
&entry.d, sizeof(entry.d)},
1051+
lfs_commit_mem, &entry.d, sizeof(entry.d),
10581052
&(struct lfs_region){
10591053
0, +entry.d.nlen,
1060-
lfs_commit_mem, &(struct lfs_commit_mem){
1061-
path, entry.d.nlen}}});
1054+
lfs_commit_mem, path, entry.d.nlen}});
10621055
if (err) {
10631056
return err;
10641057
}
@@ -1445,12 +1438,10 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
14451438
err = lfs_dir_append(lfs, &cwd, &entry,
14461439
&(struct lfs_region){
14471440
0, +sizeof(entry.d),
1448-
lfs_commit_mem, &(struct lfs_commit_mem){
1449-
&entry.d, sizeof(entry.d)},
1441+
lfs_commit_mem, &entry.d, sizeof(entry.d),
14501442
&(struct lfs_region){
14511443
0, +entry.d.nlen,
1452-
lfs_commit_mem, &(struct lfs_commit_mem){
1453-
path, entry.d.nlen}}});
1444+
lfs_commit_mem, path, entry.d.nlen}});
14541445
if (err) {
14551446
return err;
14561447
}
@@ -1669,8 +1660,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
16691660
err = lfs_dir_update(lfs, &cwd, &entry,
16701661
&(struct lfs_region){
16711662
0, 0,
1672-
lfs_commit_mem, &(struct lfs_commit_mem){
1673-
&entry.d, sizeof(entry.d)}});
1663+
lfs_commit_mem, &entry.d, sizeof(entry.d)});
16741664
if (err) {
16751665
return err;
16761666
}
@@ -2095,8 +2085,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
20952085
err = lfs_dir_update(lfs, &oldcwd, &oldentry,
20962086
&(struct lfs_region){
20972087
0, 0,
2098-
lfs_commit_mem, &(struct lfs_commit_mem){
2099-
&oldentry.d, sizeof(oldentry.d)}});
2088+
lfs_commit_mem, &oldentry.d, sizeof(oldentry.d)});
21002089
if (err) {
21012090
return err;
21022091
}
@@ -2116,25 +2105,21 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
21162105
err = lfs_dir_update(lfs, &newcwd, &newentry,
21172106
&(struct lfs_region){
21182107
0, 0,
2119-
lfs_commit_mem, &(struct lfs_commit_mem){
2120-
&newentry.d, sizeof(newentry.d)},
2108+
lfs_commit_mem, &newentry.d, sizeof(newentry.d),
21212109
&(struct lfs_region){
21222110
sizeof(newentry.d), 0,
2123-
lfs_commit_mem, &(struct lfs_commit_mem){
2124-
newpath, newentry.d.nlen}}});
2111+
lfs_commit_mem, newpath, newentry.d.nlen}});
21252112
if (err) {
21262113
return err;
21272114
}
21282115
} else {
21292116
err = lfs_dir_append(lfs, &newcwd, &newentry,
21302117
&(struct lfs_region){
21312118
0, +sizeof(newentry.d),
2132-
lfs_commit_mem, &(struct lfs_commit_mem){
2133-
&newentry.d, sizeof(newentry.d)},
2119+
lfs_commit_mem, &newentry.d, sizeof(newentry.d),
21342120
&(struct lfs_region){
21352121
0, +newentry.d.nlen,
2136-
lfs_commit_mem, &(struct lfs_commit_mem){
2137-
newpath, newentry.d.nlen}}});
2122+
lfs_commit_mem, newpath, newentry.d.nlen}});
21382123
if (err) {
21392124
return err;
21402125
}
@@ -2302,8 +2287,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
23022287
for (int i = 0; i < 2; i++) {
23032288
err = lfs_dir_commit(lfs, &superdir, &(struct lfs_region){
23042289
sizeof(superdir.d), 0,
2305-
lfs_commit_mem, &(struct lfs_commit_mem){
2306-
&superblock.d, sizeof(superblock.d)}});
2290+
lfs_commit_mem, &superblock.d, sizeof(superblock.d)});
23072291
if (err && err != LFS_ERR_CORRUPT) {
23082292
return err;
23092293
}
@@ -2570,8 +2554,7 @@ static int lfs_relocate(lfs_t *lfs,
25702554
int err = lfs_dir_update(lfs, &parent, &entry,
25712555
&(struct lfs_region){
25722556
0, 0,
2573-
lfs_commit_mem, &(struct lfs_commit_mem){
2574-
&entry.d, sizeof(entry.d)}});
2557+
lfs_commit_mem, &entry.d, sizeof(entry.d)});
25752558
if (err) {
25762559
return err;
25772560
}
@@ -2698,8 +2681,7 @@ int lfs_deorphan(lfs_t *lfs) {
26982681
err = lfs_dir_update(lfs, &cwd, &entry,
26992682
&(struct lfs_region){
27002683
0, 0,
2701-
lfs_commit_mem, &(struct lfs_commit_mem){
2702-
&entry.d, sizeof(entry.d)}});
2684+
lfs_commit_mem, &entry.d, sizeof(entry.d)});
27032685
if (err) {
27042686
return err;
27052687
}

0 commit comments

Comments
 (0)