Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/macros.h @ 1409:978cd1c0e6b0 HEAD
Added __attr_scanf__
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 28 Apr 2003 19:32:20 +0300 |
parents | 2f6225f9c170 |
children | 79790750c349 |
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) | |
1048
526415575295
No i_memmove here, just memmove.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
51 # define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list)) |
0 | 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_MEMBER_P(struct_p, struct_offset) \ | |
1052
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
61 ((void *) ((char *) (struct_p) + (long) (struct_offset))) |
0 | 62 #define STRUCT_MEMBER(member_type, struct_p, struct_offset) \ |
1052
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
63 (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset))) |
0 | 64 |
65 /* Provide simple macro statement wrappers (adapted from Perl): | |
66 STMT_START { statements; } STMT_END; | |
67 can be used as a single statement, as in | |
68 if (x) STMT_START { ... } STMT_END; else ... | |
69 | |
70 For gcc we will wrap the statements within `({' and `})' braces. | |
71 For SunOS they will be wrapped within `if (1)' and `else (void) 0', | |
72 and otherwise within `do' and `while (0)'. */ | |
73 #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
|
74 # 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
|
75 !defined (__STRICT_ANSI__) && !defined (PEDANTIC) |
0 | 76 # define STMT_START (void)( |
77 # define STMT_END ) | |
78 # else | |
79 # if (defined (sun) || defined (__sun__)) | |
80 # define STMT_START if (1) | |
81 # define STMT_END else (void)0 | |
82 # else | |
83 # define STMT_START do | |
84 # define STMT_END while (0) | |
85 # endif | |
86 # endif | |
87 #endif | |
88 | |
89 /* Provide macros to feature the GCC function attribute. */ | |
90 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) | |
91 # define __attr_format__(format_idx, arg_idx) \ | |
92 __attribute__((format (printf, format_idx, arg_idx))) | |
93 # define __attr_format_arg__(arg_idx) \ | |
94 __attribute__((format_arg (arg_idx))) | |
1409 | 95 # define __attr_scanf__(format_idx, arg_idx) \ |
96 __attribute__((format (scanf, format_idx, arg_idx))) | |
0 | 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 | |
1052
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
109 /* C99-style struct member definitions */ |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
110 #if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || __GNUC__ > 2 |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
111 # define MEMBER(name) .name = |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
112 #else |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
113 # define MEMBER(name) |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
114 #endif |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
115 |
0 | 116 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with |
117 macros, so we can refer to them as strings unconditionally. */ | |
118 #ifdef __GNUC__ | |
119 # define GNUC_FUNCTION __FUNCTION__ | |
120 # define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ | |
121 #else | |
122 # define GNUC_FUNCTION "" | |
123 # define GNUC_PRETTY_FUNCTION "" | |
124 #endif | |
125 | |
126 /* 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
|
127 #ifdef DISABLE_ASSERTS |
0 | 128 # define i_assert(expr) |
129 #elif defined (__GNUC__) && !defined (__STRICT_ANSI__) | |
130 | |
131 #define i_assert(expr) STMT_START{ \ | |
132 if (!(expr)) \ | |
133 i_panic("file %s: line %d (%s): assertion failed: (%s)", \ | |
134 __FILE__, \ | |
135 __LINE__, \ | |
136 __PRETTY_FUNCTION__, \ | |
137 #expr); }STMT_END | |
138 | |
139 #else /* !__GNUC__ */ | |
140 | |
141 #define i_assert(expr) STMT_START{ \ | |
142 if (!(expr)) \ | |
143 i_panic("file %s: line %d: assertion failed: (%s)", \ | |
144 __FILE__, \ | |
145 __LINE__, \ | |
146 #expr); }STMT_END | |
147 | |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
148 #endif |
0 | 149 |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
150 #define i_unreached() \ |
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
151 i_panic("unreached") |
0 | 152 |
153 #endif |