1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998, 1999
5  *	Sleepycat Software.  All rights reserved.
6  *
7  *	@(#)db_int.src	11.6 (Sleepycat) 10/1/99
8  */
9 
10 #ifndef _DB_INTERNAL_H_
11 #define	_DB_INTERNAL_H_
12 
13 /*******************************************************
14  * General includes.
15  *******************************************************/
16 #include "db.h"
17 
18 #ifndef _MSC_VER /* WIN32 */
19 #ifndef NO_SYSTEM_INCLUDES
20 #ifdef __STDC__
21 #include <stdarg.h>
22 #else
23 #include <varargs.h>
24 #endif
25 #endif
26 #endif
27 
28 #include "queue.h"
29 #include "shqueue.h"
30 
31 /*******************************************************
32  * General purpose constants and macros.
33  *******************************************************/
34 #define	UINT16_T_MAX	    0xffff	/* Maximum 16 bit unsigned. */
35 #define	UINT32_T_MAX	0xffffffff	/* Maximum 32 bit unsigned. */
36 
37 #define	MEGABYTE	1048576
38 #define	GIGABYTE	1073741824
39 
40 #define	MS_PER_SEC	1000		/* Milliseconds in a second. */
41 #define	USEC_PER_MS	1000		/* Microseconds in a millisecond. */
42 
43 #define	DB_MIN_PGSIZE	0x000200	/* Minimum page size (512). */
44 #define	DB_MAX_PGSIZE	0x010000	/* Maximum page size (65536). */
45 
46 #define	RECNO_OOB	0		/* Illegal record number. */
47 
48 /*
49  * If we are unable to determine the underlying filesystem block size, use
50  * 8K on the grounds that most OS's use less than 8K for a VM page size.
51  */
52 #define	DB_DEF_IOSIZE	(8 * 1024)
53 
54 /*
55  * Aligning items to particular sizes or in pages or memory.  ALIGNP is a
56  * separate macro, as we've had to cast the pointer to different integral
57  * types on different architectures.
58  *
59  * We cast pointers into unsigned longs when manipulating them because C89
60  * guarantees that u_long is the largest available integral type and further,
61  * to never generate overflows.  However, neither C89 or C9X  requires that
62  * any integer type be large enough to hold a pointer, although C9X created
63  * the intptr_t type, which is guaranteed to hold a pointer but may or may
64  * not exist.  At some point in the future, we should test for intptr_t and
65  * use it where available.
66  */
67 #undef	ALIGNTYPE
68 #define	ALIGNTYPE		u_long
69 #undef	ALIGNP
70 #define	ALIGNP(value, bound)	ALIGN((ALIGNTYPE)value, bound)
71 #undef	ALIGN
72 #define	ALIGN(value, bound)	(((value) + (bound) - 1) & ~(((u_int)bound) - 1))
73 
74 /*
75  * There are several on-page structures that are declared to have a number of
76  * fields followed by a variable length array of items.  The structure size
77  * without including the variable length array or the address of the first of
78  * those elements can be found using SSZ.
79  *
80  * This macro can also be used to find the offset of a structure element in a
81  * structure.  This is used in various places to copy structure elements from
82  * unaligned memory references, e.g., pointers into a packed page.
83  *
84  * There are two versions because compilers object if you take the address of
85  * an array.
86  */
87 #undef	SSZ
88 #define SSZ(name, field)	((int)&(((name *)0)->field))
89 
90 #undef	SSZA
91 #define SSZA(name, field)	((int)&(((name *)0)->field[0]))
92 
93 /* Structure used to print flag values. */
94 typedef struct __fn {
95 	u_int32_t mask;			/* Flag value. */
96 	const char *name;		/* Flag name. */
97 } FN;
98 
99 /* Set, clear and test flags. */
100 #define	FLD_CLR(fld, f)		(fld) &= ~(f)
101 #define	FLD_ISSET(fld, f)	((fld) & (f))
102 #define	FLD_SET(fld, f)		(fld) |= (f)
103 #define	F_CLR(p, f)		(p)->flags &= ~(f)
104 #define	F_ISSET(p, f)		((p)->flags & (f))
105 #define	F_SET(p, f)		(p)->flags |= (f)
106 #define	LF_CLR(f)		(flags &= ~(f))
107 #define	LF_ISSET(f)		(flags & (f))
108 #define	LF_SET(f)		(flags |= (f))
109 
110 /* Display separator string. */
111 #undef	DB_LINE
112 #define	DB_LINE "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
113 
114 /* Unused, or not-used-yet variable.  "Shut that bloody compiler up!" */
115 #define	COMPQUIET(n, v)	(n) = (v)
116 
117 /*
118  * Purify and similar run-time tools complain about unitialized reads/writes
119  * for structure fields whose only purpose is padding.
120  */
121 #define	UMRW(v)		(v) = 0
122 
123 /*******************************************************
124  * Files.
125  *******************************************************/
126  /*
127   * We use 1024 as the maximum path length.  It's too hard to figure out what
128   * the real path length is, as it was traditionally stored in <sys/param.h>,
129   * and that file isn't always available.
130   */
131 #undef	MAXPATHLEN
132 #define	MAXPATHLEN	1024
133 
134 #define	PATH_DOT	"."	/* Current working directory. */
135 #define	PATH_SEPARATOR	"/"	/* Path separator character. */
136 
137 /*
138  * Flags understood by CDB___os_open.
139  */
140 #define	DB_OSO_CREATE	0x001		/* POSIX: O_CREAT */
141 #define	DB_OSO_EXCL	0x002		/* POSIX: O_EXCL */
142 #define	DB_OSO_LOG	0x004		/* Opening a log file. */
143 #define	DB_OSO_RDONLY	0x008		/* POSIX: O_RDONLY */
144 #define	DB_OSO_SEQ	0x010		/* Expected sequential access. */
145 #define	DB_OSO_TEMP	0x020		/* Remove after last close. */
146 #define	DB_OSO_TRUNC	0x040		/* POSIX: O_TRUNC */
147 
148 /*
149  * Seek options understood by CDB___os_seek.
150  */
151 typedef enum {
152 	DB_OS_SEEK_CUR,			/* POSIX: SEEK_CUR */
153 	DB_OS_SEEK_END,			/* POSIX: SEEK_END */
154 	DB_OS_SEEK_SET			/* POSIX: SEEK_SET */
155 } DB_OS_SEEK;
156 
157 /*******************************************************
158  * Environment.
159  *******************************************************/
160 /* Type passed to CDB___db_appname(). */
161 typedef enum {
162 	DB_APP_NONE=0,			/* No type (region). */
163 	DB_APP_DATA,			/* Data file. */
164 	DB_APP_LOG,			/* Log file. */
165 	DB_APP_TMP			/* Temporary file. */
166 } APPNAME;
167 
168 /* Most initialization methods cannot be called after open is called. */
169 #define	ENV_ILLEGAL_AFTER_OPEN(dbenv, name)				\
170 	if (F_ISSET((dbenv), DB_ENV_OPEN_CALLED))			\
171 		return (CDB___db_mi_open(dbenv, name, 1));
172 
173 /* We're not actually user hostile, honest. */
174 #define	ENV_REQUIRES_CONFIG(dbenv, handle, subsystem)			\
175 	if (handle == NULL)						\
176 		return (CDB___db_env_config(dbenv, subsystem));
177 
178 /*******************************************************
179  * Database Access Methods.
180  *******************************************************/
181 /* Initialization methods are often illegal before/after open is called. */
182 #define	DB_ILLEGAL_AFTER_OPEN(dbp, name)				\
183 	if (F_ISSET((dbp), DB_OPEN_CALLED))				\
184 		return (CDB___db_mi_open(dbp->dbenv, name, 1));
185 #define	DB_ILLEGAL_BEFORE_OPEN(dbp, name)				\
186 	if (!F_ISSET((dbp), DB_OPEN_CALLED))				\
187 		return (CDB___db_mi_open(dbp->dbenv, name, 0));
188 /* Some initialization methods are illegal if environment isn't local. */
189 #define	DB_ILLEGAL_IN_ENV(dbp, name)					\
190 	if (!F_ISSET(dbp->dbenv, DB_ENV_DBLOCAL))			\
191 		return (CDB___db_mi_env(dbp->dbenv, name));
192 #define	DB_ILLEGAL_METHOD(dbp, flags) {					\
193 	int __ret;							\
194 	if ((__ret = CDB___dbh_am_chk(dbp, flags)) != 0)			\
195 		return (__ret);						\
196 }
197 
198 /*******************************************************
199  * Mpool.
200  *******************************************************/
201 /*
202  * File types for DB access methods.  Negative numbers are reserved to DB.
203  */
204 #define	DB_FTYPE_SET		-1	/* Call pgin/pgout functions. */
205 #define	DB_FTYPE_NOTSET		 0	/* Don't call... */
206 
207 /* Structure used as the DB pgin/pgout pgcookie. */
208 typedef struct __dbpginfo {
209 	size_t	db_pagesize;		/* Underlying page size. */
210 	int	needswap;		/* If swapping required. */
211 } DB_PGINFO;
212 
213 /*******************************************************
214  * Log.
215  *******************************************************/
216 /* Initialize an LSN to 'zero'. */
217 #define	ZERO_LSN(LSN) {							\
218 	(LSN).file = 0;							\
219 	(LSN).offset = 0;						\
220 }
221 
222 /* Return 1 if LSN is a 'zero' lsn, otherwise return 0. */
223 #define	IS_ZERO_LSN(LSN)	((LSN).file == 0)
224 
225 /* Test if we need to log a change. */
226 #define	DB_LOGGING(dbc)							\
227 	(F_ISSET((dbc)->dbp->dbenv, DB_ENV_LOGGING) &&			\
228 	    !F_ISSET(dbc, DBC_RECOVER))
229 
230 /*******************************************************
231  * Txn.
232  *******************************************************/
233 #define	DB_NONBLOCK(C)	((C)->txn != NULL && F_ISSET((C)->txn, TXN_NOWAIT))
234 
235 /*******************************************************
236  * Global variables.
237  *******************************************************/
238 /*
239  * !!!
240  * Initialized in env/env_method.c, don't change this without changing that.
241  */
242 typedef struct __db_globals {
243 	u_int32_t db_mutexlocks;	/* db_set_mutexlocks */
244 	u_int32_t db_pageyield;		/* db_set_pageyield */
245 	u_int32_t db_panic;		/* db_set_panic */
246 	u_int32_t db_region_init;	/* db_set_region_init */
247 	u_int32_t db_tas_spins;		/* db_set_tas_spins */
248 					/* XA: list of opened environments. */
249 	TAILQ_HEAD(__db_envq, __db_env) db_envq;
250 } DB_GLOBALS;
251 
252 extern	DB_GLOBALS	CDB___db_global_values;
253 #define	DB_GLOBAL(v)	CDB___db_global_values.v
254 
255 /* Forward structure declarations. */
256 struct __db_reginfo_t;	typedef struct __db_reginfo_t REGINFO;
257 struct __mutex_t;	typedef struct __mutex_t MUTEX;
258 
259 /*******************************************************
260  * More general includes.
261  *******************************************************/
262 #include "debug.h"
263 #include "mutex.h"
264 #include "mutex_ext.h"
265 #include "region.h"
266 #include "env_ext.h"
267 #include "os.h"
268 #include "os_ext.h"
269 #include "common_ext.h"
270 
271 
272 /*******************************************************
273  * Stuff not defined in native WIN32 Env.
274  *******************************************************/
275 #ifdef _MSC_VER /* _WIN32 */
276 
277 #include <windows.h>
278 
279 #define S_IRGRP 0               /* R for group */
280 #define S_IWGRP 0               /* W for group */
281 #define S_IROTH 0               /* R for other */
282 #define S_IWOTH 0               /* W for other */
283 #endif
284 
285 #ifdef _MSC_VER
286 #ifndef	S_IRUSR
287 #if defined(_WIN32) || defined(WIN16)
288 #define	S_IRUSR	S_IREAD		/* R for owner */
289 #define	S_IWUSR	S_IWRITE	/* W for owner */
290 #define	S_IRGRP	0		/* R for group */
291 #define	S_IWGRP	0		/* W for group */
292 #define	S_IROTH	0		/* R for other */
293 #define	S_IWOTH	0		/* W for other */
294 #else
295 #define	S_IRUSR	0000400		/* R for owner */
296 #define	S_IWUSR	0000200		/* W for owner */
297 #define	S_IRGRP	0000040		/* R for group */
298 #define	S_IWGRP	0000020		/* W for group */
299 #define	S_IROTH	0000004		/* R for other */
300 #define	S_IWOTH	0000002		/* W for other */
301 #endif /* _WIN32 || WIN16 */
302 #endif
303 #endif
304 
305 #endif /* !_DB_INTERNAL_H_ */
306