• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include <grpc/byte_buffer.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
25 #include "src/core/lib/iomgr/exec_ctx.h"
26 #include "src/core/lib/slice/slice_internal.h"
27 
grpc_raw_byte_buffer_create(grpc_slice * slices,size_t nslices)28 grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices,
29                                               size_t nslices) {
30   return grpc_raw_compressed_byte_buffer_create(slices, nslices,
31                                                 GRPC_COMPRESS_NONE);
32 }
33 
grpc_raw_compressed_byte_buffer_create(grpc_slice * slices,size_t nslices,grpc_compression_algorithm compression)34 grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
35     grpc_slice* slices, size_t nslices,
36     grpc_compression_algorithm compression) {
37   size_t i;
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]);
46   }
47   return bb;
48 }
49 
grpc_raw_byte_buffer_from_reader(grpc_byte_buffer_reader * reader)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)));
54   grpc_slice slice;
55   bb->type = GRPC_BB_RAW;
56   bb->data.raw.compression = GRPC_COMPRESS_NONE;
57   grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
58 
59   while (grpc_byte_buffer_reader_next(reader, &slice)) {
60     grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
61   }
62   return bb;
63 }
64 
grpc_byte_buffer_copy(grpc_byte_buffer * bb)65 grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) {
66   switch (bb->type) {
67     case GRPC_BB_RAW:
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);
71   }
72   GPR_UNREACHABLE_CODE(return nullptr);
73 }
74 
grpc_byte_buffer_destroy(grpc_byte_buffer * bb)75 void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) {
76   if (!bb) return;
77   grpc_core::ExecCtx exec_ctx;
78   switch (bb->type) {
79     case GRPC_BB_RAW:
80       grpc_slice_buffer_destroy_internal(&bb->data.raw.slice_buffer);
81       break;
82   }
83   gpr_free(bb);
84 }
85 
grpc_byte_buffer_length(grpc_byte_buffer * bb)86 size_t grpc_byte_buffer_length(grpc_byte_buffer* bb) {
87   switch (bb->type) {
88     case GRPC_BB_RAW:
89       return bb->data.raw.slice_buffer.length;
90   }
91   GPR_UNREACHABLE_CODE(return 0);
92 }
93