blob: 9e0636b4ceae42762366896771fbee6e2b24593b [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 Tillerbaa14a92017-11-03 09:09:36 -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
Craig Tillerbaa14a92017-11-03 09:09:36 -070031grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
32 grpc_slice* slices, size_t nslices,
Craig Tiller28b72422016-10-26 21:15:29 -070033 grpc_compression_algorithm compression) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034 size_t i;
Craig Tillerbaa14a92017-11-03 09:09:36 -070035 grpc_byte_buffer* bb =
36 (grpc_byte_buffer*)gpr_malloc(sizeof(grpc_byte_buffer));
David Garcia Quintas59f905d2015-06-08 16:31:19 -070037 bb->type = GRPC_BB_RAW;
38 bb->data.raw.compression = compression;
Craig Tillerd41a4a72016-10-26 16:16:06 -070039 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040 for (i = 0; i < nslices; i++) {
Craig Tillera59c16c2016-10-31 07:25:01 -070041 grpc_slice_ref_internal(slices[i]);
Craig Tillerd41a4a72016-10-26 16:16:06 -070042 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044 return bb;
45}
46
Craig Tillerbaa14a92017-11-03 09:09:36 -070047grpc_byte_buffer* grpc_raw_byte_buffer_from_reader(
48 grpc_byte_buffer_reader* reader) {
49 grpc_byte_buffer* bb =
50 (grpc_byte_buffer*)gpr_malloc(sizeof(grpc_byte_buffer));
Craig Tillerd41a4a72016-10-26 16:16:06 -070051 grpc_slice slice;
David Garcia Quintas3df5c442015-06-17 18:31:58 -070052 bb->type = GRPC_BB_RAW;
53 bb->data.raw.compression = GRPC_COMPRESS_NONE;
Craig Tillerd41a4a72016-10-26 16:16:06 -070054 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
David Garcia Quintas3df5c442015-06-17 18:31:58 -070055
56 while (grpc_byte_buffer_reader_next(reader, &slice)) {
Craig Tillerd41a4a72016-10-26 16:16:06 -070057 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
David Garcia Quintas3df5c442015-06-17 18:31:58 -070058 }
59 return bb;
60}
61
Craig Tillerbaa14a92017-11-03 09:09:36 -070062grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) {
Craig Tiller80fa15c2015-01-13 16:10:49 -080063 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070064 case GRPC_BB_RAW:
Lizan Zhoua3570f22016-09-05 13:29:17 +090065 return grpc_raw_compressed_byte_buffer_create(
66 bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
67 bb->data.raw.compression);
Craig Tiller80fa15c2015-01-13 16:10:49 -080068 }
Craig Tiller4782d922017-11-10 09:53:21 -080069 GPR_UNREACHABLE_CODE(return nullptr);
Craig Tiller80fa15c2015-01-13 16:10:49 -080070}
71
Craig Tillerbaa14a92017-11-03 09:09:36 -070072void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) {
Craig Tillerfee91b42015-02-05 16:03:13 -080073 if (!bb) return;
Craig Tillera59c16c2016-10-31 07:25:01 -070074 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070076 case GRPC_BB_RAW:
Craig Tillera59c16c2016-10-31 07:25:01 -070077 grpc_slice_buffer_destroy_internal(&exec_ctx, &bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 break;
79 }
Nicolas "Pixel" Noble7c9a1542016-03-26 01:33:34 +010080 gpr_free(bb);
Craig Tillera59c16c2016-10-31 07:25:01 -070081 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082}
83
Craig Tillerbaa14a92017-11-03 09:09:36 -070084size_t grpc_byte_buffer_length(grpc_byte_buffer* bb) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070086 case GRPC_BB_RAW:
87 return bb->data.raw.slice_buffer.length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088 }
yang-gb063c872015-10-07 11:40:13 -070089 GPR_UNREACHABLE_CODE(return 0);
Craig Tiller190d3602015-02-18 09:23:38 -080090}