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/tsl/platform/protobuf.h" |
17 | |
18 | namespace tsl { |
19 | |
20 | const char* kProtobufInt64Typename = "::tensorflow::protobuf_int64" ; |
21 | const char* kProtobufUint64Typename = "::tensorflow::protobuf_uint64" ; |
22 | |
23 | TStringOutputStream::TStringOutputStream(tstring* target) : target_(target) {} |
24 | |
25 | bool TStringOutputStream::Next(void** data, int* size) { |
26 | size_t 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<size_t>::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(std::max( |
43 | old_size * 2, |
44 | (size_t)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 | |
52 | void TStringOutputStream::BackUp(int count) { |
53 | target_->resize(target_->size() - count); |
54 | } |
55 | |
56 | int64_t TStringOutputStream::ByteCount() const { return target_->size(); } |
57 | |
58 | } // namespace tsl |
59 | |