Introduce btr_node_description.
authorAndre Noll <maan@systemlinux.org>
Thu, 14 Jan 2010 03:19:59 +0000 (04:19 +0100)
committerAndre Noll <maan@systemlinux.org>
Thu, 14 Jan 2010 03:19:59 +0000 (04:19 +0100)
As the number of arguments to btr_new_node() grows, the code becomes
hard to read, especially since some callers must set many of the
arguments to NULL.

Using a pointer to a struct has the additional advantage that adding
new parameters does no longer require to touch all callers.

audiod.c
buffer_tree.c
buffer_tree.h
filter.c
grab_client.c
para.h
recv.c
write.c
write_common.c

index 2819091..2bda0f6 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -479,7 +479,11 @@ static void open_filters(struct slot_info *s)
                fn->conf = a->filter_conf[i];
                fn->task.pre_select = f->pre_select;
                fn->task.post_select = f->post_select;
-               fn->btrn = btr_new_node(f->name, parent, f->execute, fn);
+
+               fn->btrn = btr_new_node(&(struct btr_node_description)
+                       EMBRACE(.name = f->name, .parent = parent,
+                               .handler = f->execute, .context = fn));
+
                f->open(fn);
                register_task(&fn->task);
                parent = fn->btrn;
@@ -532,7 +536,8 @@ static int open_receiver(int format)
        rn = s->receiver_node;
        rn->receiver = r;
        rn->conf = a->receiver_conf;
-       rn->btrn = btr_new_node(r->name, NULL, NULL, rn);
+       rn->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = r->name, .context = rn));
        ret = r->open(rn);
        if (ret < 0) {
                btr_free_node(rn->btrn);
index 40079c2..e023bb1 100644 (file)
@@ -168,25 +168,32 @@ static void btr_pool_deallocate(struct btr_pool *btrp, size_t size)
 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
        list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
 
-struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
-               btr_command_handler handler, void *context)
+/*
+       (parent, child):
+       (NULL, NULL): new, isolated node.
+       (NULL, c): new node becomes root, c must be old root
+       (p, NULL): new leaf node
+       (p, c): new internal node, ch must be child of p
+
+*/
+struct btr_node *btr_new_node(struct btr_node_description *bnd)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
 
-       btrn->name = para_strdup(name);
-       btrn->parent = parent;
-       btrn->execute = handler;
-       btrn->context = context;
+       btrn->name = para_strdup(bnd->name);
+       btrn->parent = bnd->parent;
+       btrn->execute = bnd->handler;
+       btrn->context = bnd->context;
        btrn->start.tv_sec = 0;
        btrn->start.tv_usec = 0;
-       if (parent)
-               list_add_tail(&btrn->node, &parent->children);
+       if (bnd->parent)
+               list_add_tail(&btrn->node, &bnd->parent->children);
        INIT_LIST_HEAD(&btrn->children);
        INIT_LIST_HEAD(&btrn->input_queue);
-       if (parent)
-               PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
+       if (bnd->parent)
+               PARA_INFO_LOG("added %s as child of %s\n", bnd->name, bnd->parent->name);
        else
-               PARA_INFO_LOG("added %s as btr root\n", name);
+               PARA_INFO_LOG("added %s as btr root\n", bnd->name);
        return btrn;
 }
 
index 2c102ee..cd8c28d 100644 (file)
@@ -11,6 +11,13 @@ enum btr_node_type {
        BTR_NT_LEAF,
 };
 
+struct btr_node_description {
+       const char *name;
+       struct btr_node *parent;
+       btr_command_handler handler;
+       void *context;
+};
+
 size_t btr_pool_size(struct btr_pool *btrp);
 struct btr_pool *btr_pool_new(const char *name, size_t area_size);
 void btr_pool_free(struct btr_pool *btrp);
@@ -22,8 +29,7 @@ size_t btr_pool_unused(struct btr_pool *btrp);
 void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
        struct btr_node *btrn);
 
-struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
-               btr_command_handler handler, void *context);
+struct btr_node *btr_new_node(struct btr_node_description *bnd);
 void btr_remove_node(struct btr_node *btrn);
 void btr_free_node(struct btr_node *btrn);
 void btr_add_output(char *buf, size_t size, struct btr_node *btrn);
index 08398d5..93e4161 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -116,7 +116,8 @@ int main(int argc, char *argv[])
        ret = parse_config(argc, argv);
        if (ret < 0)
                goto out;
-       sit->btrn = btr_new_node("stdin", NULL, NULL, NULL);
+       sit->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = "stdin"));
        stdin_set_defaults(sit);
        register_task(&sit->task);
 
