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 #include "sqliteInt.h" 16 #include <assert.h> 17 18 /* Turn bulk memory into a hash table object by initializing the 19 ** fields of the Hash structure. 20 ** 21 ** "pNew" is a pointer to the hash table that is to be initialized. 22 */ 23 void sqlite3HashInit(Hash *pNew){ 24 assert( pNew!=0 ); 25 pNew->first = 0; 26 pNew->count = 0; 27 pNew->htsize = 0; 28 pNew->ht = 0; 29 } 30 31 /* Remove all entries from a hash table. Reclaim all memory. 32 ** Call this routine to delete a hash table or to reset a hash table 33 ** to the empty state. 34 */ 35 void sqlite3HashClear(Hash *pH){ 36 HashElem *elem; /* For looping over all elements of the table */ 37 38 assert( pH!=0 ); 39 elem = pH->first; 40 pH->first = 0; 41 sqlite3_free(pH->ht); 42 pH->ht = 0; 43 pH->htsize = 0; 44 while( elem ){ 45 HashElem *next_elem = elem->next; 46 sqlite3_free(elem); 47 elem = next_elem; 48 } 49 pH->count = 0; 50 } 51 52 /* 53 ** The hashing function. 54 */ 55 static unsigned int strHash(const char *z){ 56 unsigned int h = 0; 57 unsigned char c; 58 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/ 59 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). 60 ** 0x9e3779b1 is 2654435761 which is the closest prime number to 61 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ 62 h += sqlite3UpperToLower[c]; 63 h *= 0x9e3779b1; 64 } 65 return h; 66 } 67 #ifdef SQLITE_ENABLE_NORMALIZE 68 static unsigned int strHashN(const char *z, int n){ 69 unsigned int h = 0; 70 int i; 71 for(i=0; i<n; i++){ 72 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). 73 ** 0x9e3779b1 is 2654435761 which is the closest prime number to 74 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ 75 h += sqlite3UpperToLower[z[i]]; 76 h *= 0x9e3779b1; 77 } 78 return h; 79 } 80 #endif /* SQLITE_ENABLE_NORMALIZE */ 81 82 83 /* Link pNew element into the hash table pH. If pEntry!=0 then also 84 ** insert pNew into the pEntry hash bucket. 85 */ 86 static void insertElement( 87 Hash *pH, /* The complete hash table */ 88 struct _ht *pEntry, /* The entry into which pNew is inserted */ 89 HashElem *pNew /* The element to be inserted */ 90 ){ 91 HashElem *pHead; /* First element already in pEntry */ 92 if( pEntry ){ 93 pHead = pEntry->count ? pEntry->chain : 0; 94 pEntry->count++; 95 pEntry->chain = pNew; 96 }else{ 97 pHead = 0; 98 } 99 if( pHead ){ 100 pNew->next = pHead; 101 pNew->prev = pHead->prev; 102 if( pHead->prev ){ pHead->prev->next = pNew; } 103 else { pH->first = pNew; } 104 pHead->prev = pNew; 105 }else{ 106 pNew->next = pH->first; 107 if( pH->first ){ pH->first->prev = pNew; } 108 pNew->prev = 0; 109 pH->first = pNew; 110 } 111 } 112 113 114 /* Resize the hash table so that it cantains "new_size" buckets. 115 ** 116 ** The hash table might fail to resize if sqlite3_malloc() fails or 117 ** if the new size is the same as the prior size. 118 ** Return TRUE if the resize occurs and false if not. 119 */ 120 static int rehash(Hash *pH, unsigned int new_size){ 121 struct _ht *new_ht; /* The new hash table */ 122 HashElem *elem, *next_elem; /* For looping over existing elements */ 123 124 #if SQLITE_MALLOC_SOFT_LIMIT>0 125 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ 126 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); 127 } 128 if( new_size==pH->htsize ) return 0; 129 #endif 130 131 /* The inability to allocates space for a larger hash table is 132 ** a performance hit but it is not a fatal error. So mark the 133 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 134 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() 135 ** only zeroes the requested number of bytes whereas this module will 136 ** use the actual amount of space allocated for the hash table (which 137 ** may be larger than the requested amount). 138 */ 139 sqlite3BeginBenignMalloc(); 140 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); 141 sqlite3EndBenignMalloc(); 142 143 if( new_ht==0 ) return 0; 144 sqlite3_free(pH->ht); 145 pH->ht = new_ht; 146 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); 147 memset(new_ht, 0, new_size*sizeof(struct _ht)); 148 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ 149 unsigned int h = strHash(elem->pKey) % new_size; 150 next_elem = elem->next; 151 insertElement(pH, &new_ht[h], elem); 152 } 153 return 1; 154 } 155 156 /* This function (for internal use only) locates an element in an 157 ** hash table that matches the given key. If no element is found, 158 ** a pointer to a static null element with HashElem.data==0 is returned. 159 ** If pH is not NULL, then the hash for this key is written to *pH. 160 */ 161 static HashElem *findElementWithHash( 162 const Hash *pH, /* The pH to be searched */ 163 const char *pKey, /* The key we are searching for */ 164 unsigned int *pHash /* Write the hash value here */ 165 ){ 166 HashElem *elem; /* Used to loop thru the element list */ 167 int count; /* Number of elements left to test */ 168 unsigned int h; /* The computed hash */ 169 static HashElem nullElement = { 0, 0, 0, 0 }; 170 171 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ 172 struct _ht *pEntry; 173 h = strHash(pKey) % pH->htsize; 174 pEntry = &pH->ht[h]; 175 elem = pEntry->chain; 176 count = pEntry->count; 177 }else{ 178 h = 0; 179 elem = pH->first; 180 count = pH->count; 181 } 182 if( pHash ) *pHash = h; 183 while( count-- ){ 184 assert( elem!=0 ); 185 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 186 return elem; 187 } 188 elem = elem->next; 189 } 190 return &nullElement; 191 } 192 #ifdef SQLITE_ENABLE_NORMALIZE 193 static HashElem *findElementWithHashN( 194 const Hash *pH, /* The pH to be searched */ 195 const char *pKey, /* The key we are searching for */ 196 int nKey, /* Number of key bytes to use */ 197 unsigned int *pHash /* Write the hash value here */ 198 ){ 199 HashElem *elem; /* Used to loop thru the element list */ 200 int count; /* Number of elements left to test */ 201 unsigned int h; /* The computed hash */ 202 static HashElem nullElement = { 0, 0, 0, 0 }; 203 204 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ 205 struct _ht *pEntry; 206 h = strHashN(pKey, nKey) % pH->htsize; 207 pEntry = &pH->ht[h]; 208 elem = pEntry->chain; 209 count = pEntry->count; 210 }else{ 211 h = 0; 212 elem = pH->first; 213 count = pH->count; 214 } 215 if( pHash ) *pHash = h; 216 while( count-- ){ 217 assert( elem!=0 ); 218 if( sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 219 return elem; 220 } 221 elem = elem->next; 222 } 223 return &nullElement; 224 } 225 #endif /* SQLITE_ENABLE_NORMALIZE */ 226 227 /* Remove a single entry from the hash table given a pointer to that 228 ** element and a hash on the element's key. 229 */ 230 static void removeElementGivenHash( 231 Hash *pH, /* The pH containing "elem" */ 232 HashElem* elem, /* The element to be removed from the pH */ 233 unsigned int h /* Hash value for the element */ 234 ){ 235 struct _ht *pEntry; 236 if( elem->prev ){ 237 elem->prev->next = elem->next; 238 }else{ 239 pH->first = elem->next; 240 } 241 if( elem->next ){ 242 elem->next->prev = elem->prev; 243 } 244 if( pH->ht ){ 245 pEntry = &pH->ht[h]; 246 if( pEntry->chain==elem ){ 247 pEntry->chain = elem->next; 248 } 249 pEntry->count--; 250 assert( pEntry->count>=0 ); 251 } 252 sqlite3_free( elem ); 253 pH->count--; 254 if( pH->count==0 ){ 255 assert( pH->first==0 ); 256 assert( pH->count==0 ); 257 sqlite3HashClear(pH); 258 } 259 } 260 261 /* Attempt to locate an element of the hash table pH with a key 262 ** that matches pKey. Return the data for this element if it is 263 ** found, or NULL if there is no match. 264 */ 265 void *sqlite3HashFind(const Hash *pH, const char *pKey){ 266 assert( pH!=0 ); 267 assert( pKey!=0 ); 268 return findElementWithHash(pH, pKey, 0)->data; 269 } 270 #ifdef SQLITE_ENABLE_NORMALIZE 271 void *sqlite3HashFindN(const Hash *pH, const char *pKey, int nKey){ 272 assert( pH!=0 ); 273 assert( pKey!=0 ); 274 assert( nKey>=0 ); 275 return findElementWithHashN(pH, pKey, nKey, 0)->data; 276 } 277 #endif /* SQLITE_ENABLE_NORMALIZE */ 278 279 /* Insert an element into the hash table pH. The key is pKey 280 ** and the data is "data". 281 ** 282 ** If no element exists with a matching key, then a new 283 ** element is created and NULL is returned. 284 ** 285 ** If another element already exists with the same key, then the 286 ** new data replaces the old data and the old data is returned. 287 ** The key is not copied in this instance. If a malloc fails, then 288 ** the new data is returned and the hash table is unchanged. 289 ** 290 ** If the "data" parameter to this function is NULL, then the 291 ** element corresponding to "key" is removed from the hash table. 292 */ 293 void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ 294 unsigned int h; /* the hash of the key modulo hash table size */ 295 HashElem *elem; /* Used to loop thru the element list */ 296 HashElem *new_elem; /* New element added to the pH */ 297 298 assert( pH!=0 ); 299 assert( pKey!=0 ); 300 elem = findElementWithHash(pH,pKey,&h); 301 if( elem->data ){ 302 void *old_data = elem->data; 303 if( data==0 ){ 304 removeElementGivenHash(pH,elem,h); 305 }else{ 306 elem->data = data; 307 elem->pKey = pKey; 308 } 309 return old_data; 310 } 311 if( data==0 ) return 0; 312 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); 313 if( new_elem==0 ) return data; 314 new_elem->pKey = pKey; 315 new_elem->data = data; 316 pH->count++; 317 if( pH->count>=10 && pH->count > 2*pH->htsize ){ 318 if( rehash(pH, pH->count*2) ){ 319 assert( pH->htsize>0 ); 320 h = strHash(pKey) % pH->htsize; 321 } 322 } 323 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); 324 return 0; 325 } 326