hash.c revision 1.30.6.2 1 1.30.6.2 joerg /* $NetBSD: hash.c,v 1.30.6.2 2008/09/11 12:58:01 joerg Exp $ */
2 1.30.6.2 joerg
3 1.30.6.2 joerg /*-
4 1.30.6.2 joerg * Copyright (c) 1990, 1993, 1994
5 1.30.6.2 joerg * The Regents of the University of California. All rights reserved.
6 1.30.6.2 joerg *
7 1.30.6.2 joerg * This code is derived from software contributed to Berkeley by
8 1.30.6.2 joerg * Margo Seltzer.
9 1.30.6.2 joerg *
10 1.30.6.2 joerg * Redistribution and use in source and binary forms, with or without
11 1.30.6.2 joerg * modification, are permitted provided that the following conditions
12 1.30.6.2 joerg * are met:
13 1.30.6.2 joerg * 1. Redistributions of source code must retain the above copyright
14 1.30.6.2 joerg * notice, this list of conditions and the following disclaimer.
15 1.30.6.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.30.6.2 joerg * notice, this list of conditions and the following disclaimer in the
17 1.30.6.2 joerg * documentation and/or other materials provided with the distribution.
18 1.30.6.2 joerg * 3. Neither the name of the University nor the names of its contributors
19 1.30.6.2 joerg * may be used to endorse or promote products derived from this software
20 1.30.6.2 joerg * without specific prior written permission.
21 1.30.6.2 joerg *
22 1.30.6.2 joerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.30.6.2 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.30.6.2 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.30.6.2 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.30.6.2 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.30.6.2 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.30.6.2 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.30.6.2 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.30.6.2 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.30.6.2 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.30.6.2 joerg * SUCH DAMAGE.
33 1.30.6.2 joerg */
34 1.30.6.2 joerg
35 1.30.6.2 joerg #if HAVE_NBTOOL_CONFIG_H
36 1.30.6.2 joerg #include "nbtool_config.h"
37 1.30.6.2 joerg #endif
38 1.30.6.2 joerg
39 1.30.6.2 joerg #include <sys/cdefs.h>
40 1.30.6.2 joerg __RCSID("$NetBSD: hash.c,v 1.30.6.2 2008/09/11 12:58:01 joerg Exp $");
41 1.30.6.2 joerg
42 1.30.6.2 joerg #include "namespace.h"
43 1.30.6.2 joerg #include <sys/param.h>
44 1.30.6.2 joerg #include <sys/stat.h>
45 1.30.6.2 joerg
46 1.30.6.2 joerg #include <errno.h>
47 1.30.6.2 joerg #include <fcntl.h>
48 1.30.6.2 joerg #include <stdio.h>
49 1.30.6.2 joerg #include <stdlib.h>
50 1.30.6.2 joerg #include <string.h>
51 1.30.6.2 joerg #include <unistd.h>
52 1.30.6.2 joerg #include <assert.h>
53 1.30.6.2 joerg
54 1.30.6.2 joerg #include <db.h>
55 1.30.6.2 joerg #include "hash.h"
56 1.30.6.2 joerg #include "page.h"
57 1.30.6.2 joerg #include "extern.h"
58 1.30.6.2 joerg
59 1.30.6.2 joerg static int alloc_segs(HTAB *, int);
60 1.30.6.2 joerg static int flush_meta(HTAB *);
61 1.30.6.2 joerg static int hash_access(HTAB *, ACTION, DBT *, DBT *);
62 1.30.6.2 joerg static int hash_close(DB *);
63 1.30.6.2 joerg static int hash_delete(const DB *, const DBT *, uint32_t);
64 1.30.6.2 joerg static int hash_fd(const DB *);
65 1.30.6.2 joerg static int hash_get(const DB *, const DBT *, DBT *, uint32_t);
66 1.30.6.2 joerg static int hash_put(const DB *, DBT *, const DBT *, uint32_t);
67 1.30.6.2 joerg static void *hash_realloc(SEGMENT **, size_t, size_t);
68 1.30.6.2 joerg static int hash_seq(const DB *, DBT *, DBT *, uint32_t);
69 1.30.6.2 joerg static int hash_sync(const DB *, uint32_t);
70 1.30.6.2 joerg static int hdestroy(HTAB *);
71 1.30.6.2 joerg static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
72 1.30.6.2 joerg static int init_htab(HTAB *, size_t);
73 1.30.6.2 joerg #if BYTE_ORDER == LITTLE_ENDIAN
74 1.30.6.2 joerg static void swap_header(HTAB *);
75 1.30.6.2 joerg static void swap_header_copy(HASHHDR *, HASHHDR *);
76 1.30.6.2 joerg #endif
77 1.30.6.2 joerg
78 1.30.6.2 joerg /* Fast arithmetic, relying on powers of 2, */
79 1.30.6.2 joerg #define MOD(x, y) ((x) & ((y) - 1))
80 1.30.6.2 joerg
81 1.30.6.2 joerg #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
82 1.30.6.2 joerg
83 1.30.6.2 joerg /* Return values */
84 1.30.6.2 joerg #define SUCCESS (0)
85 1.30.6.2 joerg #define ERROR (-1)
86 1.30.6.2 joerg #define ABNORMAL (1)
87 1.30.6.2 joerg
88 1.30.6.2 joerg #ifdef HASH_STATISTICS
89 1.30.6.2 joerg int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
90 1.30.6.2 joerg #endif
91 1.30.6.2 joerg
92 1.30.6.2 joerg /************************** INTERFACE ROUTINES ***************************/
93 1.30.6.2 joerg /* OPEN/CLOSE */
94 1.30.6.2 joerg
95 1.30.6.2 joerg /* ARGSUSED */
96 1.30.6.2 joerg DB *
97 1.30.6.2 joerg __hash_open(const char *file, int flags, mode_t mode, const HASHINFO *info,
98 1.30.6.2 joerg int dflags)
99 1.30.6.2 joerg {
100 1.30.6.2 joerg HTAB *hashp;
101 1.30.6.2 joerg struct stat statbuf;
102 1.30.6.2 joerg DB *dbp;
103 1.30.6.2 joerg int bpages, new_table, nsegs, save_errno;
104 1.30.6.2 joerg ssize_t hdrsize;
105 1.30.6.2 joerg
106 1.30.6.2 joerg if ((flags & O_ACCMODE) == O_WRONLY) {
107 1.30.6.2 joerg errno = EINVAL;
108 1.30.6.2 joerg return (NULL);
109 1.30.6.2 joerg }
110 1.30.6.2 joerg
111 1.30.6.2 joerg if (!(hashp = calloc(1, sizeof(HTAB))))
112 1.30.6.2 joerg return (NULL);
113 1.30.6.2 joerg hashp->fp = -1;
114 1.30.6.2 joerg
115 1.30.6.2 joerg /*
116 1.30.6.2 joerg * Even if user wants write only, we need to be able to read
117 1.30.6.2 joerg * the actual file, so we need to open it read/write. But, the
118 1.30.6.2 joerg * field in the hashp structure needs to be accurate so that
119 1.30.6.2 joerg * we can check accesses.
120 1.30.6.2 joerg */
121 1.30.6.2 joerg hashp->flags = flags;
122 1.30.6.2 joerg
123 1.30.6.2 joerg new_table = 0;
124 1.30.6.2 joerg if (!file || (flags & O_TRUNC) ||
125 1.30.6.2 joerg (stat(file, &statbuf) && (errno == ENOENT))) {
126 1.30.6.2 joerg if (errno == ENOENT)
127 1.30.6.2 joerg errno = 0; /* Just in case someone looks at errno */
128 1.30.6.2 joerg new_table = 1;
129 1.30.6.2 joerg }
130 1.30.6.2 joerg if (file) {
131 1.30.6.2 joerg if ((hashp->fp = open(file, flags, mode)) == -1)
132 1.30.6.2 joerg RETURN_ERROR(errno, error0);
133 1.30.6.2 joerg if (fcntl(hashp->fp, F_SETFD, FD_CLOEXEC) == -1)
134 1.30.6.2 joerg RETURN_ERROR(errno, error1);
135 1.30.6.2 joerg if (fstat(hashp->fp, &statbuf) == -1)
136 1.30.6.2 joerg RETURN_ERROR(errno, error1);
137 1.30.6.2 joerg new_table |= statbuf.st_size == 0;
138 1.30.6.2 joerg }
139 1.30.6.2 joerg if (new_table) {
140 1.30.6.2 joerg if (!(hashp = init_hash(hashp, file, info)))
141 1.30.6.2 joerg RETURN_ERROR(errno, error1);
142 1.30.6.2 joerg } else {
143 1.30.6.2 joerg /* Table already exists */
144 1.30.6.2 joerg if (info && info->hash)
145 1.30.6.2 joerg hashp->hash = info->hash;
146 1.30.6.2 joerg else
147 1.30.6.2 joerg hashp->hash = __default_hash;
148 1.30.6.2 joerg
149 1.30.6.2 joerg hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
150 1.30.6.2 joerg #if BYTE_ORDER == LITTLE_ENDIAN
151 1.30.6.2 joerg swap_header(hashp);
152 1.30.6.2 joerg #endif
153 1.30.6.2 joerg if (hdrsize == -1)
154 1.30.6.2 joerg RETURN_ERROR(errno, error1);
155 1.30.6.2 joerg if (hdrsize != sizeof(HASHHDR))
156 1.30.6.2 joerg RETURN_ERROR(EFTYPE, error1);
157 1.30.6.2 joerg /* Verify file type, versions and hash function */
158 1.30.6.2 joerg if (hashp->MAGIC != HASHMAGIC)
159 1.30.6.2 joerg RETURN_ERROR(EFTYPE, error1);
160 1.30.6.2 joerg #define OLDHASHVERSION 1
161 1.30.6.2 joerg if (hashp->VERSION != HASHVERSION &&
162 1.30.6.2 joerg hashp->VERSION != OLDHASHVERSION)
163 1.30.6.2 joerg RETURN_ERROR(EFTYPE, error1);
164 1.30.6.2 joerg if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
165 1.30.6.2 joerg RETURN_ERROR(EFTYPE, error1);
166 1.30.6.2 joerg /*
167 1.30.6.2 joerg * Figure out how many segments we need. Max_Bucket is the
168 1.30.6.2 joerg * maximum bucket number, so the number of buckets is
169 1.30.6.2 joerg * max_bucket + 1.
170 1.30.6.2 joerg */
171 1.30.6.2 joerg nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
172 1.30.6.2 joerg hashp->SGSIZE;
173 1.30.6.2 joerg hashp->nsegs = 0;
174 1.30.6.2 joerg if (alloc_segs(hashp, nsegs))
175 1.30.6.2 joerg /*
176 1.30.6.2 joerg * If alloc_segs fails, table will have been destroyed
177 1.30.6.2 joerg * and errno will have been set.
178 1.30.6.2 joerg */
179 1.30.6.2 joerg return (NULL);
180 1.30.6.2 joerg /* Read in bitmaps */
181 1.30.6.2 joerg bpages = (hashp->SPARES[hashp->OVFL_POINT] +
182 1.30.6.2 joerg (unsigned int)(hashp->BSIZE << BYTE_SHIFT) - 1) >>
183 1.30.6.2 joerg (hashp->BSHIFT + BYTE_SHIFT);
184 1.30.6.2 joerg
185 1.30.6.2 joerg hashp->nmaps = bpages;
186 1.30.6.2 joerg (void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32_t *));
187 1.30.6.2 joerg }
188 1.30.6.2 joerg
189 1.30.6.2 joerg /* Initialize Buffer Manager */
190 1.30.6.2 joerg if (info && info->cachesize)
191 1.30.6.2 joerg __buf_init(hashp, info->cachesize);
192 1.30.6.2 joerg else
193 1.30.6.2 joerg __buf_init(hashp, DEF_BUFSIZE);
194 1.30.6.2 joerg
195 1.30.6.2 joerg hashp->new_file = new_table;
196 1.30.6.2 joerg hashp->save_file = file && (hashp->flags & O_RDWR);
197 1.30.6.2 joerg hashp->cbucket = -1;
198 1.30.6.2 joerg if (!(dbp = malloc(sizeof(DB)))) {
199 1.30.6.2 joerg save_errno = errno;
200 1.30.6.2 joerg hdestroy(hashp);
201 1.30.6.2 joerg errno = save_errno;
202 1.30.6.2 joerg return (NULL);
203 1.30.6.2 joerg }
204 1.30.6.2 joerg dbp->internal = hashp;
205 1.30.6.2 joerg dbp->close = hash_close;
206 1.30.6.2 joerg dbp->del = hash_delete;
207 1.30.6.2 joerg dbp->fd = hash_fd;
208 1.30.6.2 joerg dbp->get = hash_get;
209 1.30.6.2 joerg dbp->put = hash_put;
210 1.30.6.2 joerg dbp->seq = hash_seq;
211 1.30.6.2 joerg dbp->sync = hash_sync;
212 1.30.6.2 joerg dbp->type = DB_HASH;
213 1.30.6.2 joerg
214 1.30.6.2 joerg #ifdef DEBUG
215 1.30.6.2 joerg (void)fprintf(stderr,
216 1.30.6.2 joerg "%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
217 1.30.6.2 joerg "init_htab:",
218 1.30.6.2 joerg "TABLE POINTER ", hashp,
219 1.30.6.2 joerg "BUCKET SIZE ", hashp->BSIZE,
220 1.30.6.2 joerg "BUCKET SHIFT ", hashp->BSHIFT,
221 1.30.6.2 joerg "DIRECTORY SIZE ", hashp->DSIZE,
222 1.30.6.2 joerg "SEGMENT SIZE ", hashp->SGSIZE,
223 1.30.6.2 joerg "SEGMENT SHIFT ", hashp->SSHIFT,
224 1.30.6.2 joerg "FILL FACTOR ", hashp->FFACTOR,
225 1.30.6.2 joerg "MAX BUCKET ", hashp->MAX_BUCKET,
226 1.30.6.2 joerg "OVFL POINT ", hashp->OVFL_POINT,
227 1.30.6.2 joerg "LAST FREED ", hashp->LAST_FREED,
228 1.30.6.2 joerg "HIGH MASK ", hashp->HIGH_MASK,
229 1.30.6.2 joerg "LOW MASK ", hashp->LOW_MASK,
230 1.30.6.2 joerg "NSEGS ", hashp->nsegs,
231 1.30.6.2 joerg "NKEYS ", hashp->NKEYS);
232 1.30.6.2 joerg #endif
233 1.30.6.2 joerg #ifdef HASH_STATISTICS
234 1.30.6.2 joerg hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
235 1.30.6.2 joerg #endif
236 1.30.6.2 joerg return (dbp);
237 1.30.6.2 joerg
238 1.30.6.2 joerg error1:
239 1.30.6.2 joerg if (hashp != NULL)
240 1.30.6.2 joerg (void)close(hashp->fp);
241 1.30.6.2 joerg
242 1.30.6.2 joerg error0:
243 1.30.6.2 joerg free(hashp);
244 1.30.6.2 joerg errno = save_errno;
245 1.30.6.2 joerg return (NULL);
246 1.30.6.2 joerg }
247 1.30.6.2 joerg
248 1.30.6.2 joerg static int
249 1.30.6.2 joerg hash_close(DB *dbp)
250 1.30.6.2 joerg {
251 1.30.6.2 joerg HTAB *hashp;
252 1.30.6.2 joerg int retval;
253 1.30.6.2 joerg
254 1.30.6.2 joerg if (!dbp)
255 1.30.6.2 joerg return (ERROR);
256 1.30.6.2 joerg
257 1.30.6.2 joerg hashp = dbp->internal;
258 1.30.6.2 joerg retval = hdestroy(hashp);
259 1.30.6.2 joerg free(dbp);
260 1.30.6.2 joerg return (retval);
261 1.30.6.2 joerg }
262 1.30.6.2 joerg
263 1.30.6.2 joerg static int
264 1.30.6.2 joerg hash_fd(const DB *dbp)
265 1.30.6.2 joerg {
266 1.30.6.2 joerg HTAB *hashp;
267 1.30.6.2 joerg
268 1.30.6.2 joerg if (!dbp)
269 1.30.6.2 joerg return (ERROR);
270 1.30.6.2 joerg
271 1.30.6.2 joerg hashp = dbp->internal;
272 1.30.6.2 joerg if (hashp->fp == -1) {
273 1.30.6.2 joerg errno = ENOENT;
274 1.30.6.2 joerg return (-1);
275 1.30.6.2 joerg }
276 1.30.6.2 joerg return (hashp->fp);
277 1.30.6.2 joerg }
278 1.30.6.2 joerg
279 1.30.6.2 joerg /************************** LOCAL CREATION ROUTINES **********************/
280 1.30.6.2 joerg static HTAB *
281 1.30.6.2 joerg init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
282 1.30.6.2 joerg {
283 1.30.6.2 joerg struct stat statbuf;
284 1.30.6.2 joerg int nelem;
285 1.30.6.2 joerg
286 1.30.6.2 joerg nelem = 1;
287 1.30.6.2 joerg hashp->NKEYS = 0;
288 1.30.6.2 joerg hashp->LORDER = BYTE_ORDER;
289 1.30.6.2 joerg hashp->BSIZE = DEF_BUCKET_SIZE;
290 1.30.6.2 joerg hashp->BSHIFT = DEF_BUCKET_SHIFT;
291 1.30.6.2 joerg hashp->SGSIZE = DEF_SEGSIZE;
292 1.30.6.2 joerg hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
293 1.30.6.2 joerg hashp->DSIZE = DEF_DIRSIZE;
294 1.30.6.2 joerg hashp->FFACTOR = DEF_FFACTOR;
295 1.30.6.2 joerg hashp->hash = __default_hash;
296 1.30.6.2 joerg memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
297 1.30.6.2 joerg memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
298 1.30.6.2 joerg
299 1.30.6.2 joerg /* Fix bucket size to be optimal for file system */
300 1.30.6.2 joerg if (file != NULL) {
301 1.30.6.2 joerg if (stat(file, &statbuf))
302 1.30.6.2 joerg return (NULL);
303 1.30.6.2 joerg hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE);
304 1.30.6.2 joerg hashp->BSHIFT = __log2((uint32_t)hashp->BSIZE);
305 1.30.6.2 joerg }
306 1.30.6.2 joerg
307 1.30.6.2 joerg if (info) {
308 1.30.6.2 joerg if (info->bsize) {
309 1.30.6.2 joerg /* Round pagesize up to power of 2 */
310 1.30.6.2 joerg hashp->BSHIFT = __log2(info->bsize);
311 1.30.6.2 joerg hashp->BSIZE = 1 << hashp->BSHIFT;
312 1.30.6.2 joerg if (hashp->BSIZE > MAX_BSIZE) {
313 1.30.6.2 joerg errno = EINVAL;
314 1.30.6.2 joerg return (NULL);
315 1.30.6.2 joerg }
316 1.30.6.2 joerg }
317 1.30.6.2 joerg if (info->ffactor)
318 1.30.6.2 joerg hashp->FFACTOR = info->ffactor;
319 1.30.6.2 joerg if (info->hash)
320 1.30.6.2 joerg hashp->hash = info->hash;
321 1.30.6.2 joerg if (info->nelem)
322 1.30.6.2 joerg nelem = info->nelem;
323 1.30.6.2 joerg if (info->lorder) {
324 1.30.6.2 joerg if (info->lorder != BIG_ENDIAN &&
325 1.30.6.2 joerg info->lorder != LITTLE_ENDIAN) {
326 1.30.6.2 joerg errno = EINVAL;
327 1.30.6.2 joerg return (NULL);
328 1.30.6.2 joerg }
329 1.30.6.2 joerg hashp->LORDER = info->lorder;
330 1.30.6.2 joerg }
331 1.30.6.2 joerg }
332 1.30.6.2 joerg /* init_htab should destroy the table and set errno if it fails */
333 1.30.6.2 joerg if (init_htab(hashp, (size_t)nelem))
334 1.30.6.2 joerg return (NULL);
335 1.30.6.2 joerg else
336 1.30.6.2 joerg return (hashp);
337 1.30.6.2 joerg }
338 1.30.6.2 joerg /*
339 1.30.6.2 joerg * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
340 1.30.6.2 joerg * the table and set errno, so we just pass the error information along.
341 1.30.6.2 joerg *
342 1.30.6.2 joerg * Returns 0 on No Error
343 1.30.6.2 joerg */
344 1.30.6.2 joerg static int
345 1.30.6.2 joerg init_htab(HTAB *hashp, size_t nelem)
346 1.30.6.2 joerg {
347 1.30.6.2 joerg int nbuckets;
348 1.30.6.2 joerg uint32_t nsegs;
349 1.30.6.2 joerg int l2;
350 1.30.6.2 joerg
351 1.30.6.2 joerg /*
352 1.30.6.2 joerg * Divide number of elements by the fill factor and determine a
353 1.30.6.2 joerg * desired number of buckets. Allocate space for the next greater
354 1.30.6.2 joerg * power of two number of buckets.
355 1.30.6.2 joerg */
356 1.30.6.2 joerg nelem = (nelem - 1) / hashp->FFACTOR + 1;
357 1.30.6.2 joerg
358 1.30.6.2 joerg _DBFIT(nelem, uint32_t);
359 1.30.6.2 joerg l2 = __log2(MAX((uint32_t)nelem, 2));
360 1.30.6.2 joerg nbuckets = 1 << l2;
361 1.30.6.2 joerg
362 1.30.6.2 joerg hashp->SPARES[l2] = l2 + 1;
363 1.30.6.2 joerg hashp->SPARES[l2 + 1] = l2 + 1;
364 1.30.6.2 joerg hashp->OVFL_POINT = l2;
365 1.30.6.2 joerg hashp->LAST_FREED = 2;
366 1.30.6.2 joerg
367 1.30.6.2 joerg /* First bitmap page is at: splitpoint l2 page offset 1 */
368 1.30.6.2 joerg if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0))
369 1.30.6.2 joerg return (-1);
370 1.30.6.2 joerg
371 1.30.6.2 joerg hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
372 1.30.6.2 joerg hashp->HIGH_MASK = (nbuckets << 1) - 1;
373 1.30.6.2 joerg /* LINTED constant in conditional context */
374 1.30.6.2 joerg hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
375 1.30.6.2 joerg hashp->BSHIFT) + 1;
376 1.30.6.2 joerg
377 1.30.6.2 joerg nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
378 1.30.6.2 joerg nsegs = 1 << __log2(nsegs);
379 1.30.6.2 joerg
380 1.30.6.2 joerg if (nsegs > hashp->DSIZE)
381 1.30.6.2 joerg hashp->DSIZE = nsegs;
382 1.30.6.2 joerg return (alloc_segs(hashp, (int)nsegs));
383 1.30.6.2 joerg }
384 1.30.6.2 joerg
385 1.30.6.2 joerg /********************** DESTROY/CLOSE ROUTINES ************************/
386 1.30.6.2 joerg
387 1.30.6.2 joerg /*
388 1.30.6.2 joerg * Flushes any changes to the file if necessary and destroys the hashp
389 1.30.6.2 joerg * structure, freeing all allocated space.
390 1.30.6.2 joerg */
391 1.30.6.2 joerg static int
392 1.30.6.2 joerg hdestroy(HTAB *hashp)
393 1.30.6.2 joerg {
394 1.30.6.2 joerg int i, save_errno;
395 1.30.6.2 joerg
396 1.30.6.2 joerg save_errno = 0;
397 1.30.6.2 joerg
398 1.30.6.2 joerg #ifdef HASH_STATISTICS
399 1.30.6.2 joerg (void)fprintf(stderr, "hdestroy: accesses %d collisions %d\n",
400 1.30.6.2 joerg hash_accesses, hash_collisions);
401 1.30.6.2 joerg (void)fprintf(stderr, "hdestroy: expansions %d\n",
402 1.30.6.2 joerg hash_expansions);
403 1.30.6.2 joerg (void)fprintf(stderr, "hdestroy: overflows %d\n",
404 1.30.6.2 joerg hash_overflows);
405 1.30.6.2 joerg (void)fprintf(stderr, "keys %d maxp %d segmentcount %d\n",
406 1.30.6.2 joerg hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
407 1.30.6.2 joerg
408 1.30.6.2 joerg for (i = 0; i < NCACHED; i++)
409 1.30.6.2 joerg (void)fprintf(stderr,
410 1.30.6.2 joerg "spares[%d] = %d\n", i, hashp->SPARES[i]);
411 1.30.6.2 joerg #endif
412 1.30.6.2 joerg /*
413 1.30.6.2 joerg * Call on buffer manager to free buffers, and if required,
414 1.30.6.2 joerg * write them to disk.
415 1.30.6.2 joerg */
416 1.30.6.2 joerg if (__buf_free(hashp, 1, hashp->save_file))
417 1.30.6.2 joerg save_errno = errno;
418 1.30.6.2 joerg if (hashp->dir) {
419 1.30.6.2 joerg free(*hashp->dir); /* Free initial segments */
420 1.30.6.2 joerg /* Free extra segments */
421 1.30.6.2 joerg while (hashp->exsegs--)
422 1.30.6.2 joerg free(hashp->dir[--hashp->nsegs]);
423 1.30.6.2 joerg free(hashp->dir);
424 1.30.6.2 joerg }
425 1.30.6.2 joerg if (flush_meta(hashp) && !save_errno)
426 1.30.6.2 joerg save_errno = errno;
427 1.30.6.2 joerg /* Free Bigmaps */
428 1.30.6.2 joerg for (i = 0; i < hashp->nmaps; i++)
429 1.30.6.2 joerg if (hashp->mapp[i])
430 1.30.6.2 joerg free(hashp->mapp[i]);
431 1.30.6.2 joerg
432 1.30.6.2 joerg if (hashp->fp != -1)
433 1.30.6.2 joerg (void)close(hashp->fp);
434 1.30.6.2 joerg
435 1.30.6.2 joerg free(hashp);
436 1.30.6.2 joerg
437 1.30.6.2 joerg if (save_errno) {
438 1.30.6.2 joerg errno = save_errno;
439 1.30.6.2 joerg return (ERROR);
440 1.30.6.2 joerg }
441 1.30.6.2 joerg return (SUCCESS);
442 1.30.6.2 joerg }
443 1.30.6.2 joerg /*
444 1.30.6.2 joerg * Write modified pages to disk
445 1.30.6.2 joerg *
446 1.30.6.2 joerg * Returns:
447 1.30.6.2 joerg * 0 == OK
448 1.30.6.2 joerg * -1 ERROR
449 1.30.6.2 joerg */
450 1.30.6.2 joerg static int
451 1.30.6.2 joerg hash_sync(const DB *dbp, uint32_t flags)
452 1.30.6.2 joerg {
453 1.30.6.2 joerg HTAB *hashp;
454 1.30.6.2 joerg
455 1.30.6.2 joerg if (flags != 0) {
456 1.30.6.2 joerg errno = EINVAL;
457 1.30.6.2 joerg return (ERROR);
458 1.30.6.2 joerg }
459 1.30.6.2 joerg
460 1.30.6.2 joerg if (!dbp)
461 1.30.6.2 joerg return (ERROR);
462 1.30.6.2 joerg
463 1.30.6.2 joerg hashp = dbp->internal;
464 1.30.6.2 joerg if (!hashp->save_file)
465 1.30.6.2 joerg return (0);
466 1.30.6.2 joerg if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
467 1.30.6.2 joerg return (ERROR);
468 1.30.6.2 joerg hashp->new_file = 0;
469 1.30.6.2 joerg return (0);
470 1.30.6.2 joerg }
471 1.30.6.2 joerg
472 1.30.6.2 joerg /*
473 1.30.6.2 joerg * Returns:
474 1.30.6.2 joerg * 0 == OK
475 1.30.6.2 joerg * -1 indicates that errno should be set
476 1.30.6.2 joerg */
477 1.30.6.2 joerg static int
478 1.30.6.2 joerg flush_meta(HTAB *hashp)
479 1.30.6.2 joerg {
480 1.30.6.2 joerg HASHHDR *whdrp;
481 1.30.6.2 joerg #if BYTE_ORDER == LITTLE_ENDIAN
482 1.30.6.2 joerg HASHHDR whdr;
483 1.30.6.2 joerg #endif
484 1.30.6.2 joerg int fp, i;
485 1.30.6.2 joerg ssize_t wsize;
486 1.30.6.2 joerg
487 1.30.6.2 joerg if (!hashp->save_file)
488 1.30.6.2 joerg return (0);
489 1.30.6.2 joerg hashp->MAGIC = HASHMAGIC;
490 1.30.6.2 joerg hashp->VERSION = HASHVERSION;
491 1.30.6.2 joerg hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
492 1.30.6.2 joerg
493 1.30.6.2 joerg fp = hashp->fp;
494 1.30.6.2 joerg whdrp = &hashp->hdr;
495 1.30.6.2 joerg #if BYTE_ORDER == LITTLE_ENDIAN
496 1.30.6.2 joerg whdrp = &whdr;
497 1.30.6.2 joerg swap_header_copy(&hashp->hdr, whdrp);
498 1.30.6.2 joerg #endif
499 1.30.6.2 joerg if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
500 1.30.6.2 joerg return (-1);
501 1.30.6.2 joerg else
502 1.30.6.2 joerg if (wsize != sizeof(HASHHDR)) {
503 1.30.6.2 joerg errno = EFTYPE;
504 1.30.6.2 joerg hashp->err = errno;
505 1.30.6.2 joerg return (-1);
506 1.30.6.2 joerg }
507 1.30.6.2 joerg for (i = 0; i < NCACHED; i++)
508 1.30.6.2 joerg if (hashp->mapp[i])
509 1.30.6.2 joerg if (__put_page(hashp, (char *)(void *)hashp->mapp[i],
510 1.30.6.2 joerg (u_int)hashp->BITMAPS[i], 0, 1))
511 1.30.6.2 joerg return (-1);
512 1.30.6.2 joerg return (0);
513 1.30.6.2 joerg }
514 1.30.6.2 joerg
515 1.30.6.2 joerg /*******************************SEARCH ROUTINES *****************************/
516 1.30.6.2 joerg /*
517 1.30.6.2 joerg * All the access routines return
518 1.30.6.2 joerg *
519 1.30.6.2 joerg * Returns:
520 1.30.6.2 joerg * 0 on SUCCESS
521 1.30.6.2 joerg * 1 to indicate an external ERROR (i.e. key not found, etc)
522 1.30.6.2 joerg * -1 to indicate an internal ERROR (i.e. out of memory, etc)
523 1.30.6.2 joerg */
524 1.30.6.2 joerg static int
525 1.30.6.2 joerg hash_get(const DB *dbp, const DBT *key, DBT *data, uint32_t flag)
526 1.30.6.2 joerg {
527 1.30.6.2 joerg HTAB *hashp;
528 1.30.6.2 joerg
529 1.30.6.2 joerg hashp = dbp->internal;
530 1.30.6.2 joerg if (flag) {
531 1.30.6.2 joerg hashp->err = errno = EINVAL;
532 1.30.6.2 joerg return (ERROR);
533 1.30.6.2 joerg }
534 1.30.6.2 joerg return (hash_access(hashp, HASH_GET, __UNCONST(key), data));
535 1.30.6.2 joerg }
536 1.30.6.2 joerg
537 1.30.6.2 joerg static int
538 1.30.6.2 joerg hash_put(const DB *dbp, DBT *key, const DBT *data, uint32_t flag)
539 1.30.6.2 joerg {
540 1.30.6.2 joerg HTAB *hashp;
541 1.30.6.2 joerg
542 1.30.6.2 joerg hashp = dbp->internal;
543 1.30.6.2 joerg if (flag && flag != R_NOOVERWRITE) {
544 1.30.6.2 joerg hashp->err = errno = EINVAL;
545 1.30.6.2 joerg return (ERROR);
546 1.30.6.2 joerg }
547 1.30.6.2 joerg if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
548 1.30.6.2 joerg hashp->err = errno = EPERM;
549 1.30.6.2 joerg return (ERROR);
550 1.30.6.2 joerg }
551 1.30.6.2 joerg /* LINTED const castaway */
552 1.30.6.2 joerg return (hash_access(hashp, flag == R_NOOVERWRITE ?
553 1.30.6.2 joerg HASH_PUTNEW : HASH_PUT, __UNCONST(key), __UNCONST(data)));
554 1.30.6.2 joerg }
555 1.30.6.2 joerg
556 1.30.6.2 joerg static int
557 1.30.6.2 joerg hash_delete(const DB *dbp, const DBT *key, uint32_t flag)
558 1.30.6.2 joerg {
559 1.30.6.2 joerg HTAB *hashp;
560 1.30.6.2 joerg
561 1.30.6.2 joerg hashp = dbp->internal;
562 1.30.6.2 joerg if (flag && flag != R_CURSOR) {
563 1.30.6.2 joerg hashp->err = errno = EINVAL;
564 1.30.6.2 joerg return (ERROR);
565 1.30.6.2 joerg }
566 1.30.6.2 joerg if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
567 1.30.6.2 joerg hashp->err = errno = EPERM;
568 1.30.6.2 joerg return (ERROR);
569 1.30.6.2 joerg }
570 1.30.6.2 joerg return hash_access(hashp, HASH_DELETE, __UNCONST(key), NULL);
571 1.30.6.2 joerg }
572 1.30.6.2 joerg
573 1.30.6.2 joerg /*
574 1.30.6.2 joerg * Assume that hashp has been set in wrapper routine.
575 1.30.6.2 joerg */
576 1.30.6.2 joerg static int
577 1.30.6.2 joerg hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
578 1.30.6.2 joerg {
579 1.30.6.2 joerg BUFHEAD *rbufp;
580 1.30.6.2 joerg BUFHEAD *bufp, *save_bufp;
581 1.30.6.2 joerg uint16_t *bp;
582 1.30.6.2 joerg int n, ndx, off;
583 1.30.6.2 joerg size_t size;
584 1.30.6.2 joerg char *kp;
585 1.30.6.2 joerg uint16_t pageno;
586 1.30.6.2 joerg
587 1.30.6.2 joerg #ifdef HASH_STATISTICS
588 1.30.6.2 joerg hash_accesses++;
589 1.30.6.2 joerg #endif
590 1.30.6.2 joerg
591 1.30.6.2 joerg off = hashp->BSIZE;
592 1.30.6.2 joerg size = key->size;
593 1.30.6.2 joerg kp = (char *)key->data;
594 1.30.6.2 joerg rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0);
595 1.30.6.2 joerg if (!rbufp)
596 1.30.6.2 joerg return (ERROR);
597 1.30.6.2 joerg save_bufp = rbufp;
598 1.30.6.2 joerg
599 1.30.6.2 joerg /* Pin the bucket chain */
600 1.30.6.2 joerg rbufp->flags |= BUF_PIN;
601 1.30.6.2 joerg for (bp = (uint16_t *)(void *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
602 1.30.6.2 joerg if (bp[1] >= REAL_KEY) {
603 1.30.6.2 joerg /* Real key/data pair */
604 1.30.6.2 joerg if (size == off - *bp &&
605 1.30.6.2 joerg memcmp(kp, rbufp->page + *bp, size) == 0)
606 1.30.6.2 joerg goto found;
607 1.30.6.2 joerg off = bp[1];
608 1.30.6.2 joerg #ifdef HASH_STATISTICS
609 1.30.6.2 joerg hash_collisions++;
610 1.30.6.2 joerg #endif
611 1.30.6.2 joerg bp += 2;
612 1.30.6.2 joerg ndx += 2;
613 1.30.6.2 joerg } else if (bp[1] == OVFLPAGE) {
614 1.30.6.2 joerg rbufp = __get_buf(hashp, (uint32_t)*bp, rbufp, 0);
615 1.30.6.2 joerg if (!rbufp) {
616 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
617 1.30.6.2 joerg return (ERROR);
618 1.30.6.2 joerg }
619 1.30.6.2 joerg /* FOR LOOP INIT */
620 1.30.6.2 joerg bp = (uint16_t *)(void *)rbufp->page;
621 1.30.6.2 joerg n = *bp++;
622 1.30.6.2 joerg ndx = 1;
623 1.30.6.2 joerg off = hashp->BSIZE;
624 1.30.6.2 joerg } else if (bp[1] < REAL_KEY) {
625 1.30.6.2 joerg if ((ndx =
626 1.30.6.2 joerg __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
627 1.30.6.2 joerg goto found;
628 1.30.6.2 joerg if (ndx == -2) {
629 1.30.6.2 joerg bufp = rbufp;
630 1.30.6.2 joerg if (!(pageno =
631 1.30.6.2 joerg __find_last_page(hashp, &bufp))) {
632 1.30.6.2 joerg ndx = 0;
633 1.30.6.2 joerg rbufp = bufp;
634 1.30.6.2 joerg break; /* FOR */
635 1.30.6.2 joerg }
636 1.30.6.2 joerg rbufp = __get_buf(hashp, (uint32_t)pageno,
637 1.30.6.2 joerg bufp, 0);
638 1.30.6.2 joerg if (!rbufp) {
639 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
640 1.30.6.2 joerg return (ERROR);
641 1.30.6.2 joerg }
642 1.30.6.2 joerg /* FOR LOOP INIT */
643 1.30.6.2 joerg bp = (uint16_t *)(void *)rbufp->page;
644 1.30.6.2 joerg n = *bp++;
645 1.30.6.2 joerg ndx = 1;
646 1.30.6.2 joerg off = hashp->BSIZE;
647 1.30.6.2 joerg } else {
648 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
649 1.30.6.2 joerg return (ERROR);
650 1.30.6.2 joerg }
651 1.30.6.2 joerg }
652 1.30.6.2 joerg
653 1.30.6.2 joerg /* Not found */
654 1.30.6.2 joerg switch (action) {
655 1.30.6.2 joerg case HASH_PUT:
656 1.30.6.2 joerg case HASH_PUTNEW:
657 1.30.6.2 joerg if (__addel(hashp, rbufp, key, val)) {
658 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
659 1.30.6.2 joerg return (ERROR);
660 1.30.6.2 joerg } else {
661 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
662 1.30.6.2 joerg return (SUCCESS);
663 1.30.6.2 joerg }
664 1.30.6.2 joerg case HASH_GET:
665 1.30.6.2 joerg case HASH_DELETE:
666 1.30.6.2 joerg default:
667 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
668 1.30.6.2 joerg return (ABNORMAL);
669 1.30.6.2 joerg }
670 1.30.6.2 joerg
671 1.30.6.2 joerg found:
672 1.30.6.2 joerg switch (action) {
673 1.30.6.2 joerg case HASH_PUTNEW:
674 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
675 1.30.6.2 joerg return (ABNORMAL);
676 1.30.6.2 joerg case HASH_GET:
677 1.30.6.2 joerg bp = (uint16_t *)(void *)rbufp->page;
678 1.30.6.2 joerg if (bp[ndx + 1] < REAL_KEY) {
679 1.30.6.2 joerg if (__big_return(hashp, rbufp, ndx, val, 0))
680 1.30.6.2 joerg return (ERROR);
681 1.30.6.2 joerg } else {
682 1.30.6.2 joerg val->data = (uint8_t *)rbufp->page + (int)bp[ndx + 1];
683 1.30.6.2 joerg val->size = bp[ndx] - bp[ndx + 1];
684 1.30.6.2 joerg }
685 1.30.6.2 joerg break;
686 1.30.6.2 joerg case HASH_PUT:
687 1.30.6.2 joerg if ((__delpair(hashp, rbufp, ndx)) ||
688 1.30.6.2 joerg (__addel(hashp, rbufp, key, val))) {
689 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
690 1.30.6.2 joerg return (ERROR);
691 1.30.6.2 joerg }
692 1.30.6.2 joerg break;
693 1.30.6.2 joerg case HASH_DELETE:
694 1.30.6.2 joerg if (__delpair(hashp, rbufp, ndx))
695 1.30.6.2 joerg return (ERROR);
696 1.30.6.2 joerg break;
697 1.30.6.2 joerg default:
698 1.30.6.2 joerg abort();
699 1.30.6.2 joerg }
700 1.30.6.2 joerg save_bufp->flags &= ~BUF_PIN;
701 1.30.6.2 joerg return (SUCCESS);
702 1.30.6.2 joerg }
703 1.30.6.2 joerg
704 1.30.6.2 joerg static int
705 1.30.6.2 joerg hash_seq(const DB *dbp, DBT *key, DBT *data, uint32_t flag)
706 1.30.6.2 joerg {
707 1.30.6.2 joerg uint32_t bucket;
708 1.30.6.2 joerg BUFHEAD *bufp = NULL; /* XXX: gcc */
709 1.30.6.2 joerg HTAB *hashp;
710 1.30.6.2 joerg uint16_t *bp, ndx;
711 1.30.6.2 joerg
712 1.30.6.2 joerg hashp = dbp->internal;
713 1.30.6.2 joerg if (flag && flag != R_FIRST && flag != R_NEXT) {
714 1.30.6.2 joerg hashp->err = errno = EINVAL;
715 1.30.6.2 joerg return (ERROR);
716 1.30.6.2 joerg }
717 1.30.6.2 joerg #ifdef HASH_STATISTICS
718 1.30.6.2 joerg hash_accesses++;
719 1.30.6.2 joerg #endif
720 1.30.6.2 joerg if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
721 1.30.6.2 joerg hashp->cbucket = 0;
722 1.30.6.2 joerg hashp->cndx = 1;
723 1.30.6.2 joerg hashp->cpage = NULL;
724 1.30.6.2 joerg }
725 1.30.6.2 joerg
726 1.30.6.2 joerg for (bp = NULL; !bp || !bp[0]; ) {
727 1.30.6.2 joerg if (!(bufp = hashp->cpage)) {
728 1.30.6.2 joerg for (bucket = hashp->cbucket;
729 1.30.6.2 joerg bucket <= hashp->MAX_BUCKET;
730 1.30.6.2 joerg bucket++, hashp->cndx = 1) {
731 1.30.6.2 joerg bufp = __get_buf(hashp, bucket, NULL, 0);
732 1.30.6.2 joerg if (!bufp)
733 1.30.6.2 joerg return (ERROR);
734 1.30.6.2 joerg hashp->cpage = bufp;
735 1.30.6.2 joerg bp = (uint16_t *)(void *)bufp->page;
736 1.30.6.2 joerg if (bp[0])
737 1.30.6.2 joerg break;
738 1.30.6.2 joerg }
739 1.30.6.2 joerg hashp->cbucket = bucket;
740 1.30.6.2 joerg if (hashp->cbucket > hashp->MAX_BUCKET) {
741 1.30.6.2 joerg hashp->cbucket = -1;
742 1.30.6.2 joerg return (ABNORMAL);
743 1.30.6.2 joerg }
744 1.30.6.2 joerg } else
745 1.30.6.2 joerg bp = (uint16_t *)(void *)hashp->cpage->page;
746 1.30.6.2 joerg
747 1.30.6.2 joerg _DIAGASSERT(bp != NULL);
748 1.30.6.2 joerg _DIAGASSERT(bufp != NULL);
749 1.30.6.2 joerg while (bp[hashp->cndx + 1] == OVFLPAGE) {
750 1.30.6.2 joerg bufp = hashp->cpage =
751 1.30.6.2 joerg __get_buf(hashp, (uint32_t)bp[hashp->cndx], bufp,
752 1.30.6.2 joerg 0);
753 1.30.6.2 joerg if (!bufp)
754 1.30.6.2 joerg return (ERROR);
755 1.30.6.2 joerg bp = (uint16_t *)(void *)(bufp->page);
756 1.30.6.2 joerg hashp->cndx = 1;
757 1.30.6.2 joerg }
758 1.30.6.2 joerg if (!bp[0]) {
759 1.30.6.2 joerg hashp->cpage = NULL;
760 1.30.6.2 joerg ++hashp->cbucket;
761 1.30.6.2 joerg }
762 1.30.6.2 joerg }
763 1.30.6.2 joerg ndx = hashp->cndx;
764 1.30.6.2 joerg if (bp[ndx + 1] < REAL_KEY) {
765 1.30.6.2 joerg if (__big_keydata(hashp, bufp, key, data, 1))
766 1.30.6.2 joerg return (ERROR);
767 1.30.6.2 joerg } else {
768 1.30.6.2 joerg if (hashp->cpage == NULL)
769 1.30.6.2 joerg return (ERROR);
770 1.30.6.2 joerg key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
771 1.30.6.2 joerg key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
772 1.30.6.2 joerg data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
773 1.30.6.2 joerg data->size = bp[ndx] - bp[ndx + 1];
774 1.30.6.2 joerg ndx += 2;
775 1.30.6.2 joerg if (ndx > bp[0]) {
776 1.30.6.2 joerg hashp->cpage = NULL;
777 1.30.6.2 joerg hashp->cbucket++;
778 1.30.6.2 joerg hashp->cndx = 1;
779 1.30.6.2 joerg } else
780 1.30.6.2 joerg hashp->cndx = ndx;
781 1.30.6.2 joerg }
782 1.30.6.2 joerg return (SUCCESS);
783 1.30.6.2 joerg }
784 1.30.6.2 joerg
785 1.30.6.2 joerg /********************************* UTILITIES ************************/
786 1.30.6.2 joerg
787 1.30.6.2 joerg /*
788 1.30.6.2 joerg * Returns:
789 1.30.6.2 joerg * 0 ==> OK
790 1.30.6.2 joerg * -1 ==> Error
791 1.30.6.2 joerg */
792 1.30.6.2 joerg int
793 1.30.6.2 joerg __expand_table(HTAB *hashp)
794 1.30.6.2 joerg {
795 1.30.6.2 joerg uint32_t old_bucket, new_bucket;
796 1.30.6.2 joerg int new_segnum, spare_ndx;
797 1.30.6.2 joerg size_t dirsize;
798 1.30.6.2 joerg
799 1.30.6.2 joerg #ifdef HASH_STATISTICS
800 1.30.6.2 joerg hash_expansions++;
801 1.30.6.2 joerg #endif
802 1.30.6.2 joerg new_bucket = ++hashp->MAX_BUCKET;
803 1.30.6.2 joerg old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
804 1.30.6.2 joerg
805 1.30.6.2 joerg new_segnum = new_bucket >> hashp->SSHIFT;
806 1.30.6.2 joerg
807 1.30.6.2 joerg /* Check if we need a new segment */
808 1.30.6.2 joerg if (new_segnum >= hashp->nsegs) {
809 1.30.6.2 joerg /* Check if we need to expand directory */
810 1.30.6.2 joerg if (new_segnum >= hashp->DSIZE) {
811 1.30.6.2 joerg /* Reallocate directory */
812 1.30.6.2 joerg dirsize = hashp->DSIZE * sizeof(SEGMENT *);
813 1.30.6.2 joerg if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
814 1.30.6.2 joerg return (-1);
815 1.30.6.2 joerg hashp->DSIZE = dirsize << 1;
816 1.30.6.2 joerg }
817 1.30.6.2 joerg if ((hashp->dir[new_segnum] =
818 1.30.6.2 joerg calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
819 1.30.6.2 joerg return (-1);
820 1.30.6.2 joerg hashp->exsegs++;
821 1.30.6.2 joerg hashp->nsegs++;
822 1.30.6.2 joerg }
823 1.30.6.2 joerg /*
824 1.30.6.2 joerg * If the split point is increasing (MAX_BUCKET's log base 2
825 1.30.6.2 joerg * * increases), we need to copy the current contents of the spare
826 1.30.6.2 joerg * split bucket to the next bucket.
827 1.30.6.2 joerg */
828 1.30.6.2 joerg spare_ndx = __log2((uint32_t)(hashp->MAX_BUCKET + 1));
829 1.30.6.2 joerg if (spare_ndx > hashp->OVFL_POINT) {
830 1.30.6.2 joerg hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
831 1.30.6.2 joerg hashp->OVFL_POINT = spare_ndx;
832 1.30.6.2 joerg }
833 1.30.6.2 joerg
834 1.30.6.2 joerg if (new_bucket > hashp->HIGH_MASK) {
835 1.30.6.2 joerg /* Starting a new doubling */
836 1.30.6.2 joerg hashp->LOW_MASK = hashp->HIGH_MASK;
837 1.30.6.2 joerg hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
838 1.30.6.2 joerg }
839 1.30.6.2 joerg /* Relocate records to the new bucket */
840 1.30.6.2 joerg return (__split_page(hashp, old_bucket, new_bucket));
841 1.30.6.2 joerg }
842 1.30.6.2 joerg
843 1.30.6.2 joerg /*
844 1.30.6.2 joerg * If realloc guarantees that the pointer is not destroyed if the realloc
845 1.30.6.2 joerg * fails, then this routine can go away.
846 1.30.6.2 joerg */
847 1.30.6.2 joerg static void *
848 1.30.6.2 joerg hash_realloc(SEGMENT **p_ptr, size_t oldsize, size_t newsize)
849 1.30.6.2 joerg {
850 1.30.6.2 joerg void *p;
851 1.30.6.2 joerg
852 1.30.6.2 joerg if ((p = malloc(newsize)) != NULL) {
853 1.30.6.2 joerg memmove(p, *p_ptr, oldsize);
854 1.30.6.2 joerg memset((char *)p + oldsize, 0, newsize - oldsize);
855 1.30.6.2 joerg free(*p_ptr);
856 1.30.6.2 joerg *p_ptr = p;
857 1.30.6.2 joerg }
858 1.30.6.2 joerg return (p);
859 1.30.6.2 joerg }
860 1.30.6.2 joerg
861 1.30.6.2 joerg uint32_t
862 1.30.6.2 joerg __call_hash(HTAB *hashp, char *k, int len)
863 1.30.6.2 joerg {
864 1.30.6.2 joerg int n, bucket;
865 1.30.6.2 joerg
866 1.30.6.2 joerg n = hashp->hash(k, (size_t)len);
867 1.30.6.2 joerg bucket = n & hashp->HIGH_MASK;
868 1.30.6.2 joerg if (bucket > hashp->MAX_BUCKET)
869 1.30.6.2 joerg bucket = bucket & hashp->LOW_MASK;
870 1.30.6.2 joerg return (bucket);
871 1.30.6.2 joerg }
872 1.30.6.2 joerg
873 1.30.6.2 joerg /*
874 1.30.6.2 joerg * Allocate segment table. On error, destroy the table and set errno.
875 1.30.6.2 joerg *
876 1.30.6.2 joerg * Returns 0 on success
877 1.30.6.2 joerg */
878 1.30.6.2 joerg static int
879 1.30.6.2 joerg alloc_segs(HTAB *hashp, int nsegs)
880 1.30.6.2 joerg {
881 1.30.6.2 joerg int i;
882 1.30.6.2 joerg SEGMENT store;
883 1.30.6.2 joerg
884 1.30.6.2 joerg int save_errno;
885 1.30.6.2 joerg
886 1.30.6.2 joerg hashp->dir = calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *));
887 1.30.6.2 joerg if (hashp->dir == NULL) {
888 1.30.6.2 joerg save_errno = errno;
889 1.30.6.2 joerg (void)hdestroy(hashp);
890 1.30.6.2 joerg errno = save_errno;
891 1.30.6.2 joerg return (-1);
892 1.30.6.2 joerg }
893 1.30.6.2 joerg hashp->nsegs = nsegs;
894 1.30.6.2 joerg if (nsegs == 0)
895 1.30.6.2 joerg return 0;
896 1.30.6.2 joerg /* Allocate segments */
897 1.30.6.2 joerg store = calloc((size_t)(nsegs << hashp->SSHIFT), sizeof(SEGMENT));
898 1.30.6.2 joerg if (store == NULL) {
899 1.30.6.2 joerg save_errno = errno;
900 1.30.6.2 joerg (void)hdestroy(hashp);
901 1.30.6.2 joerg errno = save_errno;
902 1.30.6.2 joerg return (-1);
903 1.30.6.2 joerg }
904 1.30.6.2 joerg for (i = 0; i < nsegs; i++)
905 1.30.6.2 joerg hashp->dir[i] = &store[i << hashp->SSHIFT];
906 1.30.6.2 joerg return (0);
907 1.30.6.2 joerg }
908 1.30.6.2 joerg
909 1.30.6.2 joerg #if BYTE_ORDER == LITTLE_ENDIAN
910 1.30.6.2 joerg /*
911 1.30.6.2 joerg * Hashp->hdr needs to be byteswapped.
912 1.30.6.2 joerg */
913 1.30.6.2 joerg static void
914 1.30.6.2 joerg swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
915 1.30.6.2 joerg {
916 1.30.6.2 joerg size_t i;
917 1.30.6.2 joerg
918 1.30.6.2 joerg P_32_COPY(srcp->magic, destp->magic);
919 1.30.6.2 joerg P_32_COPY(srcp->version, destp->version);
920 1.30.6.2 joerg P_32_COPY(srcp->lorder, destp->lorder);
921 1.30.6.2 joerg P_32_COPY(srcp->bsize, destp->bsize);
922 1.30.6.2 joerg P_32_COPY(srcp->bshift, destp->bshift);
923 1.30.6.2 joerg P_32_COPY(srcp->dsize, destp->dsize);
924 1.30.6.2 joerg P_32_COPY(srcp->ssize, destp->ssize);
925 1.30.6.2 joerg P_32_COPY(srcp->sshift, destp->sshift);
926 1.30.6.2 joerg P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
927 1.30.6.2 joerg P_32_COPY(srcp->last_freed, destp->last_freed);
928 1.30.6.2 joerg P_32_COPY(srcp->max_bucket, destp->max_bucket);
929 1.30.6.2 joerg P_32_COPY(srcp->high_mask, destp->high_mask);
930 1.30.6.2 joerg P_32_COPY(srcp->low_mask, destp->low_mask);
931 1.30.6.2 joerg P_32_COPY(srcp->ffactor, destp->ffactor);
932 1.30.6.2 joerg P_32_COPY(srcp->nkeys, destp->nkeys);
933 1.30.6.2 joerg P_32_COPY(srcp->hdrpages, destp->hdrpages);
934 1.30.6.2 joerg P_32_COPY(srcp->h_charkey, destp->h_charkey);
935 1.30.6.2 joerg for (i = 0; i < NCACHED; i++) {
936 1.30.6.2 joerg P_32_COPY(srcp->spares[i], destp->spares[i]);
937 1.30.6.2 joerg P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
938 1.30.6.2 joerg }
939 1.30.6.2 joerg }
940 1.30.6.2 joerg
941 1.30.6.2 joerg static void
942 1.30.6.2 joerg swap_header(HTAB *hashp)
943 1.30.6.2 joerg {
944 1.30.6.2 joerg HASHHDR *hdrp;
945 1.30.6.2 joerg size_t i;
946 1.30.6.2 joerg
947 1.30.6.2 joerg hdrp = &hashp->hdr;
948 1.30.6.2 joerg
949 1.30.6.2 joerg M_32_SWAP(hdrp->magic);
950 1.30.6.2 joerg M_32_SWAP(hdrp->version);
951 1.30.6.2 joerg M_32_SWAP(hdrp->lorder);
952 1.30.6.2 joerg M_32_SWAP(hdrp->bsize);
953 1.30.6.2 joerg M_32_SWAP(hdrp->bshift);
954 1.30.6.2 joerg M_32_SWAP(hdrp->dsize);
955 1.30.6.2 joerg M_32_SWAP(hdrp->ssize);
956 1.30.6.2 joerg M_32_SWAP(hdrp->sshift);
957 1.30.6.2 joerg M_32_SWAP(hdrp->ovfl_point);
958 1.30.6.2 joerg M_32_SWAP(hdrp->last_freed);
959 1.30.6.2 joerg M_32_SWAP(hdrp->max_bucket);
960 1.30.6.2 joerg M_32_SWAP(hdrp->high_mask);
961 1.30.6.2 joerg M_32_SWAP(hdrp->low_mask);
962 1.30.6.2 joerg M_32_SWAP(hdrp->ffactor);
963 1.30.6.2 joerg M_32_SWAP(hdrp->nkeys);
964 1.30.6.2 joerg M_32_SWAP(hdrp->hdrpages);
965 1.30.6.2 joerg M_32_SWAP(hdrp->h_charkey);
966 1.30.6.2 joerg for (i = 0; i < NCACHED; i++) {
967 1.30.6.2 joerg M_32_SWAP(hdrp->spares[i]);
968 1.30.6.2 joerg M_16_SWAP(hdrp->bitmaps[i]);
969 1.30.6.2 joerg }
970 1.30.6.2 joerg }
971 1.30.6.2 joerg #endif
972