1 //===-- FormatCache.cpp ------------------------------------------*- C++
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
8 //===----------------------------------------------------------------------===//
13 #include "lldb/DataFormatters/FormatCache.h"
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
) {
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()
57 #ifdef LLDB_CONFIGURATION_DEBUG
59 m_cache_hits(0), m_cache_misses(0)
64 FormatCache::Entry
&FormatCache::GetEntry(ConstString type
) {
65 auto i
= m_map
.find(type
), e
= m_map
.end();
68 m_map
[type
] = FormatCache::Entry();
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
90 entry
.Get(format_impl_sp
);
93 #ifdef LLDB_CONFIGURATION_DEBUG
96 format_impl_sp
.reset();
100 /// Explicit instantiations for the three types.
103 FormatCache::Get
<lldb::TypeFormatImplSP
>(ConstString
, lldb::TypeFormatImplSP
&);
105 FormatCache::Get
<lldb::TypeSummaryImplSP
>(ConstString
,
106 lldb::TypeSummaryImplSP
&);
108 FormatCache::Get
<lldb::SyntheticChildrenSP
>(ConstString
,
109 lldb::SyntheticChildrenSP
&);
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
);