X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=buffer_tree.c;h=28999df50a38b31277a156acfd012e2e54806535;hp=7b3355a28b98aa601431029c83063aac016d2d9d;hb=c87045a40b9462ebeb28cc169d7bb3721c9a81d3;hpb=d1e55304068a6a82553f40ae7bee746f5ca2233d diff --git a/buffer_tree.c b/buffer_tree.c index 7b3355a2..28999df5 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -47,7 +47,7 @@ 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(const char *name, struct btr_node *parent, btr_command_handler handler, void *context) { struct btr_node *btrn = para_malloc(sizeof(*btrn)); @@ -287,3 +287,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; +}