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-2012, 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 <stdint.h> 37 38 #ifndef __DICT_H 39 #define __DICT_H 40 41 #define DICT_OK 0 42 #define DICT_ERR 1 43 44 /* Unused arguments generate annoying warnings... */ 45 #define DICT_NOTUSED(V) ((void) V) 46 47 typedef struct dictEntry { 48 void *key; 49 union { 50 void *val; 51 uint64_t u64; 52 int64_t s64; 53 double d; 54 } v; 55 struct dictEntry *next; 56 } dictEntry; 57 58 typedef struct dictType { 59 uint64_t (*hashFunction)(const void *key); 60 void *(*keyDup)(void *privdata, const void *key); 61 void *(*valDup)(void *privdata, const void *obj); 62 int (*keyCompare)(void *privdata, const void *key1, const void *key2); 63 void (*keyDestructor)(void *privdata, void *key); 64 void (*valDestructor)(void *privdata, void *obj); 65 } dictType; 66 67 /* This is our hash table structure. Every dictionary has two of this as we 68 * implement incremental rehashing, for the old to the new table. */ 69 typedef struct dictht { 70 dictEntry **table; 71 unsigned long size; 72 unsigned long sizemask; 73 unsigned long used; 74 } dictht; 75 76 typedef struct dict { 77 dictType *type; 78 void *privdata; 79 dictht ht[2]; 80 long rehashidx; /* rehashing not in progress if rehashidx == -1 */ 81 unsigned long iterators; /* number of iterators currently running */ 82 } dict; 83 84 /* If safe is set to 1 this is a safe iterator, that means, you can call 85 * dictAdd, dictFind, and other functions against the dictionary even while 86 * iterating. Otherwise it is a non safe iterator, and only dictNext() 87 * should be called while iterating. */ 88 typedef struct dictIterator { 89 dict *d; 90 long index; 91 int table, safe; 92 dictEntry *entry, *nextEntry; 93 /* unsafe iterator fingerprint for misuse detection. */ 94 long long fingerprint; 95 } dictIterator; 96 97 typedef void (dictScanFunction)(void *privdata, const dictEntry *de); 98 typedef void (dictScanBucketFunction)(void *privdata, dictEntry **bucketref); 99 100 /* This is the initial size of every hash table */ 101 #define DICT_HT_INITIAL_SIZE 4 102 103 /* ------------------------------- Macros ------------------------------------*/ 104 #define dictFreeVal(d, entry) \ 105 if ((d)->type->valDestructor) \ 106 (d)->type->valDestructor((d)->privdata, (entry)->v.val) 107 108 #define dictSetVal(d, entry, _val_) do { \ 109 if ((d)->type->valDup) \ 110 (entry)->v.val = (d)->type->valDup((d)->privdata, _val_); \ 111 else \ 112 (entry)->v.val = (_val_); \ 113 } while(0) 114 115 #define dictSetSignedIntegerVal(entry, _val_) \ 116 do { (entry)->v.s64 = _val_; } while(0) 117 118 #define dictSetUnsignedIntegerVal(entry, _val_) \ 119 do { (entry)->v.u64 = _val_; } while(0) 120 121 #define dictSetDoubleVal(entry, _val_) \ 122 do { (entry)->v.d = _val_; } while(0) 123 124 #define dictFreeKey(d, entry) \ 125 if ((d)->type->keyDestructor) \ 126 (d)->type->keyDestructor((d)->privdata, (entry)->key) 127 128 #define dictSetKey(d, entry, _key_) do { \ 129 if ((d)->type->keyDup) \ 130 (entry)->key = (d)->type->keyDup((d)->privdata, _key_); \ 131 else \ 132 (entry)->key = (_key_); \ 133 } while(0) 134 135 #define dictCompareKeys(d, key1, key2) \ 136 (((d)->type->keyCompare) ? \ 137 (d)->type->keyCompare((d)->privdata, key1, key2) : \ 138 (key1) == (key2)) 139 140 #define dictHashKey(d, key) (d)->type->hashFunction(key) 141 #define dictGetKey(he) ((he)->key) 142 #define dictGetVal(he) ((he)->v.val) 143 #define dictGetSignedIntegerVal(he) ((he)->v.s64) 144 #define dictGetUnsignedIntegerVal(he) ((he)->v.u64) 145 #define dictGetDoubleVal(he) ((he)->v.d) 146 #define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size) 147 #define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used) 148 #define dictIsRehashing(d) ((d)->rehashidx != -1) 149 150 /* API */ 151 dict *dictCreate(dictType *type, void *privDataPtr); 152 int dictExpand(dict *d, unsigned long size); 153 int dictAdd(dict *d, void *key, void *val); 154 dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing); 155 dictEntry *dictAddOrFind(dict *d, void *key); 156 int dictReplace(dict *d, void *key, void *val); 157 int dictDelete(dict *d, const void *key); 158 dictEntry *dictUnlink(dict *ht, const void *key); 159 void dictFreeUnlinkedEntry(dict *d, dictEntry *he); 160 void dictRelease(dict *d); 161 dictEntry * dictFind(dict *d, const void *key); 162 void *dictFetchValue(dict *d, const void *key); 163 int dictResize(dict *d); 164 dictIterator *dictGetIterator(dict *d); 165 dictIterator *dictGetSafeIterator(dict *d); 166 dictEntry *dictNext(dictIterator *iter); 167 void dictReleaseIterator(dictIterator *iter); 168 dictEntry *dictGetRandomKey(dict *d); 169 unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count); 170 void dictGetStats(char *buf, size_t bufsize, dict *d); 171 uint64_t dictGenHashFunction(const void *key, int len); 172 uint64_t dictGenCaseHashFunction(const unsigned char *buf, int len); 173 void dictEmpty(dict *d, void(callback)(void*)); 174 void dictEnableResize(void); 175 void dictDisableResize(void); 176 int dictRehash(dict *d, int n); 177 int dictRehashMilliseconds(dict *d, int ms); 178 void dictSetHashFunctionSeed(uint8_t *seed); 179 uint8_t *dictGetHashFunctionSeed(void); 180 unsigned long dictScan(dict *d, unsigned long v, dictScanFunction *fn, dictScanBucketFunction *bucketfn, void *privdata); 181 uint64_t dictGetHash(dict *d, const void *key); 182 dictEntry **dictFindEntryRefByPtrAndHash(dict *d, const void *oldptr, uint64_t hash); 183 184 /* Hash table types */ 185 extern dictType dictTypeHeapStringCopyKey; 186 extern dictType dictTypeHeapStrings; 187 extern dictType dictTypeHeapStringCopyKeyValue; 188 189 #endif /* __DICT_H */ 190