[poppler] poppler/CMap.cc poppler/CMap.h poppler/GfxFont.cc poppler/GfxFont.h poppler/GlobalParams.cc poppler/GlobalParams.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Feb 16 22:31:26 UTC 2022


 poppler/CMap.cc         |   79 ++++++++++--------------------------------------
 poppler/CMap.h          |   20 +++++-------
 poppler/GfxFont.cc      |    9 +----
 poppler/GfxFont.h       |    5 +--
 poppler/GlobalParams.cc |    2 -
 poppler/GlobalParams.h  |    4 +-
 6 files changed, 35 insertions(+), 84 deletions(-)

New commits:
commit b9181a5a99b2cbea568b3bc15f4fc0f648358577
Author: Albert Astals Cid <aacid at kde.org>
Date:   Thu Feb 10 16:36:07 2022 +0100

    CMap: Turn manual refcounting into using shared_ptr

diff --git a/poppler/CMap.cc b/poppler/CMap.cc
index 28c0c818..b9b861c7 100644
--- a/poppler/CMap.cc
+++ b/poppler/CMap.cc
@@ -65,9 +65,9 @@ static int getCharFromStream(void *data)
 
 //------------------------------------------------------------------------
 
-CMap *CMap::parse(CMapCache *cache, const GooString *collectionA, Object *obj)
+std::shared_ptr<CMap> CMap::parse(CMapCache *cache, const GooString *collectionA, Object *obj)
 {
-    CMap *cMap;
+    std::shared_ptr<CMap> cMap;
     GooString *cMapNameA;
 
     if (obj->isName()) {
@@ -82,31 +82,30 @@ CMap *CMap::parse(CMapCache *cache, const GooString *collectionA, Object *obj)
         }
     } else {
         error(errSyntaxError, -1, "Invalid Encoding in Type 0 font");
-        return nullptr;
+        return {};
     }
     return cMap;
 }
 
-CMap *CMap::parse(CMapCache *cache, const GooString *collectionA, const GooString *cMapNameA)
+std::shared_ptr<CMap> CMap::parse(CMapCache *cache, const GooString *collectionA, const GooString *cMapNameA)
 {
     FILE *f;
-    CMap *cMap;
 
     if (!(f = globalParams->findCMapFile(collectionA, cMapNameA))) {
 
         // Check for an identity CMap.
         if (!cMapNameA->cmp("Identity") || !cMapNameA->cmp("Identity-H")) {
-            return new CMap(collectionA->copy(), cMapNameA->copy(), 0);
+            return std::shared_ptr<CMap>(new CMap(collectionA->copy(), cMapNameA->copy(), 0));
         }
         if (!cMapNameA->cmp("Identity-V")) {
-            return new CMap(collectionA->copy(), cMapNameA->copy(), 1);
+            return std::shared_ptr<CMap>(new CMap(collectionA->copy(), cMapNameA->copy(), 1));
         }
 
         error(errSyntaxError, -1, "Couldn't find '{0:t}' CMap file for '{1:t}' collection", cMapNameA, collectionA);
-        return nullptr;
+        return {};
     }
 
-    cMap = new CMap(collectionA->copy(), cMapNameA->copy());
+    auto cMap = std::shared_ptr<CMap>(new CMap(collectionA->copy(), cMapNameA->copy()));
     cMap->parse2(cache, &getCharFromFile, f);
 
     fclose(f);
@@ -114,9 +113,9 @@ CMap *CMap::parse(CMapCache *cache, const GooString *collectionA, const GooStrin
     return cMap;
 }
 
-CMap *CMap::parse(CMapCache *cache, const GooString *collectionA, Stream *str)
+std::shared_ptr<CMap> CMap::parse(CMapCache *cache, const GooString *collectionA, Stream *str)
 {
-    CMap *cMap = new CMap(collectionA->copy(), nullptr);
+    auto cMap = std::shared_ptr<CMap>(new CMap(collectionA->copy(), nullptr));
     Object obj1 = str->getDict()->lookup("UseCMap");
     if (!obj1.isNull()) {
         cMap->useCMap(cache, &obj1);
@@ -206,7 +205,6 @@ CMap::CMap(GooString *collectionA, GooString *cMapNameA)
         vector[i].isVector = false;
         vector[i].cid = 0;
     }
-    refCnt = 1;
 }
 
 CMap::CMap(GooString *collectionA, GooString *cMapNameA, int wModeA)
@@ -216,13 +214,12 @@ CMap::CMap(GooString *collectionA, GooString *cMapNameA, int wModeA)
     isIdent = true;
     wMode = wModeA;
     vector = nullptr;
-    refCnt = 1;
 }
 
 void CMap::useCMap(CMapCache *cache, const char *useName)
 {
     GooString *useNameStr;
-    CMap *subCMap;
+    std::shared_ptr<CMap> subCMap;
 
     useNameStr = new GooString(useName);
     // if cache is non-NULL, we already have a lock, and we can use
@@ -242,14 +239,11 @@ void CMap::useCMap(CMapCache *cache, const char *useName)
     if (subCMap->vector) {
         copyVector(vector, subCMap->vector);
     }
-    subCMap->decRefCnt();
 }
 
 void CMap::useCMap(CMapCache *cache, Object *obj)
 {
-    CMap *subCMap;
-
-    subCMap = CMap::parse(cache, collection, obj);
+    std::shared_ptr<CMap> subCMap = CMap::parse(cache, collection, obj);
     if (!subCMap) {
         return;
     }
@@ -257,7 +251,6 @@ void CMap::useCMap(CMapCache *cache, Object *obj)
     if (subCMap->vector) {
         copyVector(vector, subCMap->vector);
     }
-    subCMap->decRefCnt();
 }
 
 void CMap::copyVector(CMapVectorEntry *dest, CMapVectorEntry *src)
