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. Use sqlite3Malloc()/memset(0) instead of 117 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() 118 ** only zeroes the requested number of bytes whereas this module will 119 ** use the actual amount of space allocated for the hash table (which 120 ** may be larger than the requested amount). 121 */ 122 sqlite3BeginBenignMalloc(); 123 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); 124 sqlite3EndBenignMalloc(); 125 126 if( new_ht==0 ) return 0; 127 sqlite3_free(pH->ht); 128 pH->ht = new_ht; 129 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); 130 memset(new_ht, 0, new_size*sizeof(struct _ht)); 131 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ 132 unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; 133 next_elem = elem->next; 134 insertElement(pH, &new_ht[h], elem); 135 } 136 return 1; 137 } 138 139 /* This function (for internal use only) locates an element in an 140 ** hash table that matches the given key. The hash for this key has 141 ** already been computed and is passed as the 4th parameter. 142 */ 143 static HashElem *findElementGivenHash( 144 const Hash *pH, /* The pH to be searched */ 145 const char *pKey, /* The key we are searching for */ 146 int nKey, /* Bytes in key (not counting zero terminator) */ 147 unsigned int h /* The hash for this key. */ 148 ){ 149 HashElem *elem; /* Used to loop thru the element list */ 150 int count; /* Number of elements left to test */ 151 152 if( pH->ht ){ 153 struct _ht *pEntry = &pH->ht[h]; 154 elem = pEntry->chain; 155 count = pEntry->count; 156 }else{ 157 elem = pH->first; 158 count = pH->count; 159 } 160 while( count-- && ALWAYS(elem) ){ 161 if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 162 return elem; 163 } 164 elem = elem->next; 165 } 166 return 0; 167 } 168 169 /* Remove a single entry from the hash table given a pointer to that 170 ** element and a hash on the element's key. 171 */ 172 static void removeElementGivenHash( 173 Hash *pH, /* The pH containing "elem" */ 174 HashElem* elem, /* The element to be removed from the pH */ 175 unsigned int h /* Hash value for the element */ 176 ){ 177 struct _ht *pEntry; 178 if( elem->prev ){ 179 elem->prev->next = elem->next; 180 }else{ 181 pH->first = elem->next; 182 } 183 if( elem->next ){ 184 elem->next->prev = elem->prev; 185 } 186 if( pH->ht ){ 187 pEntry = &pH->ht[h]; 188 if( pEntry->chain==elem ){ 189 pEntry->chain = elem->next; 190 } 191 pEntry->count--; 192 assert( pEntry->count>=0 ); 193 } 194 sqlite3_free( elem ); 195 pH->count--; 196 if( pH->count<=0 ){ 197 assert( pH->first==0 ); 198 assert( pH->count==0 ); 199 sqlite3HashClear(pH); 200 } 201 } 202 203 /* Attempt to locate an element of the hash table pH with a key 204 ** that matches pKey,nKey. Return the data for this element if it is 205 ** found, or NULL if there is no match. 206 */ 207 void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){ 208 HashElem *elem; /* The element that matches key */ 209 unsigned int h; /* A hash on key */ 210 211 assert( pH!=0 ); 212 assert( pKey!=0 ); 213 assert( nKey>=0 ); 214 if( pH->ht ){ 215 h = strHash(pKey, nKey) % pH->htsize; 216 }else{ 217 h = 0; 218 } 219 elem = findElementGivenHash(pH, pKey, nKey, h); 220 return elem ? elem->data : 0; 221 } 222 223 /* Insert an element into the hash table pH. The key is pKey,nKey 224 ** and the data is "data". 225 ** 226 ** If no element exists with a matching key, then a new 227 ** element is created and NULL is returned. 228 ** 229 ** If another element already exists with the same key, then the 230 ** new data replaces the old data and the old data is returned. 231 ** The key is not copied in this instance. If a malloc fails, then 232 ** the new data is returned and the hash table is unchanged. 233 ** 234 ** If the "data" parameter to this function is NULL, then the 235 ** element corresponding to "key" is removed from the hash table. 236 */ 237 void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){ 238 unsigned int h; /* the hash of the key modulo hash table size */ 239 HashElem *elem; /* Used to loop thru the element list */ 240 HashElem *new_elem; /* New element added to the pH */ 241 242 assert( pH!=0 ); 243 assert( pKey!=0 ); 244 assert( nKey>=0 ); 245 if( pH->htsize ){ 246 h = strHash(pKey, nKey) % pH->htsize; 247 }else{ 248 h = 0; 249 } 250 elem = findElementGivenHash(pH,pKey,nKey,h); 251 if( elem ){ 252 void *old_data = elem->data; 253 if( data==0 ){ 254 removeElementGivenHash(pH,elem,h); 255 }else{ 256 elem->data = data; 257 elem->pKey = pKey; 258 assert(nKey==elem->nKey); 259 } 260 return old_data; 261 } 262 if( data==0 ) return 0; 263 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); 264 if( new_elem==0 ) return data; 265 new_elem->pKey = pKey; 266 new_elem->nKey = nKey; 267 new_elem->data = data; 268 pH->count++; 269 if( pH->count>=10 && pH->count > 2*pH->htsize ){ 270 if( rehash(pH, pH->count*2) ){ 271 assert( pH->htsize>0 ); 272 h = strHash(pKey, nKey) % pH->htsize; 273 } 274 } 275 if( pH->ht ){ 276 insertElement(pH, &pH->ht[h], new_elem); 277 }else{ 278 insertElement(pH, 0, new_elem); 279 } 280 return 0; 281 } 282