blob: bb5f379d3c736d1dcf3e8c3ac42938c3341781b9 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- FormatCache.cpp ------------------------------------------*- C++
2//-*-===//
Enrico Granata5548cb52013-01-28 23:47:25 +00003//
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
Enrico Granata5548cb52013-01-28 23:47:25 +000011
Enrico Granata5548cb52013-01-28 23:47:25 +000012
Enrico Granata5548cb52013-01-28 23:47:25 +000013
Enrico Granata5548cb52013-01-28 23:47:25 +000014#include "lldb/DataFormatters/FormatCache.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019FormatCache::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() {}
Enrico Granata5548cb52013-01-28 23:47:25 +000023
Kate Stoneb9c1b512016-09-06 20:57:50 +000024FormatCache::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);
Enrico Granata52b4b6c2013-10-17 22:27:19 +000029}
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031FormatCache::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);
Enrico Granata5548cb52013-01-28 23:47:25 +000036}
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038FormatCache::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);
Enrico Granata5548cb52013-01-28 23:47:25 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045FormatCache::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);
Enrico Granatac5827132014-09-05 20:45:07 +000050}
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052FormatCache::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);
Enrico Granata5548cb52013-01-28 23:47:25 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
63
64bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
65
66bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
67
68bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
69
70lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
71
72lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
73 return m_summary_sp;
Enrico Granata52b4b6c2013-10-17 22:27:19 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
77 return m_synthetic_sp;
Enrico Granata5548cb52013-01-28 23:47:25 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
81 return m_validator_sp;
Enrico Granata5548cb52013-01-28 23:47:25 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
85 m_format_cached = true;
86 m_format_sp = format_sp;
Enrico Granatac5827132014-09-05 20:45:07 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
90 m_summary_cached = true;
91 m_summary_sp = summary_sp;
Enrico Granata52b4b6c2013-10-17 22:27:19 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
95 m_synthetic_cached = true;
96 m_synthetic_sp = synthetic_sp;
Enrico Granata5548cb52013-01-28 23:47:25 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
100 m_validator_cached = true;
101 m_validator_sp = validator_sp;
Enrico Granatac5827132014-09-05 20:45:07 +0000102}
103
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000104FormatCache::FormatCache()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 : m_map(), m_mutex()
Enrico Granata5548cb52013-01-28 23:47:25 +0000106#ifdef LLDB_CONFIGURATION_DEBUG
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000107 ,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 m_cache_hits(0), m_cache_misses(0)
Enrico Granata5548cb52013-01-28 23:47:25 +0000109#endif
110{
111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113FormatCache::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];
Enrico Granata5548cb52013-01-28 23:47:25 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121bool 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()) {
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000126#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 m_cache_hits++;
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000128#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 format_sp = entry.GetFormat();
130 return true;
131 }
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000132#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 m_cache_misses++;
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000134#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 format_sp.reset();
136 return false;
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000137}
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139bool 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()) {
Enrico Granata5548cb52013-01-28 23:47:25 +0000144#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 m_cache_hits++;
Enrico Granata5548cb52013-01-28 23:47:25 +0000146#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 summary_sp = entry.GetSummary();
148 return true;
149 }
Enrico Granata5548cb52013-01-28 23:47:25 +0000150#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 m_cache_misses++;
Enrico Granata5548cb52013-01-28 23:47:25 +0000152#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 summary_sp.reset();
154 return false;
Enrico Granata5548cb52013-01-28 23:47:25 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157bool 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()) {
Enrico Granata5548cb52013-01-28 23:47:25 +0000162#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 m_cache_hits++;
Enrico Granata5548cb52013-01-28 23:47:25 +0000164#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 synthetic_sp = entry.GetSynthetic();
166 return true;
167 }
Enrico Granata5548cb52013-01-28 23:47:25 +0000168#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 m_cache_misses++;
Enrico Granata5548cb52013-01-28 23:47:25 +0000170#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 synthetic_sp.reset();
172 return false;
Enrico Granata5548cb52013-01-28 23:47:25 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175bool 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()) {
Enrico Granatac5827132014-09-05 20:45:07 +0000180#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 m_cache_hits++;
Enrico Granatac5827132014-09-05 20:45:07 +0000182#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 validator_sp = entry.GetValidator();
184 return true;
185 }
Enrico Granatac5827132014-09-05 20:45:07 +0000186#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 m_cache_misses++;
Enrico Granatac5827132014-09-05 20:45:07 +0000188#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 validator_sp.reset();
190 return false;
Enrico Granatac5827132014-09-05 20:45:07 +0000191}
192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193void 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);
Enrico Granata52b4b6c2013-10-17 22:27:19 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199void 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);
Enrico Granata5548cb52013-01-28 23:47:25 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205void 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);
Enrico Granata5548cb52013-01-28 23:47:25 +0000209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211void 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);
Enrico Granatac5827132014-09-05 20:45:07 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217void FormatCache::Clear() {
218 std::lock_guard<std::recursive_mutex> guard(m_mutex);
219 m_map.clear();
Enrico Granata5548cb52013-01-28 23:47:25 +0000220}