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 template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() { 73 return IsFormatCached(); 74 } 75 template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () { 76 return IsSummaryCached(); 77 } 78 template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() { 79 return IsSyntheticCached(); 80 } 81 82 template <typename ImplSP> 83 bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) { 84 std::lock_guard<std::recursive_mutex> guard(m_mutex); 85 auto entry = GetEntry(type); 86 if (entry.IsCached<ImplSP>()) { 87 #ifdef LLDB_CONFIGURATION_DEBUG 88 m_cache_hits++; 89 #endif 90 entry.Get(format_impl_sp); 91 return true; 92 } 93 #ifdef LLDB_CONFIGURATION_DEBUG 94 m_cache_misses++; 95 #endif 96 format_impl_sp.reset(); 97 return false; 98 } 99 100 /// Explicit instantiations for the three types. 101 /// \{ 102 template bool 103 FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &); 104 template bool 105 FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString, 106 lldb::TypeSummaryImplSP &); 107 template bool 108 FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString, 109 lldb::SyntheticChildrenSP &); 110 /// \} 111 112 void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) { 113 std::lock_guard<std::recursive_mutex> guard(m_mutex); 114 GetEntry(type).Set(format_sp); 115 } 116 117 void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) { 118 std::lock_guard<std::recursive_mutex> guard(m_mutex); 119 GetEntry(type).Set(summary_sp); 120 } 121 122 void FormatCache::Set(ConstString type, 123 lldb::SyntheticChildrenSP &synthetic_sp) { 124 std::lock_guard<std::recursive_mutex> guard(m_mutex); 125 GetEntry(type).Set(synthetic_sp); 126 } 127 128 void FormatCache::Clear() { 129 std::lock_guard<std::recursive_mutex> guard(m_mutex); 130 m_map.clear(); 131 } 132