1 //===-- FormatCache.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 // C Includes
13 
14 // C++ Includes
15 
16 // Other libraries and framework includes
17 
18 // Project includes
19 #include "lldb/DataFormatters/FormatCache.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
Entry()24 FormatCache::Entry::Entry () :
25 m_summary_cached(false),
26 m_synthetic_cached(false),
27 m_summary_sp(),
28 m_synthetic_sp()
29 {}
30 
Entry(const Entry & rhs)31 FormatCache::Entry::Entry (const Entry& rhs) :
32 m_summary_cached(rhs.m_summary_cached),
33 m_synthetic_cached(rhs.m_synthetic_cached),
34 m_summary_sp(rhs.m_summary_sp),
35 m_synthetic_sp(rhs.m_synthetic_sp)
36 {}
37 
Entry(lldb::TypeSummaryImplSP summary_sp)38 FormatCache::Entry::Entry (lldb::TypeSummaryImplSP summary_sp) :
39 m_synthetic_cached(false),
40 m_synthetic_sp()
41 {
42     SetSummary (summary_sp);
43 }
44 
Entry(lldb::SyntheticChildrenSP synthetic_sp)45 FormatCache::Entry::Entry (lldb::SyntheticChildrenSP synthetic_sp) :
46 m_summary_cached(false),
47 m_summary_sp()
48 {
49     SetSynthetic (synthetic_sp);
50 }
51 
Entry(lldb::TypeSummaryImplSP summary_sp,lldb::SyntheticChildrenSP synthetic_sp)52 FormatCache::Entry::Entry (lldb::TypeSummaryImplSP summary_sp,lldb::SyntheticChildrenSP synthetic_sp)
53 {
54     SetSummary (summary_sp);
55     SetSynthetic (synthetic_sp);
56 }
57 
operator =(const Entry & rhs)58 FormatCache::Entry& FormatCache::Entry::operator= (const Entry& rhs)
59 {
60     if (this == &rhs)
61         return *this;
62 
63     m_summary_cached = rhs.m_summary_cached;
64     m_synthetic_cached = rhs.m_synthetic_cached;
65     m_summary_sp = rhs.m_summary_sp;
66     m_synthetic_sp = rhs.m_synthetic_sp;
67     return *this;
68 }
69 
70 bool
IsSummaryCached()71 FormatCache::Entry::IsSummaryCached ()
72 {
73     return m_summary_cached;
74 }
75 
76 bool
IsSyntheticCached()77 FormatCache::Entry::IsSyntheticCached ()
78 {
79     return m_synthetic_cached;
80 }
81 
82 lldb::TypeSummaryImplSP
GetSummary()83 FormatCache::Entry::GetSummary ()
84 {
85     return m_summary_sp;
86 }
87 
88 lldb::SyntheticChildrenSP
GetSynthetic()89 FormatCache::Entry::GetSynthetic ()
90 {
91     return m_synthetic_sp;
92 }
93 
94 void
SetSummary(lldb::TypeSummaryImplSP summary_sp)95 FormatCache::Entry::SetSummary (lldb::TypeSummaryImplSP summary_sp)
96 {
97     m_summary_cached = true;
98     m_summary_sp = summary_sp;
99 }
100 
101 void
SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp)102 FormatCache::Entry::SetSynthetic (lldb::SyntheticChildrenSP synthetic_sp)
103 {
104     m_synthetic_cached = true;
105     m_synthetic_sp = synthetic_sp;
106 }
107 
FormatCache()108 FormatCache::FormatCache () :
109 m_map(),
110 m_mutex (Mutex::eMutexTypeRecursive)
111 #ifdef LLDB_CONFIGURATION_DEBUG
112 ,m_cache_hits(0),m_cache_misses(0)
113 #endif
114 {
115 }
116 
117 FormatCache::Entry&
GetEntry(const ConstString & type)118 FormatCache::GetEntry (const ConstString& type)
119 {
120     auto i = m_map.find(type),
121     e = m_map.end();
122     if (i != e)
123         return i->second;
124     m_map[type] = FormatCache::Entry();
125     return m_map[type];
126 }
127 
128 bool
GetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)129 FormatCache::GetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp)
130 {
131     Mutex::Locker lock(m_mutex);
132     auto entry = GetEntry(type);
133     if (entry.IsSummaryCached())
134     {
135 #ifdef LLDB_CONFIGURATION_DEBUG
136         m_cache_hits++;
137 #endif
138         summary_sp = entry.GetSummary();
139         return true;
140     }
141 #ifdef LLDB_CONFIGURATION_DEBUG
142     m_cache_misses++;
143 #endif
144     summary_sp.reset();
145     return false;
146 }
147 
148 bool
GetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)149 FormatCache::GetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp)
150 {
151     Mutex::Locker lock(m_mutex);
152     auto entry = GetEntry(type);
153     if (entry.IsSyntheticCached())
154     {
155 #ifdef LLDB_CONFIGURATION_DEBUG
156         m_cache_hits++;
157 #endif
158         synthetic_sp = entry.GetSynthetic();
159         return true;
160     }
161 #ifdef LLDB_CONFIGURATION_DEBUG
162     m_cache_misses++;
163 #endif
164     synthetic_sp.reset();
165     return false;
166 }
167 
168 void
SetSummary(const ConstString & type,lldb::TypeSummaryImplSP & summary_sp)169 FormatCache::SetSummary (const ConstString& type,lldb::TypeSummaryImplSP& summary_sp)
170 {
171     Mutex::Locker lock(m_mutex);
172     GetEntry(type).SetSummary(summary_sp);
173 }
174 
175 void
SetSynthetic(const ConstString & type,lldb::SyntheticChildrenSP & synthetic_sp)176 FormatCache::SetSynthetic (const ConstString& type,lldb::SyntheticChildrenSP& synthetic_sp)
177 {
178     Mutex::Locker lock(m_mutex);
179     GetEntry(type).SetSynthetic(synthetic_sp);
180 }
181 
182 void
Clear()183 FormatCache::Clear ()
184 {
185     Mutex::Locker lock(m_mutex);
186     m_map.clear();
187 }
188 
189