void grpc_sopb_move_to(grpc_stream_op_buffer *src, grpc_stream_op_buffer *dst) { if (src->nops == 0) { return; } if (dst->nops == 0) { grpc_sopb_swap(src, dst); return; } grpc_sopb_append(dst, src->ops, src->nops); src->nops = 0; }
int main(int argc, char **argv) { /* some basic test data */ gpr_slice test_slice_1 = gpr_slice_malloc(1); gpr_slice test_slice_2 = gpr_slice_malloc(2); gpr_slice test_slice_3 = gpr_slice_malloc(3); gpr_slice test_slice_4 = gpr_slice_malloc(4); unsigned i; grpc_stream_op_buffer buf; grpc_stream_op_buffer buf2; grpc_test_init(argc, argv); /* initialize one of our buffers */ grpc_sopb_init(&buf); /* it should start out empty */ GPR_ASSERT(buf.nops == 0); /* add some data to the buffer */ grpc_sopb_add_begin_message(&buf, 1, 2); grpc_sopb_add_slice(&buf, test_slice_1); grpc_sopb_add_slice(&buf, test_slice_2); grpc_sopb_add_slice(&buf, test_slice_3); grpc_sopb_add_slice(&buf, test_slice_4); grpc_sopb_add_no_op(&buf); /* verify that the data went in ok */ GPR_ASSERT(buf.nops == 6); GPR_ASSERT(buf.ops[0].type == GRPC_OP_BEGIN_MESSAGE); GPR_ASSERT(buf.ops[0].data.begin_message.length == 1); GPR_ASSERT(buf.ops[0].data.begin_message.flags == 2); GPR_ASSERT(buf.ops[1].type == GRPC_OP_SLICE); assert_slices_equal(buf.ops[1].data.slice, test_slice_1); GPR_ASSERT(buf.ops[2].type == GRPC_OP_SLICE); assert_slices_equal(buf.ops[2].data.slice, test_slice_2); GPR_ASSERT(buf.ops[3].type == GRPC_OP_SLICE); assert_slices_equal(buf.ops[3].data.slice, test_slice_3); GPR_ASSERT(buf.ops[4].type == GRPC_OP_SLICE); assert_slices_equal(buf.ops[4].data.slice, test_slice_4); GPR_ASSERT(buf.ops[5].type == GRPC_NO_OP); /* initialize the second buffer */ grpc_sopb_init(&buf2); /* add a no-op, and then the original buffer */ grpc_sopb_add_no_op(&buf2); grpc_sopb_append(&buf2, buf.ops, buf.nops); /* should be one element bigger than the original */ GPR_ASSERT(buf2.nops == buf.nops + 1); GPR_ASSERT(buf2.ops[0].type == GRPC_NO_OP); /* and the tail should be the same */ for (i = 0; i < buf.nops; i++) { GPR_ASSERT(buf2.ops[i + 1].type == buf.ops[i].type); } /* destroy the buffers */ grpc_sopb_destroy(&buf); grpc_sopb_destroy(&buf2); gpr_slice_unref(test_slice_1); gpr_slice_unref(test_slice_2); gpr_slice_unref(test_slice_3); gpr_slice_unref(test_slice_4); return 0; }