impl/codegen/proto_buffer_writer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 #ifndef GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_WRITER_H
20 #define GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_WRITER_H
21 
22 // IWYU pragma: private, include <grpcpp/support/proto_buffer_writer.h>
23 
24 #include <type_traits>
25 
33 
36 
37 namespace grpc {
38 
39 extern CoreCodegenInterface* g_core_codegen_interface;
40 
41 // Forward declaration for testing use only
42 namespace internal {
43 class ProtoBufferWriterPeer;
44 } // namespace internal
45 
46 const int kProtoBufferWriterMaxBufferLength = 1024 * 1024;
47 
56  public:
62  ProtoBufferWriter(ByteBuffer* byte_buffer, int block_size, int total_size)
63  : block_size_(block_size),
64  total_size_(total_size),
65  byte_count_(0),
67  GPR_CODEGEN_ASSERT(!byte_buffer->Valid());
69  grpc_byte_buffer* bp =
71  byte_buffer->set_buffer(bp);
73  }
74 
75  ~ProtoBufferWriter() override {
76  if (have_backup_) {
78  }
79  }
80 
83  bool Next(void** data, int* size) override {
84  // Protobuf should not ask for more memory than total_size_.
86  // 1. Use the remaining backup slice if we have one
87  // 2. Otherwise allocate a slice, up to the remaining length needed
88  // or our maximum allocation size
89  // 3. Provide the slice start and size available
90  // 4. Add the slice being returned to the slice buffer
91  size_t remain = static_cast<size_t>(total_size_ - byte_count_);
92  if (have_backup_) {
95  have_backup_ = false;
96  if (GRPC_SLICE_LENGTH(slice_) > remain) {
98  }
99  } else {
100  // When less than a whole block is needed, only allocate that much.
101  // But make sure the allocated slice is not inlined.
102  size_t allocate_length =
103  remain > static_cast<size_t>(block_size_) ? block_size_ : remain;
105  allocate_length > GRPC_SLICE_INLINED_SIZE
106  ? allocate_length
108  }
110  // On win x64, int is only 32bit
112  byte_count_ += * size = static_cast<int>(GRPC_SLICE_LENGTH(slice_));
113  // Using grpc_slice_buffer_add could modify slice_ and merge it with the
114  // previous slice. Therefore, use grpc_slice_buffer_add_indexed method to
115  // ensure the slice gets added at a separate index. It can then be kept
116  // around and popped later in the BackUp function.
118  slice_);
119  return true;
120  }
121 
125  void BackUp(int count) override {
126  // count == 0 is invoked by ZeroCopyOutputStream users indicating that any
127  // potential buffer obtained through a previous call to Next() is final.
128  // ZeroCopyOutputStream implementations such as streaming output can use
129  // these calls to flush any temporary buffer and flush the output. The logic
130  // below is not robust against count == 0 invocations, so directly return.
131  if (count == 0) return;
132 
137  GPR_CODEGEN_ASSERT(count <= static_cast<int>(GRPC_SLICE_LENGTH(slice_)));
139  if (static_cast<size_t>(count) == GRPC_SLICE_LENGTH(slice_)) {
141  } else {
145  }
146  // It's dangerous to keep an inlined grpc_slice as the backup slice, since
147  // on a following Next() call, a reference will be returned to this slice
148  // via GRPC_SLICE_START_PTR, which will not be an address held by
149  // slice_buffer_.
150  have_backup_ = backup_slice_.refcount != nullptr;
151  byte_count_ -= count;
152  }
153 
155  int64_t ByteCount() const override { return byte_count_; }
156 
157  // These protected members are needed to support internal optimizations.
158  // they expose internal bits of grpc core that are NOT stable. If you have
159  // a use case needs to use one of these functions, please send an email to
160  // https://groups.google.com/forum/#!forum/grpc-io.
161  protected:
163  void set_byte_count(int64_t byte_count) { byte_count_ = byte_count; }
164 
165  private:
166  // friend for testing purposes only
168  const int block_size_;
169  const int total_size_;
177 };
178 
179 } // namespace grpc
180 
181 #endif // GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_WRITER_H
grpc_slice::refcount
struct grpc_slice_refcount * refcount
Definition: include/grpc/impl/codegen/slice.h:66
grpc::CoreCodegenInterface::grpc_slice_unref
virtual void grpc_slice_unref(grpc_slice slice)=0
grpc::CoreCodegenInterface::grpc_slice_buffer_add
virtual void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)=0
grpc
Definition: grpcpp/alarm.h:33
false
#define false
Definition: setup_once.h:323
grpc_byte_buffer::grpc_byte_buffer_data::raw
struct grpc_byte_buffer::grpc_byte_buffer_data::grpc_compressed_buffer raw
serialization_traits.h
grpc::ProtoBufferWriter::slice_buffer
grpc_slice_buffer * slice_buffer()
Definition: impl/codegen/proto_buffer_writer.h:162
core_codegen_interface.h
slice.h
GRPC_SLICE_SET_LENGTH
#define GRPC_SLICE_SET_LENGTH(slice, newlen)
Definition: include/grpc/impl/codegen/slice.h:107
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
grpc::ProtoBufferWriter::set_byte_count
void set_byte_count(int64_t byte_count)
Definition: impl/codegen/proto_buffer_writer.h:163
grpc::ProtoBufferWriter::block_size_
const int block_size_
size to alloc for each new grpc_slice needed
Definition: impl/codegen/proto_buffer_writer.h:168
grpc::ProtoBufferWriter::~ProtoBufferWriter
~ProtoBufferWriter() override
Definition: impl/codegen/proto_buffer_writer.h:75
grpc::ProtoBufferWriter::Next
bool Next(void **data, int *size) override
Definition: impl/codegen/proto_buffer_writer.h:83
grpc_types.h
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: include/grpcpp/impl/codegen/byte_buffer.h:164
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::CoreCodegenInterface::grpc_slice_split_tail
virtual grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split)=0
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc::CoreCodegenInterface::grpc_raw_byte_buffer_create
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
grpc_byte_buffer
Definition: grpc_types.h:43
grpc::ProtoBufferWriter::have_backup_
bool have_backup_
if we are holding a backup slice or not
Definition: impl/codegen/proto_buffer_writer.h:173
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc::ByteBuffer
A sequence of bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:61
grpc::ProtoBufferWriter::BackUp
void BackUp(int count) override
Definition: impl/codegen/proto_buffer_writer.h:125
grpc::ProtoBufferWriter
Definition: impl/codegen/proto_buffer_writer.h:55
grpc::CoreCodegenInterface::grpc_slice_malloc
virtual grpc_slice grpc_slice_malloc(size_t length)=0
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
byte_buffer.h
grpc::ProtoBufferWriter::ByteCount
int64_t ByteCount() const override
Returns the total number of bytes written since this object was created.
Definition: impl/codegen/proto_buffer_writer.h:155
grpc::kProtoBufferWriterMaxBufferLength
const int kProtoBufferWriterMaxBufferLength
Definition: impl/codegen/proto_buffer_writer.h:46
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
grpc_byte_buffer::data
union grpc_byte_buffer::grpc_byte_buffer_data data
status.h
grpc::ProtoBufferWriter::backup_slice_
grpc_slice backup_slice_
Definition: impl/codegen/proto_buffer_writer.h:174
grpc::CoreCodegenInterface::grpc_slice_buffer_add_indexed
virtual void grpc_slice_buffer_add_indexed(grpc_slice_buffer *sb, grpc_slice slice)=0
grpc::ProtoBufferWriter::slice_buffer_
grpc_slice_buffer * slice_buffer_
internal buffer of slices holding the serialized data
Definition: impl/codegen/proto_buffer_writer.h:172
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
config_protobuf.h
GRPC_SLICE_INLINED_SIZE
#define GRPC_SLICE_INLINED_SIZE
Definition: include/grpc/impl/codegen/slice.h:49
grpc::CoreCodegenInterface::grpc_slice_buffer_pop
virtual void grpc_slice_buffer_pop(grpc_slice_buffer *sb)=0
grpc_byte_buffer::grpc_byte_buffer_data::grpc_compressed_buffer::slice_buffer
grpc_slice_buffer slice_buffer
Definition: grpc_types.h:52
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
grpc::ByteBuffer::set_buffer
void set_buffer(grpc_byte_buffer *buf)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:194
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::ProtoBufferWriter::ProtoBufferWriter
ProtoBufferWriter(ByteBuffer *byte_buffer, int block_size, int total_size)
Definition: impl/codegen/proto_buffer_writer.h:62
grpc::ProtoBufferWriter::total_size_
const int total_size_
byte size of proto being serialized
Definition: impl/codegen/proto_buffer_writer.h:169
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
grpc::internal::ProtoBufferWriterPeer
Definition: proto_utils_test.cc:34
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc::ProtoBufferWriter::byte_count_
int64_t byte_count_
bytes written since this object was created
Definition: impl/codegen/proto_buffer_writer.h:170
grpc::ProtoBufferWriter::slice_
grpc_slice slice_
current slice passed back to the caller
Definition: impl/codegen/proto_buffer_writer.h:176


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:56