Added lfs_memlen, replacing lfsr_gdelta_size

This drops the last lfsr_gdelta_* function, which were really just a
bunch of somewhat-quirky mem operations.

Moving this to lfs_util.h also allows users to override it with
hardware-specific tricks, though I think hardware tricks for lfs_memlen
will be quite rare. Reverse-order memory optimizations are pretty
uncommon...

This could also be done with a theoretical memrcchr, if one existed:

  p = memrcchr(buffer, 0, size);
  return (p) ? p - buffer : 0;

But I figured this use case is so niche we might as well just limit it
to c=0 (and avoid questions about memrchr).

No code changes.
This commit is contained in:
Christopher Haster
2025-01-18 16:10:31 -06:00
parent bac61a120b
commit bc959e4d0e
2 changed files with 14 additions and 14 deletions
+4 -14
View File
@@ -6818,17 +6818,6 @@ static void lfsr_fs_mkdirty(lfs_t *lfs) {
/// Global-state things ///
static inline lfs_size_t lfsr_gdelta_size(
const uint8_t *gdelta, lfs_size_t size) {
// truncate based on number of trailing zeros
while (size > 0 && gdelta[size-1] == 0) {
size -= 1;
}
return size;
}
// gcksum (global checksum) things
// cubing the gcksum prevents trivial gcksumdeltas
@@ -6886,7 +6875,7 @@ static lfsr_data_t lfsr_data_fromgrm(const lfsr_grm_t *grm,
d += d_;
}
return LFSR_DATA_BUF(buffer, lfsr_gdelta_size(buffer, LFSR_GRM_DSIZE));
return LFSR_DATA_BUF(buffer, lfs_memlen(buffer, LFSR_GRM_DSIZE));
}
// required by lfsr_data_readgrm
@@ -6922,6 +6911,7 @@ static int lfsr_data_readgrm(lfs_t *lfs, lfsr_data_t *data,
return 0;
}
// some mdir-related gstate things we need
static void lfsr_fs_flushgdelta(lfs_t *lfs) {
// zero any pending gdeltas
@@ -6960,7 +6950,7 @@ static int lfsr_rbyd_appendgdelta(lfs_t *lfs, lfsr_rbyd_t *rbyd) {
lfs_memxor(grmdelta_, lfs->grm_p, LFSR_GRM_DSIZE);
lfs_memxor(grmdelta_, lfs->grm_d, LFSR_GRM_DSIZE);
if (lfsr_gdelta_size(grmdelta_, LFSR_GRM_DSIZE) != 0) {
if (lfs_memlen(grmdelta_, LFSR_GRM_DSIZE) != 0) {
// make sure to xor any existing delta
lfsr_data_t data;
int err = lfsr_rbyd_lookup(lfs, rbyd, -1, LFSR_TAG_GRMDELTA,
@@ -6982,7 +6972,7 @@ static int lfsr_rbyd_appendgdelta(lfs_t *lfs, lfsr_rbyd_t *rbyd) {
lfs_memxor(grmdelta_, grmdelta, LFSR_GRM_DSIZE);
// append to our rbyd, replacing any existing delta
lfs_size_t size = lfsr_gdelta_size(grmdelta_, LFSR_GRM_DSIZE);
lfs_size_t size = lfs_memlen(grmdelta_, LFSR_GRM_DSIZE);
err = lfsr_rbyd_appendrat(lfs, rbyd, -1, LFSR_RAT(
// opportunistically remove this tag if delta is all zero
(size == 0)
+10
View File
@@ -473,6 +473,16 @@ static inline void *lfs_memcchr(const void *a, int c, size_t size) {
return NULL;
}
// Find the minimum length that includes all non-zero bytes
static inline size_t lfs_memlen(const void *a, size_t size) {
const uint8_t *a_ = a;
while (size > 0 && a_[size-1] == 0) {
size -= 1;
}
return size;
}
// Xor n bytes from b into a
static inline void *lfs_memxor(
void *restrict a, const void *restrict b, size_t size) {