2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
7 * @(#)db_int.h.src 10.28 (Sleepycat) 8/20/97
10 #ifndef _DB_INTERNAL_H_
11 #define _DB_INTERNAL_H_
13 #include "db.h" /* Standard DB include file. */
17 /*******************************************************
18 * General purpose constants and macros.
19 *******************************************************/
20 #define UINT32_T_MAX 0xffffffff /* Maximum 32 bit unsigned. */
21 #define UINT16_T_MAX 0xffff /* Maximum 16 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 /* Handle `errno' in the presence of multi-threading correctly. On some
29 systems we need a special macro to do this right. */
31 # define __set_errno(val) (errno) = (val)
35 * Aligning items to particular sizes or in pages or memory. ALIGNP is a
36 * separate macro, as we've had to cast the pointer to different integral
37 * types on different architectures.
39 * We cast pointers into unsigned longs when manipulating them because C89
40 * guarantees that u_long is the largest available integral type and further,
41 * to never generate overflows. However, neither C89 or C9X requires that
42 * any integer type be large enough to hold a pointer, although C9X created
43 * the intptr_t type, which is guaranteed to hold a pointer but may or may
44 * not exist. At some point in the future, we should test for intptr_t and
45 * use it where available.
48 #define ALIGNTYPE u_long
50 #define ALIGNP(value, bound) ALIGN((ALIGNTYPE)value, bound)
52 #define ALIGN(value, bound) (((value) + (bound) - 1) & ~((bound) - 1))
55 * There are several on-page structures that are declared to have a number of
56 * fields followed by a variable length array of items. The structure size
57 * without including the variable length array or the address of the first of
58 * those elements can be found using SSZ.
60 * This macro can also be used to find the offset of a structure element in a
61 * structure. This is used in various places to copy structure elements from
62 * unaligned memory references, e.g., pointers into a packed page.
64 * There are two versions because compilers object if you take the address of
68 #define SSZ(name, field) ((int)&(((name *)0)->field))
71 #define SSZA(name, field) ((int)&(((name *)0)->field[0]))
73 /* Free and free-string macros that overwrite memory during debugging. */
76 #define FREE(p, len) { \
77 memset(p, 0xff, len); \
86 #define FREE(p, len) { \
95 /* Structure used to print flag values. */
97 u_int32_t mask; /* Flag value. */
98 const char *name; /* Flag name. */
101 /* Set, clear and test flags. */
102 #define F_SET(p, f) (p)->flags |= (f)
103 #define F_CLR(p, f) (p)->flags &= ~(f)
104 #define F_ISSET(p, f) ((p)->flags & (f))
105 #define LF_SET(f) (flags |= (f))
106 #define LF_CLR(f) (flags &= ~(f))
107 #define LF_ISSET(f) (flags & (f))
109 /* Display separator string. */
111 #define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
113 /*******************************************************
115 *******************************************************/
116 #ifndef MAXPATHLEN /* Maximum path length. */
118 #define MAXPATHLEN PATH_MAX
120 #define MAXPATHLEN 1024
124 #define PATH_DOT "." /* Current working directory. */
125 #define PATH_SEPARATOR "/" /* Path separator character. */
127 #ifndef S_IRUSR /* UNIX specific file permissions. */
128 #define S_IRUSR 0000400 /* R for owner */
129 #define S_IWUSR 0000200 /* W for owner */
130 #define S_IRGRP 0000040 /* R for group */
131 #define S_IWGRP 0000020 /* W for group */
132 #define S_IROTH 0000004 /* R for other */
133 #define S_IWOTH 0000002 /* W for other */
136 #ifndef S_ISDIR /* UNIX specific: directory test. */
137 #define S_ISDIR(m) ((m & 0170000) == 0040000)
140 /*******************************************************
142 *******************************************************/
143 typedef unsigned char tsl_t;
149 * Various systems require different alignments for mutexes (the worst we've
150 * seen so far is 16-bytes on some HP architectures). The mutex (tsl_t) must
151 * be first in the db_mutex_t structure, which must itself be first in the
152 * region. This ensures the alignment is as returned by mmap(2), which should
153 * be sufficient. All other mutex users must ensure proper alignment locally.
155 #define MUTEX_ALIGNMENT 1
158 * The offset of a mutex in memory.
160 #define MUTEX_LOCK_OFFSET(a, b) ((off_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. */
166 u_long pid; /* Lock holder: 0 or process pid. */
169 off_t off; /* Backing file offset. */
170 u_long pid; /* Lock holder: 0 or process pid. */
172 #ifdef MUTEX_STATISTICS
173 u_long mutex_set_wait; /* Blocking mutex: required waiting. */
174 u_long mutex_set_nowait; /* Blocking mutex: without waiting. */
178 #include "mutex_ext.h"
180 /*******************************************************
182 *******************************************************/
183 /* Lock/unlock a DB thread. */
184 #define DB_THREAD_LOCK(dbp) \
185 (F_ISSET(dbp, DB_AM_THREAD) ? \
186 __db_mutex_lock((db_mutex_t *)(dbp)->mutex, -1, \
187 (dbp)->dbenv == NULL ? NULL : (dbp)->dbenv->db_yield) : 0)
188 #define DB_THREAD_UNLOCK(dbp) \
189 (F_ISSET(dbp, DB_AM_THREAD) ? \
190 __db_mutex_unlock((db_mutex_t *)(dbp)->mutex, -1) : 0)
192 /* Btree/recno local statistics structure. */
193 struct __db_bt_lstat; typedef struct __db_bt_lstat DB_BTREE_LSTAT;
194 struct __db_bt_lstat {
195 u_int32_t bt_freed; /* Pages freed for reuse. */
196 u_int32_t bt_pfxsaved; /* Bytes saved by prefix compression. */
197 u_int32_t bt_split; /* Total number of splits. */
198 u_int32_t bt_rootsplit; /* Root page splits. */
199 u_int32_t bt_fastsplit; /* Fast splits. */
200 u_int32_t bt_added; /* Items added. */
201 u_int32_t bt_deleted; /* Items deleted. */
202 u_int32_t bt_get; /* Items retrieved. */
203 u_int32_t bt_cache_hit; /* Hits in fast-insert code. */
204 u_int32_t bt_cache_miss; /* Misses in fast-insert code. */
207 /*******************************************************
209 *******************************************************/
210 /* Type passed to __db_appname(). */
212 DB_APP_NONE=0, /* No type (region). */
213 DB_APP_DATA, /* Data file. */
214 DB_APP_LOG, /* Log file. */
215 DB_APP_TMP /* Temporary file. */
218 /*******************************************************
220 *******************************************************/
222 * The shared memory regions share an initial structure so that the general
223 * region code can handle races between the region being deleted and other
224 * processes waiting on the region mutex.
227 * Note, the mutex must be the first entry in the region; see comment above.
229 typedef struct _rlayout {
230 db_mutex_t lock; /* Region mutex. */
231 u_int32_t refcnt; /* Region reference count. */
232 size_t size; /* Region length. */
233 int majver; /* Major version number. */
234 int minver; /* Minor version number. */
235 int patch; /* Patch version number. */
237 #define DB_R_DELETED 0x01 /* Region was deleted. */
241 /*******************************************************
243 *******************************************************/
245 * File types for DB access methods. Negative numbers are reserved to DB.
247 #define DB_FTYPE_BTREE -1 /* Btree. */
248 #define DB_FTYPE_HASH -2 /* Hash. */
250 /* Structure used as the DB pgin/pgout pgcookie. */
251 typedef struct __dbpginfo {
252 size_t db_pagesize; /* Underlying page size. */
253 int needswap; /* If swapping required. */
256 /*******************************************************
258 *******************************************************/
259 /* Initialize an LSN to 'zero'. */
260 #define ZERO_LSN(LSN) { \
265 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
266 #define IS_ZERO_LSN(LSN) ((LSN).file == 0)
268 /* Test if we need to log a change. */
269 #define DB_LOGGING(dbp) \
270 (F_ISSET(dbp, DB_AM_LOGGING) && !F_ISSET(dbp, DB_AM_RECOVER))
274 * Debugging macro to log operations.
275 * If DEBUG_WOP is defined, log operations that modify the database.
276 * If DEBUG_ROP is defined, log operations that read the database.
280 * O operation (string)
285 #define LOG_OP(D, T, O, K, A, F) { \
288 if (DB_LOGGING((D))) { \
289 memset(&_op, 0, sizeof(_op)); \
291 _op.size = strlen(O) + 1; \
292 (void)__db_debug_log((D)->dbenv->lg_info, \
293 T, &_lsn, 0, &_op, (D)->log_fileid, K, A, F); \
297 #define DEBUG_LREAD(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
299 #define DEBUG_LREAD(D, T, O, K, A, F)
302 #define DEBUG_LWRITE(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
304 #define DEBUG_LWRITE(D, T, O, K, A, F)
307 #define DEBUG_LREAD(D, T, O, K, A, F)
308 #define DEBUG_LWRITE(D, T, O, K, A, F)
311 /*******************************************************
312 * Transactions and recovery.
313 *******************************************************/
315 * The locker id space is divided between the transaction manager and the lock
316 * manager. Lockid's start at 0 and go to MAX_LOCKER_ID. Txn Id's start at
317 * MAX_LOCKER_ID + 1 and go up to MAX_TXNID.
319 #define MAX_LOCKER_ID 0x0fffffff
320 #define MAX_TXNID 0xffffffff
323 * Out of band value for a lock. The locks are returned to callers as offsets
324 * into the lock regions. Since the RLAYOUT structure begins all regions, an
325 * offset of 0 is guaranteed not to be a valid lock.
327 #define LOCK_INVALID 0
329 /* The structure allocated for every transaction. */
331 DB_TXNMGR *mgrp; /* Pointer to transaction manager. */
332 DB_TXN *parent; /* Pointer to transaction's parent. */
333 DB_LSN last_lsn; /* Lsn of last log write. */
334 u_int32_t txnid; /* Unique transaction id. */
335 size_t off; /* Detail structure within region. */
336 TAILQ_ENTRY(__db_txn) links;
338 #endif /* !_DB_INTERNAL_H_ */