Update.
[glibc.git] / db2 / db_int.h
blob228c7ab4f27b5194a0da13b91d525e3f1bd639d7
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
7 * @(#)db_int.h 10.77 (Sleepycat) 1/3/99
8 */
10 #ifndef _DB_INTERNAL_H_
11 #define _DB_INTERNAL_H_
13 #include <db.h> /* Standard DB include file. */
14 #include "queue.h"
15 #include "shqueue.h"
17 /*******************************************************
18 * General purpose constants and macros.
19 *******************************************************/
20 #define UINT16_T_MAX 0xffff /* Maximum 16 bit unsigned. */
21 #define UINT32_T_MAX 0xffffffff /* Maximum 32 bit unsigned. */
23 #define DB_MIN_PGSIZE 0x000200 /* Minimum page size. */
24 #define DB_MAX_PGSIZE 0x010000 /* Maximum page size. */
26 #define DB_MINCACHE 10 /* Minimum cached pages */
28 #define MEGABYTE 1048576
31 * If we are unable to determine the underlying filesystem block size, use
32 * 8K on the grounds that most OS's use less than 8K as their VM page size.
34 #define DB_DEF_IOSIZE (8 * 1024)
37 * Aligning items to particular sizes or in pages or memory. ALIGNP is a
38 * separate macro, as we've had to cast the pointer to different integral
39 * types on different architectures.
41 * We cast pointers into unsigned longs when manipulating them because C89
42 * guarantees that u_long is the largest available integral type and further,
43 * to never generate overflows. However, neither C89 or C9X requires that
44 * any integer type be large enough to hold a pointer, although C9X created
45 * the intptr_t type, which is guaranteed to hold a pointer but may or may
46 * not exist. At some point in the future, we should test for intptr_t and
47 * use it where available.
49 #undef ALIGNTYPE
50 #define ALIGNTYPE u_long
51 #undef ALIGNP
52 #define ALIGNP(value, bound) ALIGN((ALIGNTYPE)value, bound)
53 #undef ALIGN
54 #define ALIGN(value, bound) (((value) + (bound) - 1) & ~((bound) - 1))
57 * There are several on-page structures that are declared to have a number of
58 * fields followed by a variable length array of items. The structure size
59 * without including the variable length array or the address of the first of
60 * those elements can be found using SSZ.
62 * This macro can also be used to find the offset of a structure element in a
63 * structure. This is used in various places to copy structure elements from
64 * unaligned memory references, e.g., pointers into a packed page.
66 * There are two versions because compilers object if you take the address of
67 * an array.
69 #undef SSZ
70 #define SSZ(name, field) ((int)&(((name *)0)->field))
72 #undef SSZA
73 #define SSZA(name, field) ((int)&(((name *)0)->field[0]))
75 /* Macros to return per-process address, offsets based on shared regions. */
76 #define R_ADDR(base, offset) ((void *)((u_int8_t *)((base)->addr) + offset))
77 #define R_OFFSET(base, p) ((u_int8_t *)(p) - (u_int8_t *)(base)->addr)
79 #define DB_DEFAULT 0x000000 /* No flag was specified. */
81 /* Structure used to print flag values. */
82 typedef struct __fn {
83 u_int32_t mask; /* Flag value. */
84 const char *name; /* Flag name. */
85 } FN;
87 /* Set, clear and test flags. */
88 #define F_SET(p, f) (p)->flags |= (f)
89 #define F_CLR(p, f) (p)->flags &= ~(f)
90 #define F_ISSET(p, f) ((p)->flags & (f))
91 #define LF_SET(f) (flags |= (f))
92 #define LF_CLR(f) (flags &= ~(f))
93 #define LF_ISSET(f) (flags & (f))
96 * Panic check:
97 * All interfaces check the panic flag, if it's set, the tree is dead.
99 #define DB_PANIC_CHECK(dbp) { \
100 if ((dbp)->dbenv != NULL && (dbp)->dbenv->db_panic != 0) \
101 return (DB_RUNRECOVERY); \
104 /* Display separator string. */
105 #undef DB_LINE
106 #define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
108 /* Unused, or not-used-yet variable. "Shut that bloody compiler up!" */
109 #define COMPQUIET(n, v) (n) = (v)
112 * Purify and similar run-time tools complain about unitialized reads/writes
113 * for structure fields whose only purpose is padding.
115 #define UMRW(v) (v) = 0
118 * Win16 needs specific syntax on callback functions. Nobody else cares.
120 #ifndef DB_CALLBACK
121 #define DB_CALLBACK /* Nothing. */
122 #endif
124 /*******************************************************
125 * Files.
126 *******************************************************/
128 * We use 1024 as the maximum path length. It's too hard to figure out what
129 * the real path length is, as it was traditionally stored in <sys/param.h>,
130 * and that file isn't always available.
132 #undef MAXPATHLEN
133 #define MAXPATHLEN 1024
135 #define PATH_DOT "." /* Current working directory. */
136 #define PATH_SEPARATOR "/" /* Path separator character. */
138 /*******************************************************
139 * Mutex support.
140 *******************************************************/
141 typedef u_int32_t tsl_t;
144 * !!!
145 * Various systems require different alignments for mutexes (the worst we've
146 * seen so far is 16-bytes on some HP architectures). The mutex (tsl_t) must
147 * be first in the db_mutex_t structure, which must itself be first in the
148 * region. This ensures the alignment is as returned by mmap(2), which should
149 * be sufficient. All other mutex users must ensure proper alignment locally.
151 #define MUTEX_ALIGNMENT 1
154 * The offset of a mutex in memory.
156 * !!!
157 * Not an off_t, so backing file offsets MUST be less than 4Gb. See the
158 * off field of the db_mutex_t as well.
160 #define MUTEX_LOCK_OFFSET(a, b) ((u_int32_t)((u_int8_t *)b - (u_int8_t *)a))
162 typedef struct _db_mutex_t {
163 #ifdef HAVE_SPINLOCKS
164 tsl_t tsl_resource; /* Resource test and set. */
165 #ifdef DIAGNOSTIC
166 u_int32_t pid; /* Lock holder: 0 or process pid. */
167 #endif
168 #else
169 u_int32_t off; /* Backing file offset. */
170 u_int32_t pid; /* Lock holder: 0 or process pid. */
171 #endif
172 u_int32_t spins; /* Spins before block. */
173 u_int32_t mutex_set_wait; /* Granted after wait. */
174 u_int32_t mutex_set_nowait; /* Granted without waiting. */
175 } db_mutex_t;
177 #include "mutex_ext.h"
179 /*******************************************************
180 * Access methods.
181 *******************************************************/
182 /* Lock/unlock a DB thread. */
183 #define DB_THREAD_LOCK(dbp) \
184 if (F_ISSET(dbp, DB_AM_THREAD)) \
185 (void)__db_mutex_lock((db_mutex_t *)(dbp)->mutexp, -1);
186 #define DB_THREAD_UNLOCK(dbp) \
187 if (F_ISSET(dbp, DB_AM_THREAD)) \
188 (void)__db_mutex_unlock((db_mutex_t *)(dbp)->mutexp, -1);
190 /*******************************************************
191 * Environment.
192 *******************************************************/
193 /* Type passed to __db_appname(). */
194 typedef enum {
195 DB_APP_NONE=0, /* No type (region). */
196 DB_APP_DATA, /* Data file. */
197 DB_APP_LOG, /* Log file. */
198 DB_APP_TMP /* Temporary file. */
199 } APPNAME;
201 /*******************************************************
202 * Shared memory regions.
203 *******************************************************/
205 * The shared memory regions share an initial structure so that the general
206 * region code can handle races between the region being deleted and other
207 * processes waiting on the region mutex.
209 * !!!
210 * Note, the mutex must be the first entry in the region; see comment above.
212 typedef struct _rlayout {
213 db_mutex_t lock; /* Region mutex. */
214 #define DB_REGIONMAGIC 0x120897
215 u_int32_t valid; /* Valid magic number. */
216 u_int32_t refcnt; /* Region reference count. */
217 size_t size; /* Region length. */
218 int majver; /* Major version number. */
219 int minver; /* Minor version number. */
220 int patch; /* Patch version number. */
221 int panic; /* Region is dead. */
222 #define INVALID_SEGID -1
223 int segid; /* shmget(2) ID, or Win16 segment ID. */
225 #define REGION_ANONYMOUS 0x01 /* Region is/should be in anon mem. */
226 u_int32_t flags;
227 } RLAYOUT;
230 * DB creates all regions on 4K boundaries out of sheer paranoia, so that
231 * we don't make the underlying VM unhappy.
233 #define DB_VMPAGESIZE (4 * 1024)
234 #define DB_ROUNDOFF(n, round) { \
235 (n) += (round) - 1; \
236 (n) -= (n) % (round); \
240 * The interface to region attach is nasty, there is a lot of complex stuff
241 * going on, which has to be retained between create/attach and detach. The
242 * REGINFO structure keeps track of it.
244 struct __db_reginfo; typedef struct __db_reginfo REGINFO;
245 struct __db_reginfo {
246 /* Arguments. */
247 DB_ENV *dbenv; /* Region naming info. */
248 APPNAME appname; /* Region naming info. */
249 char *path; /* Region naming info. */
250 const char *file; /* Region naming info. */
251 int mode; /* Region mode, if a file. */
252 size_t size; /* Region size. */
253 u_int32_t dbflags; /* Region file open flags, if a file. */
255 /* Results. */
256 char *name; /* Region name. */
257 void *addr; /* Region address. */
258 int fd; /* Fcntl(2) locking file descriptor.
259 NB: this is only valid if a regular
260 file is backing the shared region,
261 and mmap(2) is being used to map it
262 into our address space. */
263 int segid; /* shmget(2) ID, or Win16 segment ID. */
264 void *wnt_handle; /* Win/NT HANDLE. */
266 /* Shared flags. */
267 /* 0x0001 COMMON MASK with RLAYOUT structure. */
268 #define REGION_CANGROW 0x0002 /* Can grow. */
269 #define REGION_CREATED 0x0004 /* Created. */
270 #define REGION_HOLDINGSYS 0x0008 /* Holding system resources. */
271 #define REGION_LASTDETACH 0x0010 /* Delete on last detach. */
272 #define REGION_MALLOC 0x0020 /* Created in malloc'd memory. */
273 #define REGION_PRIVATE 0x0040 /* Private to thread/process. */
274 #define REGION_REMOVED 0x0080 /* Already deleted. */
275 #define REGION_SIZEDEF 0x0100 /* Use default region size if exists. */
276 u_int32_t flags;
279 /*******************************************************
280 * Mpool.
281 *******************************************************/
283 * File types for DB access methods. Negative numbers are reserved to DB.
285 #define DB_FTYPE_BTREE -1 /* Btree. */
286 #define DB_FTYPE_HASH -2 /* Hash. */
288 /* Structure used as the DB pgin/pgout pgcookie. */
289 typedef struct __dbpginfo {
290 size_t db_pagesize; /* Underlying page size. */
291 int needswap; /* If swapping required. */
292 } DB_PGINFO;
294 /*******************************************************
295 * Log.
296 *******************************************************/
297 /* Initialize an LSN to 'zero'. */
298 #define ZERO_LSN(LSN) { \
299 (LSN).file = 0; \
300 (LSN).offset = 0; \
303 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
304 #define IS_ZERO_LSN(LSN) ((LSN).file == 0)
306 /* Test if we need to log a change. */
307 #define DB_LOGGING(dbc) \
308 (F_ISSET((dbc)->dbp, DB_AM_LOGGING) && !F_ISSET(dbc, DBC_RECOVER))
310 #ifdef DIAGNOSTIC
312 * Debugging macro to log operations.
313 * If DEBUG_WOP is defined, log operations that modify the database.
314 * If DEBUG_ROP is defined, log operations that read the database.
316 * D dbp
317 * T txn
318 * O operation (string)
319 * K key
320 * A data
321 * F flags
323 #define LOG_OP(C, T, O, K, A, F) { \
324 DB_LSN _lsn; \
325 DBT _op; \
326 if (DB_LOGGING((C))) { \
327 memset(&_op, 0, sizeof(_op)); \
328 _op.data = O; \
329 _op.size = strlen(O) + 1; \
330 (void)__db_debug_log((C)->dbp->dbenv->lg_info, \
331 T, &_lsn, 0, &_op, (C)->dbp->log_fileid, K, A, F); \
334 #ifdef DEBUG_ROP
335 #define DEBUG_LREAD(C, T, O, K, A, F) LOG_OP(C, T, O, K, A, F)
336 #else
337 #define DEBUG_LREAD(C, T, O, K, A, F)
338 #endif
339 #ifdef DEBUG_WOP
340 #define DEBUG_LWRITE(C, T, O, K, A, F) LOG_OP(C, T, O, K, A, F)
341 #else
342 #define DEBUG_LWRITE(C, T, O, K, A, F)
343 #endif
344 #else
345 #define DEBUG_LREAD(C, T, O, K, A, F)
346 #define DEBUG_LWRITE(C, T, O, K, A, F)
347 #endif /* DIAGNOSTIC */
349 /*******************************************************
350 * Transactions and recovery.
351 *******************************************************/
353 * Out of band value for a lock. The locks are returned to callers as offsets
354 * into the lock regions. Since the RLAYOUT structure begins all regions, an
355 * offset of 0 is guaranteed not to be a valid lock.
357 #define LOCK_INVALID 0
359 /* The structure allocated for every transaction. */
360 struct __db_txn {
361 DB_TXNMGR *mgrp; /* Pointer to transaction manager. */
362 DB_TXN *parent; /* Pointer to transaction's parent. */
363 DB_LSN last_lsn; /* Lsn of last log write. */
364 u_int32_t txnid; /* Unique transaction id. */
365 size_t off; /* Detail structure within region. */
366 TAILQ_ENTRY(__db_txn) links; /* Links transactions off manager. */
367 TAILQ_HEAD(__kids, __db_txn) kids; /* Child transactions. */
368 TAILQ_ENTRY(__db_txn) klinks; /* Links child transactions. */
370 #define TXN_MALLOC 0x01 /* Structure allocated by TXN system. */
371 u_int32_t flags;
374 /*******************************************************
375 * Global variables.
376 *******************************************************/
378 * !!!
379 * Initialized in os/os_config.c, don't change this unless you change it
380 * as well.
383 struct __rmname {
384 char *dbhome;
385 int rmid;
386 TAILQ_ENTRY(__rmname) links;
389 typedef struct __db_globals {
390 int db_mutexlocks; /* DB_MUTEXLOCKS */
391 int db_pageyield; /* DB_PAGEYIELD */
392 int db_region_anon; /* DB_REGION_ANON, DB_REGION_NAME */
393 int db_region_init; /* DB_REGION_INIT */
394 int db_tsl_spins; /* DB_TSL_SPINS */
395 /* XA: list of opened environments. */
396 TAILQ_HEAD(__db_envq, __db_env) db_envq;
397 /* XA: list of id to dbhome mappings. */
398 TAILQ_HEAD(__db_nameq, __rmname) db_nameq;
399 } DB_GLOBALS;
401 extern DB_GLOBALS __db_global_values;
402 #define DB_GLOBAL(v) __db_global_values.v
404 #include "os.h"
405 #include "os_ext.h"
407 #endif /* !_DB_INTERNAL_H_ */