summaryrefslogtreecommitdiff
path: root/inc_posix/misc.h
blob: 9906a1c245cad875f23561e148f0d6e848120b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008 WiredTiger Software.
 *	All rights reserved.
 *
 * $Id$
 */

#if defined(__cplusplus)
extern "C" {
#endif

/* Basic constants. */
#define	WT_FRAGMENT	(512)
#define	WT_MEGABYTE	(1024 * 1024)			/* MB */

/* Align a number to a specified power-of-2. */
#define	WT_ALIGN(n, v)							\
	(((n) + ((v) - 1)) & ~(((uintmax_t)(v)) - 1))

/*
 * Flag checking for API functions.
 */
#define	WT_DB_FCHK(db, name, f, mask)					\
	if ((f) & ~(mask))						\
		return (__wt_api_flags((db)->env, name));
#define	WT_DB_FCHK_NOTFATAL(db, name, f, mask, ret)			\
	if ((f) & ~(mask))						\
		(ret) = __wt_api_flags((db)->env, name);
#define	WT_ENV_FCHK(env, name, f, mask)					\
	if ((f) & ~(mask))						\
		return (__wt_api_flags(env, name));
#define	WT_ENV_FCHK_NOTFATAL(env, name, f, mask, ret)			\
	if ((f) & ~(mask))						\
		(ret) = __wt_api_flags(env, name);

/*
 * Flag set, clear and test.  They come in 3 flavors: F_XXX (handles a
 * field named "flags" in the structure referenced by its argument),
 * LF_XXX (handles a local variable named "flags"), and FLD_XXX (handles
 * any variable, anywhere.
 */
#define	F_CLR(p, mask)		((p)->flags &= ~(mask))
#define	F_ISSET(p, mask)	((p)->flags & (mask) ? 1 : 0)
#define	F_SET(p, mask)		((p)->flags |= (mask))

#define	LF_CLR(mask)		((flags) &= ~(mask))
#define	LF_ISSET(mask)		((flags) & (mask) ? 1 : 0)
#define	LF_SET(mask)		((flags) |= (mask))

#define	FLD_CLR(field, mask)	((field) &= ~(mask))
#define	FLD_ISSET(field, mask)	((field) & (mask) ? 1 : 0)
#define	FLD_SET(field, mask)	((field) |= (mask))

/* Clear a chunk of memory. */
#define	WT_CLEAR(s)							\
	memset(&(s), 0, sizeof(s))

/* Free memory if set. */
#define	WT_FREE_AND_CLEAR(env, p) do {					\
	if ((p) != NULL) {						\
		__wt_free(env, p);					\
		(p) = NULL;						\
	}								\
} while (0)

/* A distinguished byte pattern to overwrite memory we are done using. */
#define	OVERWRITE_BYTE	0xab

#ifdef HAVE_DIAGNOSTIC
#define	WT_ASSERT(env, e)						\
	((e) ? (void)0 : __wt_assert(env, #e, __FILE__, __LINE__))
#else
#define	WT_ASSERT(ienv, e)
#endif

#define	WT_DEFAULT_FORMAT(db)						\
	default:							\
		return (__wt_database_format(db))

/*
 * Standard macros that set return values and optionally branch to an error
 * label.
 */
#define	WT_ERR(a) {							\
	if ((ret = a) != 0)						\
		goto err;						\
}
#define	WT_RET(a) {							\
	int __ret;							\
	if ((__ret = a) != 0)						\
		return (__ret);						\
}
#define	WT_TRET(a) {							\
	int __ret;							\
	if ((__ret = a) != 0 && ret == 0)				\
		ret = __ret;						\
}

#if defined(__cplusplus)
}
#endif