]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - buffer_tree.c
Add btr support to filter code.
[paraslash.git] / buffer_tree.c
index 2a52f097a325641c5695ae841eaa4608263d3d99..28999df50a38b31277a156acfd012e2e54806535 100644 (file)
@@ -36,6 +36,7 @@ struct btr_node {
         */
        struct list_head input_queue;
        btr_command_handler execute;
+       void *context;
 };
 
 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
@@ -46,14 +47,15 @@ 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,
-               btr_command_handler handler)
+struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
+               btr_command_handler handler, void *context)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
 
        btrn->name = para_strdup(name);
        btrn->parent = parent;
        btrn->execute = handler;
+       btrn->context = context;
        if (parent)
                list_add_tail(&btrn->node, &parent->children);
        INIT_LIST_HEAD(&btrn->children);
@@ -93,14 +95,15 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
        }
 }
 
-static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
+static void add_btrb_to_children(struct btr_buffer *btrb,
+               struct btr_node *btrn, size_t consumed)
 {
        struct btr_node *ch;
 
        FOR_EACH_CHILD(ch, btrn) {
                struct btr_buffer_reference *br = para_malloc(sizeof(*br));
                br->btrb = btrb;
-               br->consumed = 0;
+               br->consumed = consumed;
                list_add_tail(&br->node, &ch->input_queue);
                btrb->refcount++;
        }
@@ -111,12 +114,12 @@ void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
        struct btr_buffer *btrb;
 
        btrb = new_btrb(buf, size);
-       add_btrb_to_children(btrb, btrn);
+       add_btrb_to_children(btrb, btrn, 0);
 }
 
 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
 {
-       add_btrb_to_children(br->btrb, btrn);
+       add_btrb_to_children(br->btrb, btrn, br->consumed);
        btr_drop_buffer_reference(br);
 }
 
@@ -216,6 +219,24 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        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.
  *
@@ -239,7 +260,7 @@ int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
                return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (!btrn->execute)
                return -ERRNO_TO_PARA_ERROR(ENOTSUP);
-       return btrn->execute(command, value_result);
+       return btrn->execute(btrn, command, value_result);
 }
 
 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
@@ -253,7 +274,7 @@ int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
                        return -ERRNO_TO_PARA_ERROR(ENOTSUP);
                if (!parent->execute)
                        continue;
-               ret = parent->execute(command, value_result);
+               ret = parent->execute(parent, command, value_result);
                if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
                        continue;
                if (ret < 0)
@@ -261,3 +282,56 @@ int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
        }
        return -ERRNO_TO_PARA_ERROR(ENOTSUP);
 }
+
+void *btr_context(struct btr_node *btrn)
+{
+       return btrn->context;
+}
+
+/**
+ * Merge the first two input buffers into one.
+ *
+ * This is a quite expensive operation.
+ *
+ * \return The number of buffers that have been merged (zero, one or two).
+ */
+int btr_merge(struct btr_node *btrn)
+{
+       struct btr_buffer_reference *brs[2], *br;
+       char *bufs[2], *buf;
+       size_t szs[2], sz;
+       int i;
+
+       if (list_empty(&btrn->input_queue))
+               return 0;
+       if (list_is_singular(&btrn->input_queue))
+               return 1;
+       i = 0;
+       /* get references to the first two buffers */
+       FOR_EACH_BUFFER_REF(br, btrn) {
+               brs[i] = br;
+               szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
+               i++;
+               if (i == 2)
+                       break;
+       }
+       /* make a new btrb that combines the two buffers and a br to it. */
+       sz = szs[0] + szs[1];
+       //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
+       //      szs[0], szs[1], sz);
+       buf = para_malloc(sz);
+       /* TODO: Avoid this memcopy by introducing btr buffer pool. */
+       memcpy(buf, bufs[0], szs[0]);
+       memcpy(buf + szs[0], bufs[1], szs[1]);
+
+       br = para_malloc(sizeof(*br));
+       br->btrb = new_btrb(buf, sz);
+       br->btrb->refcount = 1;
+       br->consumed = 0;
+
+       /* replace the first two refs by the new one */
+       btr_drop_buffer_reference(brs[0]);
+       btr_drop_buffer_reference(brs[1]);
+       para_list_add(&br->node, &btrn->input_queue);
+       return 2;
+}