1 //===-- FormatCache.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 12 // C++ Includes 13 14 // Other libraries and framework includes 15 16 // Project includes 17 #include "lldb/DataFormatters/FormatCache.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 FormatCache::Entry::Entry () : 23 m_format_cached(false), 24 m_summary_cached(false), 25 m_synthetic_cached(false), 26 m_validator_cached(false), 27 m_format_sp(), 28 m_summary_sp(), 29 m_synthetic_sp(), 30 m_validator_sp() 31 {} 32 33 FormatCache::Entry::Entry (lldb::TypeFormatImplSP format_sp) : 34 m_summary_cached(false), 35 m_synthetic_cached(false), 36 m_validator_cached(false), 37 m_summary_sp(), 38 m_synthetic_sp(), 39 m_validator_sp() 40 { 41 SetFormat (format_sp); 42 } 43 44 FormatCache::Entry::Entry (lldb::TypeSummaryImplSP summary_sp) : 45 m_format_cached(false), 46 m_synthetic_cached(false), 47 m_validator_cached(false), 48 m_format_sp(), 49 m_synthetic_sp(), 50 m_validator_sp() 51 { 52 SetSummary (summary_sp); 53 } 54 55 FormatCache::Entry::Entry (lldb::SyntheticChildrenSP synthetic_sp) : 56 m_format_cached(false), 57 m_summary_cached(false), 58 m_validator_cached(false), 59 m_format_sp(), 60 m_summary_sp(), 61 m_validator_sp() 62 { 63 SetSynthetic (synthetic_sp); 64 } 65 66 FormatCache::Entry::Entry (lldb::TypeValidatorImplSP validator_sp) : 67 m_format_cached(false), 68 m_summary_cached(false), 69 m_synthetic_cached(false), 70 m_format_sp(), 71 m_summary_sp(), 72 m_synthetic_sp() 73 { 74 SetValidator (validator_sp); 75 } 76 77 FormatCache::Entry::Entry (lldb::TypeFormatImplSP format_sp, lldb::TypeSummaryImplSP summary_sp, lldb::SyntheticChildrenSP synthetic_sp, lldb::TypeValidatorImplSP validator_sp) 78 { 79 SetFormat (format_sp); 80 SetSummary (summary_sp); 81 SetSynthetic (synthetic_sp); 82 SetValidator (validator_sp); 83 } 84 85 bool 86 FormatCache::Entry::IsFormatCached () 87 { 88 return m_format_cached; 89 } 90 91 bool 92 FormatCache::Entry::IsSummaryCached () 93 { 94 return m_summary_cached; 95 } 96 97 bool 98 FormatCache::Entry::IsSyntheticCached () 99 { 100 return m_synthetic_cached; 101 } 102 103 bool 104 FormatCache::Entry::IsValidatorCached () 105 { 106 return m_validator_cached; 107 } 108 109 lldb::TypeFormatImplSP 110 FormatCache::Entry::GetFormat () 111 { 112 return m_format_sp; 113 } 114 115 lldb::TypeSummaryImplSP 116 FormatCache::Entry::GetSummary () 117 { 118 return m_summary_sp; 119 } 120 121 lldb::SyntheticChildrenSP 122 FormatCache::Entry::GetSynthetic () 123 { 124 return m_synthetic_sp; 125 } 126 127 lldb::TypeValidatorImplSP 128 FormatCache::Entry::GetValidator () 129 { 130 return m_validator_sp; 131 } 132 133 void 134 FormatCache::Entry::SetFormat (lldb::TypeFormatImplSP format_sp) 135 { 136 m_format_cached = true; 137 m_format_sp = format_sp; 138 } 139 140 void 141 FormatCache::Entry::SetSummary (lldb::TypeSummaryImplSP summary_sp) 142 { 143 m_summary_cached = true; 144 m_summary_sp = summary_sp; 145 } 146 147 void 148 FormatCache::Entry::SetSynthetic (lldb::SyntheticChildrenSP synthetic_sp) 149 { 150 m_synthetic_cached = true; 151 m_synthetic_sp = synthetic_sp; 152 } 153 154 void 155 FormatCache::Entry::SetValidator (lldb::TypeValidatorImplSP validator_sp) 156 { 157 m_validator_cached = true; 158 m_validator_sp = validator_sp; 159 } 160 161 FormatCache::FormatCache() 162 : m_map(), 163 m_mutex() 164 #ifdef LLDB_CONFIGURATION_DEBUG 165 , 166 m_cache_hits(0), 167 m_cache_misses(0) 168 #endif 169 { 170 } 171 172 FormatCache::Entry& 173 FormatCache::GetEntry (const ConstString& type) 174 { 175 auto i = m_map.find(type), 176 e = m_map.end(); 177 if (i != e) 178 return i->second; 179 m_map[type] = FormatCache::Entry(); 180 return m_map[type]; 181 } 182 183 bool 184 FormatCache::GetFormat (const ConstString& type,lldb::TypeFormatImplSP& format_sp) 185 { 186 std::lock_guard<std::recursive_mutex> guard(m_mutex); 187 auto entry = GetEntry(type); 188 if (entry.IsFormatCached()) 189 { 190 #ifdef LLDB_CONFIGURATION_DEBUG 191 m_cache_hits++; 192 #endif 193 format_sp = entry.GetFormat(); 194 return true; 195 } 196 #ifdef LLDB_CONFIGURATION_DEBUG 197 m_cache_misses++; 198 #endif 199 format_sp.reset(); 200 return false; 201 } 202 203 bool 204 FormatCache::GetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 205 { 206 std::lock_guard<std::recursive_mutex> guard(m_mutex); 207 auto entry = GetEntry(type); 208 if (entry.IsSummaryCached()) 209 { 210 #ifdef LLDB_CONFIGURATION_DEBUG 211 m_cache_hits++; 212 #endif 213 summary_sp = entry.GetSummary(); 214 return true; 215 } 216 #ifdef LLDB_CONFIGURATION_DEBUG 217 m_cache_misses++; 218 #endif 219 summary_sp.reset(); 220 return false; 221 } 222 223 bool 224 FormatCache::GetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 225 { 226 std::lock_guard<std::recursive_mutex> guard(m_mutex); 227 auto entry = GetEntry(type); 228 if (entry.IsSyntheticCached()) 229 { 230 #ifdef LLDB_CONFIGURATION_DEBUG 231 m_cache_hits++; 232 #endif 233 synthetic_sp = entry.GetSynthetic(); 234 return true; 235 } 236 #ifdef LLDB_CONFIGURATION_DEBUG 237 m_cache_misses++; 238 #endif 239 synthetic_sp.reset(); 240 return false; 241 } 242 243 bool 244 FormatCache::GetValidator (const ConstString& type,lldb::TypeValidatorImplSP& validator_sp) 245 { 246 std::lock_guard<std::recursive_mutex> guard(m_mutex); 247 auto entry = GetEntry(type); 248 if (entry.IsValidatorCached()) 249 { 250 #ifdef LLDB_CONFIGURATION_DEBUG 251 m_cache_hits++; 252 #endif 253 validator_sp = entry.GetValidator(); 254 return true; 255 } 256 #ifdef LLDB_CONFIGURATION_DEBUG 257 m_cache_misses++; 258 #endif 259 validator_sp.reset(); 260 return false; 261 } 262 263 void 264 FormatCache::SetFormat (const ConstString& type,lldb::TypeFormatImplSP& format_sp) 265 { 266 std::lock_guard<std::recursive_mutex> guard(m_mutex); 267 GetEntry(type).SetFormat(format_sp); 268 } 269 270 void 271 FormatCache::SetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 272 { 273 std::lock_guard<std::recursive_mutex> guard(m_mutex); 274 GetEntry(type).SetSummary(summary_sp); 275 } 276 277 void 278 FormatCache::SetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 279 { 280 std::lock_guard<std::recursive_mutex> guard(m_mutex); 281 GetEntry(type).SetSynthetic(synthetic_sp); 282 } 283 284 void 285 FormatCache::SetValidator (const ConstString& type,lldb::TypeValidatorImplSP& validator_sp) 286 { 287 std::lock_guard<std::recursive_mutex> guard(m_mutex); 288 GetEntry(type).SetValidator(validator_sp); 289 } 290 291 void 292 FormatCache::Clear () 293 { 294 std::lock_guard<std::recursive_mutex> guard(m_mutex); 295 m_map.clear(); 296 } 297 298