@@ -341,18 +334,6 @@ void CMap::freeCMapVector(CMapVectorEntry *vec)
     gfree(vec);
 }
 
-void CMap::incRefCnt()
-{
-    ++refCnt;
-}
-
-void CMap::decRefCnt()
-{
-    if (--refCnt == 0) {
-        delete this;
-    }
-}
-
 bool CMap::match(const GooString *collectionA, const GooString *cMapNameA)
 {
     return !collection->cmp(collectionA) && !cMapName->cmp(cMapNameA);
@@ -425,56 +406,32 @@ void CMap::setReverseMap(unsigned int *rmap, unsigned int rmapSize, unsigned int
 
 //------------------------------------------------------------------------
 
-CMapCache::CMapCache()
-{
-    int i;
+CMapCache::CMapCache() { }
 
-    for (i = 0; i < cMapCacheSize; ++i) {
-        cache[i] = nullptr;
-    }
-}
-
-CMapCache::~CMapCache()
-{
-    int i;
-
-    for (i = 0; i < cMapCacheSize; ++i) {
-        if (cache[i]) {
-            cache[i]->decRefCnt();
-        }
-    }
-}
-
-CMap *CMapCache::getCMap(const GooString *collection, const GooString *cMapName)
+std::shared_ptr<CMap> CMapCache::getCMap(const GooString *collection, const GooString *cMapName)
 {
-    CMap *cmap;
     int i, j;
 
     if (cache[0] && cache[0]->match(collection, cMapName)) {
-        cache[0]->incRefCnt();
         return cache[0];
     }
     for (i = 1; i < cMapCacheSize; ++i) {
         if (cache[i] && cache[i]->match(collection, cMapName)) {
-            cmap = cache[i];
+            std::shared_ptr<CMap> cmap = cache[i];
             for (j = i; j >= 1; --j) {
                 cache[j] = cache[j - 1];
             }
             cache[0] = cmap;
-            cmap->incRefCnt();
             return cmap;
         }
     }
-    if ((cmap = CMap::parse(this, collection, cMapName))) {
-        if (cache[cMapCacheSize - 1]) {
-            cache[cMapCacheSize - 1]->decRefCnt();
-        }
+    std::shared_ptr<CMap> cmap = CMap::parse(this, collection, cMapName);
+    if (cmap) {
         for (j = cMapCacheSize - 1; j >= 1; --j) {
             cache[j] = cache[j - 1];
         }
         cache[0] = cmap;
-        cmap->incRefCnt();
         return cmap;
     }
-    return nullptr;
+    return {};
 }
diff --git a/poppler/CMap.h b/poppler/CMap.h
index ae19cf4d..9ef876e9 100644
--- a/poppler/CMap.h
+++ b/poppler/CMap.h
@@ -14,7 +14,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2008 Koji Otani <sho at bbr.jp>
-// Copyright (C) 2009, 2018-2020 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2009, 2018-2020, 2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2012, 2017 Adrian Johnson <ajohnson at redneon.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
 //
@@ -26,7 +26,9 @@
 #ifndef CMAP_H
 #define CMAP_H
 
+#include <array>
 #include <atomic>
+#include <memory>
 
 #include "poppler-config.h"
 #include "CharTypes.h"
@@ -44,24 +46,21 @@ class CMap
 public:
     // Parse a CMap from <obj>, which can be a name or a stream.  Sets
     // the initial reference count to 1.  Returns NULL on failure.
-    static CMap *parse(CMapCache *cache, const GooString *collectionA, Object *obj);
+    static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, Object *obj);
 
     // Create the CMap specified by <collection> and <cMapName>.  Sets
     // the initial reference count to 1.  Returns NULL on failure.
-    static CMap *parse(CMapCache *cache, const GooString *collectionA, const GooString *cMapNameA);
+    static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, const GooString *cMapNameA);
 
     // Parse a CMap from <str>.  Sets the initial reference count to 1.
     // Returns NULL on failure.
-    static CMap *parse(CMapCache *cache, const GooString *collectionA, Stream *str);
+    static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, Stream *str);
 
     ~CMap();
 
     CMap(const CMap &) = delete;
     CMap &operator=(const CMap &) = delete;
 
