Pārlūkot izejas kodu

Add lfs_mlist_append/remove helper

Noah Gorny 5 gadi atpakaļ
vecāks
revīzija
008ebc37df
1 mainītis faili ar 18 papildinājumiem un 16 dzēšanām
  1. 18 16
      lfs.c

+ 18 - 16
lfs.c

@@ -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) {