diff options
author | Noah Gorny <noah@gittabags.com> | 2020-11-18 01:20:34 +0300 |
---|---|---|
committer | Christopher Haster <chaster@utexas.edu> | 2020-12-04 07:42:39 +0300 |
commit | 008ebc37dfe09c67b7b8b3b511f44399d3c3c684 (patch) | |
tree | 0e2fd15cc946aa5d8f44d13f37f1ab6709539f2d /lfs.c | |
parent | 6303558aee5c7ab9a5eb3ac495b3094647e962b4 (diff) |
Add lfs_mlist_append/remove helper
Diffstat (limited to 'lfs.c')
-rw-r--r-- | lfs.c | 34 |
1 files changed, 18 insertions, 16 deletions
@@ -422,6 +422,20 @@ static inline bool lfs_mlist_isopen(struct lfs_mlist *head, return false; } +static inline void lfs_mlist_remove(lfs_t *lfs, struct lfs_mlist *mlist) { + for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { + if (*p == mlist) { + *p = (*p)->next; + break; + } + } +} + +static inline void lfs_mlist_append(lfs_t *lfs, struct lfs_mlist *mlist) { + mlist->next = lfs->mlist; + lfs->mlist = mlist; +} + /// Internal operations predeclared here /// static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, @@ -2062,8 +2076,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { // add to list of mdirs dir->type = LFS_TYPE_DIR; - dir->next = (lfs_dir_t*)lfs->mlist; - lfs->mlist = (struct lfs_mlist*)dir; + lfs_mlist_append(lfs, (struct lfs_mlist *)dir); LFS_TRACE("lfs_dir_open -> %d", 0); return 0; @@ -2072,12 +2085,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs - for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs_mlist*)dir) { - *p = (*p)->next; - break; - } - } + lfs_mlist_remove(lfs, (struct lfs_mlist *)dir); LFS_TRACE("lfs_dir_close -> %d", 0); return 0; @@ -2428,8 +2436,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // get id, add to list of mdirs to catch update changes file->type = LFS_TYPE_REG; - file->next = (lfs_file_t*)lfs->mlist; - lfs->mlist = (struct lfs_mlist*)file; + lfs_mlist_append(lfs, (struct lfs_mlist *)file); if (tag == LFS_ERR_NOENT) { if (!(flags & LFS_O_CREAT)) { @@ -2565,12 +2572,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { int err = lfs_file_sync(lfs, file); // remove from list of mdirs - for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs_mlist*)file) { - *p = (*p)->next; - break; - } - } + lfs_mlist_remove(lfs, (struct lfs_mlist*)file); // clean up memory if (!file->cfg->buffer) { |