-    void incRefCnt();
-    void decRefCnt();
-
     // Return collection name (<registry>-<ordering>).
     const GooString *getCollection() const { return collection; }
 
@@ -99,7 +98,6 @@ private:
     int wMode; // writing mode (0=horizontal, 1=vertical)
     CMapVectorEntry *vector; // vector for first byte (NULL for
                              //   identity CMap)
-    std::atomic_int refCnt;
 };
 
 //------------------------------------------------------------------------
@@ -110,7 +108,7 @@ class CMapCache
 {
 public:
     CMapCache();
-    ~CMapCache();
+    ~CMapCache() = default;
 
     CMapCache(const CMapCache &) = delete;
     CMapCache &operator=(const CMapCache &) = delete;
@@ -121,10 +119,10 @@ public:
     // Stream is a stream containing the CMap, can be NULL and
     // this means the CMap will be searched in the CMap files
     // Returns NULL on failure.
-    CMap *getCMap(const GooString *collection, const GooString *cMapName);
+    std::shared_ptr<CMap> getCMap(const GooString *collection, const GooString *cMapName);
 
 private:
-    CMap *cache[cMapCacheSize];
+    std::array<std::shared_ptr<CMap>, cMapCacheSize> cache;
 };
 
 #endif
diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc
index 0c04cae4..bd30e892 100644
--- a/poppler/GfxFont.cc
+++ b/poppler/GfxFont.cc
@@ -13,7 +13,7 @@
 // All changes made under the Poppler project to this file are licensed
 // under GPL version 2 or later
 //
-// Copyright (C) 2005, 2006, 2008-2010, 2012, 2014, 2015, 2017-2021 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2005, 2006, 2008-2010, 2012, 2014, 2015, 2017-2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2005, 2006 Kristian Høgsberg <krh at redhat.com>
 // Copyright (C) 2006 Takashi Iwai <tiwai at suse.de>
 // Copyright (C) 2007 Julien Rebetez <julienr at svn.gnome.org>
@@ -1740,7 +1740,6 @@ GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA,
     descent = -0.35;
     fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
     collection = nullptr;
-    cMap = nullptr;
     ctu = nullptr;
     ctuUsesCharCode = true;
     widths.defWidth = 1.0;
