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