Adopted 0xffffffff as null pointer

- Default value of most flash-based storage
- Avoids 0 == superblock/dir issue
- Usually causes assertions in bd driver layer
- Easier to notice in hex dumps
This commit is contained in:
Christopher Haster
2017-04-30 11:54:27 -05:00
parent 4808e9ae26
commit 8621b61f38

40
lfs.c
View File

@@ -17,6 +17,7 @@ static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache,
const lfs_cache_t *pcache, lfs_block_t block, const lfs_cache_t *pcache, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size) { lfs_off_t off, void *buffer, lfs_size_t size) {
uint8_t *data = buffer; uint8_t *data = buffer;
assert(block < lfs->cfg->block_count);
while (size > 0) { while (size > 0) {
if (pcache && block == pcache->block && off >= pcache->off && if (pcache && block == pcache->block && off >= pcache->off &&
@@ -73,14 +74,14 @@ static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache,
} }
static int lfs_cache_flush(lfs_t *lfs, lfs_cache_t *cache) { static int lfs_cache_flush(lfs_t *lfs, lfs_cache_t *cache) {
if (cache->off != -1) { if (cache->block != 0xffffffff) {
int err = lfs->cfg->prog(lfs->cfg, cache->block, int err = lfs->cfg->prog(lfs->cfg, cache->block,
cache->off, cache->buffer, lfs->cfg->prog_size); cache->off, cache->buffer, lfs->cfg->prog_size);
if (err) { if (err) {
return err; return err;
} }
cache->off = -1; cache->block = 0xffffffff;
} }
return 0; return 0;
@@ -89,6 +90,7 @@ static int lfs_cache_flush(lfs_t *lfs, lfs_cache_t *cache) {
static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *cache, lfs_block_t block, static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *cache, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size) { lfs_off_t off, const void *buffer, lfs_size_t size) {
const uint8_t *data = buffer; const uint8_t *data = buffer;
assert(block < lfs->cfg->block_count);
while (size > 0) { while (size > 0) {
if (block == cache->block && off >= cache->off && if (block == cache->block && off >= cache->off &&
@@ -115,7 +117,7 @@ static int lfs_cache_prog(lfs_t *lfs, lfs_cache_t *cache, lfs_block_t block,
// cache must have been flushed, either by programming and // cache must have been flushed, either by programming and
// entire block or manually flushing the cache // entire block or manually flushing the cache
assert(cache->off == -1); assert(cache->block == 0xffffffff);
if (off % lfs->cfg->prog_size == 0 && if (off % lfs->cfg->prog_size == 0 &&
size >= lfs->cfg->prog_size) { size >= lfs->cfg->prog_size) {
@@ -264,7 +266,7 @@ static inline void lfs_pairswap(lfs_block_t pair[2]) {
} }
static inline bool lfs_pairisnull(const lfs_block_t pair[2]) { static inline bool lfs_pairisnull(const lfs_block_t pair[2]) {
return !pair[0] && !pair[1]; return pair[0] == 0xffffffff || pair[1] == 0xffffffff;
} }
static inline int lfs_paircmp( static inline int lfs_paircmp(
@@ -298,8 +300,8 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_dir_t *dir) {
// set defaults // set defaults
dir->d.rev += 1; dir->d.rev += 1;
dir->d.size = sizeof(dir->d); dir->d.size = sizeof(dir->d);
dir->d.tail[0] = 0; dir->d.tail[0] = -1;
dir->d.tail[1] = 0; dir->d.tail[1] = -1;
dir->off = sizeof(dir->d); dir->off = sizeof(dir->d);
// don't write out yet, let caller take care of that // don't write out yet, let caller take care of that
@@ -872,7 +874,7 @@ static int lfs_index_find(lfs_t *lfs,
lfs_block_t head, lfs_size_t size, lfs_block_t head, lfs_size_t size,
lfs_size_t pos, lfs_block_t *block, lfs_off_t *off) { lfs_size_t pos, lfs_block_t *block, lfs_off_t *off) {
if (size == 0) { if (size == 0) {
*block = 0; *block = -1;
*off = 0; *off = 0;
return 0; return 0;
} }
@@ -1019,7 +1021,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
// create entry to remember name // create entry to remember name
entry.d.type = LFS_TYPE_REG; entry.d.type = LFS_TYPE_REG;
entry.d.len = sizeof(entry.d) + strlen(path); entry.d.len = sizeof(entry.d) + strlen(path);
entry.d.u.file.head = 0; entry.d.u.file.head = -1;
entry.d.u.file.size = 0; entry.d.u.file.size = 0;
err = lfs_dir_append(lfs, &cwd, &entry, path); err = lfs_dir_append(lfs, &cwd, &entry, path);
if (err) { if (err) {
@@ -1039,15 +1041,15 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
file->size = entry.d.u.file.size; file->size = entry.d.u.file.size;
file->flags = flags; file->flags = flags;
file->pos = 0; file->pos = 0;
file->block = 0; file->block = -1; // TODO rm me?
if (flags & LFS_O_TRUNC) { if (flags & LFS_O_TRUNC) {
file->head = 0; file->head = -1;
file->size = 0; file->size = 0;
} }
// allocate buffer if needed // allocate buffer if needed
file->cache.off = -1; file->cache.block = 0xffffffff;
if (lfs->cfg->file_buffer) { if (lfs->cfg->file_buffer) {
file->cache.buffer = lfs->cfg->file_buffer; file->cache.buffer = lfs->cfg->file_buffer;
} else if ((file->flags & 3) == LFS_O_RDONLY) { } else if ((file->flags & 3) == LFS_O_RDONLY) {
@@ -1091,7 +1093,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
if (file->flags & LFS_F_READING) { if (file->flags & LFS_F_READING) {
// just drop read cache // just drop read cache
file->cache.off = -1; file->cache.block = 0xffffffff;
file->flags &= ~LFS_F_READING; file->flags &= ~LFS_F_READING;
} }
@@ -1106,7 +1108,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
.pos = file->pos, .pos = file->pos,
.cache = lfs->rcache, .cache = lfs->rcache,
}; };
lfs->rcache.off = -1; lfs->rcache.block = 0xffffffff;
while (file->pos < file->size) { while (file->pos < file->size) {
// copy over a byte at a time, leave it up to caching // copy over a byte at a time, leave it up to caching
@@ -1123,9 +1125,9 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
} }
// keep our reference to the rcache in sync // keep our reference to the rcache in sync
if (lfs->rcache.off != -1) { if (lfs->rcache.block != 0xffffffff) {
orig.cache.off = -1; orig.cache.block = 0xffffffff;
lfs->rcache.off = -1; lfs->rcache.block = 0xffffffff;
} }
} }
@@ -1273,7 +1275,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
} }
// mark cache as dirty since we may have read data into it // mark cache as dirty since we may have read data into it
file->cache.off = -1; file->cache.block = 0xffffffff;
file->flags |= LFS_F_WRITING; file->flags |= LFS_F_WRITING;
} }
@@ -1515,7 +1517,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->words = lfs->cfg->block_size / sizeof(uint32_t); lfs->words = lfs->cfg->block_size / sizeof(uint32_t);
// setup read cache // setup read cache
lfs->rcache.off = -1; lfs->rcache.block = 0xffffffff;
if (lfs->cfg->read_buffer) { if (lfs->cfg->read_buffer) {
lfs->rcache.buffer = lfs->cfg->read_buffer; lfs->rcache.buffer = lfs->cfg->read_buffer;
} else { } else {
@@ -1526,7 +1528,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
} }
// setup program cache // setup program cache
lfs->pcache.off = -1; lfs->pcache.block = 0xffffffff;
if (lfs->cfg->prog_buffer) { if (lfs->cfg->prog_buffer) {
lfs->pcache.buffer = lfs->cfg->prog_buffer; lfs->pcache.buffer = lfs->cfg->prog_buffer;
} else { } else {