]> git.ipfire.org Git - thirdparty/glibc.git/blob - db2/db_int.h
Update.
[thirdparty/glibc.git] / db2 / db_int.h
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 *
7 * @(#)db_int.h.src 10.41 (Sleepycat) 1/8/98
8 */
9
10 #ifndef _DB_INTERNAL_H_
11 #define _DB_INTERNAL_H_
12
13 #include "db.h" /* Standard DB include file. */
14 #include "queue.h"
15 #include "os_func.h"
16 #include "os_ext.h"
17
18 /*******************************************************
19 * General purpose constants and macros.
20 *******************************************************/
21 #define UINT16_T_MAX 0xffff /* Maximum 16 bit unsigned. */
22 #define UINT32_T_MAX 0xffffffff /* Maximum 32 bit unsigned. */
23
24 #define DB_MIN_PGSIZE 0x000200 /* Minimum page size. */
25 #define DB_MAX_PGSIZE 0x010000 /* Maximum page size. */
26
27 #define DB_MINCACHE 10 /* Minimum cached pages */
28
29 #define MEGABYTE 1048576
30
31 /*
32 * If we are unable to determine the underlying filesystem block size, use
33 * 8K on the grounds that most OS's use less than 8K as their VM page size.
34 */
35 #define DB_DEF_IOSIZE (8 * 1024)
36
37 /*
38 * Aligning items to particular sizes or in pages or memory. ALIGNP is a
39 * separate macro, as we've had to cast the pointer to different integral
40 * types on different architectures.
41 *
42 * We cast pointers into unsigned longs when manipulating them because C89
43 * guarantees that u_long is the largest available integral type and further,
44 * to never generate overflows. However, neither C89 or C9X requires that
45 * any integer type be large enough to hold a pointer, although C9X created
46 * the intptr_t type, which is guaranteed to hold a pointer but may or may
47 * not exist. At some point in the future, we should test for intptr_t and
48 * use it where available.
49 */
50 #undef ALIGNTYPE
51 #define ALIGNTYPE u_long
52 #undef ALIGNP
53 #define ALIGNP(value, bound) ALIGN((ALIGNTYPE)value, bound)
54 #undef ALIGN
55 #define ALIGN(value, bound) (((value) + (bound) - 1) & ~((bound) - 1))
56
57 /*
58 * There are several on-page structures that are declared to have a number of
59 * fields followed by a variable length array of items. The structure size
60 * without including the variable length array or the address of the first of
61 * those elements can be found using SSZ.
62 *
63 * This macro can also be used to find the offset of a structure element in a
64 * structure. This is used in various places to copy structure elements from
65 * unaligned memory references, e.g., pointers into a packed page.
66 *
67 * There are two versions because compilers object if you take the address of
68 * an array.
69 */
70 #undef SSZ
71 #define SSZ(name, field) ((int)&(((name *)0)->field))
72
73 #undef SSZA
74 #define SSZA(name, field) ((int)&(((name *)0)->field[0]))
75
76 /* Macros to return per-process address, offsets based on shared regions. */
77 #define R_ADDR(base, offset) ((void *)((u_int8_t *)((base)->addr) + offset))
78 #define R_OFFSET(base, p) ((u_int8_t *)(p) - (u_int8_t *)(base)->addr)
79
80 /* Free and free-string macros that overwrite memory during debugging. */
81 #ifdef DEBUG
82 #undef FREE
83 #define FREE(p, len) { \
84 memset(p, 0xff, len); \
85 __db_free(p); \
86 }
87 #undef FREES
88 #define FREES(p) { \
89 FREE(p, strlen(p)); \
90 }
91 #else
92 #undef FREE
93 #define FREE(p, len) { \
94 __db_free(p); \
95 }
96 #undef FREES
97 #define FREES(p) { \
98 __db_free(p); \
99 }
100 #endif
101
102 /* Structure used to print flag values. */
103 typedef struct __fn {
104 u_int32_t mask; /* Flag value. */
105 const char *name; /* Flag name. */
106 } FN;
107
108 /* Set, clear and test flags. */
109 #define F_SET(p, f) (p)->flags |= (f)
110 #define F_CLR(p, f) (p)->flags &= ~(f)
111 #define F_ISSET(p, f) ((p)->flags & (f))
112 #define LF_SET(f) (flags |= (f))
113 #define LF_CLR(f) (flags &= ~(f))
114 #define LF_ISSET(f) (flags & (f))
115
116 /* Display separator string. */
117 #undef DB_LINE
118 #define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
119
120 /* Unused, or not-used-yet variable. "Shut that bloody compiler up!" */
121 #define COMPQUIET(n, v) (n) = (v)
122
123 /*******************************************************
124 * Files.
125 *******************************************************/
126 #ifndef MAXPATHLEN /* Maximum path length. */
127 #ifdef PATH_MAX
128 #define MAXPATHLEN PATH_MAX
129 #else
130 #define MAXPATHLEN 1024
131 #endif
132 #endif
133
134 #define PATH_DOT "." /* Current working directory. */
135 #define PATH_SEPARATOR "/" /* Path separator character. */
136
137 #ifndef S_IRUSR /* UNIX specific file permissions. */
138 #define S_IRUSR 0000400 /* R for owner */
139 #define S_IWUSR 0000200 /* W for owner */
140 #define S_IRGRP 0000040 /* R for group */
141 #define S_IWGRP 0000020 /* W for group */
142 #define S_IROTH 0000004 /* R for other */
143 #define S_IWOTH 0000002 /* W for other */
144 #endif
145
146 #ifndef S_ISDIR /* UNIX specific: directory test. */
147 #define S_ISDIR(m) ((m & 0170000) == 0040000)
148 #endif
149
150 /*******************************************************
151 * Mutex support.
152 *******************************************************/
153 typedef unsigned char tsl_t;
154
155
156
157 /*
158 * !!!
159 * Various systems require different alignments for mutexes (the worst we've
160 * seen so far is 16-bytes on some HP architectures). The mutex (tsl_t) must
161 * be first in the db_mutex_t structure, which must itself be first in the
162 * region. This ensures the alignment is as returned by mmap(2), which should
163 * be sufficient. All other mutex users must ensure proper alignment locally.
164 */
165 #define MUTEX_ALIGNMENT 1
166
167 /*
168 * The offset of a mutex in memory.
169 *
170 * !!!
171 * Not an off_t, so backing file offsets MUST be less than 4Gb. See the
172 * off field of the db_mutex_t as well.
173 */
174 #define MUTEX_LOCK_OFFSET(a, b) ((u_int32_t)((u_int8_t *)b - (u_int8_t *)a))
175
176 typedef struct _db_mutex_t {
177 #ifdef HAVE_SPINLOCKS
178 tsl_t tsl_resource; /* Resource test and set. */
179 #ifdef DEBUG
180 u_long pid; /* Lock holder: 0 or process pid. */
181 #endif
182 #else
183 u_int32_t off; /* Backing file offset. */
184 u_long pid; /* Lock holder: 0 or process pid. */
185 #endif
186 u_int32_t spins; /* Spins before block. */
187 u_int32_t mutex_set_wait; /* Granted after wait. */
188 u_int32_t mutex_set_nowait; /* Granted without waiting. */
189 } db_mutex_t;
190
191 #include "mutex_ext.h"
192
193 /*******************************************************
194 * Access methods.
195 *******************************************************/
196 /* Lock/unlock a DB thread. */
197 #define DB_THREAD_LOCK(dbp) \
198 (F_ISSET(dbp, DB_AM_THREAD) ? \
199 __db_mutex_lock((db_mutex_t *)(dbp)->mutexp, -1) : 0)
200 #define DB_THREAD_UNLOCK(dbp) \
201 (F_ISSET(dbp, DB_AM_THREAD) ? \
202 __db_mutex_unlock((db_mutex_t *)(dbp)->mutexp, -1) : 0)
203
204 /* Btree/recno local statistics structure. */
205 struct __db_bt_lstat; typedef struct __db_bt_lstat DB_BTREE_LSTAT;
206 struct __db_bt_lstat {
207 u_int32_t bt_freed; /* Pages freed for reuse. */
208 u_int32_t bt_pfxsaved; /* Bytes saved by prefix compression. */
209 u_int32_t bt_split; /* Total number of splits. */
210 u_int32_t bt_rootsplit; /* Root page splits. */
211 u_int32_t bt_fastsplit; /* Fast splits. */
212 u_int32_t bt_added; /* Items added. */
213 u_int32_t bt_deleted; /* Items deleted. */
214 u_int32_t bt_get; /* Items retrieved. */
215 u_int32_t bt_cache_hit; /* Hits in fast-insert code. */
216 u_int32_t bt_cache_miss; /* Misses in fast-insert code. */
217 };
218
219 /*******************************************************
220 * Environment.
221 *******************************************************/
222 /* Type passed to __db_appname(). */
223 typedef enum {
224 DB_APP_NONE=0, /* No type (region). */
225 DB_APP_DATA, /* Data file. */
226 DB_APP_LOG, /* Log file. */
227 DB_APP_TMP /* Temporary file. */
228 } APPNAME;
229
230 /*******************************************************
231 * Regions.
232 *******************************************************/
233 /*
234 * The shared memory regions share an initial structure so that the general
235 * region code can handle races between the region being deleted and other
236 * processes waiting on the region mutex.
237 *
238 * !!!
239 * Note, the mutex must be the first entry in the region; see comment above.
240 */
241 typedef struct _rlayout {
242 db_mutex_t lock; /* Region mutex. */
243 u_int32_t refcnt; /* Region reference count. */
244 size_t size; /* Region length. */
245 int majver; /* Major version number. */
246 int minver; /* Minor version number. */
247 int patch; /* Patch version number. */
248
249 #define DB_R_DELETED 0x01 /* Region was deleted. */
250 u_int32_t flags;
251 } RLAYOUT;
252
253 /*******************************************************
254 * Mpool.
255 *******************************************************/
256 /*
257 * File types for DB access methods. Negative numbers are reserved to DB.
258 */
259 #define DB_FTYPE_BTREE -1 /* Btree. */
260 #define DB_FTYPE_HASH -2 /* Hash. */
261
262 /* Structure used as the DB pgin/pgout pgcookie. */
263 typedef struct __dbpginfo {
264 size_t db_pagesize; /* Underlying page size. */
265 int needswap; /* If swapping required. */
266 } DB_PGINFO;
267
268 /*******************************************************
269 * Log.
270 *******************************************************/
271 /* Initialize an LSN to 'zero'. */
272 #define ZERO_LSN(LSN) { \
273 (LSN).file = 0; \
274 (LSN).offset = 0; \
275 }
276
277 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
278 #define IS_ZERO_LSN(LSN) ((LSN).file == 0)
279
280 /* Test if we need to log a change. */
281 #define DB_LOGGING(dbp) \
282 (F_ISSET(dbp, DB_AM_LOGGING) && !F_ISSET(dbp, DB_AM_RECOVER))
283
284 #ifdef DEBUG
285 /*
286 * Debugging macro to log operations.
287 * If DEBUG_WOP is defined, log operations that modify the database.
288 * If DEBUG_ROP is defined, log operations that read the database.
289 *
290 * D dbp
291 * T txn
292 * O operation (string)
293 * K key
294 * A data
295 * F flags
296 */
297 #define LOG_OP(D, T, O, K, A, F) { \
298 DB_LSN _lsn; \
299 DBT _op; \
300 if (DB_LOGGING((D))) { \
301 memset(&_op, 0, sizeof(_op)); \
302 _op.data = O; \
303 _op.size = strlen(O) + 1; \
304 (void)__db_debug_log((D)->dbenv->lg_info, \
305 T, &_lsn, 0, &_op, (D)->log_fileid, K, A, F); \
306 } \
307 }
308 #ifdef DEBUG_ROP
309 #define DEBUG_LREAD(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
310 #else
311 #define DEBUG_LREAD(D, T, O, K, A, F)
312 #endif
313 #ifdef DEBUG_WOP
314 #define DEBUG_LWRITE(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
315 #else
316 #define DEBUG_LWRITE(D, T, O, K, A, F)
317 #endif
318 #else
319 #define DEBUG_LREAD(D, T, O, K, A, F)
320 #define DEBUG_LWRITE(D, T, O, K, A, F)
321 #endif /* DEBUG */
322
323 /*******************************************************
324 * Transactions and recovery.
325 *******************************************************/
326 /*
327 * Out of band value for a lock. The locks are returned to callers as offsets
328 * into the lock regions. Since the RLAYOUT structure begins all regions, an
329 * offset of 0 is guaranteed not to be a valid lock.
330 */
331 #define LOCK_INVALID 0
332
333 /* The structure allocated for every transaction. */
334 struct __db_txn {
335 DB_TXNMGR *mgrp; /* Pointer to transaction manager. */
336 DB_TXN *parent; /* Pointer to transaction's parent. */
337 DB_LSN last_lsn; /* Lsn of last log write. */
338 u_int32_t txnid; /* Unique transaction id. */
339 size_t off; /* Detail structure within region. */
340 TAILQ_ENTRY(__db_txn) links;
341 };
342 #endif /* !_DB_INTERNAL_H_ */