blob: 0bc990d487deeece386235ea9841cda14d9c930d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include <grpc/byte_buffer.h>
20#include <grpc/support/alloc.h>
21#include <grpc/support/log.h>
22
Craig Tillera59c16c2016-10-31 07:25:01 -070023#include "src/core/lib/slice/slice_internal.h"
24
Craig Tillerd41a4a72016-10-26 16:16:06 -070025grpc_byte_buffer *grpc_raw_byte_buffer_create(grpc_slice *slices,
David Garcia Quintas59f905d2015-06-08 16:31:19 -070026 size_t nslices) {
27 return grpc_raw_compressed_byte_buffer_create(slices, nslices,
28 GRPC_COMPRESS_NONE);
David Garcia Quintas25d02d52015-06-04 17:40:54 -070029}
30
David Garcia Quintas59f905d2015-06-08 16:31:19 -070031grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
Craig Tiller28b72422016-10-26 21:15:29 -070032 grpc_slice *slices, size_t nslices,
33 grpc_compression_algorithm compression) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034 size_t i;
Nicolas "Pixel" Noble7c9a1542016-03-26 01:33:34 +010035 grpc_byte_buffer *bb = gpr_malloc(sizeof(grpc_byte_buffer));
David Garcia Quintas59f905d2015-06-08 16:31:19 -070036 bb->type = GRPC_BB_RAW;
37 bb->data.raw.compression = compression;
Craig Tillerd41a4a72016-10-26 16:16:06 -070038 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039 for (i = 0; i < nslices; i++) {
Craig Tillera59c16c2016-10-31 07:25:01 -070040 grpc_slice_ref_internal(slices[i]);
Craig Tillerd41a4a72016-10-26 16:16:06 -070041 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 return bb;
44}
45
David Garcia Quintas3df5c442015-06-17 18:31:58 -070046grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
47 grpc_byte_buffer_reader *reader) {
Nicolas "Pixel" Noble7c9a1542016-03-26 01:33:34 +010048 grpc_byte_buffer *bb = gpr_malloc(sizeof(grpc_byte_buffer));
Craig Tillerd41a4a72016-10-26 16:16:06 -070049 grpc_slice slice;
David Garcia Quintas3df5c442015-06-17 18:31:58 -070050 bb->type = GRPC_BB_RAW;
51 bb->data.raw.compression = GRPC_COMPRESS_NONE;
Craig Tillerd41a4a72016-10-26 16:16:06 -070052 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
David Garcia Quintas3df5c442015-06-17 18:31:58 -070053
54 while (grpc_byte_buffer_reader_next(reader, &slice)) {
Craig Tillerd41a4a72016-10-26 16:16:06 -070055 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
David Garcia Quintas3df5c442015-06-17 18:31:58 -070056 }
57 return bb;
58}
59
Craig Tiller80fa15c2015-01-13 16:10:49 -080060grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
61 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070062 case GRPC_BB_RAW:
Lizan Zhoua3570f22016-09-05 13:29:17 +090063 return grpc_raw_compressed_byte_buffer_create(
64 bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
65 bb->data.raw.compression);
Craig Tiller80fa15c2015-01-13 16:10:49 -080066 }
yang-gb063c872015-10-07 11:40:13 -070067 GPR_UNREACHABLE_CODE(return NULL);
Craig Tiller80fa15c2015-01-13 16:10:49 -080068}
69
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) {
Craig Tillerfee91b42015-02-05 16:03:13 -080071 if (!bb) return;
Craig Tillera59c16c2016-10-31 07:25:01 -070072 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070074 case GRPC_BB_RAW:
Craig Tillera59c16c2016-10-31 07:25:01 -070075 grpc_slice_buffer_destroy_internal(&exec_ctx, &bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076 break;
77 }
Nicolas "Pixel" Noble7c9a1542016-03-26 01:33:34 +010078 gpr_free(bb);
Craig Tillera59c16c2016-10-31 07:25:01 -070079 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080}
81
82size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) {
83 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070084 case GRPC_BB_RAW:
85 return bb->data.raw.slice_buffer.length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 }
yang-gb063c872015-10-07 11:40:13 -070087 GPR_UNREACHABLE_CODE(return 0);
Craig Tiller190d3602015-02-18 09:23:38 -080088}