From: Andre Noll Date: Thu, 14 Jan 2010 03:19:59 +0000 (+0100) Subject: Introduce btr_node_description. X-Git-Tag: v0.4.2~101 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=5ec0d9ef2189e22b3ee80f661196b6ae0593a525 Introduce btr_node_description. 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. --- diff --git a/audiod.c b/audiod.c index 2819091b..2bda0f69 100644 --- 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); diff --git a/buffer_tree.c b/buffer_tree.c index 40079c2f..e023bb14 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -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; } diff --git a/buffer_tree.h b/buffer_tree.h index 2c102eec..cd8c28d3 100644 --- a/buffer_tree.h +++ b/buffer_tree.h @@ -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); diff --git a/filter.c b/filter.c index 08398d59..93e41618 100644 --- 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); diff --git a/grab_client.c b/grab_client.c index 2b8235b4..466380fd 100644 --- a/grab_client.c +++ b/grab_client.c @@ -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 454d736a..687ad30e 100644 --- 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 05f616cd..5cd64cc7 100644 --- 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 103f2acc..d8863359 100644 --- 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; diff --git a/write_common.c b/write_common.c index 66f097af..b92c5f1c 100644 --- a/write_common.c +++ b/write_common.c @@ -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);