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