Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_shash.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_shash.c,v 12.1 2005/06/16 20:21:56 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 #endif
00015 
00016 #include "db_int.h"
00017 
00018 /*
00019  * Table of good hash values.  Up to ~250,000 buckets, we use powers of 2.
00020  * After that, we slow the rate of increase by half.  For each choice, we
00021  * then use a nearby prime number as the hash value.
00022  *
00023  * If a terabyte is the maximum cache we'll see, and we assume there are
00024  * 10 1K buckets on each hash chain, then 107374182 is the maximum number
00025  * of buckets we'll ever need.
00026  */
00027 static const struct {
00028         u_int32_t power;
00029         u_int32_t prime;
00030 } list[] = {
00031         {        32,            37},            /* 2^5 */
00032         {        64,            67},            /* 2^6 */
00033         {       128,           131},            /* 2^7 */
00034         {       256,           257},            /* 2^8 */
00035         {       512,           521},            /* 2^9 */
00036         {      1024,          1031},            /* 2^10 */
00037         {      2048,          2053},            /* 2^11 */
00038         {      4096,          4099},            /* 2^12 */
00039         {      8192,          8191},            /* 2^13 */
00040         {     16384,         16381},            /* 2^14 */
00041         {     32768,         32771},            /* 2^15 */
00042         {     65536,         65537},            /* 2^16 */
00043         {    131072,        131071},            /* 2^17 */
00044         {    262144,        262147},            /* 2^18 */
00045         {    393216,        393209},            /* 2^18 + 2^18/2 */
00046         {    524288,        524287},            /* 2^19 */
00047         {    786432,        786431},            /* 2^19 + 2^19/2 */
00048         {   1048576,       1048573},            /* 2^20 */
00049         {   1572864,       1572869},            /* 2^20 + 2^20/2 */
00050         {   2097152,       2097169},            /* 2^21 */
00051         {   3145728,       3145721},            /* 2^21 + 2^21/2 */
00052         {   4194304,       4194301},            /* 2^22 */
00053         {   6291456,       6291449},            /* 2^22 + 2^22/2 */
00054         {   8388608,       8388617},            /* 2^23 */
00055         {  12582912,      12582917},            /* 2^23 + 2^23/2 */
00056         {  16777216,      16777213},            /* 2^24 */
00057         {  25165824,      25165813},            /* 2^24 + 2^24/2 */
00058         {  33554432,      33554393},            /* 2^25 */
00059         {  50331648,      50331653},            /* 2^25 + 2^25/2 */
00060         {  67108864,      67108859},            /* 2^26 */
00061         { 100663296,     100663291},            /* 2^26 + 2^26/2 */
00062         { 134217728,     134217757},            /* 2^27 */
00063         { 201326592,     201326611},            /* 2^27 + 2^27/2 */
00064         { 268435456,     268435459},            /* 2^28 */
00065         { 402653184,     402653189},            /* 2^28 + 2^28/2 */
00066         { 536870912,     536870909},            /* 2^29 */
00067         { 805306368,     805306357},            /* 2^29 + 2^29/2 */
00068         {1073741824,    1073741827},            /* 2^30 */
00069         {0,             0}
00070 };
00071 
00072 /*
00073  * __db_tablesize --
00074  *      Choose a size for the hash table.
00075  *
00076  * PUBLIC: u_int32_t __db_tablesize __P((u_int32_t));
00077  */
00078 u_int32_t
00079 __db_tablesize(n_buckets)
00080         u_int32_t n_buckets;
00081 {
00082         u_int i;
00083 
00084         /*
00085          * We try to be clever about how big we make the hash tables.  Use a
00086          * prime number close to the "suggested" number of elements that will
00087          * be in the hash table.  Use 64 as the minimum hash table size.
00088          *
00089          * Ref: Sedgewick, Algorithms in C, "Hash Functions"
00090          */
00091         if (n_buckets < 32)
00092                 n_buckets = 32;
00093 
00094         for (i = 0; i < sizeof(list)/sizeof(list[0]); ++i)
00095                 if (list[i].power >= n_buckets)
00096                         return (list[i].prime);
00097         return (list[--i].prime);
00098 }
00099 
00100 /*
00101  * __db_hashinit --
00102  *      Initialize a hash table that resides in shared memory.
00103  *
00104  * PUBLIC: void __db_hashinit __P((void *, u_int32_t));
00105  */
00106 void
00107 __db_hashinit(begin, nelements)
00108         void *begin;
00109         u_int32_t nelements;
00110 {
00111         u_int32_t i;
00112         SH_TAILQ_HEAD(hash_head) *headp;
00113 
00114         headp = (struct hash_head *)begin;
00115 
00116         for (i = 0; i < nelements; i++, headp++)
00117                 SH_TAILQ_INIT(headp);
00118 }

Generated on Sun Dec 25 12:14:24 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2