1*dda28197Spatrick //===-- FormatCache.cpp ---------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick
10061da546Spatrick
11061da546Spatrick
12061da546Spatrick #include "lldb/DataFormatters/FormatCache.h"
13061da546Spatrick
14061da546Spatrick using namespace lldb;
15061da546Spatrick using namespace lldb_private;
16061da546Spatrick
Entry()17061da546Spatrick FormatCache::Entry::Entry()
18061da546Spatrick : m_format_cached(false), m_summary_cached(false),
19061da546Spatrick m_synthetic_cached(false) {}
20061da546Spatrick
IsFormatCached()21061da546Spatrick bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
22061da546Spatrick
IsSummaryCached()23061da546Spatrick bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
24061da546Spatrick
IsSyntheticCached()25061da546Spatrick bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
26061da546Spatrick
Get(lldb::TypeFormatImplSP & retval)27061da546Spatrick void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
28061da546Spatrick retval = m_format_sp;
29061da546Spatrick }
30061da546Spatrick
Get(lldb::TypeSummaryImplSP & retval)31061da546Spatrick void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
32061da546Spatrick retval = m_summary_sp;
33061da546Spatrick }
34061da546Spatrick
Get(lldb::SyntheticChildrenSP & retval)35061da546Spatrick void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
36061da546Spatrick retval = m_synthetic_sp;
37061da546Spatrick }
38061da546Spatrick
Set(lldb::TypeFormatImplSP format_sp)39061da546Spatrick void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
40061da546Spatrick m_format_cached = true;
41061da546Spatrick m_format_sp = format_sp;
42061da546Spatrick }
43061da546Spatrick
Set(lldb::TypeSummaryImplSP summary_sp)44061da546Spatrick void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
45061da546Spatrick m_summary_cached = true;
46061da546Spatrick m_summary_sp = summary_sp;
47061da546Spatrick }
48061da546Spatrick
Set(lldb::SyntheticChildrenSP synthetic_sp)49061da546Spatrick void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
50061da546Spatrick m_synthetic_cached = true;
51061da546Spatrick m_synthetic_sp = synthetic_sp;
52061da546Spatrick }
53061da546Spatrick
GetEntry(ConstString type)54061da546Spatrick FormatCache::Entry &FormatCache::GetEntry(ConstString type) {
55061da546Spatrick auto i = m_map.find(type), e = m_map.end();
56061da546Spatrick if (i != e)
57061da546Spatrick return i->second;
58061da546Spatrick m_map[type] = FormatCache::Entry();
59061da546Spatrick return m_map[type];
60061da546Spatrick }
61061da546Spatrick
62061da546Spatrick namespace lldb_private {
63061da546Spatrick
IsCached()64061da546Spatrick template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
65061da546Spatrick return IsFormatCached();
66061da546Spatrick }
IsCached()67061da546Spatrick template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
68061da546Spatrick return IsSummaryCached();
69061da546Spatrick }
IsCached()70061da546Spatrick template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
71061da546Spatrick return IsSyntheticCached();
72061da546Spatrick }
73061da546Spatrick
74061da546Spatrick } // namespace lldb_private
75061da546Spatrick
76061da546Spatrick template <typename ImplSP>
Get(ConstString type,ImplSP & format_impl_sp)77061da546Spatrick bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
78061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
79061da546Spatrick auto entry = GetEntry(type);
80061da546Spatrick if (entry.IsCached<ImplSP>()) {
81061da546Spatrick m_cache_hits++;
82061da546Spatrick entry.Get(format_impl_sp);
83061da546Spatrick return true;
84061da546Spatrick }
85061da546Spatrick m_cache_misses++;
86061da546Spatrick format_impl_sp.reset();
87061da546Spatrick return false;
88061da546Spatrick }
89061da546Spatrick
90061da546Spatrick /// Explicit instantiations for the three types.
91061da546Spatrick /// \{
92061da546Spatrick template bool
93061da546Spatrick FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
94061da546Spatrick template bool
95061da546Spatrick FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
96061da546Spatrick lldb::TypeSummaryImplSP &);
97061da546Spatrick template bool
98061da546Spatrick FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
99061da546Spatrick lldb::SyntheticChildrenSP &);
100061da546Spatrick /// \}
101061da546Spatrick
Set(ConstString type,lldb::TypeFormatImplSP & format_sp)102061da546Spatrick void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {
103061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
104061da546Spatrick GetEntry(type).Set(format_sp);
105061da546Spatrick }
106061da546Spatrick
Set(ConstString type,lldb::TypeSummaryImplSP & summary_sp)107061da546Spatrick void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {
108061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
109061da546Spatrick GetEntry(type).Set(summary_sp);
110061da546Spatrick }
111061da546Spatrick
Set(ConstString type,lldb::SyntheticChildrenSP & synthetic_sp)112061da546Spatrick void FormatCache::Set(ConstString type,
113061da546Spatrick lldb::SyntheticChildrenSP &synthetic_sp) {
114061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
115061da546Spatrick GetEntry(type).Set(synthetic_sp);
116061da546Spatrick }
117061da546Spatrick
Clear()118061da546Spatrick void FormatCache::Clear() {
119061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
120061da546Spatrick m_map.clear();
121061da546Spatrick }
122