blob: 62b695a1bb3447b98f7bffb719ecb0a782e13885 [file] [log] [blame]
drhbeae3192001-09-22 18:12:08 +00001/*
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.
drhbeae3192001-09-22 18:12:08 +000014*/
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.
drhaacc5432002-01-06 17:07:40 +000020**
drh76d7f8b2004-06-30 22:43:21 +000021** "pNew" is a pointer to the hash table that is to be initialized.
drhbeae3192001-09-22 18:12:08 +000022*/
drhe61922a2009-05-02 13:29:37 +000023void sqlite3HashInit(Hash *pNew){
drh76d7f8b2004-06-30 22:43:21 +000024 assert( pNew!=0 );
drh76d7f8b2004-06-30 22:43:21 +000025 pNew->first = 0;
26 pNew->count = 0;
27 pNew->htsize = 0;
28 pNew->ht = 0;
drhbeae3192001-09-22 18:12:08 +000029}
30
31/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000032** Call this routine to delete a hash table or to reset a hash table
33** to the empty state.
drhbeae3192001-09-22 18:12:08 +000034*/
danielk19774adee202004-05-08 08:23:19 +000035void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000036 HashElem *elem; /* For looping over all elements of the table */
37
38 assert( pH!=0 );
39 elem = pH->first;
40 pH->first = 0;
drh41eb9e92008-04-02 18:33:07 +000041 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +000042 pH->ht = 0;
43 pH->htsize = 0;
44 while( elem ){
45 HashElem *next_elem = elem->next;
drh17435752007-08-16 04:30:38 +000046 sqlite3_free(elem);
drhbeae3192001-09-22 18:12:08 +000047 elem = next_elem;
48 }
49 pH->count = 0;
50}
51
drhbeae3192001-09-22 18:12:08 +000052/*
drhe61922a2009-05-02 13:29:37 +000053** The hashing function.
drhbeae3192001-09-22 18:12:08 +000054*/
drhacbcb7e2014-08-21 20:26:37 +000055static unsigned int strHash(const char *z){
drhdc3bb0d2014-01-24 16:36:18 +000056 unsigned int h = 0;
drhacbcb7e2014-08-21 20:26:37 +000057 unsigned char c;
drh75ab50c2016-04-28 14:15:12 +000058 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
drh5f33eaa2016-09-28 20:42:31 +000059 /* 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;
danielk197752a83fb2005-01-31 12:56:44 +000064 }
drh8a1e5942009-04-28 15:43:45 +000065 return h;
drhbeae3192001-09-22 18:12:08 +000066}
drhbeae3192001-09-22 18:12:08 +000067
drhe8cf2ca2004-08-20 14:08:50 +000068
drh8a1e5942009-04-28 15:43:45 +000069/* Link pNew element into the hash table pH. If pEntry!=0 then also
70** insert pNew into the pEntry hash bucket.
drhe8cf2ca2004-08-20 14:08:50 +000071*/
72static void insertElement(
73 Hash *pH, /* The complete hash table */
74 struct _ht *pEntry, /* The entry into which pNew is inserted */
75 HashElem *pNew /* The element to be inserted */
76){
77 HashElem *pHead; /* First element already in pEntry */
drh8a1e5942009-04-28 15:43:45 +000078 if( pEntry ){
79 pHead = pEntry->count ? pEntry->chain : 0;
80 pEntry->count++;
81 pEntry->chain = pNew;
82 }else{
83 pHead = 0;
84 }
drhe8cf2ca2004-08-20 14:08:50 +000085 if( pHead ){
86 pNew->next = pHead;
87 pNew->prev = pHead->prev;
88 if( pHead->prev ){ pHead->prev->next = pNew; }
89 else { pH->first = pNew; }
90 pHead->prev = pNew;
91 }else{
92 pNew->next = pH->first;
93 if( pH->first ){ pH->first->prev = pNew; }
94 pNew->prev = 0;
95 pH->first = pNew;
96 }
drhbeae3192001-09-22 18:12:08 +000097}
98
99
drhaacc5432002-01-06 17:07:40 +0000100/* Resize the hash table so that it cantains "new_size" buckets.
drh8a1e5942009-04-28 15:43:45 +0000101**
102** The hash table might fail to resize if sqlite3_malloc() fails or
103** if the new size is the same as the prior size.
104** Return TRUE if the resize occurs and false if not.
drhbeae3192001-09-22 18:12:08 +0000105*/
drh8a1e5942009-04-28 15:43:45 +0000106static int rehash(Hash *pH, unsigned int new_size){
drhbeae3192001-09-22 18:12:08 +0000107 struct _ht *new_ht; /* The new hash table */
108 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000109
drh8a1e5942009-04-28 15:43:45 +0000110#if SQLITE_MALLOC_SOFT_LIMIT>0
drheee4c8c2008-02-18 22:24:57 +0000111 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
112 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
113 }
drh8a1e5942009-04-28 15:43:45 +0000114 if( new_size==pH->htsize ) return 0;
drheee4c8c2008-02-18 22:24:57 +0000115#endif
danielk1977a1644fd2007-08-29 12:31:25 +0000116
drh8a1e5942009-04-28 15:43:45 +0000117 /* The inability to allocates space for a larger hash table is
118 ** a performance hit but it is not a fatal error. So mark the
dan38d07302012-08-07 15:19:27 +0000119 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
120 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
121 ** only zeroes the requested number of bytes whereas this module will
122 ** use the actual amount of space allocated for the hash table (which
123 ** may be larger than the requested amount).
danielk1977a1644fd2007-08-29 12:31:25 +0000124 */
drh8a1e5942009-04-28 15:43:45 +0000125 sqlite3BeginBenignMalloc();
dan38d07302012-08-07 15:19:27 +0000126 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
drh8a1e5942009-04-28 15:43:45 +0000127 sqlite3EndBenignMalloc();
drh643167f2008-01-22 21:30:53 +0000128
drh8a1e5942009-04-28 15:43:45 +0000129 if( new_ht==0 ) return 0;
drh41eb9e92008-04-02 18:33:07 +0000130 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000131 pH->ht = new_ht;
drh8a1e5942009-04-28 15:43:45 +0000132 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
dan38d07302012-08-07 15:19:27 +0000133 memset(new_ht, 0, new_size*sizeof(struct _ht));
drhbeae3192001-09-22 18:12:08 +0000134 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
drhacbcb7e2014-08-21 20:26:37 +0000135 unsigned int h = strHash(elem->pKey) % new_size;
drhbeae3192001-09-22 18:12:08 +0000136 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000137 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000138 }
drh8a1e5942009-04-28 15:43:45 +0000139 return 1;
drhbeae3192001-09-22 18:12:08 +0000140}
141
142/* This function (for internal use only) locates an element in an
drhacbcb7e2014-08-21 20:26:37 +0000143** hash table that matches the given key. The hash for this key is
144** also computed and returned in the *pH parameter.
drhbeae3192001-09-22 18:12:08 +0000145*/
drhacbcb7e2014-08-21 20:26:37 +0000146static HashElem *findElementWithHash(
drhbeae3192001-09-22 18:12:08 +0000147 const Hash *pH, /* The pH to be searched */
drhe61922a2009-05-02 13:29:37 +0000148 const char *pKey, /* The key we are searching for */
drhacbcb7e2014-08-21 20:26:37 +0000149 unsigned int *pHash /* Write the hash value here */
drhbeae3192001-09-22 18:12:08 +0000150){
151 HashElem *elem; /* Used to loop thru the element list */
152 int count; /* Number of elements left to test */
drhacbcb7e2014-08-21 20:26:37 +0000153 unsigned int h; /* The computed hash */
drhbeae3192001-09-22 18:12:08 +0000154
drh75ab50c2016-04-28 14:15:12 +0000155 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
drhacbcb7e2014-08-21 20:26:37 +0000156 struct _ht *pEntry;
157 h = strHash(pKey) % pH->htsize;
158 pEntry = &pH->ht[h];
drhe8cf2ca2004-08-20 14:08:50 +0000159 elem = pEntry->chain;
160 count = pEntry->count;
drh8a1e5942009-04-28 15:43:45 +0000161 }else{
drhacbcb7e2014-08-21 20:26:37 +0000162 h = 0;
drh8a1e5942009-04-28 15:43:45 +0000163 elem = pH->first;
164 count = pH->count;
165 }
drhacbcb7e2014-08-21 20:26:37 +0000166 *pHash = h;
167 while( count-- ){
168 assert( elem!=0 );
169 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
drh8a1e5942009-04-28 15:43:45 +0000170 return elem;
drhbeae3192001-09-22 18:12:08 +0000171 }
drh8a1e5942009-04-28 15:43:45 +0000172 elem = elem->next;
drhbeae3192001-09-22 18:12:08 +0000173 }
174 return 0;
175}
176
drh81a20f22001-10-12 17:30:04 +0000177/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000178** element and a hash on the element's key.
179*/
180static void removeElementGivenHash(
181 Hash *pH, /* The pH containing "elem" */
182 HashElem* elem, /* The element to be removed from the pH */
drh8a1e5942009-04-28 15:43:45 +0000183 unsigned int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000184){
drhe8cf2ca2004-08-20 14:08:50 +0000185 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000186 if( elem->prev ){
187 elem->prev->next = elem->next;
188 }else{
189 pH->first = elem->next;
190 }
191 if( elem->next ){
192 elem->next->prev = elem->prev;
193 }
drh8a1e5942009-04-28 15:43:45 +0000194 if( pH->ht ){
195 pEntry = &pH->ht[h];
196 if( pEntry->chain==elem ){
197 pEntry->chain = elem->next;
198 }
199 pEntry->count--;
200 assert( pEntry->count>=0 );
drhbeae3192001-09-22 18:12:08 +0000201 }
drh17435752007-08-16 04:30:38 +0000202 sqlite3_free( elem );
drhbeae3192001-09-22 18:12:08 +0000203 pH->count--;
mistachkin5f070c72012-10-18 10:35:19 +0000204 if( pH->count==0 ){
drh762e5842005-10-03 15:11:08 +0000205 assert( pH->first==0 );
206 assert( pH->count==0 );
207 sqlite3HashClear(pH);
208 }
drhbeae3192001-09-22 18:12:08 +0000209}
210
drhaacc5432002-01-06 17:07:40 +0000211/* Attempt to locate an element of the hash table pH with a key
drhacbcb7e2014-08-21 20:26:37 +0000212** that matches pKey. Return the data for this element if it is
danielk19777c836f02007-09-04 14:31:47 +0000213** found, or NULL if there is no match.
214*/
drhacbcb7e2014-08-21 20:26:37 +0000215void *sqlite3HashFind(const Hash *pH, const char *pKey){
danielk19777c836f02007-09-04 14:31:47 +0000216 HashElem *elem; /* The element that matches key */
drh8a1e5942009-04-28 15:43:45 +0000217 unsigned int h; /* A hash on key */
218
219 assert( pH!=0 );
220 assert( pKey!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000221 elem = findElementWithHash(pH, pKey, &h);
drhbeae3192001-09-22 18:12:08 +0000222 return elem ? elem->data : 0;
223}
224
drhacbcb7e2014-08-21 20:26:37 +0000225/* Insert an element into the hash table pH. The key is pKey
drh81a20f22001-10-12 17:30:04 +0000226** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000227**
drh81a20f22001-10-12 17:30:04 +0000228** If no element exists with a matching key, then a new
drhe61922a2009-05-02 13:29:37 +0000229** element is created and NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000230**
231** If another element already exists with the same key, then the
232** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000233** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000234** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000235**
236** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000237** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000238*/
drhacbcb7e2014-08-21 20:26:37 +0000239void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
drh8a1e5942009-04-28 15:43:45 +0000240 unsigned int h; /* the hash of the key modulo hash table size */
drhbeae3192001-09-22 18:12:08 +0000241 HashElem *elem; /* Used to loop thru the element list */
242 HashElem *new_elem; /* New element added to the pH */
drhbeae3192001-09-22 18:12:08 +0000243
244 assert( pH!=0 );
drh8a1e5942009-04-28 15:43:45 +0000245 assert( pKey!=0 );
drhacbcb7e2014-08-21 20:26:37 +0000246 elem = findElementWithHash(pH,pKey,&h);
drh8a1e5942009-04-28 15:43:45 +0000247 if( elem ){
248 void *old_data = elem->data;
249 if( data==0 ){
250 removeElementGivenHash(pH,elem,h);
251 }else{
252 elem->data = data;
drhe61922a2009-05-02 13:29:37 +0000253 elem->pKey = pKey;
drhbeae3192001-09-22 18:12:08 +0000254 }
drh8a1e5942009-04-28 15:43:45 +0000255 return old_data;
drhbeae3192001-09-22 18:12:08 +0000256 }
257 if( data==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000258 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000259 if( new_elem==0 ) return data;
drhe61922a2009-05-02 13:29:37 +0000260 new_elem->pKey = pKey;
drh8a1e5942009-04-28 15:43:45 +0000261 new_elem->data = data;
drhbeae3192001-09-22 18:12:08 +0000262 pH->count++;
drh8a1e5942009-04-28 15:43:45 +0000263 if( pH->count>=10 && pH->count > 2*pH->htsize ){
drh782b8732009-05-09 23:29:12 +0000264 if( rehash(pH, pH->count*2) ){
265 assert( pH->htsize>0 );
drhacbcb7e2014-08-21 20:26:37 +0000266 h = strHash(pKey) % pH->htsize;
drhe8cf2ca2004-08-20 14:08:50 +0000267 }
drhbeae3192001-09-22 18:12:08 +0000268 }
drhacbcb7e2014-08-21 20:26:37 +0000269 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
drhbeae3192001-09-22 18:12:08 +0000270 return 0;
271}