2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
10
#ifndef _DB_INTERNAL_H_
11
#define _DB_INTERNAL_H_
13
/*******************************************************
14
* System includes, db.h, a few general DB includes. The DB includes are
15
* here because it's OK if db_int.h includes queue structure declarations.
16
*******************************************************/
17
#ifndef NO_SYSTEM_INCLUDES
18
#if defined(__STDC__) || defined(__cplusplus)
28
#include "dbinc/queue.h"
29
#include "dbinc/shqueue.h"
31
#if defined(__cplusplus)
35
/*******************************************************
36
* General purpose constants and macros.
37
*******************************************************/
38
#define UINT16_T_MAX 0xffff /* Maximum 16 bit unsigned. */
39
#define UINT32_T_MAX 0xffffffff /* Maximum 32 bit unsigned. */
41
#define MEGABYTE 1048576
42
#define GIGABYTE 1073741824
44
#define MS_PER_SEC 1000 /* Milliseconds in a second. */
45
#define USEC_PER_MS 1000 /* Microseconds in a millisecond. */
47
#define RECNO_OOB 0 /* Illegal record number. */
49
/* Test for a power-of-two (tests true for zero, which doesn't matter here). */
50
#define POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
52
/* Test for valid page sizes. */
53
#define DB_MIN_PGSIZE 0x000200 /* Minimum page size (512). */
54
#define DB_MAX_PGSIZE 0x010000 /* Maximum page size (65536). */
55
#define IS_VALID_PAGESIZE(x) \
56
(POWER_OF_TWO(x) && (x) >= DB_MIN_PGSIZE && ((x) <= DB_MAX_PGSIZE))
58
/* Minimum number of pages cached, by default. */
59
#define DB_MINPAGECACHE 16
62
* If we are unable to determine the underlying filesystem block size, use
63
* 8K on the grounds that most OS's use less than 8K for a VM page size.
65
#define DB_DEF_IOSIZE (8 * 1024)
68
* Aligning items to particular sizes or in pages or memory.
71
* Largest integral type, used to align structures in memory. We don't store
72
* floating point types in structures, so integral types should be sufficient
73
* (and we don't have to worry about systems that store floats in other than
74
* power-of-2 numbers of bytes). Additionally this fixes compiler that rewrite
75
* structure assignments and ANSI C memcpy calls to be in-line instructions
76
* that happen to require alignment. Note: this alignment isn't sufficient for
77
* mutexes, which depend on things like cache line alignment. Mutex alignment
78
* is handled separately, in mutex.h.
81
* Integral type that's the same size as a pointer. There are places where
82
* DB modifies pointers by discarding the bottom bits to guarantee alignment.
83
* We can't use db_align_t, it may be larger than the pointer, and compilers
84
* get upset about that. So far we haven't run on any machine where there
85
* isn't an integral type the same size as a pointer -- here's hoping.
87
typedef unsigned long long db_align_t;
88
typedef unsigned long db_alignp_t;
90
/* Align an integer to a specific boundary. */
92
#define ALIGN(v, bound) (((v) + (bound) - 1) & ~(((db_align_t)bound) - 1))
95
* Print an address as a u_long (a u_long is the largest type we can print
96
* portably). Most 64-bit systems have made longs 64-bits, so this should
99
#define P_TO_ULONG(p) ((u_long)(db_alignp_t)(p))
102
* Convert a pointer to a small integral value.
104
* The (u_int16_t)(db_alignp_t) cast avoids warnings: the (db_alignp_t) cast
105
* converts the value to an integral type, and the (u_int16_t) cast converts
106
* it to a small integral type so we don't get complaints when we assign the
107
* final result to an integral type smaller than db_alignp_t.
109
#define P_TO_UINT32(p) ((u_int32_t)(db_alignp_t)(p))
110
#define P_TO_UINT16(p) ((u_int16_t)(db_alignp_t)(p))
113
* There are several on-page structures that are declared to have a number of
114
* fields followed by a variable length array of items. The structure size
115
* without including the variable length array or the address of the first of
116
* those elements can be found using SSZ.
118
* This macro can also be used to find the offset of a structure element in a
119
* structure. This is used in various places to copy structure elements from
120
* unaligned memory references, e.g., pointers into a packed page.
122
* There are two versions because compilers object if you take the address of
126
#define SSZ(name, field) P_TO_UINT16(&(((name *)0)->field))
129
#define SSZA(name, field) P_TO_UINT16(&(((name *)0)->field[0]))
131
/* Structure used to print flag values. */
132
typedef struct __fn {
133
u_int32_t mask; /* Flag value. */
134
const char *name; /* Flag name. */
137
/* Set, clear and test flags. */
138
#define FLD_CLR(fld, f) (fld) &= ~(f)
139
#define FLD_ISSET(fld, f) ((fld) & (f))
140
#define FLD_SET(fld, f) (fld) |= (f)
141
#define F_CLR(p, f) (p)->flags &= ~(f)
142
#define F_ISSET(p, f) ((p)->flags & (f))
143
#define F_SET(p, f) (p)->flags |= (f)
144
#define LF_CLR(f) ((flags) &= ~(f))
145
#define LF_ISSET(f) ((flags) & (f))
146
#define LF_SET(f) ((flags) |= (f))
148
/* Display separator string. */
150
#define DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
152
/* Unused, or not-used-yet variable. "Shut that bloody compiler up!" */
153
#define COMPQUIET(n, v) (n) = (v)
155
/*******************************************************
157
*******************************************************/
159
* Return values that are OK for each different call. Most calls have
160
* a standard 'return of 0 is only OK value', but some, like db->get
161
* have DB_NOTFOUND as a return value, but it really isn't an error.
163
#define DB_RETOK_STD(ret) ((ret) == 0)
164
#define DB_RETOK_DBCDEL(ret) ((ret) == 0 || (ret) == DB_KEYEMPTY || \
165
(ret) == DB_NOTFOUND)
166
#define DB_RETOK_DBCGET(ret) DB_RETOK_DBGET(ret)
167
#define DB_RETOK_DBCPUT(ret) ((ret) == 0 || (ret) == DB_KEYEXIST || \
168
(ret) == DB_NOTFOUND)
169
#define DB_RETOK_DBDEL(ret) ((ret) == 0 || (ret) == DB_NOTFOUND)
170
#define DB_RETOK_DBGET(ret) ((ret) == 0 || (ret) == DB_KEYEMPTY || \
171
(ret) == DB_NOTFOUND)
172
#define DB_RETOK_DBPUT(ret) ((ret) == 0 || (ret) == DB_KEYEXIST)
173
#define DB_RETOK_LGGET(ret) ((ret) == 0 || (ret) == DB_NOTFOUND)
174
#define DB_RETOK_MPGET(ret) ((ret) == 0 || (ret) == DB_PAGE_NOTFOUND)
175
#define DB_RETOK_REPPMSG(ret) ((ret) == 0 || (ret) == DB_REP_NEWMASTER || \
176
(ret) == DB_REP_NEWSITE)
178
/*******************************************************
180
*******************************************************/
182
* We use 1024 as the maximum path length. It's too hard to figure out what
183
* the real path length is, as it was traditionally stored in <sys/param.h>,
184
* and that file isn't always available.
187
#define MAXPATHLEN 1024
189
#define PATH_DOT "." /* Current working directory. */
191
#define PATH_SEPARATOR "\\/:" /* Path separator character(s). */
193
#define PATH_SEPARATOR "/" /* Path separator character(s). */
197
* Flags understood by __os_open.
199
#define DB_OSO_CREATE 0x0001 /* POSIX: O_CREAT */
200
#define DB_OSO_DIRECT 0x0002 /* Don't buffer the file in the OS. */
201
#define DB_OSO_EXCL 0x0004 /* POSIX: O_EXCL */
202
#define DB_OSO_LOG 0x0008 /* Opening a log file. */
203
#define DB_OSO_RDONLY 0x0010 /* POSIX: O_RDONLY */
204
#define DB_OSO_REGION 0x0020 /* Opening a region file. */
205
#define DB_OSO_SEQ 0x0040 /* Expected sequential access. */
206
#define DB_OSO_TEMP 0x0080 /* Remove after last close. */
207
#define DB_OSO_TRUNC 0x0100 /* POSIX: O_TRUNC */
210
* Seek options understood by __os_seek.
213
DB_OS_SEEK_CUR, /* POSIX: SEEK_CUR */
214
DB_OS_SEEK_END, /* POSIX: SEEK_END */
215
DB_OS_SEEK_SET /* POSIX: SEEK_SET */
218
/*******************************************************
220
*******************************************************/
221
/* Type passed to __db_appname(). */
223
DB_APP_NONE=0, /* No type (region). */
224
DB_APP_DATA, /* Data file. */
225
DB_APP_LOG, /* Log file. */
226
DB_APP_TMP /* Temporary file. */
230
* CDB_LOCKING CDB product locking.
231
* CRYPTO_ON Security has been configured.
232
* LOCKING_ON Locking has been configured.
233
* LOGGING_ON Logging has been configured.
234
* MPOOL_ON Memory pool has been configured.
235
* RPC_ON RPC has been configured.
236
* TXN_ON Transactions have been configured.
238
#define CDB_LOCKING(dbenv) F_ISSET(dbenv, DB_ENV_CDB)
239
#define CRYPTO_ON(dbenv) ((dbenv)->crypto_handle != NULL)
240
#define LOCKING_ON(dbenv) ((dbenv)->lk_handle != NULL)
241
#define LOGGING_ON(dbenv) ((dbenv)->lg_handle != NULL)
242
#define MPOOL_ON(dbenv) ((dbenv)->mp_handle != NULL)
243
#define RPC_ON(dbenv) ((dbenv)->cl_handle != NULL)
244
#define TXN_ON(dbenv) ((dbenv)->tx_handle != NULL)
247
* STD_LOCKING Standard locking, that is, locking was configured and CDB
248
* was not. We do not do locking in off-page duplicate trees,
249
* so we check for that in the cursor first.
251
#define STD_LOCKING(dbc) \
252
(!F_ISSET(dbc, DBC_OPD) && \
253
!CDB_LOCKING((dbc)->dbp->dbenv) && LOCKING_ON((dbc)->dbp->dbenv))
256
* IS_RECOVERING: The system is running recovery.
258
#define IS_RECOVERING(dbenv) \
259
(LOGGING_ON(dbenv) && \
260
F_ISSET((DB_LOG *)(dbenv)->lg_handle, DBLOG_RECOVER))
262
/* Initialization methods are often illegal before/after open is called. */
263
#define ENV_ILLEGAL_AFTER_OPEN(dbenv, name) \
264
if (F_ISSET((dbenv), DB_ENV_OPEN_CALLED)) \
265
return (__db_mi_open(dbenv, name, 1));
266
#define ENV_ILLEGAL_BEFORE_OPEN(dbenv, name) \
267
if (!F_ISSET((dbenv), DB_ENV_OPEN_CALLED)) \
268
return (__db_mi_open(dbenv, name, 0));
270
/* We're not actually user hostile, honest. */
271
#define ENV_REQUIRES_CONFIG(dbenv, handle, i, flags) \
272
if (handle == NULL) \
273
return (__db_env_config(dbenv, i, flags));
275
/*******************************************************
276
* Database Access Methods.
277
*******************************************************/
280
* The database handle is free-threaded (was opened with DB_THREAD).
282
#define DB_IS_THREADED(dbp) \
283
((dbp)->mutexp != NULL)
285
/* Initialization methods are often illegal before/after open is called. */
286
#define DB_ILLEGAL_AFTER_OPEN(dbp, name) \
287
if (F_ISSET((dbp), DB_AM_OPEN_CALLED)) \
288
return (__db_mi_open((dbp)->dbenv, name, 1));
289
#define DB_ILLEGAL_BEFORE_OPEN(dbp, name) \
290
if (!F_ISSET((dbp), DB_AM_OPEN_CALLED)) \
291
return (__db_mi_open((dbp)->dbenv, name, 0));
292
/* Some initialization methods are illegal if environment isn't local. */
293
#define DB_ILLEGAL_IN_ENV(dbp, name) \
294
if (!F_ISSET((dbp)->dbenv, DB_ENV_DBLOCAL)) \
295
return (__db_mi_env((dbp)->dbenv, name));
296
#define DB_ILLEGAL_METHOD(dbp, flags) { \
298
if ((__ret = __dbh_am_chk(dbp, flags)) != 0) \
303
* Common DBC->internal fields. Each access method adds additional fields
304
* to this list, but the initial fields are common.
306
#define __DBC_INTERNAL \
307
DBC *opd; /* Off-page duplicate cursor. */\
309
void *page; /* Referenced page. */ \
310
db_pgno_t root; /* Tree root. */ \
311
db_pgno_t pgno; /* Referenced page number. */ \
312
db_indx_t indx; /* Referenced key item index. */\
314
DB_LOCK lock; /* Cursor lock. */ \
315
db_lockmode_t lock_mode; /* Lock mode. */
317
struct __dbc_internal {
321
/* Actions that __db_master_update can take. */
322
typedef enum { MU_REMOVE, MU_RENAME, MU_OPEN } mu_action;
325
* Access-method-common macro for determining whether a cursor
326
* has been initialized.
328
#define IS_INITIALIZED(dbc) ((dbc)->internal->pgno != PGNO_INVALID)
330
/* Free the callback-allocated buffer, if necessary, hanging off of a DBT. */
331
#define FREE_IF_NEEDED(sdbp, dbt) \
332
if (F_ISSET((dbt), DB_DBT_APPMALLOC)) { \
333
__os_ufree((sdbp)->dbenv, (dbt)->data); \
334
F_CLR((dbt), DB_DBT_APPMALLOC); \
338
* Use memory belonging to object "owner" to return the results of
339
* any no-DBT-flag get ops on cursor "dbc".
341
#define SET_RET_MEM(dbc, owner) \
343
(dbc)->rskey = &(owner)->my_rskey; \
344
(dbc)->rkey = &(owner)->my_rkey; \
345
(dbc)->rdata = &(owner)->my_rdata; \
348
/* Use the return-data memory src is currently set to use in dest as well. */
349
#define COPY_RET_MEM(src, dest) \
351
(dest)->rskey = (src)->rskey; \
352
(dest)->rkey = (src)->rkey; \
353
(dest)->rdata = (src)->rdata; \
356
/* Reset the returned-memory pointers to their defaults. */
357
#define RESET_RET_MEM(dbc) \
359
(dbc)->rskey = &(dbc)->my_rskey; \
360
(dbc)->rkey = &(dbc)->my_rkey; \
361
(dbc)->rdata = &(dbc)->my_rdata; \
364
/*******************************************************
366
*******************************************************/
368
* File types for DB access methods. Negative numbers are reserved to DB.
370
#define DB_FTYPE_SET -1 /* Call pgin/pgout functions. */
371
#define DB_FTYPE_NOTSET 0 /* Don't call... */
373
/* Structure used as the DB pgin/pgout pgcookie. */
374
typedef struct __dbpginfo {
375
size_t db_pagesize; /* Underlying page size. */
376
u_int32_t flags; /* Some DB_AM flags needed. */
377
DBTYPE type; /* DB type */
380
/*******************************************************
382
*******************************************************/
383
/* Initialize an LSN to 'zero'. */
384
#define ZERO_LSN(LSN) do { \
388
#define IS_ZERO_LSN(LSN) ((LSN).file == 0)
390
#define IS_INIT_LSN(LSN) ((LSN).file == 1 && (LSN).offset == 0)
391
#define INIT_LSN(LSN) do { \
396
#define MAX_LSN(LSN) do { \
397
(LSN).file = UINT32_T_MAX; \
398
(LSN).offset = UINT32_T_MAX; \
400
#define IS_MAX_LSN(LSN) \
401
((LSN).file == UINT32_T_MAX && (LSN).offset == UINT32_T_MAX)
403
/* If logging is turned off, smash the lsn. */
404
#define LSN_NOT_LOGGED(LSN) do { \
408
#define IS_NOT_LOGGED_LSN(LSN) \
409
((LSN).file == 0 && (LSN).offset == 1)
412
* Test if the environment is currently logging changes. If we're in
413
* recovery or we're a replication client, we don't need to log changes
414
* because they're already in the log, even though we have a fully functional
417
#define DBENV_LOGGING(dbenv) \
418
(LOGGING_ON(dbenv) && !F_ISSET((dbenv), DB_ENV_REP_CLIENT) && \
419
(!IS_RECOVERING(dbenv)))
422
* Test if we need to log a change. Note that the DBC_RECOVER flag is set
423
* when we're in abort, as well as during recovery; thus DBC_LOGGING may be
424
* false for a particular dbc even when DBENV_LOGGING is true.
426
* We explicitly use LOGGING_ON/DB_ENV_REP_CLIENT here because we don't
427
* want to have to pull in the log headers, which IS_RECOVERING (and thus
428
* DBENV_LOGGING) rely on, and because DBC_RECOVER should be set anytime
429
* IS_RECOVERING would be true.
431
#define DBC_LOGGING(dbc) \
432
(LOGGING_ON((dbc)->dbp->dbenv) && !F_ISSET((dbc), DBC_RECOVER) && \
433
!F_ISSET((dbc)->dbp->dbenv, DB_ENV_REP_CLIENT))
435
/*******************************************************
437
*******************************************************/
438
#define DB_NONBLOCK(C) ((C)->txn != NULL && F_ISSET((C)->txn, TXN_NOWAIT))
439
#define IS_SUBTRANSACTION(txn) \
440
((txn) != NULL && (txn)->parent != NULL)
442
/*******************************************************
444
*******************************************************/
445
#define DB_IV_BYTES 16 /* Bytes per IV */
446
#define DB_MAC_KEY 20 /* Bytes per MAC checksum */
448
/*******************************************************
449
* Forward structure declarations.
450
*******************************************************/
451
struct __db_reginfo_t; typedef struct __db_reginfo_t REGINFO;
452
struct __db_txnhead; typedef struct __db_txnhead DB_TXNHEAD;
453
struct __db_txnlist; typedef struct __db_txnlist DB_TXNLIST;
454
struct __vrfy_childinfo; typedef struct __vrfy_childinfo VRFY_CHILDINFO;
455
struct __vrfy_dbinfo; typedef struct __vrfy_dbinfo VRFY_DBINFO;
456
struct __vrfy_pageinfo; typedef struct __vrfy_pageinfo VRFY_PAGEINFO;
458
#if defined(__cplusplus)
462
/*******************************************************
463
* Remaining general DB includes.
464
*******************************************************/
465
#include "db_int_def.h"
467
#include "dbinc/globals.h"
468
#include "dbinc/debug.h"
469
#include "dbinc/mutex.h"
470
#include "dbinc/region.h"
471
#include "dbinc_auto/mutex_ext.h" /* XXX: Include after region.h. */
472
#include "dbinc_auto/env_ext.h"
473
#include "dbinc/os.h"
474
#include "dbinc_auto/clib_ext.h"
475
#include "dbinc_auto/common_ext.h"
477
#endif /* !_DB_INTERNAL_H_ */