[poppler] poppler/GlobalParams.cc poppler/GlobalParams.h poppler/MarkedContentOutputDev.cc poppler/MarkedContentOutputDev.h poppler/PSOutputDev.cc poppler/TextOutputDev.cc poppler/UnicodeMap.cc poppler/UnicodeMap.h poppler/UTF.cc utils/HtmlFonts.cc utils/JSInfo.cc utils/JSInfo.h utils/pdfdetach.cc utils/pdfinfo.cc utils/pdftotext.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sun Jan 5 11:49:47 UTC 2020


 poppler/GlobalParams.cc           |    8 +---
 poppler/GlobalParams.h            |    6 +--
 poppler/MarkedContentOutputDev.cc |    2 -
 poppler/MarkedContentOutputDev.h  |    2 -
 poppler/PSOutputDev.cc            |    8 +---
 poppler/TextOutputDev.cc          |   21 +++----------
 poppler/UTF.cc                    |    2 -
 poppler/UnicodeMap.cc             |   61 +++++---------------------------------
 poppler/UnicodeMap.h              |   16 +++------
 utils/HtmlFonts.cc                |    3 -
 utils/JSInfo.cc                   |    2 -
 utils/JSInfo.h                    |    4 +-
 utils/pdfdetach.cc                |    3 -
 utils/pdfinfo.cc                  |   11 +++---
 utils/pdftotext.cc                |    7 +---
 15 files changed, 42 insertions(+), 114 deletions(-)

New commits:
commit eb5e7d9a62be5f76718115a058e2e3c4b32eb5ff
Author: Albert Astals Cid <aacid at kde.org>
Date:   Sun Jan 5 01:20:37 2020 +0100

    Remove UnicodeMap reference counting
    
    And make the cache just be "infinite", it's not like we support
    that many maps or that there's so many used in a given session,
    and if they are, well it's good we cached them
    
    All the unicode maps we support use about 2MB of memory, but PSOutputDev
    is the only one that loads "random" unicodeMaps so to load them all
    you'd had to print lots of different documents with fonts with lots of
    different font encodings, so it seems like a not very likely situation
    and the code gets simplified a bit

diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index 403dfbfe..8b69e499 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -593,7 +593,6 @@ UnicodeMap *GlobalParams::getResidentUnicodeMap(const GooString *encodingName) {
   const auto unicodeMap = residentUnicodeMaps.find(encodingName->toStr());
   if (unicodeMap != residentUnicodeMaps.end()) {
     map = &unicodeMap->second;
-    map->incRefCnt();
   }
 
   return map;
@@ -1138,7 +1137,6 @@ const UnicodeMap *GlobalParams::getUtf8Map() {
   if (!utf8Map) {
     GooString enc("UTF-8");
     utf8Map = globalParams->getUnicodeMap(&enc);
-    utf8Map->incRefCnt();
   }
 
   return utf8Map;
@@ -1176,8 +1174,8 @@ CharCodeToUnicode *GlobalParams::getCIDToUnicode(const GooString *collection) {
   return ctu;
 }
 
-UnicodeMap *GlobalParams::getUnicodeMap(const GooString *encodingName) {
-  UnicodeMap *map;
+const UnicodeMap *GlobalParams::getUnicodeMap(const GooString *encodingName) {
+  const UnicodeMap *map;
 
   if (!(map = getResidentUnicodeMap(encodingName))) {
     unicodeMapCacheLocker();
@@ -1192,7 +1190,7 @@ CMap *GlobalParams::getCMap(const GooString *collection, const GooString *cMapNa
   return cMapCache->getCMap(collection, cMapName, stream);
 }
 
-UnicodeMap *GlobalParams::getTextEncoding() {
+const UnicodeMap *GlobalParams::getTextEncoding() {
   return getUnicodeMap(textEncoding);
 }
 
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index e0fa2435..3a99edf9 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -135,9 +135,9 @@ public:
   bool getErrQuiet();
 
   CharCodeToUnicode *getCIDToUnicode(const GooString *collection);
-  UnicodeMap *getUnicodeMap(const GooString *encodingName);
+  const UnicodeMap *getUnicodeMap(const GooString *encodingName);
   CMap *getCMap(const GooString *collection, const GooString *cMapName, Stream *stream = nullptr);
-  UnicodeMap *getTextEncoding();
+  const UnicodeMap *getTextEncoding();
 
   const UnicodeMap *getUtf8Map();
 
@@ -211,7 +211,7 @@ private:
   UnicodeMapCache *unicodeMapCache;
   CMapCache *cMapCache;
 
-  UnicodeMap *utf8Map;
+  const UnicodeMap *utf8Map;
   
   mutable std::recursive_mutex mutex;
   mutable std::recursive_mutex unicodeMapCacheMutex;
diff --git a/poppler/MarkedContentOutputDev.cc b/poppler/MarkedContentOutputDev.cc
index 02135156..225581e4 100644
--- a/poppler/MarkedContentOutputDev.cc
+++ b/poppler/MarkedContentOutputDev.cc
@@ -32,8 +32,6 @@ MarkedContentOutputDev::MarkedContentOutputDev(int mcidA):
 
 MarkedContentOutputDev::~MarkedContentOutputDev()
 {
-  if (unicodeMap)
-    unicodeMap->decRefCnt();
   if (currentFont)
     currentFont->decRefCnt();
   delete currentText;
diff --git a/poppler/MarkedContentOutputDev.h b/poppler/MarkedContentOutputDev.h
index 829a850f..51b1450e 100644
--- a/poppler/MarkedContentOutputDev.h
+++ b/poppler/MarkedContentOutputDev.h
@@ -127,7 +127,7 @@ private:
   std::vector<int> mcidStack;
   double           pageWidth;
   double           pageHeight;
-  UnicodeMap      *unicodeMap;
+  const UnicodeMap *unicodeMap;
 };
 
 #endif /* !MARKEDCONTENTOUTPUTDEV_H */
diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index d3d93119..013b68f7 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -1880,7 +1880,7 @@ void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict) {
   GooString *psName;
   char buf[16];
   bool subst;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   const char *charName;
   double xs, ys;
   int code;
@@ -2048,7 +2048,6 @@ void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict) {
       font16Enc[font16EncLen].fontID = *font->getID();
       if ((uMap = globalParams->getUnicodeMap(fontLoc->encoding))) {
 	font16Enc[font16EncLen].enc = fontLoc->encoding->copy();
-	uMap->decRefCnt();
       } else {
 	error(errSyntaxError, -1,
 	      "Couldn't find Unicode map for 16-bit font encoding '{0:t}'",
@@ -5058,7 +5057,7 @@ void PSOutputDev::drawString(GfxState *state, const GooString *s) {
   GooString *s2;
   double dx, dy, originX, originY;
   const char *p;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   CharCode code;
   const Unicode *u;
   char buf[8];
@@ -5190,9 +5189,6 @@ void PSOutputDev::drawString(GfxState *state, const GooString *s) {
     p += n;
     len -= n;
   }
-  if (uMap) {
-    uMap->decRefCnt();
-  }
 
   if (nChars > 0) {
     writePSString(s2->toStr());
diff --git a/poppler/TextOutputDev.cc b/poppler/TextOutputDev.cc
index 2c630403..0fc5ea6b 100644
--- a/poppler/TextOutputDev.cc
+++ b/poppler/TextOutputDev.cc
@@ -803,7 +803,7 @@ int TextWord::cmpYX(const void *p1, const void *p2) {
 
 GooString *TextWord::getText() const {
   GooString *s;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   char buf[8];
   int n, i;
 
@@ -815,7 +815,6 @@ GooString *TextWord::getText() const {
     n = uMap->mapUnicode(text[i], buf, sizeof(buf));
     s->append(buf, n);
   }
-  uMap->decRefCnt();
   return s;
 }
 
@@ -2818,7 +2817,6 @@ void TextPage::addLink(int xMin, int yMin, int xMax, int yMax, AnnotLink *link)
 }
 
 void TextPage::coalesce(bool physLayout, double fixedPitch, bool doHTML) {
-  UnicodeMap *uMap;
   TextPool *pool;
   TextWord *word0, *word1, *word2;
   TextLine *line;
@@ -2839,7 +2837,7 @@ void TextPage::coalesce(bool physLayout, double fixedPitch, bool doHTML) {
     return;
   }
 
-  uMap = globalParams->getTextEncoding();
+  const UnicodeMap *uMap = globalParams->getTextEncoding();
   blkList = nullptr;
   lastBlk = nullptr;
   nBlocks = 0;
@@ -3884,10 +3882,6 @@ void TextPage::coalesce(bool physLayout, double fixedPitch, bool doHTML) {
   }
   printf("\n");
 #endif
-
-  if (uMap) {
-    uMap->decRefCnt();
-  }
 }
 
 bool TextPage::findText(const Unicode *s, int len,
@@ -4168,7 +4162,7 @@ bool TextPage::findText(const Unicode *s, int len,
 GooString *TextPage::getText(double xMin, double yMin,
 			   double xMax, double yMax, EndOfLineKind textEOL) const {
   GooString *s;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   TextBlock *blk;
   TextLine *line;
   TextLineFrag *frags;
@@ -4400,7 +4394,6 @@ GooString *TextPage::getText(double xMin, double yMin,
   }
 
   gfree(frags);
-  uMap->decRefCnt();
 
   return s;
 }
@@ -4563,7 +4556,7 @@ GooString *TextSelectionDumper::getText ()
 {
   GooString *text;
   int i;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   char space[8], eol[16];
   int spaceLen, eolLen;
 
@@ -4588,8 +4581,6 @@ GooString *TextSelectionDumper::getText ()
       text->append(eol, eolLen);
   }
 
-  uMap->decRefCnt();
-
   return text;
 }
 
@@ -5290,7 +5281,7 @@ bool TextPage::findCharRange(int pos, int length,
 
 void TextPage::dump(void *outputStream, TextOutputFunc outputFunc,
 		    bool physLayout, EndOfLineKind textEOL, bool pageBreaks) {
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   TextFlow *flow;
   TextBlock *blk;
   TextLine *line;
@@ -5461,8 +5452,6 @@ void TextPage::dump(void *outputStream, TextOutputFunc outputFunc,
   if (pageBreaks) {
     (*outputFunc)(outputStream, eop, eopLen);
   }
-
-  uMap->decRefCnt();
 }
 
 void TextPage::setMergeCombining(bool merge) {
diff --git a/poppler/UTF.cc b/poppler/UTF.cc
index b009abf1..37d21c75 100644
--- a/poppler/UTF.cc
+++ b/poppler/UTF.cc
@@ -422,7 +422,7 @@ char *utf16ToUtf8(const uint16_t *utf16, int *len)
 
 struct Ascii7Map
 {
-  UnicodeMap *d;
+  const UnicodeMap *d;
   Ascii7Map()
   {
     GooString enc("ASCII7");
diff --git a/poppler/UnicodeMap.cc b/poppler/UnicodeMap.cc
index 377c9ac0..857e923b 100644
--- a/poppler/UnicodeMap.cc
+++ b/poppler/UnicodeMap.cc
@@ -137,7 +137,6 @@ UnicodeMap::UnicodeMap(const std::string &encodingNameA) {
   len = 0;
   eMaps = nullptr;
   eMapsLen = 0;
-  refCnt = 1;
 }
 
 UnicodeMap::UnicodeMap(const char *encodingNameA, bool unicodeOutA,
@@ -149,7 +148,6 @@ UnicodeMap::UnicodeMap(const char *encodingNameA, bool unicodeOutA,
   len = lenA;
   eMaps = nullptr;
   eMapsLen = 0;
-  refCnt = 1;
 }
 
 UnicodeMap::UnicodeMap(const char *encodingNameA, bool unicodeOutA,
@@ -160,7 +158,6 @@ UnicodeMap::UnicodeMap(const char *encodingNameA, bool unicodeOutA,
   func = funcA;
   eMaps = nullptr;
   eMapsLen = 0;
-  refCnt = 1;
 }
 
 UnicodeMap::~UnicodeMap() {
@@ -179,7 +176,6 @@ UnicodeMap::UnicodeMap(UnicodeMap &&other) noexcept
   , len{other.len}
   , eMaps{other.eMaps}
   , eMapsLen{other.eMapsLen}
-  , refCnt{1}
 {
   switch (kind) {
   case unicodeMapUser:
@@ -245,16 +241,6 @@ void UnicodeMap::swap(UnicodeMap &other) noexcept
   swap(eMapsLen, other.eMapsLen);
 }
 
-void UnicodeMap::incRefCnt() {
-  refCnt.fetch_add(1);
-}
-
-void UnicodeMap::decRefCnt() {
-  if (refCnt.fetch_sub(1) == 1) {
-    delete this;
-  }
-}
-
 bool UnicodeMap::match(const GooString *encodingNameA) const {
   return encodingName == encodingNameA->toStr();
 }
@@ -309,52 +295,23 @@ int UnicodeMap::mapUnicode(Unicode u, char *buf, int bufSize) const {
 //------------------------------------------------------------------------
 
 UnicodeMapCache::UnicodeMapCache() {
-  int i;
-
-  for (i = 0; i < unicodeMapCacheSize; ++i) {
-    cache[i] = nullptr;
-  }
 }
 
 UnicodeMapCache::~UnicodeMapCache() {
-  int i;
-
-  for (i = 0; i < unicodeMapCacheSize; ++i) {
-    if (cache[i]) {
-      cache[i]->decRefCnt();
-    }
+  for (UnicodeMap *map : cache) {
+    delete map;
   }
 }
 
-UnicodeMap *UnicodeMapCache::getUnicodeMap(const GooString *encodingName) {
-  UnicodeMap *map;
-  int i, j;
-
-  if (cache[0] && cache[0]->match(encodingName)) {
-    cache[0]->incRefCnt();
-    return cache[0];
-  }
-  for (i = 1; i < unicodeMapCacheSize; ++i) {
-    if (cache[i] && cache[i]->match(encodingName)) {
-      map = cache[i];
-      for (j = i; j >= 1; --j) {
-	cache[j] = cache[j - 1];
-      }
-      cache[0] = map;
-      map->incRefCnt();
+const UnicodeMap *UnicodeMapCache::getUnicodeMap(const GooString *encodingName) {
+  for (UnicodeMap *map : cache) {
+    if (map->match(encodingName)) {
       return map;
     }
   }
-  if ((map = UnicodeMap::parse(encodingName))) {
-    if (cache[unicodeMapCacheSize - 1]) {
-      cache[unicodeMapCacheSize - 1]->decRefCnt();
-    }
-    for (j = unicodeMapCacheSize - 1; j >= 1; --j) {
-      cache[j] = cache[j - 1];
-    }
-    cache[0] = map;
-    map->incRefCnt();
-    return map;
+  UnicodeMap *map = UnicodeMap::parse(encodingName);
+  if (map) {
+    cache.emplace_back(map);
   }
-  return nullptr;
+  return map;
 }
diff --git a/poppler/UnicodeMap.h b/poppler/UnicodeMap.h
index bf412bf3..2e56c03a 100644
--- a/poppler/UnicodeMap.h
+++ b/poppler/UnicodeMap.h
@@ -30,8 +30,10 @@
 
 #include "poppler-config.h"
 #include "CharTypes.h"
+
 #include <atomic>
 #include <string>
+#include <vector>
 
 class GooString;
 
@@ -80,9 +82,6 @@ public:
   UnicodeMap(const UnicodeMap &) = delete;
   UnicodeMap& operator=(const UnicodeMap &) = delete;
 
-  void incRefCnt();
-  void decRefCnt();
-
   std::string getEncodingName() const { return encodingName; }
 
   bool isUnicode() const { return unicodeOut; }
@@ -111,13 +110,10 @@ private:
   int len;			// (user, resident)
   UnicodeMapExt *eMaps;		// (user)
   int eMapsLen;			// (user)
-  std::atomic_int refCnt;
 };
 
 //------------------------------------------------------------------------
 
-#define unicodeMapCacheSize 4
-
 class UnicodeMapCache {
 public:
 
@@ -127,14 +123,12 @@ public:
   UnicodeMapCache(const UnicodeMapCache &) = delete;
   UnicodeMapCache& operator=(const UnicodeMapCache &) = delete;
 
-  // Get the UnicodeMap for <encodingName>.  Increments its reference
-  // count; there will be one reference for the cache plus one for the
-  // caller of this function.  Returns NULL on failure.
-  UnicodeMap *getUnicodeMap(const GooString *encodingName);
+  // Get the UnicodeMap for <encodingName>.  Returns NULL on failure.
+  const UnicodeMap *getUnicodeMap(const GooString *encodingName);
 
 private:
 
-  UnicodeMap *cache[unicodeMapCacheSize];
+  std::vector<UnicodeMap *> cache;
 };
 
 #endif
diff --git a/utils/HtmlFonts.cc b/utils/HtmlFonts.cc
index ad67c5db..281c718a 100644
--- a/utils/HtmlFonts.cc
+++ b/utils/HtmlFonts.cc
@@ -214,7 +214,7 @@ GooString* HtmlFont::getFullName(){
 // this method if plain wrong todo
 GooString* HtmlFont::HtmlFilter(const Unicode* u, int uLen) {
   GooString *tmp = new GooString();
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   char buf[8];
   int n;
 
@@ -247,7 +247,6 @@ GooString* HtmlFont::HtmlFilter(const Unicode* u, int uLen) {
     }
   }
 
-  uMap->decRefCnt();
   return tmp;
 }
 
diff --git a/utils/JSInfo.cc b/utils/JSInfo.cc
index 7bc9d147..ad15e45a 100644
--- a/utils/JSInfo.cc
+++ b/utils/JSInfo.cc
@@ -92,7 +92,7 @@ void JSInfo::scanJS(int nPages) {
   scan(nPages);
 }
 
-void JSInfo::scanJS(int nPages, FILE *fout, UnicodeMap *uMap) {
+void JSInfo::scanJS(int nPages, FILE *fout, const UnicodeMap *uMap) {
   print = true;
   file = fout;
   uniMap = uMap;
diff --git a/utils/JSInfo.h b/utils/JSInfo.h
index 14a26c62..21615278 100644
--- a/utils/JSInfo.h
+++ b/utils/JSInfo.h
@@ -38,7 +38,7 @@ public:
   void scanJS(int nPages);
 
   // scan and print JS in the PDF
-  void scanJS(int nPages, FILE *fout, UnicodeMap *uMap);
+  void scanJS(int nPages, FILE *fout, const UnicodeMap *uMap);
 
   // return true if PDF contains JavaScript
   bool containsJS();
@@ -50,7 +50,7 @@ private:
   bool hasJS;
   bool print;
   FILE *file;
-  UnicodeMap *uniMap;
+  const UnicodeMap *uniMap;
 
   void scan(int nPages);
   void scanLinkAction(LinkAction *link, const char *action, bool deleteLink = true);
diff --git a/utils/pdfdetach.cc b/utils/pdfdetach.cc
index 54115659..3e152136 100644
--- a/utils/pdfdetach.cc
+++ b/utils/pdfdetach.cc
@@ -83,7 +83,7 @@ static ArgDesc argDesc[] = {
 
 int main(int argc, char *argv[]) {
   GooString *fileName;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   GooString *ownerPW, *userPW;
   PDFDoc *doc;
   char uBuf[8];
@@ -329,7 +329,6 @@ int main(int argc, char *argv[]) {
  err2:
   for (auto& file : embeddedFiles)
     delete file;
-  uMap->decRefCnt();
   delete doc;
  err0:
 
diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc
index 59e812a4..d9e19397 100644
--- a/utils/pdfinfo.cc
+++ b/utils/pdfinfo.cc
@@ -130,7 +130,7 @@ static const ArgDesc argDesc[] = {
 };
 
 static void printInfoString(Dict *infoDict, const char *key, const char *text,
-			    UnicodeMap *uMap) {
+			    const UnicodeMap *uMap) {
   const GooString *s1;
   Unicode *u;
   char buf[8];
@@ -374,7 +374,7 @@ static void printLinkDest(LinkDest *dest) {
   printf("%s", s.c_str());
 }
 
-static void printDestinations(PDFDoc *doc, UnicodeMap *uMap) {
+static void printDestinations(PDFDoc *doc, const UnicodeMap *uMap) {
   std::map<Ref,std::map<GooString*,LinkDest*,GooStringCompare> > map;
 
   int numDests = doc->getCatalog()->numDestNameTree();
@@ -428,7 +428,7 @@ static void printDestinations(PDFDoc *doc, UnicodeMap *uMap) {
   }
 }
 
-static void printPdfSubtype(PDFDoc *doc, UnicodeMap *uMap) {
+static void printPdfSubtype(PDFDoc *doc, const UnicodeMap *uMap) {
   const Object info = doc->getDocInfo();
   if (info.isDict()) {
     const PDFSubtype pdftype = doc->getPDFSubtype();
@@ -663,7 +663,7 @@ static void printPdfSubtype(PDFDoc *doc, UnicodeMap *uMap) {
   }
 }
 
-static void printInfo(PDFDoc *doc, UnicodeMap *uMap, long long filesize, bool multiPage) {
+static void printInfo(PDFDoc *doc, const UnicodeMap *uMap, long long filesize, bool multiPage) {
   Page *page;
   char buf[256];
   double w, h, wISO, hISO, isoThreshold;
@@ -845,7 +845,7 @@ int main(int argc, char *argv[]) {
   PDFDoc *doc;
   GooString *fileName;
   GooString *ownerPW, *userPW;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   FILE *f;
   bool ok;
   int exitCode;
@@ -984,7 +984,6 @@ int main(int argc, char *argv[]) {
 
   // clean up
  err2:
-  uMap->decRefCnt();
   delete doc;
   delete fileName;
  err1:
diff --git a/utils/pdftotext.cc b/utils/pdftotext.cc
index 53f2e131..6a119772 100644
--- a/utils/pdftotext.cc
+++ b/utils/pdftotext.cc
@@ -68,7 +68,7 @@
 #include "Win32Console.h"
 
 static void printInfoString(FILE *f, Dict *infoDict, const char *key,
-			    const char *text1, const char *text2, UnicodeMap *uMap);
+			    const char *text1, const char *text2, const UnicodeMap *uMap);
 static void printInfoDate(FILE *f, Dict *infoDict, const char *key, const char *fmt);
 void printDocBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last);
 void printWordBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last);
@@ -184,7 +184,7 @@ int main(int argc, char *argv[]) {
   GooString *ownerPW, *userPW;
   TextOutputDev *textOut;
   FILE *f;
-  UnicodeMap *uMap;
+  const UnicodeMap *uMap;
   Object info;
   bool ok;
   int exitCode;
@@ -443,7 +443,6 @@ int main(int argc, char *argv[]) {
  err2:
   delete doc;
   delete fileName;
-  uMap->decRefCnt();
  err1:
  err0:
 
@@ -451,7 +450,7 @@ int main(int argc, char *argv[]) {
 }
 
 static void printInfoString(FILE *f, Dict *infoDict, const char *key,
-			    const char *text1, const char *text2, UnicodeMap *uMap) {
+			    const char *text1, const char *text2, const UnicodeMap *uMap) {
   const GooString *s1;
   bool isUnicode;
   Unicode u;


More information about the poppler mailing list