1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "perfetto/protozero/packed_repeated_fields.h" 18 19 #include "perfetto/ext/base/utils.h" 20 21 namespace protozero { 22 GrowSlowpath()23void PackedBufferBase::GrowSlowpath() { 24 size_t write_off = static_cast<size_t>(write_ptr_ - storage_begin_); 25 size_t old_size = static_cast<size_t>(storage_end_ - storage_begin_); 26 size_t new_size = old_size < 65536 ? (old_size * 2) : (old_size * 3 / 2); 27 new_size = perfetto::base::AlignUp<4096>(new_size); 28 std::unique_ptr<uint8_t[]> new_buf(new uint8_t[new_size]); 29 memcpy(new_buf.get(), storage_begin_, old_size); 30 heap_buf_ = std::move(new_buf); 31 storage_begin_ = heap_buf_.get(); 32 storage_end_ = storage_begin_ + new_size; 33 write_ptr_ = storage_begin_ + write_off; 34 } 35 Reset()36void PackedBufferBase::Reset() { 37 heap_buf_.reset(); 38 storage_begin_ = reinterpret_cast<uint8_t*>(&stack_buf_[0]); 39 storage_end_ = reinterpret_cast<uint8_t*>(&stack_buf_[kOnStackStorageSize]); 40 write_ptr_ = storage_begin_; 41 } 42 43 } // namespace protozero 44