]> git.saurik.com Git - redis.git/blame - src/dict.c
Remove _dictAlloc and friends
[redis.git] / src / dict.c
CommitLineData
ed9b544e 1/* Hash Tables Implementation.
2 *
3 * This file implements in memory hash tables with insert/del/replace/find/
4 * get-random-element operations. Hash tables will auto resize if needed
5 * tables of power of two in size are used, collisions are handled by
6 * chaining. See the source code for more information... :)
7 *
12d090d2 8 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the name of Redis nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
23d4709d 36#include "fmacros.h"
37
ed9b544e 38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <stdarg.h>
42#include <assert.h>
f2923bec 43#include <limits.h>
8ca3e9d1 44#include <sys/time.h>
ed9b544e 45
46#include "dict.h"
47#include "zmalloc.h"
48
884d4b39 49/* Using dictEnableResize() / dictDisableResize() we make possible to
50 * enable/disable resizing of the hash table as needed. This is very important
51 * for Redis, as we use copy-on-write and don't want to move too much memory
52 * around when there is a child performing saving operations. */
53static int dict_can_resize = 1;
54
ed9b544e 55/* -------------------------- private prototypes ---------------------------- */
56
57static int _dictExpandIfNeeded(dict *ht);
f2923bec 58static unsigned long _dictNextPower(unsigned long size);
ed9b544e 59static int _dictKeyIndex(dict *ht, const void *key);
60static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
61
62/* -------------------------- hash functions -------------------------------- */
63
64/* Thomas Wang's 32 bit Mix Function */
65unsigned int dictIntHashFunction(unsigned int key)
66{
67 key += ~(key << 15);
68 key ^= (key >> 10);
69 key += (key << 3);
70 key ^= (key >> 6);
71 key += ~(key << 11);
72 key ^= (key >> 16);
73 return key;
74}
75
76/* Identity hash function for integer keys */
77unsigned int dictIdentityHashFunction(unsigned int key)
78{
79 return key;
80}
81
82/* Generic hash function (a popular one from Bernstein).
83 * I tested a few and this was the best. */
84unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
85 unsigned int hash = 5381;
86
87 while (len--)
88 hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
89 return hash;
90}
91
92/* ----------------------------- API implementation ------------------------- */
93
94/* Reset an hashtable already initialized with ht_init().
95 * NOTE: This function should only called by ht_destroy(). */
5413c40d 96static void _dictReset(dictht *ht)
ed9b544e 97{
98 ht->table = NULL;
99 ht->size = 0;
100 ht->sizemask = 0;
101 ht->used = 0;
102}
103
104/* Create a new hash table */
105dict *dictCreate(dictType *type,
106 void *privDataPtr)
107{
d9dd352b 108 dict *d = zmalloc(sizeof(*d));
ed9b544e 109
5413c40d 110 _dictInit(d,type,privDataPtr);
111 return d;
ed9b544e 112}
113
114/* Initialize the hash table */
5413c40d 115int _dictInit(dict *d, dictType *type,
ed9b544e 116 void *privDataPtr)
117{
5413c40d 118 _dictReset(&d->ht[0]);
119 _dictReset(&d->ht[1]);
120 d->type = type;
121 d->privdata = privDataPtr;
122 d->rehashidx = -1;
123 d->iterators = 0;
ed9b544e 124 return DICT_OK;
125}
126
127/* Resize the table to the minimal size that contains all the elements,
128 * but with the invariant of a USER/BUCKETS ration near to <= 1 */
5413c40d 129int dictResize(dict *d)
ed9b544e 130{
5413c40d 131 int minimal;
ed9b544e 132
5413c40d 133 if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR;
134 minimal = d->ht[0].used;
ed9b544e 135 if (minimal < DICT_HT_INITIAL_SIZE)
136 minimal = DICT_HT_INITIAL_SIZE;
5413c40d 137 return dictExpand(d, minimal);
ed9b544e 138}
139
140/* Expand or create the hashtable */
5413c40d 141int dictExpand(dict *d, unsigned long size)
ed9b544e 142{
5413c40d 143 dictht n; /* the new hashtable */
144 unsigned long realsize = _dictNextPower(size);
ed9b544e 145
146 /* the size is invalid if it is smaller than the number of
147 * elements already inside the hashtable */
5413c40d 148 if (dictIsRehashing(d) || d->ht[0].used > size)
ed9b544e 149 return DICT_ERR;
150
ed9b544e 151 n.size = realsize;
152 n.sizemask = realsize-1;
d9dd352b 153 n.table = zmalloc(realsize*sizeof(dictEntry*));
5413c40d 154 n.used = 0;
ed9b544e 155
156 /* Initialize all the pointers to NULL */
157 memset(n.table, 0, realsize*sizeof(dictEntry*));
158
5413c40d 159 /* Is this the first initialization? If so it's not really a rehashing
160 * we just set the first hash table so that it can accept keys. */
161 if (d->ht[0].table == NULL) {
162 d->ht[0] = n;
163 return DICT_OK;
164 }
ed9b544e 165
5413c40d 166 /* Prepare a second hash table for incremental rehashing */
167 d->ht[1] = n;
168 d->rehashidx = 0;
169 return DICT_OK;
170}
171
172/* Performs N steps of incremental rehashing. Returns 1 if there are still
173 * keys to move from the old to the new hash table, otherwise 0 is returned.
174 * Note that a rehashing step consists in moving a bucket (that may have more
175 * thank one key as we use chaining) from the old to the new hash table. */
176int dictRehash(dict *d, int n) {
177 if (!dictIsRehashing(d)) return 0;
178
179 while(n--) {
180 dictEntry *de, *nextde;
181
182 /* Check if we already rehashed the whole table... */
183 if (d->ht[0].used == 0) {
d9dd352b 184 zfree(d->ht[0].table);
5413c40d 185 d->ht[0] = d->ht[1];
186 _dictReset(&d->ht[1]);
187 d->rehashidx = -1;
188 return 0;
189 }
190
191 /* Note that rehashidx can't overflow as we are sure there are more
192 * elements because ht[0].used != 0 */
193 while(d->ht[0].table[d->rehashidx] == NULL) d->rehashidx++;
194 de = d->ht[0].table[d->rehashidx];
195 /* Move all the keys in this bucket from the old to the new hash HT */
196 while(de) {
ed9b544e 197 unsigned int h;
198
5413c40d 199 nextde = de->next;
200 /* Get the index in the new hash table */
201 h = dictHashKey(d, de->key) & d->ht[1].sizemask;
202 de->next = d->ht[1].table[h];
203 d->ht[1].table[h] = de;
204 d->ht[0].used--;
205 d->ht[1].used++;
206 de = nextde;
ed9b544e 207 }
5413c40d 208 d->ht[0].table[d->rehashidx] = NULL;
209 d->rehashidx++;
ed9b544e 210 }
5413c40d 211 return 1;
212}
ed9b544e 213
8ca3e9d1 214long long timeInMilliseconds(void) {
215 struct timeval tv;
216
217 gettimeofday(&tv,NULL);
218 return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
219}
220
221/* Rehash for an amount of time between ms milliseconds and ms+1 milliseconds */
222int dictRehashMilliseconds(dict *d, int ms) {
223 long long start = timeInMilliseconds();
224 int rehashes = 0;
225
226 while(dictRehash(d,100)) {
227 rehashes += 100;
228 if (timeInMilliseconds()-start > ms) break;
229 }
230 return rehashes;
231}
232
5413c40d 233/* This function performs just a step of rehashing, and only if there are
234 * not iterators bound to our hash table. When we have iterators in the middle
235 * of a rehashing we can't mess with the two hash tables otherwise some element
236 * can be missed or duplicated.
237 *
238 * This function is called by common lookup or update operations in the
239 * dictionary so that the hash table automatically migrates from H1 to H2
240 * while it is actively used. */
241static void _dictRehashStep(dict *d) {
242 if (d->iterators == 0) dictRehash(d,1);
ed9b544e 243}
244
245/* Add an element to the target hash table */
5413c40d 246int dictAdd(dict *d, void *key, void *val)
ed9b544e 247{
248 int index;
249 dictEntry *entry;
5413c40d 250 dictht *ht;
251
252 if (dictIsRehashing(d)) _dictRehashStep(d);
ed9b544e 253
254 /* Get the index of the new element, or -1 if
255 * the element already exists. */
5413c40d 256 if ((index = _dictKeyIndex(d, key)) == -1)
ed9b544e 257 return DICT_ERR;
258
259 /* Allocates the memory and stores key */
5413c40d 260 ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
d9dd352b 261 entry = zmalloc(sizeof(*entry));
ed9b544e 262 entry->next = ht->table[index];
263 ht->table[index] = entry;
5413c40d 264 ht->used++;
ed9b544e 265
266 /* Set the hash entry fields. */
5413c40d 267 dictSetHashKey(d, entry, key);
268 dictSetHashVal(d, entry, val);
ed9b544e 269 return DICT_OK;
270}
271
121796f7 272/* Add an element, discarding the old if the key already exists.
273 * Return 1 if the key was added from scratch, 0 if there was already an
274 * element with such key and dictReplace() just performed a value update
275 * operation. */
5413c40d 276int dictReplace(dict *d, void *key, void *val)
ed9b544e 277{
2069d06a 278 dictEntry *entry, auxentry;
ed9b544e 279
280 /* Try to add the element. If the key
281 * does not exists dictAdd will suceed. */
5413c40d 282 if (dictAdd(d, key, val) == DICT_OK)
121796f7 283 return 1;
ed9b544e 284 /* It already exists, get the entry */
5413c40d 285 entry = dictFind(d, key);
ed9b544e 286 /* Free the old value and set the new one */
2069d06a 287 /* Set the new value and free the old one. Note that it is important
288 * to do that in this order, as the value may just be exactly the same
289 * as the previous one. In this context, think to reference counting,
290 * you want to increment (set), and then decrement (free), and not the
291 * reverse. */
292 auxentry = *entry;
5413c40d 293 dictSetHashVal(d, entry, val);
294 dictFreeEntryVal(d, &auxentry);
121796f7 295 return 0;
ed9b544e 296}
297
298/* Search and remove an element */
5413c40d 299static int dictGenericDelete(dict *d, const void *key, int nofree)
ed9b544e 300{
5413c40d 301 unsigned int h, idx;
ed9b544e 302 dictEntry *he, *prevHe;
5413c40d 303 int table;
ed9b544e 304
5413c40d 305 if (d->ht[0].size == 0) return DICT_ERR; /* d->ht[0].table is NULL */
306 if (dictIsRehashing(d)) _dictRehashStep(d);
307 h = dictHashKey(d, key);
ed9b544e 308
5413c40d 309 for (table = 0; table <= 1; table++) {
310 idx = h & d->ht[table].sizemask;
311 he = d->ht[table].table[idx];
312 prevHe = NULL;
313 while(he) {
314 if (dictCompareHashKeys(d, key, he->key)) {
315 /* Unlink the element from the list */
316 if (prevHe)
317 prevHe->next = he->next;
318 else
319 d->ht[table].table[idx] = he->next;
320 if (!nofree) {
321 dictFreeEntryKey(d, he);
322 dictFreeEntryVal(d, he);
323 }
d9dd352b 324 zfree(he);
5413c40d 325 d->ht[table].used--;
326 return DICT_OK;
ed9b544e 327 }
5413c40d 328 prevHe = he;
329 he = he->next;
ed9b544e 330 }
5413c40d 331 if (!dictIsRehashing(d)) break;
ed9b544e 332 }
333 return DICT_ERR; /* not found */
334}
335
336int dictDelete(dict *ht, const void *key) {
337 return dictGenericDelete(ht,key,0);
338}
339
340int dictDeleteNoFree(dict *ht, const void *key) {
341 return dictGenericDelete(ht,key,1);
342}
343
5413c40d 344/* Destroy an entire dictionary */
345int _dictClear(dict *d, dictht *ht)
ed9b544e 346{
f2923bec 347 unsigned long i;
ed9b544e 348
349 /* Free all the elements */
350 for (i = 0; i < ht->size && ht->used > 0; i++) {
351 dictEntry *he, *nextHe;
352
353 if ((he = ht->table[i]) == NULL) continue;
354 while(he) {
355 nextHe = he->next;
5413c40d 356 dictFreeEntryKey(d, he);
357 dictFreeEntryVal(d, he);
d9dd352b 358 zfree(he);
ed9b544e 359 ht->used--;
360 he = nextHe;
361 }
362 }
363 /* Free the table and the allocated cache structure */
d9dd352b 364 zfree(ht->table);
ed9b544e 365 /* Re-initialize the table */
366 _dictReset(ht);
367 return DICT_OK; /* never fails */
368}
369
370/* Clear & Release the hash table */
5413c40d 371void dictRelease(dict *d)
ed9b544e 372{
5413c40d 373 _dictClear(d,&d->ht[0]);
374 _dictClear(d,&d->ht[1]);
d9dd352b 375 zfree(d);
ed9b544e 376}
377
5413c40d 378dictEntry *dictFind(dict *d, const void *key)
ed9b544e 379{
380 dictEntry *he;
5413c40d 381 unsigned int h, idx, table;
382
383 if (d->ht[0].size == 0) return NULL; /* We don't have a table at all */
384 if (dictIsRehashing(d)) _dictRehashStep(d);
385 h = dictHashKey(d, key);
386 for (table = 0; table <= 1; table++) {
387 idx = h & d->ht[table].sizemask;
388 he = d->ht[table].table[idx];
389 while(he) {
390 if (dictCompareHashKeys(d, key, he->key))
391 return he;
392 he = he->next;
393 }
394 if (!dictIsRehashing(d)) return NULL;
ed9b544e 395 }
396 return NULL;
397}
398
58e1c9c1 399void *dictFetchValue(dict *d, const void *key) {
400 dictEntry *he;
401
402 he = dictFind(d,key);
403 return he ? dictGetEntryVal(he) : NULL;
404}
405
5413c40d 406dictIterator *dictGetIterator(dict *d)
ed9b544e 407{
d9dd352b 408 dictIterator *iter = zmalloc(sizeof(*iter));
ed9b544e 409
5413c40d 410 iter->d = d;
411 iter->table = 0;
ed9b544e 412 iter->index = -1;
413 iter->entry = NULL;
414 iter->nextEntry = NULL;
415 return iter;
416}
417
418dictEntry *dictNext(dictIterator *iter)
419{
420 while (1) {
421 if (iter->entry == NULL) {
5413c40d 422 dictht *ht = &iter->d->ht[iter->table];
423 if (iter->index == -1 && iter->table == 0) iter->d->iterators++;
ed9b544e 424 iter->index++;
5413c40d 425 if (iter->index >= (signed) ht->size) {
426 if (dictIsRehashing(iter->d) && iter->table == 0) {
427 iter->table++;
428 iter->index = 0;
429 ht = &iter->d->ht[1];
430 } else {
431 break;
432 }
433 }
434 iter->entry = ht->table[iter->index];
ed9b544e 435 } else {
436 iter->entry = iter->nextEntry;
437 }
438 if (iter->entry) {
439 /* We need to save the 'next' here, the iterator user
440 * may delete the entry we are returning. */
441 iter->nextEntry = iter->entry->next;
442 return iter->entry;
443 }
444 }
445 return NULL;
446}
447
448void dictReleaseIterator(dictIterator *iter)
449{
5413c40d 450 if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--;
d9dd352b 451 zfree(iter);
ed9b544e 452}
453
454/* Return a random entry from the hash table. Useful to
455 * implement randomized algorithms */
5413c40d 456dictEntry *dictGetRandomKey(dict *d)
ed9b544e 457{
5413c40d 458 dictEntry *he, *orighe;
ed9b544e 459 unsigned int h;
460 int listlen, listele;
461
5413c40d 462 if (dictSize(d) == 0) return NULL;
463 if (dictIsRehashing(d)) _dictRehashStep(d);
464 if (dictIsRehashing(d)) {
465 do {
466 h = random() % (d->ht[0].size+d->ht[1].size);
467 he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] :
468 d->ht[0].table[h];
469 } while(he == NULL);
470 } else {
471 do {
472 h = random() & d->ht[0].sizemask;
473 he = d->ht[0].table[h];
474 } while(he == NULL);
475 }
ed9b544e 476
477 /* Now we found a non empty bucket, but it is a linked
478 * list and we need to get a random element from the list.
5413c40d 479 * The only sane way to do so is counting the elements and
ed9b544e 480 * select a random index. */
481 listlen = 0;
5413c40d 482 orighe = he;
ed9b544e 483 while(he) {
484 he = he->next;
485 listlen++;
486 }
487 listele = random() % listlen;
5413c40d 488 he = orighe;
ed9b544e 489 while(listele--) he = he->next;
490 return he;
491}
492
493/* ------------------------- private functions ------------------------------ */
494
495/* Expand the hash table if needed */
5413c40d 496static int _dictExpandIfNeeded(dict *d)
ed9b544e 497{
498 /* If the hash table is empty expand it to the intial size,
499 * if the table is "full" dobule its size. */
5413c40d 500 if (dictIsRehashing(d)) return DICT_OK;
501 if (d->ht[0].size == 0)
502 return dictExpand(d, DICT_HT_INITIAL_SIZE);
503 if (d->ht[0].used >= d->ht[0].size && dict_can_resize)
504 return dictExpand(d, ((d->ht[0].size > d->ht[0].used) ?
505 d->ht[0].size : d->ht[0].used)*2);
ed9b544e 506 return DICT_OK;
507}
508
509/* Our hash table capability is a power of two */
f2923bec 510static unsigned long _dictNextPower(unsigned long size)
ed9b544e 511{
f2923bec 512 unsigned long i = DICT_HT_INITIAL_SIZE;
ed9b544e 513
f2923bec 514 if (size >= LONG_MAX) return LONG_MAX;
ed9b544e 515 while(1) {
516 if (i >= size)
517 return i;
518 i *= 2;
519 }
520}
521
522/* Returns the index of a free slot that can be populated with
523 * an hash entry for the given 'key'.
5413c40d 524 * If the key already exists, -1 is returned.
525 *
526 * Note that if we are in the process of rehashing the hash table, the
527 * index is always returned in the context of the second (new) hash table. */
528static int _dictKeyIndex(dict *d, const void *key)
ed9b544e 529{
8ca3e9d1 530 unsigned int h, idx, table;
ed9b544e 531 dictEntry *he;
532
533 /* Expand the hashtable if needed */
5413c40d 534 if (_dictExpandIfNeeded(d) == DICT_ERR)
ed9b544e 535 return -1;
536 /* Compute the key hash value */
5413c40d 537 h = dictHashKey(d, key);
8ca3e9d1 538 for (table = 0; table <= 1; table++) {
539 idx = h & d->ht[table].sizemask;
540 /* Search if this slot does not already contain the given key */
541 he = d->ht[table].table[idx];
542 while(he) {
543 if (dictCompareHashKeys(d, key, he->key))
544 return -1;
545 he = he->next;
546 }
547 if (!dictIsRehashing(d)) break;
ed9b544e 548 }
8ca3e9d1 549 return idx;
ed9b544e 550}
551
5413c40d 552void dictEmpty(dict *d) {
553 _dictClear(d,&d->ht[0]);
554 _dictClear(d,&d->ht[1]);
555 d->rehashidx = -1;
556 d->iterators = 0;
ed9b544e 557}
558
559#define DICT_STATS_VECTLEN 50
5413c40d 560static void _dictPrintStatsHt(dictht *ht) {
f2923bec 561 unsigned long i, slots = 0, chainlen, maxchainlen = 0;
562 unsigned long totchainlen = 0;
563 unsigned long clvector[DICT_STATS_VECTLEN];
ed9b544e 564
565 if (ht->used == 0) {
566 printf("No stats available for empty dictionaries\n");
567 return;
568 }
569
570 for (i = 0; i < DICT_STATS_VECTLEN; i++) clvector[i] = 0;
571 for (i = 0; i < ht->size; i++) {
572 dictEntry *he;
573
574 if (ht->table[i] == NULL) {
575 clvector[0]++;
576 continue;
577 }
578 slots++;
579 /* For each hash entry on this slot... */
580 chainlen = 0;
581 he = ht->table[i];
582 while(he) {
583 chainlen++;
584 he = he->next;
585 }
586 clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++;
587 if (chainlen > maxchainlen) maxchainlen = chainlen;
588 totchainlen += chainlen;
589 }
590 printf("Hash table stats:\n");
f2923bec 591 printf(" table size: %ld\n", ht->size);
592 printf(" number of elements: %ld\n", ht->used);
593 printf(" different slots: %ld\n", slots);
594 printf(" max chain length: %ld\n", maxchainlen);
ed9b544e 595 printf(" avg chain length (counted): %.02f\n", (float)totchainlen/slots);
596 printf(" avg chain length (computed): %.02f\n", (float)ht->used/slots);
597 printf(" Chain length distribution:\n");
598 for (i = 0; i < DICT_STATS_VECTLEN-1; i++) {
599 if (clvector[i] == 0) continue;
f2923bec 600 printf(" %s%ld: %ld (%.02f%%)\n",(i == DICT_STATS_VECTLEN-1)?">= ":"", i, clvector[i], ((float)clvector[i]/ht->size)*100);
ed9b544e 601 }
602}
603
5413c40d 604void dictPrintStats(dict *d) {
605 _dictPrintStatsHt(&d->ht[0]);
606 if (dictIsRehashing(d)) {
607 printf("-- Rehashing into ht[1]:\n");
608 _dictPrintStatsHt(&d->ht[1]);
609 }
610}
611
884d4b39 612void dictEnableResize(void) {
613 dict_can_resize = 1;
614}
615
616void dictDisableResize(void) {
dae121d9 617 dict_can_resize = 0;
884d4b39 618}
619
ed9b544e 620/* ----------------------- StringCopy Hash Table Type ------------------------*/
621
622static unsigned int _dictStringCopyHTHashFunction(const void *key)
623{
624 return dictGenHashFunction(key, strlen(key));
625}
626
b1e0bd4b 627static void *_dictStringDup(void *privdata, const void *key)
ed9b544e 628{
629 int len = strlen(key);
d9dd352b 630 char *copy = zmalloc(len+1);
ed9b544e 631 DICT_NOTUSED(privdata);
632
633 memcpy(copy, key, len);
634 copy[len] = '\0';
635 return copy;
636}
637
ed9b544e 638static int _dictStringCopyHTKeyCompare(void *privdata, const void *key1,
639 const void *key2)
640{
641 DICT_NOTUSED(privdata);
642
643 return strcmp(key1, key2) == 0;
644}
645
b1e0bd4b 646static void _dictStringDestructor(void *privdata, void *key)
ed9b544e 647{
648 DICT_NOTUSED(privdata);
649
d9dd352b 650 zfree(key);
ed9b544e 651}
652
653dictType dictTypeHeapStringCopyKey = {
b1e0bd4b
BK
654 _dictStringCopyHTHashFunction, /* hash function */
655 _dictStringDup, /* key dup */
656 NULL, /* val dup */
657 _dictStringCopyHTKeyCompare, /* key compare */
658 _dictStringDestructor, /* key destructor */
659 NULL /* val destructor */
ed9b544e 660};
661
662/* This is like StringCopy but does not auto-duplicate the key.
663 * It's used for intepreter's shared strings. */
664dictType dictTypeHeapStrings = {
b1e0bd4b
BK
665 _dictStringCopyHTHashFunction, /* hash function */
666 NULL, /* key dup */
667 NULL, /* val dup */
668 _dictStringCopyHTKeyCompare, /* key compare */
669 _dictStringDestructor, /* key destructor */
670 NULL /* val destructor */
ed9b544e 671};
672
673/* This is like StringCopy but also automatically handle dynamic
674 * allocated C strings as values. */
675dictType dictTypeHeapStringCopyKeyValue = {
b1e0bd4b
BK
676 _dictStringCopyHTHashFunction, /* hash function */
677 _dictStringDup, /* key dup */
678 _dictStringDup, /* val dup */
679 _dictStringCopyHTKeyCompare, /* key compare */
680 _dictStringDestructor, /* key destructor */
681 _dictStringDestructor, /* val destructor */
ed9b544e 682};