@@ -1983,9 +1982,6 @@ GfxCIDFont::~GfxCIDFont()
     if (collection) {
         delete collection;
     }
-    if (cMap) {
-        cMap->decRefCnt();
-    }
     if (ctu) {
         ctu->decRefCnt();
     }
@@ -2228,14 +2224,13 @@ int *GfxCIDFont::getCodeToGIDMap(FoFiTrueType *ff, int *codeToGIDLen)
         for (cmapName = lp->CMaps; *cmapName != nullptr; cmapName++) {
             GooString cname(*cmapName);
 
-            CMap *cnameCMap;
+            std::shared_ptr<CMap> cnameCMap;
             if ((cnameCMap = globalParams->getCMap(getCollection(), &cname)) != nullptr) {
                 if (cnameCMap->getWMode()) {
                     cnameCMap->setReverseMap(vumap, n, 1);
                 } else {
                     cnameCMap->setReverseMap(humap, n, N_UCS_CANDIDATES);
                 }
-                cnameCMap->decRefCnt();
             }
         }
         ff->setupGSUB(lp->scriptTag, lp->languageTag);
diff --git a/poppler/GfxFont.h b/poppler/GfxFont.h
index e8cb4e3d..6cb3514b 100644
--- a/poppler/GfxFont.h
+++ b/poppler/GfxFont.h
@@ -13,7 +13,7 @@
 // All changes made under the Poppler project to this file are licensed
 // under GPL version 2 or later
 //
-// Copyright (C) 2005, 2008, 2015, 2017-2021 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2005, 2008, 2015, 2017-2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2006 Takashi Iwai <tiwai at suse.de>
 // Copyright (C) 2006 Kristian Høgsberg <krh at redhat.com>
 // Copyright (C) 2007 Julien Rebetez <julienr at svn.gnome.org>
@@ -34,6 +34,7 @@
 #ifndef GFXFONT_H
 #define GFXFONT_H
 
+#include <memory>
 #include <optional>
 
 #include "goo/GooString.h"
@@ -433,7 +434,7 @@ private:
     double getWidth(CID cid) const; // Get width of a character.
 
     GooString *collection; // collection name
-    CMap *cMap; // char code --> CID
+    std::shared_ptr<CMap> cMap; // char code --> CID
     CharCodeToUnicode *ctu; // CID --> Unicode
     bool ctuUsesCharCode; // true: ctu maps char code to Unicode;
                           //   false: ctu maps CID to Unicode
diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index 916f8375..8082a571 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -1198,7 +1198,7 @@ const UnicodeMap *GlobalParams::getUnicodeMap(const std::string &encodingName)
     return map;
 }
 
-CMap *GlobalParams::getCMap(const GooString *collection, const GooString *cMapName)
+std::shared_ptr<CMap> GlobalParams::getCMap(const GooString *collection, const GooString *cMapName)
 {
     cMapCacheLocker();
     return cMapCache->getCMap(collection, cMapName);
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index 6a19d687..bbe759bf 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -13,7 +13,7 @@
 // All changes made under the Poppler project to this file are licensed
 // under GPL version 2 or later
 //
-// Copyright (C) 2005, 2007-2010, 2012, 2015, 2017-2021 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2005, 2007-2010, 2012, 2015, 2017-2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2005 Jonathan Blandford <jrb at redhat.com>
 // Copyright (C) 2006 Takashi Iwai <tiwai at suse.de>
 // Copyright (C) 2006 Kristian Høgsberg <krh at redhat.com>
@@ -118,7 +118,7 @@ public:
 
     CharCodeToUnicode *getCIDToUnicode(const GooString *collection);
     const UnicodeMap *getUnicodeMap(const std::string &encodingName);
-    CMap *getCMap(const GooString *collection, const GooString *cMapName);
+    std::shared_ptr<CMap> getCMap(const GooString *collection, const GooString *cMapName);
     const UnicodeMap *getTextEncoding();
 
     const UnicodeMap *getUtf8Map();


More information about the poppler mailing list