• 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/byte_buffer.h>
20 #include <grpc/grpc.h>
21 #include <grpc/impl/compression_types.h>
22 #include <grpc/slice.h>
23 #include <grpc/slice_buffer.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/port_platform.h>
26 #include <stddef.h>
27 
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/slice/slice.h"
30 
grpc_raw_byte_buffer_create(grpc_slice * slices,size_t nslices)31 grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices,
32                                               size_t nslices) {
33   return grpc_raw_compressed_byte_buffer_create(slices, nslices,
34                                                 GRPC_COMPRESS_NONE);
35 }
36 
grpc_raw_compressed_byte_buffer_create(grpc_slice * slices,size_t nslices,grpc_compression_algorithm compression)37 grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
38     grpc_slice* slices, size_t nslices,
39     grpc_compression_algorithm compression) {
40   size_t i;
41   grpc_byte_buffer* bb =
42       static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer)));
43   bb->type = GRPC_BB_RAW;
44   bb->data.raw.compression = compression;
45   grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
46   for (i = 0; i < nslices; i++) {
47     grpc_core::CSliceRef(slices[i]);
48     grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
49   }
50   return bb;
51 }
52 
grpc_raw_byte_buffer_from_reader(grpc_byte_buffer_reader * reader)53 grpc_byte_buffer* grpc_raw_byte_buffer_from_reader(
54     grpc_byte_buffer_reader* reader) {
55   grpc_byte_buffer* bb =
56       static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer)));
57   grpc_slice slice;
58   bb->type = GRPC_BB_RAW;
59   bb->data.raw.compression = GRPC_COMPRESS_NONE;
60   grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
61 
62   while (grpc_byte_buffer_reader_next(reader, &slice)) {
63     grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
64   }
65   return bb;
66 }
67 
grpc_byte_buffer_copy(grpc_byte_buffer * bb)68 grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) {
69   switch (bb->type) {
70     case GRPC_BB_RAW:
71       return grpc_raw_compressed_byte_buffer_create(
72           bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
73           bb->data.raw.compression);
74   }
75   GPR_UNREACHABLE_CODE(return nullptr);
76 }
77 
grpc_byte_buffer_destroy(grpc_byte_buffer * bb)78 void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) {
79   if (!bb) return;
80   grpc_core::ExecCtx exec_ctx;
81   switch (bb->type) {
82     case GRPC_BB_RAW:
83       grpc_slice_buffer_destroy(&bb->data.raw.slice_buffer);
84       break;
85   }
86   gpr_free(bb);
87 }
88 
grpc_byte_buffer_length(grpc_byte_buffer * bb)89 size_t grpc_byte_buffer_length(grpc_byte_buffer* bb) {
90   switch (bb->type) {
91     case GRPC_BB_RAW:
92       return bb->data.raw.slice_buffer.length;
93   }
94   GPR_UNREACHABLE_CODE(return 0);
95 }
96