]> Sergey Matveev's repositories - bfs.git/commitdiff
Use the new list macros
authorTavian Barnes <tavianator@tavianator.com>
Mon, 25 Sep 2023 17:58:19 +0000 (13:58 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Mon, 25 Sep 2023 18:15:01 +0000 (14:15 -0400)
src/bftw.c
src/color.c
src/ctx.c
src/trie.h
src/xspawn.c
tests/trie.c

index e6b8cd5bf1867fbfcbbfa3acdfae38fd52e247d5..f3060ce617b5e792786f4af93293333aa362a193 100644 (file)
@@ -318,8 +318,7 @@ static size_t bftw_child_nameoff(const struct bftw_file *parent) {
 
 /** Destroy a cache. */
 static void bftw_cache_destroy(struct bftw_cache *cache) {
-       bfs_assert(!cache->head);
-       bfs_assert(!cache->tail);
+       bfs_assert(LIST_EMPTY(cache));
        bfs_assert(!cache->target);
 
        varena_destroy(&cache->files);
@@ -347,9 +346,9 @@ static struct bftw_file *bftw_file_new(struct bftw_cache *cache, struct bftw_fil
                file->nameoff = 0;
        }
 
-       file->next = NULL;
-       file->to_read.next = NULL;
-       file->lru.prev = file->lru.next = NULL;
+       SLIST_ITEM_INIT(file);
+       SLIST_ITEM_INIT(file, to_read);
+       LIST_ITEM_INIT(file, lru);
 
        file->refcount = 1;
        file->pincount = 0;
@@ -833,12 +832,11 @@ static void bftw_push_dir(struct bftw_state *state, struct bftw_file *file) {
                SLIST_APPEND(&state->to_read, file, to_read);
        }
 
-       while (state->to_open.head) {
-               if (bftw_ioq_opendir(state, state->to_open.head) == 0) {
-                       SLIST_POP(&state->to_open);
-               } else {
+       for_slist (struct bftw_file, dir, &state->to_open) {
+               if (bftw_ioq_opendir(state, dir) != 0) {
                        break;
                }
+               SLIST_POP(&state->to_open);
        }
 }
 
@@ -847,7 +845,7 @@ static bool bftw_pop_dir(struct bftw_state *state) {
        bfs_assert(!state->file);
 
        struct bftw_cache *cache = &state->cache;
-       bool have_files = state->to_visit.head;
+       bool have_files = !SLIST_EMPTY(&state->to_visit);
 
        if (state->flags & BFTW_SORT) {
                // Keep strict breadth-first order when sorting
@@ -855,9 +853,9 @@ static bool bftw_pop_dir(struct bftw_state *state) {
                        return false;
                }
        } else {
-               while (!state->to_read.head) {
+               while (SLIST_EMPTY(&state->to_read)) {
                        // Block if we have no other files/dirs to visit, or no room in the cache
-                       bool have_dirs = state->to_open.head;
+                       bool have_dirs = !SLIST_EMPTY(&state->to_open);
                        bool have_room = cache->capacity > 0 && cache->dirlimit > 0;
                        bool block = !(have_dirs || have_files) || !have_room;
 
@@ -1303,7 +1301,7 @@ static void bftw_list_sort(struct bftw_list *list) {
        bftw_list_sort(&right);
 
        // Merge
-       while (left.head && right.head) {
+       while (!SLIST_EMPTY(&left) && !SLIST_EMPTY(&right)) {
                struct bftw_file *lf = left.head;
                struct bftw_file *rf = right.head;
 
index b9a788bd13a34c1648a722263503d4a69bdc525a..5e78c6c9292899fba03d672f25f69b3c7505a43f 100644 (file)
@@ -328,7 +328,7 @@ fail:
 static int build_iext_trie(struct colors *colors) {
        trie_clear(&colors->iext_trie);
 
-       TRIE_FOR_EACH(&colors->ext_trie, leaf) {
+       for_trie (leaf, &colors->ext_trie) {
                size_t len = leaf->length - 1;
                if (colors->ext_len < len) {
                        colors->ext_len = len;
index a940bed0fe8c88389665d3cc0554b634bd74fb5a..3a44e68a03481f7f738bf00905930861e7a42b21 100644 (file)
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -152,7 +152,7 @@ void bfs_ctx_flush(const struct bfs_ctx *ctx) {
        // - the user sees everything relevant before an -ok[dir] prompt
        // - output from commands is interleaved consistently with bfs
        // - executed commands can rely on I/O from other bfs actions
-       TRIE_FOR_EACH(&ctx->files, leaf) {
+       for_trie (leaf, &ctx->files) {
                struct bfs_ctx_file *ctx_file = leaf->value;
                CFILE *cfile = ctx_file->cfile;
                if (fflush(cfile->file) == 0) {
@@ -239,7 +239,7 @@ int bfs_ctx_free(struct bfs_ctx *ctx) {
                bfs_groups_free(ctx->groups);
                bfs_users_free(ctx->users);
 
-               TRIE_FOR_EACH(&ctx->files, leaf) {
+               for_trie (leaf, &ctx->files) {
                        struct bfs_ctx_file *ctx_file = leaf->value;
 
                        if (ctx_file->error) {
index dfaae154de3127a577090d45ca72d0ee1a4cc772..2f51db59d19205c4544d4088f6371e767f41cda9 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "config.h"
 #include "alloc.h"
+#include "list.h"
 #include <stddef.h>
 #include <stdint.h>
 
@@ -141,9 +142,7 @@ void trie_destroy(struct trie *trie);
 /**
  * Iterate over the leaves of a trie.
  */
-#define TRIE_FOR_EACH(trie, leaf) \
-       for (struct trie_leaf *leaf = (trie)->head, *_next; \
-            leaf && (_next = leaf->next, true); \
-            leaf = _next)
+#define for_trie(leaf, trie) \
+       for_list(struct trie_leaf, leaf, trie)
 
 #endif // BFS_TRIE_H
index 2cabdcc90beba7e5d4424d0f394de8d01037cce3..80bafefe64e7ee96104f3fdc273b549273719c6e 100644 (file)
@@ -49,8 +49,8 @@ int bfs_spawn_init(struct bfs_spawn *ctx) {
 }
 
 int bfs_spawn_destroy(struct bfs_spawn *ctx) {
-       while (ctx->head) {
-               free(SLIST_POP(ctx));
+       for_slist (struct bfs_spawn_action, action, ctx) {
+               free(action);
        }
 
        return 0;
@@ -68,7 +68,7 @@ static struct bfs_spawn_action *bfs_spawn_add(struct bfs_spawn *ctx, enum bfs_sp
                return NULL;
        }
 
-       action->next = NULL;
+       SLIST_ITEM_INIT(action);
        action->op = op;
        action->in_fd = -1;
        action->out_fd = -1;
@@ -138,7 +138,7 @@ int bfs_spawn_addsetrlimit(struct bfs_spawn *ctx, int resource, const struct rli
 static void bfs_spawn_exec(const char *exe, const struct bfs_spawn *ctx, char **argv, char **envp, int pipefd[2]) {
        xclose(pipefd[0]);
 
-       for (const struct bfs_spawn_action *action = ctx ? ctx->head : NULL; action; action = action->next) {
+       for_slist (const struct bfs_spawn_action, action, ctx) {
                // Move the error-reporting pipe out of the way if necessary...
                if (action->out_fd == pipefd[1]) {
                        int fd = dup_cloexec(pipefd[1]);
@@ -199,9 +199,8 @@ pid_t bfs_spawn(const char *exe, const struct bfs_spawn *ctx, char **argv, char
                envp = environ;
        }
 
-       enum bfs_spawn_flags flags = ctx ? ctx->flags : 0;
        char *resolved = NULL;
-       if (flags & BFS_SPAWN_USEPATH) {
+       if (ctx->flags & BFS_SPAWN_USEPATH) {
                exe = resolved = bfs_spawn_resolve(exe);
                if (!resolved) {
                        return -1;
index e687f96e3aceb86f0b2a5e3c2a38ace6425ed846..6ea94a288fd382ebb3ca019c48aac1f7561a8845 100644 (file)
@@ -70,7 +70,7 @@ int main(void) {
 
        {
                size_t i = 0;
-               TRIE_FOR_EACH(&trie, leaf) {
+               for_trie (leaf, &trie) {
                        bfs_verify(leaf == trie_find_str(&trie, keys[i]));
                        bfs_verify(!leaf->prev || leaf->prev->next == leaf);
                        bfs_verify(!leaf->next || leaf->next->prev == leaf);
@@ -107,7 +107,7 @@ int main(void) {
                }
        }
 
-       TRIE_FOR_EACH(&trie, leaf) {
+       for_trie (leaf, &trie) {
                bfs_verify(false);
        }