FormatCache.cpp revision 360784
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//===----------------------------------------------------------------------===//
9
10
11
12
13#include "lldb/DataFormatters/FormatCache.h"
14
15using namespace lldb;
16using namespace lldb_private;
17
18FormatCache::Entry::Entry()
19    : m_format_cached(false), m_summary_cached(false),
20      m_synthetic_cached(false) {}
21
22bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
23
24bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }
25
26bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
27
28void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
29  retval = m_format_sp;
30}
31
32void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
33  retval = m_summary_sp;
34}
35
36void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
37  retval = m_synthetic_sp;
38}
39
40void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
41  m_format_cached = true;
42  m_format_sp = format_sp;
43}
44
45void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
46  m_summary_cached = true;
47  m_summary_sp = summary_sp;
48}
49
50void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
51  m_synthetic_cached = true;
52  m_synthetic_sp = synthetic_sp;
53}
54
55FormatCache::FormatCache()
56    : m_map(), m_mutex()
57#ifdef LLDB_CONFIGURATION_DEBUG
58      ,
59      m_cache_hits(0), m_cache_misses(0)
60#endif
61{
62}
63
64FormatCache::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];
70}
71
72namespace lldb_private {
73
74template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
75  return IsFormatCached();
76}
77template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
78  return IsSummaryCached();
79}
80template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
81  return IsSyntheticCached();
82}
83
84} // namespace lldb_private
85
86template <typename ImplSP>
87bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
88  std::lock_guard<std::recursive_mutex> guard(m_mutex);
89  auto entry = GetEntry(type);
90  if (entry.IsCached<ImplSP>()) {
91#ifdef LLDB_CONFIGURATION_DEBUG
92    m_cache_hits++;
93#endif
94    entry.Get(format_impl_sp);
95    return true;
96  }
97#ifdef LLDB_CONFIGURATION_DEBUG
98  m_cache_misses++;
99#endif
100  format_impl_sp.reset();
101  return false;
102}
103
104/// Explicit instantiations for the three types.
105/// \{
106template bool
107FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
108template bool
109FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
110                                          lldb::TypeSummaryImplSP &);
111template bool
112FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
113                                            lldb::SyntheticChildrenSP &);
114/// \}
115
116void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {
117  std::lock_guard<std::recursive_mutex> guard(m_mutex);
118  GetEntry(type).Set(format_sp);
119}
120
121void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {
122  std::lock_guard<std::recursive_mutex> guard(m_mutex);
123  GetEntry(type).Set(summary_sp);
124}
125
126void FormatCache::Set(ConstString type,
127                      lldb::SyntheticChildrenSP &synthetic_sp) {
128  std::lock_guard<std::recursive_mutex> guard(m_mutex);
129  GetEntry(type).Set(synthetic_sp);
130}
131
132void FormatCache::Clear() {
133  std::lock_guard<std::recursive_mutex> guard(m_mutex);
134  m_map.clear();
135}
136