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