From 910b7635d72b66a06ab22806ad09b650131b4dc2 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 19 May 2017 17:08:25 -0700 Subject: Use std::vector and std::sort() in CPDF_PageRenderCache. Instead of allocating a chunk of memory and using qsort(). Change-Id: I95041f79cfe609a9d99493d59eb074599ec49230 Reviewed-on: https://pdfium-review.googlesource.com/5690 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fpdfapi/render/cpdf_pagerendercache.cpp | 42 +++++++++++++--------------- core/fpdfapi/render/cpdf_pagerendercache.h | 1 - 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/core/fpdfapi/render/cpdf_pagerendercache.cpp b/core/fpdfapi/render/cpdf_pagerendercache.cpp index e427627a9b..fa23355154 100644 --- a/core/fpdfapi/render/cpdf_pagerendercache.cpp +++ b/core/fpdfapi/render/cpdf_pagerendercache.cpp @@ -6,23 +6,23 @@ #include "core/fpdfapi/render/cpdf_pagerendercache.h" +#include +#include + #include "core/fpdfapi/page/cpdf_page.h" #include "core/fpdfapi/render/cpdf_imagecacheentry.h" #include "core/fpdfapi/render/cpdf_renderstatus.h" namespace { -struct CACHEINFO { +struct CacheInfo { + CacheInfo(uint32_t t, CPDF_Stream* stream) : time(t), pStream(stream) {} + uint32_t time; CPDF_Stream* pStream; -}; -extern "C" { -static int compare(const void* data1, const void* data2) { - return reinterpret_cast(data1)->time - - reinterpret_cast(data2)->time; -} -} // extern "C" + bool operator<(const CacheInfo& other) const { return time < other.time; } +}; } // namespace @@ -43,31 +43,27 @@ void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) { return; size_t nCount = m_ImageCache.size(); - CACHEINFO* pCACHEINFO = FX_Alloc(CACHEINFO, nCount); - size_t i = 0; - for (const auto& it : m_ImageCache) { - pCACHEINFO[i].time = it.second->GetTimeCount(); - pCACHEINFO[i++].pStream = it.second->GetStream(); - } - qsort(pCACHEINFO, nCount, sizeof(CACHEINFO), compare); - uint32_t nTimeCount = m_nTimeCount; + std::vector cache_info; + cache_info.reserve(nCount); + for (const auto& it : m_ImageCache) + cache_info.emplace_back(it.second->GetTimeCount(), it.second->GetStream()); + std::sort(cache_info.begin(), cache_info.end()); // Check if time value is about to roll over and reset all entries. // The comparision is legal because uint32_t is an unsigned type. + uint32_t nTimeCount = m_nTimeCount; if (nTimeCount + 1 < nTimeCount) { - for (i = 0; i < nCount; i++) - m_ImageCache[pCACHEINFO[i].pStream]->m_dwTimeCount = i; + for (size_t i = 0; i < nCount; i++) + m_ImageCache[cache_info[i].pStream]->m_dwTimeCount = i; m_nTimeCount = nCount; } - i = 0; + size_t i = 0; while (i + 15 < nCount) - ClearImageCacheEntry(pCACHEINFO[i++].pStream); + ClearImageCacheEntry(cache_info[i++].pStream); while (i < nCount && m_nCacheSize > (uint32_t)dwLimitCacheSize) - ClearImageCacheEntry(pCACHEINFO[i++].pStream); - - FX_Free(pCACHEINFO); + ClearImageCacheEntry(cache_info[i++].pStream); } void CPDF_PageRenderCache::ClearImageCacheEntry(CPDF_Stream* pStream) { diff --git a/core/fpdfapi/render/cpdf_pagerendercache.h b/core/fpdfapi/render/cpdf_pagerendercache.h index 8235cec22d..5d9ae03a1c 100644 --- a/core/fpdfapi/render/cpdf_pagerendercache.h +++ b/core/fpdfapi/render/cpdf_pagerendercache.h @@ -17,7 +17,6 @@ class CPDF_ImageCacheEntry; class CPDF_Page; class CPDF_RenderStatus; class CFX_DIBitmap; -class CFX_DIBSource; class IFX_Pause; class CPDF_PageRenderCache { -- cgit v1.2.3