• 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 #ifndef INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
18 #define INCLUDE_PERFETTO_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 
37   // Used to inherit ownership of a buffer from a protobuf via release_str().
SliceSlice38   explicit Slice(std::unique_ptr<std::string> str)
39       : start(&(*str)[0]), size(str->size()), moved_str_data_(std::move(str)) {}
40 
41   Slice(Slice&& other) noexcept = default;
42 
43   // Create a Slice which owns |size| bytes of memory.
AllocateSlice44   static Slice Allocate(size_t size) {
45     Slice slice;
46     slice.own_data_.reset(new uint8_t[size]);
47     slice.start = &slice.own_data_[0];
48     slice.size = size;
49     return slice;
50   }
51 
own_dataSlice52   uint8_t* own_data() {
53     PERFETTO_DCHECK(own_data_);
54     return own_data_.get();
55   }
56 
57   const void* start;
58   size_t size;
59 
60  private:
61   Slice(const Slice&) = delete;
62   void operator=(const Slice&) = delete;
63 
64   std::unique_ptr<uint8_t[]> own_data_;
65   std::unique_ptr<std::string> moved_str_data_;
66 };
67 
68 // TODO(primiano): most TracePacket(s) fit in a slice or two. We need something
69 // a bit more clever here that has inline capacity for 2 slices and then uses a
70 // std::forward_list or a std::vector for the less likely cases.
71 using Slices = std::vector<Slice>;
72 
73 }  // namespace perfetto
74 
75 #endif  // INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
76