1435933ddSDimitry Andric //===-- FormatCache.cpp ------------------------------------------*- C++
2435933ddSDimitry Andric //-*-===//
3ac7ddfbfSEd Maste //
4ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
5ac7ddfbfSEd Maste //
6ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
7ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
8ac7ddfbfSEd Maste //
9ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
10ac7ddfbfSEd Maste 
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste 
13ac7ddfbfSEd Maste 
14ac7ddfbfSEd Maste #include "lldb/DataFormatters/FormatCache.h"
15ac7ddfbfSEd Maste 
16ac7ddfbfSEd Maste using namespace lldb;
17ac7ddfbfSEd Maste using namespace lldb_private;
18ac7ddfbfSEd Maste 
Entry()19435933ddSDimitry Andric FormatCache::Entry::Entry()
20435933ddSDimitry Andric     : m_format_cached(false), m_summary_cached(false),
21435933ddSDimitry Andric       m_synthetic_cached(false), m_validator_cached(false), m_format_sp(),
22435933ddSDimitry Andric       m_summary_sp(), m_synthetic_sp(), m_validator_sp() {}
23ac7ddfbfSEd Maste 
Entry(lldb::TypeFormatImplSP format_sp)24435933ddSDimitry Andric FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp)
25435933ddSDimitry Andric     : m_summary_cached(false), m_synthetic_cached(false),
26435933ddSDimitry Andric       m_validator_cached(false), m_summary_sp(), m_synthetic_sp(),
27435933ddSDimitry Andric       m_validator_sp() {
2835617911SEd Maste   SetFormat(format_sp);
2935617911SEd Maste }
3035617911SEd Maste 
Entry(lldb::TypeSummaryImplSP summary_sp)31435933ddSDimitry Andric FormatCache::Entry::Entry(lldb::TypeSummaryImplSP summary_sp)
32435933ddSDimitry Andric     : m_format_cached(false), m_synthetic_cached(false),
33435933ddSDimitry Andric       m_validator_cached(false), m_format_sp(), m_synthetic_sp(),
34435933ddSDimitry Andric       m_validator_sp() {
35ac7ddfbfSEd Maste   SetSummary(summary_sp);
36ac7ddfbfSEd Maste }
37ac7ddfbfSEd Maste 
Entry(lldb::SyntheticChildrenSP synthetic_sp)38435933ddSDimitry Andric FormatCache::Entry::Entry(lldb::SyntheticChildrenSP synthetic_sp)
39435933ddSDimitry Andric     : m_format_cached(false), m_summary_cached(false),
40435933ddSDimitry Andric       m_validator_cached(false), m_format_sp(), m_summary_sp(),
41435933ddSDimitry Andric       m_validator_sp() {
42ac7ddfbfSEd Maste   SetSynthetic(synthetic_sp);
43ac7ddfbfSEd Maste }
44ac7ddfbfSEd Maste 
Entry(lldb::TypeValidatorImplSP validator_sp)45435933ddSDimitry Andric FormatCache::Entry::Entry(lldb::TypeValidatorImplSP validator_sp)
46435933ddSDimitry Andric     : m_format_cached(false), m_summary_cached(false),
47435933ddSDimitry Andric       m_synthetic_cached(false), m_format_sp(), m_summary_sp(),
48435933ddSDimitry Andric       m_synthetic_sp() {
497aa51b79SEd Maste   SetValidator(validator_sp);
507aa51b79SEd Maste }
517aa51b79SEd Maste 
Entry(lldb::TypeFormatImplSP format_sp,lldb::TypeSummaryImplSP summary_sp,lldb::SyntheticChildrenSP synthetic_sp,lldb::TypeValidatorImplSP validator_sp)52435933ddSDimitry Andric FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp,
53435933ddSDimitry Andric                           lldb::TypeSummaryImplSP summary_sp,
54435933ddSDimitry Andric                           lldb::SyntheticChildrenSP synthetic_sp,
55435933ddSDimitry Andric                           lldb::TypeValidatorImplSP validator_sp) {
5635617911SEd Maste   SetFormat(format_sp);
57ac7ddfbfSEd Maste   SetSummary(summary_sp);
58ac7ddfbfSEd Maste   SetSynthetic(synthetic_sp);
597aa51b79SEd Maste   SetValidator(validator_sp);
60ac7ddfbfSEd Maste }
61ac7ddfbfSEd Maste 
IsFormatCached()62435933ddSDimitry Andric bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
6335617911SEd Maste 
IsSummaryCached()64435933ddSDimitry Andric bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
65ac7ddfbfSEd Maste 
IsSyntheticCached()66435933ddSDimitry Andric bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
67ac7ddfbfSEd Maste 
IsValidatorCached()68435933ddSDimitry Andric bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
697aa51b79SEd Maste 
GetFormat()70435933ddSDimitry Andric lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
7135617911SEd Maste 
GetSummary()72435933ddSDimitry Andric lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
73ac7ddfbfSEd Maste   return m_summary_sp;
74ac7ddfbfSEd Maste }
75ac7ddfbfSEd Maste 
GetSynthetic()76435933ddSDimitry Andric lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
77ac7ddfbfSEd Maste   return m_synthetic_sp;
78ac7ddfbfSEd Maste }
79ac7ddfbfSEd Maste 
GetValidator()80435933ddSDimitry Andric lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
817aa51b79SEd Maste   return m_validator_sp;
827aa51b79SEd Maste }
837aa51b79SEd Maste 
SetFormat(lldb::TypeFormatImplSP format_sp)84435933ddSDimitry Andric void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
8535617911SEd Maste   m_format_cached = true;
8635617911SEd Maste   m_format_sp = format_sp;
8735617911SEd Maste }
8835617911SEd Maste 
SetSummary(lldb::TypeSummaryImplSP summary_sp)89435933ddSDimitry Andric void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
90ac7ddfbfSEd Maste   m_summary_cached = true;
91ac7ddfbfSEd Maste   m_summary_sp = summary_sp;
92ac7ddfbfSEd Maste }
93ac7ddfbfSEd Maste 
SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp)94435933ddSDimitry Andric void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
95ac7ddfbfSEd Maste   m_synthetic_cached = true;
96ac7ddfbfSEd Maste   m_synthetic_sp = synthetic_sp;
97ac7ddfbfSEd Maste }
98ac7ddfbfSEd Maste 
SetValidator(lldb::TypeValidatorImplSP validator_sp)99435933ddSDimitry Andric void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
1007aa51b79SEd Maste   m_validator_cached = true;
1017aa51b79SEd Maste   m_validator_sp = validator_sp;
1027aa51b79SEd Maste }
1037aa51b79SEd Maste 
FormatCache()1044bb0738eSEd Maste FormatCache::FormatCache()
105435933ddSDimitry Andric     : m_map(), m_mutex()
106ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
1074bb0738eSEd Maste       ,
108435933ddSDimitry Andric       m_cache_hits(0), m_cache_misses(0)
109ac7ddfbfSEd Maste #endif
110ac7ddfbfSEd Maste {
111ac7ddfbfSEd Maste }
112ac7ddfbfSEd Maste 
GetEntry(const ConstString & type)113435933ddSDimitry Andric FormatCache::Entry &FormatCache::GetEntry(const ConstString &type) {
114435933ddSDimitry Andric   auto i = m_map.find(type), e = m_map.end();
115ac7ddfbfSEd Maste   if (i != e)
116ac7ddfbfSEd Maste     return i->second;
117ac7ddfbfSEd Maste   m_map[type] = FormatCache::Entry();
118ac7ddfbfSEd Maste   return m_map[type];
119ac7ddfbfSEd Maste }
120ac7ddfbfSEd Maste 
GetFormat(const ConstString & type,lldb::TypeFormatImplSP & format_sp)121435933ddSDimitry Andric bool FormatCache::GetFormat(const ConstString &type,
122435933ddSDimitry Andric                             lldb::TypeFormatImplSP &format_sp) {
1234bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
12435617911SEd Maste   auto entry = GetEntry(type);
125435933ddSDimitry Andric   if (entry.IsFormatCached()) {
12635617911SEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
12735617911SEd Maste     m_cache_hits++;
12835617911SEd Maste #endif
12935617911SEd Maste     format_sp = entry.GetFormat();
13035617911SEd Maste     return true;
13135617911SEd Maste   }
13235617911SEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
13335617911SEd Maste   m_cache_misses++;
13435617911SEd Maste #endif
13535617911SEd Maste   format_sp.reset();
13635617911SEd Maste   return false;
13735617911SEd Maste }
13835617911SEd Maste 
GetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)139435933ddSDimitry Andric bool FormatCache::GetSummary(const ConstString &type,
140435933ddSDimitry Andric                              lldb::TypeSummaryImplSP &summary_sp) {
1414bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
142ac7ddfbfSEd Maste   auto entry = GetEntry(type);
143435933ddSDimitry Andric   if (entry.IsSummaryCached()) {
144ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
145ac7ddfbfSEd Maste     m_cache_hits++;
146ac7ddfbfSEd Maste #endif
147ac7ddfbfSEd Maste     summary_sp = entry.GetSummary();
148ac7ddfbfSEd Maste     return true;
149ac7ddfbfSEd Maste   }
150ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
151ac7ddfbfSEd Maste   m_cache_misses++;
152ac7ddfbfSEd Maste #endif
153ac7ddfbfSEd Maste   summary_sp.reset();
154ac7ddfbfSEd Maste   return false;
155ac7ddfbfSEd Maste }
156ac7ddfbfSEd Maste 
GetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)157435933ddSDimitry Andric bool FormatCache::GetSynthetic(const ConstString &type,
158435933ddSDimitry Andric                                lldb::SyntheticChildrenSP &synthetic_sp) {
1594bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
160ac7ddfbfSEd Maste   auto entry = GetEntry(type);
161435933ddSDimitry Andric   if (entry.IsSyntheticCached()) {
162ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
163ac7ddfbfSEd Maste     m_cache_hits++;
164ac7ddfbfSEd Maste #endif
165ac7ddfbfSEd Maste     synthetic_sp = entry.GetSynthetic();
166ac7ddfbfSEd Maste     return true;
167ac7ddfbfSEd Maste   }
168ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
169ac7ddfbfSEd Maste   m_cache_misses++;
170ac7ddfbfSEd Maste #endif
171ac7ddfbfSEd Maste   synthetic_sp.reset();
172ac7ddfbfSEd Maste   return false;
173ac7ddfbfSEd Maste }
174ac7ddfbfSEd Maste 
GetValidator(const ConstString & type,lldb::TypeValidatorImplSP & validator_sp)175435933ddSDimitry Andric bool FormatCache::GetValidator(const ConstString &type,
176435933ddSDimitry Andric                                lldb::TypeValidatorImplSP &validator_sp) {
1774bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1787aa51b79SEd Maste   auto entry = GetEntry(type);
179435933ddSDimitry Andric   if (entry.IsValidatorCached()) {
1807aa51b79SEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
1817aa51b79SEd Maste     m_cache_hits++;
1827aa51b79SEd Maste #endif
1837aa51b79SEd Maste     validator_sp = entry.GetValidator();
1847aa51b79SEd Maste     return true;
1857aa51b79SEd Maste   }
1867aa51b79SEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
1877aa51b79SEd Maste   m_cache_misses++;
1887aa51b79SEd Maste #endif
1897aa51b79SEd Maste   validator_sp.reset();
1907aa51b79SEd Maste   return false;
1917aa51b79SEd Maste }
1927aa51b79SEd Maste 
SetFormat(const ConstString & type,lldb::TypeFormatImplSP & format_sp)193435933ddSDimitry Andric void FormatCache::SetFormat(const ConstString &type,
194435933ddSDimitry Andric                             lldb::TypeFormatImplSP &format_sp) {
1954bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
19635617911SEd Maste   GetEntry(type).SetFormat(format_sp);
19735617911SEd Maste }
19835617911SEd Maste 
SetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)199435933ddSDimitry Andric void FormatCache::SetSummary(const ConstString &type,
200435933ddSDimitry Andric                              lldb::TypeSummaryImplSP &summary_sp) {
2014bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
202ac7ddfbfSEd Maste   GetEntry(type).SetSummary(summary_sp);
203ac7ddfbfSEd Maste }
204ac7ddfbfSEd Maste 
SetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)205435933ddSDimitry Andric void FormatCache::SetSynthetic(const ConstString &type,
206435933ddSDimitry Andric                                lldb::SyntheticChildrenSP &synthetic_sp) {
2074bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
208ac7ddfbfSEd Maste   GetEntry(type).SetSynthetic(synthetic_sp);
209ac7ddfbfSEd Maste }
210ac7ddfbfSEd Maste 
SetValidator(const ConstString & type,lldb::TypeValidatorImplSP & validator_sp)211435933ddSDimitry Andric void FormatCache::SetValidator(const ConstString &type,
212435933ddSDimitry Andric                                lldb::TypeValidatorImplSP &validator_sp) {
2134bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2147aa51b79SEd Maste   GetEntry(type).SetValidator(validator_sp);
2157aa51b79SEd Maste }
2167aa51b79SEd Maste 
Clear()217435933ddSDimitry Andric void FormatCache::Clear() {
2184bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
219ac7ddfbfSEd Maste   m_map.clear();
220ac7ddfbfSEd Maste }
221