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_summary_cached(false), 24 m_synthetic_cached(false), 25 m_summary_sp(nullptr), 26 m_synthetic_sp(nullptr) 27 {} 28 29 FormatCache::Entry::Entry (lldb::TypeSummaryImplSP summary_sp) : FormatCache::Entry() 30 { 31 SetSummary (summary_sp); 32 } 33 34 FormatCache::Entry::Entry (lldb::SyntheticChildrenSP synthetic_sp) : FormatCache::Entry() 35 { 36 SetSynthetic (synthetic_sp); 37 } 38 39 FormatCache::Entry::Entry (lldb::TypeSummaryImplSP summary_sp,lldb::SyntheticChildrenSP synthetic_sp) : FormatCache::Entry() 40 { 41 SetSummary (summary_sp); 42 SetSynthetic (synthetic_sp); 43 } 44 45 bool 46 FormatCache::Entry::IsSummaryCached () 47 { 48 return m_summary_cached; 49 } 50 51 bool 52 FormatCache::Entry::IsSyntheticCached () 53 { 54 return m_synthetic_cached; 55 } 56 57 lldb::TypeSummaryImplSP 58 FormatCache::Entry::GetSummary () 59 { 60 return m_summary_sp; 61 } 62 63 lldb::SyntheticChildrenSP 64 FormatCache::Entry::GetSynthetic () 65 { 66 return m_synthetic_sp; 67 } 68 69 void 70 FormatCache::Entry::SetSummary (lldb::TypeSummaryImplSP summary_sp) 71 { 72 m_summary_cached = true; 73 m_summary_sp = summary_sp; 74 } 75 76 void 77 FormatCache::Entry::SetSynthetic (lldb::SyntheticChildrenSP synthetic_sp) 78 { 79 m_synthetic_cached = true; 80 m_synthetic_sp = synthetic_sp; 81 } 82 83 FormatCache::FormatCache () : 84 m_map(), 85 m_mutex (Mutex::eMutexTypeRecursive) 86 #ifdef LLDB_CONFIGURATION_DEBUG 87 ,m_cache_hits(0),m_cache_misses(0) 88 #endif 89 { 90 } 91 92 FormatCache::Entry& 93 FormatCache::GetEntry (const ConstString& type) 94 { 95 auto i = m_map.find(type), 96 e = m_map.end(); 97 if (i != e) 98 return i->second; 99 m_map[type] = FormatCache::Entry(); 100 return m_map[type]; 101 } 102 103 bool 104 FormatCache::GetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 105 { 106 Mutex::Locker lock(m_mutex); 107 auto entry = GetEntry(type); 108 if (entry.IsSummaryCached()) 109 { 110 #ifdef LLDB_CONFIGURATION_DEBUG 111 m_cache_hits++; 112 #endif 113 summary_sp = entry.GetSummary(); 114 return true; 115 } 116 #ifdef LLDB_CONFIGURATION_DEBUG 117 m_cache_misses++; 118 #endif 119 summary_sp.reset(); 120 return false; 121 } 122 123 bool 124 FormatCache::GetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 125 { 126 Mutex::Locker lock(m_mutex); 127 auto entry = GetEntry(type); 128 if (entry.IsSyntheticCached()) 129 { 130 #ifdef LLDB_CONFIGURATION_DEBUG 131 m_cache_hits++; 132 #endif 133 synthetic_sp = entry.GetSynthetic(); 134 return true; 135 } 136 #ifdef LLDB_CONFIGURATION_DEBUG 137 m_cache_misses++; 138 #endif 139 synthetic_sp.reset(); 140 return false; 141 } 142 143 void 144 FormatCache::SetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp) 145 { 146 Mutex::Locker lock(m_mutex); 147 GetEntry(type).SetSummary(summary_sp); 148 } 149 150 void 151 FormatCache::SetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp) 152 { 153 Mutex::Locker lock(m_mutex); 154 GetEntry(type).SetSynthetic(synthetic_sp); 155 } 156 157 void 158 FormatCache::Clear () 159 { 160 Mutex::Locker lock(m_mutex); 161 m_map.clear(); 162 } 163 164