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