protobuf/src/google/protobuf/stubs/bytestream.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/stubs/bytestream.h>
32 
33 #include <string.h>
34 #include <algorithm>
35 
36 #include <google/protobuf/stubs/logging.h>
37 
38 namespace google {
39 namespace protobuf {
40 namespace strings {
41 
42 void ByteSource::CopyTo(ByteSink* sink, size_t n) {
43  while (n > 0) {
44  StringPiece fragment = Peek();
45  if (fragment.empty()) {
46  GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input.";
47  break;
48  }
49  std::size_t fragment_size = std::min<std::size_t>(n, fragment.size());
50  sink->Append(fragment.data(), fragment_size);
51  Skip(fragment_size);
52  n -= fragment_size;
53  }
54 }
55 
56 void ByteSink::Flush() {}
57 
58 void UncheckedArrayByteSink::Append(const char* data, size_t n) {
59  if (data != dest_) {
60  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
61  GOOGLE_DCHECK(!(dest_ <= data && data < (dest_ + n)))
62  << "Append() data[] overlaps with dest_[]";
63  memcpy(dest_, data, n);
64  }
65  dest_ += n;
66 }
67 
69  : outbuf_(outbuf), capacity_(capacity), size_(0), overflowed_(false) {
70 }
71 
72 void CheckedArrayByteSink::Append(const char* bytes, size_t n) {
73  size_t available = capacity_ - size_;
74  if (n > available) {
75  n = available;
76  overflowed_ = true;
77  }
78  if (n > 0 && bytes != (outbuf_ + size_)) {
79  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
81  << "Append() bytes[] overlaps with outbuf_[]";
82  memcpy(outbuf_ + size_, bytes, n);
83  }
84  size_ += n;
85 }
86 
87 GrowingArrayByteSink::GrowingArrayByteSink(size_t estimated_size)
88  : capacity_(estimated_size),
89  buf_(new char[estimated_size]),
90  size_(0) {
91 }
92 
94  delete[] buf_; // Just in case the user didn't call GetBuffer.
95 }
96 
97 void GrowingArrayByteSink::Append(const char* bytes, size_t n) {
98  size_t available = capacity_ - size_;
99  if (bytes != (buf_ + size_)) {
100  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
101  // We need to test for this before calling Expand() which may reallocate.
102  GOOGLE_DCHECK(!(buf_ <= bytes && bytes < (buf_ + capacity_)))
103  << "Append() bytes[] overlaps with buf_[]";
104  }
105  if (n > available) {
106  Expand(n - available);
107  }
108  if (n > 0 && bytes != (buf_ + size_)) {
109  memcpy(buf_ + size_, bytes, n);
110  }
111  size_ += n;
112 }
113 
114 char* GrowingArrayByteSink::GetBuffer(size_t* nbytes) {
115  ShrinkToFit();
116  char* b = buf_;
117  *nbytes = size_;
118  buf_ = nullptr;
119  size_ = capacity_ = 0;
120  return b;
121 }
122 
123 void GrowingArrayByteSink::Expand(size_t amount) { // Expand by at least 50%.
124  size_t new_capacity = std::max(capacity_ + amount, (3 * capacity_) / 2);
125  char* bigger = new char[new_capacity];
126  memcpy(bigger, buf_, size_);
127  delete[] buf_;
128  buf_ = bigger;
129  capacity_ = new_capacity;
130 }
131 
133  // Shrink only if the buffer is large and size_ is less than 3/4
134  // of capacity_.
135  if (capacity_ > 256 && size_ < (3 * capacity_) / 4) {
136  char* just_enough = new char[size_];
137  memcpy(just_enough, buf_, size_);
138  delete[] buf_;
139  buf_ = just_enough;
140  capacity_ = size_;
141  }
142 }
143 
144 void StringByteSink::Append(const char* data, size_t n) {
145  dest_->append(data, n);
146 }
147 
148 size_t ArrayByteSource::Available() const {
149  return input_.size();
150 }
151 
152 StringPiece ArrayByteSource::Peek() {
153  return input_;
154 }
155 
156 void ArrayByteSource::Skip(size_t n) {
159 }
160 
161 LimitByteSource::LimitByteSource(ByteSource *source, size_t limit)
162  : source_(source),
163  limit_(limit) {
164 }
165 
166 size_t LimitByteSource::Available() const {
167  size_t available = source_->Available();
168  if (available > limit_) {
169  available = limit_;
170  }
171 
172  return available;
173 }
174 
175 StringPiece LimitByteSource::Peek() {
176  StringPiece piece = source_->Peek();
177  return StringPiece(piece.data(), std::min(piece.size(), limit_));
178 }
179 
180 void LimitByteSource::Skip(size_t n) {
182  source_->Skip(n);
183  limit_ -= n;
184 }
185 
186 void LimitByteSource::CopyTo(ByteSink *sink, size_t n) {
188  source_->CopyTo(sink, n);
189  limit_ -= n;
190 }
191 
192 } // namespace strings
193 } // namespace protobuf
194 } // namespace google
google::protobuf::strings::StringByteSink::dest_
string * dest_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:263
google::protobuf::strings::GrowingArrayByteSink::GrowingArrayByteSink
GrowingArrayByteSink(size_t estimated_size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:87
google::protobuf::strings::ByteSink::Flush
virtual void Flush()
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:56
google::protobuf::strings::LimitByteSource::Peek
virtual StringPiece Peek() override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:175
google::protobuf::strings::GrowingArrayByteSink::Append
virtual void Append(const char *bytes, size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:97
false
#define false
Definition: setup_once.h:323
capacity
uint16_t capacity
Definition: protobuf/src/google/protobuf/descriptor.cc:948
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:194
google::protobuf::strings::GrowingArrayByteSink::buf_
char * buf_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:242
string.h
outbuf
unsigned char outbuf[SIZE]
Definition: bloaty/third_party/zlib/examples/gun.c:162
google::protobuf::strings::CheckedArrayByteSink::capacity_
const size_t capacity_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:205
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::strings::LimitByteSource::limit_
size_t limit_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:342
env.new
def new
Definition: env.py:51
google::protobuf::strings::ByteSource::Available
virtual size_t Available() const =0
google::protobuf::strings::ArrayByteSource::Peek
virtual StringPiece Peek() override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:152
google::protobuf::strings::LimitByteSource::Available
virtual size_t Available() const override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:166
google::protobuf::strings::CheckedArrayByteSink::outbuf_
char * outbuf_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:204
google::protobuf::strings::ArrayByteSource::Available
virtual size_t Available() const override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:148
google::protobuf::strings::LimitByteSource::Skip
virtual void Skip(size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:184
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
google::protobuf::strings::LimitByteSource::LimitByteSource
LimitByteSource(ByteSource *source, size_t limit)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:161
capacity_
uint32_t capacity_
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:132
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
google::protobuf::strings::GrowingArrayByteSink::Expand
void Expand(size_t amount)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:123
google::protobuf::strings::ByteSource::Peek
virtual StringPiece Peek()=0
google::protobuf::strings::UncheckedArrayByteSink::dest_
char * dest_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:175
buf_
char buf_[N]
Definition: cxa_demangle.cpp:4722
google::protobuf::strings::GrowingArrayByteSink::capacity_
size_t capacity_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:241
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf::strings::GrowingArrayByteSink::~GrowingArrayByteSink
virtual ~GrowingArrayByteSink()
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:93
min
#define min(a, b)
Definition: qsort.h:83
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google::protobuf::strings::ByteSource::Skip
virtual void Skip(size_t n)=0
google::protobuf::strings::StringByteSink::Append
virtual void Append(const char *data, size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:144
google::protobuf::StringPiece::remove_prefix
void remove_prefix(stringpiece_ssize_type n)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:282
google::protobuf::strings::LimitByteSource::CopyTo
virtual void CopyTo(ByteSink *sink, size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:190
google::protobuf::strings::ByteSource::CopyTo
virtual void CopyTo(ByteSink *sink, size_t n)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:42
google::protobuf::strings::CheckedArrayByteSink::Append
virtual void Append(const char *bytes, size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:72
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
size_
size_t size_
Definition: memory_allocator.cc:56
sink
FormatSinkImpl * sink
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:450
google::protobuf::strings::CheckedArrayByteSink::size_
size_t size_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:206
google::protobuf::strings::ArrayByteSource::Skip
virtual void Skip(size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:156
google::protobuf::strings::GrowingArrayByteSink::GetBuffer
char * GetBuffer(size_t *nbytes)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:114
google::protobuf::strings::GrowingArrayByteSink::ShrinkToFit
void ShrinkToFit()
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:132
google::protobuf::strings::CheckedArrayByteSink::CheckedArrayByteSink
CheckedArrayByteSink(char *outbuf, size_t capacity)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:68
google::protobuf::strings::ArrayByteSource::input_
StringPiece input_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:305
google::protobuf::strings::GrowingArrayByteSink::size_
size_t size_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:243
google::protobuf::StringPiece::size
stringpiece_ssize_type size() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:248
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
GOOGLE_DCHECK_LE
#define GOOGLE_DCHECK_LE
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:199
google::protobuf::strings::UncheckedArrayByteSink::Append
virtual void Append(const char *data, size_t n) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.cc:58
google::protobuf::strings::CheckedArrayByteSink::overflowed_
bool overflowed_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:207
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::strings::LimitByteSource::source_
ByteSource * source_
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/bytestream.h:341


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51