1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* stringbuf.h: interface for the String buffering module.
*
* daniel@veillard.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "stringbuf.h"
#include "memory.h"
/*
* Not the default, too heavy on CPU ...
*/
#ifdef WITH_STRING_BUF
#define CLEANUP_FREQUENCY 2000
/*
* An hash table for the Strings
*/
#define HASH_SIZE 65536
typedef struct stringHash {
struct stringHash *next; /* next clash in the hash table */
int use; /* number of references */
const char *val; /* the string value */
} stringHash, *stringHashPtr;
stringHashPtr stringHashTable[HASH_SIZE];
/*
* Hash table initialization.
*/
static int stringHashInitialized = 0;
static void stringHashInitialize(void) {
int i;
for (i = 0; i < HASH_SIZE; i++) {
stringHashTable[i] = NULL;
}
stringHashInitialized = 1;
}
/*
* Compute an hash key
*/
static int stringGetHash(const char *str) {
unsigned short res = 0;
while (*str != 0) res += *(str++);
return((int) (res % HASH_SIZE));
}
/*
* Cleanup the hash table for unused entries
*/
void stringTableCleanup(void) {
int key;
int used = 0, entries = 0;
stringHashPtr cur;
stringHashPtr prev;
for (key =0;key < HASH_SIZE;key++) {
if (stringHashTable[key] != NULL) used++;
while (stringHashTable[key] != NULL) {
if (stringHashTable[key]->use == 0) {
cur = stringHashTable[key]->next;
stringHashTable[key]->val = NULL;
stringHashTable[key]->use = 0;
stringHashTable[key]->next = NULL;
xmlFree(stringHashTable[key]);
stringHashTable[key] = cur;
} else {
entries++;
prev = stringHashTable[key];
cur = stringHashTable[key]->next;
while (cur != NULL) {
if (cur->use == 0) {
xmlFree(cur->val);
prev->next = cur->next;
cur->next = NULL;
xmlFree(cur);
cur = prev->next;
} else {
entries++;
prev = cur;
cur = cur->next;
}
}
goto next_key;
}
}
next_key:
}
printf("String Buffer: %d entries, %d hash\n", entries, used);
}
/*
* Duplicate an "external" string. Search the string in the
* list if not found, add it, otherwise increment the counter.
*/
const char *stringAdd(const char *str) {
int key;
stringHashPtr cur;
static int toCleanup = CLEANUP_FREQUENCY;
if (stringHashInitialized == 0) stringHashInitialize();
toCleanup--;
if (toCleanup <= 0) {
toCleanup = CLEANUP_FREQUENCY;
stringTableCleanup();
}
key = stringGetHash(str);
cur = stringHashTable[key];
while (cur != NULL) {
if (!strcmp(str, cur->val)) {
cur->use++;
return(cur->val);
}
cur = cur->next;
}
cur = xmlMalloc(sizeof(stringHash));
cur->val = xmlStrdup(str);
cur->use = 1;
cur->next = stringHashTable[key];
stringHashTable[key] = cur;
return(cur->val);
}
/*
* Free a duplicate of the string. Decrement the counter, if zero,
* free the string and remove it from the list.
*/
void stringFree(const char *str) {
int key;
stringHashPtr cur;
key = stringGetHash(str);
cur = stringHashTable[key];
while (cur != NULL) {
if (!strcmp(str, cur->val)) {
cur->use--;
/* TODO : garbage collect ... */
return;
}
cur = cur->next;
}
printf("stringFree : %s not found !\n", str);
}
#else /* ! WITH_STRING_BUF */
const char *stringAdd(const char *str) {
return(xmlStrdup(str));
}
void stringFree(const char *str) {
xmlFree((char *) str);
}
#endif /* !WITH_STRING_BUF */
|