4 #include <rpm/rpmstring.h>
5 #include <rpm/rpmstrpool.h>
8 #define STRDATA_CHUNKS 1024
9 #define STRDATA_CHUNK 65536
10 #define STROFFS_CHUNK 2048
11 /* XXX this is ridiculously small... */
12 #define STRHASH_INITSIZE 1024
14 static int pool_debug = 0;
16 typedef struct poolHash_s * poolHash;
17 typedef struct poolHashBucket_s poolHashBucket;
19 struct poolHashBucket_s {
25 poolHashBucket * buckets;
30 const char ** offs; /* pointers into data area */
31 rpmsid offs_size; /* largest offset index */;
32 rpmsid offs_alloced; /* offsets allocation size */
34 char ** chunks; /* memory chunks for storing the strings */
35 size_t chunks_size; /* current chunk */
36 size_t chunks_allocated; /* allocated size of the chunks array */
37 size_t chunk_allocated; /* size of the current chunk */
38 size_t chunk_used; /* usage of the current chunk */
40 poolHash hash; /* string -> sid hash table */
41 int frozen; /* are new id additions allowed? */
42 int nrefs; /* refcount */
45 /* calculate hash and string length on at once */
46 static inline unsigned int rstrlenhash(const char * str, size_t * len)
48 /* Jenkins One-at-a-time hash */
49 unsigned int hash = 0xe4721b68;
68 static inline unsigned int rstrnlenhash(const char * str, size_t n, size_t * len)
70 /* Jenkins One-at-a-time hash */
71 unsigned int hash = 0xe4721b68;
74 while (*s != '\0' && n-- > 0) {
90 static inline unsigned int rstrnhash(const char * string, size_t n)
92 return rstrnlenhash(string, n, NULL);
95 unsigned int rstrhash(const char * string)
97 return rstrlenhash(string, NULL);
100 static inline unsigned int hashbucket(unsigned int hash, unsigned int number)
102 return hash + number*number;
105 static poolHash poolHashCreate(int numBuckets)
109 ht = xmalloc(sizeof(*ht));
110 ht->numBuckets = numBuckets;
111 ht->buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
116 static void poolHashResize(rpmstrPool pool, int numBuckets)
118 poolHash ht = pool->hash;
119 poolHashBucket * buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
121 for (int i=0; i<ht->numBuckets; i++) {
122 if (!ht->buckets[i].keyid) continue;
123 unsigned int keyHash = rstrhash(rpmstrPoolStr(pool, ht->buckets[i].keyid));
124 for (unsigned int j=0;;j++) {
125 unsigned int hash = hashbucket(keyHash, j) % numBuckets;
126 if (!buckets[hash].keyid) {
127 buckets[hash].keyid = ht->buckets[i].keyid;
132 free((void *)(ht->buckets));
133 ht->buckets = buckets;
134 ht->numBuckets = numBuckets;
137 static void poolHashAddHEntry(rpmstrPool pool, const char * key, unsigned int keyHash, rpmsid keyid)
139 poolHash ht = pool->hash;
141 /* keep load factor between 0.25 and 0.5 */
142 if (2*(ht->keyCount) > ht->numBuckets) {
143 poolHashResize(pool, ht->numBuckets * 2);
146 for (unsigned int i=0;;i++) {
147 unsigned int hash = hashbucket(keyHash, i) % ht->numBuckets;
148 if (!ht->buckets[hash].keyid) {
149 ht->buckets[hash].keyid = keyid;
152 } else if (!strcmp(rpmstrPoolStr(pool, ht->buckets[hash].keyid), key)) {
158 static void poolHashAddEntry(rpmstrPool pool, const char * key, rpmsid keyid)
160 poolHashAddHEntry(pool, key, rstrhash(key), keyid);
163 static void poolHashEmpty( poolHash ht)
167 if (ht->keyCount == 0) return;
169 for (i = 0; i < ht->numBuckets; i++) {
170 ht->buckets[i].keyid = 0;
175 static poolHash poolHashFree(poolHash ht)
180 ht->buckets = _free(ht->buckets);
186 static void poolHashPrintStats(rpmstrPool pool)
188 poolHash ht = pool->hash;
191 int maxcollisions = 0;
193 for (i=0; i<ht->numBuckets; i++) {
194 unsigned int keyHash = rstrhash(rpmstrPoolStr(pool, ht->buckets[i].keyid));
195 for (unsigned int j=0;;j++) {
196 unsigned int hash = hashbucket(keyHash, i) % ht->numBuckets;
199 maxcollisions = maxcollisions > j ? maxcollisions : j;
204 fprintf(stderr, "Hashsize: %i\n", ht->numBuckets);
205 fprintf(stderr, "Keys: %i\n", ht->keyCount);
206 fprintf(stderr, "Collisions: %i\n", collisions);
207 fprintf(stderr, "Max collisions: %i\n", maxcollisions);
210 static void rpmstrPoolRehash(rpmstrPool pool)
214 if (pool->offs_size < STRHASH_INITSIZE)
215 sizehint = STRHASH_INITSIZE;
217 sizehint = pool->offs_size * 2;
220 pool->hash = poolHashFree(pool->hash);
222 pool->hash = poolHashCreate(sizehint);
223 for (int i = 1; i <= pool->offs_size; i++)
224 poolHashAddEntry(pool, rpmstrPoolStr(pool, i), i);
227 rpmstrPool rpmstrPoolCreate(void)
229 rpmstrPool pool = xcalloc(1, sizeof(*pool));
231 pool->offs_alloced = STROFFS_CHUNK;
232 pool->offs = xcalloc(pool->offs_alloced, sizeof(*pool->offs));
234 pool->chunks_allocated = STRDATA_CHUNKS;
235 pool->chunks = xcalloc(pool->chunks_allocated, sizeof(*pool->chunks));
236 pool->chunks_size = 1;
237 pool->chunk_allocated = STRDATA_CHUNK;
238 pool->chunks[pool->chunks_size] = xcalloc(1, pool->chunk_allocated);
239 pool->offs[1] = pool->chunks[pool->chunks_size];
241 rpmstrPoolRehash(pool);
246 rpmstrPool rpmstrPoolFree(rpmstrPool pool)
249 if (pool->nrefs > 1) {
253 poolHashPrintStats(pool);
254 poolHashFree(pool->hash);
256 for (int i=1;i<=pool->chunks_size;i++) {
257 pool->chunks[i] = _free(pool->chunks[i]);
266 rpmstrPool rpmstrPoolLink(rpmstrPool pool)
273 void rpmstrPoolFreeze(rpmstrPool pool, int keephash)
275 if (pool && !pool->frozen) {
277 pool->hash = poolHashFree(pool->hash);
279 pool->offs_alloced = pool->offs_size + 2; /* space for end marker */
280 pool->offs = xrealloc(pool->offs,
281 pool->offs_alloced * sizeof(*pool->offs));
286 void rpmstrPoolUnfreeze(rpmstrPool pool)
289 if (pool->hash == NULL) {
290 rpmstrPoolRehash(pool);
296 static rpmsid rpmstrPoolPut(rpmstrPool pool, const char *s, size_t slen, unsigned int hash)
299 size_t ssize = slen + 1;
301 pool->offs_size += 1;
302 if (pool->offs_alloced <= pool->offs_size) {
303 pool->offs_alloced += STROFFS_CHUNK;
304 pool->offs = xrealloc(pool->offs,
305 pool->offs_alloced * sizeof(*pool->offs));
308 /* Do we need a new chunk to store the string? */
309 if (ssize > pool->chunk_allocated - pool->chunk_used) {
310 pool->chunks_size += 1;
311 /* Grow chunks array if needed */
312 if (pool->chunks_size >= pool->chunks_allocated) {
313 pool->chunks_allocated += pool->chunks_allocated;
314 pool->chunks = xrealloc(pool->chunks,
315 pool->chunks_allocated * sizeof(*pool->chunks));
318 /* Ensure the string fits in the new chunk we're about to allocate */
319 if (ssize > pool->chunk_allocated) {
320 pool->chunk_allocated = 2 * ssize;
323 pool->chunks[pool->chunks_size] = xcalloc(1, pool->chunk_allocated);
324 pool->chunk_used = 0;
327 /* Copy the string into current chunk, ensure termination */
328 t = memcpy(pool->chunks[pool->chunks_size] + pool->chunk_used, s, slen);
330 pool->chunk_used += ssize;
332 /* Actually add the string to the pool */
333 pool->offs[pool->offs_size] = t;
334 poolHashAddHEntry(pool, t, hash, pool->offs_size);
336 return pool->offs_size;
339 static rpmsid rpmstrPoolGet(rpmstrPool pool, const char * key, size_t keylen,
340 unsigned int keyHash)
342 poolHash ht = pool->hash;
346 for (unsigned int i=0;; i++) {
347 unsigned int hash = hashbucket(keyHash, i) % ht->numBuckets;
349 if (!ht->buckets[hash].keyid) {
353 s = rpmstrPoolStr(pool, ht->buckets[hash].keyid);
354 /* pool string could be longer than keylen, require exact matche */
355 if (strncmp(s, key, keylen) == 0 && s[keylen] == '\0')
356 return ht->buckets[hash].keyid;
360 static inline rpmsid strn2id(rpmstrPool pool, const char *s, size_t slen,
361 unsigned int hash, int create)
365 if (pool && pool->hash) {
366 sid = rpmstrPoolGet(pool, s, slen, hash);
367 if (sid == 0 && create && !pool->frozen)
368 sid = rpmstrPoolPut(pool, s, slen, hash);
373 rpmsid rpmstrPoolIdn(rpmstrPool pool, const char *s, size_t slen, int create)
378 unsigned int hash = rstrnhash(s, slen);
379 sid = strn2id(pool, s, slen, hash, create);
384 rpmsid rpmstrPoolId(rpmstrPool pool, const char *s, int create)
390 unsigned int hash = rstrlenhash(s, &slen);
391 sid = strn2id(pool, s, slen, hash, create);
396 const char * rpmstrPoolStr(rpmstrPool pool, rpmsid sid)
398 const char *s = NULL;
399 if (pool && sid > 0 && sid <= pool->offs_size)
404 size_t rpmstrPoolStrlen(rpmstrPool pool, rpmsid sid)
407 if (pool && sid > 0 && sid <= pool->offs_size) {
408 slen = strlen(pool->offs[sid]);
413 int rpmstrPoolStreq(rpmstrPool poolA, rpmsid sidA,
414 rpmstrPool poolB, rpmsid sidB)
417 return (sidA == sidB);
419 return rstreq(rpmstrPoolStr(poolA, sidA), rpmstrPoolStr(poolB, sidB));
422 rpmsid rpmstrPoolNumStr(rpmstrPool pool)
424 return (pool != NULL) ? pool->offs_size : 0;