Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: components/tracing/core/proto_zero_message_handle.cc

Issue 2158323005: tracing v2: Introduce handles for safe usage of ProtoZeroMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final nits Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/tracing/core/proto_zero_message_handle.h"
6
7 #include "base/logging.h"
8 #include "components/tracing/core/proto_zero_message.h"
9
10 namespace tracing {
11 namespace v2 {
12
13 namespace {
14
15 inline void FinalizeMessageIfSet(ProtoZeroMessage* message) {
16 if (message) {
17 message->Finalize();
18 #if DCHECK_IS_ON()
19 message->set_handle(nullptr);
20 #endif
21 }
22 }
23
24 } // namespace
25
26 ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
27 ProtoZeroMessage* message)
28 : message_(message) {
29 #if DCHECK_IS_ON()
30 message_->set_handle(this);
31 #endif
32 }
33
34 ProtoZeroMessageHandleBase::~ProtoZeroMessageHandleBase() {
35 FinalizeMessageIfSet(message_);
36 }
37
38 ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
39 ProtoZeroMessageHandleBase&& other) {
40 Move(&other);
41 }
42
43 ProtoZeroMessageHandleBase& ProtoZeroMessageHandleBase::operator=(
44 ProtoZeroMessageHandleBase&& other) {
45 Move(&other);
46 return *this;
47 }
48
49 void ProtoZeroMessageHandleBase::Move(ProtoZeroMessageHandleBase* other) {
50 // If the current handle was pointing to a message and is being reset to a new
51 // one, finalize the old message.
52 FinalizeMessageIfSet(message_);
53
54 // In theory other->message_ could be nullptr, if |other| is a handle that has
55 // been std::move-d (and hence empty). There isn't a legitimate use case for
56 // doing so, though. Therefore this case is deliberately ignored (if hit, it
57 // will manifest as a segfault when dereferencing |message_| below) to avoid a
58 // useless null-check.
59 message_ = other->message_;
60 other->message_ = nullptr;
61 #if DCHECK_IS_ON()
62 message_->set_handle(this);
63 #endif
64 }
65
66 } // namespace v2
67 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/core/proto_zero_message_handle.h ('k') | components/tracing/core/proto_zero_message_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698