Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/byte_buffer.h> |
| 20 | #include <grpc/support/alloc.h> |
| 21 | #include <grpc/support/log.h> |
| 22 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 23 | #include "src/core/lib/slice/slice_internal.h" |
| 24 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 25 | grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices, |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 26 | size_t nslices) { |
| 27 | return grpc_raw_compressed_byte_buffer_create(slices, nslices, |
| 28 | GRPC_COMPRESS_NONE); |
David Garcia Quintas | 25d02d5 | 2015-06-04 17:40:54 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 31 | grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create( |
| 32 | grpc_slice* slices, size_t nslices, |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 33 | grpc_compression_algorithm compression) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 34 | size_t i; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 35 | grpc_byte_buffer* bb = |
| 36 | (grpc_byte_buffer*)gpr_malloc(sizeof(grpc_byte_buffer)); |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 37 | bb->type = GRPC_BB_RAW; |
| 38 | bb->data.raw.compression = compression; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 39 | grpc_slice_buffer_init(&bb->data.raw.slice_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | for (i = 0; i < nslices; i++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 41 | grpc_slice_ref_internal(slices[i]); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 42 | grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | return bb; |
| 45 | } |
| 46 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 47 | grpc_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 Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 51 | grpc_slice slice; |
David Garcia Quintas | 3df5c44 | 2015-06-17 18:31:58 -0700 | [diff] [blame] | 52 | bb->type = GRPC_BB_RAW; |
| 53 | bb->data.raw.compression = GRPC_COMPRESS_NONE; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 54 | grpc_slice_buffer_init(&bb->data.raw.slice_buffer); |
David Garcia Quintas | 3df5c44 | 2015-06-17 18:31:58 -0700 | [diff] [blame] | 55 | |
| 56 | while (grpc_byte_buffer_reader_next(reader, &slice)) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 57 | grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice); |
David Garcia Quintas | 3df5c44 | 2015-06-17 18:31:58 -0700 | [diff] [blame] | 58 | } |
| 59 | return bb; |
| 60 | } |
| 61 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 62 | grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) { |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 63 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 64 | case GRPC_BB_RAW: |
Lizan Zhou | a3570f2 | 2016-09-05 13:29:17 +0900 | [diff] [blame] | 65 | 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 Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 68 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 69 | GPR_UNREACHABLE_CODE(return nullptr); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 72 | void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 73 | if (!bb) return; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 74 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 76 | case GRPC_BB_RAW: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 77 | grpc_slice_buffer_destroy_internal(&exec_ctx, &bb->data.raw.slice_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | break; |
| 79 | } |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 80 | gpr_free(bb); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 81 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 84 | size_t grpc_byte_buffer_length(grpc_byte_buffer* bb) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 86 | case GRPC_BB_RAW: |
| 87 | return bb->data.raw.slice_buffer.length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 89 | GPR_UNREACHABLE_CODE(return 0); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 90 | } |