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 #ifdef __GNUC__
81 #pragma GCC diagnostic ignored "-Wpragmas"
82 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
83 /* 'bsize' is used directly with malloc/realloc which confuses -fanalyzer */
84 #pragma GCC diagnostic ignored "-Wanalyzer-allocation-size"
85 #endif
86 
87 /* Hash Table Information */
88 typedef struct hashhdr {		/* Disk resident portion */
89 	int32_t		magic;		/* Magic NO for hash tables */
90 	int32_t		version;	/* Version ID */
91 	__uint32_t	lorder;		/* Byte Order */
92 	int32_t		bsize;		/* Bucket/Page Size */
93 	int32_t		bshift;		/* Bucket shift */
94 	int32_t		dsize;		/* Directory Size */
95 	int32_t		ssize;		/* Segment Size */
96 	int32_t		sshift;		/* Segment shift */
97 	int32_t		ovfl_point;	/* Where overflow pages are being
98 					 * allocated */
99 	int32_t		last_freed;	/* Last overflow page freed */
100 	int32_t		max_bucket;	/* ID of Maximum bucket in use */
101 	int32_t		high_mask;	/* Mask to modulo into entire table */
102 	int32_t		low_mask;	/* Mask to modulo into lower half of
103 					 * table */
104 	int32_t		ffactor;	/* Fill factor */
105 	int32_t		nkeys;		/* Number of keys in hash table */
106 	int32_t		hdrpages;	/* Size of table header */
107 	uint32_t	h_charkey;	/* value of hash(CHARKEY) */
108 #define NCACHED	32			/* number of bit maps and spare
109 					 * points */
110 	int32_t		spares[NCACHED];/* spare pages for overflow */
111 	__uint16_t	bitmaps[NCACHED];	/* address of overflow page
112 						 * bitmaps */
113 } HASHHDR;
114 
115 typedef struct htab	 {		/* Memory resident data structure */
116 	HASHHDR 	hdr;		/* Header */
117 	int		nsegs;		/* Number of allocated segments */
118 	int		exsegs;		/* Number of extra allocated
119 					 * segments */
120 	__uint32_t			/* Hash function */
121 	    (*hash)(const void *, size_t);
122 	int		flags;		/* Flag values */
123 	int		fp;		/* File pointer */
124 	char		*tmp_buf;	/* Temporary Buffer for BIG data */
125 	char		*tmp_key;	/* Temporary Buffer for BIG keys */
126 	BUFHEAD 	*cpage;		/* Current page */
127 	int32_t		cbucket;	/* Current bucket */
128 	int		cndx;		/* Index of next item on cpage */
129 	int		error;		/* Error Number -- for DBM
130 					 * compatibility */
131 	int		new_file;	/* Indicates if fd is backing store
132 					 * or no */
133 	int		save_file;	/* Indicates whether we need to flush
134 					 * file at
135 					 * exit */
136 	__uint32_t	*mapp[NCACHED];	/* Pointers to page maps */
137 	int		nmaps;		/* Initial number of bitmaps */
138 	int		nbufs;		/* Number of buffers left to
139 					 * allocate */
140 	BUFHEAD 	bufhead;	/* Header of buffer lru list */
141 	SEGMENT 	*dir;		/* Hash Bucket directory */
142 } HTAB;
143 
144 /*
145  * Constants
146  */
147 #if INT_MAX == 32767
148 #define	MAX_BSIZE		4096
149 #else
150 #define	MAX_BSIZE		65536		/* 2^16 */
151 #endif
152 #define MIN_BUFFERS		6
153 #define MINHDRSIZE		512
154 #if INT_MAX == 32767
155 #define DEF_BUFSIZE		4096
156 #else
157 #define DEF_BUFSIZE		65536		/* 64 K */
158 #endif
159 #define DEF_BUCKET_SIZE		4096
160 #define DEF_BUCKET_SHIFT	12		/* log2(BUCKET) */
161 #define DEF_SEGSIZE		256
162 #define DEF_SEGSIZE_SHIFT	8		/* log2(SEGSIZE)	 */
163 #define DEF_DIRSIZE		256
164 #define DEF_FFACTOR		65536
165 #define MIN_FFACTOR		4
166 #define SPLTMAX			8
167 #define CHARKEY			"%$sniglet^&"
168 #define NUMKEY			1038583
169 #define BYTE_SHIFT		3
170 #define INT_TO_BYTE		2
171 #define INT_BYTE_SHIFT		5
172 #define ALL_SET			((__uint32_t)0xFFFFFFFF)
173 #define ALL_CLEAR		0
174 
175 #define PTROF(X)	((BUFHEAD *)((ptrdiff_t)(X)&~0x3))
176 #define ISMOD(X)	((__uint32_t)(ptrdiff_t)(X)&0x1)
177 #define DOMOD(X)	((X) = (char *)((ptrdiff_t)(X)|0x1))
178 #define ISDISK(X)	((__uint32_t)(ptrdiff_t)(X)&0x2)
179 #define DODISK(X)	((X) = (char *)((ptrdiff_t)(X)|0x2))
180 
181 #define BITS_PER_MAP	32
182 
183 /* Given the address of the beginning of a big map, clear/set the nth bit */
184 #define CLRBIT(A, N)	((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
185 #define SETBIT(A, N)	((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
186 #define ISSET(A, N)	((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
187 
188 /* Overflow management */
189 /*
190  * Overflow page numbers are allocated per split point.  At each doubling of
191  * the table, we can allocate extra pages.  So, an overflow page number has
192  * the top 5 bits indicate which split point and the lower 11 bits indicate
193  * which page at that split point is indicated (pages within split points are
194  * numberered starting with 1).
195  */
196 
197 #define SPLITSHIFT	11
198 #define SPLITMASK	0x7FF
199 #define SPLITNUM(N)	(((__uint32_t)(N)) >> SPLITSHIFT)
200 #define OPAGENUM(N)	((N) & SPLITMASK)
201 #define	OADDR_OF(S,O)	((__uint32_t)((__uint32_t)(S) << SPLITSHIFT) + (O))
202 
203 #define BUCKET_TO_PAGE(B) \
204 	(B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0)
205 #define OADDR_TO_PAGE(B) 	\
206 	BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
207 
208 /*
209  * page.h contains a detailed description of the page format.
210  *
211  * Normally, keys and data are accessed from offset tables in the top of
212  * each page which point to the beginning of the key and data.  There are
213  * four flag values which may be stored in these offset tables which indicate
214  * the following:
215  *
216  *
217  * OVFLPAGE	Rather than a key data pair, this pair contains
218  *		the address of an overflow page.  The format of
219  *		the pair is:
220  *		    OVERFLOW_PAGE_NUMBER OVFLPAGE
221  *
222  * PARTIAL_KEY	This must be the first key/data pair on a page
223  *		and implies that page contains only a partial key.
224  *		That is, the key is too big to fit on a single page
225  *		so it starts on this page and continues on the next.
226  *		The format of the page is:
227  *		    KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
228  *
229  *		    KEY_OFF -- offset of the beginning of the key
230  *		    PARTIAL_KEY -- 1
231  *		    OVFL_PAGENO - page number of the next overflow page
232  *		    OVFLPAGE -- 0
233  *
234  * FULL_KEY	This must be the first key/data pair on the page.  It
235  *		is used in two cases.
236  *
237  *		Case 1:
238  *		    There is a complete key on the page but no data
239  *		    (because it wouldn't fit).  The next page contains
240  *		    the data.
241  *
242  *		    Page format it:
243  *		    KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
244  *
245  *		    KEY_OFF -- offset of the beginning of the key
246  *		    FULL_KEY -- 2
247  *		    OVFL_PAGENO - page number of the next overflow page
248  *		    OVFLPAGE -- 0
249  *
250  *		Case 2:
251  *		    This page contains no key, but part of a large
252  *		    data field, which is continued on the next page.
253  *
254  *		    Page format it:
255  *		    DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
256  *
257  *		    KEY_OFF -- offset of the beginning of the data on
258  *				this page
259  *		    FULL_KEY -- 2
260  *		    OVFL_PAGENO - page number of the next overflow page
261  *		    OVFLPAGE -- 0
262  *
263  * FULL_KEY_DATA
264  *		This must be the first key/data pair on the page.
265  *		There are two cases:
266  *
267  *		Case 1:
268  *		    This page contains a key and the beginning of the
269  *		    data field, but the data field is continued on the
270  *		    next page.
271  *
272  *		    Page format is:
273  *		    KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
274  *
275  *		    KEY_OFF -- offset of the beginning of the key
276  *		    FULL_KEY_DATA -- 3
277  *		    OVFL_PAGENO - page number of the next overflow page
278  *		    DATA_OFF -- offset of the beginning of the data
279  *
280  *		Case 2:
281  *		    This page contains the last page of a big data pair.
282  *		    There is no key, only the  tail end of the data
283  *		    on this page.
284  *
285  *		    Page format is:
286  *		    DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
287  *
288  *		    DATA_OFF -- offset of the beginning of the data on
289  *				this page
290  *		    FULL_KEY_DATA -- 3
291  *		    OVFL_PAGENO - page number of the next overflow page
292  *		    OVFLPAGE -- 0
293  *
294  *		    OVFL_PAGENO and OVFLPAGE are optional (they are
295  *		    not present if there is no next page).
296  */
297 
298 #define OVFLPAGE	0
299 #define PARTIAL_KEY	1
300 #define FULL_KEY	2
301 #define FULL_KEY_DATA	3
302 #define	REAL_KEY	4
303 
304 /* Short hands for accessing structure */
305 #define BSIZE		hdr.bsize
306 #define BSHIFT		hdr.bshift
307 #define DSIZE		hdr.dsize
308 #define SGSIZE		hdr.ssize
309 #define SSHIFT		hdr.sshift
310 #define LORDER		hdr.lorder
311 #define OVFL_POINT	hdr.ovfl_point
312 #define	LAST_FREED	hdr.last_freed
313 #define MAX_BUCKET	hdr.max_bucket
314 #define FFACTOR		hdr.ffactor
315 #define HIGH_MASK	hdr.high_mask
316 #define LOW_MASK	hdr.low_mask
317 #define NKEYS		hdr.nkeys
318 #define HDRPAGES	hdr.hdrpages
319 #define SPARES		hdr.spares
320 #define BITMAPS		hdr.bitmaps
321 #define HASH_VERSION	hdr.version
322 #define MAGIC		hdr.magic
323 #define NEXT_FREE	hdr.next_free
324 #define H_CHARKEY	hdr.h_charkey
325