2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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 $
36 #include <sys/param.h>
41 /* Check that newlib understands the byte order of its target system. */
43 #error BYTE_ORDER not defined by sys/param.h
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
52 #define DB_BYTE_ORDER DB_BIG_ENDIAN
57 HASH_GET
, HASH_PUT
, HASH_PUTNEW
, HASH_DELETE
, HASH_FIRST
, HASH_NEXT
60 /* Buffer Management structures */
61 typedef struct _bufhead 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 */
70 #define BUF_MOD 0x0001
71 #define BUF_DISK 0x0002
72 #define BUF_BUCKET 0x0004
73 #define BUF_PIN 0x0008
76 #define IS_BUCKET(X) ((X) & BUF_BUCKET)
78 typedef BUFHEAD
**SEGMENT
;
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
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
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 int32_t h_charkey
; /* value of hash(CHARKEY) */
101 #define NCACHED 32 /* number of bit maps and spare
103 int32_t spares
[NCACHED
];/* spare pages for overflow */
104 __uint16_t bitmaps
[NCACHED
]; /* address of overflow page
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
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
124 int new_file
; /* Indicates if fd is backing store
126 int save_file
; /* Indicates whether we need to flush
129 __uint32_t
*mapp
[NCACHED
]; /* Pointers to page maps */
130 int nmaps
; /* Initial number of bitmaps */
131 int nbufs
; /* Number of buffers left to
133 BUFHEAD bufhead
; /* Header of buffer lru list */
134 SEGMENT
*dir
; /* Hash Bucket directory */
141 #define MAX_BSIZE 4096
143 #define MAX_BSIZE 65536 /* 2^16 */
145 #define MIN_BUFFERS 6
146 #define MINHDRSIZE 512
148 #define DEF_BUFSIZE 4096
150 #define DEF_BUFSIZE 65536 /* 64 K */
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
160 #define CHARKEY "%$sniglet^&"
161 #define NUMKEY 1038583
163 #define INT_TO_BYTE 2
164 #define INT_BYTE_SHIFT 5
165 #define ALL_SET ((__uint32_t)0xFFFFFFFF)
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))
174 #define BITS_PER_MAP 32
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)))
181 /* Overflow management */
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).
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))
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));
202 * page.h contains a detailed description of the page format.
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
210 * OVFLPAGE Rather than a key data pair, this pair contains
211 * the address of an overflow page. The format of
213 * OVERFLOW_PAGE_NUMBER OVFLPAGE
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
222 * KEY_OFF -- offset of the beginning of the key
224 * OVFL_PAGENO - page number of the next overflow page
227 * FULL_KEY This must be the first key/data pair on the page. It
228 * is used in two cases.
231 * There is a complete key on the page but no data
232 * (because it wouldn't fit). The next page contains
236 * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
238 * KEY_OFF -- offset of the beginning of the key
240 * OVFL_PAGENO - page number of the next overflow page
244 * This page contains no key, but part of a large
245 * data field, which is continued on the next page.
248 * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
250 * KEY_OFF -- offset of the beginning of the data on
253 * OVFL_PAGENO - page number of the next overflow page
257 * This must be the first key/data pair on the page.
258 * There are two cases:
261 * This page contains a key and the beginning of the
262 * data field, but the data field is continued on the
266 * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
268 * KEY_OFF -- offset of the beginning of the key
270 * OVFL_PAGENO - page number of the next overflow page
271 * DATA_OFF -- offset of the beginning of the data
274 * This page contains the last page of a big data pair.
275 * There is no key, only the tail end of the data
279 * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
281 * DATA_OFF -- offset of the beginning of the data on
284 * OVFL_PAGENO - page number of the next overflow page
287 * OVFL_PAGENO and OVFLPAGE are optional (they are
288 * not present if there is no next page).
292 #define PARTIAL_KEY 1
294 #define FULL_KEY_DATA 3
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