security/nss/lib/dbm/include/hash.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/dbm/include/hash.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,335 @@
     1.4 +/*-
     1.5 + * Copyright (c) 1990, 1993, 1994
     1.6 + *	The Regents of the University of California.  All rights reserved.
     1.7 + *
     1.8 + * This code is derived from software contributed to Berkeley by
     1.9 + * Margo Seltzer.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + * 1. Redistributions of source code must retain the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer.
    1.16 + * 2. Redistributions in binary form must reproduce the above copyright
    1.17 + *    notice, this list of conditions and the following disclaimer in the
    1.18 + *    documentation and/or other materials provided with the distribution.
    1.19 + * 3. ***REMOVED*** - see 
    1.20 + *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
    1.21 + * 4. Neither the name of the University nor the names of its contributors
    1.22 + *    may be used to endorse or promote products derived from this software
    1.23 + *    without specific prior written permission.
    1.24 + *
    1.25 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    1.26 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.27 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.28 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    1.29 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.30 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.31 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.32 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.33 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.34 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.35 + * SUCH DAMAGE.
    1.36 + *
    1.37 + *	@(#)hash.h	8.3 (Berkeley) 5/31/94
    1.38 + */
    1.39 +
    1.40 +/* Operations */
    1.41 +
    1.42 +#include <stdio.h>
    1.43 +#include "mcom_db.h"
    1.44 +typedef enum {
    1.45 +	HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
    1.46 +} ACTION;
    1.47 +
    1.48 +/* Buffer Management structures */
    1.49 +typedef struct _bufhead BUFHEAD;
    1.50 +
    1.51 +struct _bufhead {
    1.52 +	BUFHEAD		*prev;		/* LRU links */
    1.53 +	BUFHEAD		*next;		/* LRU links */
    1.54 +	BUFHEAD		*ovfl;		/* Overflow page buffer header */
    1.55 +	uint32	 	addr;		/* Address of this page */
    1.56 +	char		*page;		/* Actual page data */
    1.57 +	char     	is_disk;
    1.58 +	char	 	flags;
    1.59 +#define	BUF_MOD		0x0001
    1.60 +#define BUF_DISK	0x0002
    1.61 +#define	BUF_BUCKET	0x0004
    1.62 +#define	BUF_PIN		0x0008
    1.63 +};
    1.64 +
    1.65 +#define IS_BUCKET(X)	((X) & BUF_BUCKET)
    1.66 +
    1.67 +typedef BUFHEAD **SEGMENT;
    1.68 +
    1.69 +typedef int DBFILE_PTR;
    1.70 +#define NO_FILE -1
    1.71 +#ifdef macintosh
    1.72 +#define DBFILE_OPEN(path, flag,mode) open((path), flag)
    1.73 +#define EXISTS(path)
    1.74 +#else
    1.75 +#define DBFILE_OPEN(path, flag,mode) open((path), (flag), (mode))
    1.76 +#endif
    1.77 +/* Hash Table Information */
    1.78 +typedef struct hashhdr {		/* Disk resident portion */
    1.79 +	int32		magic;		/* Magic NO for hash tables */
    1.80 +	int32		version;	/* Version ID */
    1.81 +	uint32		lorder;		/* Byte Order */
    1.82 +	int32		bsize;		/* Bucket/Page Size */
    1.83 +	int32		bshift;		/* Bucket shift */
    1.84 +	int32		dsize;		/* Directory Size */
    1.85 +	int32		ssize;		/* Segment Size */
    1.86 +	int32		sshift;		/* Segment shift */
    1.87 +	int32		ovfl_point;	/* Where overflow pages are being 
    1.88 +					 * allocated */
    1.89 +	int32		last_freed;	/* Last overflow page freed */
    1.90 +	int32		max_bucket;	/* ID of Maximum bucket in use */
    1.91 +	int32		high_mask;	/* Mask to modulo into entire table */
    1.92 +	int32		low_mask;	/* Mask to modulo into lower half of 
    1.93 +					 * table */
    1.94 +	int32		ffactor;	/* Fill factor */
    1.95 +	int32		nkeys;		/* Number of keys in hash table */
    1.96 +	int32		hdrpages;	/* Size of table header */
    1.97 +	uint32		h_charkey;	/* value of hash(CHARKEY) */
    1.98 +#define NCACHED	32			/* number of bit maps and spare 
    1.99 +					 * points */
   1.100 +	int32		spares[NCACHED];/* spare pages for overflow */
   1.101 +	uint16		bitmaps[NCACHED];	/* address of overflow page 
   1.102 +						 * bitmaps */
   1.103 +} HASHHDR;
   1.104 +
   1.105 +typedef struct htab	 {		/* Memory resident data structure */
   1.106 +	HASHHDR 	hdr;		/* Header */
   1.107 +	int		nsegs;		/* Number of allocated segments */
   1.108 +	int		exsegs;		/* Number of extra allocated 
   1.109 +					 * segments */
   1.110 +	uint32			/* Hash function */
   1.111 +	    (*hash)(const void *, size_t);
   1.112 +	int		flags;		/* Flag values */
   1.113 +	DBFILE_PTR	fp;		/* File pointer */
   1.114 +	char            *filename;
   1.115 +	char		*tmp_buf;	/* Temporary Buffer for BIG data */
   1.116 +	char		*tmp_key;	/* Temporary Buffer for BIG keys */
   1.117 +	BUFHEAD 	*cpage;		/* Current page */
   1.118 +	int		cbucket;	/* Current bucket */
   1.119 +	int		cndx;		/* Index of next item on cpage */
   1.120 +	int		dbmerrno;		/* Error Number -- for DBM 
   1.121 +					 * compatability */
   1.122 +	int		new_file;	/* Indicates if fd is backing store 
   1.123 +					 * or no */
   1.124 +	int		save_file;	/* Indicates whether we need to flush 
   1.125 +					 * file at
   1.126 +					 * exit */
   1.127 +	uint32		*mapp[NCACHED];	/* Pointers to page maps */
   1.128 +	int		nmaps;		/* Initial number of bitmaps */
   1.129 +	int		nbufs;		/* Number of buffers left to 
   1.130 +					 * allocate */
   1.131 +	BUFHEAD 	bufhead;	/* Header of buffer lru list */
   1.132 +	SEGMENT 	*dir;		/* Hash Bucket directory */
   1.133 +	off_t		file_size;	/* in bytes */
   1.134 +	char		is_temp;	/* unlink file on close */
   1.135 +	char		updateEOF;	/* force EOF update on flush */
   1.136 +} HTAB;
   1.137 +
   1.138 +/*
   1.139 + * Constants
   1.140 + */
   1.141 +#define DATABASE_CORRUPTED_ERROR -999   /* big ugly abort, delete database */
   1.142 +#define	OLD_MAX_BSIZE		65536		/* 2^16 */
   1.143 +#define MAX_BSIZE       	32l*1024l         /* 2^15 */
   1.144 +#define MIN_BUFFERS		6
   1.145 +#define MINHDRSIZE		512
   1.146 +#define DEF_BUFSIZE		65536l		/* 64 K */
   1.147 +#define DEF_BUCKET_SIZE		4096
   1.148 +#define DEF_BUCKET_SHIFT	12		/* log2(BUCKET) */
   1.149 +#define DEF_SEGSIZE		256
   1.150 +#define DEF_SEGSIZE_SHIFT	8		/* log2(SEGSIZE)	 */
   1.151 +#define DEF_DIRSIZE		256
   1.152 +#define DEF_FFACTOR		65536l
   1.153 +#define MIN_FFACTOR		4
   1.154 +#define SPLTMAX			8
   1.155 +#define CHARKEY			"%$sniglet^&"
   1.156 +#define NUMKEY			1038583l
   1.157 +#define BYTE_SHIFT		3
   1.158 +#define INT_TO_BYTE		2
   1.159 +#define INT_BYTE_SHIFT		5
   1.160 +#define ALL_SET			((uint32)0xFFFFFFFF)
   1.161 +#define ALL_CLEAR		0
   1.162 +
   1.163 +#define PTROF(X)	((ptrdiff_t)(X) == BUF_DISK ? 0 : (X))
   1.164 +#define ISDISK(X)	((X) ? ((ptrdiff_t)(X) == BUF_DISK ? BUF_DISK \
   1.165 +				: (X)->is_disk) : 0)
   1.166 +
   1.167 +#define BITS_PER_MAP	32
   1.168 +
   1.169 +/* Given the address of the beginning of a big map, clear/set the nth bit */
   1.170 +#define CLRBIT(A, N)	((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
   1.171 +#define SETBIT(A, N)	((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
   1.172 +#define ISSET(A, N)	((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
   1.173 +
   1.174 +/* Overflow management */
   1.175 +/*
   1.176 + * Overflow page numbers are allocated per split point.  At each doubling of
   1.177 + * the table, we can allocate extra pages.  So, an overflow page number has
   1.178 + * the top 5 bits indicate which split point and the lower 11 bits indicate
   1.179 + * which page at that split point is indicated (pages within split points are
   1.180 + * numberered starting with 1).
   1.181 + */
   1.182 +
   1.183 +#define SPLITSHIFT	11
   1.184 +#define SPLITMASK	0x7FF
   1.185 +#define SPLITNUM(N)	(((uint32)(N)) >> SPLITSHIFT)
   1.186 +#define OPAGENUM(N)	((N) & SPLITMASK)
   1.187 +#define	OADDR_OF(S,O)	((uint32)((uint32)(S) << SPLITSHIFT) + (O))
   1.188 +
   1.189 +#define BUCKET_TO_PAGE(B) \
   1.190 +	(B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((uint32)((B)+1))-1] : 0)
   1.191 +#define OADDR_TO_PAGE(B) 	\
   1.192 +	BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
   1.193 +
   1.194 +/*
   1.195 + * page.h contains a detailed description of the page format.
   1.196 + *
   1.197 + * Normally, keys and data are accessed from offset tables in the top of
   1.198 + * each page which point to the beginning of the key and data.  There are
   1.199 + * four flag values which may be stored in these offset tables which indicate
   1.200 + * the following:
   1.201 + *
   1.202 + *
   1.203 + * OVFLPAGE	Rather than a key data pair, this pair contains
   1.204 + *		the address of an overflow page.  The format of
   1.205 + *		the pair is:
   1.206 + *		    OVERFLOW_PAGE_NUMBER OVFLPAGE
   1.207 + *
   1.208 + * PARTIAL_KEY	This must be the first key/data pair on a page
   1.209 + *		and implies that page contains only a partial key.
   1.210 + *		That is, the key is too big to fit on a single page
   1.211 + *		so it starts on this page and continues on the next.
   1.212 + *		The format of the page is:
   1.213 + *		    KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
   1.214 + *		
   1.215 + *		    KEY_OFF -- offset of the beginning of the key
   1.216 + *		    PARTIAL_KEY -- 1
   1.217 + *		    OVFL_PAGENO - page number of the next overflow page
   1.218 + *		    OVFLPAGE -- 0
   1.219 + *
   1.220 + * FULL_KEY	This must be the first key/data pair on the page.  It
   1.221 + *		is used in two cases.
   1.222 + *
   1.223 + *		Case 1:
   1.224 + *		    There is a complete key on the page but no data
   1.225 + *		    (because it wouldn't fit).  The next page contains
   1.226 + *		    the data.
   1.227 + *
   1.228 + *		    Page format it:
   1.229 + *		    KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
   1.230 + *
   1.231 + *		    KEY_OFF -- offset of the beginning of the key
   1.232 + *		    FULL_KEY -- 2
   1.233 + *		    OVFL_PAGENO - page number of the next overflow page
   1.234 + *		    OVFLPAGE -- 0
   1.235 + *
   1.236 + *		Case 2:
   1.237 + *		    This page contains no key, but part of a large
   1.238 + *		    data field, which is continued on the next page.
   1.239 + *
   1.240 + *		    Page format it:
   1.241 + *		    DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
   1.242 + *
   1.243 + *		    KEY_OFF -- offset of the beginning of the data on
   1.244 + *				this page
   1.245 + *		    FULL_KEY -- 2
   1.246 + *		    OVFL_PAGENO - page number of the next overflow page
   1.247 + *		    OVFLPAGE -- 0
   1.248 + *
   1.249 + * FULL_KEY_DATA 
   1.250 + *		This must be the first key/data pair on the page.
   1.251 + *		There are two cases:
   1.252 + *
   1.253 + *		Case 1:
   1.254 + *		    This page contains a key and the beginning of the
   1.255 + *		    data field, but the data field is continued on the
   1.256 + *		    next page.
   1.257 + *
   1.258 + *		    Page format is:
   1.259 + *		    KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
   1.260 + *
   1.261 + *		    KEY_OFF -- offset of the beginning of the key
   1.262 + *		    FULL_KEY_DATA -- 3
   1.263 + *		    OVFL_PAGENO - page number of the next overflow page
   1.264 + *		    DATA_OFF -- offset of the beginning of the data
   1.265 + *
   1.266 + *		Case 2:
   1.267 + *		    This page contains the last page of a big data pair.
   1.268 + *		    There is no key, only the  tail end of the data
   1.269 + *		    on this page.
   1.270 + *
   1.271 + *		    Page format is:
   1.272 + *		    DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
   1.273 + *
   1.274 + *		    DATA_OFF -- offset of the beginning of the data on
   1.275 + *				this page
   1.276 + *		    FULL_KEY_DATA -- 3
   1.277 + *		    OVFL_PAGENO - page number of the next overflow page
   1.278 + *		    OVFLPAGE -- 0
   1.279 + *
   1.280 + *		    OVFL_PAGENO and OVFLPAGE are optional (they are
   1.281 + *		    not present if there is no next page).
   1.282 + */
   1.283 +
   1.284 +#define OVFLPAGE	0
   1.285 +#define PARTIAL_KEY	1
   1.286 +#define FULL_KEY	2
   1.287 +#define FULL_KEY_DATA	3
   1.288 +#define	REAL_KEY	4
   1.289 +
   1.290 +/* Short hands for accessing structure */
   1.291 +#undef BSIZE
   1.292 +#define BSIZE		hdr.bsize
   1.293 +#undef BSHIFT
   1.294 +#define BSHIFT		hdr.bshift
   1.295 +#define DSIZE		hdr.dsize
   1.296 +#define SGSIZE		hdr.ssize
   1.297 +#define SSHIFT		hdr.sshift
   1.298 +#define LORDER		hdr.lorder
   1.299 +#define OVFL_POINT	hdr.ovfl_point
   1.300 +#define	LAST_FREED	hdr.last_freed
   1.301 +#define MAX_BUCKET	hdr.max_bucket
   1.302 +#define FFACTOR		hdr.ffactor
   1.303 +#define HIGH_MASK	hdr.high_mask
   1.304 +#define LOW_MASK	hdr.low_mask
   1.305 +#define NKEYS		hdr.nkeys
   1.306 +#define HDRPAGES	hdr.hdrpages
   1.307 +#define SPARES		hdr.spares
   1.308 +#define BITMAPS		hdr.bitmaps
   1.309 +#define VERSION		hdr.version
   1.310 +#define MAGIC		hdr.magic
   1.311 +#define NEXT_FREE	hdr.next_free
   1.312 +#define H_CHARKEY	hdr.h_charkey
   1.313 +
   1.314 +extern uint32 (*__default_hash) (const void *, size_t);
   1.315 +void __buf_init(HTAB *hashp, int32 nbytes);
   1.316 +int __big_delete(HTAB *hashp, BUFHEAD *bufp);
   1.317 +BUFHEAD * __get_buf(HTAB *hashp, uint32 addr, BUFHEAD *prev_bp, int newpage);
   1.318 +uint32 __call_hash(HTAB *hashp, char *k, size_t len);
   1.319 +#include "page.h"
   1.320 +extern int __big_split(HTAB *hashp, BUFHEAD *op,BUFHEAD *np,
   1.321 +BUFHEAD *big_keyp,uint32 addr,uint32   obucket, SPLIT_RETURN *ret);
   1.322 +void __free_ovflpage(HTAB *hashp, BUFHEAD *obufp);
   1.323 +BUFHEAD * __add_ovflpage(HTAB *hashp, BUFHEAD *bufp);
   1.324 +int __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val);
   1.325 +int __expand_table(HTAB *hashp);
   1.326 +uint32 __log2(uint32 num);
   1.327 +void __reclaim_buf(HTAB *hashp, BUFHEAD *bp);
   1.328 +int __get_page(HTAB *hashp, char * p, uint32 bucket, int is_bucket, int is_disk, int is_bitmap);
   1.329 +int __put_page(HTAB *hashp, char *p, uint32 bucket, int is_bucket, int is_bitmap);
   1.330 +int __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx);
   1.331 +int __buf_free(HTAB *hashp, int do_free, int to_disk);
   1.332 +int __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size);
   1.333 +uint16 __find_last_page(HTAB *hashp, BUFHEAD **bpp);
   1.334 +int __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT * val);
   1.335 +int __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current);
   1.336 +int __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx);
   1.337 +int __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set);
   1.338 +int __split_page(HTAB *hashp, uint32 obucket, uint32 nbucket);

mercurial