1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/platform/protobuf.h"
17
18 namespace tensorflow {
19
20 const char* kProtobufInt64Typename = "::tensorflow::protobuf_int64";
21 const char* kProtobufUint64Typename = "::tensorflow::protobuf_uint64";
22
TStringOutputStream(tstring * target)23 TStringOutputStream::TStringOutputStream(tstring* target) : target_(target) {}
24
Next(void ** data,int * size)25 bool TStringOutputStream::Next(void** data, int* size) {
26 int old_size = target_->size();
27
28 // Grow the string.
29 if (old_size < target_->capacity()) {
30 // Resize the string to match its capacity, since we can get away
31 // without a memory allocation this way.
32 target_->resize_uninitialized(target_->capacity());
33 } else {
34 // Size has reached capacity, try to double the size.
35 if (old_size > std::numeric_limits<int>::max() / 2) {
36 // Can not double the size otherwise it is going to cause integer
37 // overflow in the expression below: old_size * 2 ";
38 return false;
39 }
40 // Double the size, also make sure that the new size is at least
41 // kMinimumSize.
42 target_->resize_uninitialized(
43 std::max(old_size * 2,
44 kMinimumSize + 0)); // "+ 0" works around GCC4 weirdness.
45 }
46
47 *data = target_->data() + old_size;
48 *size = target_->size() - old_size;
49 return true;
50 }
51
BackUp(int count)52 void TStringOutputStream::BackUp(int count) {
53 target_->resize(target_->size() - count);
54 }
55
ByteCount() const56 int64_t TStringOutputStream::ByteCount() const { return target_->size(); }
57
58 } // namespace tensorflow
59