Cleaned up lfs_t struct

- Removed no longer used fields.
- Commented out related field asserts in lfs_init.
- Commented out pre-lfsr structs and function decls.
- Moved cfg to the first field in lfs_t.

Note that most of the code saves actually came from that last point.
Moving lfs.cfg, probably the currently most accessed field, resulted in
a surprising amount of code savings:

                     code          stack          lfs_t
  before:           33702           2640            220
  after+cfg last:   33592 (-0.3%)   2632 (-0.3%)    164 (-25.5%)
  after+cfg first:  33358 (-1.0%)   2632 (-0.3%)    164 (-25.5%)

Maybe we should take a more rigorous/analytical approach to field
placement?
This commit is contained in:
Christopher Haster
2024-05-15 01:30:40 -05:00
parent 5c70013c11
commit 7980d0e21f
2 changed files with 180 additions and 214 deletions
+152 -173
View File
@@ -81,15 +81,15 @@ typedef int32_t lfsr_sdid_t;
//#ifndef LFS_ATTR_MAX
//#define LFS_ATTR_MAX 1022
//#endif
// TODO document
#ifndef LFS_UATTR_MAX
#define LFS_UATTR_MAX 255
#endif
#ifndef LFS_SATTR_MAX
#define LFS_SATTR_MAX 255
#endif
//
//// TODO document
//#ifndef LFS_UATTR_MAX
//#define LFS_UATTR_MAX 255
//#endif
//
//#ifndef LFS_SATTR_MAX
//#define LFS_SATTR_MAX 255
//#endif
// Possible error codes, these are negative to allow
@@ -258,10 +258,6 @@ struct lfs_config {
// in superblock and must be respected by other littlefs drivers.
lfs_size_t file_limit;
// TODO document
lfs_size_t uattr_limit;
lfs_size_t sattr_limit;
// TODO rm me
// // Optional upper limit on custom attributes in bytes. No downside for
// // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to
@@ -320,19 +316,19 @@ struct lfs_info {
char name[LFS_NAME_MAX+1];
};
// Custom attribute structure, used to describe custom attributes
// committed atomically during file writes.
struct lfs_attr {
// 8-bit type of attribute, provided by user and used to
// identify the attribute
uint8_t type;
// Pointer to buffer containing the attribute
void *buffer;
// Size of attribute in bytes, limited to LFS_ATTR_MAX
lfs_size_t size;
};
//// Custom attribute structure, used to describe custom attributes
//// committed atomically during file writes.
//struct lfs_attr {
// // 8-bit type of attribute, provided by user and used to
// // identify the attribute
// uint8_t type;
//
// // Pointer to buffer containing the attribute
// void *buffer;
//
// // Size of attribute in bytes, limited to LFS_ATTR_MAX
// lfs_size_t size;
//};
// Optional configuration provided during lfs_file_opencfg
struct lfs_file_config {
@@ -345,31 +341,32 @@ struct lfs_file_config {
// accesses. Defaults to file_buffer_size.
lfs_size_t buffer_size;
// Optional list of custom attributes related to the file. If the file
// is opened with read access, these attributes will be read from disk
// during the open call. If the file is opened with write access, the
// attributes will be written to disk every file sync or close. This
// write occurs atomically with update to the file's contents.
//
// Custom attributes are uniquely identified by an 8-bit type and limited
// to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller
// than the buffer, it will be padded with zeros. If the stored attribute
// is larger, then it will be silently truncated. If the attribute is not
// found, it will be created implicitly.
struct lfs_attr *attrs;
// Number of custom attributes in the list
lfs_size_t attr_count;
// // Optional list of custom attributes related to the file. If the file
// // is opened with read access, these attributes will be read from disk
// // during the open call. If the file is opened with write access, the
// // attributes will be written to disk every file sync or close. This
// // write occurs atomically with update to the file's contents.
// //
// // Custom attributes are uniquely identified by an 8-bit type and limited
// // to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller
// // than the buffer, it will be padded with zeros. If the stored attribute
// // is larger, then it will be silently truncated. If the attribute is not
// // found, it will be created implicitly.
// struct lfs_attr *attrs;
//
// // Number of custom attributes in the list
// lfs_size_t attr_count;
};
/// internal littlefs data structures ///
typedef struct lfs_cache {
lfs_block_t block;
lfs_size_t off;
lfs_size_t size;
uint8_t *buffer;
} lfs_cache_t;
//typedef struct lfs_cache {
// lfs_block_t block;
// lfs_size_t off;
// lfs_size_t size;
// uint8_t *buffer;
//} lfs_cache_t;
// TODO do we get ram savings with a lfsr_rorbyd_t substruct? need to measure
typedef struct lfsr_rbyd {
@@ -418,16 +415,16 @@ typedef struct lfsr_opened {
} lfsr_opened_t;
typedef struct lfs_mdir {
lfs_block_t pair[2];
uint32_t rev;
lfs_off_t off;
uint32_t etag;
uint16_t count;
bool erased;
bool split;
lfs_block_t tail[2];
} lfs_mdir_t;
//typedef struct lfs_mdir {
// lfs_block_t pair[2];
// uint32_t rev;
// lfs_off_t off;
// uint32_t etag;
// uint16_t count;
// bool erased;
// bool split;
// lfs_block_t tail[2];
//} lfs_mdir_t;
// either an on-disk or in-device data pointer
typedef struct lfsr_data {
@@ -444,15 +441,16 @@ typedef struct lfsr_data {
} lfsr_data_t;
// littlefs directory type
typedef struct lfs_dir {
struct lfs_dir *next;
uint16_t id;
uint8_t type;
lfs_mdir_t m;
lfs_off_t pos;
lfs_block_t head[2];
} lfs_dir_t;
//typedef struct lfs_dir {
// struct lfs_dir *next;
// uint16_t id;
// uint8_t type;
// lfs_mdir_t m;
//
// lfs_off_t pos;
// lfs_block_t head[2];
//} lfs_dir_t;
typedef struct lfsr_dir {
lfsr_opened_t p; // pos mdir
@@ -462,25 +460,26 @@ typedef struct lfsr_dir {
} lfsr_dir_t;
// littlefs file type
typedef struct lfs_file {
struct lfs_file *next;
uint16_t id;
uint8_t type;
lfs_mdir_t m;
struct lfs_ctz {
lfs_block_t head;
lfs_size_t size;
} ctz;
uint32_t flags;
lfs_off_t pos;
lfs_block_t block;
lfs_off_t off;
lfs_cache_t cache;
const struct lfs_file_config *cfg;
} lfs_file_t;
//typedef struct lfs_file {
// struct lfs_file *next;
// uint16_t id;
// uint8_t type;
// lfs_mdir_t m;
//
// struct lfs_ctz {
// lfs_block_t head;
// lfs_size_t size;
// } ctz;
//
// uint32_t flags;
// lfs_off_t pos;
// lfs_block_t block;
// lfs_off_t off;
// lfs_cache_t cache;
//
// const struct lfs_file_config *cfg;
//} lfs_file_t;
typedef lfsr_data_t lfsr_sprout_t;
@@ -512,6 +511,7 @@ typedef struct lfsr_bshrub {
typedef struct lfsr_file {
lfsr_opened_t m;
const struct lfs_file_config *cfg;
// files contain both an active bshrub and staging bshrub, to allow
// staging during mdir compacts
@@ -528,23 +528,21 @@ typedef struct lfsr_file {
lfs_block_t eblock;
lfs_size_t eoff;
const struct lfs_file_config *cfg;
} lfsr_file_t;
typedef struct lfs_superblock {
uint32_t version;
lfs_size_t block_size;
lfs_size_t block_count;
lfs_size_t name_max;
lfs_size_t file_max;
lfs_size_t attr_max;
} lfs_superblock_t;
typedef struct lfs_gstate {
uint32_t tag;
lfs_block_t pair[2];
} lfs_gstate_t;
//typedef struct lfs_superblock {
// uint32_t version;
// lfs_size_t block_size;
// lfs_size_t block_count;
// lfs_size_t name_max;
// lfs_size_t file_max;
// lfs_size_t attr_max;
//} lfs_superblock_t;
//
//typedef struct lfs_gstate {
// uint32_t tag;
// lfs_block_t pair[2];
//} lfs_gstate_t;
typedef struct lfsr_mtree {
union {
@@ -576,6 +574,24 @@ typedef struct lfsr_grm {
// The littlefs filesystem type
typedef struct lfs {
const struct lfs_config *cfg;
lfs_size_t name_limit;
lfs_off_t file_limit;
// TODO we should put this flag somewhere, should lfs_t have a general
// purpose flags field? this has been useful for lfsr_file_t
bool hasorphans;
uint8_t attr_estimate;
uint8_t mleaf_bits;
// linked-list of opened mdirs
lfsr_opened_t *opened;
lfsr_mdir_t mroot;
lfsr_mtree_t mtree;
uint32_t seed;
struct {
lfs_block_t block;
lfs_size_t off;
@@ -590,19 +606,6 @@ typedef struct lfs {
} pcache;
uint32_t pcksum;
lfs_block_t root[2];
struct lfs_mlist {
struct lfs_mlist *next;
uint16_t id;
uint8_t type;
lfs_mdir_t m;
} *mlist;
uint32_t seed;
lfs_gstate_t gstate;
lfs_gstate_t gdisk;
lfs_gstate_t gdelta;
struct lfs_lookahead {
lfs_block_t start;
lfs_block_t size;
@@ -611,33 +614,9 @@ typedef struct lfs {
uint8_t *buffer;
} lookahead;
const struct lfs_config *cfg;
lfs_size_t name_limit;
lfs_off_t file_limit;
lfs_size_t uattr_limit;
lfs_size_t sattr_limit;
// begin lfsr things
lfsr_grm_t grm;
uint8_t grm_g[LFSR_GRM_DSIZE];
uint8_t grm_d[LFSR_GRM_DSIZE];
// TODO we should put this flag somewhere, should lfs_t have a general
// purpose flags field? this has been useful for lfsr_file_t
bool hasorphans;
uint8_t attr_estimate;
uint8_t mleaf_bits;
lfsr_mdir_t mroot;
lfsr_mtree_t mtree;
// linked-list of opened mdirs
lfsr_opened_t *opened;
#ifdef LFS_MIGRATE
struct lfs1 *lfs1;
#endif
} lfs_t;
@@ -651,7 +630,7 @@ typedef struct lfs {
// be zeroed for defaults and backwards compatibility.
//
// Returns a negative error code on failure.
int lfs_format(lfs_t *lfs, const struct lfs_config *config);
//int lfs_format(lfs_t *lfs, const struct lfs_config *config);
int lfsr_format(lfs_t *lfs, const struct lfs_config *config);
#endif
@@ -663,14 +642,14 @@ int lfsr_format(lfs_t *lfs, const struct lfs_config *config);
// be zeroed for defaults and backwards compatibility.
//
// Returns a negative error code on failure.
int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
//int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
int lfsr_mount(lfs_t *lfs, const struct lfs_config *config);
// Unmounts a littlefs
//
// Does nothing besides releasing any allocated resources.
// Returns a negative error code on failure.
int lfs_unmount(lfs_t *lfs);
//int lfs_unmount(lfs_t *lfs);
int lfsr_unmount(lfs_t *lfs);
/// General operations ///
@@ -680,7 +659,7 @@ int lfsr_unmount(lfs_t *lfs);
//
// If removing a directory, the directory must be empty.
// Returns a negative error code on failure.
int lfs_remove(lfs_t *lfs, const char *path);
//int lfs_remove(lfs_t *lfs, const char *path);
int lfsr_remove(lfs_t *lfs, const char *path);
#endif
@@ -691,7 +670,7 @@ int lfsr_remove(lfs_t *lfs, const char *path);
// If the destination is a directory, the directory must be empty.
//
// Returns a negative error code on failure.
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
//int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
int lfsr_rename(lfs_t *lfs, const char *old_path, const char *new_path);
#endif
@@ -699,7 +678,7 @@ int lfsr_rename(lfs_t *lfs, const char *old_path, const char *new_path);
//
// Fills out the info structure, based on the specified file or directory.
// Returns a negative error code on failure.
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
//int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
int lfsr_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
// Get a custom attribute
@@ -714,8 +693,8 @@ int lfsr_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
// Note, the returned size is the size of the attribute on disk, irrespective
// of the size of the buffer. This can be used to dynamically allocate a buffer
// or check for existence.
lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
uint8_t type, void *buffer, lfs_size_t size);
//lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
// uint8_t type, void *buffer, lfs_size_t size);
#ifndef LFS_READONLY
// Set custom attributes
@@ -725,8 +704,8 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
// implicitly created.
//
// Returns a negative error code on failure.
int lfs_setattr(lfs_t *lfs, const char *path,
uint8_t type, const void *buffer, lfs_size_t size);
//int lfs_setattr(lfs_t *lfs, const char *path,
// uint8_t type, const void *buffer, lfs_size_t size);
#endif
#ifndef LFS_READONLY
@@ -735,7 +714,7 @@ int lfs_setattr(lfs_t *lfs, const char *path,
// If an attribute is not found, nothing happens.
//
// Returns a negative error code on failure.
int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
//int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
#endif
@@ -748,8 +727,8 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
// are values from the enum lfs_open_flags that are bitwise-ored together.
//
// Returns a negative error code on failure.
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags);
//int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
// const char *path, int flags);
int lfsr_file_open(lfs_t *lfs, lfsr_file_t *file,
const char *path, uint32_t flags);
@@ -767,9 +746,9 @@ int lfsr_file_open(lfs_t *lfs, lfsr_file_t *file,
// the config struct must be zeroed for defaults and backwards compatibility.
//
// Returns a negative error code on failure.
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags,
const struct lfs_file_config *config);
//int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
// const char *path, int flags,
// const struct lfs_file_config *config);
int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file,
const char *path, uint32_t flags,
const struct lfs_file_config *config);
@@ -785,7 +764,7 @@ int lfsr_file_opencfg(lfs_t *lfs, lfsr_file_t *file,
// return 0.
//
// Returns a negative error code on failure.
int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
//int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
int lfsr_file_close(lfs_t *lfs, lfsr_file_t *file);
// Synchronize a file on storage
@@ -796,7 +775,7 @@ int lfsr_file_close(lfs_t *lfs, lfsr_file_t *file);
// now recieve file updates and syncs on close.
//
// Returns a negative error code on failure.
int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
//int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
int lfsr_file_sync(lfs_t *lfs, lfsr_file_t *file);
// Mark a file as desynchronized
@@ -827,8 +806,8 @@ int lfsr_file_flush(lfs_t *lfs, lfsr_file_t *file);
//
// Takes a buffer and size indicating where to store the read data.
// Returns the number of bytes read, or a negative error code on failure.
lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
void *buffer, lfs_size_t size);
//lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
// void *buffer, lfs_size_t size);
lfs_ssize_t lfsr_file_read(lfs_t *lfs, lfsr_file_t *file,
void *buffer, lfs_size_t size);
@@ -839,8 +818,8 @@ lfs_ssize_t lfsr_file_read(lfs_t *lfs, lfsr_file_t *file,
// actually be updated on the storage until either sync or close is called.
//
// Returns the number of bytes written, or a negative error code on failure.
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
const void *buffer, lfs_size_t size);
//lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
// const void *buffer, lfs_size_t size);
lfs_ssize_t lfsr_file_write(lfs_t *lfs, lfsr_file_t *file,
const void *buffer, lfs_size_t size);
#endif
@@ -849,8 +828,8 @@ lfs_ssize_t lfsr_file_write(lfs_t *lfs, lfsr_file_t *file,
//
// The change in position is determined by the offset and whence flag.
// Returns the new position of the file, or a negative error code on failure.
lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
lfs_soff_t off, int whence);
//lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
// lfs_soff_t off, int whence);
lfs_soff_t lfsr_file_seek(lfs_t *lfs, lfsr_file_t *file,
lfs_soff_t off, uint8_t whence);
@@ -861,7 +840,7 @@ lfs_soff_t lfsr_file_seek(lfs_t *lfs, lfsr_file_t *file,
// as if the file was filled with zeros.
//
// Returns a negative error code on failure.
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
//int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
int lfsr_file_truncate(lfs_t *lfs, lfsr_file_t *file, lfs_off_t size);
#endif
@@ -879,21 +858,21 @@ int lfsr_file_fruncate(lfs_t *lfs, lfsr_file_t *file, lfs_off_t size);
//
// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
// Returns the position of the file, or a negative error code on failure.
lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
//lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
lfs_soff_t lfsr_file_tell(lfs_t *lfs, lfsr_file_t *file);
// Change the position of the file to the beginning of the file
//
// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET)
// Returns a negative error code on failure.
int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
//int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
int lfsr_file_rewind(lfs_t *lfs, lfsr_file_t *file);
// Return the size of the file
//
// Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END)
// Returns the size of the file, or a negative error code on failure.
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
//lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
lfs_soff_t lfsr_file_size(lfs_t *lfs, lfsr_file_t *file);
@@ -903,7 +882,7 @@ lfs_soff_t lfsr_file_size(lfs_t *lfs, lfsr_file_t *file);
// Create a directory
//
// Returns a negative error code on failure.
int lfs_mkdir(lfs_t *lfs, const char *path);
//int lfs_mkdir(lfs_t *lfs, const char *path);
int lfsr_mkdir(lfs_t *lfs, const char *path);
#endif
@@ -911,14 +890,14 @@ int lfsr_mkdir(lfs_t *lfs, const char *path);
//
// Once open a directory can be used with read to iterate over files.
// Returns a negative error code on failure.
int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
//int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
int lfsr_dir_open(lfs_t *lfs, lfsr_dir_t *dir, const char *path);
// Close a directory
//
// Releases any allocated resources.
// Returns a negative error code on failure.
int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
//int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
int lfsr_dir_close(lfs_t *lfs, lfsr_dir_t *dir);
// Read an entry in the directory
@@ -926,7 +905,7 @@ int lfsr_dir_close(lfs_t *lfs, lfsr_dir_t *dir);
// Fills out the info structure, based on the specified file or directory.
// Returns a positive value on success, 0 at the end of directory,
// or a negative error code on failure.
int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
//int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
int lfsr_dir_read(lfs_t *lfs, lfsr_dir_t *dir, struct lfs_info *info);
// Change the position of the directory
@@ -935,7 +914,7 @@ int lfsr_dir_read(lfs_t *lfs, lfsr_dir_t *dir, struct lfs_info *info);
// an absolute offset in the directory seek.
//
// Returns a negative error code on failure.
int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
//int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
int lfsr_dir_seek(lfs_t *lfs, lfsr_dir_t *dir, lfs_soff_t off);
// Return the position of the directory
@@ -944,13 +923,13 @@ int lfsr_dir_seek(lfs_t *lfs, lfsr_dir_t *dir, lfs_soff_t off);
// sense, but does indicate the current position in the directory iteration.
//
// Returns the position of the directory, or a negative error code on failure.
lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
//lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
lfs_soff_t lfsr_dir_tell(lfs_t *lfs, lfsr_dir_t *dir);
// Change the position of the directory to the beginning of the directory
//
// Returns a negative error code on failure.
int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
//int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
int lfsr_dir_rewind(lfs_t *lfs, lfsr_dir_t *dir);
@@ -962,7 +941,7 @@ int lfsr_dir_rewind(lfs_t *lfs, lfsr_dir_t *dir);
// size may be larger than the filesystem actually is.
//
// Returns the number of allocated blocks, or a negative error code on failure.
lfs_ssize_t lfs_fs_size(lfs_t *lfs);
//lfs_ssize_t lfs_fs_size(lfs_t *lfs);
lfs_ssize_t lfsr_fs_size(lfs_t *lfs);
// Traverse through all blocks in use by the filesystem
@@ -972,7 +951,7 @@ lfs_ssize_t lfsr_fs_size(lfs_t *lfs);
// blocks are in use or how much of the storage is available.
//
// Returns a negative error code on failure.
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
//int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
#ifndef LFS_READONLY
#ifdef LFS_MIGRATE
@@ -987,7 +966,7 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
// be zeroed for defaults and backwards compatibility.
//
// Returns a negative error code on failure.
int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg);
//int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg);
#endif
#endif