• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 // static
24 constexpr size_t PackedBufferBase::kOnStackStorageSize;
25 
GrowSlowpath()26 void PackedBufferBase::GrowSlowpath() {
27   size_t write_off = static_cast<size_t>(write_ptr_ - storage_begin_);
28   size_t old_size = static_cast<size_t>(storage_end_ - storage_begin_);
29   size_t new_size = old_size < 65536 ? (old_size * 2) : (old_size * 3 / 2);
30   new_size = perfetto::base::AlignUp<4096>(new_size);
31   std::unique_ptr<uint8_t[]> new_buf(new uint8_t[new_size]);
32   memcpy(new_buf.get(), storage_begin_, old_size);
33   heap_buf_ = std::move(new_buf);
34   storage_begin_ = heap_buf_.get();
35   storage_end_ = storage_begin_ + new_size;
36   write_ptr_ = storage_begin_ + write_off;
37 }
38 
Reset()39 void PackedBufferBase::Reset() {
40   heap_buf_.reset();
41   storage_begin_ = reinterpret_cast<uint8_t*>(&stack_buf_[0]);
42   storage_end_ = reinterpret_cast<uint8_t*>(&stack_buf_[kOnStackStorageSize]);
43   write_ptr_ = storage_begin_;
44 }
45 
46 }  // namespace protozero
47