1 /*
2 * Copyright (C) 2019 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/trace_processor/trace_blob.h"
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "perfetto/base/compiler.h"
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/utils.h"
25 #include "perfetto/trace_processor/basic_types.h"
26
27 #if TRACE_PROCESSOR_HAS_MMAP()
28 #include <sys/mman.h>
29 #endif
30
31 namespace perfetto {
32 namespace trace_processor {
33
34 // static
Allocate(size_t size)35 TraceBlob TraceBlob::Allocate(size_t size) {
36 TraceBlob blob(Ownership::kHeapBuf, new uint8_t[size], size);
37 PERFETTO_CHECK(blob.data_);
38 return blob;
39 }
40
41 // static
CopyFrom(const void * src,size_t size)42 TraceBlob TraceBlob::CopyFrom(const void* src, size_t size) {
43 TraceBlob blob = Allocate(size);
44 memcpy(blob.data_, src, size);
45 return blob;
46 }
47
48 // static
TakeOwnership(std::unique_ptr<uint8_t[]> buf,size_t size)49 TraceBlob TraceBlob::TakeOwnership(std::unique_ptr<uint8_t[]> buf,
50 size_t size) {
51 PERFETTO_CHECK(buf);
52 return TraceBlob(Ownership::kHeapBuf, buf.release(), size);
53 }
54
55 // static
FromMmap(void * data,size_t size)56 TraceBlob TraceBlob::FromMmap(void* data, size_t size) {
57 #if TRACE_PROCESSOR_HAS_MMAP()
58 PERFETTO_CHECK(data && data != MAP_FAILED);
59 return TraceBlob(Ownership::kMmaped, static_cast<uint8_t*>(data), size);
60 #else
61 base::ignore_result(data);
62 base::ignore_result(size);
63 PERFETTO_FATAL("mmap not supported");
64 #endif
65 }
66
~TraceBlob()67 TraceBlob::~TraceBlob() {
68 switch (ownership_) {
69 case Ownership::kHeapBuf:
70 delete[] data_;
71 break;
72
73 case Ownership::kMmaped:
74 #if TRACE_PROCESSOR_HAS_MMAP()
75 PERFETTO_CHECK(munmap(data_, size_) == 0);
76 #else
77 PERFETTO_FATAL("mmap not supported");
78 #endif
79 break;
80
81 case Ownership::kNull:
82 // Nothing to do.
83 break;
84 }
85 data_ = nullptr;
86 size_ = 0;
87 }
88
operator =(TraceBlob && other)89 TraceBlob& TraceBlob::operator=(TraceBlob&& other) noexcept {
90 if (this == &other)
91 return *this;
92 static_assert(sizeof(*this) == base::AlignUp<sizeof(void*)>(
93 sizeof(data_) + sizeof(size_) +
94 sizeof(ownership_) + sizeof(RefCounted)),
95 "TraceBlob move operator needs updating");
96 data_ = other.data_;
97 size_ = other.size_;
98 ownership_ = other.ownership_;
99 other.data_ = nullptr;
100 other.size_ = 0;
101 other.ownership_ = Ownership::kNull;
102 RefCounted::operator=(std::move(other));
103 return *this;
104 }
105
106 } // namespace trace_processor
107 } // namespace perfetto
108