@@ -135,14 +136,17 @@ int main(int argc, char *argv[])
                f = filters + fn->filter_num;
                sprintf(fn->task.status, "%s", f->name);
                PARA_DEBUG_LOG("filter #%d: %s\n", i, f->name);
-               fn->btrn = btr_new_node(f->name, parent, f->execute, fn);
+               fn->btrn = btr_new_node(&(struct btr_node_description)
+                       EMBRACE(.name = f->name, .parent = parent,
+                       .handler = f->execute, .context = fn));
                fn->task.pre_select = f->pre_select;
                fn->task.post_select = f->post_select;
                f->open(fn);
                register_task(&fn->task);
                parent = fn->btrn;
        }
-       sot->btrn = btr_new_node("stdout", parent, NULL, NULL);
+       sot->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = "stdout", .parent = parent));
        stdout_set_defaults(sot);
        register_task(&sot->task);
 
index 2b8235b..466380f 100644 (file)
@@ -132,7 +132,8 @@ static void gc_activate(struct grab_client *gc)
                return;
        PARA_INFO_LOG("activating fd %d\n", gc->fd);
        list_move(&gc->node, &active_grab_client_list);
-       gc->btrn = btr_new_node(name, parent, NULL, NULL);
+       gc->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = name, .parent = parent));
        if (!gc->task.pre_select) {
                gc->task.pre_select = gc_pre_select;
                gc->task.post_select = gc_post_select;
diff --git a/para.h b/para.h
index 454d736..687ad30 100644 (file)
--- a/para.h
+++ b/para.h
@@ -255,3 +255,7 @@ _static_inline_ long int para_random(unsigned max)
 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
        "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
 #define FEC_EOF_PACKET_LEN 32
+
+/** Used to avoid a shortcoming in vim's syntax highlighting. */
+#define EMBRACE(...) { __VA_ARGS__}
+
diff --git a/recv.c b/recv.c
index 05f616c..5cd64cc 100644 (file)
--- a/recv.c
+++ b/recv.c
@@ -60,7 +60,7 @@ static void *parse_config(int argc, char *argv[], int *receiver_num)
 }
 
 /**
- * the main function of para_recv
+ * The main function of para_recv.
  *
  * \param argc number of arguments
  * \param argv vector of arguments
@@ -92,19 +92,21 @@ int main(int argc, char *argv[])
        }
        r = &receivers[receiver_num];
        rn.receiver = r;
-       rn.btrn = btr_new_node("receiver", NULL, NULL, NULL);
+       rn.btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = r->name));
        ret = r->open(&rn);
        if (ret < 0)
                goto out;
        r_opened = 1;
 
-       sot.btrn = btr_new_node("stdout", rn.btrn, NULL, NULL);
+       sot.btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.parent = rn.btrn, .name = "stdout"));
        stdout_set_defaults(&sot);
        register_task(&sot.task);
 
        rn.task.pre_select = r->pre_select;
        rn.task.post_select = r->post_select;
-       sprintf(rn.task.status, "receiver node");
+       sprintf(rn.task.status, "%s", r->name);
        register_task(&rn.task);
 
        ret = schedule(&s);
diff --git a/write.c b/write.c
index 103f2ac..d886335 100644 (file)
--- a/write.c
+++ b/write.c
@@ -154,14 +154,17 @@ static int main_btr(struct sched *s)
        struct writer_node *wns;
 
        loglevel = get_loglevel_by_name(conf.loglevel_arg);
-       sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL, NULL);
+       sit.btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = "stdin"));
        stdin_set_defaults(&sit);
        register_task(&sit.task);
 
        cwt->state = CWS_NEED_HEADER;
        cwt->min_iqs = WAV_HEADER_LEN;
-       cwt->btrn = btr_new_node("check wav", sit.btrn, check_wav_exec, cwt);
-       sprintf(cwt->task.status, "check wav");
+       cwt->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = "check_wav", .parent = sit.btrn,
+               .handler = check_wav_exec, .context = cwt));
+       sprintf(cwt->task.status, "check_wav");
        cwt->task.pre_select = check_wav_pre_select;
        cwt->task.post_select = check_wav_post_select;
        cwt->task.error = 0;
index 66f097a..b92c5f1 100644 (file)
@@ -81,7 +81,9 @@ void register_writer_node(struct writer_node *wn, struct btr_node *parent)
        char *name = make_message("%s writer", writer_names[wn->writer_num]);
        int ret;
 
-       wn->btrn = btr_new_node(name, parent, w->execute, wn);
+       wn->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = name, .parent = parent,
+               .handler = w->execute, .context = wn));
        strcpy(wn->task.status, name);
        free(name);
        ret = w->open(wn);