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) {} 21 22 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; } 23 24 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; } 25 26 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; } 27 28 void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) { 29 retval = m_format_sp; 30 } 31 32 void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) { 33 retval = m_summary_sp; 34 } 35 36 void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) { 37 retval = m_synthetic_sp; 38 } 39 40 void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) { 41 m_format_cached = true; 42 m_format_sp = format_sp; 43 } 44 45 void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) { 46 m_summary_cached = true; 47 m_summary_sp = summary_sp; 48 } 49 50 void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) { 51 m_synthetic_cached = true; 52 m_synthetic_sp = synthetic_sp; 53 } 54 55 FormatCache::FormatCache() 56 : m_map(), m_mutex() 57 #ifdef LLDB_CONFIGURATION_DEBUG 58 , 59 m_cache_hits(0), m_cache_misses(0) 60 #endif 61 { 62 } 63 64 FormatCache::Entry &FormatCache::GetEntry(ConstString type) { 65 auto i = m_map.find(type), e = m_map.end(); 66 if (i != e) 67 return i->second; 68 m_map[type] = FormatCache::Entry(); 69 return m_map[type]; 70 } 71 72 namespace lldb_private { 73 74 template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() { 75 return IsFormatCached(); 76 } 77 template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () { 78 return IsSummaryCached(); 79 } 80 template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() { 81 return IsSyntheticCached(); 82 } 83 84 } // namespace lldb_private 85 86 template <typename ImplSP> 87 bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) { 88 std::lock_guard<std::recursive_mutex> guard(m_mutex); 89 auto entry = GetEntry(type); 90 if (entry.IsCached<ImplSP>()) { 91 #ifdef LLDB_CONFIGURATION_DEBUG 92 m_cache_hits++; 93 #endif 94 entry.Get(format_impl_sp); 95 return true; 96 } 97 #ifdef LLDB_CONFIGURATION_DEBUG 98 m_cache_misses++; 99 #endif 100 format_impl_sp.reset(); 101 return false; 102 } 103 104 /// Explicit instantiations for the three types. 105 /// \{ 106 template bool 107 FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &); 108 template bool 109 FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString, 110 lldb::TypeSummaryImplSP &); 111 template bool 112 FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString, 113 lldb::SyntheticChildrenSP &); 114 /// \} 115 116 void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) { 117 std::lock_guard<std::recursive_mutex> guard(m_mutex); 118 GetEntry(type).Set(format_sp); 119 } 120 121 void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) { 122 std::lock_guard<std::recursive_mutex> guard(m_mutex); 123 GetEntry(type).Set(summary_sp); 124 } 125 126 void FormatCache::Set(ConstString type, 127 lldb::SyntheticChildrenSP &synthetic_sp) { 128 std::lock_guard<std::recursive_mutex> guard(m_mutex); 129 GetEntry(type).Set(synthetic_sp); 130 } 131 132 void FormatCache::Clear() { 133 std::lock_guard<std::recursive_mutex> guard(m_mutex); 134 m_map.clear(); 135 } 136