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.37 (Sleepycat) 11/25/97
10 #ifndef _DB_INTERNAL_H_
11 #define _DB_INTERNAL_H_
13 #include "db.h" /* Standard DB include file. */
18 /*******************************************************
19 * General purpose constants and macros.
20 *******************************************************/
21 #define UINT32_T_MAX 0xffffffff /* Maximum 32 bit unsigned. */
22 #define UINT16_T_MAX 0xffff /* Maximum 16 bit unsigned. */
24 #define DB_MIN_PGSIZE 0x000200 /* Minimum page size. */
25 #define DB_MAX_PGSIZE 0x010000 /* Maximum page size. */
27 #define DB_MINCACHE 10 /* Minimum cached pages */
30 * Aligning items to particular sizes or in pages or memory. ALIGNP is a
31 * separate macro, as we've had to cast the pointer to different integral
32 * types on different architectures.
34 * We cast pointers into unsigned longs when manipulating them because C89
35 * guarantees that u_long is the largest available integral type and further,
36 * to never generate overflows. However, neither C89 or C9X requires that
37 * any integer type be large enough to hold a pointer, although C9X created
38 * the intptr_t type, which is guaranteed to hold a pointer but may or may
39 * not exist. At some point in the future, we should test for intptr_t and
40 * use it where available.
43 #define ALIGNTYPE u_long
45 #define ALIGNP(value, bound) ALIGN((ALIGNTYPE)value, bound)
47 #define ALIGN(value, bound) (((value) + (bound) - 1) & ~((bound) - 1))
50 * There are several on-page structures that are declared to have a number of
51 * fields followed by a variable length array of items. The structure size
52 * without including the variable length array or the address of the first of
53 * those elements can be found using SSZ.
55 * This macro can also be used to find the offset of a structure element in a
56 * structure. This is used in various places to copy structure elements from
57 * unaligned memory references, e.g., pointers into a packed page.
59 * There are two versions because compilers object if you take the address of
63 #define SSZ(name, field) ((int)&(((name *)0)->field))
66 #define SSZA(name, field) ((int)&(((name *)0)->field[0]))
68 /* Macros to return per-process address, offsets based on shared regions. */
69 #define R_ADDR(base, offset) ((void *)((u_int8_t *)((base)->addr) + offset))
70 #define R_OFFSET(base, p) ((u_int8_t *)(p) - (u_int8_t *)(base)->addr)
72 /* Free and free-string macros that overwrite memory during debugging. */
75 #define FREE(p, len) { \
76 memset(p, 0xff, len); \
85 #define FREE(p, len) { \
94 /* Structure used to print flag values. */
96 u_int32_t mask; /* Flag value. */
97 const char *name; /* Flag name. */
100 /* Set, clear and test flags. */
101 #define F_SET(p, f) (p)->flags |= (f)
102 #define F_CLR(p, f) (p)->flags &= ~(f)
103 #define F_ISSET(p, f) ((p)->flags & (f))
104 #define LF_SET(f) (flags |= (f))
105 #define LF_CLR(f) (flags &= ~(f))
106 #define LF_ISSET(f) (flags & (f))
108 /* Display separator string. */
110 #define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
112 /*******************************************************
114 *******************************************************/
115 #ifndef MAXPATHLEN /* Maximum path length. */
117 #define MAXPATHLEN PATH_MAX
119 #define MAXPATHLEN 1024
123 #define PATH_DOT "." /* Current working directory. */
124 #define PATH_SEPARATOR "/" /* Path separator character. */
126 #ifndef S_IRUSR /* UNIX specific file permissions. */
127 #define S_IRUSR 0000400 /* R for owner */
128 #define S_IWUSR 0000200 /* W for owner */
129 #define S_IRGRP 0000040 /* R for group */
130 #define S_IWGRP 0000020 /* W for group */
131 #define S_IROTH 0000004 /* R for other */
132 #define S_IWOTH 0000002 /* W for other */
135 #ifndef S_ISDIR /* UNIX specific: directory test. */
136 #define S_ISDIR(m) ((m & 0170000) == 0040000)
139 /*******************************************************
141 *******************************************************/
142 typedef unsigned char tsl_t;
148 * Various systems require different alignments for mutexes (the worst we've
149 * seen so far is 16-bytes on some HP architectures). The mutex (tsl_t) must
150 * be first in the db_mutex_t structure, which must itself be first in the
151 * region. This ensures the alignment is as returned by mmap(2), which should
152 * be sufficient. All other mutex users must ensure proper alignment locally.
154 #define MUTEX_ALIGNMENT 1
157 * The offset of a mutex in memory.
159 #define MUTEX_LOCK_OFFSET(a, b) ((off_t)((u_int8_t *)b - (u_int8_t *)a))
161 typedef struct _db_mutex_t {
162 #ifdef HAVE_SPINLOCKS
163 tsl_t tsl_resource; /* Resource test and set. */
165 u_long pid; /* Lock holder: 0 or process pid. */
168 off_t off; /* Backing file offset. */
169 u_long pid; /* Lock holder: 0 or process pid. */
171 u_int32_t spins; /* Spins before block. */
172 u_int32_t mutex_set_wait; /* Granted after wait. */
173 u_int32_t mutex_set_nowait; /* Granted without waiting. */
176 #include "mutex_ext.h"
178 /*******************************************************
180 *******************************************************/
181 /* Lock/unlock a DB thread. */
182 #define DB_THREAD_LOCK(dbp) \
183 (F_ISSET(dbp, DB_AM_THREAD) ? \
184 __db_mutex_lock((db_mutex_t *)(dbp)->mutexp, -1) : 0)
185 #define DB_THREAD_UNLOCK(dbp) \
186 (F_ISSET(dbp, DB_AM_THREAD) ? \
187 __db_mutex_unlock((db_mutex_t *)(dbp)->mutexp, -1) : 0)
189 /* Btree/recno local statistics structure. */
190 struct __db_bt_lstat; typedef struct __db_bt_lstat DB_BTREE_LSTAT;
191 struct __db_bt_lstat {
192 u_int32_t bt_freed; /* Pages freed for reuse. */
193 u_int32_t bt_pfxsaved; /* Bytes saved by prefix compression. */
194 u_int32_t bt_split; /* Total number of splits. */
195 u_int32_t bt_rootsplit; /* Root page splits. */
196 u_int32_t bt_fastsplit; /* Fast splits. */
197 u_int32_t bt_added; /* Items added. */
198 u_int32_t bt_deleted; /* Items deleted. */
199 u_int32_t bt_get; /* Items retrieved. */
200 u_int32_t bt_cache_hit; /* Hits in fast-insert code. */
201 u_int32_t bt_cache_miss; /* Misses in fast-insert code. */
204 /*******************************************************
206 *******************************************************/
207 /* Type passed to __db_appname(). */
209 DB_APP_NONE=0, /* No type (region). */
210 DB_APP_DATA, /* Data file. */
211 DB_APP_LOG, /* Log file. */
212 DB_APP_TMP /* Temporary file. */
215 /*******************************************************
217 *******************************************************/
219 * The shared memory regions share an initial structure so that the general
220 * region code can handle races between the region being deleted and other
221 * processes waiting on the region mutex.
224 * Note, the mutex must be the first entry in the region; see comment above.
226 typedef struct _rlayout {
227 db_mutex_t lock; /* Region mutex. */
228 u_int32_t refcnt; /* Region reference count. */
229 size_t size; /* Region length. */
230 int majver; /* Major version number. */
231 int minver; /* Minor version number. */
232 int patch; /* Patch version number. */
234 #define DB_R_DELETED 0x01 /* Region was deleted. */
238 /*******************************************************
240 *******************************************************/
242 * File types for DB access methods. Negative numbers are reserved to DB.
244 #define DB_FTYPE_BTREE -1 /* Btree. */
245 #define DB_FTYPE_HASH -2 /* Hash. */
247 /* Structure used as the DB pgin/pgout pgcookie. */
248 typedef struct __dbpginfo {
249 size_t db_pagesize; /* Underlying page size. */
250 int needswap; /* If swapping required. */
253 /*******************************************************
255 *******************************************************/
256 /* Initialize an LSN to 'zero'. */
257 #define ZERO_LSN(LSN) { \
262 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
263 #define IS_ZERO_LSN(LSN) ((LSN).file == 0)
265 /* Test if we need to log a change. */
266 #define DB_LOGGING(dbp) \
267 (F_ISSET(dbp, DB_AM_LOGGING) && !F_ISSET(dbp, DB_AM_RECOVER))
271 * Debugging macro to log operations.
272 * If DEBUG_WOP is defined, log operations that modify the database.
273 * If DEBUG_ROP is defined, log operations that read the database.
277 * O operation (string)
282 #define LOG_OP(D, T, O, K, A, F) { \
285 if (DB_LOGGING((D))) { \
286 memset(&_op, 0, sizeof(_op)); \
288 _op.size = strlen(O) + 1; \
289 (void)__db_debug_log((D)->dbenv->lg_info, \
290 T, &_lsn, 0, &_op, (D)->log_fileid, K, A, F); \
294 #define DEBUG_LREAD(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
296 #define DEBUG_LREAD(D, T, O, K, A, F)
299 #define DEBUG_LWRITE(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
301 #define DEBUG_LWRITE(D, T, O, K, A, F)
304 #define DEBUG_LREAD(D, T, O, K, A, F)
305 #define DEBUG_LWRITE(D, T, O, K, A, F)
308 /*******************************************************
309 * Transactions and recovery.
310 *******************************************************/
312 * Out of band value for a lock. The locks are returned to callers as offsets
313 * into the lock regions. Since the RLAYOUT structure begins all regions, an
314 * offset of 0 is guaranteed not to be a valid lock.
316 #define LOCK_INVALID 0
318 /* The structure allocated for every transaction. */
320 DB_TXNMGR *mgrp; /* Pointer to transaction manager. */
321 DB_TXN *parent; /* Pointer to transaction's parent. */
322 DB_LSN last_lsn; /* Lsn of last log write. */
323 u_int32_t txnid; /* Unique transaction id. */
324 size_t off; /* Detail structure within region. */
325 TAILQ_ENTRY(__db_txn) links;
327 #endif /* !_DB_INTERNAL_H_ */