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