Commit 873f6d04 authored by Adam Reichold's avatar Adam Reichold

Also simplify the Array implementation by rebasing it on std::vector<Object>.

parent 51f1c813
Pipeline #3904 passed with stage
in 5 minutes and 44 seconds
......@@ -31,10 +31,8 @@
#pragma implementation
#endif
#include <stdlib.h>
#include <stddef.h>
#include <cassert>
#include "goo/gmem.h"
#include "Object.h"
#include "Array.h"
......@@ -49,8 +47,6 @@
Array::Array(XRef *xrefA) {
xref = xrefA;
elems = nullptr;
size = length = 0;
ref = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
......@@ -58,11 +54,6 @@ Array::Array(XRef *xrefA) {
}
Array::~Array() {
int i;
for (i = 0; i < length; ++i)
elems[i].free();
gfree(elems);
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
......@@ -71,58 +62,36 @@ Array::~Array() {
Object Array::copy(XRef *xrefA) const {
arrayLocker();
Array *a = new Array(xrefA);
for (int i = 0; i < length; ++i) {
a->add(elems[i].copy());
a->elems.reserve(elems.size());
for (const auto& elem : elems) {
a->elems.push_back(elem.copy());
}
return Object(a);
}
int Array::incRef() {
arrayLocker();
++ref;
return ref;
}
int Array::decRef() {
arrayLocker();
--ref;
return ref;
}
void Array::add(Object &&elem) {
arrayLocker();
if (length == size) {
if (length == 0) {
size = 8;
} else {
size *= 2;
}
elems = (Object *)greallocn(elems, size, sizeof(Object));
}
elems[length].initNullAfterMalloc();
elems[length] = std::move(elem);
++length;
elems.push_back(std::move(elem));
}
void Array::remove(int i) {
arrayLocker();
if (i < 0 || i >= length) {
assert(i >= 0 && i < length);
if (i < 0 || std::size_t(i) >= elems.size()) {
assert(i >= 0 && std::size_t(i) < elems.size());
return;
}
--length;
memmove( static_cast<void*>(elems + i), elems + i + 1, sizeof(elems[0]) * (length - i) );
elems.erase(elems.begin() + i);
}
Object Array::get(int i, int recursion) const {
if (i < 0 || i >= length) {
if (i < 0 || std::size_t(i) >= elems.size()) {
return Object(objNull);
}
return elems[i].fetch(xref, recursion);
}
Object Array::getNF(int i) const {
if (i < 0 || i >= length) {
if (i < 0 || std::size_t(i) >= elems.size()) {
return Object(objNull);
}
return elems[i].copy();
......
......@@ -31,6 +31,9 @@
#pragma interface
#endif
#include <atomic>
#include <vector>
#include "poppler-config.h"
#include "Object.h"
#include "goo/GooMutex.h"
......@@ -54,7 +57,7 @@ public:
Array& operator=(const Array &) = delete;
// Get number of elements.
int getLength() const { return length; }
int getLength() const { return elems.size(); }
// Copy array with new xref
Object copy(XRef *xrefA) const;
......@@ -75,14 +78,12 @@ private:
friend class Object; // for incRef/decRef
// Reference counting.
int incRef();
int decRef();
int incRef() { return ++ref; }
int decRef() { return --ref; }
XRef *xref; // the xref table for this PDF file
Object *elems; // array of elements
int size; // size of <elems> array
int length; // number of elements in array
int ref; // reference count
std::vector<Object> elems; // array of elements
std::atomic_int ref; // reference count
#ifdef MULTITHREADED
mutable GooMutex mutex;
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment