sqlcipher/src/hash.c

279 lines
7.7 KiB
C
Raw Normal View History

2008-07-30 13:15:43 +00:00
/*
** 2001 September 22
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This is the implementation of generic hash-tables
** used in SQLite.
**
2009-05-08 09:27:02 +00:00
** $Id: hash.c,v 1.37 2009/05/02 13:29:38 drh Exp $
2008-07-30 13:15:43 +00:00
*/
#include "sqliteInt.h"
#include <assert.h>
/* Turn bulk memory into a hash table object by initializing the
** fields of the Hash structure.
**
** "pNew" is a pointer to the hash table that is to be initialized.
*/
2009-05-08 09:27:02 +00:00
void sqlite3HashInit(Hash *pNew){
2008-07-30 13:15:43 +00:00
assert( pNew!=0 );
pNew->first = 0;
pNew->count = 0;
pNew->htsize = 0;
pNew->ht = 0;
}
/* Remove all entries from a hash table. Reclaim all memory.
** Call this routine to delete a hash table or to reset a hash table
** to the empty state.
*/
void sqlite3HashClear(Hash *pH){
HashElem *elem; /* For looping over all elements of the table */
assert( pH!=0 );
elem = pH->first;
pH->first = 0;
sqlite3_free(pH->ht);
pH->ht = 0;
pH->htsize = 0;
while( elem ){
HashElem *next_elem = elem->next;
sqlite3_free(elem);
elem = next_elem;
}
pH->count = 0;
}
/*
2009-05-08 09:27:02 +00:00
** The hashing function.
2008-07-30 13:15:43 +00:00
*/
2009-05-08 09:27:02 +00:00
static unsigned int strHash(const char *z, int nKey){
2008-07-30 13:15:43 +00:00
int h = 0;
2009-05-08 09:27:02 +00:00
assert( nKey>=0 );
2008-07-30 13:15:43 +00:00
while( nKey > 0 ){
h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
nKey--;
}
2009-05-08 09:27:02 +00:00
return h;
2008-07-30 13:15:43 +00:00
}
2009-05-08 09:27:02 +00:00
/* Link pNew element into the hash table pH. If pEntry!=0 then also
** insert pNew into the pEntry hash bucket.
2008-07-30 13:15:43 +00:00
*/
static void insertElement(
Hash *pH, /* The complete hash table */
struct _ht *pEntry, /* The entry into which pNew is inserted */
HashElem *pNew /* The element to be inserted */
){
HashElem *pHead; /* First element already in pEntry */
2009-05-08 09:27:02 +00:00
if( pEntry ){
pHead = pEntry->count ? pEntry->chain : 0;
pEntry->count++;
pEntry->chain = pNew;
}else{
pHead = 0;
}
2008-07-30 13:15:43 +00:00
if( pHead ){
pNew->next = pHead;
pNew->prev = pHead->prev;
if( pHead->prev ){ pHead->prev->next = pNew; }
else { pH->first = pNew; }
pHead->prev = pNew;
}else{
pNew->next = pH->first;
if( pH->first ){ pH->first->prev = pNew; }
pNew->prev = 0;
pH->first = pNew;
}
}
/* Resize the hash table so that it cantains "new_size" buckets.
2009-05-08 09:27:02 +00:00
**
** The hash table might fail to resize if sqlite3_malloc() fails or
** if the new size is the same as the prior size.
** Return TRUE if the resize occurs and false if not.
2008-07-30 13:15:43 +00:00
*/
2009-05-08 09:27:02 +00:00
static int rehash(Hash *pH, unsigned int new_size){
2008-07-30 13:15:43 +00:00
struct _ht *new_ht; /* The new hash table */
HashElem *elem, *next_elem; /* For looping over existing elements */
2009-05-08 09:27:02 +00:00
#if SQLITE_MALLOC_SOFT_LIMIT>0
2008-07-30 13:15:43 +00:00
if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
}
2009-05-08 09:27:02 +00:00
if( new_size==pH->htsize ) return 0;
2008-07-30 13:15:43 +00:00
#endif
2009-05-08 09:27:02 +00:00
/* The inability to allocates space for a larger hash table is
** a performance hit but it is not a fatal error. So mark the
** allocation as a benign.
2008-07-30 13:15:43 +00:00
*/
2009-05-08 09:27:02 +00:00
sqlite3BeginBenignMalloc();
new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
sqlite3EndBenignMalloc();
2008-07-30 13:15:43 +00:00
2009-05-08 09:27:02 +00:00
if( new_ht==0 ) return 0;
2008-07-30 13:15:43 +00:00
sqlite3_free(pH->ht);
pH->ht = new_ht;
2009-05-08 09:27:02 +00:00
pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
memset(new_ht, 0, new_size*sizeof(struct _ht));
2008-07-30 13:15:43 +00:00
for(elem=pH->first, pH->first=0; elem; elem = next_elem){
2009-05-08 09:27:02 +00:00
unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
2008-07-30 13:15:43 +00:00
next_elem = elem->next;
insertElement(pH, &new_ht[h], elem);
}
2009-05-08 09:27:02 +00:00
return 1;
2008-07-30 13:15:43 +00:00
}
/* This function (for internal use only) locates an element in an
** hash table that matches the given key. The hash for this key has
** already been computed and is passed as the 4th parameter.
*/
static HashElem *findElementGivenHash(
const Hash *pH, /* The pH to be searched */
2009-05-08 09:27:02 +00:00
const char *pKey, /* The key we are searching for */
int nKey, /* Bytes in key (not counting zero terminator) */
unsigned int h /* The hash for this key. */
2008-07-30 13:15:43 +00:00
){
HashElem *elem; /* Used to loop thru the element list */
int count; /* Number of elements left to test */
if( pH->ht ){
struct _ht *pEntry = &pH->ht[h];
elem = pEntry->chain;
count = pEntry->count;
2009-05-08 09:27:02 +00:00
}else{
elem = pH->first;
count = pH->count;
}
while( count-- && ALWAYS(elem) ){
if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
return elem;
2008-07-30 13:15:43 +00:00
}
2009-05-08 09:27:02 +00:00
elem = elem->next;
2008-07-30 13:15:43 +00:00
}
return 0;
}
/* Remove a single entry from the hash table given a pointer to that
** element and a hash on the element's key.
*/
static void removeElementGivenHash(
Hash *pH, /* The pH containing "elem" */
HashElem* elem, /* The element to be removed from the pH */
2009-05-08 09:27:02 +00:00
unsigned int h /* Hash value for the element */
2008-07-30 13:15:43 +00:00
){
struct _ht *pEntry;
if( elem->prev ){
elem->prev->next = elem->next;
}else{
pH->first = elem->next;
}
if( elem->next ){
elem->next->prev = elem->prev;
}
2009-05-08 09:27:02 +00:00
if( pH->ht ){
pEntry = &pH->ht[h];
if( pEntry->chain==elem ){
pEntry->chain = elem->next;
}
pEntry->count--;
assert( pEntry->count>=0 );
2008-07-30 13:15:43 +00:00
}
sqlite3_free( elem );
pH->count--;
if( pH->count<=0 ){
assert( pH->first==0 );
assert( pH->count==0 );
sqlite3HashClear(pH);
}
}
/* Attempt to locate an element of the hash table pH with a key
** that matches pKey,nKey. Return the data for this element if it is
** found, or NULL if there is no match.
*/
2009-05-08 09:27:02 +00:00
void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
2008-07-30 13:15:43 +00:00
HashElem *elem; /* The element that matches key */
2009-05-08 09:27:02 +00:00
unsigned int h; /* A hash on key */
assert( pH!=0 );
assert( pKey!=0 );
assert( nKey>=0 );
if( pH->ht ){
h = strHash(pKey, nKey) % pH->htsize;
}else{
h = 0;
}
elem = findElementGivenHash(pH, pKey, nKey, h);
2008-07-30 13:15:43 +00:00
return elem ? elem->data : 0;
}
/* Insert an element into the hash table pH. The key is pKey,nKey
** and the data is "data".
**
** If no element exists with a matching key, then a new
2009-05-08 09:27:02 +00:00
** element is created and NULL is returned.
2008-07-30 13:15:43 +00:00
**
** If another element already exists with the same key, then the
** new data replaces the old data and the old data is returned.
** The key is not copied in this instance. If a malloc fails, then
** the new data is returned and the hash table is unchanged.
**
** If the "data" parameter to this function is NULL, then the
** element corresponding to "key" is removed from the hash table.
*/
2009-05-08 09:27:02 +00:00
void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
unsigned int h; /* the hash of the key modulo hash table size */
2008-07-30 13:15:43 +00:00
HashElem *elem; /* Used to loop thru the element list */
HashElem *new_elem; /* New element added to the pH */
assert( pH!=0 );
2009-05-08 09:27:02 +00:00
assert( pKey!=0 );
assert( nKey>=0 );
2008-07-30 13:15:43 +00:00
if( pH->htsize ){
2009-05-08 09:27:02 +00:00
h = strHash(pKey, nKey) % pH->htsize;
}else{
h = 0;
}
elem = findElementGivenHash(pH,pKey,nKey,h);
if( elem ){
void *old_data = elem->data;
if( data==0 ){
removeElementGivenHash(pH,elem,h);
}else{
elem->data = data;
elem->pKey = pKey;
assert(nKey==elem->nKey);
2008-07-30 13:15:43 +00:00
}
2009-05-08 09:27:02 +00:00
return old_data;
2008-07-30 13:15:43 +00:00
}
if( data==0 ) return 0;
new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
if( new_elem==0 ) return data;
2009-05-08 09:27:02 +00:00
new_elem->pKey = pKey;
2008-07-30 13:15:43 +00:00
new_elem->nKey = nKey;
2009-05-08 09:27:02 +00:00
new_elem->data = data;
2008-07-30 13:15:43 +00:00
pH->count++;
2009-05-08 09:27:02 +00:00
if( pH->count>=10 && pH->count > 2*pH->htsize ){
if( rehash(pH, pH->count*2) && pH->htsize ){
h = strHash(pKey, nKey) % pH->htsize;
2008-07-30 13:15:43 +00:00
}
}
2009-05-08 09:27:02 +00:00
if( pH->ht ){
insertElement(pH, &pH->ht[h], new_elem);
}else{
insertElement(pH, 0, new_elem);
2008-07-30 13:15:43 +00:00
}
return 0;
}