Changed custom attribute descriptors to used arrays

While linked-lists do have some minor benefits, arrays are more
idiomatic in C and may provide a more intuitive API.

Initially the linked-list approach was more beneficial than it is now,
since it allowed custom attributes to be chained to internal linked
lists of attributes. However, this was dropped because exposing the
internal attribute list in this way created a rather messy user
interface that required strictly encoding the attributes with the
on-disk tag format.

Minor downside, users can no longer introduce custom attributes in
different layers (think OS vs app). Minor upside, the code size and
stack usage was reduced a bit.

Fortunately, this API can always be changed in the future without
breaking anything (except maybe API compatibility).
This commit is contained in:
Christopher Haster
2019-01-08 08:52:03 -06:00
parent 66d751544d
commit 51b2c7e4b6
3 changed files with 115 additions and 119 deletions

14
lfs.h
View File

@@ -123,6 +123,7 @@ enum lfs_type {
LFS_TYPE_MOVESTATE = 0x7ff,
// internal chip sources
LFS_FROM_NOOP = 0x000,
LFS_FROM_MOVE = 0x101,
LFS_FROM_USERATTRS = 0x102,
};
@@ -268,7 +269,8 @@ struct lfs_info {
char name[LFS_NAME_MAX+1];
};
// Custom attribute structure
// 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
@@ -279,9 +281,6 @@ struct lfs_attr {
// Size of attribute in bytes, limited to LFS_ATTR_MAX
lfs_size_t size;
// Pointer to next attribute in linked list
struct lfs_attr *next;
};
// Optional configuration provided during lfs_file_opencfg
@@ -290,8 +289,8 @@ struct lfs_file_config {
// By default lfs_malloc is used to allocate this buffer.
void *buffer;
// Optional linked list of custom attributes related to the file. If the
// file is opened with read access, the attributes will be read from
// 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.
@@ -302,6 +301,9 @@ struct lfs_file_config {
// 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;
};