Loading...
--- Libc/Libc-262.3.2/db/hash/hash.c
+++ Libc/Libc-320/db/hash/hash.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
@@ -22,8 +22,8 @@
*
* @APPLE_LICENSE_HEADER_END@
*/
-/*
- * Copyright (c) 1990, 1993
+/*-
+ * Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
@@ -58,6 +58,10 @@
* SUCH DAMAGE.
*/
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/stat.h>
@@ -77,23 +81,23 @@
#include "page.h"
#include "extern.h"
-static int alloc_segs __P((HTAB *, int));
-static int flush_meta __P((HTAB *));
-static int hash_access __P((HTAB *, ACTION, DBT *, DBT *));
-static int hash_close __P((DB *));
-static int hash_delete __P((const DB *, const DBT *, u_int));
-static int hash_fd __P((const DB *));
-static int hash_get __P((const DB *, const DBT *, DBT *, u_int));
-static int hash_put __P((const DB *, DBT *, const DBT *, u_int));
-static void *hash_realloc __P((SEGMENT **, int, int));
-static int hash_seq __P((const DB *, DBT *, DBT *, u_int));
-static int hash_sync __P((const DB *, u_int));
-static int hdestroy __P((HTAB *));
-static HTAB *init_hash __P((HTAB *, const char *, HASHINFO *));
-static int init_htab __P((HTAB *, int));
+static int alloc_segs(HTAB *, int);
+static int flush_meta(HTAB *);
+static int hash_access(HTAB *, ACTION, DBT *, DBT *);
+static int hash_close(DB *);
+static int hash_delete(const DB *, const DBT *, u_int32_t);
+static int hash_fd(const DB *);
+static int hash_get(const DB *, const DBT *, DBT *, u_int32_t);
+static int hash_put(const DB *, DBT *, const DBT *, u_int32_t);
+static void *hash_realloc(SEGMENT **, int, int);
+static int hash_seq(const DB *, DBT *, DBT *, u_int32_t);
+static int hash_sync(const DB *, u_int32_t);
+static int hdestroy(HTAB *);
+static HTAB *init_hash(HTAB *, const char *, HASHINFO *);
+static int init_htab(HTAB *, int);
#if BYTE_ORDER == LITTLE_ENDIAN
-static void swap_header __P((HTAB *));
-static void swap_header_copy __P((HASHHDR *, HASHHDR *));
+static void swap_header(HTAB *);
+static void swap_header_copy(HASHHDR *, HASHHDR *);
#endif
/* Fast arithmetic, relying on powers of 2, */
@@ -107,7 +111,7 @@
#define ABNORMAL (1)
#ifdef HASH_STATISTICS
-long hash_accesses, hash_collisions, hash_expansions, hash_overflows;
+int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
#endif
/************************** INTERFACE ROUTINES ***************************/
@@ -151,6 +155,13 @@
if (file) {
if ((hashp->fp = open(file, flags, mode)) == -1)
RETURN_ERROR(errno, error0);
+
+ /* if the .db file is empty, and we had permission to create
+ a new .db file, then reinitialize the database */
+ if ((flags & O_CREAT) &&
+ fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
+ new_table = 1;
+
(void)fcntl(hashp->fp, F_SETFD, 1);
}
if (new_table) {
@@ -200,7 +211,7 @@
(hashp->BSHIFT + BYTE_SHIFT);
hashp->nmaps = bpages;
- (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_long *));
+ (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
}
/* Initialize Buffer Manager */
@@ -368,7 +379,7 @@
HTAB *hashp;
int nelem;
{
- register int nbuckets, nsegs;
+ int nbuckets, nsegs;
int l2;
/*
@@ -387,7 +398,7 @@
hashp->LAST_FREED = 2;
/* First bitmap page is at: splitpoint l2 page offset 1 */
- if (__init_bitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
+ if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
return (-1);
hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
@@ -472,7 +483,7 @@
static int
hash_sync(dbp, flags)
const DB *dbp;
- u_int flags;
+ u_int32_t flags;
{
HTAB *hashp;
@@ -551,7 +562,7 @@
const DB *dbp;
const DBT *key;
DBT *data;
- u_int flag;
+ u_int32_t flag;
{
HTAB *hashp;
@@ -568,13 +579,14 @@
const DB *dbp;
DBT *key;
const DBT *data;
- u_int flag;
+ u_int32_t flag;
{
HTAB *hashp;
hashp = (HTAB *)dbp->internal;
if (flag && flag != R_NOOVERWRITE) {
- hashp->error = errno = EINVAL;
+ hashp->error = EINVAL;
+ errno = EINVAL;
return (ERROR);
}
if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
@@ -589,7 +601,7 @@
hash_delete(dbp, key, flag)
const DB *dbp;
const DBT *key;
- u_int flag; /* Ignored */
+ u_int32_t flag; /* Ignored */
{
HTAB *hashp;
@@ -614,12 +626,12 @@
ACTION action;
DBT *key, *val;
{
- register BUFHEAD *rbufp;
+ BUFHEAD *rbufp;
BUFHEAD *bufp, *save_bufp;
- register u_short *bp;
- register int n, ndx, off, size;
- register char *kp;
- u_short pageno;
+ u_int16_t *bp;
+ int n, ndx, off, size;
+ char *kp;
+ u_int16_t pageno;
#ifdef HASH_STATISTICS
hash_accesses++;
@@ -635,7 +647,7 @@
/* Pin the bucket chain */
rbufp->flags |= BUF_PIN;
- for (bp = (u_short *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
+ for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
if (bp[1] >= REAL_KEY) {
/* Real key/data pair */
if (size == off - *bp &&
@@ -654,7 +666,7 @@
return (ERROR);
}
/* FOR LOOP INIT */
- bp = (u_short *)rbufp->page;
+ bp = (u_int16_t *)rbufp->page;
n = *bp++;
ndx = 1;
off = hashp->BSIZE;
@@ -676,7 +688,7 @@
return (ERROR);
}
/* FOR LOOP INIT */
- bp = (u_short *)rbufp->page;
+ bp = (u_int16_t *)rbufp->page;
n = *bp++;
ndx = 1;
off = hashp->BSIZE;
@@ -710,7 +722,7 @@
save_bufp->flags &= ~BUF_PIN;
return (ABNORMAL);
case HASH_GET:
- bp = (u_short *)rbufp->page;
+ bp = (u_int16_t *)rbufp->page;
if (bp[ndx + 1] < REAL_KEY) {
if (__big_return(hashp, rbufp, ndx, val, 0))
return (ERROR);
@@ -741,12 +753,12 @@
hash_seq(dbp, key, data, flag)
const DB *dbp;
DBT *key, *data;
- u_int flag;
-{
- register u_int bucket;
- register BUFHEAD *bufp;
- HTAB *hashp;
- u_short *bp, ndx;
+ u_int32_t flag;
+{
+ u_int32_t bucket;
+ BUFHEAD *bufp;
+ HTAB *hashp;
+ u_int16_t *bp, ndx;
hashp = (HTAB *)dbp->internal;
if (flag && flag != R_FIRST && flag != R_NEXT) {
@@ -771,7 +783,7 @@
if (!bufp)
return (ERROR);
hashp->cpage = bufp;
- bp = (u_short *)bufp->page;
+ bp = (u_int16_t *)bufp->page;
if (bp[0])
break;
}
@@ -781,7 +793,7 @@
return (ABNORMAL);
}
} else
- bp = (u_short *)hashp->cpage->page;
+ bp = (u_int16_t *)hashp->cpage->page;
#ifdef DEBUG
assert(bp);
@@ -792,7 +804,7 @@
__get_buf(hashp, bp[hashp->cndx], bufp, 0);
if (!bufp)
return (ERROR);
- bp = (u_short *)(bufp->page);
+ bp = (u_int16_t *)(bufp->page);
hashp->cndx = 1;
}
if (!bp[0]) {
@@ -831,7 +843,7 @@
__expand_table(hashp)
HTAB *hashp;
{
- u_int old_bucket, new_bucket;
+ u_int32_t old_bucket, new_bucket;
int dirsize, new_segnum, spare_ndx;
#ifdef HASH_STATISTICS
@@ -887,9 +899,9 @@
SEGMENT **p_ptr;
int oldsize, newsize;
{
- register void *p;
-
- if (p = malloc(newsize)) {
+ void *p;
+
+ if ( (p = malloc(newsize)) ) {
memmove(p, *p_ptr, oldsize);
memset((char *)p + oldsize, 0, newsize - oldsize);
free(*p_ptr);
@@ -898,7 +910,7 @@
return (p);
}
-extern u_int
+extern u_int32_t
__call_hash(hashp, k, len)
HTAB *hashp;
char *k;
@@ -923,8 +935,8 @@
HTAB *hashp;
int nsegs;
{
- register int i;
- register SEGMENT store;
+ int i;
+ SEGMENT store;
int save_errno;