Update.
[glibc.git] / db2 / db_int.h
blob56dfddb73f557ee9a4f3d913aee112b02450023c
1 /*-
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.30 (Sleepycat) 9/23/97
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 "os_ext.h"
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 */
29 * Aligning items to particular sizes or in pages or memory. ALIGNP is a
30 * separate macro, as we've had to cast the pointer to different integral
31 * types on different architectures.
33 * We cast pointers into unsigned longs when manipulating them because C89
34 * guarantees that u_long is the largest available integral type and further,
35 * to never generate overflows. However, neither C89 or C9X requires that
36 * any integer type be large enough to hold a pointer, although C9X created
37 * the intptr_t type, which is guaranteed to hold a pointer but may or may
38 * not exist. At some point in the future, we should test for intptr_t and
39 * use it where available.
41 #undef ALIGNTYPE
42 #define ALIGNTYPE u_long
43 #undef ALIGNP
44 #define ALIGNP(value, bound) ALIGN((ALIGNTYPE)value, bound)
45 #undef ALIGN
46 #define ALIGN(value, bound) (((value) + (bound) - 1) & ~((bound) - 1))
49 * There are several on-page structures that are declared to have a number of
50 * fields followed by a variable length array of items. The structure size
51 * without including the variable length array or the address of the first of
52 * those elements can be found using SSZ.
54 * This macro can also be used to find the offset of a structure element in a
55 * structure. This is used in various places to copy structure elements from
56 * unaligned memory references, e.g., pointers into a packed page.
58 * There are two versions because compilers object if you take the address of
59 * an array.
61 #undef SSZ
62 #define SSZ(name, field) ((int)&(((name *)0)->field))
64 #undef SSZA
65 #define SSZA(name, field) ((int)&(((name *)0)->field[0]))
67 /* Free and free-string macros that overwrite memory during debugging. */
68 #ifdef DEBUG
69 #undef FREE
70 #define FREE(p, len) { \
71 memset(p, 0xff, len); \
72 free(p); \
74 #undef FREES
75 #define FREES(p) { \
76 FREE(p, strlen(p)); \
78 #else
79 #undef FREE
80 #define FREE(p, len) { \
81 free(p); \
83 #undef FREES
84 #define FREES(p) { \
85 free(p); \
87 #endif
89 /* Structure used to print flag values. */
90 typedef struct __fn {
91 u_int32_t mask; /* Flag value. */
92 const char *name; /* Flag name. */
93 } FN;
95 /* Set, clear and test flags. */
96 #define F_SET(p, f) (p)->flags |= (f)
97 #define F_CLR(p, f) (p)->flags &= ~(f)
98 #define F_ISSET(p, f) ((p)->flags & (f))
99 #define LF_SET(f) (flags |= (f))
100 #define LF_CLR(f) (flags &= ~(f))
101 #define LF_ISSET(f) (flags & (f))
103 /* Display separator string. */
104 #undef DB_LINE
105 #define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
107 /*******************************************************
108 * Files.
109 *******************************************************/
110 #ifndef MAXPATHLEN /* Maximum path length. */
111 #ifdef PATH_MAX
112 #define MAXPATHLEN PATH_MAX
113 #else
114 #define MAXPATHLEN 1024
115 #endif
116 #endif
118 #define PATH_DOT "." /* Current working directory. */
119 #define PATH_SEPARATOR "/" /* Path separator character. */
121 #ifndef S_IRUSR /* UNIX specific file permissions. */
122 #define S_IRUSR 0000400 /* R for owner */
123 #define S_IWUSR 0000200 /* W for owner */
124 #define S_IRGRP 0000040 /* R for group */
125 #define S_IWGRP 0000020 /* W for group */
126 #define S_IROTH 0000004 /* R for other */
127 #define S_IWOTH 0000002 /* W for other */
128 #endif
130 #ifndef S_ISDIR /* UNIX specific: directory test. */
131 #define S_ISDIR(m) ((m & 0170000) == 0040000)
132 #endif
134 /*******************************************************
135 * Mutex support.
136 *******************************************************/
137 typedef unsigned char tsl_t;
142 * !!!
143 * Various systems require different alignments for mutexes (the worst we've
144 * seen so far is 16-bytes on some HP architectures). The mutex (tsl_t) must
145 * be first in the db_mutex_t structure, which must itself be first in the
146 * region. This ensures the alignment is as returned by mmap(2), which should
147 * be sufficient. All other mutex users must ensure proper alignment locally.
149 #define MUTEX_ALIGNMENT 1
152 * The offset of a mutex in memory.
154 #define MUTEX_LOCK_OFFSET(a, b) ((off_t)((u_int8_t *)b - (u_int8_t *)a))
156 typedef struct _db_mutex_t {
157 #ifdef HAVE_SPINLOCKS
158 tsl_t tsl_resource; /* Resource test and set. */
159 #ifdef DEBUG
160 u_long pid; /* Lock holder: 0 or process pid. */
161 #endif
162 #else
163 off_t off; /* Backing file offset. */
164 u_long pid; /* Lock holder: 0 or process pid. */
165 #endif
166 #ifdef MUTEX_STATISTICS
167 u_long mutex_set_wait; /* Blocking mutex: required waiting. */
168 u_long mutex_set_nowait; /* Blocking mutex: without waiting. */
169 #endif
170 } db_mutex_t;
172 #include "mutex_ext.h"
174 /*******************************************************
175 * Access methods.
176 *******************************************************/
177 /* Lock/unlock a DB thread. */
178 #define DB_THREAD_LOCK(dbp) \
179 (F_ISSET(dbp, DB_AM_THREAD) ? \
180 __db_mutex_lock((db_mutex_t *)(dbp)->mutexp, -1, \
181 (dbp)->dbenv == NULL ? NULL : (dbp)->dbenv->db_yield) : 0)
182 #define DB_THREAD_UNLOCK(dbp) \
183 (F_ISSET(dbp, DB_AM_THREAD) ? \
184 __db_mutex_unlock((db_mutex_t *)(dbp)->mutexp, -1) : 0)
186 /* Btree/recno local statistics structure. */
187 struct __db_bt_lstat; typedef struct __db_bt_lstat DB_BTREE_LSTAT;
188 struct __db_bt_lstat {
189 u_int32_t bt_freed; /* Pages freed for reuse. */
190 u_int32_t bt_pfxsaved; /* Bytes saved by prefix compression. */
191 u_int32_t bt_split; /* Total number of splits. */
192 u_int32_t bt_rootsplit; /* Root page splits. */
193 u_int32_t bt_fastsplit; /* Fast splits. */
194 u_int32_t bt_added; /* Items added. */
195 u_int32_t bt_deleted; /* Items deleted. */
196 u_int32_t bt_get; /* Items retrieved. */
197 u_int32_t bt_cache_hit; /* Hits in fast-insert code. */
198 u_int32_t bt_cache_miss; /* Misses in fast-insert code. */
201 /*******************************************************
202 * Environment.
203 *******************************************************/
204 /* Type passed to __db_appname(). */
205 typedef enum {
206 DB_APP_NONE=0, /* No type (region). */
207 DB_APP_DATA, /* Data file. */
208 DB_APP_LOG, /* Log file. */
209 DB_APP_TMP /* Temporary file. */
210 } APPNAME;
212 /*******************************************************
213 * Regions.
214 *******************************************************/
216 * The shared memory regions share an initial structure so that the general
217 * region code can handle races between the region being deleted and other
218 * processes waiting on the region mutex.
220 * !!!
221 * Note, the mutex must be the first entry in the region; see comment above.
223 typedef struct _rlayout {
224 db_mutex_t lock; /* Region mutex. */
225 u_int32_t refcnt; /* Region reference count. */
226 size_t size; /* Region length. */
227 int majver; /* Major version number. */
228 int minver; /* Minor version number. */
229 int patch; /* Patch version number. */
231 #define DB_R_DELETED 0x01 /* Region was deleted. */
232 u_int32_t flags;
233 } RLAYOUT;
235 /*******************************************************
236 * Mpool.
237 *******************************************************/
239 * File types for DB access methods. Negative numbers are reserved to DB.
241 #define DB_FTYPE_BTREE -1 /* Btree. */
242 #define DB_FTYPE_HASH -2 /* Hash. */
244 /* Structure used as the DB pgin/pgout pgcookie. */
245 typedef struct __dbpginfo {
246 size_t db_pagesize; /* Underlying page size. */
247 int needswap; /* If swapping required. */
248 } DB_PGINFO;
250 /*******************************************************
251 * Log.
252 *******************************************************/
253 /* Initialize an LSN to 'zero'. */
254 #define ZERO_LSN(LSN) { \
255 (LSN).file = 0; \
256 (LSN).offset = 0; \
259 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
260 #define IS_ZERO_LSN(LSN) ((LSN).file == 0)
262 /* Test if we need to log a change. */
263 #define DB_LOGGING(dbp) \
264 (F_ISSET(dbp, DB_AM_LOGGING) && !F_ISSET(dbp, DB_AM_RECOVER))
266 #ifdef DEBUG
268 * Debugging macro to log operations.
269 * If DEBUG_WOP is defined, log operations that modify the database.
270 * If DEBUG_ROP is defined, log operations that read the database.
272 * D dbp
273 * T txn
274 * O operation (string)
275 * K key
276 * A data
277 * F flags
279 #define LOG_OP(D, T, O, K, A, F) { \
280 DB_LSN _lsn; \
281 DBT _op; \
282 if (DB_LOGGING((D))) { \
283 memset(&_op, 0, sizeof(_op)); \
284 _op.data = O; \
285 _op.size = strlen(O) + 1; \
286 (void)__db_debug_log((D)->dbenv->lg_info, \
287 T, &_lsn, 0, &_op, (D)->log_fileid, K, A, F); \
290 #ifdef DEBUG_ROP
291 #define DEBUG_LREAD(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
292 #else
293 #define DEBUG_LREAD(D, T, O, K, A, F)
294 #endif
295 #ifdef DEBUG_WOP
296 #define DEBUG_LWRITE(D, T, O, K, A, F) LOG_OP(D, T, O, K, A, F)
297 #else
298 #define DEBUG_LWRITE(D, T, O, K, A, F)
299 #endif
300 #else
301 #define DEBUG_LREAD(D, T, O, K, A, F)
302 #define DEBUG_LWRITE(D, T, O, K, A, F)
303 #endif /* DEBUG */
305 /*******************************************************
306 * Transactions and recovery.
307 *******************************************************/
309 * Out of band value for a lock. The locks are returned to callers as offsets
310 * into the lock regions. Since the RLAYOUT structure begins all regions, an
311 * offset of 0 is guaranteed not to be a valid lock.
313 #define LOCK_INVALID 0
315 /* The structure allocated for every transaction. */
316 struct __db_txn {
317 DB_TXNMGR *mgrp; /* Pointer to transaction manager. */
318 DB_TXN *parent; /* Pointer to transaction's parent. */
319 DB_LSN last_lsn; /* Lsn of last log write. */
320 u_int32_t txnid; /* Unique transaction id. */
321 size_t off; /* Detail structure within region. */
322 TAILQ_ENTRY(__db_txn) links;
324 #endif /* !_DB_INTERNAL_H_ */