X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=buffer_tree.c;h=925667456a89f34921aca4a6cf01681c3e99824c;hb=5587494468627e20fe622b6055689717262d09ab;hp=1046460d1d1c56dd424b0cc9a7a3e33c76857188;hpb=0b101563e67c91bc660f6a16b5889901d9e8eeff;p=paraslash.git diff --git a/buffer_tree.c b/buffer_tree.c index 1046460d..92566745 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -5,6 +5,7 @@ #include "list.h" #include "string.h" #include "buffer_tree.h" +#include "error.h" struct btr_buffer { @@ -34,6 +35,7 @@ struct btr_node { * used by this btr node. */ struct list_head input_queue; + btr_command_handler execute; }; #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \ @@ -44,12 +46,14 @@ struct btr_node { #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(char *name, struct btr_node *parent) +struct btr_node *btr_new_node(char *name, struct btr_node *parent, + btr_command_handler handler) { struct btr_node *btrn = para_malloc(sizeof(*btrn)); btrn->name = para_strdup(name); btrn->parent = parent; + btrn->execute = handler; if (parent) list_add_tail(&btrn->node, &parent->children); INIT_LIST_HEAD(&btrn->children); @@ -79,6 +83,7 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br) { struct btr_buffer *btrb = br->btrb; + //PARA_CRIT_LOG("dropping buffer reference %p\n", br); list_del(&br->node); free(br); btrb->refcount--; @@ -124,7 +129,7 @@ void btr_pushdown(struct btr_node *btrn) } /* Return true if this node has no children. */ -bool btr_is_leaf_node(struct btr_node *btrn) +bool btr_no_children(struct btr_node *btrn) { return list_empty(&btrn->children); } @@ -204,11 +209,31 @@ size_t btr_get_input_queue_size(struct btr_node *btrn) struct btr_buffer_reference *br; size_t size = 0; - FOR_EACH_BUFFER_REF(br, btrn) + FOR_EACH_BUFFER_REF(br, btrn) { + //PARA_CRIT_LOG("size: %zu\n", size); size += br_available_bytes(br); + } return size; } +int btr_splice_out_node(struct btr_node *btrn) +{ + struct btr_node *ch; + + if (!btrn) + return -ERRNO_TO_PARA_ERROR(EINVAL); + if (btr_get_input_queue_size(btrn) != 0) + return -ERRNO_TO_PARA_ERROR(EINVAL); + PARA_NOTICE_LOG("splicing out %s\n", btrn->name); + if (btrn->parent) + list_del(&btrn->node); + FOR_EACH_CHILD(ch, btrn) + ch->parent = btrn->parent; + free(btrn->name); + free(btrn); + return 1; +} + /** * Return the size of the largest input queue. * @@ -225,3 +250,32 @@ size_t btr_bytes_pending(struct btr_node *btrn) } return max_size; } + +int btr_exec(struct btr_node *btrn, const char *command, char **value_result) +{ + if (!btrn) + return -ERRNO_TO_PARA_ERROR(EINVAL); + if (!btrn->execute) + return -ERRNO_TO_PARA_ERROR(ENOTSUP); + return btrn->execute(command, value_result); +} + +int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result) +{ + int ret; + + for (; btrn; btrn = btrn->parent) { + struct btr_node *parent = btrn->parent; + PARA_CRIT_LOG("parent: %p\n", parent); + if (!parent) + return -ERRNO_TO_PARA_ERROR(ENOTSUP); + if (!parent->execute) + continue; + ret = parent->execute(command, value_result); + if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP)) + continue; + if (ret < 0) + return ret; + } + return -ERRNO_TO_PARA_ERROR(ENOTSUP); +}