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