[poppler] poppler/Annot.cc poppler/Annot.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Oct 2 15:59:21 UTC 2018


 poppler/Annot.cc |   44 +++++++++++++-------------------------------
 poppler/Annot.h  |    9 ++++-----
 2 files changed, 17 insertions(+), 36 deletions(-)

New commits:
commit 139f2a4c1ab53fd5eb339e395a7c068e3622f83f
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Thu Aug 30 23:58:37 2018 +0200

    Port the implementation of 'class Annots' to std::vector
    
    This simplifies the code a little: most of the memory management
    code disappears, because std::vector does it for us.

diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 75320836..0ec9e22c 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -41,7 +41,7 @@
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
 // Copyright (C) 2018 Dileep Sankhla <sankhla.dileep96 at gmail.com>
 // Copyright (C) 2018 Tobias Deiminger <haxtibal at posteo.de>
-// Copyright (C) Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2018 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -6816,9 +6816,6 @@ Annots::Annots(PDFDoc *docA, int page, Object *annotsObj) {
   int i;
 
   doc = docA;
-  annots = nullptr;
-  size = 0;
-  nAnnots = 0;
 
   if (annotsObj->isArray()) {
     for (i = 0; i < annotsObj->arrayGetLength(); ++i) {
@@ -6843,30 +6840,20 @@ Annots::Annots(PDFDoc *docA, int page, Object *annotsObj) {
 
 void Annots::appendAnnot(Annot *annot) {
   if (annot && annot->isOk()) {
-    if (nAnnots >= size) {
-      size += 16;
-      annots = (Annot **)greallocn(annots, size, sizeof(Annot *));
-    }
-    annots[nAnnots++] = annot;
+    annots.push_back(annot);
     annot->incRefCnt();
   }
 }
 
 GBool Annots::removeAnnot(Annot *annot) {
-  int idx = -1;
-  // Search annot and determine its index
-  for (int i = 0; idx == -1 && i < nAnnots; i++) {
-    if (annots[i] == annot) {
-      idx = i;
-    }
-  }
-  if (idx == -1) {
-    return gFalse;
+  auto idx = std::find(annots.begin(), annots.end(), annot);
+
+  if (idx == annots.end()) {
+    return false;
   } else {
-    --nAnnots;
-    memmove( annots + idx, annots + idx + 1, sizeof(annots[0]) * (nAnnots - idx) );
     annot->decRefCnt();
-    return gTrue;
+    annots.erase(idx);
+    return true;
   }
 }
 
@@ -6957,11 +6944,9 @@ Annot *Annots::createAnnot(Object* dictObject, Object *obj) {
 }
 
 Annot *Annots::findAnnot(Ref *ref) {
-  int i;
-
-  for (i = 0; i < nAnnots; ++i) {
-    if (annots[i]->match(ref)) {
-      return annots[i];
+  for (auto* annot : annots) {
+    if (annot->match(ref)) {
+      return annot;
     }
   }
   return nullptr;
@@ -6969,12 +6954,9 @@ Annot *Annots::findAnnot(Ref *ref) {
 
 
 Annots::~Annots() {
-  int i;
-
-  for (i = 0; i < nAnnots; ++i) {
-    annots[i]->decRefCnt();
+  for (auto* annot : annots) {
+    annot->decRefCnt();
   }
-  gfree(annots);
 }
 
 
diff --git a/poppler/Annot.h b/poppler/Annot.h
index e4c5d65b..7934c4f0 100644
--- a/poppler/Annot.h
+++ b/poppler/Annot.h
@@ -29,7 +29,7 @@
 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info at kdab.com>. Work sponsored by the LiMux project of the city of Munich
 // Copyright (C) 2018 Dileep Sankhla <sankhla.dileep96 at gmail.com>
 // Copyright (C) 2018 Tobias Deiminger <haxtibal at posteo.de>
-// Copyright (C) Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2018 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -46,6 +46,7 @@
 #include <memory>
 #include <atomic>
 #include <mutex>
+#include <vector>
 
 #include "Object.h"
 
@@ -1684,7 +1685,7 @@ public:
   Annots& operator=(const Annots &) = delete;
 
   // Iterate through list of annotations.
-  int getNumAnnots() { return nAnnots; }
+  int getNumAnnots() const { return annots.size(); }
   Annot *getAnnot(int i) { return annots[i]; }
   void appendAnnot(Annot *annot);
   GBool removeAnnot(Annot *annot);
@@ -1694,9 +1695,7 @@ private:
   Annot *findAnnot(Ref *ref);
 
   PDFDoc *doc;
-  Annot **annots;
-  int nAnnots;
-  int size;
+  std::vector<Annot*> annots;
 };
 
 #endif


More information about the poppler mailing list