1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_BARD_TRACE_LIVE_MAP_IMPL_H_
16 : #define SYZYGY_BARD_TRACE_LIVE_MAP_IMPL_H_
17 :
18 : #include "base/logging.h"
19 :
20 : namespace bard {
21 :
22 : template <typename T>
23 E : bool TraceLiveMap<T>::AddMapping(T trace, T live) {
24 E : DCHECK((trace == nullptr) == (live == nullptr));
25 E : if (trace == nullptr && live == nullptr)
26 i : return true;
27 :
28 E : base::AutoLock auto_lock(lock_);
29 :
30 E : auto insert_trace_live = trace_live_.insert(std::make_pair(trace, live));
31 :
32 E : if (!insert_trace_live.second) {
33 E : LOG(ERROR) << "Trace argument was previously added: " << trace;
34 E : return false;
35 : }
36 :
37 E : auto insert_live_trace = live_trace_.insert(std::make_pair(live, trace));
38 :
39 E : if (!insert_live_trace.second) {
40 E : LOG(ERROR) << "Live argument was previously added: " << live;
41 E : trace_live_.erase(insert_trace_live.first);
42 E : return false;
43 : }
44 :
45 E : return true;
46 E : }
47 :
48 : template <typename T>
49 E : bool TraceLiveMap<T>::RemoveMapping(T trace, T live) {
50 E : DCHECK((trace == nullptr) == (live == nullptr));
51 E : if (trace == nullptr && live == nullptr)
52 i : return true;
53 :
54 E : base::AutoLock auto_lock(lock_);
55 :
56 E : auto find_trace_live = trace_live_.find(trace);
57 E : auto find_live_trace = live_trace_.find(live);
58 :
59 E : if (find_trace_live == trace_live_.end()) {
60 E : LOG(ERROR) << "Trace was not previously added:" << trace;
61 E : return false;
62 : }
63 :
64 E : if (find_live_trace == live_trace_.end()) {
65 i : LOG(ERROR) << "Live was not previously added: " << live;
66 i : return false;
67 : }
68 :
69 E : trace_live_.erase(find_trace_live);
70 E : live_trace_.erase(find_live_trace);
71 E : return true;
72 E : }
73 :
74 : template <typename T>
75 E : bool TraceLiveMap<T>::GetLiveFromTrace(T trace, T* live) {
76 E : DCHECK_NE(static_cast<T*>(nullptr), live);
77 E : if (trace == nullptr) {
78 i : *live = nullptr;
79 i : return true;
80 : }
81 :
82 E : base::AutoLock auto_lock(lock_);
83 :
84 E : auto live_it = trace_live_.find(trace);
85 E : if (live_it == trace_live_.end()) {
86 E : LOG(ERROR) << "Trace argument was not previously added: " << trace;
87 E : return false;
88 : }
89 :
90 E : *live = live_it->second;
91 E : return true;
92 E : }
93 :
94 : template <typename T>
95 E : bool TraceLiveMap<T>::GetTraceFromLive(T live, T* trace) {
96 E : DCHECK_NE(static_cast<T*>(nullptr), trace);
97 E : if (live == nullptr) {
98 i : *trace = nullptr;
99 i : return true;
100 : }
101 :
102 E : base::AutoLock auto_lock(lock_);
103 :
104 E : auto trace_it = live_trace_.find(live);
105 E : if (trace_it == live_trace_.end()) {
106 E : LOG(ERROR) << "Live argument was not previously added: " << live;
107 E : return false;
108 : }
109 :
110 E : *trace = trace_it->second;
111 E : return true;
112 E : }
113 :
114 : template <typename T>
115 E : void TraceLiveMap<T>::Clear() {
116 E : trace_live_.clear();
117 E : live_trace_.clear();
118 E : }
119 :
120 : } // namespace bard
121 :
122 : #endif // SYZYGY_BARD_TRACE_LIVE_MAP_IMPL_H_
|