Path: blob/main/contrib/llvm-project/lldb/source/DataFormatters/FormatCache.cpp
39587 views
//===-- FormatCache.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7891011#include "lldb/DataFormatters/FormatCache.h"1213using namespace lldb;14using namespace lldb_private;1516FormatCache::Entry::Entry()17: m_format_cached(false), m_summary_cached(false),18m_synthetic_cached(false) {}1920bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }2122bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached; }2324bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }2526void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {27retval = m_format_sp;28}2930void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {31retval = m_summary_sp;32}3334void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {35retval = m_synthetic_sp;36}3738void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {39m_format_cached = true;40m_format_sp = format_sp;41}4243void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {44m_summary_cached = true;45m_summary_sp = summary_sp;46}4748void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {49m_synthetic_cached = true;50m_synthetic_sp = synthetic_sp;51}5253namespace lldb_private {5455template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {56return IsFormatCached();57}58template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {59return IsSummaryCached();60}61template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {62return IsSyntheticCached();63}6465} // namespace lldb_private6667template <typename ImplSP>68bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {69std::lock_guard<std::recursive_mutex> guard(m_mutex);70auto entry = m_entries[type];71if (entry.IsCached<ImplSP>()) {72m_cache_hits++;73entry.Get(format_impl_sp);74return true;75}76m_cache_misses++;77format_impl_sp.reset();78return false;79}8081/// Explicit instantiations for the three types.82/// \{83template bool84FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);85template bool86FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,87lldb::TypeSummaryImplSP &);88template bool89FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,90lldb::SyntheticChildrenSP &);91/// \}9293void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {94std::lock_guard<std::recursive_mutex> guard(m_mutex);95m_entries[type].Set(format_sp);96}9798void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {99std::lock_guard<std::recursive_mutex> guard(m_mutex);100m_entries[type].Set(summary_sp);101}102103void FormatCache::Set(ConstString type,104lldb::SyntheticChildrenSP &synthetic_sp) {105std::lock_guard<std::recursive_mutex> guard(m_mutex);106m_entries[type].Set(synthetic_sp);107}108109void FormatCache::Clear() {110std::lock_guard<std::recursive_mutex> guard(m_mutex);111m_entries.clear();112}113114115