1 //===-- FormatCache.cpp ------------------------------------------*- C++
2 //-*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11
12
13
14 #include "lldb/DataFormatters/FormatCache.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
Entry()19 FormatCache::Entry::Entry()
20 : m_format_cached(false), m_summary_cached(false),
21 m_synthetic_cached(false), m_validator_cached(false), m_format_sp(),
22 m_summary_sp(), m_synthetic_sp(), m_validator_sp() {}
23
Entry(lldb::TypeFormatImplSP format_sp)24 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp)
25 : m_summary_cached(false), m_synthetic_cached(false),
26 m_validator_cached(false), m_summary_sp(), m_synthetic_sp(),
27 m_validator_sp() {
28 SetFormat(format_sp);
29 }
30
Entry(lldb::TypeSummaryImplSP summary_sp)31 FormatCache::Entry::Entry(lldb::TypeSummaryImplSP summary_sp)
32 : m_format_cached(false), m_synthetic_cached(false),
33 m_validator_cached(false), m_format_sp(), m_synthetic_sp(),
34 m_validator_sp() {
35 SetSummary(summary_sp);
36 }
37
Entry(lldb::SyntheticChildrenSP synthetic_sp)38 FormatCache::Entry::Entry(lldb::SyntheticChildrenSP synthetic_sp)
39 : m_format_cached(false), m_summary_cached(false),
40 m_validator_cached(false), m_format_sp(), m_summary_sp(),
41 m_validator_sp() {
42 SetSynthetic(synthetic_sp);
43 }
44
Entry(lldb::TypeValidatorImplSP validator_sp)45 FormatCache::Entry::Entry(lldb::TypeValidatorImplSP validator_sp)
46 : m_format_cached(false), m_summary_cached(false),
47 m_synthetic_cached(false), m_format_sp(), m_summary_sp(),
48 m_synthetic_sp() {
49 SetValidator(validator_sp);
50 }
51
Entry(lldb::TypeFormatImplSP format_sp,lldb::TypeSummaryImplSP summary_sp,lldb::SyntheticChildrenSP synthetic_sp,lldb::TypeValidatorImplSP validator_sp)52 FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp,
53 lldb::TypeSummaryImplSP summary_sp,
54 lldb::SyntheticChildrenSP synthetic_sp,
55 lldb::TypeValidatorImplSP validator_sp) {
56 SetFormat(format_sp);
57 SetSummary(summary_sp);
58 SetSynthetic(synthetic_sp);
59 SetValidator(validator_sp);
60 }
61
IsFormatCached()62 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
63
IsSummaryCached()64 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
65
IsSyntheticCached()66 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
67
IsValidatorCached()68 bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
69
GetFormat()70 lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
71
GetSummary()72 lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
73 return m_summary_sp;
74 }
75
GetSynthetic()76 lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
77 return m_synthetic_sp;
78 }
79
GetValidator()80 lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
81 return m_validator_sp;
82 }
83
SetFormat(lldb::TypeFormatImplSP format_sp)84 void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
85 m_format_cached = true;
86 m_format_sp = format_sp;
87 }
88
SetSummary(lldb::TypeSummaryImplSP summary_sp)89 void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
90 m_summary_cached = true;
91 m_summary_sp = summary_sp;
92 }
93
SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp)94 void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
95 m_synthetic_cached = true;
96 m_synthetic_sp = synthetic_sp;
97 }
98
SetValidator(lldb::TypeValidatorImplSP validator_sp)99 void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
100 m_validator_cached = true;
101 m_validator_sp = validator_sp;
102 }
103
FormatCache()104 FormatCache::FormatCache()
105 : m_map(), m_mutex()
106 #ifdef LLDB_CONFIGURATION_DEBUG
107 ,
108 m_cache_hits(0), m_cache_misses(0)
109 #endif
110 {
111 }
112
GetEntry(const ConstString & type)113 FormatCache::Entry &FormatCache::GetEntry(const ConstString &type) {
114 auto i = m_map.find(type), e = m_map.end();
115 if (i != e)
116 return i->second;
117 m_map[type] = FormatCache::Entry();
118 return m_map[type];
119 }
120
GetFormat(const ConstString & type,lldb::TypeFormatImplSP & format_sp)121 bool FormatCache::GetFormat(const ConstString &type,
122 lldb::TypeFormatImplSP &format_sp) {
123 std::lock_guard<std::recursive_mutex> guard(m_mutex);
124 auto entry = GetEntry(type);
125 if (entry.IsFormatCached()) {
126 #ifdef LLDB_CONFIGURATION_DEBUG
127 m_cache_hits++;
128 #endif
129 format_sp = entry.GetFormat();
130 return true;
131 }
132 #ifdef LLDB_CONFIGURATION_DEBUG
133 m_cache_misses++;
134 #endif
135 format_sp.reset();
136 return false;
137 }
138
GetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)139 bool FormatCache::GetSummary(const ConstString &type,
140 lldb::TypeSummaryImplSP &summary_sp) {
141 std::lock_guard<std::recursive_mutex> guard(m_mutex);
142 auto entry = GetEntry(type);
143 if (entry.IsSummaryCached()) {
144 #ifdef LLDB_CONFIGURATION_DEBUG
145 m_cache_hits++;
146 #endif
147 summary_sp = entry.GetSummary();
148 return true;
149 }
150 #ifdef LLDB_CONFIGURATION_DEBUG
151 m_cache_misses++;
152 #endif
153 summary_sp.reset();
154 return false;
155 }
156
GetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)157 bool FormatCache::GetSynthetic(const ConstString &type,
158 lldb::SyntheticChildrenSP &synthetic_sp) {
159 std::lock_guard<std::recursive_mutex> guard(m_mutex);
160 auto entry = GetEntry(type);
161 if (entry.IsSyntheticCached()) {
162 #ifdef LLDB_CONFIGURATION_DEBUG
163 m_cache_hits++;
164 #endif
165 synthetic_sp = entry.GetSynthetic();
166 return true;
167 }
168 #ifdef LLDB_CONFIGURATION_DEBUG
169 m_cache_misses++;
170 #endif
171 synthetic_sp.reset();
172 return false;
173 }
174
GetValidator(const ConstString & type,lldb::TypeValidatorImplSP & validator_sp)175 bool FormatCache::GetValidator(const ConstString &type,
176 lldb::TypeValidatorImplSP &validator_sp) {
177 std::lock_guard<std::recursive_mutex> guard(m_mutex);
178 auto entry = GetEntry(type);
179 if (entry.IsValidatorCached()) {
180 #ifdef LLDB_CONFIGURATION_DEBUG
181 m_cache_hits++;
182 #endif
183 validator_sp = entry.GetValidator();
184 return true;
185 }
186 #ifdef LLDB_CONFIGURATION_DEBUG
187 m_cache_misses++;
188 #endif
189 validator_sp.reset();
190 return false;
191 }
192
SetFormat(const ConstString & type,lldb::TypeFormatImplSP & format_sp)193 void FormatCache::SetFormat(const ConstString &type,
194 lldb::TypeFormatImplSP &format_sp) {
195 std::lock_guard<std::recursive_mutex> guard(m_mutex);
196 GetEntry(type).SetFormat(format_sp);
197 }
198
SetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)199 void FormatCache::SetSummary(const ConstString &type,
200 lldb::TypeSummaryImplSP &summary_sp) {
201 std::lock_guard<std::recursive_mutex> guard(m_mutex);
202 GetEntry(type).SetSummary(summary_sp);
203 }
204
SetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)205 void FormatCache::SetSynthetic(const ConstString &type,
206 lldb::SyntheticChildrenSP &synthetic_sp) {
207 std::lock_guard<std::recursive_mutex> guard(m_mutex);
208 GetEntry(type).SetSynthetic(synthetic_sp);
209 }
210
SetValidator(const ConstString & type,lldb::TypeValidatorImplSP & validator_sp)211 void FormatCache::SetValidator(const ConstString &type,
212 lldb::TypeValidatorImplSP &validator_sp) {
213 std::lock_guard<std::recursive_mutex> guard(m_mutex);
214 GetEntry(type).SetValidator(validator_sp);
215 }
216
Clear()217 void FormatCache::Clear() {
218 std::lock_guard<std::recursive_mutex> guard(m_mutex);
219 m_map.clear();
220 }
221