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