1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Margo Seltzer. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)hash.h 8.3 (Berkeley) 5/31/94 35 * $FreeBSD$ 36 */ 37 38 /* Operations */ 39 typedef enum { 40 HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT 41 } ACTION; 42 43 /* Buffer Management structures */ 44 typedef struct _bufhead BUFHEAD; 45 46 struct _bufhead { 47 BUFHEAD *prev; /* LRU links */ 48 BUFHEAD *next; /* LRU links */ 49 BUFHEAD *ovfl; /* Overflow page buffer header */ 50 u_int32_t addr; /* Address of this page */ 51 char *page; /* Actual page data */ 52 char flags; 53 #define BUF_MOD 0x0001 54 #define BUF_DISK 0x0002 55 #define BUF_BUCKET 0x0004 56 #define BUF_PIN 0x0008 57 }; 58 59 #define IS_BUCKET(X) ((X) & BUF_BUCKET) 60 61 typedef BUFHEAD **SEGMENT; 62 63 /* Hash Table Information */ 64 typedef struct hashhdr { /* Disk resident portion */ 65 int32_t magic; /* Magic NO for hash tables */ 66 int32_t version; /* Version ID */ 67 u_int32_t lorder; /* Byte Order */ 68 int32_t bsize; /* Bucket/Page Size */ 69 int32_t bshift; /* Bucket shift */ 70 int32_t dsize; /* Directory Size */ 71 int32_t ssize; /* Segment Size */ 72 int32_t sshift; /* Segment shift */ 73 int32_t ovfl_point; /* Where overflow pages are being 74 * allocated */ 75 int32_t last_freed; /* Last overflow page freed */ 76 u_int32_t max_bucket; /* ID of Maximum bucket in use */ 77 u_int32_t high_mask; /* Mask to modulo into entire table */ 78 u_int32_t low_mask; /* Mask to modulo into lower half of 79 * table */ 80 u_int32_t ffactor; /* Fill factor */ 81 int32_t nkeys; /* Number of keys in hash table */ 82 int32_t hdrpages; /* Size of table header */ 83 int32_t h_charkey; /* value of hash(CHARKEY) */ 84 #define NCACHED 32 /* number of bit maps and spare 85 * points */ 86 int32_t spares[NCACHED];/* spare pages for overflow */ 87 u_int16_t bitmaps[NCACHED]; /* address of overflow page 88 * bitmaps */ 89 } HASHHDR; 90 91 typedef struct htab { /* Memory resident data structure */ 92 HASHHDR hdr; /* Header */ 93 int nsegs; /* Number of allocated segments */ 94 int exsegs; /* Number of extra allocated 95 * segments */ 96 u_int32_t /* Hash function */ 97 (*hash)(const void *, size_t); 98 int flags; /* Flag values */ 99 int fp; /* File pointer */ 100 char *tmp_buf; /* Temporary Buffer for BIG data */ 101 char *tmp_key; /* Temporary Buffer for BIG keys */ 102 BUFHEAD *cpage; /* Current page */ 103 int cbucket; /* Current bucket */ 104 int cndx; /* Index of next item on cpage */ 105 int error; /* Error Number -- for DBM 106 * compatibility */ 107 int new_file; /* Indicates if fd is backing store 108 * or no */ 109 int save_file; /* Indicates whether we need to flush 110 * file at 111 * exit */ 112 u_int32_t *mapp[NCACHED]; /* Pointers to page maps */ 113 int nmaps; /* Initial number of bitmaps */ 114 int nbufs; /* Number of buffers left to 115 * allocate */ 116 BUFHEAD bufhead; /* Header of buffer lru list */ 117 SEGMENT *dir; /* Hash Bucket directory */ 118 } HTAB; 119 120 /* 121 * Constants 122 */ 123 #define MAX_BSIZE 32768 /* 2^15 but should be 65536 */ 124 #define MIN_BUFFERS 6 125 #define MINHDRSIZE 512 126 #define DEF_BUFSIZE 65536 /* 64 K */ 127 #define DEF_BUCKET_SIZE 4096 128 #define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */ 129 #define DEF_SEGSIZE 256 130 #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ 131 #define DEF_DIRSIZE 256 132 #define DEF_FFACTOR 65536 133 #define MIN_FFACTOR 4 134 #define SPLTMAX 8 135 #define CHARKEY "%$sniglet^&" 136 #define NUMKEY 1038583 137 #define BYTE_SHIFT 3 138 #define INT_TO_BYTE 2 139 #define INT_BYTE_SHIFT 5 140 #define ALL_SET ((u_int32_t)0xFFFFFFFF) 141 #define ALL_CLEAR 0 142 143 #define PTROF(X) ((BUFHEAD *)((intptr_t)(X)&~0x3)) 144 #define ISMOD(X) ((u_int32_t)(intptr_t)(X)&0x1) 145 #define DOMOD(X) ((X) = (char *)((intptr_t)(X)|0x1)) 146 #define ISDISK(X) ((u_int32_t)(intptr_t)(X)&0x2) 147 #define DODISK(X) ((X) = (char *)((intptr_t)(X)|0x2)) 148 149 #define BITS_PER_MAP 32 150 151 /* Given the address of the beginning of a big map, clear/set the nth bit */ 152 #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP))) 153 #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP))) 154 #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP))) 155 156 /* Overflow management */ 157 /* 158 * Overflow page numbers are allocated per split point. At each doubling of 159 * the table, we can allocate extra pages. So, an overflow page number has 160 * the top 5 bits indicate which split point and the lower 11 bits indicate 161 * which page at that split point is indicated (pages within split points are 162 * numberered starting with 1). 163 */ 164 165 #define SPLITSHIFT 11 166 #define SPLITMASK 0x7FF 167 #define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT) 168 #define OPAGENUM(N) ((N) & SPLITMASK) 169 #define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O)) 170 171 #define BUCKET_TO_PAGE(B) \ 172 (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0) 173 #define OADDR_TO_PAGE(B) \ 174 BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)); 175 176 /* 177 * page.h contains a detailed description of the page format. 178 * 179 * Normally, keys and data are accessed from offset tables in the top of 180 * each page which point to the beginning of the key and data. There are 181 * four flag values which may be stored in these offset tables which indicate 182 * the following: 183 * 184 * 185 * OVFLPAGE Rather than a key data pair, this pair contains 186 * the address of an overflow page. The format of 187 * the pair is: 188 * OVERFLOW_PAGE_NUMBER OVFLPAGE 189 * 190 * PARTIAL_KEY This must be the first key/data pair on a page 191 * and implies that page contains only a partial key. 192 * That is, the key is too big to fit on a single page 193 * so it starts on this page and continues on the next. 194 * The format of the page is: 195 * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE 196 * 197 * KEY_OFF -- offset of the beginning of the key 198 * PARTIAL_KEY -- 1 199 * OVFL_PAGENO - page number of the next overflow page 200 * OVFLPAGE -- 0 201 * 202 * FULL_KEY This must be the first key/data pair on the page. It 203 * is used in two cases. 204 * 205 * Case 1: 206 * There is a complete key on the page but no data 207 * (because it wouldn't fit). The next page contains 208 * the data. 209 * 210 * Page format it: 211 * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 212 * 213 * KEY_OFF -- offset of the beginning of the key 214 * FULL_KEY -- 2 215 * OVFL_PAGENO - page number of the next overflow page 216 * OVFLPAGE -- 0 217 * 218 * Case 2: 219 * This page contains no key, but part of a large 220 * data field, which is continued on the next page. 221 * 222 * Page format it: 223 * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE 224 * 225 * KEY_OFF -- offset of the beginning of the data on 226 * this page 227 * FULL_KEY -- 2 228 * OVFL_PAGENO - page number of the next overflow page 229 * OVFLPAGE -- 0 230 * 231 * FULL_KEY_DATA 232 * This must be the first key/data pair on the page. 233 * There are two cases: 234 * 235 * Case 1: 236 * This page contains a key and the beginning of the 237 * data field, but the data field is continued on the 238 * next page. 239 * 240 * Page format is: 241 * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF 242 * 243 * KEY_OFF -- offset of the beginning of the key 244 * FULL_KEY_DATA -- 3 245 * OVFL_PAGENO - page number of the next overflow page 246 * DATA_OFF -- offset of the beginning of the data 247 * 248 * Case 2: 249 * This page contains the last page of a big data pair. 250 * There is no key, only the tail end of the data 251 * on this page. 252 * 253 * Page format is: 254 * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE> 255 * 256 * DATA_OFF -- offset of the beginning of the data on 257 * this page 258 * FULL_KEY_DATA -- 3 259 * OVFL_PAGENO - page number of the next overflow page 260 * OVFLPAGE -- 0 261 * 262 * OVFL_PAGENO and OVFLPAGE are optional (they are 263 * not present if there is no next page). 264 */ 265 266 #define OVFLPAGE 0 267 #define PARTIAL_KEY 1 268 #define FULL_KEY 2 269 #define FULL_KEY_DATA 3 270 #define REAL_KEY 4 271 272 /* Short hands for accessing structure */ 273 #define BSIZE hdr.bsize 274 #define BSHIFT hdr.bshift 275 #define DSIZE hdr.dsize 276 #define SGSIZE hdr.ssize 277 #define SSHIFT hdr.sshift 278 #define LORDER hdr.lorder 279 #define OVFL_POINT hdr.ovfl_point 280 #define LAST_FREED hdr.last_freed 281 #define MAX_BUCKET hdr.max_bucket 282 #define FFACTOR hdr.ffactor 283 #define HIGH_MASK hdr.high_mask 284 #define LOW_MASK hdr.low_mask 285 #define NKEYS hdr.nkeys 286 #define HDRPAGES hdr.hdrpages 287 #define SPARES hdr.spares 288 #define BITMAPS hdr.bitmaps 289 #define VERSION hdr.version 290 #define MAGIC hdr.magic 291 #define NEXT_FREE hdr.next_free 292 #define H_CHARKEY hdr.h_charkey 293