]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - buffer_tree.c
btr_slice_out_node() cleanup.
[paraslash.git] / buffer_tree.c
index 7b3355a28b98aa601431029c83063aac016d2d9d..b7420ebeeb38e70f85ad4c9f34fa215b59504f61 100644 (file)
@@ -41,13 +41,15 @@ struct btr_node {
 
 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
        &((_btrn)->children), node)
+#define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
+       list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
 
 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
        list_for_each_entry((_br), &(_btrn)->input_queue, 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(const char *name, struct btr_node *parent,
                btr_command_handler handler, void *context)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
@@ -60,6 +62,10 @@ struct btr_node *btr_new_node(char *name, struct btr_node *parent,
                list_add_tail(&btrn->node, &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);
+       else
+               PARA_INFO_LOG("added %s as btr root\n", name);
        return btrn;
 }
 
@@ -219,19 +225,23 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        return size;
 }
 
-int btr_splice_out_node(struct btr_node *btrn)
+void btr_splice_out_node(struct btr_node *btrn)
 {
-       struct btr_node *ch;
+       struct btr_node *ch, *tmp;
 
-       if (!btrn)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
-       if (btr_get_input_queue_size(btrn) != 0)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       assert(btrn);
        PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
+       btr_pushdown(btrn);
        if (btrn->parent)
                list_del(&btrn->node);
-       FOR_EACH_CHILD(ch, btrn)
+       FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
+               PARA_INFO_LOG("parent(%s): %s\n", ch->name,
+                       btrn->parent? btrn->parent->name : "NULL");
                ch->parent = btrn->parent;
+               if (btrn->parent)
+                       list_move(&ch->node, &btrn->parent->children);
+       }
+       assert(list_empty(&btrn->children));
        free(btrn->name);
        free(btrn);
        return 1;
@@ -269,16 +279,20 @@ int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
 
        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;
+               PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
                ret = parent->execute(parent, command, value_result);
                if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
                        continue;
                if (ret < 0)
                        return ret;
+               if (value_result && *value_result)
+                       PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
+                               *value_result);
+               return 1;
        }
        return -ERRNO_TO_PARA_ERROR(ENOTSUP);
 }
@@ -287,3 +301,51 @@ 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;
+}