• 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/scattered_heap_buffer.h"
18 
19 #include <algorithm>
20 
21 namespace protozero {
22 
Slice()23 ScatteredHeapBuffer::Slice::Slice()
24     : buffer_(nullptr), size_(0u), unused_bytes_(0u) {}
25 
Slice(size_t size)26 ScatteredHeapBuffer::Slice::Slice(size_t size)
27     : buffer_(std::unique_ptr<uint8_t[]>(new uint8_t[size])),
28       size_(size),
29       unused_bytes_(size) {
30   PERFETTO_DCHECK(size);
31   Clear();
32 }
33 
34 ScatteredHeapBuffer::Slice::Slice(Slice&& slice) noexcept = default;
35 
36 ScatteredHeapBuffer::Slice::~Slice() = default;
37 
38 ScatteredHeapBuffer::Slice& ScatteredHeapBuffer::Slice::operator=(Slice&&) =
39     default;
40 
Clear()41 void ScatteredHeapBuffer::Slice::Clear() {
42   unused_bytes_ = size_;
43 #if PERFETTO_DCHECK_IS_ON()
44   memset(start(), 0xff, size_);
45 #endif  // PERFETTO_DCHECK_IS_ON()
46 }
47 
ScatteredHeapBuffer(size_t initial_slice_size_bytes,size_t maximum_slice_size_bytes)48 ScatteredHeapBuffer::ScatteredHeapBuffer(size_t initial_slice_size_bytes,
49                                          size_t maximum_slice_size_bytes)
50     : next_slice_size_(initial_slice_size_bytes),
51       maximum_slice_size_(maximum_slice_size_bytes) {
52   PERFETTO_DCHECK(next_slice_size_ && maximum_slice_size_);
53   PERFETTO_DCHECK(maximum_slice_size_ >= initial_slice_size_bytes);
54 }
55 
56 ScatteredHeapBuffer::~ScatteredHeapBuffer() = default;
57 
GetNewBuffer()58 protozero::ContiguousMemoryRange ScatteredHeapBuffer::GetNewBuffer() {
59   PERFETTO_CHECK(writer_);
60   AdjustUsedSizeOfCurrentSlice();
61 
62   if (cached_slice_.start()) {
63     slices_.push_back(std::move(cached_slice_));
64     PERFETTO_DCHECK(!cached_slice_.start());
65   } else {
66     slices_.emplace_back(next_slice_size_);
67   }
68   next_slice_size_ = std::min(maximum_slice_size_, next_slice_size_ * 2);
69   return slices_.back().GetTotalRange();
70 }
71 
StitchSlices()72 std::vector<uint8_t> ScatteredHeapBuffer::StitchSlices() {
73   AdjustUsedSizeOfCurrentSlice();
74   std::vector<uint8_t> buffer;
75   for (const auto& slice : slices_) {
76     auto used_range = slice.GetUsedRange();
77     buffer.insert(buffer.end(), used_range.begin, used_range.end);
78   }
79   return buffer;
80 }
81 
GetRanges()82 std::vector<protozero::ContiguousMemoryRange> ScatteredHeapBuffer::GetRanges() {
83   AdjustUsedSizeOfCurrentSlice();
84   std::vector<protozero::ContiguousMemoryRange> ranges;
85   for (const auto& slice : slices_)
86     ranges.push_back(slice.GetUsedRange());
87   return ranges;
88 }
89 
AdjustUsedSizeOfCurrentSlice()90 void ScatteredHeapBuffer::AdjustUsedSizeOfCurrentSlice() {
91   if (!slices_.empty())
92     slices_.back().set_unused_bytes(writer_->bytes_available());
93 }
94 
GetTotalSize()95 size_t ScatteredHeapBuffer::GetTotalSize() {
96   size_t total_size = 0;
97   for (auto& slice : slices_) {
98     total_size += slice.size();
99   }
100   return total_size;
101 }
102 
Reset()103 void ScatteredHeapBuffer::Reset() {
104   if (slices_.empty())
105     return;
106   cached_slice_ = std::move(slices_.front());
107   cached_slice_.Clear();
108   slices_.clear();
109 }
110 
111 }  // namespace protozero
112