[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / lldb / source / DataFormatters / FormatCache.cpp
blob231e7ed0c0a07f884d3c68f8e6f7ba6afa7880a6
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 //===----------------------------------------------------------------------===//
13 #include "lldb/DataFormatters/FormatCache.h"
15 using namespace lldb;
16 using namespace lldb_private;
18 FormatCache::Entry::Entry()
19 : m_format_cached(false), m_summary_cached(false),
20 m_synthetic_cached(false) {}
22 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
24 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
26 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
28 void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
29 retval = m_format_sp;
32 void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
33 retval = m_summary_sp;
36 void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
37 retval = m_synthetic_sp;
40 void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
41 m_format_cached = true;
42 m_format_sp = format_sp;
45 void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
46 m_summary_cached = true;
47 m_summary_sp = summary_sp;
50 void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
51 m_synthetic_cached = true;
52 m_synthetic_sp = synthetic_sp;
55 FormatCache::FormatCache()
56 : m_map(), m_mutex()
57 #ifdef LLDB_CONFIGURATION_DEBUG
59 m_cache_hits(0), m_cache_misses(0)
60 #endif
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];
72 template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
73 return IsFormatCached();
75 template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
76 return IsSummaryCached();
78 template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
79 return IsSyntheticCached();
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;
93 #ifdef LLDB_CONFIGURATION_DEBUG
94 m_cache_misses++;
95 #endif
96 format_impl_sp.reset();
97 return false;
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 /// \}
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);
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);
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);
128 void FormatCache::Clear() {
129 std::lock_guard<std::recursive_mutex> guard(m_mutex);
130 m_map.clear();