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