drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 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. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 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. |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 20 | ** |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 21 | ** "pNew" is a pointer to the hash table that is to be initialized. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 22 | */ |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 23 | void sqlite3HashInit(Hash *pNew){ |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 24 | assert( pNew!=0 ); |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 25 | pNew->first = 0; |
| 26 | pNew->count = 0; |
| 27 | pNew->htsize = 0; |
| 28 | pNew->ht = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /* Remove all entries from a hash table. Reclaim all memory. |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 32 | ** Call this routine to delete a hash table or to reset a hash table |
| 33 | ** to the empty state. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 34 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 35 | void sqlite3HashClear(Hash *pH){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 36 | HashElem *elem; /* For looping over all elements of the table */ |
| 37 | |
| 38 | assert( pH!=0 ); |
| 39 | elem = pH->first; |
| 40 | pH->first = 0; |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 41 | sqlite3_free(pH->ht); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 42 | pH->ht = 0; |
| 43 | pH->htsize = 0; |
| 44 | while( elem ){ |
| 45 | HashElem *next_elem = elem->next; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 46 | sqlite3_free(elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 47 | elem = next_elem; |
| 48 | } |
| 49 | pH->count = 0; |
| 50 | } |
| 51 | |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 52 | /* |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 53 | ** The hashing function. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 54 | */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 55 | static unsigned int strHash(const char *z){ |
drh | dc3bb0d | 2014-01-24 16:36:18 +0000 | [diff] [blame] | 56 | unsigned int h = 0; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 57 | unsigned char c; |
| 58 | while( (c = (unsigned char)*z++)!=0 ){ |
| 59 | h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 60 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 61 | return h; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 62 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 63 | |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 64 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 65 | /* Link pNew element into the hash table pH. If pEntry!=0 then also |
| 66 | ** insert pNew into the pEntry hash bucket. |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 67 | */ |
| 68 | static void insertElement( |
| 69 | Hash *pH, /* The complete hash table */ |
| 70 | struct _ht *pEntry, /* The entry into which pNew is inserted */ |
| 71 | HashElem *pNew /* The element to be inserted */ |
| 72 | ){ |
| 73 | HashElem *pHead; /* First element already in pEntry */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 74 | if( pEntry ){ |
| 75 | pHead = pEntry->count ? pEntry->chain : 0; |
| 76 | pEntry->count++; |
| 77 | pEntry->chain = pNew; |
| 78 | }else{ |
| 79 | pHead = 0; |
| 80 | } |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 81 | if( pHead ){ |
| 82 | pNew->next = pHead; |
| 83 | pNew->prev = pHead->prev; |
| 84 | if( pHead->prev ){ pHead->prev->next = pNew; } |
| 85 | else { pH->first = pNew; } |
| 86 | pHead->prev = pNew; |
| 87 | }else{ |
| 88 | pNew->next = pH->first; |
| 89 | if( pH->first ){ pH->first->prev = pNew; } |
| 90 | pNew->prev = 0; |
| 91 | pH->first = pNew; |
| 92 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 96 | /* Resize the hash table so that it cantains "new_size" buckets. |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 97 | ** |
| 98 | ** The hash table might fail to resize if sqlite3_malloc() fails or |
| 99 | ** if the new size is the same as the prior size. |
| 100 | ** Return TRUE if the resize occurs and false if not. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 101 | */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 102 | static int rehash(Hash *pH, unsigned int new_size){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 103 | struct _ht *new_ht; /* The new hash table */ |
| 104 | HashElem *elem, *next_elem; /* For looping over existing elements */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 105 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 106 | #if SQLITE_MALLOC_SOFT_LIMIT>0 |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 107 | if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ |
| 108 | new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); |
| 109 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 110 | if( new_size==pH->htsize ) return 0; |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 111 | #endif |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 112 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 113 | /* The inability to allocates space for a larger hash table is |
| 114 | ** a performance hit but it is not a fatal error. So mark the |
dan | 38d0730 | 2012-08-07 15:19:27 +0000 | [diff] [blame] | 115 | ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of |
| 116 | ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() |
| 117 | ** only zeroes the requested number of bytes whereas this module will |
| 118 | ** use the actual amount of space allocated for the hash table (which |
| 119 | ** may be larger than the requested amount). |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 120 | */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 121 | sqlite3BeginBenignMalloc(); |
dan | 38d0730 | 2012-08-07 15:19:27 +0000 | [diff] [blame] | 122 | new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 123 | sqlite3EndBenignMalloc(); |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 124 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 125 | if( new_ht==0 ) return 0; |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 126 | sqlite3_free(pH->ht); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 127 | pH->ht = new_ht; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 128 | pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); |
dan | 38d0730 | 2012-08-07 15:19:27 +0000 | [diff] [blame] | 129 | memset(new_ht, 0, new_size*sizeof(struct _ht)); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 130 | for(elem=pH->first, pH->first=0; elem; elem = next_elem){ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 131 | unsigned int h = strHash(elem->pKey) % new_size; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 132 | next_elem = elem->next; |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 133 | insertElement(pH, &new_ht[h], elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 134 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 135 | return 1; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* This function (for internal use only) locates an element in an |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 139 | ** hash table that matches the given key. The hash for this key is |
| 140 | ** also computed and returned in the *pH parameter. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 141 | */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 142 | static HashElem *findElementWithHash( |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 143 | const Hash *pH, /* The pH to be searched */ |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 144 | const char *pKey, /* The key we are searching for */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 145 | unsigned int *pHash /* Write the hash value here */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 146 | ){ |
| 147 | HashElem *elem; /* Used to loop thru the element list */ |
| 148 | int count; /* Number of elements left to test */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 149 | unsigned int h; /* The computed hash */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 150 | |
| 151 | if( pH->ht ){ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 152 | struct _ht *pEntry; |
| 153 | h = strHash(pKey) % pH->htsize; |
| 154 | pEntry = &pH->ht[h]; |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 155 | elem = pEntry->chain; |
| 156 | count = pEntry->count; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 157 | }else{ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 158 | h = 0; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 159 | elem = pH->first; |
| 160 | count = pH->count; |
| 161 | } |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 162 | *pHash = h; |
| 163 | while( count-- ){ |
| 164 | assert( elem!=0 ); |
| 165 | if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 166 | return elem; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 167 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 168 | elem = elem->next; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 173 | /* Remove a single entry from the hash table given a pointer to that |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 174 | ** element and a hash on the element's key. |
| 175 | */ |
| 176 | static void removeElementGivenHash( |
| 177 | Hash *pH, /* The pH containing "elem" */ |
| 178 | HashElem* elem, /* The element to be removed from the pH */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 179 | unsigned int h /* Hash value for the element */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 180 | ){ |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 181 | struct _ht *pEntry; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 182 | if( elem->prev ){ |
| 183 | elem->prev->next = elem->next; |
| 184 | }else{ |
| 185 | pH->first = elem->next; |
| 186 | } |
| 187 | if( elem->next ){ |
| 188 | elem->next->prev = elem->prev; |
| 189 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 190 | if( pH->ht ){ |
| 191 | pEntry = &pH->ht[h]; |
| 192 | if( pEntry->chain==elem ){ |
| 193 | pEntry->chain = elem->next; |
| 194 | } |
| 195 | pEntry->count--; |
| 196 | assert( pEntry->count>=0 ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 197 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 198 | sqlite3_free( elem ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 199 | pH->count--; |
mistachkin | 5f070c7 | 2012-10-18 10:35:19 +0000 | [diff] [blame] | 200 | if( pH->count==0 ){ |
drh | 762e584 | 2005-10-03 15:11:08 +0000 | [diff] [blame] | 201 | assert( pH->first==0 ); |
| 202 | assert( pH->count==0 ); |
| 203 | sqlite3HashClear(pH); |
| 204 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 205 | } |
| 206 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 207 | /* Attempt to locate an element of the hash table pH with a key |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 208 | ** that matches pKey. Return the data for this element if it is |
danielk1977 | 7c836f0 | 2007-09-04 14:31:47 +0000 | [diff] [blame] | 209 | ** found, or NULL if there is no match. |
| 210 | */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 211 | void *sqlite3HashFind(const Hash *pH, const char *pKey){ |
danielk1977 | 7c836f0 | 2007-09-04 14:31:47 +0000 | [diff] [blame] | 212 | HashElem *elem; /* The element that matches key */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 213 | unsigned int h; /* A hash on key */ |
| 214 | |
| 215 | assert( pH!=0 ); |
| 216 | assert( pKey!=0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 217 | elem = findElementWithHash(pH, pKey, &h); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 218 | return elem ? elem->data : 0; |
| 219 | } |
| 220 | |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 221 | /* Insert an element into the hash table pH. The key is pKey |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 222 | ** and the data is "data". |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 223 | ** |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 224 | ** If no element exists with a matching key, then a new |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 225 | ** element is created and NULL is returned. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 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. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 229 | ** The key is not copied in this instance. If a malloc fails, then |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 230 | ** the new data is returned and the hash table is unchanged. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 231 | ** |
| 232 | ** If the "data" parameter to this function is NULL, then the |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 233 | ** element corresponding to "key" is removed from the hash table. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 234 | */ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 235 | void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 236 | unsigned int h; /* the hash of the key modulo hash table size */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 237 | HashElem *elem; /* Used to loop thru the element list */ |
| 238 | HashElem *new_elem; /* New element added to the pH */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 239 | |
| 240 | assert( pH!=0 ); |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 241 | assert( pKey!=0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 242 | elem = findElementWithHash(pH,pKey,&h); |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 243 | if( elem ){ |
| 244 | void *old_data = elem->data; |
| 245 | if( data==0 ){ |
| 246 | removeElementGivenHash(pH,elem,h); |
| 247 | }else{ |
| 248 | elem->data = data; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 249 | elem->pKey = pKey; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 250 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 251 | return old_data; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 252 | } |
| 253 | if( data==0 ) return 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 254 | new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 255 | if( new_elem==0 ) return data; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 256 | new_elem->pKey = pKey; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 257 | new_elem->data = data; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 258 | pH->count++; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 259 | if( pH->count>=10 && pH->count > 2*pH->htsize ){ |
drh | 782b873 | 2009-05-09 23:29:12 +0000 | [diff] [blame] | 260 | if( rehash(pH, pH->count*2) ){ |
| 261 | assert( pH->htsize>0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 262 | h = strHash(pKey) % pH->htsize; |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 263 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 264 | } |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 265 | insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |