From aee2d80f628ba02d0561c921b7bb1939b3480fca Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 23 Jan 2017 15:39:01 -0500 Subject: Cleanup memory in CFDE_CSSRuleCollection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This Cl cleans up the memory for the CFDE_CSSRuleCollection::Data parameter. Change-Id: I449ffdeebdc6463bf68b991fffecdc6cf0b25362 Reviewed-on: https://pdfium-review.googlesource.com/2296 Reviewed-by: Nicolás Peña Commit-Queue: dsinclair --- xfa/fde/css/cfde_cssrulecollection.cpp | 62 ++++++++++------------------------ xfa/fde/css/cfde_cssrulecollection.h | 22 ++++-------- xfa/fde/css/cfde_cssstyleselector.cpp | 35 ++++++++++--------- xfa/fde/css/cfde_cssstyleselector.h | 7 ++-- 4 files changed, 47 insertions(+), 79 deletions(-) diff --git a/xfa/fde/css/cfde_cssrulecollection.cpp b/xfa/fde/css/cfde_cssrulecollection.cpp index c140c8e682..a72e1bb0bd 100644 --- a/xfa/fde/css/cfde_cssrulecollection.cpp +++ b/xfa/fde/css/cfde_cssrulecollection.cpp @@ -7,9 +7,9 @@ #include "xfa/fde/css/cfde_cssrulecollection.h" #include -#include -#include +#include +#include "third_party/base/ptr_util.h" #include "xfa/fde/css/cfde_cssdeclaration.h" #include "xfa/fde/css/cfde_cssselector.h" #include "xfa/fde/css/cfde_cssstylerule.h" @@ -17,15 +17,21 @@ #include "xfa/fde/css/cfde_csssyntaxparser.h" #include "xfa/fde/css/cfde_csstagcache.h" +CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() : m_iSelectors(0) {} + +CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() { + Clear(); +} + void CFDE_CSSRuleCollection::Clear() { m_TagRules.clear(); m_iSelectors = 0; } -CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() : m_iSelectors(0) {} - -CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() { - Clear(); +const std::vector>* +CFDE_CSSRuleCollection::GetTagRuleData(uint32_t dwTagHash) const { + auto it = m_TagRules.find(dwTagHash); + return it != m_TagRules.end() ? &it->second : nullptr; } void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* sheet, @@ -42,46 +48,12 @@ void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* pStyleSheet, int32_t iSelectors = pStyleRule->CountSelectorLists(); for (int32_t i = 0; i < iSelectors; ++i) { CFDE_CSSSelector* pSelector = pStyleRule->GetSelectorList(i); - AddRuleTo(&m_TagRules, pSelector->GetNameHash(), pSelector, pDeclaration); + m_TagRules[pSelector->GetNameHash()].push_back( + pdfium::MakeUnique(pSelector, pDeclaration)); + m_iSelectors++; } } -void CFDE_CSSRuleCollection::AddRuleTo(std::map* pMap, - uint32_t dwKey, - CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl) { - Data* pData = NewRuleData(pSel, pDecl); - Data* pList = (*pMap)[dwKey]; - if (!pList) { - (*pMap)[dwKey] = pData; - } else if (AddRuleTo(&pList, pData)) { - (*pMap)[dwKey] = pList; - } -} - -bool CFDE_CSSRuleCollection::AddRuleTo(Data** pList, Data* pData) { - if (*pList) { - pData->pNext = (*pList)->pNext; - (*pList)->pNext = pData; - return false; - } - *pList = pData; - return true; -} - -CFDE_CSSRuleCollection::Data* CFDE_CSSRuleCollection::NewRuleData( - CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl) { - return new Data(pSel, pDecl, ++m_iSelectors); -} - CFDE_CSSRuleCollection::Data::Data(CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl, - uint32_t dwPos) - : pSelector(pSel), pDeclaration(pDecl), dwPriority(dwPos), pNext(nullptr) { - static const uint32_t s_Specific[5] = {0x00010000, 0x00010000, 0x00100000, - 0x00100000, 0x01000000}; - for (; pSel; pSel = pSel->GetNextSelector()) { - dwPriority += s_Specific[static_cast(pSel->GetType())]; - } -} + CFDE_CSSDeclaration* pDecl) + : pSelector(pSel), pDeclaration(pDecl) {} diff --git a/xfa/fde/css/cfde_cssrulecollection.h b/xfa/fde/css/cfde_cssrulecollection.h index 47e7696c34..d9bbeb3736 100644 --- a/xfa/fde/css/cfde_cssrulecollection.h +++ b/xfa/fde/css/cfde_cssrulecollection.h @@ -8,6 +8,8 @@ #define XFA_FDE_CSS_CFDE_CSSRULECOLLECTION_H_ #include +#include +#include #include "core/fxcrt/fx_basic.h" @@ -21,12 +23,10 @@ class CFDE_CSSRuleCollection { public: class Data { public: - Data(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl, uint32_t dwPos); + Data(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl); CFDE_CSSSelector* const pSelector; CFDE_CSSDeclaration* const pDeclaration; - uint32_t dwPriority; - Data* pNext; }; CFDE_CSSRuleCollection(); @@ -36,23 +36,15 @@ class CFDE_CSSRuleCollection { void Clear(); int32_t CountSelectors() const { return m_iSelectors; } - Data* GetTagRuleData(uint32_t dwTagHash) { - auto it = m_TagRules.find(dwTagHash); - return it != m_TagRules.end() ? it->second : nullptr; - } + const std::vector>* GetTagRuleData( + uint32_t dwTagHash) const; private: void AddRulesFrom(const CFDE_CSSStyleSheet* pStyleSheet, CFDE_CSSStyleRule* pRule, CFGAS_FontMgr* pFontMgr); - void AddRuleTo(std::map* pMap, - uint32_t dwKey, - CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl); - bool AddRuleTo(Data** pList, Data* pData); - Data* NewRuleData(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl); - - std::map m_TagRules; + + std::map>> m_TagRules; int32_t m_iSelectors; }; diff --git a/xfa/fde/css/cfde_cssstyleselector.cpp b/xfa/fde/css/cfde_cssstyleselector.cpp index b7275fca93..63d8e0986b 100644 --- a/xfa/fde/css/cfde_cssstyleselector.cpp +++ b/xfa/fde/css/cfde_cssstyleselector.cpp @@ -85,27 +85,30 @@ int32_t CFDE_CSSStyleSelector::MatchDeclarations( if (m_UARules.CountSelectors() == 0) return 0; - if (pCache->HashTag()) - MatchRules(pCache, m_UARules.GetTagRuleData(pCache->HashTag())); - - std::sort(m_MatchedRules.begin(), m_MatchedRules.end(), - [](const CFDE_CSSRuleCollection::Data* p1, - const CFDE_CSSRuleCollection::Data* p2) { - return p1->dwPriority < p2->dwPriority; - }); - for (const auto& rule : m_MatchedRules) + // The ::Data is owned by the m_RuleCollection. + std::vector matchedRules; + + if (pCache->HashTag()) { + MatchRules(&matchedRules, pCache, + m_UARules.GetTagRuleData(pCache->HashTag())); + } + + for (const auto& rule : matchedRules) matchedDecls.Add(rule->pDeclaration); - m_MatchedRules.clear(); return matchedDecls.GetSize(); } -void CFDE_CSSStyleSelector::MatchRules(CFDE_CSSTagCache* pCache, - CFDE_CSSRuleCollection::Data* pList) { - while (pList) { - if (MatchSelector(pCache, pList->pSelector)) - m_MatchedRules.push_back(pList); - pList = pList->pNext; +void CFDE_CSSStyleSelector::MatchRules( + std::vector* matchedRules, + CFDE_CSSTagCache* pCache, + const std::vector>* pList) { + if (!pList) + return; + + for (const auto& d : *pList) { + if (MatchSelector(pCache, d->pSelector)) + matchedRules->push_back(d.get()); } } diff --git a/xfa/fde/css/cfde_cssstyleselector.h b/xfa/fde/css/cfde_cssstyleselector.h index 6eb32aef62..2b03d7e961 100644 --- a/xfa/fde/css/cfde_cssstyleselector.h +++ b/xfa/fde/css/cfde_cssstyleselector.h @@ -48,8 +48,10 @@ class CFDE_CSSStyleSelector { CFDE_CSSComputedStyle* pDestStyle); private: - void MatchRules(CFDE_CSSTagCache* pCache, - CFDE_CSSRuleCollection::Data* pList); + void MatchRules( + std::vector* matchedRules, + CFDE_CSSTagCache* pCache, + const std::vector>* pList); bool MatchSelector(CFDE_CSSTagCache* pCache, CFDE_CSSSelector* pSel); void AppendInlineStyle(CFDE_CSSDeclaration* pDecl, const FX_WCHAR* psz, @@ -80,7 +82,6 @@ class CFDE_CSSStyleSelector { std::unique_ptr m_UAStyles; CFDE_CSSRuleCollection m_UARules; std::unique_ptr m_pAccelerator; - std::vector m_MatchedRules; }; #endif // XFA_FDE_CSS_CFDE_CSSSTYLESELECTOR_H_ -- cgit v1.2.3