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