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