Bug 469739 - Add support for displaying Vista UAC shield icon; r=joe sr=vladimir
[wine-gecko.git] / dbm / src / hash.c
blobd4c0e073912cfaa95d0a77c7ee844426fa327491
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. ***REMOVED*** - see
17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
18 * 4. 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.
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.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
37 #endif /* LIBC_SCCS and not lint */
39 #include "watcomfx.h"
41 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
42 #include <sys/param.h>
43 #endif
45 #if !defined(macintosh)
46 #ifdef XP_OS2_EMX
47 #include <sys/types.h>
48 #endif
49 #include <sys/stat.h>
50 #endif
52 #if defined(macintosh)
53 #include <unix.h>
54 #include <unistd.h>
55 #endif
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
63 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
64 #include <unistd.h>
65 #endif
66 #if defined(_WIN32) || defined(_WINDOWS)
67 #include <windows.h>
68 #endif
70 #include <assert.h>
72 #include "mcom_db.h"
73 #include "hash.h"
74 #include "page.h"
77 #include "extern.h"
79 static int alloc_segs __P((HTAB *, int));
80 static int flush_meta __P((HTAB *));
81 static int hash_access __P((HTAB *, ACTION, DBT *, DBT *));
82 static int hash_close __P((DB *));
83 static int hash_delete __P((const DB *, const DBT *, uint));
84 static int hash_fd __P((const DB *));
85 static int hash_get __P((const DB *, const DBT *, DBT *, uint));
86 static int hash_put __P((const DB *, DBT *, const DBT *, uint));
87 static void *hash_realloc __P((SEGMENT **, size_t, size_t));
88 static int hash_seq __P((const DB *, DBT *, DBT *, uint));
89 static int hash_sync __P((const DB *, uint));
90 static int hdestroy __P((HTAB *));
91 static HTAB *init_hash __P((HTAB *, const char *, HASHINFO *));
92 static int init_htab __P((HTAB *, int));
93 #if BYTE_ORDER == LITTLE_ENDIAN
94 static void swap_header __P((HTAB *));
95 static void swap_header_copy __P((HASHHDR *, HASHHDR *));
96 #endif
98 /* Fast arithmetic, relying on powers of 2, */
99 #define MOD(x, y) ((x) & ((y) - 1))
101 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
103 /* Return values */
104 #define SUCCESS (0)
105 #define DBM_ERROR (-1)
106 #define ABNORMAL (1)
108 #ifdef HASH_STATISTICS
109 int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
110 #endif
112 /* A new Lou (montulli@mozilla.com) routine.
114 * The database is screwed.
116 * This closes the file, flushing buffers as appropriate.
118 static void
119 __remove_database(DB *dbp)
121 HTAB *hashp = (HTAB *)dbp->internal;
123 assert(0);
125 if (!hashp)
126 return;
127 hdestroy(hashp);
128 dbp->internal = NULL;
131 /************************** INTERFACE ROUTINES ***************************/
132 /* OPEN/CLOSE */
135 extern DB *
136 __hash_open(const char *file, int flags, int mode, const HASHINFO *info, int dflags)
138 HTAB *hashp=NULL;
139 struct stat statbuf;
140 DB *dbp;
141 int bpages, hdrsize, new_table, nsegs, save_errno;
143 if ((flags & O_ACCMODE) == O_WRONLY) {
144 errno = EINVAL;
145 return NULL;
148 /* zero the statbuffer so that
149 * we can check it for a non-zero
150 * date to see if stat succeeded
152 memset(&statbuf, 0, sizeof(struct stat));
154 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB)))) {
155 errno = ENOMEM;
156 return NULL;
158 hashp->fp = NO_FILE;
159 if(file)
160 hashp->filename = strdup(file);
163 * Even if user wants write only, we need to be able to read
164 * the actual file, so we need to open it read/write. But, the
165 * field in the hashp structure needs to be accurate so that
166 * we can check accesses.
168 hashp->flags = flags;
170 new_table = 0;
171 if (!file || (flags & O_TRUNC) || (stat(file, &statbuf) && (errno == ENOENT)))
173 if (errno == ENOENT)
174 errno = 0; /* Just in case someone looks at errno */
175 new_table = 1;
177 else if(statbuf.st_mtime && statbuf.st_size == 0)
179 /* check for a zero length file and delete it
180 * if it exists
182 new_table = 1;
184 hashp->file_size = statbuf.st_size;
186 if (file) {
187 #if defined(_WIN32) || defined(_WINDOWS) || defined (macintosh) || defined(XP_OS2)
188 if ((hashp->fp = DBFILE_OPEN(file, flags | O_BINARY, mode)) == -1)
189 RETURN_ERROR(errno, error1);
190 #else
191 if ((hashp->fp = open(file, flags, mode)) == -1)
192 RETURN_ERROR(errno, error1);
193 (void)fcntl(hashp->fp, F_SETFD, 1);
194 #endif
196 if (new_table) {
197 if (!init_hash(hashp, file, (HASHINFO *)info))
198 RETURN_ERROR(errno, error1);
199 } else {
200 /* Table already exists */
201 if (info && info->hash)
202 hashp->hash = info->hash;
203 else
204 hashp->hash = __default_hash;
206 hdrsize = read(hashp->fp, (char *)&hashp->hdr, sizeof(HASHHDR));
207 if (hdrsize == -1)
208 RETURN_ERROR(errno, error1);
209 if (hdrsize != sizeof(HASHHDR))
210 RETURN_ERROR(EFTYPE, error1);
211 #if BYTE_ORDER == LITTLE_ENDIAN
212 swap_header(hashp);
213 #endif
214 /* Verify file type, versions and hash function */
215 if (hashp->MAGIC != HASHMAGIC)
216 RETURN_ERROR(EFTYPE, error1);
217 #define OLDHASHVERSION 1
218 if (hashp->VERSION != HASHVERSION &&
219 hashp->VERSION != OLDHASHVERSION)
220 RETURN_ERROR(EFTYPE, error1);
221 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
222 RETURN_ERROR(EFTYPE, error1);
223 if (hashp->NKEYS < 0) /* Old bad database. */
224 RETURN_ERROR(EFTYPE, error1);
227 * Figure out how many segments we need. Max_Bucket is the
228 * maximum bucket number, so the number of buckets is
229 * max_bucket + 1.
231 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
232 hashp->SGSIZE;
233 hashp->nsegs = 0;
234 if (alloc_segs(hashp, nsegs))
235 /* If alloc_segs fails, errno will have been set. */
236 RETURN_ERROR(errno, error1);
237 /* Read in bitmaps */
238 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
239 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
240 (hashp->BSHIFT + BYTE_SHIFT);
242 hashp->nmaps = bpages;
243 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32 *));
246 /* Initialize Buffer Manager */
247 if (info && info->cachesize)
248 __buf_init(hashp, (int32) info->cachesize);
249 else
250 __buf_init(hashp, DEF_BUFSIZE);
252 hashp->new_file = new_table;
253 #ifdef macintosh
254 hashp->save_file = file && !(hashp->flags & O_RDONLY);
255 #else
256 hashp->save_file = file && (hashp->flags & O_RDWR);
257 #endif
258 hashp->cbucket = -1;
259 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
260 RETURN_ERROR(ENOMEM, error1);
262 dbp->internal = hashp;
263 dbp->close = hash_close;
264 dbp->del = hash_delete;
265 dbp->fd = hash_fd;
266 dbp->get = hash_get;
267 dbp->put = hash_put;
268 dbp->seq = hash_seq;
269 dbp->sync = hash_sync;
270 dbp->type = DB_HASH;
272 #ifdef HASH_STATISTICS
273 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
274 #endif
275 return (dbp);
277 error1:
278 hdestroy(hashp);
279 errno = save_errno;
280 return (NULL);
283 static int
284 hash_close(DB *dbp)
286 HTAB *hashp;
287 int retval;
289 if (!dbp)
290 return (DBM_ERROR);
292 hashp = (HTAB *)dbp->internal;
293 if(!hashp)
294 return (DBM_ERROR);
296 retval = hdestroy(hashp);
297 free(dbp);
298 return (retval);
301 static int hash_fd(const DB *dbp)
303 HTAB *hashp;
305 if (!dbp)
306 return (DBM_ERROR);
308 hashp = (HTAB *)dbp->internal;
309 if(!hashp)
310 return (DBM_ERROR);
312 if (hashp->fp == -1) {
313 errno = ENOENT;
314 return (-1);
316 return (hashp->fp);
319 /************************** LOCAL CREATION ROUTINES **********************/
320 static HTAB *
321 init_hash(HTAB *hashp, const char *file, HASHINFO *info)
323 struct stat statbuf;
324 int nelem;
326 nelem = 1;
327 hashp->NKEYS = 0;
328 hashp->LORDER = BYTE_ORDER;
329 hashp->BSIZE = DEF_BUCKET_SIZE;
330 hashp->BSHIFT = DEF_BUCKET_SHIFT;
331 hashp->SGSIZE = DEF_SEGSIZE;
332 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
333 hashp->DSIZE = DEF_DIRSIZE;
334 hashp->FFACTOR = DEF_FFACTOR;
335 hashp->hash = __default_hash;
336 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
337 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
339 /* Fix bucket size to be optimal for file system */
340 if (file != NULL) {
341 if (stat(file, &statbuf))
342 return (NULL);
344 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(VMS) && !defined(XP_OS2)
345 #if defined(__QNX__) && !defined(__QNXNTO__)
346 hashp->BSIZE = 512; /* preferred blk size on qnx4 */
347 #else
348 hashp->BSIZE = statbuf.st_blksize;
349 #endif
351 /* new code added by Lou to reduce block
352 * size down below MAX_BSIZE
354 if (hashp->BSIZE > MAX_BSIZE)
355 hashp->BSIZE = MAX_BSIZE;
356 #endif
357 hashp->BSHIFT = __log2((uint32)hashp->BSIZE);
360 if (info) {
361 if (info->bsize) {
362 /* Round pagesize up to power of 2 */
363 hashp->BSHIFT = __log2(info->bsize);
364 hashp->BSIZE = 1 << hashp->BSHIFT;
365 if (hashp->BSIZE > MAX_BSIZE) {
366 errno = EINVAL;
367 return (NULL);
370 if (info->ffactor)
371 hashp->FFACTOR = info->ffactor;
372 if (info->hash)
373 hashp->hash = info->hash;
374 if (info->nelem)
375 nelem = info->nelem;
376 if (info->lorder) {
377 if (info->lorder != BIG_ENDIAN &&
378 info->lorder != LITTLE_ENDIAN) {
379 errno = EINVAL;
380 return (NULL);
382 hashp->LORDER = info->lorder;
385 /* init_htab sets errno if it fails */
386 if (init_htab(hashp, nelem))
387 return (NULL);
388 else
389 return (hashp);
392 * This calls alloc_segs which may run out of memory. Alloc_segs will
393 * set errno, so we just pass the error information along.
395 * Returns 0 on No Error
397 static int
398 init_htab(HTAB *hashp, int nelem)
400 register int nbuckets, nsegs;
401 int l2;
404 * Divide number of elements by the fill factor and determine a
405 * desired number of buckets. Allocate space for the next greater
406 * power of two number of buckets.
408 nelem = (nelem - 1) / hashp->FFACTOR + 1;
410 l2 = __log2((uint32)PR_MAX(nelem, 2));
411 nbuckets = 1 << l2;
413 hashp->SPARES[l2] = l2 + 1;
414 hashp->SPARES[l2 + 1] = l2 + 1;
415 hashp->OVFL_POINT = l2;
416 hashp->LAST_FREED = 2;
418 /* First bitmap page is at: splitpoint l2 page offset 1 */
419 if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0))
420 return (-1);
422 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
423 hashp->HIGH_MASK = (nbuckets << 1) - 1;
424 hashp->HDRPAGES = ((PR_MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
425 hashp->BSHIFT) + 1;
427 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
428 nsegs = 1 << __log2((uint32)nsegs);
430 if (nsegs > hashp->DSIZE)
431 hashp->DSIZE = nsegs;
432 return (alloc_segs(hashp, nsegs));
435 /********************** DESTROY/CLOSE ROUTINES ************************/
438 * Flushes any changes to the file if necessary and destroys the hashp
439 * structure, freeing all allocated space.
441 static int
442 hdestroy(HTAB *hashp)
444 int i, save_errno;
446 save_errno = 0;
448 #ifdef HASH_STATISTICS
449 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
450 hash_accesses, hash_collisions);
451 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
452 hash_expansions);
453 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
454 hash_overflows);
455 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
456 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
458 for (i = 0; i < NCACHED; i++)
459 (void)fprintf(stderr,
460 "spares[%d] = %d\n", i, hashp->SPARES[i]);
461 #endif
463 * Call on buffer manager to free buffers, and if required,
464 * write them to disk.
466 if (__buf_free(hashp, 1, hashp->save_file))
467 save_errno = errno;
468 if (hashp->dir) {
469 free(*hashp->dir); /* Free initial segments */
470 /* Free extra segments */
471 while (hashp->exsegs--)
472 free(hashp->dir[--hashp->nsegs]);
473 free(hashp->dir);
475 if (flush_meta(hashp) && !save_errno)
476 save_errno = errno;
477 /* Free Bigmaps */
478 for (i = 0; i < hashp->nmaps; i++)
479 if (hashp->mapp[i])
480 free(hashp->mapp[i]);
482 if (hashp->fp != -1)
483 (void)close(hashp->fp);
485 if(hashp->filename) {
486 #if defined(_WIN32) || defined(_WINDOWS) || defined(XP_OS2)
487 if (hashp->is_temp)
488 (void)unlink(hashp->filename);
489 #endif
490 free(hashp->filename);
492 if (hashp->tmp_buf)
493 free(hashp->tmp_buf);
494 if (hashp->tmp_key)
495 free(hashp->tmp_key);
496 free(hashp);
497 if (save_errno) {
498 errno = save_errno;
499 return (DBM_ERROR);
501 return (SUCCESS);
504 #if defined(_WIN32) || defined(_WINDOWS)
506 * Close and reopen file to force file length update on windows.
508 * Returns:
509 * 0 == OK
510 * -1 DBM_ERROR
512 static int
513 update_EOF(HTAB *hashp)
515 #if defined(DBM_REOPEN_ON_FLUSH)
516 char * file = hashp->filename;
517 off_t file_size;
518 int flags;
519 int mode = -1;
520 struct stat statbuf;
522 memset(&statbuf, 0, sizeof statbuf);
524 /* make sure we won't lose the file by closing it. */
525 if (!file || (stat(file, &statbuf) && (errno == ENOENT))) {
526 /* pretend we did it. */
527 return 0;
530 (void)close(hashp->fp);
532 flags = hashp->flags & ~(O_TRUNC | O_CREAT | O_EXCL);
534 if ((hashp->fp = DBFILE_OPEN(file, flags | O_BINARY, mode)) == -1)
535 return -1;
536 file_size = lseek(hashp->fp, (off_t)0, SEEK_END);
537 if (file_size == -1)
538 return -1;
539 hashp->file_size = file_size;
540 return 0;
541 #else
542 int fd = hashp->fp;
543 off_t file_size = lseek(fd, (off_t)0, SEEK_END);
544 HANDLE handle = (HANDLE)_get_osfhandle(fd);
545 BOOL cool = FlushFileBuffers(handle);
546 #ifdef DEBUG3
547 if (!cool) {
548 DWORD err = GetLastError();
549 (void)fprintf(stderr,
550 "FlushFileBuffers failed, last error = %d, 0x%08x\n",
551 err, err);
553 #endif
554 if (file_size == -1)
555 return -1;
556 hashp->file_size = file_size;
557 return cool ? 0 : -1;
558 #endif
560 #endif
563 * Write modified pages to disk
565 * Returns:
566 * 0 == OK
567 * -1 DBM_ERROR
569 static int
570 hash_sync(const DB *dbp, uint flags)
572 HTAB *hashp;
574 if (flags != 0) {
575 errno = EINVAL;
576 return (DBM_ERROR);
579 if (!dbp)
580 return (DBM_ERROR);
582 hashp = (HTAB *)dbp->internal;
583 if(!hashp)
584 return (DBM_ERROR);
586 if (!hashp->save_file)
587 return (0);
588 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
589 return (DBM_ERROR);
590 #if defined(_WIN32) || defined(_WINDOWS)
591 if (hashp->updateEOF && hashp->filename && !hashp->is_temp) {
592 int status = update_EOF(hashp);
593 hashp->updateEOF = 0;
594 if (status)
595 return status;
597 #endif
598 hashp->new_file = 0;
599 return (0);
603 * Returns:
604 * 0 == OK
605 * -1 indicates that errno should be set
607 static int
608 flush_meta(HTAB *hashp)
610 HASHHDR *whdrp;
611 #if BYTE_ORDER == LITTLE_ENDIAN
612 HASHHDR whdr;
613 #endif
614 int fp, i, wsize;
616 if (!hashp->save_file)
617 return (0);
618 hashp->MAGIC = HASHMAGIC;
619 hashp->VERSION = HASHVERSION;
620 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
622 fp = hashp->fp;
623 whdrp = &hashp->hdr;
624 #if BYTE_ORDER == LITTLE_ENDIAN
625 whdrp = &whdr;
626 swap_header_copy(&hashp->hdr, whdrp);
627 #endif
628 if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
629 ((wsize = write(fp, (char*)whdrp, sizeof(HASHHDR))) == -1))
630 return (-1);
631 else
632 if (wsize != sizeof(HASHHDR)) {
633 errno = EFTYPE;
634 hashp->dbmerrno = errno;
635 return (-1);
637 for (i = 0; i < NCACHED; i++)
638 if (hashp->mapp[i])
639 if (__put_page(hashp, (char *)hashp->mapp[i],
640 hashp->BITMAPS[i], 0, 1))
641 return (-1);
642 return (0);
645 /*******************************SEARCH ROUTINES *****************************/
647 * All the access routines return
649 * Returns:
650 * 0 on SUCCESS
651 * 1 to indicate an external DBM_ERROR (i.e. key not found, etc)
652 * -1 to indicate an internal DBM_ERROR (i.e. out of memory, etc)
654 static int
655 hash_get(
656 const DB *dbp,
657 const DBT *key,
658 DBT *data,
659 uint flag)
661 HTAB *hashp;
662 int rv;
664 hashp = (HTAB *)dbp->internal;
665 if (!hashp)
666 return (DBM_ERROR);
668 if (flag) {
669 hashp->dbmerrno = errno = EINVAL;
670 return (DBM_ERROR);
673 rv = hash_access(hashp, HASH_GET, (DBT *)key, data);
675 if(rv == DATABASE_CORRUPTED_ERROR)
677 #if defined(unix) && defined(DEBUG)
678 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
679 #endif
680 __remove_database((DB *)dbp);
683 return(rv);
686 static int
687 hash_put(
688 const DB *dbp,
689 DBT *key,
690 const DBT *data,
691 uint flag)
693 HTAB *hashp;
694 int rv;
696 hashp = (HTAB *)dbp->internal;
697 if (!hashp)
698 return (DBM_ERROR);
700 if (flag && flag != R_NOOVERWRITE) {
701 hashp->dbmerrno = errno = EINVAL;
702 return (DBM_ERROR);
704 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
705 hashp->dbmerrno = errno = EPERM;
706 return (DBM_ERROR);
709 rv = hash_access(hashp, flag == R_NOOVERWRITE ?
710 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data);
712 if(rv == DATABASE_CORRUPTED_ERROR)
714 #if defined(unix) && defined(DEBUG)
715 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
716 #endif
717 __remove_database((DB *)dbp);
720 return(rv);
723 static int
724 hash_delete(
725 const DB *dbp,
726 const DBT *key,
727 uint flag) /* Ignored */
729 HTAB *hashp;
730 int rv;
732 hashp = (HTAB *)dbp->internal;
733 if (!hashp)
734 return (DBM_ERROR);
736 if (flag && flag != R_CURSOR) {
737 hashp->dbmerrno = errno = EINVAL;
738 return (DBM_ERROR);
740 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
741 hashp->dbmerrno = errno = EPERM;
742 return (DBM_ERROR);
744 rv = hash_access(hashp, HASH_DELETE, (DBT *)key, NULL);
746 if(rv == DATABASE_CORRUPTED_ERROR)
748 #if defined(unix) && defined(DEBUG)
749 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
750 #endif
751 __remove_database((DB *)dbp);
754 return(rv);
757 #define MAX_OVERFLOW_HASH_ACCESS_LOOPS 2000
759 * Assume that hashp has been set in wrapper routine.
761 static int
762 hash_access(
763 HTAB *hashp,
764 ACTION action,
765 DBT *key, DBT *val)
767 register BUFHEAD *rbufp;
768 BUFHEAD *bufp, *save_bufp;
769 register uint16 *bp;
770 register long n, ndx, off;
771 register size_t size;
772 register char *kp;
773 uint16 pageno;
774 uint32 ovfl_loop_count=0;
775 int32 last_overflow_page_no = -1;
777 #ifdef HASH_STATISTICS
778 hash_accesses++;
779 #endif
781 off = hashp->BSIZE;
782 size = key->size;
783 kp = (char *)key->data;
784 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
785 if (!rbufp)
786 return (DATABASE_CORRUPTED_ERROR);
787 save_bufp = rbufp;
789 /* Pin the bucket chain */
790 rbufp->flags |= BUF_PIN;
791 for (bp = (uint16 *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
794 if (bp[1] >= REAL_KEY) {
795 /* Real key/data pair */
796 if (size == (unsigned long)(off - *bp) &&
797 memcmp(kp, rbufp->page + *bp, size) == 0)
798 goto found;
799 off = bp[1];
800 #ifdef HASH_STATISTICS
801 hash_collisions++;
802 #endif
803 bp += 2;
804 ndx += 2;
805 } else if (bp[1] == OVFLPAGE) {
807 /* database corruption: overflow loop detection */
808 if(last_overflow_page_no == (int32)*bp)
809 return (DATABASE_CORRUPTED_ERROR);
811 last_overflow_page_no = *bp;
813 rbufp = __get_buf(hashp, *bp, rbufp, 0);
814 if (!rbufp) {
815 save_bufp->flags &= ~BUF_PIN;
816 return (DBM_ERROR);
819 ovfl_loop_count++;
820 if(ovfl_loop_count > MAX_OVERFLOW_HASH_ACCESS_LOOPS)
821 return (DATABASE_CORRUPTED_ERROR);
823 /* FOR LOOP INIT */
824 bp = (uint16 *)rbufp->page;
825 n = *bp++;
826 ndx = 1;
827 off = hashp->BSIZE;
828 } else if (bp[1] < REAL_KEY) {
829 if ((ndx =
830 __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
831 goto found;
832 if (ndx == -2) {
833 bufp = rbufp;
834 if (!(pageno =
835 __find_last_page(hashp, &bufp))) {
836 ndx = 0;
837 rbufp = bufp;
838 break; /* FOR */
840 rbufp = __get_buf(hashp, pageno, bufp, 0);
841 if (!rbufp) {
842 save_bufp->flags &= ~BUF_PIN;
843 return (DBM_ERROR);
845 /* FOR LOOP INIT */
846 bp = (uint16 *)rbufp->page;
847 n = *bp++;
848 ndx = 1;
849 off = hashp->BSIZE;
850 } else {
851 save_bufp->flags &= ~BUF_PIN;
852 return (DBM_ERROR);
858 /* Not found */
859 switch (action) {
860 case HASH_PUT:
861 case HASH_PUTNEW:
862 if (__addel(hashp, rbufp, key, val)) {
863 save_bufp->flags &= ~BUF_PIN;
864 return (DBM_ERROR);
865 } else {
866 save_bufp->flags &= ~BUF_PIN;
867 return (SUCCESS);
869 case HASH_GET:
870 case HASH_DELETE:
871 default:
872 save_bufp->flags &= ~BUF_PIN;
873 return (ABNORMAL);
876 found:
877 switch (action) {
878 case HASH_PUTNEW:
879 save_bufp->flags &= ~BUF_PIN;
880 return (ABNORMAL);
881 case HASH_GET:
882 bp = (uint16 *)rbufp->page;
883 if (bp[ndx + 1] < REAL_KEY) {
884 if (__big_return(hashp, rbufp, ndx, val, 0))
885 return (DBM_ERROR);
886 } else {
887 val->data = (uint8 *)rbufp->page + (int)bp[ndx + 1];
888 val->size = bp[ndx] - bp[ndx + 1];
890 break;
891 case HASH_PUT:
892 if ((__delpair(hashp, rbufp, ndx)) ||
893 (__addel(hashp, rbufp, key, val))) {
894 save_bufp->flags &= ~BUF_PIN;
895 return (DBM_ERROR);
897 break;
898 case HASH_DELETE:
899 if (__delpair(hashp, rbufp, ndx))
900 return (DBM_ERROR);
901 break;
902 default:
903 abort();
905 save_bufp->flags &= ~BUF_PIN;
906 return (SUCCESS);
909 static int
910 hash_seq(
911 const DB *dbp,
912 DBT *key, DBT *data,
913 uint flag)
915 register uint32 bucket;
916 register BUFHEAD *bufp;
917 HTAB *hashp;
918 uint16 *bp, ndx;
920 hashp = (HTAB *)dbp->internal;
921 if (!hashp)
922 return (DBM_ERROR);
924 if (flag && flag != R_FIRST && flag != R_NEXT) {
925 hashp->dbmerrno = errno = EINVAL;
926 return (DBM_ERROR);
928 #ifdef HASH_STATISTICS
929 hash_accesses++;
930 #endif
931 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
932 hashp->cbucket = 0;
933 hashp->cndx = 1;
934 hashp->cpage = NULL;
937 for (bp = NULL; !bp || !bp[0]; ) {
938 if (!(bufp = hashp->cpage)) {
939 for (bucket = hashp->cbucket;
940 bucket <= (uint32)hashp->MAX_BUCKET;
941 bucket++, hashp->cndx = 1) {
942 bufp = __get_buf(hashp, bucket, NULL, 0);
943 if (!bufp)
944 return (DBM_ERROR);
945 hashp->cpage = bufp;
946 bp = (uint16 *)bufp->page;
947 if (bp[0])
948 break;
950 hashp->cbucket = bucket;
951 if (hashp->cbucket > hashp->MAX_BUCKET) {
952 hashp->cbucket = -1;
953 return (ABNORMAL);
955 } else
956 bp = (uint16 *)hashp->cpage->page;
958 #ifdef DEBUG
959 assert(bp);
960 assert(bufp);
961 #endif
962 while (bp[hashp->cndx + 1] == OVFLPAGE) {
963 bufp = hashp->cpage =
964 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
965 if (!bufp)
966 return (DBM_ERROR);
967 bp = (uint16 *)(bufp->page);
968 hashp->cndx = 1;
970 if (!bp[0]) {
971 hashp->cpage = NULL;
972 ++hashp->cbucket;
975 ndx = hashp->cndx;
976 if (bp[ndx + 1] < REAL_KEY) {
977 if (__big_keydata(hashp, bufp, key, data, 1))
978 return (DBM_ERROR);
979 } else {
980 key->data = (uint8 *)hashp->cpage->page + bp[ndx];
981 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
982 data->data = (uint8 *)hashp->cpage->page + bp[ndx + 1];
983 data->size = bp[ndx] - bp[ndx + 1];
984 ndx += 2;
985 if (ndx > bp[0]) {
986 hashp->cpage = NULL;
987 hashp->cbucket++;
988 hashp->cndx = 1;
989 } else
990 hashp->cndx = ndx;
992 return (SUCCESS);
995 /********************************* UTILITIES ************************/
998 * Returns:
999 * 0 ==> OK
1000 * -1 ==> Error
1002 extern int
1003 __expand_table(HTAB *hashp)
1005 uint32 old_bucket, new_bucket;
1006 int new_segnum, spare_ndx;
1007 size_t dirsize;
1009 #ifdef HASH_STATISTICS
1010 hash_expansions++;
1011 #endif
1012 new_bucket = ++hashp->MAX_BUCKET;
1013 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
1015 new_segnum = new_bucket >> hashp->SSHIFT;
1017 /* Check if we need a new segment */
1018 if (new_segnum >= hashp->nsegs) {
1019 /* Check if we need to expand directory */
1020 if (new_segnum >= hashp->DSIZE) {
1021 /* Reallocate directory */
1022 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
1023 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
1024 return (-1);
1025 hashp->DSIZE = dirsize << 1;
1027 if ((hashp->dir[new_segnum] =
1028 (SEGMENT)calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
1029 return (-1);
1030 hashp->exsegs++;
1031 hashp->nsegs++;
1034 * If the split point is increasing (MAX_BUCKET's log base 2
1035 * * increases), we need to copy the current contents of the spare
1036 * split bucket to the next bucket.
1038 spare_ndx = __log2((uint32)(hashp->MAX_BUCKET + 1));
1039 if (spare_ndx > hashp->OVFL_POINT) {
1040 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
1041 hashp->OVFL_POINT = spare_ndx;
1044 if (new_bucket > (uint32)hashp->HIGH_MASK) {
1045 /* Starting a new doubling */
1046 hashp->LOW_MASK = hashp->HIGH_MASK;
1047 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
1049 /* Relocate records to the new bucket */
1050 return (__split_page(hashp, old_bucket, new_bucket));
1054 * If realloc guarantees that the pointer is not destroyed if the realloc
1055 * fails, then this routine can go away.
1057 static void *
1058 hash_realloc(
1059 SEGMENT **p_ptr,
1060 size_t oldsize, size_t newsize)
1062 register void *p;
1064 if ((p = malloc(newsize))) {
1065 memmove(p, *p_ptr, oldsize);
1066 memset((char *)p + oldsize, 0, newsize - oldsize);
1067 free(*p_ptr);
1068 *p_ptr = (SEGMENT *)p;
1070 return (p);
1073 extern uint32
1074 __call_hash(HTAB *hashp, char *k, size_t len)
1076 uint32 n, bucket;
1078 n = hashp->hash(k, len);
1079 bucket = n & hashp->HIGH_MASK;
1080 if (bucket > (uint32)hashp->MAX_BUCKET)
1081 bucket = bucket & hashp->LOW_MASK;
1082 return (bucket);
1086 * Allocate segment table. On error, set errno.
1088 * Returns 0 on success
1090 static int
1091 alloc_segs(
1092 HTAB *hashp,
1093 int nsegs)
1095 register int i;
1096 register SEGMENT store;
1098 if ((hashp->dir =
1099 (SEGMENT *)calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
1100 errno = ENOMEM;
1101 return (-1);
1103 /* Allocate segments */
1104 if ((store =
1105 (SEGMENT)calloc((size_t)nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
1106 errno = ENOMEM;
1107 return (-1);
1109 for (i = 0; i < nsegs; i++, hashp->nsegs++)
1110 hashp->dir[i] = &store[i << hashp->SSHIFT];
1111 return (0);
1114 #if BYTE_ORDER == LITTLE_ENDIAN
1116 * Hashp->hdr needs to be byteswapped.
1118 static void
1119 swap_header_copy(
1120 HASHHDR *srcp, HASHHDR *destp)
1122 int i;
1124 P_32_COPY(srcp->magic, destp->magic);
1125 P_32_COPY(srcp->version, destp->version);
1126 P_32_COPY(srcp->lorder, destp->lorder);
1127 P_32_COPY(srcp->bsize, destp->bsize);
1128 P_32_COPY(srcp->bshift, destp->bshift);
1129 P_32_COPY(srcp->dsize, destp->dsize);
1130 P_32_COPY(srcp->ssize, destp->ssize);
1131 P_32_COPY(srcp->sshift, destp->sshift);
1132 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
1133 P_32_COPY(srcp->last_freed, destp->last_freed);
1134 P_32_COPY(srcp->max_bucket, destp->max_bucket);
1135 P_32_COPY(srcp->high_mask, destp->high_mask);
1136 P_32_COPY(srcp->low_mask, destp->low_mask);
1137 P_32_COPY(srcp->ffactor, destp->ffactor);
1138 P_32_COPY(srcp->nkeys, destp->nkeys);
1139 P_32_COPY(srcp->hdrpages, destp->hdrpages);
1140 P_32_COPY(srcp->h_charkey, destp->h_charkey);
1141 for (i = 0; i < NCACHED; i++) {
1142 P_32_COPY(srcp->spares[i], destp->spares[i]);
1143 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
1147 static void
1148 swap_header(HTAB *hashp)
1150 HASHHDR *hdrp;
1151 int i;
1153 hdrp = &hashp->hdr;
1155 M_32_SWAP(hdrp->magic);
1156 M_32_SWAP(hdrp->version);
1157 M_32_SWAP(hdrp->lorder);
1158 M_32_SWAP(hdrp->bsize);
1159 M_32_SWAP(hdrp->bshift);
1160 M_32_SWAP(hdrp->dsize);
1161 M_32_SWAP(hdrp->ssize);
1162 M_32_SWAP(hdrp->sshift);
1163 M_32_SWAP(hdrp->ovfl_point);
1164 M_32_SWAP(hdrp->last_freed);
1165 M_32_SWAP(hdrp->max_bucket);
1166 M_32_SWAP(hdrp->high_mask);
1167 M_32_SWAP(hdrp->low_mask);
1168 M_32_SWAP(hdrp->ffactor);
1169 M_32_SWAP(hdrp->nkeys);
1170 M_32_SWAP(hdrp->hdrpages);
1171 M_32_SWAP(hdrp->h_charkey);
1172 for (i = 0; i < NCACHED; i++) {
1173 M_32_SWAP(hdrp->spares[i]);
1174 M_16_SWAP(hdrp->bitmaps[i]);
1177 #endif