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, int nKey){ 56 int h = 0; 57 assert( nKey>=0 ); 58 while( nKey > 0 ){ 59 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++]; 60 nKey--; 61 } 62 return h; 63 } 64 65 66 /* Link pNew element into the hash table pH. If pEntry!=0 then also 67 ** insert pNew into the pEntry hash bucket. 68 */ 69 static void insertElement( 70 Hash *pH, /* The complete hash table */ 71 struct _ht *pEntry, /* The entry into which pNew is inserted */ 72 HashElem *pNew /* The element to be inserted */ 73 ){ 74 HashElem *pHead; /* First element already in pEntry */ 75 if( pEntry ){ 76 pHead = pEntry->count ? pEntry->chain : 0; 77 pEntry->count++; 78 pEntry->chain = pNew; 79 }else{ 80 pHead = 0; 81 } 82 if( pHead ){ 83 pNew->next = pHead; 84 pNew->prev = pHead->prev; 85 if( pHead->prev ){ pHead->prev->next = pNew; } 86 else { pH->first = pNew; } 87 pHead->prev = pNew; 88 }else{ 89 pNew->next = pH->first; 90 if( pH->first ){ pH->first->prev = pNew; } 91 pNew->prev = 0; 92 pH->first = pNew; 93 } 94 } 95 96 97 /* Resize the hash table so that it cantains "new_size" buckets. 98 ** 99 ** The hash table might fail to resize if sqlite3_malloc() fails or 100 ** if the new size is the same as the prior size. 101 ** Return TRUE if the resize occurs and false if not. 102 */ 103 static int rehash(Hash *pH, unsigned int new_size){ 104 struct _ht *new_ht; /* The new hash table */ 105 HashElem *elem, *next_elem; /* For looping over existing elements */ 106 107 #if SQLITE_MALLOC_SOFT_LIMIT>0 108 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ 109 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); 110 } 111 if( new_size==pH->htsize ) return 0; 112 #endif 113 114 /* The inability to allocates space for a larger hash table is 115 ** a performance hit but it is not a fatal error. So mark the 116 ** allocation as a benign. 117 */ 118 sqlite3BeginBenignMalloc(); 119 new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) ); 120 sqlite3EndBenignMalloc(); 121 122 if( new_ht==0 ) return 0; 123 sqlite3_free(pH->ht); 124 pH->ht = new_ht; 125 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); 126 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ 127 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; 128 next_elem = elem->next; 129 insertElement(pH, &new_ht[h], elem); 130 } 131 return 1; 132 } 133 134 /* This function (for internal use only) locates an element in an 135 ** hash table that matches the given key. The hash for this key has 136 ** already been computed and is passed as the 4th parameter. 137 */ 138 static HashElem *findElementGivenHash( 139 const Hash *pH, /* The pH to be searched */ 140 const char *pKey, /* The key we are searching for */ 141 int nKey, /* Bytes in key (not counting zero terminator) */ 142 unsigned int h /* The hash for this key. */ 143 ){ 144 HashElem *elem; /* Used to loop thru the element list */ 145 int count; /* Number of elements left to test */ 146 147 if( pH->ht ){ 148 struct _ht *pEntry = &pH->ht[h]; 149 elem = pEntry->chain; 150 count = pEntry->count; 151 }else{ 152 elem = pH->first; 153 count = pH->count; 154 } 155 while( count-- && ALWAYS(elem) ){ 156 if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 157 return elem; 158 } 159 elem = elem->next; 160 } 161 return 0; 162 } 163 164 /* Remove a single entry from the hash table given a pointer to that 165 ** element and a hash on the element's key. 166 */ 167 static void removeElementGivenHash( 168 Hash *pH, /* The pH containing "elem" */ 169 HashElem* elem, /* The element to be removed from the pH */ 170 unsigned int h /* Hash value for the element */ 171 ){ 172 struct _ht *pEntry; 173 if( elem->prev ){ 174 elem->prev->next = elem->next; 175 }else{ 176 pH->first = elem->next; 177 } 178 if( elem->next ){ 179 elem->next->prev = elem->prev; 180 } 181 if( pH->ht ){ 182 pEntry = &pH->ht[h]; 183 if( pEntry->chain==elem ){ 184 pEntry->chain = elem->next; 185 } 186 pEntry->count--; 187 assert( pEntry->count>=0 ); 188 } 189 sqlite3_free( elem ); 190 pH->count--; 191 if( pH->count<=0 ){ 192 assert( pH->first==0 ); 193 assert( pH->count==0 ); 194 sqlite3HashClear(pH); 195 } 196 } 197 198 /* Attempt to locate an element of the hash table pH with a key 199 ** that matches pKey,nKey. Return the data for this element if it is 200 ** found, or NULL if there is no match. 201 */ 202 void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){ 203 HashElem *elem; /* The element that matches key */ 204 unsigned int h; /* A hash on key */ 205 206 assert( pH!=0 ); 207 assert( pKey!=0 ); 208 assert( nKey>=0 ); 209 if( pH->ht ){ 210 h = strHash(pKey, nKey) % pH->htsize; 211 }else{ 212 h = 0; 213 } 214 elem = findElementGivenHash(pH, pKey, nKey, h); 215 return elem ? elem->data : 0; 216 } 217 218 /* Insert an element into the hash table pH. The key is pKey,nKey 219 ** and the data is "data". 220 ** 221 ** If no element exists with a matching key, then a new 222 ** element is created and NULL is returned. 223 ** 224 ** If another element already exists with the same key, then the 225 ** new data replaces the old data and the old data is returned. 226 ** The key is not copied in this instance. If a malloc fails, then 227 ** the new data is returned and the hash table is unchanged. 228 ** 229 ** If the "data" parameter to this function is NULL, then the 230 ** element corresponding to "key" is removed from the hash table. 231 */ 232 void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){ 233 unsigned int h; /* the hash of the key modulo hash table size */ 234 HashElem *elem; /* Used to loop thru the element list */ 235 HashElem *new_elem; /* New element added to the pH */ 236 237 assert( pH!=0 ); 238 assert( pKey!=0 ); 239 assert( nKey>=0 ); 240 if( pH->htsize ){ 241 h = strHash(pKey, nKey) % pH->htsize; 242 }else{ 243 h = 0; 244 } 245 elem = findElementGivenHash(pH,pKey,nKey,h); 246 if( elem ){ 247 void *old_data = elem->data; 248 if( data==0 ){ 249 removeElementGivenHash(pH,elem,h); 250 }else{ 251 elem->data = data; 252 elem->pKey = pKey; 253 assert(nKey==elem->nKey); 254 } 255 return old_data; 256 } 257 if( data==0 ) return 0; 258 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); 259 if( new_elem==0 ) return data; 260 new_elem->pKey = pKey; 261 new_elem->nKey = nKey; 262 new_elem->data = data; 263 pH->count++; 264 if( pH->count>=10 && pH->count > 2*pH->htsize ){ 265 if( rehash(pH, pH->count*2) ){ 266 assert( pH->htsize>0 ); 267 h = strHash(pKey, nKey) % pH->htsize; 268 } 269 } 270 if( pH->ht ){ 271 insertElement(pH, &pH->ht[h], new_elem); 272 }else{ 273 insertElement(pH, 0, new_elem); 274 } 275 return 0; 276 } 277