3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/byte_buffer.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
25 #include "src/core/lib/iomgr/exec_ctx.h"
26 #include "src/core/lib/slice/slice_internal.h"
28 grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices,
30 return grpc_raw_compressed_byte_buffer_create(slices, nslices,
34 grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
35 grpc_slice* slices, size_t nslices,
36 grpc_compression_algorithm compression) {
38 grpc_byte_buffer* bb =
39 static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer)));
40 bb->type = GRPC_BB_RAW;
41 bb->data.raw.compression = compression;
42 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
43 for (i = 0; i < nslices; i++) {
44 grpc_slice_ref_internal(slices[i]);
45 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
50 grpc_byte_buffer* grpc_raw_byte_buffer_from_reader(
51 grpc_byte_buffer_reader* reader) {
52 grpc_byte_buffer* bb =
53 static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer)));
55 bb->type = GRPC_BB_RAW;
56 bb->data.raw.compression = GRPC_COMPRESS_NONE;
57 grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
59 while (grpc_byte_buffer_reader_next(reader, &slice)) {
60 grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
65 grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) {
68 return grpc_raw_compressed_byte_buffer_create(
69 bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
70 bb->data.raw.compression);
72 GPR_UNREACHABLE_CODE(return nullptr);
75 void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) {
77 grpc_core::ExecCtx exec_ctx;
80 grpc_slice_buffer_destroy_internal(&bb->data.raw.slice_buffer);
86 size_t grpc_byte_buffer_length(grpc_byte_buffer* bb) {
89 return bb->data.raw.slice_buffer.length;
91 GPR_UNREACHABLE_CODE(return 0);