1cfca06d7SDimitry Andric //===-- FormatCache.cpp ---------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste
10f034231aSEd Maste
11f034231aSEd Maste
12f034231aSEd Maste #include "lldb/DataFormatters/FormatCache.h"
13f034231aSEd Maste
14f034231aSEd Maste using namespace lldb;
15f034231aSEd Maste using namespace lldb_private;
16f034231aSEd Maste
Entry()1714f1b3e8SDimitry Andric FormatCache::Entry::Entry()
1814f1b3e8SDimitry Andric : m_format_cached(false), m_summary_cached(false),
19706b4fc4SDimitry Andric m_synthetic_cached(false) {}
20f034231aSEd Maste
IsFormatCached()2114f1b3e8SDimitry Andric bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
22f21a844fSEd Maste
IsSummaryCached()2314f1b3e8SDimitry Andric bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
24f034231aSEd Maste
IsSyntheticCached()2514f1b3e8SDimitry Andric bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
26f034231aSEd Maste
Get(lldb::TypeFormatImplSP & retval)27706b4fc4SDimitry Andric void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
28706b4fc4SDimitry Andric retval = m_format_sp;
29f034231aSEd Maste }
30f034231aSEd Maste
Get(lldb::TypeSummaryImplSP & retval)31706b4fc4SDimitry Andric void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
32706b4fc4SDimitry Andric retval = m_summary_sp;
33f034231aSEd Maste }
34f034231aSEd Maste
Get(lldb::SyntheticChildrenSP & retval)35706b4fc4SDimitry Andric void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
36706b4fc4SDimitry Andric retval = m_synthetic_sp;
37205afe67SEd Maste }
38205afe67SEd Maste
Set(lldb::TypeFormatImplSP format_sp)39706b4fc4SDimitry Andric void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
40f21a844fSEd Maste m_format_cached = true;
41f21a844fSEd Maste m_format_sp = format_sp;
42f21a844fSEd Maste }
43f21a844fSEd Maste
Set(lldb::TypeSummaryImplSP summary_sp)44706b4fc4SDimitry Andric void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
45f034231aSEd Maste m_summary_cached = true;
46f034231aSEd Maste m_summary_sp = summary_sp;
47f034231aSEd Maste }
48f034231aSEd Maste
Set(lldb::SyntheticChildrenSP synthetic_sp)49706b4fc4SDimitry Andric void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
50f034231aSEd Maste m_synthetic_cached = true;
51f034231aSEd Maste m_synthetic_sp = synthetic_sp;
52f034231aSEd Maste }
53f034231aSEd Maste
54cfca06d7SDimitry Andric namespace lldb_private {
55cfca06d7SDimitry Andric
IsCached()56706b4fc4SDimitry Andric template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
57706b4fc4SDimitry Andric return IsFormatCached();
58706b4fc4SDimitry Andric }
IsCached()59706b4fc4SDimitry Andric template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
60706b4fc4SDimitry Andric return IsSummaryCached();
61706b4fc4SDimitry Andric }
IsCached()62706b4fc4SDimitry Andric template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
63706b4fc4SDimitry Andric return IsSyntheticCached();
64706b4fc4SDimitry Andric }
65706b4fc4SDimitry Andric
66cfca06d7SDimitry Andric } // namespace lldb_private
67cfca06d7SDimitry Andric
68706b4fc4SDimitry Andric template <typename ImplSP>
Get(ConstString type,ImplSP & format_impl_sp)69706b4fc4SDimitry Andric bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
70f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
71ac9a064cSDimitry Andric auto entry = m_entries[type];
72706b4fc4SDimitry Andric if (entry.IsCached<ImplSP>()) {
73f21a844fSEd Maste m_cache_hits++;
74706b4fc4SDimitry Andric entry.Get(format_impl_sp);
75f21a844fSEd Maste return true;
76f21a844fSEd Maste }
77f21a844fSEd Maste m_cache_misses++;
78706b4fc4SDimitry Andric format_impl_sp.reset();
79f21a844fSEd Maste return false;
80f21a844fSEd Maste }
81f21a844fSEd Maste
82706b4fc4SDimitry Andric /// Explicit instantiations for the three types.
83706b4fc4SDimitry Andric /// \{
84706b4fc4SDimitry Andric template bool
85706b4fc4SDimitry Andric FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
86706b4fc4SDimitry Andric template bool
87706b4fc4SDimitry Andric FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
88706b4fc4SDimitry Andric lldb::TypeSummaryImplSP &);
89706b4fc4SDimitry Andric template bool
90706b4fc4SDimitry Andric FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
91706b4fc4SDimitry Andric lldb::SyntheticChildrenSP &);
92706b4fc4SDimitry Andric /// \}
93706b4fc4SDimitry Andric
Set(ConstString type,lldb::TypeFormatImplSP & format_sp)94706b4fc4SDimitry Andric void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {
95f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
96ac9a064cSDimitry Andric m_entries[type].Set(format_sp);
97f034231aSEd Maste }
98f034231aSEd Maste
Set(ConstString type,lldb::TypeSummaryImplSP & summary_sp)99706b4fc4SDimitry Andric void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {
100706b4fc4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
101ac9a064cSDimitry Andric m_entries[type].Set(summary_sp);
102706b4fc4SDimitry Andric }
103706b4fc4SDimitry Andric
Set(ConstString type,lldb::SyntheticChildrenSP & synthetic_sp)104706b4fc4SDimitry Andric void FormatCache::Set(ConstString type,
10514f1b3e8SDimitry Andric lldb::SyntheticChildrenSP &synthetic_sp) {
106f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
107ac9a064cSDimitry Andric m_entries[type].Set(synthetic_sp);
108205afe67SEd Maste }
109205afe67SEd Maste
Clear()11014f1b3e8SDimitry Andric void FormatCache::Clear() {
111f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
112ac9a064cSDimitry Andric m_entries.clear();
113f034231aSEd Maste }
114