Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/macros.h @ 903:fd8888f6f037 HEAD
Naming style changes, finally got tired of most of the typedefs. Also the
previous enum -> macro change reverted so that we don't use the highest bit
anymore, that's incompatible with old indexes so they will be rebuilt.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 05 Jan 2003 15:09:51 +0200 |
parents | a225abf5f479 |
children | 526415575295 |
rev | line source |
---|---|
0 | 1 #ifndef __MACROS_H |
2 #define __MACROS_H | |
3 | |
4 /* several useful macros, mostly from glib.h */ | |
5 | |
6 #ifndef NULL | |
7 # define NULL ((void *)0) | |
8 #endif | |
9 | |
10 #ifndef FALSE | |
11 # define FALSE (0) | |
12 #endif | |
13 | |
14 #ifndef TRUE | |
15 # define TRUE (!FALSE) | |
16 #endif | |
17 | |
18 #define BITS_IN_UINT (CHAR_BIT * sizeof(unsigned int)) | |
187
ecbff0f2b811
nearest_power() uses now size_t.
Timo Sirainen <tss@iki.fi>
parents:
178
diff
changeset
|
19 #define BITS_IN_SIZE_T (CHAR_BIT * sizeof(size_t)) |
0 | 20 |
21 #define MEM_ALIGN(size) \ | |
22 (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1)) | |
23 | |
24 /* Don't use simply MIN/MAX, as they're often defined elsewhere in include | |
25 files that are included after this file generating tons of warnings. */ | |
26 #define I_MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
27 #define I_MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
28 | |
29 #undef CLAMP | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
901
diff
changeset
|
30 #define CLAMP(x, low, high) \ |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
901
diff
changeset
|
31 (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
0 | 32 |
33 #undef NVL | |
34 #define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr)) | |
35 | |
195
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
36 /* make it easier to cast from/to pointers. assumes that |
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
37 sizeof(size_t) == sizeof(void *) and they're both the largest datatypes |
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
38 that are allowed to be used. so, long long isn't safe with these. */ |
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
39 #define POINTER_CAST(i) \ |
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
40 ((void *) ((char *) NULL + (i))) |
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
41 #define POINTER_CAST_TO(p, type) \ |
579
e524da896d92
Several minor fixes: signess, casting away const, missing static, etc.
Timo Sirainen <tss@iki.fi>
parents:
546
diff
changeset
|
42 ((type) ((const char *) (p) - (const char *) NULL)) |
0 | 43 |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
579
diff
changeset
|
44 /* Define VA_COPY() to do the right thing for copying va_list variables. |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
579
diff
changeset
|
45 config.h may have already defined VA_COPY as va_copy or __va_copy. */ |
0 | 46 #ifndef VA_COPY |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
901
diff
changeset
|
47 # if defined (__GNUC__) && defined (__PPC__) && \ |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
901
diff
changeset
|
48 (defined (_CALL_SYSV) || defined (_WIN32)) |
0 | 49 # define VA_COPY(ap1, ap2) (*(ap1) = *(ap2)) |
50 # elif defined (VA_COPY_AS_ARRAY) | |
51 # define VA_COPY(ap1, ap2) i_memmove ((ap1), (ap2), sizeof (va_list)) | |
52 # else /* va_list is a pointer */ | |
53 # define VA_COPY(ap1, ap2) ((ap1) = (ap2)) | |
54 # endif /* va_list is a pointer */ | |
55 #endif | |
56 | |
57 /* Provide convenience macros for handling structure | |
58 * fields through their offsets. | |
59 */ | |
60 #define STRUCT_OFFSET(struct_p, member) \ | |
61 ((long) ((char *) &((struct_p)->member) - (char *) (struct_p))) | |
62 #define STRUCT_MEMBER_P(struct_p, struct_offset) \ | |
63 ((void *) ((char *) (struct_p) + (long) (struct_offset))) | |
64 #define STRUCT_MEMBER(member_type, struct_p, struct_offset) \ | |
65 (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset))) | |
66 | |
67 /* Provide simple macro statement wrappers (adapted from Perl): | |
68 STMT_START { statements; } STMT_END; | |
69 can be used as a single statement, as in | |
70 if (x) STMT_START { ... } STMT_END; else ... | |
71 | |
72 For gcc we will wrap the statements within `({' and `})' braces. | |
73 For SunOS they will be wrapped within `if (1)' and `else (void) 0', | |
74 and otherwise within `do' and `while (0)'. */ | |
75 #if !(defined (STMT_START) && defined (STMT_END)) | |
901
a225abf5f479
If PEDANTIC is defined, don't use (void)(..) with STMT_START/END.
Timo Sirainen <tss@iki.fi>
parents:
859
diff
changeset
|
76 # if defined (__GNUC__) && !defined (__cplusplus) && \ |
a225abf5f479
If PEDANTIC is defined, don't use (void)(..) with STMT_START/END.
Timo Sirainen <tss@iki.fi>
parents:
859
diff
changeset
|
77 !defined (__STRICT_ANSI__) && !defined (PEDANTIC) |
0 | 78 # define STMT_START (void)( |
79 # define STMT_END ) | |
80 # else | |
81 # if (defined (sun) || defined (__sun__)) | |
82 # define STMT_START if (1) | |
83 # define STMT_END else (void)0 | |
84 # else | |
85 # define STMT_START do | |
86 # define STMT_END while (0) | |
87 # endif | |
88 # endif | |
89 #endif | |
90 | |
91 /* Provide macros to feature the GCC function attribute. */ | |
92 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) | |
93 # define __attr_format__(format_idx, arg_idx) \ | |
94 __attribute__((format (printf, format_idx, arg_idx))) | |
95 # define __attr_format_arg__(arg_idx) \ | |
96 __attribute__((format_arg (arg_idx))) | |
97 # define __attr_unused__ __attribute__((unused)) | |
98 # define __attr_noreturn__ __attribute__((noreturn)) | |
99 # define __attr_const__ __attribute__((const)) | |
100 #else | |
101 # define __attr_format__(format_idx, arg_idx) | |
102 # define __attr_format_arg__(arg_idx) | |
103 # define __attr_unused__ | |
104 # define __attr_noreturn__ | |
105 # define __attr_const__ | |
106 # define __attr_unused__ | |
107 #endif | |
108 | |
109 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with | |
110 macros, so we can refer to them as strings unconditionally. */ | |
111 #ifdef __GNUC__ | |
112 # define GNUC_FUNCTION __FUNCTION__ | |
113 # define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ | |
114 #else | |
115 # define GNUC_FUNCTION "" | |
116 # define GNUC_PRETTY_FUNCTION "" | |
117 #endif | |
118 | |
119 /* Provide macros for error handling. */ | |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
120 #ifdef DISABLE_ASSERTS |
0 | 121 # define i_assert(expr) |
122 #elif defined (__GNUC__) && !defined (__STRICT_ANSI__) | |
123 | |
124 #define i_assert(expr) STMT_START{ \ | |
125 if (!(expr)) \ | |
126 i_panic("file %s: line %d (%s): assertion failed: (%s)", \ | |
127 __FILE__, \ | |
128 __LINE__, \ | |
129 __PRETTY_FUNCTION__, \ | |
130 #expr); }STMT_END | |
131 | |
132 #else /* !__GNUC__ */ | |
133 | |
134 #define i_assert(expr) STMT_START{ \ | |
135 if (!(expr)) \ | |
136 i_panic("file %s: line %d: assertion failed: (%s)", \ | |
137 __FILE__, \ | |
138 __LINE__, \ | |
139 #expr); }STMT_END | |
140 | |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
141 #endif |
0 | 142 |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
143 #define i_unreached() \ |
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
144 i_panic("unreached") |
0 | 145 |
146 #endif |