4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This is the implementation of generic hash-tables
15 #include "sqliteInt.h"
18 /* Turn bulk memory into a hash table object by initializing the
19 ** fields of the Hash structure.
21 ** "pNew" is a pointer to the hash table that is to be initialized.
23 void sqlite3HashInit(Hash
*pNew
){
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.
35 void sqlite3HashClear(Hash
*pH
){
36 HashElem
*elem
; /* For looping over all elements of the table */
45 HashElem
*next_elem
= elem
->next
;
53 ** The hashing function.
55 static unsigned int strHash(const char *z
, int nKey
){
59 h
= (h
<<3) ^ h
^ sqlite3UpperToLower
[(unsigned char)*z
++];
66 /* Link pNew element into the hash table pH. If pEntry!=0 then also
67 ** insert pNew into the pEntry hash bucket.
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 */
74 HashElem
*pHead
; /* First element already in pEntry */
76 pHead
= pEntry
->count
? pEntry
->chain
: 0;
84 pNew
->prev
= pHead
->prev
;
85 if( pHead
->prev
){ pHead
->prev
->next
= pNew
; }
86 else { pH
->first
= pNew
; }
89 pNew
->next
= pH
->first
;
90 if( pH
->first
){ pH
->first
->prev
= pNew
; }
97 /* Resize the hash table so that it cantains "new_size" buckets.
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.
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 */
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
);
111 if( new_size
==pH
->htsize
) return 0;
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.
118 sqlite3BeginBenignMalloc();
119 new_ht
= (struct _ht
*)sqlite3Malloc( new_size
*sizeof(struct _ht
) );
120 sqlite3EndBenignMalloc();
122 if( new_ht
==0 ) return 0;
123 sqlite3_free(pH
->ht
);
125 pH
->htsize
= new_size
= sqlite3MallocSize(new_ht
)/sizeof(struct _ht
);
126 memset(new_ht
, 0, new_size
*sizeof(struct _ht
));
127 for(elem
=pH
->first
, pH
->first
=0; elem
; elem
= next_elem
){
128 unsigned int h
= strHash(elem
->pKey
, elem
->nKey
) % new_size
;
129 next_elem
= elem
->next
;
130 insertElement(pH
, &new_ht
[h
], elem
);
135 /* This function (for internal use only) locates an element in an
136 ** hash table that matches the given key. The hash for this key has
137 ** already been computed and is passed as the 4th parameter.
139 static HashElem
*findElementGivenHash(
140 const Hash
*pH
, /* The pH to be searched */
141 const char *pKey
, /* The key we are searching for */
142 int nKey
, /* Bytes in key (not counting zero terminator) */
143 unsigned int h
/* The hash for this key. */
145 HashElem
*elem
; /* Used to loop thru the element list */
146 int count
; /* Number of elements left to test */
149 struct _ht
*pEntry
= &pH
->ht
[h
];
150 elem
= pEntry
->chain
;
151 count
= pEntry
->count
;
156 while( count
-- && ALWAYS(elem
) ){
157 if( elem
->nKey
==nKey
&& sqlite3StrNICmp(elem
->pKey
,pKey
,nKey
)==0 ){
165 /* Remove a single entry from the hash table given a pointer to that
166 ** element and a hash on the element's key.
168 static void removeElementGivenHash(
169 Hash
*pH
, /* The pH containing "elem" */
170 HashElem
* elem
, /* The element to be removed from the pH */
171 unsigned int h
/* Hash value for the element */
175 elem
->prev
->next
= elem
->next
;
177 pH
->first
= elem
->next
;
180 elem
->next
->prev
= elem
->prev
;
184 if( pEntry
->chain
==elem
){
185 pEntry
->chain
= elem
->next
;
188 assert( pEntry
->count
>=0 );
190 sqlite3_free( elem
);
193 assert( pH
->first
==0 );
194 assert( pH
->count
==0 );
195 sqlite3HashClear(pH
);
199 /* Attempt to locate an element of the hash table pH with a key
200 ** that matches pKey,nKey. Return the data for this element if it is
201 ** found, or NULL if there is no match.
203 void *sqlite3HashFind(const Hash
*pH
, const char *pKey
, int nKey
){
204 HashElem
*elem
; /* The element that matches key */
205 unsigned int h
; /* A hash on key */
211 h
= strHash(pKey
, nKey
) % pH
->htsize
;
215 elem
= findElementGivenHash(pH
, pKey
, nKey
, h
);
216 return elem
? elem
->data
: 0;
219 /* Insert an element into the hash table pH. The key is pKey,nKey
220 ** and the data is "data".
222 ** If no element exists with a matching key, then a new
223 ** element is created and NULL is returned.
225 ** If another element already exists with the same key, then the
226 ** new data replaces the old data and the old data is returned.
227 ** The key is not copied in this instance. If a malloc fails, then
228 ** the new data is returned and the hash table is unchanged.
230 ** If the "data" parameter to this function is NULL, then the
231 ** element corresponding to "key" is removed from the hash table.
233 void *sqlite3HashInsert(Hash
*pH
, const char *pKey
, int nKey
, void *data
){
234 unsigned int h
; /* the hash of the key modulo hash table size */
235 HashElem
*elem
; /* Used to loop thru the element list */
236 HashElem
*new_elem
; /* New element added to the pH */
242 h
= strHash(pKey
, nKey
) % pH
->htsize
;
246 elem
= findElementGivenHash(pH
,pKey
,nKey
,h
);
248 void *old_data
= elem
->data
;
250 removeElementGivenHash(pH
,elem
,h
);
254 assert(nKey
==elem
->nKey
);
258 if( data
==0 ) return 0;
259 new_elem
= (HashElem
*)sqlite3Malloc( sizeof(HashElem
) );
260 if( new_elem
==0 ) return data
;
261 new_elem
->pKey
= pKey
;
262 new_elem
->nKey
= nKey
;
263 new_elem
->data
= data
;
265 if( pH
->count
>=10 && pH
->count
> 2*pH
->htsize
){
266 if( rehash(pH
, pH
->count
*2) ){
267 assert( pH
->htsize
>0 );
268 h
= strHash(pKey
, nKey
) % pH
->htsize
;
272 insertElement(pH
, &pH
->ht
[h
], new_elem
);
274 insertElement(pH
, 0, new_elem
);