1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)hash.h	8.3 (Berkeley) 5/31/94
33  * $FreeBSD: src/lib/libc/db/hash/hash.h,v 1.6 2002/03/21 22:46:26 obrien Exp $
34  */
35 
36 #include <sys/param.h>
37 #define __need_size_t
38 #include <stddef.h>
39 #include <stdint.h>
40 
41 /* Check that newlib understands the byte order of its target system.  */
42 #ifndef _BYTE_ORDER
43 #error _BYTE_ORDER not defined by sys/param.h
44 #endif
45 
46 /* Define DB endianness constants based on target endianness.  */
47 #define DB_LITTLE_ENDIAN 1234
48 #define DB_BIG_ENDIAN 4321
49 #if (_BYTE_ORDER == _LITTLE_ENDIAN)
50 #define DB_BYTE_ORDER DB_LITTLE_ENDIAN
51 #else
52 #define DB_BYTE_ORDER DB_BIG_ENDIAN
53 #endif
54 
55 /* Operations */
56 typedef enum {
57 	HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
58 } HASH_ACTION;
59 
60 /* Buffer Management structures */
61 typedef struct _bufhead BUFHEAD;
62 
63 struct _bufhead {
64 	BUFHEAD		*prev;		/* LRU links */
65 	BUFHEAD		*next;		/* LRU links */
66 	BUFHEAD		*ovfl;		/* Overflow page buffer header */
67 	__uint32_t	 addr;		/* Address of this page */
68 	char		*page;		/* Actual page data */
69 	char	 	flags;
70 #define	BUF_MOD		0x0001
71 #define BUF_DISK	0x0002
72 #define	BUF_BUCKET	0x0004
73 #define	BUF_PIN		0x0008
74 };
75 
76 #define IS_BUCKET(X)	((X) & BUF_BUCKET)
77 
78 typedef BUFHEAD **SEGMENT;
79 
80 /* Hash Table Information */
81 typedef struct hashhdr {		/* Disk resident portion */
82 	int32_t		magic;		/* Magic NO for hash tables */
83 	int32_t		version;	/* Version ID */
84 	__uint32_t	lorder;		/* Byte Order */
85 	int32_t		bsize;		/* Bucket/Page Size */
86 	int32_t		bshift;		/* Bucket shift */
87 	int32_t		dsize;		/* Directory Size */
88 	int32_t		ssize;		/* Segment Size */
89 	int32_t		sshift;		/* Segment shift */
90 	int32_t		ovfl_point;	/* Where overflow pages are being
91 					 * allocated */
92 	int32_t		last_freed;	/* Last overflow page freed */
93 	int32_t		max_bucket;	/* ID of Maximum bucket in use */
94 	int32_t		high_mask;	/* Mask to modulo into entire table */
95 	int32_t		low_mask;	/* Mask to modulo into lower half of
96 					 * table */
97 	int32_t		ffactor;	/* Fill factor */
98 	int32_t		nkeys;		/* Number of keys in hash table */
99 	int32_t		hdrpages;	/* Size of table header */
100 	uint32_t	h_charkey;	/* value of hash(CHARKEY) */
101 #define NCACHED	32			/* number of bit maps and spare
102 					 * points */
103 	int32_t		spares[NCACHED];/* spare pages for overflow */
104 	__uint16_t	bitmaps[NCACHED];	/* address of overflow page
105 						 * bitmaps */
106 } HASHHDR;
107 
108 typedef struct htab	 {		/* Memory resident data structure */
109 	HASHHDR 	hdr;		/* Header */
110 	int		nsegs;		/* Number of allocated segments */
111 	int		exsegs;		/* Number of extra allocated
112 					 * segments */
113 	__uint32_t			/* Hash function */
114 	    (*hash)(const void *, size_t);
115 	int		flags;		/* Flag values */
116 	int		fp;		/* File pointer */
117 	char		*tmp_buf;	/* Temporary Buffer for BIG data */
118 	char		*tmp_key;	/* Temporary Buffer for BIG keys */
119 	BUFHEAD 	*cpage;		/* Current page */
120 	int32_t		cbucket;	/* Current bucket */
121 	int		cndx;		/* Index of next item on cpage */
122 	int		error;		/* Error Number -- for DBM
123 					 * compatibility */
124 	int		new_file;	/* Indicates if fd is backing store
125 					 * or no */
126 	int		save_file;	/* Indicates whether we need to flush
127 					 * file at
128 					 * exit */
129 	__uint32_t	*mapp[NCACHED];	/* Pointers to page maps */
130 	int		nmaps;		/* Initial number of bitmaps */
131 	int		nbufs;		/* Number of buffers left to
132 					 * allocate */
133 	BUFHEAD 	bufhead;	/* Header of buffer lru list */
134 	SEGMENT 	*dir;		/* Hash Bucket directory */
135 } HTAB;
136 
137 /*
138  * Constants
139  */
140 #if INT_MAX == 32767
141 #define	MAX_BSIZE		4096
142 #else
143 #define	MAX_BSIZE		65536		/* 2^16 */
144 #endif
145 #define MIN_BUFFERS		6
146 #define MINHDRSIZE		512
147 #if INT_MAX == 32767
148 #define DEF_BUFSIZE		4096
149 #else
150 #define DEF_BUFSIZE		65536		/* 64 K */
151 #endif
152 #define DEF_BUCKET_SIZE		4096
153 #define DEF_BUCKET_SHIFT	12		/* log2(BUCKET) */
154 #define DEF_SEGSIZE		256
155 #define DEF_SEGSIZE_SHIFT	8		/* log2(SEGSIZE)	 */
156 #define DEF_DIRSIZE		256
157 #define DEF_FFACTOR		65536
158 #define MIN_FFACTOR		4
159 #define SPLTMAX			8
160 #define CHARKEY			"%$sniglet^&"
161 #define NUMKEY			1038583
162 #define BYTE_SHIFT		3
163 #define INT_TO_BYTE		2
164 #define INT_BYTE_SHIFT		5
165 #define ALL_SET			((__uint32_t)0xFFFFFFFF)
166 #define ALL_CLEAR		0
167 
168 #define PTROF(X)	((BUFHEAD *)((ptrdiff_t)(X)&~0x3))
169 #define ISMOD(X)	((__uint32_t)(ptrdiff_t)(X)&0x1)
170 #define DOMOD(X)	((X) = (char *)((ptrdiff_t)(X)|0x1))
171 #define ISDISK(X)	((__uint32_t)(ptrdiff_t)(X)&0x2)
172 #define DODISK(X)	((X) = (char *)((ptrdiff_t)(X)|0x2))
173 
174 #define BITS_PER_MAP	32
175 
176 /* Given the address of the beginning of a big map, clear/set the nth bit */
177 #define CLRBIT(A, N)	((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
178 #define SETBIT(A, N)	((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
179 #define ISSET(A, N)	((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
180 
181 /* Overflow management */
182 /*
183  * Overflow page numbers are allocated per split point.  At each doubling of
184  * the table, we can allocate extra pages.  So, an overflow page number has
185  * the top 5 bits indicate which split point and the lower 11 bits indicate
186  * which page at that split point is indicated (pages within split points are
187  * numberered starting with 1).
188  */
189 
190 #define SPLITSHIFT	11
191 #define SPLITMASK	0x7FF
192 #define SPLITNUM(N)	(((__uint32_t)(N)) >> SPLITSHIFT)
193 #define OPAGENUM(N)	((N) & SPLITMASK)
194 #define	OADDR_OF(S,O)	((__uint32_t)((__uint32_t)(S) << SPLITSHIFT) + (O))
195 
196 #define BUCKET_TO_PAGE(B) \
197 	(B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0)
198 #define OADDR_TO_PAGE(B) 	\
199 	BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
200 
201 /*
202  * page.h contains a detailed description of the page format.
203  *
204  * Normally, keys and data are accessed from offset tables in the top of
205  * each page which point to the beginning of the key and data.  There are
206  * four flag values which may be stored in these offset tables which indicate
207  * the following:
208  *
209  *
210  * OVFLPAGE	Rather than a key data pair, this pair contains
211  *		the address of an overflow page.  The format of
212  *		the pair is:
213  *		    OVERFLOW_PAGE_NUMBER OVFLPAGE
214  *
215  * PARTIAL_KEY	This must be the first key/data pair on a page
216  *		and implies that page contains only a partial key.
217  *		That is, the key is too big to fit on a single page
218  *		so it starts on this page and continues on the next.
219  *		The format of the page is:
220  *		    KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
221  *
222  *		    KEY_OFF -- offset of the beginning of the key
223  *		    PARTIAL_KEY -- 1
224  *		    OVFL_PAGENO - page number of the next overflow page
225  *		    OVFLPAGE -- 0
226  *
227  * FULL_KEY	This must be the first key/data pair on the page.  It
228  *		is used in two cases.
229  *
230  *		Case 1:
231  *		    There is a complete key on the page but no data
232  *		    (because it wouldn't fit).  The next page contains
233  *		    the data.
234  *
235  *		    Page format it:
236  *		    KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
237  *
238  *		    KEY_OFF -- offset of the beginning of the key
239  *		    FULL_KEY -- 2
240  *		    OVFL_PAGENO - page number of the next overflow page
241  *		    OVFLPAGE -- 0
242  *
243  *		Case 2:
244  *		    This page contains no key, but part of a large
245  *		    data field, which is continued on the next page.
246  *
247  *		    Page format it:
248  *		    DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
249  *
250  *		    KEY_OFF -- offset of the beginning of the data on
251  *				this page
252  *		    FULL_KEY -- 2
253  *		    OVFL_PAGENO - page number of the next overflow page
254  *		    OVFLPAGE -- 0
255  *
256  * FULL_KEY_DATA
257  *		This must be the first key/data pair on the page.
258  *		There are two cases:
259  *
260  *		Case 1:
261  *		    This page contains a key and the beginning of the
262  *		    data field, but the data field is continued on the
263  *		    next page.
264  *
265  *		    Page format is:
266  *		    KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
267  *
268  *		    KEY_OFF -- offset of the beginning of the key
269  *		    FULL_KEY_DATA -- 3
270  *		    OVFL_PAGENO - page number of the next overflow page
271  *		    DATA_OFF -- offset of the beginning of the data
272  *
273  *		Case 2:
274  *		    This page contains the last page of a big data pair.
275  *		    There is no key, only the  tail end of the data
276  *		    on this page.
277  *
278  *		    Page format is:
279  *		    DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
280  *
281  *		    DATA_OFF -- offset of the beginning of the data on
282  *				this page
283  *		    FULL_KEY_DATA -- 3
284  *		    OVFL_PAGENO - page number of the next overflow page
285  *		    OVFLPAGE -- 0
286  *
287  *		    OVFL_PAGENO and OVFLPAGE are optional (they are
288  *		    not present if there is no next page).
289  */
290 
291 #define OVFLPAGE	0
292 #define PARTIAL_KEY	1
293 #define FULL_KEY	2
294 #define FULL_KEY_DATA	3
295 #define	REAL_KEY	4
296 
297 /* Short hands for accessing structure */
298 #define BSIZE		hdr.bsize
299 #define BSHIFT		hdr.bshift
300 #define DSIZE		hdr.dsize
301 #define SGSIZE		hdr.ssize
302 #define SSHIFT		hdr.sshift
303 #define LORDER		hdr.lorder
304 #define OVFL_POINT	hdr.ovfl_point
305 #define	LAST_FREED	hdr.last_freed
306 #define MAX_BUCKET	hdr.max_bucket
307 #define FFACTOR		hdr.ffactor
308 #define HIGH_MASK	hdr.high_mask
309 #define LOW_MASK	hdr.low_mask
310 #define NKEYS		hdr.nkeys
311 #define HDRPAGES	hdr.hdrpages
312 #define SPARES		hdr.spares
313 #define BITMAPS		hdr.bitmaps
314 #define HASH_VERSION	hdr.version
315 #define MAGIC		hdr.magic
316 #define NEXT_FREE	hdr.next_free
317 #define H_CHARKEY	hdr.h_charkey
318