Added clarification on buffer alignment.
In v2, the lookahead_buffer was changed from requiring 4-byte alignment to requiring 8-byte alignment. This was not documented as well as it could be, and as FabianInostroza noted, this also implies that lfs_malloc must provide 8-byte alignment. To protect against this, I've also added an assert on the alignment of both the lookahead_size and lookahead_buffer. found by FabianInostroza and amitv87
This commit is contained in:
@@ -3228,8 +3228,9 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
|||||||
lfs_cache_zero(lfs, &lfs->pcache);
|
lfs_cache_zero(lfs, &lfs->pcache);
|
||||||
|
|
||||||
// setup lookahead, must be multiple of 64-bits
|
// setup lookahead, must be multiple of 64-bits
|
||||||
LFS_ASSERT(lfs->cfg->lookahead_size % 8 == 0);
|
|
||||||
LFS_ASSERT(lfs->cfg->lookahead_size > 0);
|
LFS_ASSERT(lfs->cfg->lookahead_size > 0);
|
||||||
|
LFS_ASSERT(lfs->cfg->lookahead_size % 8 == 0 &&
|
||||||
|
(uintptr_t)lfs->cfg->lookahead_buffer % 8 == 0);
|
||||||
if (lfs->cfg->lookahead_buffer) {
|
if (lfs->cfg->lookahead_buffer) {
|
||||||
lfs->free.buffer = lfs->cfg->lookahead_buffer;
|
lfs->free.buffer = lfs->cfg->lookahead_buffer;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -215,8 +215,9 @@ struct lfs_config {
|
|||||||
// By default lfs_malloc is used to allocate this buffer.
|
// By default lfs_malloc is used to allocate this buffer.
|
||||||
void *prog_buffer;
|
void *prog_buffer;
|
||||||
|
|
||||||
// Optional statically allocated program buffer. Must be lookahead_size.
|
// Optional statically allocated lookahead buffer. Must be lookahead_size
|
||||||
// By default lfs_malloc is used to allocate this buffer.
|
// and aligned to a 64-bit boundary. By default lfs_malloc is used to
|
||||||
|
// allocate this buffer.
|
||||||
void *lookahead_buffer;
|
void *lookahead_buffer;
|
||||||
|
|
||||||
// Optional upper limit on length of file names in bytes. No downside for
|
// Optional upper limit on length of file names in bytes. No downside for
|
||||||
|
|||||||
@@ -192,6 +192,7 @@ static inline uint32_t lfs_tobe32(uint32_t a) {
|
|||||||
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
|
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
|
||||||
|
|
||||||
// Allocate memory, only used if buffers are not provided to littlefs
|
// Allocate memory, only used if buffers are not provided to littlefs
|
||||||
|
// Note, memory must be 64-bit aligned
|
||||||
static inline void *lfs_malloc(size_t size) {
|
static inline void *lfs_malloc(size_t size) {
|
||||||
#ifndef LFS_NO_MALLOC
|
#ifndef LFS_NO_MALLOC
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
|
|||||||
Reference in New Issue
Block a user