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 #ifndef INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_ 18 #define INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_ 19 20 #include <stddef.h> 21 #include <string.h> 22 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include "perfetto/base/logging.h" 28 29 namespace perfetto { 30 31 // A simple wrapper around a virtually contiguous memory range that contains a 32 // TracePacket, or just a portion of it. 33 struct Slice { SliceSlice34 Slice() : start(nullptr), size(0) {} SliceSlice35 Slice(const void* st, size_t sz) : start(st), size(sz) {} 36 Slice(Slice&& other) noexcept = default; 37 38 // Create a Slice which owns |size| bytes of memory. AllocateSlice39 static Slice Allocate(size_t size) { 40 Slice slice; 41 slice.own_data_.reset(new uint8_t[size]); 42 slice.start = &slice.own_data_[0]; 43 slice.size = size; 44 return slice; 45 } 46 TakeOwnershipSlice47 static Slice TakeOwnership(std::unique_ptr<uint8_t[]> buf, size_t size) { 48 Slice slice; 49 slice.own_data_ = std::move(buf); 50 slice.start = &slice.own_data_[0]; 51 slice.size = size; 52 return slice; 53 } 54 own_dataSlice55 uint8_t* own_data() { 56 PERFETTO_DCHECK(own_data_); 57 return own_data_.get(); 58 } 59 60 const void* start; 61 size_t size; 62 63 private: 64 Slice(const Slice&) = delete; 65 void operator=(const Slice&) = delete; 66 67 std::unique_ptr<uint8_t[]> own_data_; 68 }; 69 70 // TODO(primiano): most TracePacket(s) fit in a slice or two. We need something 71 // a bit more clever here that has inline capacity for 2 slices and then uses a 72 // std::forward_list or a std::vector for the less likely cases. 73 using Slices = std::vector<Slice>; 74 75 } // namespace perfetto 76 77 #endif // INCLUDE_PERFETTO_EXT_TRACING_CORE_SLICE_H_ 78