LLDB  mainline
FormatCache.cpp
Go to the documentation of this file.
1 //===-- FormatCache.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 
10 
11 
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
18  : m_format_cached(false), m_summary_cached(false),
19  m_synthetic_cached(false) {}
20 
21 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
22 
23 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
24 
25 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
26 
27 void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
28  retval = m_format_sp;
29 }
30 
31 void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
32  retval = m_summary_sp;
33 }
34 
35 void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
36  retval = m_synthetic_sp;
37 }
38 
39 void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
40  m_format_cached = true;
41  m_format_sp = format_sp;
42 }
43 
44 void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
45  m_summary_cached = true;
46  m_summary_sp = summary_sp;
47 }
48 
49 void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
50  m_synthetic_cached = true;
51  m_synthetic_sp = synthetic_sp;
52 }
53 
55  auto i = m_map.find(type), e = m_map.end();
56  if (i != e)
57  return i->second;
58  m_map[type] = FormatCache::Entry();
59  return m_map[type];
60 }
61 
62 namespace lldb_private {
63 
64 template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
65  return IsFormatCached();
66 }
67 template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
68  return IsSummaryCached();
69 }
70 template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
71  return IsSyntheticCached();
72 }
73 
74 } // namespace lldb_private
75 
76 template <typename ImplSP>
77 bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
78  std::lock_guard<std::recursive_mutex> guard(m_mutex);
79  auto entry = GetEntry(type);
80  if (entry.IsCached<ImplSP>()) {
81  m_cache_hits++;
82  entry.Get(format_impl_sp);
83  return true;
84  }
86  format_impl_sp.reset();
87  return false;
88 }
89 
90 /// Explicit instantiations for the three types.
91 /// \{
92 template bool
93 FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
94 template bool
95 FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
96  lldb::TypeSummaryImplSP &);
97 template bool
98 FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
99  lldb::SyntheticChildrenSP &);
100 /// \}
101 
102 void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {
103  std::lock_guard<std::recursive_mutex> guard(m_mutex);
104  GetEntry(type).Set(format_sp);
105 }
106 
107 void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {
108  std::lock_guard<std::recursive_mutex> guard(m_mutex);
109  GetEntry(type).Set(summary_sp);
110 }
111 
113  lldb::SyntheticChildrenSP &synthetic_sp) {
114  std::lock_guard<std::recursive_mutex> guard(m_mutex);
115  GetEntry(type).Set(synthetic_sp);
116 }
117 
119  std::lock_guard<std::recursive_mutex> guard(m_mutex);
120  m_map.clear();
121 }
lldb_private::FormatCache::Set
void Set(ConstString type, lldb::TypeFormatImplSP &format_sp)
Definition: FormatCache.cpp:102
lldb_private::FormatCache::m_map
CacheMap m_map
Definition: FormatCache.h:49
lldb_private::FormatCache::m_cache_misses
uint64_t m_cache_misses
Definition: FormatCache.h:53
lldb_private::FormatCache::Get
bool Get(ConstString type, ImplSP &format_impl_sp)
Definition: FormatCache.cpp:77
lldb_private::FormatCache::Entry::Set
void Set(lldb::TypeFormatImplSP)
Definition: FormatCache.cpp:39
lldb_private::FormatCache::Entry::IsSummaryCached
bool IsSummaryCached()
Definition: FormatCache.cpp:23
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::FormatCache::m_mutex
std::recursive_mutex m_mutex
Definition: FormatCache.h:50
FormatCache.h
lldb_private::FormatCache::m_cache_hits
uint64_t m_cache_hits
Definition: FormatCache.h:52
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::FormatCache::GetEntry
Entry & GetEntry(ConstString type)
Definition: FormatCache.cpp:54
lldb_private::FormatCache::Clear
void Clear()
Definition: FormatCache.cpp:118
Entry
FormatEntity::Entry Entry
Definition: FormatEntity.cpp:82
lldb_private::FormatCache::Entry::IsSyntheticCached
bool IsSyntheticCached()
Definition: FormatCache.cpp:25
lldb_private::FormatCache::Entry
Definition: FormatCache.h:22
lldb
Definition: SBAddress.h:15
lldb_private::FormatCache::Entry::Get
void Get(lldb::TypeFormatImplSP &)
Definition: FormatCache.cpp:27
lldb_private::FormatCache::Entry::IsFormatCached
bool IsFormatCached()
Definition: FormatCache.cpp:21