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_NE(static_cast<T>(nullptr), trace);
25 E : DCHECK_NE(static_cast<T>(nullptr), live);
26 E : base::AutoLock auto_lock(lock_);
27 :
28 E : auto insert_trace_live = trace_live_.insert(std::make_pair(trace, live));
29 :
30 E : if (!insert_trace_live.second) {
31 E : LOG(ERROR) << "Trace argument was previously added.";
32 E : return false;
33 : }
34 :
35 E : auto insert_live_trace = live_trace_.insert(std::make_pair(live, trace));
36 :
37 E : if (!insert_live_trace.second) {
38 E : LOG(ERROR) << "Live argument was previously added.";
39 E : trace_live_.erase(insert_trace_live.first);
40 E : return false;
41 : }
42 :
43 E : return true;
44 E : }
45 :
46 : template <typename T>
47 E : bool TraceLiveMap<T>::RemoveMapping(T trace, T live) {
48 E : DCHECK_NE(static_cast<T>(nullptr), trace);
49 E : DCHECK_NE(static_cast<T>(nullptr), live);
50 E : base::AutoLock auto_lock(lock_);
51 :
52 E : auto find_trace_live = trace_live_.find(trace);
53 E : auto find_live_trace = live_trace_.find(live);
54 :
55 E : if (find_trace_live == trace_live_.end()) {
56 E : LOG(ERROR) << "Trace was not previously added.";
57 E : return false;
58 : }
59 :
60 E : if (find_live_trace == live_trace_.end()) {
61 i : LOG(ERROR) << "Live was not previously added.";
62 i : return false;
63 : }
64 :
65 E : trace_live_.erase(find_trace_live);
66 E : live_trace_.erase(find_live_trace);
67 E : return true;
68 E : }
69 :
70 : template <typename T>
71 E : bool TraceLiveMap<T>::GetLiveFromTrace(T trace, T* live) {
72 E : DCHECK_NE(static_cast<T>(nullptr), trace);
73 E : base::AutoLock auto_lock(lock_);
74 :
75 E : auto live_it = trace_live_.find(trace);
76 E : if (live_it == trace_live_.end()) {
77 E : LOG(ERROR) << "Trace argument was not previously added.";
78 E : return false;
79 : }
80 :
81 E : *live = live_it->second;
82 E : return true;
83 E : }
84 :
85 : template <typename T>
86 E : bool TraceLiveMap<T>::GetTraceFromLive(T live, T* trace) {
87 E : DCHECK_NE(static_cast<T>(nullptr), live);
88 E : base::AutoLock auto_lock(lock_);
89 :
90 E : auto trace_it = live_trace_.find(live);
91 E : if (trace_it == live_trace_.end()) {
92 E : LOG(ERROR) << "Live argument was not previously added.";
93 E : return false;
94 : }
95 :
96 E : *trace = trace_it->second;
97 E : return true;
98 E : }
99 :
100 : } // namespace bard
101 :
102 : #endif // SYZYGY_BARD_TRACE_LIVE_MAP_IMPL_H_
|