hash.c 8.0 KB

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