File: | src/lib/libc/db/hash/hash.c |
Warning: | line 127, column 4 Potential leak of memory pointed to by 'hashp' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: hash.c,v 1.29 2016/09/21 04:38:56 guenther Exp $ */ | |||
2 | ||||
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 | ||||
35 | #include <sys/stat.h> | |||
36 | ||||
37 | #include <errno(*__errno()).h> | |||
38 | #include <fcntl.h> | |||
39 | #include <stdio.h> | |||
40 | #include <stdlib.h> | |||
41 | #include <string.h> | |||
42 | #include <unistd.h> | |||
43 | #ifdef DEBUG | |||
44 | #include <assert.h> | |||
45 | #endif | |||
46 | ||||
47 | #include <db.h> | |||
48 | #include "hash.h" | |||
49 | #include "page.h" | |||
50 | #include "extern.h" | |||
51 | ||||
52 | #define MAXIMUM(a, b)(((a) > (b)) ? (a) : (b)) (((a) > (b)) ? (a) : (b)) | |||
53 | ||||
54 | static int alloc_segs(HTAB *, int); | |||
55 | static int flush_meta(HTAB *); | |||
56 | static int hash_access(HTAB *, ACTION, DBT *, DBT *); | |||
57 | static int hash_close(DB *); | |||
58 | static int hash_delete(const DB *, const DBT *, u_int32_t); | |||
59 | static int hash_fd(const DB *); | |||
60 | static int hash_get(const DB *, const DBT *, DBT *, u_int32_t); | |||
61 | static int hash_put(const DB *, DBT *, const DBT *, u_int32_t); | |||
62 | static void *hash_realloc(SEGMENT **, int, int); | |||
63 | static int hash_seq(const DB *, DBT *, DBT *, u_int32_t); | |||
64 | static int hash_sync(const DB *, u_int32_t); | |||
65 | static int hdestroy(HTAB *); | |||
66 | static HTAB *init_hash(HTAB *, const char *, const HASHINFO *); | |||
67 | static int init_htab(HTAB *, int); | |||
68 | #if BYTE_ORDER1234 == LITTLE_ENDIAN1234 | |||
69 | static void swap_header(HTAB *); | |||
70 | static void swap_header_copy(HASHHDR *, HASHHDR *); | |||
71 | #endif | |||
72 | ||||
73 | /* Fast arithmetic, relying on powers of 2, */ | |||
74 | #define MOD(x, y)((x) & ((y) - 1)) ((x) & ((y) - 1)) | |||
75 | ||||
76 | #define RETURN_ERROR(ERR, LOC){ save_errno = ERR; goto LOC; } { save_errno = ERR; goto LOC; } | |||
77 | ||||
78 | /* Return values */ | |||
79 | #define SUCCESS(0) (0) | |||
80 | #define ERROR(-1) (-1) | |||
81 | #define ABNORMAL(1) (1) | |||
82 | ||||
83 | #ifdef HASH_STATISTICS | |||
84 | int hash_accesses, hash_collisions, hash_expansions, hash_overflows; | |||
85 | #endif | |||
86 | ||||
87 | /************************** INTERFACE ROUTINES ***************************/ | |||
88 | /* OPEN/CLOSE */ | |||
89 | ||||
90 | DB * | |||
91 | __hash_open(const char *file, int flags, int mode, | |||
92 | const HASHINFO *info, /* Special directives for create */ | |||
93 | int dflags) | |||
94 | { | |||
95 | HTAB *hashp; | |||
96 | struct stat statbuf; | |||
97 | DB *dbp; | |||
98 | int bpages, hdrsize, new_table, nsegs, save_errno; | |||
99 | ||||
100 | if ((flags & O_ACCMODE0x0003) == O_WRONLY0x0001) { | |||
| ||||
101 | errno(*__errno()) = EINVAL22; | |||
102 | return (NULL((void *)0)); | |||
103 | } | |||
104 | ||||
105 | if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB)))) | |||
106 | return (NULL((void *)0)); | |||
107 | hashp->fp = -1; | |||
108 | ||||
109 | /* | |||
110 | * Even if user wants write only, we need to be able to read | |||
111 | * the actual file, so we need to open it read/write. But, the | |||
112 | * field in the hashp structure needs to be accurate so that | |||
113 | * we can check accesses. | |||
114 | */ | |||
115 | hashp->flags = flags; | |||
116 | ||||
117 | if (file) { | |||
118 | if ((hashp->fp = open(file, flags | O_CLOEXEC0x10000, mode)) == -1) | |||
119 | RETURN_ERROR(errno, error0){ save_errno = (*__errno()); goto error0; }; | |||
120 | new_table = fstat(hashp->fp, &statbuf) == 0 && | |||
121 | statbuf.st_size == 0 && (flags & O_ACCMODE0x0003) != O_RDONLY0x0000; | |||
122 | } else | |||
123 | new_table = 1; | |||
124 | ||||
125 | if (new_table
| |||
126 | if (!(hashp = init_hash(hashp, file, info))) | |||
127 | RETURN_ERROR(errno, error1){ save_errno = (*__errno()); goto error1; }; | |||
| ||||
128 | } else { | |||
129 | /* Table already exists */ | |||
130 | if (info && info->hash) | |||
131 | hashp->hash = info->hash; | |||
132 | else | |||
133 | hashp->hash = __default_hash; | |||
134 | ||||
135 | hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR)); | |||
136 | #if BYTE_ORDER1234 == LITTLE_ENDIAN1234 | |||
137 | swap_header(hashp); | |||
138 | #endif | |||
139 | if (hdrsize == -1) | |||
140 | RETURN_ERROR(errno, error1){ save_errno = (*__errno()); goto error1; }; | |||
141 | if (hdrsize != sizeof(HASHHDR)) | |||
142 | RETURN_ERROR(EFTYPE, error1){ save_errno = 79; goto error1; }; | |||
143 | /* Verify file type, versions and hash function */ | |||
144 | if (hashp->MAGIChdr.magic != HASHMAGIC0x061561) | |||
145 | RETURN_ERROR(EFTYPE, error1){ save_errno = 79; goto error1; }; | |||
146 | #define OLDHASHVERSION1 1 | |||
147 | if (hashp->VERSIONhdr.version != HASHVERSION2 && | |||
148 | hashp->VERSIONhdr.version != OLDHASHVERSION1) | |||
149 | RETURN_ERROR(EFTYPE, error1){ save_errno = 79; goto error1; }; | |||
150 | if (hashp->hash(CHARKEY"%$sniglet^&", sizeof(CHARKEY"%$sniglet^&")) != hashp->H_CHARKEYhdr.h_charkey) | |||
151 | RETURN_ERROR(EFTYPE, error1){ save_errno = 79; goto error1; }; | |||
152 | /* | |||
153 | * Figure out how many segments we need. Max_Bucket is the | |||
154 | * maximum bucket number, so the number of buckets is | |||
155 | * max_bucket + 1. | |||
156 | */ | |||
157 | nsegs = (hashp->MAX_BUCKEThdr.max_bucket + 1 + hashp->SGSIZEhdr.ssize - 1) / | |||
158 | hashp->SGSIZEhdr.ssize; | |||
159 | if (alloc_segs(hashp, nsegs)) | |||
160 | /* | |||
161 | * If alloc_segs fails, table will have been destroyed | |||
162 | * and errno will have been set. | |||
163 | */ | |||
164 | return (NULL((void *)0)); | |||
165 | /* Read in bitmaps */ | |||
166 | bpages = (hashp->SPAREShdr.spares[hashp->OVFL_POINThdr.ovfl_point] + | |||
167 | (hashp->BSIZEhdr.bsize << BYTE_SHIFT3) - 1) >> | |||
168 | (hashp->BSHIFThdr.bshift + BYTE_SHIFT3); | |||
169 | ||||
170 | hashp->nmaps = bpages; | |||
171 | (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *)); | |||
172 | } | |||
173 | ||||
174 | /* Initialize Buffer Manager */ | |||
175 | if (info && info->cachesize) | |||
176 | __buf_init(hashp, info->cachesize); | |||
177 | else | |||
178 | __buf_init(hashp, DEF_BUFSIZE65536); | |||
179 | ||||
180 | hashp->new_file = new_table; | |||
181 | hashp->save_file = file && (hashp->flags & O_RDWR0x0002); | |||
182 | hashp->cbucket = -1; | |||
183 | if (!(dbp = (DB *)malloc(sizeof(DB)))) { | |||
184 | save_errno = errno(*__errno()); | |||
185 | hdestroy(hashp); | |||
186 | errno(*__errno()) = save_errno; | |||
187 | return (NULL((void *)0)); | |||
188 | } | |||
189 | dbp->internal = hashp; | |||
190 | dbp->close = hash_close; | |||
191 | dbp->del = hash_delete; | |||
192 | dbp->fd = hash_fd; | |||
193 | dbp->get = hash_get; | |||
194 | dbp->put = hash_put; | |||
195 | dbp->seq = hash_seq; | |||
196 | dbp->sync = hash_sync; | |||
197 | dbp->type = DB_HASH; | |||
198 | ||||
199 | #ifdef DEBUG | |||
200 | (void)fprintf(stderr(&__sF[2]), | |||
201 | "%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", | |||
202 | "init_htab:", | |||
203 | "TABLE POINTER ", hashp, | |||
204 | "BUCKET SIZE ", hashp->BSIZEhdr.bsize, | |||
205 | "BUCKET SHIFT ", hashp->BSHIFThdr.bshift, | |||
206 | "DIRECTORY SIZE ", hashp->DSIZEhdr.dsize, | |||
207 | "SEGMENT SIZE ", hashp->SGSIZEhdr.ssize, | |||
208 | "SEGMENT SHIFT ", hashp->SSHIFThdr.sshift, | |||
209 | "FILL FACTOR ", hashp->FFACTORhdr.ffactor, | |||
210 | "MAX BUCKET ", hashp->MAX_BUCKEThdr.max_bucket, | |||
211 | "OVFL POINT ", hashp->OVFL_POINThdr.ovfl_point, | |||
212 | "LAST FREED ", hashp->LAST_FREEDhdr.last_freed, | |||
213 | "HIGH MASK ", hashp->HIGH_MASKhdr.high_mask, | |||
214 | "LOW MASK ", hashp->LOW_MASKhdr.low_mask, | |||
215 | "NSEGS ", hashp->nsegs, | |||
216 | "NKEYS ", hashp->NKEYShdr.nkeys); | |||
217 | #endif | |||
218 | #ifdef HASH_STATISTICS | |||
219 | hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0; | |||
220 | #endif | |||
221 | return (dbp); | |||
222 | ||||
223 | error1: | |||
224 | if (hashp != NULL((void *)0)) | |||
225 | (void)close(hashp->fp); | |||
226 | ||||
227 | error0: | |||
228 | free(hashp); | |||
229 | errno(*__errno()) = save_errno; | |||
230 | return (NULL((void *)0)); | |||
231 | } | |||
232 | ||||
233 | static int | |||
234 | hash_close(DB *dbp) | |||
235 | { | |||
236 | HTAB *hashp; | |||
237 | int retval; | |||
238 | ||||
239 | if (!dbp) | |||
240 | return (ERROR(-1)); | |||
241 | ||||
242 | hashp = (HTAB *)dbp->internal; | |||
243 | retval = hdestroy(hashp); | |||
244 | free(dbp); | |||
245 | return (retval); | |||
246 | } | |||
247 | ||||
248 | static int | |||
249 | hash_fd(const DB *dbp) | |||
250 | { | |||
251 | HTAB *hashp; | |||
252 | ||||
253 | if (!dbp) | |||
254 | return (ERROR(-1)); | |||
255 | ||||
256 | hashp = (HTAB *)dbp->internal; | |||
257 | if (hashp->fp == -1) { | |||
258 | errno(*__errno()) = ENOENT2; | |||
259 | return (-1); | |||
260 | } | |||
261 | return (hashp->fp); | |||
262 | } | |||
263 | ||||
264 | /************************** LOCAL CREATION ROUTINES **********************/ | |||
265 | static HTAB * | |||
266 | init_hash(HTAB *hashp, const char *file, const HASHINFO *info) | |||
267 | { | |||
268 | struct stat statbuf; | |||
269 | int nelem; | |||
270 | ||||
271 | nelem = 1; | |||
272 | hashp->NKEYShdr.nkeys = 0; | |||
273 | hashp->LORDERhdr.lorder = BYTE_ORDER1234; | |||
274 | hashp->BSIZEhdr.bsize = DEF_BUCKET_SIZE4096; | |||
275 | hashp->BSHIFThdr.bshift = DEF_BUCKET_SHIFT12; | |||
276 | hashp->SGSIZEhdr.ssize = DEF_SEGSIZE256; | |||
277 | hashp->SSHIFThdr.sshift = DEF_SEGSIZE_SHIFT8; | |||
278 | hashp->DSIZEhdr.dsize = DEF_DIRSIZE256; | |||
279 | hashp->FFACTORhdr.ffactor = DEF_FFACTOR65536; | |||
280 | hashp->hash = __default_hash; | |||
281 | memset(hashp->SPAREShdr.spares, 0, sizeof(hashp->SPAREShdr.spares)); | |||
282 | memset(hashp->BITMAPShdr.bitmaps, 0, sizeof (hashp->BITMAPShdr.bitmaps)); | |||
283 | ||||
284 | /* Fix bucket size to be optimal for file system */ | |||
285 | if (file != NULL((void *)0)) { | |||
286 | if (stat(file, &statbuf)) | |||
287 | return (NULL((void *)0)); | |||
288 | hashp->BSIZEhdr.bsize = statbuf.st_blksize; | |||
289 | hashp->BSHIFThdr.bshift = __log2(hashp->BSIZEhdr.bsize); | |||
290 | } | |||
291 | ||||
292 | if (info) { | |||
293 | if (info->bsize) { | |||
294 | /* Round pagesize up to power of 2 */ | |||
295 | hashp->BSHIFThdr.bshift = __log2(info->bsize); | |||
296 | hashp->BSIZEhdr.bsize = 1 << hashp->BSHIFThdr.bshift; | |||
297 | if (hashp->BSIZEhdr.bsize > MAX_BSIZE65536) { | |||
298 | errno(*__errno()) = EINVAL22; | |||
299 | return (NULL((void *)0)); | |||
300 | } | |||
301 | } | |||
302 | if (info->ffactor) | |||
303 | hashp->FFACTORhdr.ffactor = info->ffactor; | |||
304 | if (info->hash) | |||
305 | hashp->hash = info->hash; | |||
306 | if (info->nelem) | |||
307 | nelem = info->nelem; | |||
308 | if (info->lorder) { | |||
309 | if (info->lorder != BIG_ENDIAN4321 && | |||
310 | info->lorder != LITTLE_ENDIAN1234) { | |||
311 | errno(*__errno()) = EINVAL22; | |||
312 | return (NULL((void *)0)); | |||
313 | } | |||
314 | hashp->LORDERhdr.lorder = info->lorder; | |||
315 | } | |||
316 | } | |||
317 | /* init_htab should destroy the table and set errno if it fails */ | |||
318 | if (init_htab(hashp, nelem)) | |||
319 | return (NULL((void *)0)); | |||
320 | else | |||
321 | return (hashp); | |||
322 | } | |||
323 | /* | |||
324 | * This calls alloc_segs which may run out of memory. Alloc_segs will destroy | |||
325 | * the table and set errno, so we just pass the error information along. | |||
326 | * | |||
327 | * Returns 0 on No Error | |||
328 | */ | |||
329 | static int | |||
330 | init_htab(HTAB *hashp, int nelem) | |||
331 | { | |||
332 | int nbuckets, nsegs, l2; | |||
333 | ||||
334 | /* | |||
335 | * Divide number of elements by the fill factor and determine a | |||
336 | * desired number of buckets. Allocate space for the next greater | |||
337 | * power of two number of buckets. | |||
338 | */ | |||
339 | nelem = (nelem - 1) / hashp->FFACTORhdr.ffactor + 1; | |||
340 | ||||
341 | l2 = __log2(MAXIMUM(nelem, 2)(((nelem) > (2)) ? (nelem) : (2))); | |||
342 | nbuckets = 1 << l2; | |||
343 | ||||
344 | hashp->SPAREShdr.spares[l2] = l2 + 1; | |||
345 | hashp->SPAREShdr.spares[l2 + 1] = l2 + 1; | |||
346 | hashp->OVFL_POINThdr.ovfl_point = l2; | |||
347 | hashp->LAST_FREEDhdr.last_freed = 2; | |||
348 | ||||
349 | /* First bitmap page is at: splitpoint l2 page offset 1 */ | |||
350 | if (__ibitmap(hashp, OADDR_OF(l2, 1)((u_int32_t)((u_int32_t)(l2) << 11) + (1)), l2 + 1, 0)) | |||
351 | return (-1); | |||
352 | ||||
353 | hashp->MAX_BUCKEThdr.max_bucket = hashp->LOW_MASKhdr.low_mask = nbuckets - 1; | |||
354 | hashp->HIGH_MASKhdr.high_mask = (nbuckets << 1) - 1; | |||
355 | hashp->HDRPAGEShdr.hdrpages = ((MAXIMUM(sizeof(HASHHDR), MINHDRSIZE)(((sizeof(HASHHDR)) > (512)) ? (sizeof(HASHHDR)) : (512)) - 1) >> | |||
356 | hashp->BSHIFThdr.bshift) + 1; | |||
357 | ||||
358 | nsegs = (nbuckets - 1) / hashp->SGSIZEhdr.ssize + 1; | |||
359 | nsegs = 1 << __log2(nsegs); | |||
360 | ||||
361 | if (nsegs > hashp->DSIZEhdr.dsize) | |||
362 | hashp->DSIZEhdr.dsize = nsegs; | |||
363 | return (alloc_segs(hashp, nsegs)); | |||
364 | } | |||
365 | ||||
366 | /********************** DESTROY/CLOSE ROUTINES ************************/ | |||
367 | ||||
368 | /* | |||
369 | * Flushes any changes to the file if necessary and destroys the hashp | |||
370 | * structure, freeing all allocated space. | |||
371 | */ | |||
372 | static int | |||
373 | hdestroy(HTAB *hashp) | |||
374 | { | |||
375 | int i, save_errno; | |||
376 | ||||
377 | save_errno = 0; | |||
378 | ||||
379 | #ifdef HASH_STATISTICS | |||
380 | (void)fprintf(stderr(&__sF[2]), "hdestroy: accesses %ld collisions %ld\n", | |||
381 | hash_accesses, hash_collisions); | |||
382 | (void)fprintf(stderr(&__sF[2]), "hdestroy: expansions %ld\n", | |||
383 | hash_expansions); | |||
384 | (void)fprintf(stderr(&__sF[2]), "hdestroy: overflows %ld\n", | |||
385 | hash_overflows); | |||
386 | (void)fprintf(stderr(&__sF[2]), "keys %ld maxp %d segmentcount %d\n", | |||
387 | hashp->NKEYShdr.nkeys, hashp->MAX_BUCKEThdr.max_bucket, hashp->nsegs); | |||
388 | ||||
389 | for (i = 0; i < NCACHED32; i++) | |||
390 | (void)fprintf(stderr(&__sF[2]), | |||
391 | "spares[%d] = %d\n", i, hashp->SPAREShdr.spares[i]); | |||
392 | #endif | |||
393 | /* | |||
394 | * Call on buffer manager to free buffers, and if required, | |||
395 | * write them to disk. | |||
396 | */ | |||
397 | if (__buf_free(hashp, 1, hashp->save_file)) | |||
398 | save_errno = errno(*__errno()); | |||
399 | if (hashp->dir) { | |||
400 | free(*hashp->dir); /* Free initial segments */ | |||
401 | /* Free extra segments */ | |||
402 | while (hashp->exsegs--) | |||
403 | free(hashp->dir[--hashp->nsegs]); | |||
404 | free(hashp->dir); | |||
405 | } | |||
406 | if (flush_meta(hashp) && !save_errno) | |||
407 | save_errno = errno(*__errno()); | |||
408 | /* Free Bigmaps */ | |||
409 | for (i = 0; i < hashp->nmaps; i++) | |||
410 | free(hashp->mapp[i]); | |||
411 | free(hashp->tmp_key); | |||
412 | free(hashp->tmp_buf); | |||
413 | ||||
414 | if (hashp->fp != -1) | |||
415 | (void)close(hashp->fp); | |||
416 | ||||
417 | free(hashp); | |||
418 | ||||
419 | if (save_errno) { | |||
420 | errno(*__errno()) = save_errno; | |||
421 | return (ERROR(-1)); | |||
422 | } | |||
423 | return (SUCCESS(0)); | |||
424 | } | |||
425 | /* | |||
426 | * Write modified pages to disk | |||
427 | * | |||
428 | * Returns: | |||
429 | * 0 == OK | |||
430 | * -1 ERROR | |||
431 | */ | |||
432 | static int | |||
433 | hash_sync(const DB *dbp, u_int32_t flags) | |||
434 | { | |||
435 | HTAB *hashp; | |||
436 | ||||
437 | if (flags != 0) { | |||
438 | errno(*__errno()) = EINVAL22; | |||
439 | return (ERROR(-1)); | |||
440 | } | |||
441 | ||||
442 | if (!dbp) | |||
443 | return (ERROR(-1)); | |||
444 | ||||
445 | hashp = (HTAB *)dbp->internal; | |||
446 | if (!hashp->save_file) | |||
447 | return (0); | |||
448 | if (__buf_free(hashp, 0, 1) || flush_meta(hashp)) | |||
449 | return (ERROR(-1)); | |||
450 | hashp->new_file = 0; | |||
451 | return (0); | |||
452 | } | |||
453 | ||||
454 | /* | |||
455 | * Returns: | |||
456 | * 0 == OK | |||
457 | * -1 indicates that errno should be set | |||
458 | */ | |||
459 | static int | |||
460 | flush_meta(HTAB *hashp) | |||
461 | { | |||
462 | HASHHDR *whdrp; | |||
463 | #if BYTE_ORDER1234 == LITTLE_ENDIAN1234 | |||
464 | HASHHDR whdr; | |||
465 | #endif | |||
466 | int fp, i, wsize; | |||
467 | ||||
468 | if (!hashp->save_file) | |||
469 | return (0); | |||
470 | hashp->MAGIChdr.magic = HASHMAGIC0x061561; | |||
471 | hashp->VERSIONhdr.version = HASHVERSION2; | |||
472 | hashp->H_CHARKEYhdr.h_charkey = hashp->hash(CHARKEY"%$sniglet^&", sizeof(CHARKEY"%$sniglet^&")); | |||
473 | ||||
474 | fp = hashp->fp; | |||
475 | whdrp = &hashp->hdr; | |||
476 | #if BYTE_ORDER1234 == LITTLE_ENDIAN1234 | |||
477 | whdrp = &whdr; | |||
478 | swap_header_copy(&hashp->hdr, whdrp); | |||
479 | #endif | |||
480 | if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), 0)) == -1) | |||
481 | return (-1); | |||
482 | else | |||
483 | if (wsize != sizeof(HASHHDR)) { | |||
484 | errno(*__errno()) = EFTYPE79; | |||
485 | hashp->err = errno(*__errno()); | |||
486 | return (-1); | |||
487 | } | |||
488 | for (i = 0; i < NCACHED32; i++) | |||
489 | if (hashp->mapp[i]) | |||
490 | if (__put_page(hashp, (char *)hashp->mapp[i], | |||
491 | hashp->BITMAPShdr.bitmaps[i], 0, 1)) | |||
492 | return (-1); | |||
493 | return (0); | |||
494 | } | |||
495 | ||||
496 | /*******************************SEARCH ROUTINES *****************************/ | |||
497 | /* | |||
498 | * All the access routines return | |||
499 | * | |||
500 | * Returns: | |||
501 | * 0 on SUCCESS | |||
502 | * 1 to indicate an external ERROR (i.e. key not found, etc) | |||
503 | * -1 to indicate an internal ERROR (i.e. out of memory, etc) | |||
504 | */ | |||
505 | static int | |||
506 | hash_get(const DB *dbp, const DBT *key, DBT *data, u_int32_t flag) | |||
507 | { | |||
508 | HTAB *hashp; | |||
509 | ||||
510 | hashp = (HTAB *)dbp->internal; | |||
511 | if (flag) { | |||
512 | hashp->err = errno(*__errno()) = EINVAL22; | |||
513 | return (ERROR(-1)); | |||
514 | } | |||
515 | return (hash_access(hashp, HASH_GET, (DBT *)key, data)); | |||
516 | } | |||
517 | ||||
518 | static int | |||
519 | hash_put(const DB *dbp, DBT *key, const DBT *data, u_int32_t flag) | |||
520 | { | |||
521 | HTAB *hashp; | |||
522 | ||||
523 | hashp = (HTAB *)dbp->internal; | |||
524 | if (flag && flag != R_NOOVERWRITE8) { | |||
525 | hashp->err = errno(*__errno()) = EINVAL22; | |||
526 | return (ERROR(-1)); | |||
527 | } | |||
528 | if ((hashp->flags & O_ACCMODE0x0003) == O_RDONLY0x0000) { | |||
529 | hashp->err = errno(*__errno()) = EPERM1; | |||
530 | return (ERROR(-1)); | |||
531 | } | |||
532 | return (hash_access(hashp, flag == R_NOOVERWRITE8 ? | |||
533 | HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data)); | |||
534 | } | |||
535 | ||||
536 | static int | |||
537 | hash_delete(const DB *dbp, const DBT *key, | |||
538 | u_int32_t flag) /* Ignored */ | |||
539 | { | |||
540 | HTAB *hashp; | |||
541 | ||||
542 | hashp = (HTAB *)dbp->internal; | |||
543 | if (flag && flag != R_CURSOR1) { | |||
544 | hashp->err = errno(*__errno()) = EINVAL22; | |||
545 | return (ERROR(-1)); | |||
546 | } | |||
547 | if ((hashp->flags & O_ACCMODE0x0003) == O_RDONLY0x0000) { | |||
548 | hashp->err = errno(*__errno()) = EPERM1; | |||
549 | return (ERROR(-1)); | |||
550 | } | |||
551 | return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL((void *)0))); | |||
552 | } | |||
553 | ||||
554 | /* | |||
555 | * Assume that hashp has been set in wrapper routine. | |||
556 | */ | |||
557 | static int | |||
558 | hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val) | |||
559 | { | |||
560 | BUFHEAD *rbufp; | |||
561 | BUFHEAD *bufp, *save_bufp; | |||
562 | u_int16_t *bp; | |||
563 | int n, ndx, off, size; | |||
564 | char *kp; | |||
565 | u_int16_t pageno; | |||
566 | ||||
567 | #ifdef HASH_STATISTICS | |||
568 | hash_accesses++; | |||
569 | #endif | |||
570 | ||||
571 | off = hashp->BSIZEhdr.bsize; | |||
572 | size = key->size; | |||
573 | kp = (char *)key->data; | |||
574 | rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL((void *)0), 0); | |||
575 | if (!rbufp) | |||
576 | return (ERROR(-1)); | |||
577 | save_bufp = rbufp; | |||
578 | ||||
579 | /* Pin the bucket chain */ | |||
580 | rbufp->flags |= BUF_PIN0x0008; | |||
581 | for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;) | |||
582 | if (bp[1] >= REAL_KEY4) { | |||
583 | /* Real key/data pair */ | |||
584 | if (size == off - *bp && | |||
585 | memcmp(kp, rbufp->page + *bp, size) == 0) | |||
586 | goto found; | |||
587 | off = bp[1]; | |||
588 | #ifdef HASH_STATISTICS | |||
589 | hash_collisions++; | |||
590 | #endif | |||
591 | bp += 2; | |||
592 | ndx += 2; | |||
593 | } else if (bp[1] == OVFLPAGE0) { | |||
594 | rbufp = __get_buf(hashp, *bp, rbufp, 0); | |||
595 | if (!rbufp) { | |||
596 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
597 | return (ERROR(-1)); | |||
598 | } | |||
599 | /* FOR LOOP INIT */ | |||
600 | bp = (u_int16_t *)rbufp->page; | |||
601 | n = *bp++; | |||
602 | ndx = 1; | |||
603 | off = hashp->BSIZEhdr.bsize; | |||
604 | } else if (bp[1] < REAL_KEY4) { | |||
605 | if ((ndx = | |||
606 | __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0) | |||
607 | goto found; | |||
608 | if (ndx == -2) { | |||
609 | bufp = rbufp; | |||
610 | if (!(pageno = | |||
611 | __find_last_page(hashp, &bufp))) { | |||
612 | ndx = 0; | |||
613 | rbufp = bufp; | |||
614 | break; /* FOR */ | |||
615 | } | |||
616 | rbufp = __get_buf(hashp, pageno, bufp, 0); | |||
617 | if (!rbufp) { | |||
618 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
619 | return (ERROR(-1)); | |||
620 | } | |||
621 | /* FOR LOOP INIT */ | |||
622 | bp = (u_int16_t *)rbufp->page; | |||
623 | n = *bp++; | |||
624 | ndx = 1; | |||
625 | off = hashp->BSIZEhdr.bsize; | |||
626 | } else { | |||
627 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
628 | return (ERROR(-1)); | |||
629 | } | |||
630 | } | |||
631 | ||||
632 | /* Not found */ | |||
633 | switch (action) { | |||
634 | case HASH_PUT: | |||
635 | case HASH_PUTNEW: | |||
636 | if (__addel(hashp, rbufp, key, val)) { | |||
637 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
638 | return (ERROR(-1)); | |||
639 | } else { | |||
640 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
641 | return (SUCCESS(0)); | |||
642 | } | |||
643 | case HASH_GET: | |||
644 | case HASH_DELETE: | |||
645 | default: | |||
646 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
647 | return (ABNORMAL(1)); | |||
648 | } | |||
649 | ||||
650 | found: | |||
651 | switch (action) { | |||
652 | case HASH_PUTNEW: | |||
653 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
654 | return (ABNORMAL(1)); | |||
655 | case HASH_GET: | |||
656 | bp = (u_int16_t *)rbufp->page; | |||
657 | if (bp[ndx + 1] < REAL_KEY4) { | |||
658 | if (__big_return(hashp, rbufp, ndx, val, 0)) | |||
659 | return (ERROR(-1)); | |||
660 | } else { | |||
661 | val->data = (u_char *)rbufp->page + (int)bp[ndx + 1]; | |||
662 | val->size = bp[ndx] - bp[ndx + 1]; | |||
663 | } | |||
664 | break; | |||
665 | case HASH_PUT: | |||
666 | if ((__delpair(hashp, rbufp, ndx)) || | |||
667 | (__addel(hashp, rbufp, key, val))) { | |||
668 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
669 | return (ERROR(-1)); | |||
670 | } | |||
671 | break; | |||
672 | case HASH_DELETE: | |||
673 | if (__delpair(hashp, rbufp, ndx)) | |||
674 | return (ERROR(-1)); | |||
675 | break; | |||
676 | default: | |||
677 | abort(); | |||
678 | } | |||
679 | save_bufp->flags &= ~BUF_PIN0x0008; | |||
680 | return (SUCCESS(0)); | |||
681 | } | |||
682 | ||||
683 | static int | |||
684 | hash_seq(const DB *dbp, DBT *key, DBT *data, u_int32_t flag) | |||
685 | { | |||
686 | u_int32_t bucket; | |||
687 | BUFHEAD *bufp; | |||
688 | HTAB *hashp; | |||
689 | u_int16_t *bp, ndx; | |||
690 | ||||
691 | hashp = (HTAB *)dbp->internal; | |||
692 | if (flag && flag != R_FIRST3 && flag != R_NEXT7) { | |||
693 | hashp->err = errno(*__errno()) = EINVAL22; | |||
694 | return (ERROR(-1)); | |||
695 | } | |||
696 | #ifdef HASH_STATISTICS | |||
697 | hash_accesses++; | |||
698 | #endif | |||
699 | if ((hashp->cbucket < 0) || (flag == R_FIRST3)) { | |||
700 | hashp->cbucket = 0; | |||
701 | hashp->cndx = 1; | |||
702 | hashp->cpage = NULL((void *)0); | |||
703 | } | |||
704 | next_bucket: | |||
705 | for (bp = NULL((void *)0); !bp || !bp[0]; ) { | |||
706 | if (!(bufp = hashp->cpage)) { | |||
707 | for (bucket = hashp->cbucket; | |||
708 | bucket <= hashp->MAX_BUCKEThdr.max_bucket; | |||
709 | bucket++, hashp->cndx = 1) { | |||
710 | bufp = __get_buf(hashp, bucket, NULL((void *)0), 0); | |||
711 | if (!bufp) | |||
712 | return (ERROR(-1)); | |||
713 | hashp->cpage = bufp; | |||
714 | bp = (u_int16_t *)bufp->page; | |||
715 | if (bp[0]) | |||
716 | break; | |||
717 | } | |||
718 | hashp->cbucket = bucket; | |||
719 | if (hashp->cbucket > hashp->MAX_BUCKEThdr.max_bucket) { | |||
720 | hashp->cbucket = -1; | |||
721 | return (ABNORMAL(1)); | |||
722 | } | |||
723 | } else { | |||
724 | bp = (u_int16_t *)hashp->cpage->page; | |||
725 | if (flag == R_NEXT7) { | |||
726 | hashp->cndx += 2; | |||
727 | if (hashp->cndx > bp[0]) { | |||
728 | hashp->cpage = NULL((void *)0); | |||
729 | hashp->cbucket++; | |||
730 | hashp->cndx = 1; | |||
731 | goto next_bucket; | |||
732 | } | |||
733 | } | |||
734 | } | |||
735 | ||||
736 | #ifdef DEBUG | |||
737 | assert(bp); | |||
738 | assert(bufp); | |||
739 | #endif | |||
740 | while (bp[hashp->cndx + 1] == OVFLPAGE0) { | |||
741 | bufp = hashp->cpage = | |||
742 | __get_buf(hashp, bp[hashp->cndx], bufp, 0); | |||
743 | if (!bufp) | |||
744 | return (ERROR(-1)); | |||
745 | bp = (u_int16_t *)(bufp->page); | |||
746 | hashp->cndx = 1; | |||
747 | } | |||
748 | if (!bp[0]) { | |||
749 | hashp->cpage = NULL((void *)0); | |||
750 | ++hashp->cbucket; | |||
751 | } | |||
752 | } | |||
753 | ndx = hashp->cndx; | |||
754 | if (bp[ndx + 1] < REAL_KEY4) { | |||
755 | if (__big_keydata(hashp, bufp, key, data, 1)) | |||
756 | return (ERROR(-1)); | |||
757 | } else { | |||
758 | if (hashp->cpage == 0) | |||
759 | return (ERROR(-1)); | |||
760 | key->data = (u_char *)hashp->cpage->page + bp[ndx]; | |||
761 | key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZEhdr.bsize) - bp[ndx]; | |||
762 | data->data = (u_char *)hashp->cpage->page + bp[ndx + 1]; | |||
763 | data->size = bp[ndx] - bp[ndx + 1]; | |||
764 | } | |||
765 | return (SUCCESS(0)); | |||
766 | } | |||
767 | ||||
768 | /********************************* UTILITIES ************************/ | |||
769 | ||||
770 | /* | |||
771 | * Returns: | |||
772 | * 0 ==> OK | |||
773 | * -1 ==> Error | |||
774 | */ | |||
775 | int | |||
776 | __expand_table(HTAB *hashp) | |||
777 | { | |||
778 | u_int32_t old_bucket, new_bucket; | |||
779 | int dirsize, new_segnum, spare_ndx; | |||
780 | ||||
781 | #ifdef HASH_STATISTICS | |||
782 | hash_expansions++; | |||
783 | #endif | |||
784 | new_bucket = ++hashp->MAX_BUCKEThdr.max_bucket; | |||
785 | old_bucket = (hashp->MAX_BUCKEThdr.max_bucket & hashp->LOW_MASKhdr.low_mask); | |||
786 | ||||
787 | new_segnum = new_bucket >> hashp->SSHIFThdr.sshift; | |||
788 | ||||
789 | /* Check if we need a new segment */ | |||
790 | if (new_segnum >= hashp->nsegs) { | |||
791 | /* Check if we need to expand directory */ | |||
792 | if (new_segnum >= hashp->DSIZEhdr.dsize) { | |||
793 | /* Reallocate directory */ | |||
794 | dirsize = hashp->DSIZEhdr.dsize * sizeof(SEGMENT *); | |||
795 | if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1)) | |||
796 | return (-1); | |||
797 | hashp->DSIZEhdr.dsize = dirsize << 1; | |||
798 | } | |||
799 | if ((hashp->dir[new_segnum] = | |||
800 | (SEGMENT)calloc(hashp->SGSIZEhdr.ssize, sizeof(SEGMENT))) == NULL((void *)0)) | |||
801 | return (-1); | |||
802 | hashp->exsegs++; | |||
803 | hashp->nsegs++; | |||
804 | } | |||
805 | /* | |||
806 | * If the split point is increasing (MAX_BUCKET's log base 2 | |||
807 | * * increases), we need to copy the current contents of the spare | |||
808 | * split bucket to the next bucket. | |||
809 | */ | |||
810 | spare_ndx = __log2(hashp->MAX_BUCKEThdr.max_bucket + 1); | |||
811 | if (spare_ndx > hashp->OVFL_POINThdr.ovfl_point) { | |||
812 | hashp->SPAREShdr.spares[spare_ndx] = hashp->SPAREShdr.spares[hashp->OVFL_POINThdr.ovfl_point]; | |||
813 | hashp->OVFL_POINThdr.ovfl_point = spare_ndx; | |||
814 | } | |||
815 | ||||
816 | if (new_bucket > hashp->HIGH_MASKhdr.high_mask) { | |||
817 | /* Starting a new doubling */ | |||
818 | hashp->LOW_MASKhdr.low_mask = hashp->HIGH_MASKhdr.high_mask; | |||
819 | hashp->HIGH_MASKhdr.high_mask = new_bucket | hashp->LOW_MASKhdr.low_mask; | |||
820 | } | |||
821 | /* Relocate records to the new bucket */ | |||
822 | return (__split_page(hashp, old_bucket, new_bucket)); | |||
823 | } | |||
824 | ||||
825 | /* | |||
826 | * If realloc guarantees that the pointer is not destroyed if the realloc | |||
827 | * fails, then this routine can go away. | |||
828 | */ | |||
829 | static void * | |||
830 | hash_realloc(SEGMENT **p_ptr, int oldsize, int newsize) | |||
831 | { | |||
832 | void *p; | |||
833 | ||||
834 | if ((p = malloc(newsize))) { | |||
835 | memmove(p, *p_ptr, oldsize); | |||
836 | memset((char *)p + oldsize, 0, newsize - oldsize); | |||
837 | free(*p_ptr); | |||
838 | *p_ptr = p; | |||
839 | } | |||
840 | return (p); | |||
841 | } | |||
842 | ||||
843 | u_int32_t | |||
844 | __call_hash(HTAB *hashp, char *k, int len) | |||
845 | { | |||
846 | int n, bucket; | |||
847 | ||||
848 | n = hashp->hash(k, len); | |||
849 | bucket = n & hashp->HIGH_MASKhdr.high_mask; | |||
850 | if (bucket > hashp->MAX_BUCKEThdr.max_bucket) | |||
851 | bucket = bucket & hashp->LOW_MASKhdr.low_mask; | |||
852 | return (bucket); | |||
853 | } | |||
854 | ||||
855 | /* | |||
856 | * Allocate segment table. On error, destroy the table and set errno. | |||
857 | * | |||
858 | * Returns 0 on success | |||
859 | */ | |||
860 | static int | |||
861 | alloc_segs(HTAB *hashp, int nsegs) | |||
862 | { | |||
863 | int i; | |||
864 | SEGMENT store; | |||
865 | ||||
866 | int save_errno; | |||
867 | ||||
868 | if ((hashp->dir = | |||
869 | (SEGMENT *)calloc(hashp->DSIZEhdr.dsize, sizeof(SEGMENT *))) == NULL((void *)0)) { | |||
870 | save_errno = errno(*__errno()); | |||
871 | (void)hdestroy(hashp); | |||
872 | errno(*__errno()) = save_errno; | |||
873 | return (-1); | |||
874 | } | |||
875 | hashp->nsegs = nsegs; | |||
876 | if (nsegs == 0) | |||
877 | return (0); | |||
878 | /* Allocate segments */ | |||
879 | if ((store = (SEGMENT)calloc(nsegs << hashp->SSHIFThdr.sshift, | |||
880 | sizeof(SEGMENT))) == NULL((void *)0)) { | |||
881 | save_errno = errno(*__errno()); | |||
882 | (void)hdestroy(hashp); | |||
883 | errno(*__errno()) = save_errno; | |||
884 | return (-1); | |||
885 | } | |||
886 | for (i = 0; i < nsegs; i++) | |||
887 | hashp->dir[i] = &store[i << hashp->SSHIFThdr.sshift]; | |||
888 | return (0); | |||
889 | } | |||
890 | ||||
891 | #if BYTE_ORDER1234 == LITTLE_ENDIAN1234 | |||
892 | /* | |||
893 | * Hashp->hdr needs to be byteswapped. | |||
894 | */ | |||
895 | static void | |||
896 | swap_header_copy(HASHHDR *srcp, HASHHDR *destp) | |||
897 | { | |||
898 | int i; | |||
899 | ||||
900 | P_32_COPY(srcp->magic, destp->magic){ ((char *)&(destp->magic))[0] = ((char *)&(srcp-> magic))[3]; ((char *)&(destp->magic))[1] = ((char *)& (srcp->magic))[2]; ((char *)&(destp->magic))[2] = ( (char *)&(srcp->magic))[1]; ((char *)&(destp->magic ))[3] = ((char *)&(srcp->magic))[0]; }; | |||
901 | P_32_COPY(srcp->version, destp->version){ ((char *)&(destp->version))[0] = ((char *)&(srcp ->version))[3]; ((char *)&(destp->version))[1] = (( char *)&(srcp->version))[2]; ((char *)&(destp-> version))[2] = ((char *)&(srcp->version))[1]; ((char * )&(destp->version))[3] = ((char *)&(srcp->version ))[0]; }; | |||
902 | P_32_COPY(srcp->lorder, destp->lorder){ ((char *)&(destp->lorder))[0] = ((char *)&(srcp-> lorder))[3]; ((char *)&(destp->lorder))[1] = ((char *) &(srcp->lorder))[2]; ((char *)&(destp->lorder)) [2] = ((char *)&(srcp->lorder))[1]; ((char *)&(destp ->lorder))[3] = ((char *)&(srcp->lorder))[0]; }; | |||
903 | P_32_COPY(srcp->bsize, destp->bsize){ ((char *)&(destp->bsize))[0] = ((char *)&(srcp-> bsize))[3]; ((char *)&(destp->bsize))[1] = ((char *)& (srcp->bsize))[2]; ((char *)&(destp->bsize))[2] = ( (char *)&(srcp->bsize))[1]; ((char *)&(destp->bsize ))[3] = ((char *)&(srcp->bsize))[0]; }; | |||
904 | P_32_COPY(srcp->bshift, destp->bshift){ ((char *)&(destp->bshift))[0] = ((char *)&(srcp-> bshift))[3]; ((char *)&(destp->bshift))[1] = ((char *) &(srcp->bshift))[2]; ((char *)&(destp->bshift)) [2] = ((char *)&(srcp->bshift))[1]; ((char *)&(destp ->bshift))[3] = ((char *)&(srcp->bshift))[0]; }; | |||
905 | P_32_COPY(srcp->dsize, destp->dsize){ ((char *)&(destp->dsize))[0] = ((char *)&(srcp-> dsize))[3]; ((char *)&(destp->dsize))[1] = ((char *)& (srcp->dsize))[2]; ((char *)&(destp->dsize))[2] = ( (char *)&(srcp->dsize))[1]; ((char *)&(destp->dsize ))[3] = ((char *)&(srcp->dsize))[0]; }; | |||
906 | P_32_COPY(srcp->ssize, destp->ssize){ ((char *)&(destp->ssize))[0] = ((char *)&(srcp-> ssize))[3]; ((char *)&(destp->ssize))[1] = ((char *)& (srcp->ssize))[2]; ((char *)&(destp->ssize))[2] = ( (char *)&(srcp->ssize))[1]; ((char *)&(destp->ssize ))[3] = ((char *)&(srcp->ssize))[0]; }; | |||
907 | P_32_COPY(srcp->sshift, destp->sshift){ ((char *)&(destp->sshift))[0] = ((char *)&(srcp-> sshift))[3]; ((char *)&(destp->sshift))[1] = ((char *) &(srcp->sshift))[2]; ((char *)&(destp->sshift)) [2] = ((char *)&(srcp->sshift))[1]; ((char *)&(destp ->sshift))[3] = ((char *)&(srcp->sshift))[0]; }; | |||
908 | P_32_COPY(srcp->ovfl_point, destp->ovfl_point){ ((char *)&(destp->ovfl_point))[0] = ((char *)&(srcp ->ovfl_point))[3]; ((char *)&(destp->ovfl_point))[1 ] = ((char *)&(srcp->ovfl_point))[2]; ((char *)&(destp ->ovfl_point))[2] = ((char *)&(srcp->ovfl_point))[1 ]; ((char *)&(destp->ovfl_point))[3] = ((char *)&( srcp->ovfl_point))[0]; }; | |||
909 | P_32_COPY(srcp->last_freed, destp->last_freed){ ((char *)&(destp->last_freed))[0] = ((char *)&(srcp ->last_freed))[3]; ((char *)&(destp->last_freed))[1 ] = ((char *)&(srcp->last_freed))[2]; ((char *)&(destp ->last_freed))[2] = ((char *)&(srcp->last_freed))[1 ]; ((char *)&(destp->last_freed))[3] = ((char *)&( srcp->last_freed))[0]; }; | |||
910 | P_32_COPY(srcp->max_bucket, destp->max_bucket){ ((char *)&(destp->max_bucket))[0] = ((char *)&(srcp ->max_bucket))[3]; ((char *)&(destp->max_bucket))[1 ] = ((char *)&(srcp->max_bucket))[2]; ((char *)&(destp ->max_bucket))[2] = ((char *)&(srcp->max_bucket))[1 ]; ((char *)&(destp->max_bucket))[3] = ((char *)&( srcp->max_bucket))[0]; }; | |||
911 | P_32_COPY(srcp->high_mask, destp->high_mask){ ((char *)&(destp->high_mask))[0] = ((char *)&(srcp ->high_mask))[3]; ((char *)&(destp->high_mask))[1] = ((char *)&(srcp->high_mask))[2]; ((char *)&(destp ->high_mask))[2] = ((char *)&(srcp->high_mask))[1]; ((char *)&(destp->high_mask))[3] = ((char *)&(srcp ->high_mask))[0]; }; | |||
912 | P_32_COPY(srcp->low_mask, destp->low_mask){ ((char *)&(destp->low_mask))[0] = ((char *)&(srcp ->low_mask))[3]; ((char *)&(destp->low_mask))[1] = ( (char *)&(srcp->low_mask))[2]; ((char *)&(destp-> low_mask))[2] = ((char *)&(srcp->low_mask))[1]; ((char *)&(destp->low_mask))[3] = ((char *)&(srcp->low_mask ))[0]; }; | |||
913 | P_32_COPY(srcp->ffactor, destp->ffactor){ ((char *)&(destp->ffactor))[0] = ((char *)&(srcp ->ffactor))[3]; ((char *)&(destp->ffactor))[1] = (( char *)&(srcp->ffactor))[2]; ((char *)&(destp-> ffactor))[2] = ((char *)&(srcp->ffactor))[1]; ((char * )&(destp->ffactor))[3] = ((char *)&(srcp->ffactor ))[0]; }; | |||
914 | P_32_COPY(srcp->nkeys, destp->nkeys){ ((char *)&(destp->nkeys))[0] = ((char *)&(srcp-> nkeys))[3]; ((char *)&(destp->nkeys))[1] = ((char *)& (srcp->nkeys))[2]; ((char *)&(destp->nkeys))[2] = ( (char *)&(srcp->nkeys))[1]; ((char *)&(destp->nkeys ))[3] = ((char *)&(srcp->nkeys))[0]; }; | |||
915 | P_32_COPY(srcp->hdrpages, destp->hdrpages){ ((char *)&(destp->hdrpages))[0] = ((char *)&(srcp ->hdrpages))[3]; ((char *)&(destp->hdrpages))[1] = ( (char *)&(srcp->hdrpages))[2]; ((char *)&(destp-> hdrpages))[2] = ((char *)&(srcp->hdrpages))[1]; ((char *)&(destp->hdrpages))[3] = ((char *)&(srcp->hdrpages ))[0]; }; | |||
916 | P_32_COPY(srcp->h_charkey, destp->h_charkey){ ((char *)&(destp->h_charkey))[0] = ((char *)&(srcp ->h_charkey))[3]; ((char *)&(destp->h_charkey))[1] = ((char *)&(srcp->h_charkey))[2]; ((char *)&(destp ->h_charkey))[2] = ((char *)&(srcp->h_charkey))[1]; ((char *)&(destp->h_charkey))[3] = ((char *)&(srcp ->h_charkey))[0]; }; | |||
917 | for (i = 0; i < NCACHED32; i++) { | |||
918 | P_32_COPY(srcp->spares[i], destp->spares[i]){ ((char *)&(destp->spares[i]))[0] = ((char *)&(srcp ->spares[i]))[3]; ((char *)&(destp->spares[i]))[1] = ((char *)&(srcp->spares[i]))[2]; ((char *)&(destp ->spares[i]))[2] = ((char *)&(srcp->spares[i]))[1]; ((char *)&(destp->spares[i]))[3] = ((char *)&(srcp ->spares[i]))[0]; }; | |||
919 | P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]){ ((char *)&(destp->bitmaps[i]))[0] = ((char *)&(srcp ->bitmaps[i]))[1]; ((char *)&(destp->bitmaps[i]))[1 ] = ((char *)&(srcp->bitmaps[i]))[0]; }; | |||
920 | } | |||
921 | } | |||
922 | ||||
923 | static void | |||
924 | swap_header(HTAB *hashp) | |||
925 | { | |||
926 | HASHHDR *hdrp; | |||
927 | int i; | |||
928 | ||||
929 | hdrp = &hashp->hdr; | |||
930 | ||||
931 | M_32_SWAP(hdrp->magic){ u_int32_t _tmp = hdrp->magic; ((char *)&hdrp->magic )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->magic)[ 1] = ((char *)&_tmp)[2]; ((char *)&hdrp->magic)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->magic)[3] = ((char *)&_tmp)[0]; }; | |||
932 | M_32_SWAP(hdrp->version){ u_int32_t _tmp = hdrp->version; ((char *)&hdrp->version )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->version )[1] = ((char *)&_tmp)[2]; ((char *)&hdrp->version )[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->version )[3] = ((char *)&_tmp)[0]; }; | |||
933 | M_32_SWAP(hdrp->lorder){ u_int32_t _tmp = hdrp->lorder; ((char *)&hdrp->lorder )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->lorder) [1] = ((char *)&_tmp)[2]; ((char *)&hdrp->lorder)[ 2] = ((char *)&_tmp)[1]; ((char *)&hdrp->lorder)[3 ] = ((char *)&_tmp)[0]; }; | |||
934 | M_32_SWAP(hdrp->bsize){ u_int32_t _tmp = hdrp->bsize; ((char *)&hdrp->bsize )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->bsize)[ 1] = ((char *)&_tmp)[2]; ((char *)&hdrp->bsize)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->bsize)[3] = ((char *)&_tmp)[0]; }; | |||
935 | M_32_SWAP(hdrp->bshift){ u_int32_t _tmp = hdrp->bshift; ((char *)&hdrp->bshift )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->bshift) [1] = ((char *)&_tmp)[2]; ((char *)&hdrp->bshift)[ 2] = ((char *)&_tmp)[1]; ((char *)&hdrp->bshift)[3 ] = ((char *)&_tmp)[0]; }; | |||
936 | M_32_SWAP(hdrp->dsize){ u_int32_t _tmp = hdrp->dsize; ((char *)&hdrp->dsize )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->dsize)[ 1] = ((char *)&_tmp)[2]; ((char *)&hdrp->dsize)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->dsize)[3] = ((char *)&_tmp)[0]; }; | |||
937 | M_32_SWAP(hdrp->ssize){ u_int32_t _tmp = hdrp->ssize; ((char *)&hdrp->ssize )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->ssize)[ 1] = ((char *)&_tmp)[2]; ((char *)&hdrp->ssize)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->ssize)[3] = ((char *)&_tmp)[0]; }; | |||
938 | M_32_SWAP(hdrp->sshift){ u_int32_t _tmp = hdrp->sshift; ((char *)&hdrp->sshift )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->sshift) [1] = ((char *)&_tmp)[2]; ((char *)&hdrp->sshift)[ 2] = ((char *)&_tmp)[1]; ((char *)&hdrp->sshift)[3 ] = ((char *)&_tmp)[0]; }; | |||
939 | M_32_SWAP(hdrp->ovfl_point){ u_int32_t _tmp = hdrp->ovfl_point; ((char *)&hdrp-> ovfl_point)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> ovfl_point)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> ovfl_point)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> ovfl_point)[3] = ((char *)&_tmp)[0]; }; | |||
940 | M_32_SWAP(hdrp->last_freed){ u_int32_t _tmp = hdrp->last_freed; ((char *)&hdrp-> last_freed)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> last_freed)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> last_freed)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> last_freed)[3] = ((char *)&_tmp)[0]; }; | |||
941 | M_32_SWAP(hdrp->max_bucket){ u_int32_t _tmp = hdrp->max_bucket; ((char *)&hdrp-> max_bucket)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> max_bucket)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> max_bucket)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> max_bucket)[3] = ((char *)&_tmp)[0]; }; | |||
942 | M_32_SWAP(hdrp->high_mask){ u_int32_t _tmp = hdrp->high_mask; ((char *)&hdrp-> high_mask)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> high_mask)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> high_mask)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> high_mask)[3] = ((char *)&_tmp)[0]; }; | |||
943 | M_32_SWAP(hdrp->low_mask){ u_int32_t _tmp = hdrp->low_mask; ((char *)&hdrp-> low_mask)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> low_mask)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> low_mask)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> low_mask)[3] = ((char *)&_tmp)[0]; }; | |||
944 | M_32_SWAP(hdrp->ffactor){ u_int32_t _tmp = hdrp->ffactor; ((char *)&hdrp->ffactor )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->ffactor )[1] = ((char *)&_tmp)[2]; ((char *)&hdrp->ffactor )[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->ffactor )[3] = ((char *)&_tmp)[0]; }; | |||
945 | M_32_SWAP(hdrp->nkeys){ u_int32_t _tmp = hdrp->nkeys; ((char *)&hdrp->nkeys )[0] = ((char *)&_tmp)[3]; ((char *)&hdrp->nkeys)[ 1] = ((char *)&_tmp)[2]; ((char *)&hdrp->nkeys)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp->nkeys)[3] = ((char *)&_tmp)[0]; }; | |||
946 | M_32_SWAP(hdrp->hdrpages){ u_int32_t _tmp = hdrp->hdrpages; ((char *)&hdrp-> hdrpages)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> hdrpages)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> hdrpages)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> hdrpages)[3] = ((char *)&_tmp)[0]; }; | |||
947 | M_32_SWAP(hdrp->h_charkey){ u_int32_t _tmp = hdrp->h_charkey; ((char *)&hdrp-> h_charkey)[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> h_charkey)[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> h_charkey)[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> h_charkey)[3] = ((char *)&_tmp)[0]; }; | |||
948 | for (i = 0; i < NCACHED32; i++) { | |||
949 | M_32_SWAP(hdrp->spares[i]){ u_int32_t _tmp = hdrp->spares[i]; ((char *)&hdrp-> spares[i])[0] = ((char *)&_tmp)[3]; ((char *)&hdrp-> spares[i])[1] = ((char *)&_tmp)[2]; ((char *)&hdrp-> spares[i])[2] = ((char *)&_tmp)[1]; ((char *)&hdrp-> spares[i])[3] = ((char *)&_tmp)[0]; }; | |||
950 | M_16_SWAP(hdrp->bitmaps[i]){ u_int16_t _tmp = hdrp->bitmaps[i]; ((char *)&hdrp-> bitmaps[i])[0] = ((char *)&_tmp)[1]; ((char *)&hdrp-> bitmaps[i])[1] = ((char *)&_tmp)[0]; }; | |||
951 | } | |||
952 | } | |||
953 | #endif |