Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/macros.h @ 4891:6ab2712f1a93 HEAD
Only imap binary was actually working.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 10 Dec 2006 14:35:02 +0200 |
parents | d972f9e2902e |
children | 204d7edc7cdc |
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 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1409
diff
changeset
|
24 #define PTR_OFFSET(ptr, offset) \ |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1409
diff
changeset
|
25 ((void *) (((unsigned char *) (ptr)) + (offset))) |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1409
diff
changeset
|
26 #define CONST_PTR_OFFSET(ptr, offset) \ |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1409
diff
changeset
|
27 ((const void *) (((const unsigned char *) (ptr)) + (offset))) |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1409
diff
changeset
|
28 |
0 | 29 /* Don't use simply MIN/MAX, as they're often defined elsewhere in include |
30 files that are included after this file generating tons of warnings. */ | |
31 #define I_MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
32 #define I_MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
33 | |
34 #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
|
35 #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
|
36 (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
0 | 37 |
38 #undef NVL | |
39 #define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr)) | |
40 | |
195
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
41 /* 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
|
42 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
|
43 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
|
44 #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
|
45 ((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
|
46 #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
|
47 ((type) ((const char *) (p) - (const char *) NULL)) |
0 | 48 |
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
|
49 /* 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
|
50 config.h may have already defined VA_COPY as va_copy or __va_copy. */ |
0 | 51 #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
|
52 # 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
|
53 (defined (_CALL_SYSV) || defined (_WIN32)) |
0 | 54 # define VA_COPY(ap1, ap2) (*(ap1) = *(ap2)) |
55 # elif defined (VA_COPY_AS_ARRAY) | |
1048
526415575295
No i_memmove here, just memmove.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
56 # define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list)) |
0 | 57 # else /* va_list is a pointer */ |
58 # define VA_COPY(ap1, ap2) ((ap1) = (ap2)) | |
59 # endif /* va_list is a pointer */ | |
60 #endif | |
61 | |
62 /* Provide convenience macros for handling structure | |
63 * fields through their offsets. | |
64 */ | |
65 #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
|
66 ((void *) ((char *) (struct_p) + (long) (struct_offset))) |
0 | 67 #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
|
68 (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset))) |
0 | 69 |
70 /* Provide simple macro statement wrappers (adapted from Perl): | |
71 STMT_START { statements; } STMT_END; | |
72 can be used as a single statement, as in | |
73 if (x) STMT_START { ... } STMT_END; else ... | |
74 | |
75 For gcc we will wrap the statements within `({' and `})' braces. | |
76 For SunOS they will be wrapped within `if (1)' and `else (void) 0', | |
77 and otherwise within `do' and `while (0)'. */ | |
78 #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
|
79 # 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
|
80 !defined (__STRICT_ANSI__) && !defined (PEDANTIC) |
0 | 81 # define STMT_START (void)( |
82 # define STMT_END ) | |
83 # else | |
84 # if (defined (sun) || defined (__sun__)) | |
85 # define STMT_START if (1) | |
86 # define STMT_END else (void)0 | |
87 # else | |
88 # define STMT_START do | |
89 # define STMT_END while (0) | |
90 # endif | |
91 # endif | |
92 #endif | |
93 | |
94 /* Provide macros to feature the GCC function attribute. */ | |
95 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) | |
4821
d972f9e2902e
Define __attrs_used__ if the compiler actually supports attributes.
Timo Sirainen <tss@iki.fi>
parents:
4720
diff
changeset
|
96 # define __attrs_used__ |
0 | 97 # define __attr_format__(format_idx, arg_idx) \ |
98 __attribute__((format (printf, format_idx, arg_idx))) | |
99 # define __attr_format_arg__(arg_idx) \ | |
100 __attribute__((format_arg (arg_idx))) | |
1409 | 101 # define __attr_scanf__(format_idx, arg_idx) \ |
102 __attribute__((format (scanf, format_idx, arg_idx))) | |
0 | 103 # define __attr_unused__ __attribute__((unused)) |
104 # define __attr_noreturn__ __attribute__((noreturn)) | |
105 # define __attr_const__ __attribute__((const)) | |
4720
b0daeec3d416
Use malloc attribute for the most commonly used memory and string allocation
Timo Sirainen <tss@iki.fi>
parents:
4719
diff
changeset
|
106 # define __attr_malloc__ __attribute__((malloc)) |
b0daeec3d416
Use malloc attribute for the most commonly used memory and string allocation
Timo Sirainen <tss@iki.fi>
parents:
4719
diff
changeset
|
107 # define __attr_warn_unused_result__ __attribute__((warn_unused_result)) |
4719
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
108 # if __GNUC__ > 3 |
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
109 /* GCC 4.0 and later */ |
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
110 # define __attr_sentinel__ __attribute__((sentinel)) |
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
111 # else |
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
112 # define __attr_sentinel__ |
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
113 # endif |
0 | 114 #else |
115 # define __attr_format__(format_idx, arg_idx) | |
116 # define __attr_format_arg__(arg_idx) | |
117 # define __attr_unused__ | |
118 # define __attr_noreturn__ | |
119 # define __attr_const__ | |
120 # define __attr_unused__ | |
4719
1995cb3763db
Added sentinel GCC attribute to *_strconcat() functions. Added
Timo Sirainen <tss@iki.fi>
parents:
2755
diff
changeset
|
121 # define __attr_sentinel__ |
0 | 122 #endif |
123 | |
1052
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
124 /* 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
|
125 #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
|
126 # 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
|
127 #else |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
128 # 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
|
129 #endif |
2f6225f9c170
Removed STRUCT_OFFSET, offsetof() is ANSI-C for the same purpose. Added
Timo Sirainen <tss@iki.fi>
parents:
1048
diff
changeset
|
130 |
0 | 131 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with |
132 macros, so we can refer to them as strings unconditionally. */ | |
133 #ifdef __GNUC__ | |
134 # define GNUC_FUNCTION __FUNCTION__ | |
135 # define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ | |
136 #else | |
137 # define GNUC_FUNCTION "" | |
138 # define GNUC_PRETTY_FUNCTION "" | |
139 #endif | |
140 | |
141 /* 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
|
142 #ifdef DISABLE_ASSERTS |
0 | 143 # define i_assert(expr) |
144 #elif defined (__GNUC__) && !defined (__STRICT_ANSI__) | |
145 | |
146 #define i_assert(expr) STMT_START{ \ | |
147 if (!(expr)) \ | |
148 i_panic("file %s: line %d (%s): assertion failed: (%s)", \ | |
149 __FILE__, \ | |
150 __LINE__, \ | |
151 __PRETTY_FUNCTION__, \ | |
152 #expr); }STMT_END | |
153 | |
154 #else /* !__GNUC__ */ | |
155 | |
156 #define i_assert(expr) STMT_START{ \ | |
157 if (!(expr)) \ | |
158 i_panic("file %s: line %d: assertion failed: (%s)", \ | |
159 __FILE__, \ | |
160 __LINE__, \ | |
161 #expr); }STMT_END | |
162 | |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
163 #endif |
0 | 164 |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
165 #define i_unreached() \ |
2755
13c70c53cb55
i_unreached(): show also file and line number in error message
Timo Sirainen <tss@iki.fi>
parents:
1915
diff
changeset
|
166 i_panic("file %s: line %d: unreached", __FILE__, __LINE__) |
0 | 167 |
168 #endif |