Changed callbacks to take user-provided context directly

This is a style change to make littlefs's callbacks consistent with most
callback declarations found in C. That is, taking in a user-provided
`void*`.

Previously, these callbacks took a pointer to the config struct itself,
which indirectly contained a user provided context, and this gets the
job done, but taking in a callback with a `void*` is arguably more
expected, has a better chance of integrating with C++/OS-specific code,
and is more likely to be optimized out by a clever compiler.

---

As a part of these changes, the geometry for the test bds needed to be
moved into bd specific configuration objects. This is a good change as
it also allows for testing situations where littlefs's geometry does not
match the underlying bd.
This commit is contained in:
Christopher Haster
2020-11-26 09:16:20 -06:00
parent a549413077
commit a7cdd563f6
15 changed files with 383 additions and 397 deletions

View File

@@ -34,13 +34,43 @@ $(foreach target,$(SRC),$(eval $(FLATTEN)))
%.test: %.test.o $(foreach f,$(subst /,.,$(SRC:.c=.o)),%.$f)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
"""
GLOBALS = """
BEFORE_MAIN = """
const char *lfs_testbd_path;
uint32_t lfs_testbd_cycles;
int lfs_testbd_readctx(void *ctx, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size) {
return lfs_testbd_read((lfs_testbd_t*)ctx, block, off, buffer, size);
}
int lfs_testbd_progctx(void *ctx, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size) {
return lfs_testbd_prog((lfs_testbd_t*)ctx, block, off, buffer, size);
}
int lfs_testbd_erasectx(void *ctx, lfs_block_t block) {
return lfs_testbd_erase((lfs_testbd_t*)ctx, block);
}
int lfs_testbd_syncctx(void *ctx) {
return lfs_testbd_sync((lfs_testbd_t*)ctx);
}
"""
BEFORE_TESTS = """
//////////////// AUTOGENERATED TEST ////////////////
#include "lfs.h"
#include "bd/lfs_testbd.h"
#include <stdio.h>
extern const char *lfs_testbd_path;
extern uint32_t lfs_testbd_cycles;
extern int lfs_testbd_readctx(void *ctx, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size);
extern int lfs_testbd_progctx(void *ctx, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size);
extern int lfs_testbd_erasectx(void *ctx, lfs_block_t block);
extern int lfs_testbd_syncctx(void *ctx);
"""
DEFINES = {
'LFS_READ_SIZE': 16,
@@ -67,11 +97,11 @@ PROLOGUE = """
__attribute__((unused)) int err;
__attribute__((unused)) const struct lfs_cfg cfg = {
.context = &bd,
.read = lfs_testbd_read,
.prog = lfs_testbd_prog,
.erase = lfs_testbd_erase,
.sync = lfs_testbd_sync,
.ctx = &bd,
.read = lfs_testbd_readctx,
.prog = lfs_testbd_progctx,
.erase = lfs_testbd_erasectx,
.sync = lfs_testbd_syncctx,
.read_size = LFS_READ_SIZE,
.prog_size = LFS_PROG_SIZE,
.block_size = LFS_BLOCK_SIZE,
@@ -82,17 +112,21 @@ PROLOGUE = """
};
__attribute__((unused)) const struct lfs_testbd_cfg bdcfg = {
.read_size = LFS_READ_SIZE,
.prog_size = LFS_PROG_SIZE,
.erase_size = LFS_BLOCK_SIZE,
.erase_count = LFS_BLOCK_COUNT,
.erase_value = LFS_ERASE_VALUE,
.erase_cycles = LFS_ERASE_CYCLES,
.badblock_behavior = LFS_BADBLOCK_BEHAVIOR,
.power_cycles = lfs_testbd_cycles,
};
lfs_testbd_createcfg(&cfg, lfs_testbd_path, &bdcfg) => 0;
lfs_testbd_createcfg(&bd, lfs_testbd_path, &bdcfg) => 0;
"""
EPILOGUE = """
// epilogue
lfs_testbd_destroy(&cfg) => 0;
lfs_testbd_destroy(&bd) => 0;
"""
PASS = '\033[32m✓\033[0m'
FAIL = '\033[31m✗\033[0m'
@@ -468,7 +502,7 @@ class TestSuite:
def build(self, **args):
# build test files
tf = open(self.path + '.test.c.t', 'w')
tf.write(GLOBALS)
tf.write(BEFORE_TESTS)
if self.code is not None:
tf.write('#line %d "%s"\n' % (self.code_lineno, self.path))
tf.write(self.code)
@@ -483,14 +517,13 @@ class TestSuite:
for line in f:
tfs[case.in_].write(line)
tfs[case.in_].write('\n')
tfs[case.in_].write(GLOBALS)
tfs[case.in_].write(BEFORE_TESTS)
tfs[case.in_].write('\n')
case.build(tfs[case.in_], **args)
tf.write(BEFORE_MAIN)
tf.write('\n')
tf.write('const char *lfs_testbd_path;\n')
tf.write('uint32_t lfs_testbd_cycles;\n')
tf.write('int main(int argc, char **argv) {\n')
tf.write(4*' '+'int case_ = (argc > 1) ? atoi(argv[1]) : 0;\n')
tf.write(4*' '+'int perm = (argc > 2) ? atoi(argv[2]) : 0;\n')