Commit 244e5c1f authored by Brad Hards's avatar Brad Hards

Merge the Goo* improvements from xpdf 3.0.1. This change is based on

martink's work (7-xpdf-3.01-goo-improvements.patch), with some tweaking
by me.
parent eb91b274
2005-08-28 Brad Hards <bradh@frogmouth.net>
* fofi/FoFiTrueType.cc:
* goo/GooHash.cc:
* goo/GooHash.h:
* goo/GooList.cc:
* goo/GooList.h:
* goo/GooString.cc:
* goo/GooString.h:
* goo/gmem.c: merge the Goo* improvements from xpdf 3.0.1. This
change is based on martink's work (7-xpdf-3.01-goo-improvements.patch)
with some tweaking by me.
2005-08-27 Jeff Muizelaar <jeff@infidigm.net>
* poppler/FlateStream.cc: Fix predictor leak.
......
......@@ -13,6 +13,7 @@
#endif
#include <stdlib.h>
#include <string.h>
#include "goo/gtypes.h"
#include "goo/gmem.h"
#include "goo/GooString.h"
......
......@@ -101,6 +101,30 @@ void GooHash::add(GooString *key, int val) {
++len;
}
void GooHash::replace(GooString *key, void *val) {
GooHashBucket *p;
int h;
if ((p = find(key, &h))) {
p->val.p = val;
delete key;
} else {
add(key, val);
}
}
void GooHash::replace(GooString *key, int val) {
GooHashBucket *p;
int h;
if ((p = find(key, &h))) {
p->val.i = val;
delete key;
} else {
add(key, val);
}
}
void *GooHash::lookup(GooString *key) {
GooHashBucket *p;
int h;
......
......@@ -28,6 +28,8 @@ public:
~GooHash();
void add(GooString *key, void *val);
void add(GooString *key, int val);
void replace(GooString *key, void *val);
void replace(GooString *key, int val);
void *lookup(GooString *key);
int lookupInt(GooString *key);
void *lookup(char *key);
......
......@@ -12,6 +12,7 @@
#pragma implementation
#endif
#include <stdlib.h>
#include <string.h>
#include "gmem.h"
#include "GooList.h"
......@@ -81,6 +82,10 @@ void *GooList::del(int i) {
return p;
}
void GooList::sort(int (*cmp)(const void *obj1, const void *obj2)) {
qsort(data, length, sizeof(void *), cmp);
}
void GooList::expand() {
size += (inc > 0) ? inc : size;
data = (void **)greallocn(data, size, sizeof(void*));
......
......@@ -56,6 +56,11 @@ public:
// Assumes 0 <= i < length.
void *del(int i);
// Sort the list accoring to the given comparison function.
// NB: this sorts an array of pointers, so the pointer args need to
// be double-dereferenced.
void sort(int (*cmp)(const void *ptr1, const void *ptr2));
//----- control
// Set allocation increment to <inc>. If inc > 0, that many
......
......@@ -35,7 +35,12 @@ inline void GooString::resize(int length1) {
s = new char[size(length1)];
} else if (size(length1) != size(length)) {
s1 = new char[size(length1)];
memcpy(s1, s, length + 1);
if (length1 < length) {
memcpy(s1, s, length1);
s1[length1] = '\0';
} else {
memcpy(s1, s, length + 1);
}
delete[] s;
s = s1;
}
......@@ -234,3 +239,81 @@ GooString *GooString::lowerCase() {
}
return this;
}
int GooString::cmp(GooString *str) {
int n1, n2, i, x;
char *p1, *p2;
n1 = length;
n2 = str->length;
for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) {
x = *p1 - *p2;
if (x != 0) {
return x;
}
}
return n1 - n2;
}
int GooString::cmpN(GooString *str, int n) {
int n1, n2, i, x;
char *p1, *p2;
n1 = length;
n2 = str->length;
for (i = 0, p1 = s, p2 = str->s;
i < n1 && i < n2 && i < n;
++i, ++p1, ++p2) {
x = *p1 - *p2;
if (x != 0) {
return x;
}
}
if (i == n) {
return 0;
}
return n1 - n2;
}
int GooString::cmp(const char *sA) {
int n1, i, x;
const char *p1, *p2;
n1 = length;
for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) {
x = *p1 - *p2;
if (x != 0) {
return x;
}
}
if (i < n1) {
return 1;
}
if (*p2) {
return -1;
}
return 0;
}
int GooString::cmpN(const char *sA, int n) {
int n1, i, x;
const char *p1, *p2;
n1 = length;
for (i = 0, p1 = s, p2 = sA; i < n1 && *p2 && i < n; ++i, ++p1, ++p2) {
x = *p1 - *p2;
if (x != 0) {
return x;
}
}
if (i == n) {
return 0;
}
if (i < n1) {
return 1;
}
if (*p2) {
return -1;
}
return 0;
}
......@@ -15,8 +15,6 @@
#pragma interface
#endif
#include <string.h>
class GooString {
public:
......@@ -81,11 +79,10 @@ public:
GooString *lowerCase();
// Compare two strings: -1:< 0:= +1:>
// These functions assume the strings do not contain null characters.
int cmp(GooString *str) { return strcmp(s, str->getCString()); }
int cmpN(GooString *str, int n) { return strncmp(s, str->getCString(), n); }
int cmp(const char *sA) { return strcmp(s, sA); }
int cmpN(const char *sA, int n) { return strncmp(s, sA, n); }
int cmp(GooString *str);
int cmpN(GooString *str, int n);
int cmp(const char *sA);
int cmpN(const char *sA, int n);
private:
......
......@@ -50,6 +50,7 @@ static GMemHdr *gMemList[gMemNLists] = {
static int gMemIndex = 0;
static int gMemAlloc = 0;
static int gMemInUse = 0;
#endif /* DEBUG_MEM */
......@@ -78,6 +79,7 @@ void *gmalloc(size_t size) {
hdr->next = gMemList[lst];
gMemList[lst] = hdr;
++gMemAlloc;
gMemInUse += size;
for (p = (unsigned long *)data; p <= trl; ++p)
*p = gMemDeadVal;
return data;
......@@ -178,6 +180,7 @@ void gfree(void *p) {
else
gMemList[lst] = hdr->next;
--gMemAlloc;
gMemInUse -= hdr->size;
size = gMemDataSize(hdr->size);
trl = (unsigned long *)((char *)hdr + gMemHdrSize + size);
if (*trl != gMemDeadVal) {
......
......@@ -12,6 +12,7 @@
#pragma implementation
#endif
#include <string.h>
#include "goo/gmem.h"
#include "Decrypt.h"
......
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