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

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: . 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 "components/tracing/core/proto_zero_message.h"
8
9 namespace tracing {
10 namespace v2 {
11
12 namespace {
13
14 inline void FinalizeMessageIfSet(ProtoZeroMessage* message) {
15 if (message) {
16 message->Finalize();
17 message->set_handle(nullptr);
18 }
19 }
20
21 } // namespace
22
23 ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
24 ProtoZeroMessage* message)
25 : message_(message) {
26 message_->set_handle(this);
27 }
28
29 ProtoZeroMessageHandleBase::~ProtoZeroMessageHandleBase() {
30 FinalizeMessageIfSet(message_);
31 }
32
33 ProtoZeroMessageHandleBase::ProtoZeroMessageHandleBase(
34 ProtoZeroMessageHandleBase&& other) {
35 Move(&other);
36 }
37
38 ProtoZeroMessageHandleBase& ProtoZeroMessageHandleBase::operator=(
39 ProtoZeroMessageHandleBase&& other) {
40 Move(&other);
41 return *this;
42 }
43
44 void ProtoZeroMessageHandleBase::Move(ProtoZeroMessageHandleBase* other) {
45 // If the current handle was pointing to a message and is being reset to a new
46 // one, finalize the old message.
47 FinalizeMessageIfSet(message_);
48
49 // In theory other->message_ could be nullptr, if |other| is a handle that has
50 // been std::move-d (and hence empty). There isn't a legitimate use case for
51 // doing so, though. Threfore this case is deliberately ignored (if hit, it
oystein (OOO til 10th of July) 2016/07/20 21:19:37 typo: Therefore
Primiano Tucci (use gerrit) 2016/07/21 10:50:03 Done.
52 // will manifest as a segfault when dereferencing |message_| below) to avoid a
53 // useles null-check.
alph 2016/07/20 18:35:00 typo: useless
Primiano Tucci (use gerrit) 2016/07/21 10:50:03 Done.
54 message_ = other->message_;
55 other->message_ = nullptr;
56 message_->set_handle(this);
57 }
58
59 } // namespace v2
60 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698