| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** 2001 September 22 | |
| 3 ** | |
| 4 ** The author disclaims copyright to this source code. In place of | |
| 5 ** a legal notice, here is a blessing: | |
| 6 ** | |
| 7 ** May you do good and not evil. | |
| 8 ** May you find forgiveness for yourself and forgive others. | |
| 9 ** May you share freely, never taking more than you give. | |
| 10 ** | |
| 11 ************************************************************************* | |
| 12 ** This is the implementation of generic hash-tables | |
| 13 ** used in SQLite. | |
| 14 ** | |
| 15 ** $Id: hash.c,v 1.38 2009/05/09 23:29:12 drh Exp $ | |
| 16 */ | |
| 17 #include "sqliteInt.h" | |
| 18 #include <assert.h> | |
| 19 | |
| 20 /* Turn bulk memory into a hash table object by initializing the | |
| 21 ** fields of the Hash structure. | |
| 22 ** | |
| 23 ** "pNew" is a pointer to the hash table that is to be initialized. | |
| 24 */ | |
| 25 void sqlite3HashInit(Hash *pNew){ | |
| 26 assert( pNew!=0 ); | |
| 27 pNew->first = 0; | |
| 28 pNew->count = 0; | |
| 29 pNew->htsize = 0; | |
| 30 pNew->ht = 0; | |
| 31 } | |
| 32 | |
| 33 /* Remove all entries from a hash table. Reclaim all memory. | |
| 34 ** Call this routine to delete a hash table or to reset a hash table | |
| 35 ** to the empty state. | |
| 36 */ | |
| 37 void sqlite3HashClear(Hash *pH){ | |
| 38 HashElem *elem; /* For looping over all elements of the table */ | |
| 39 | |
| 40 assert( pH!=0 ); | |
| 41 elem = pH->first; | |
| 42 pH->first = 0; | |
| 43 sqlite3_free(pH->ht); | |
| 44 pH->ht = 0; | |
| 45 pH->htsize = 0; | |
| 46 while( elem ){ | |
| 47 HashElem *next_elem = elem->next; | |
| 48 sqlite3_free(elem); | |
| 49 elem = next_elem; | |
| 50 } | |
| 51 pH->count = 0; | |
| 52 } | |
| 53 | |
| 54 /* | |
| 55 ** The hashing function. | |
| 56 */ | |
| 57 static unsigned int strHash(const char *z, int nKey){ | |
| 58 int h = 0; | |
| 59 assert( nKey>=0 ); | |
| 60 while( nKey > 0 ){ | |
| 61 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++]; | |
| 62 nKey--; | |
| 63 } | |
| 64 return h; | |
| 65 } | |
| 66 | |
| 67 | |
| 68 /* Link pNew element into the hash table pH. If pEntry!=0 then also | |
| 69 ** insert pNew into the pEntry hash bucket. | |
| 70 */ | |
| 71 static void insertElement( | |
| 72 Hash *pH, /* The complete hash table */ | |
| 73 struct _ht *pEntry, /* The entry into which pNew is inserted */ | |
| 74 HashElem *pNew /* The element to be inserted */ | |
| 75 ){ | |
| 76 HashElem *pHead; /* First element already in pEntry */ | |
| 77 if( pEntry ){ | |
| 78 pHead = pEntry->count ? pEntry->chain : 0; | |
| 79 pEntry->count++; | |
| 80 pEntry->chain = pNew; | |
| 81 }else{ | |
| 82 pHead = 0; | |
| 83 } | |
| 84 if( pHead ){ | |
| 85 pNew->next = pHead; | |
| 86 pNew->prev = pHead->prev; | |
| 87 if( pHead->prev ){ pHead->prev->next = pNew; } | |
| 88 else { pH->first = pNew; } | |
| 89 pHead->prev = pNew; | |
| 90 }else{ | |
| 91 pNew->next = pH->first; | |
| 92 if( pH->first ){ pH->first->prev = pNew; } | |
| 93 pNew->prev = 0; | |
| 94 pH->first = pNew; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 | |
| 99 /* Resize the hash table so that it cantains "new_size" buckets. | |
| 100 ** | |
| 101 ** The hash table might fail to resize if sqlite3_malloc() fails or | |
| 102 ** if the new size is the same as the prior size. | |
| 103 ** Return TRUE if the resize occurs and false if not. | |
| 104 */ | |
| 105 static int rehash(Hash *pH, unsigned int new_size){ | |
| 106 struct _ht *new_ht; /* The new hash table */ | |
| 107 HashElem *elem, *next_elem; /* For looping over existing elements */ | |
| 108 | |
| 109 #if SQLITE_MALLOC_SOFT_LIMIT>0 | |
| 110 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ | |
| 111 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); | |
| 112 } | |
| 113 if( new_size==pH->htsize ) return 0; | |
| 114 #endif | |
| 115 | |
| 116 /* The inability to allocates space for a larger hash table is | |
| 117 ** a performance hit but it is not a fatal error. So mark the | |
| 118 ** allocation as a benign. | |
| 119 */ | |
| 120 sqlite3BeginBenignMalloc(); | |
| 121 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); | |
| 122 sqlite3EndBenignMalloc(); | |
| 123 | |
| 124 if( new_ht==0 ) return 0; | |
| 125 sqlite3_free(pH->ht); | |
| 126 pH->ht = new_ht; | |
| 127 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); | |
| 128 memset(new_ht, 0, new_size*sizeof(struct _ht)); | |
| 129 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ | |
| 130 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; | |
| 131 next_elem = elem->next; | |
| 132 insertElement(pH, &new_ht[h], elem); | |
| 133 } | |
| 134 return 1; | |
| 135 } | |
| 136 | |
| 137 /* This function (for internal use only) locates an element in an | |
| 138 ** hash table that matches the given key. The hash for this key has | |
| 139 ** already been computed and is passed as the 4th parameter. | |
| 140 */ | |
| 141 static HashElem *findElementGivenHash( | |
| 142 const Hash *pH, /* The pH to be searched */ | |
| 143 const char *pKey, /* The key we are searching for */ | |
| 144 int nKey, /* Bytes in key (not counting zero terminator) */ | |
| 145 unsigned int h /* The hash for this key. */ | |
| 146 ){ | |
| 147 HashElem *elem; /* Used to loop thru the element list */ | |
| 148 int count; /* Number of elements left to test */ | |
| 149 | |
| 150 if( pH->ht ){ | |
| 151 struct _ht *pEntry = &pH->ht[h]; | |
| 152 elem = pEntry->chain; | |
| 153 count = pEntry->count; | |
| 154 }else{ | |
| 155 elem = pH->first; | |
| 156 count = pH->count; | |
| 157 } | |
| 158 while( count-- && ALWAYS(elem) ){ | |
| 159 if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ | |
| 160 return elem; | |
| 161 } | |
| 162 elem = elem->next; | |
| 163 } | |
| 164 return 0; | |
| 165 } | |
| 166 | |
| 167 /* Remove a single entry from the hash table given a pointer to that | |
| 168 ** element and a hash on the element's key. | |
| 169 */ | |
| 170 static void removeElementGivenHash( | |
| 171 Hash *pH, /* The pH containing "elem" */ | |
| 172 HashElem* elem, /* The element to be removed from the pH */ | |
| 173 unsigned int h /* Hash value for the element */ | |
| 174 ){ | |
| 175 struct _ht *pEntry; | |
| 176 if( elem->prev ){ | |
| 177 elem->prev->next = elem->next; | |
| 178 }else{ | |
| 179 pH->first = elem->next; | |
| 180 } | |
| 181 if( elem->next ){ | |
| 182 elem->next->prev = elem->prev; | |
| 183 } | |
| 184 if( pH->ht ){ | |
| 185 pEntry = &pH->ht[h]; | |
| 186 if( pEntry->chain==elem ){ | |
| 187 pEntry->chain = elem->next; | |
| 188 } | |
| 189 pEntry->count--; | |
| 190 assert( pEntry->count>=0 ); | |
| 191 } | |
| 192 sqlite3_free( elem ); | |
| 193 pH->count--; | |
| 194 if( pH->count<=0 ){ | |
| 195 assert( pH->first==0 ); | |
| 196 assert( pH->count==0 ); | |
| 197 sqlite3HashClear(pH); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 /* Attempt to locate an element of the hash table pH with a key | |
| 202 ** that matches pKey,nKey. Return the data for this element if it is | |
| 203 ** found, or NULL if there is no match. | |
| 204 */ | |
| 205 void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){ | |
| 206 HashElem *elem; /* The element that matches key */ | |
| 207 unsigned int h; /* A hash on key */ | |
| 208 | |
| 209 assert( pH!=0 ); | |
| 210 assert( pKey!=0 ); | |
| 211 assert( nKey>=0 ); | |
| 212 if( pH->ht ){ | |
| 213 h = strHash(pKey, nKey) % pH->htsize; | |
| 214 }else{ | |
| 215 h = 0; | |
| 216 } | |
| 217 elem = findElementGivenHash(pH, pKey, nKey, h); | |
| 218 return elem ? elem->data : 0; | |
| 219 } | |
| 220 | |
| 221 /* Insert an element into the hash table pH. The key is pKey,nKey | |
| 222 ** and the data is "data". | |
| 223 ** | |
| 224 ** If no element exists with a matching key, then a new | |
| 225 ** element is created and NULL is returned. | |
| 226 ** | |
| 227 ** If another element already exists with the same key, then the | |
| 228 ** new data replaces the old data and the old data is returned. | |
| 229 ** The key is not copied in this instance. If a malloc fails, then | |
| 230 ** the new data is returned and the hash table is unchanged. | |
| 231 ** | |
| 232 ** If the "data" parameter to this function is NULL, then the | |
| 233 ** element corresponding to "key" is removed from the hash table. | |
| 234 */ | |
| 235 void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){ | |
| 236 unsigned int h; /* the hash of the key modulo hash table size */ | |
| 237 HashElem *elem; /* Used to loop thru the element list */ | |
| 238 HashElem *new_elem; /* New element added to the pH */ | |
| 239 | |
| 240 assert( pH!=0 ); | |
| 241 assert( pKey!=0 ); | |
| 242 assert( nKey>=0 ); | |
| 243 if( pH->htsize ){ | |
| 244 h = strHash(pKey, nKey) % pH->htsize; | |
| 245 }else{ | |
| 246 h = 0; | |
| 247 } | |
| 248 elem = findElementGivenHash(pH,pKey,nKey,h); | |
| 249 if( elem ){ | |
| 250 void *old_data = elem->data; | |
| 251 if( data==0 ){ | |
| 252 removeElementGivenHash(pH,elem,h); | |
| 253 }else{ | |
| 254 elem->data = data; | |
| 255 elem->pKey = pKey; | |
| 256 assert(nKey==elem->nKey); | |
| 257 } | |
| 258 return old_data; | |
| 259 } | |
| 260 if( data==0 ) return 0; | |
| 261 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); | |
| 262 if( new_elem==0 ) return data; | |
| 263 new_elem->pKey = pKey; | |
| 264 new_elem->nKey = nKey; | |
| 265 new_elem->data = data; | |
| 266 pH->count++; | |
| 267 if( pH->count>=10 && pH->count > 2*pH->htsize ){ | |
| 268 if( rehash(pH, pH->count*2) ){ | |
| 269 assert( pH->htsize>0 ); | |
| 270 h = strHash(pKey, nKey) % pH->htsize; | |
| 271 } | |
| 272 } | |
| 273 if( pH->ht ){ | |
| 274 insertElement(pH, &pH->ht[h], new_elem); | |
| 275 }else{ | |
| 276 insertElement(pH, 0, new_elem); | |
| 277 } | |
| 278 return 0; | |
| 279 } | |
| OLD | NEW |