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 (Mutex::eMutexTypeRecursive) 164 #ifdef LLDB_CONFIGURATION_DEBUG 165 ,m_cache_hits(0),m_cache_misses(0) 166 #endif 167 { 168 } 169 170 FormatCache::Entry& 171 FormatCache::GetEntry (const ConstString& type) 172 { 173 auto i = m_map.find(type), 174 e = m_map.end(); 175 if (i != e) 176 return i->second; 177 m_map[type] = FormatCache::Entry(); 178 return m_map[type]; 179 } 180 181 bool 182 FormatCache::GetFormat (const ConstString& type,lldb::TypeFormatImplSP& format_sp) 183 { 184 Mutex::Locker lock(m_mutex); 185 auto entry = GetEntry(type); 186 if (entry.IsFormatCached()) 187 { 188 #ifdef LLDB_CONFIGURATION_DEBUG 189 m_cache_hits++; 190 #endif 191 format_sp = entry.GetFormat(); 192 return true; 193 } 194 #ifdef LLDB_CONFIGURATION_DEBUG 195 m_cache_misses++; 196 #endif 197 format_sp.reset(); 198 return false; 199 } 200 201 bool 202 FormatCache::GetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 203 { 204 Mutex::Locker lock(m_mutex); 205 auto entry = GetEntry(type); 206 if (entry.IsSummaryCached()) 207 { 208 #ifdef LLDB_CONFIGURATION_DEBUG 209 m_cache_hits++; 210 #endif 211 summary_sp = entry.GetSummary(); 212 return true; 213 } 214 #ifdef LLDB_CONFIGURATION_DEBUG 215 m_cache_misses++; 216 #endif 217 summary_sp.reset(); 218 return false; 219 } 220 221 bool 222 FormatCache::GetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 223 { 224 Mutex::Locker lock(m_mutex); 225 auto entry = GetEntry(type); 226 if (entry.IsSyntheticCached()) 227 { 228 #ifdef LLDB_CONFIGURATION_DEBUG 229 m_cache_hits++; 230 #endif 231 synthetic_sp = entry.GetSynthetic(); 232 return true; 233 } 234 #ifdef LLDB_CONFIGURATION_DEBUG 235 m_cache_misses++; 236 #endif 237 synthetic_sp.reset(); 238 return false; 239 } 240 241 bool 242 FormatCache::GetValidator (const ConstString& type,lldb::TypeValidatorImplSP& validator_sp) 243 { 244 Mutex::Locker lock(m_mutex); 245 auto entry = GetEntry(type); 246 if (entry.IsValidatorCached()) 247 { 248 #ifdef LLDB_CONFIGURATION_DEBUG 249 m_cache_hits++; 250 #endif 251 validator_sp = entry.GetValidator(); 252 return true; 253 } 254 #ifdef LLDB_CONFIGURATION_DEBUG 255 m_cache_misses++; 256 #endif 257 validator_sp.reset(); 258 return false; 259 } 260 261 void 262 FormatCache::SetFormat (const ConstString& type,lldb::TypeFormatImplSP& format_sp) 263 { 264 Mutex::Locker lock(m_mutex); 265 GetEntry(type).SetFormat(format_sp); 266 } 267 268 void 269 FormatCache::SetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 270 { 271 Mutex::Locker lock(m_mutex); 272 GetEntry(type).SetSummary(summary_sp); 273 } 274 275 void 276 FormatCache::SetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 277 { 278 Mutex::Locker lock(m_mutex); 279 GetEntry(type).SetSynthetic(synthetic_sp); 280 } 281 282 void 283 FormatCache::SetValidator (const ConstString& type,lldb::TypeValidatorImplSP& validator_sp) 284 { 285 Mutex::Locker lock(m_mutex); 286 GetEntry(type).SetValidator(validator_sp); 287 } 288 289 void 290 FormatCache::Clear () 291 { 292 Mutex::Locker lock(m_mutex); 293 m_map.clear(); 294 } 295 296