blob: 50e4751a89733943fe99cf8f0ec1eaeb3d186813 [file] [log] [blame]
Primiano Tucci3264b592021-11-08 18:20:51 +00001/*
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/logging.h"
23#include "perfetto/ext/base/utils.h"
24#include "perfetto/trace_processor/basic_types.h"
25
26namespace perfetto {
27namespace trace_processor {
28
29// static
30TraceBlob TraceBlob::Allocate(size_t size) {
31 TraceBlob blob(Ownership::kHeapBuf, new uint8_t[size], size);
32 PERFETTO_CHECK(blob.data_);
33 return blob;
34}
35
36// static
37TraceBlob TraceBlob::CopyFrom(const void* src, size_t size) {
38 TraceBlob blob = Allocate(size);
39 memcpy(blob.data_, src, size);
40 return blob;
41}
42
43// static
44TraceBlob TraceBlob::TakeOwnership(std::unique_ptr<uint8_t[]> buf,
45 size_t size) {
46 PERFETTO_CHECK(buf);
47 return TraceBlob(Ownership::kHeapBuf, buf.release(), size);
48}
49
50TraceBlob::~TraceBlob() {
51 PERFETTO_CHECK(refcount_ == 0);
52 switch (ownership_) {
53 case Ownership::kHeapBuf:
54 delete[] data_;
55 break;
56
57 case Ownership::kNull:
58 // Nothing to do.
59 break;
60 }
61 data_ = nullptr;
62 size_ = 0;
63}
64
65TraceBlob& TraceBlob::operator=(TraceBlob&& other) noexcept {
66 if (this == &other)
67 return *this;
68 static_assert(sizeof(*this) == base::AlignUp<sizeof(void*)>(
69 sizeof(data_) + sizeof(size_) +
70 sizeof(ownership_) + sizeof(refcount_)),
71 "TraceBlob move operator needs updating");
72 data_ = other.data_;
73 size_ = other.size_;
74 ownership_ = other.ownership_;
75 refcount_ = other.refcount_;
76 other.refcount_ = 0;
77 other.data_ = nullptr;
78 other.size_ = 0;
79 other.ownership_ = Ownership::kNull;
80 return *this;
81}
82
83} // namespace trace_processor
84} // namespace perfetto