Mercurial > dovecot > core-2.2
annotate src/lib/macros.h @ 22955:812e5c961328
fts: Indexing virtual mailbox didn't always index the last mails
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 03 May 2018 18:33:00 +0300 |
parents | 31856a233751 |
children |
rev | line source |
---|---|
6410
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
6128
diff
changeset
|
1 #ifndef MACROS_H |
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
6128
diff
changeset
|
2 #define MACROS_H |
0 | 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 | |
6128 | 18 #define N_ELEMENTS(arr) \ |
19 (sizeof(arr) / sizeof((arr)[0])) | |
20 | |
0 | 21 #define MEM_ALIGN(size) \ |
17446
f9349a3c2604
lib: Fix MEM_ALIGN to cope with huge allocations
Phil Carmody <phil@dovecot.fi>
parents:
16708
diff
changeset
|
22 (((size) + MEM_ALIGN_SIZE-1) & ~((size_t) MEM_ALIGN_SIZE-1)) |
0 | 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 |
22931
31856a233751
lib: introduce container_of
Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
parents:
22463
diff
changeset
|
29 #define container_of(ptr, type, name) \ |
31856a233751
lib: introduce container_of
Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
parents:
22463
diff
changeset
|
30 (type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, name) + \ |
31856a233751
lib: introduce container_of
Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
parents:
22463
diff
changeset
|
31 COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(ptr, &((type *) 0)->name)) |
31856a233751
lib: introduce container_of
Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
parents:
22463
diff
changeset
|
32 |
0 | 33 /* Don't use simply MIN/MAX, as they're often defined elsewhere in include |
34 files that are included after this file generating tons of warnings. */ | |
35 #define I_MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
36 #define I_MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
37 | |
195
db6e288be0e9
Replaced INT_TO_POINTER and POINTER_TO_INT macros with POINTER_CAST and
Timo Sirainen <tss@iki.fi>
parents:
193
diff
changeset
|
38 /* 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
|
39 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
|
40 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
|
41 #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
|
42 ((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
|
43 #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
|
44 ((type) ((const char *) (p) - (const char *) NULL)) |
0 | 45 |
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
|
46 /* 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
|
47 config.h may have already defined VA_COPY as va_copy or __va_copy. */ |
0 | 48 #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
|
49 # 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
|
50 (defined (_CALL_SYSV) || defined (_WIN32)) |
0 | 51 # define VA_COPY(ap1, ap2) (*(ap1) = *(ap2)) |
52 # elif defined (VA_COPY_AS_ARRAY) | |
1048
526415575295
No i_memmove here, just memmove.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
53 # define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list)) |
0 | 54 # else /* va_list is a pointer */ |
55 # define VA_COPY(ap1, ap2) ((ap1) = (ap2)) | |
56 # endif /* va_list is a pointer */ | |
57 #endif | |
58 | |
59 /* Provide convenience macros for handling structure | |
60 * fields through their offsets. | |
61 */ | |
62 #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
|
63 ((void *) ((char *) (struct_p) + (long) (struct_offset))) |
10997
961ded33ca0a
Added CONST_STRUCT_MEMBER_P() and removed unused (broken) STRUCT_MEMBER().
Timo Sirainen <tss@iki.fi>
parents:
10967
diff
changeset
|
64 #define CONST_STRUCT_MEMBER_P(struct_p, struct_offset) \ |
961ded33ca0a
Added CONST_STRUCT_MEMBER_P() and removed unused (broken) STRUCT_MEMBER().
Timo Sirainen <tss@iki.fi>
parents:
10967
diff
changeset
|
65 ((const void *) ((const char *) (struct_p) + (long) (struct_offset))) |
0 | 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) | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
93 # define ATTRS_DEFINED |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
94 # define ATTR_FORMAT(format_idx, arg_idx) \ |
0 | 95 __attribute__((format (printf, format_idx, arg_idx))) |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
96 # define ATTR_FORMAT_ARG(arg_idx) \ |
0 | 97 __attribute__((format_arg (arg_idx))) |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
98 # define ATTR_SCANF(format_idx, arg_idx) \ |
1409 | 99 __attribute__((format (scanf, format_idx, arg_idx))) |
13923 | 100 # define ATTR_STRFTIME(format_idx) \ |
101 __attribute__((format (strftime, format_idx, 0))) | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
102 # define ATTR_UNUSED __attribute__((unused)) |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
103 # define ATTR_NORETURN __attribute__((noreturn)) |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
104 # define ATTR_CONST __attribute__((const)) |
7911
3d65aeed7ea0
Added ATTR_PURE macro, which expands to pure attribute if available.
Timo Sirainen <tss@iki.fi>
parents:
7868
diff
changeset
|
105 # define ATTR_PURE __attribute__((pure)) |
0 | 106 #else |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
107 # define ATTR_FORMAT(format_idx, arg_idx) |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
108 # define ATTR_FORMAT_ARG(arg_idx) |
13923 | 109 # define ATTR_SCANF(format_idx, arg_idx) |
110 # define ATTR_STRFTIME(format_idx) | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
111 # define ATTR_UNUSED |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
112 # define ATTR_NORETURN |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
113 # define ATTR_CONST |
7911
3d65aeed7ea0
Added ATTR_PURE macro, which expands to pure attribute if available.
Timo Sirainen <tss@iki.fi>
parents:
7868
diff
changeset
|
114 # define ATTR_PURE |
7204
c3cb23ed73d1
Use malloc attribute, __builtin_expect() and __builtin_types_compatible_p()
Timo Sirainen <tss@iki.fi>
parents:
6862
diff
changeset
|
115 #endif |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
13923
diff
changeset
|
116 #ifdef HAVE_ATTR_NULL |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
13923
diff
changeset
|
117 # define ATTR_NULL(...) __attribute__((null(__VA_ARGS__))) |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
13923
diff
changeset
|
118 #else |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
13923
diff
changeset
|
119 # define ATTR_NULL(...) |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
13923
diff
changeset
|
120 #endif |
14684
b39e93576e6a
Added ATTR_NOWARN_UNUSED_RESULT, which expands to nowarn_unused_result attribute if supported.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
121 #ifdef HAVE_ATTR_NOWARN_UNUSED_RESULT |
b39e93576e6a
Added ATTR_NOWARN_UNUSED_RESULT, which expands to nowarn_unused_result attribute if supported.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
122 # define ATTR_NOWARN_UNUSED_RESULT __attribute__((nowarn_unused_result)) |
b39e93576e6a
Added ATTR_NOWARN_UNUSED_RESULT, which expands to nowarn_unused_result attribute if supported.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
123 #else |
b39e93576e6a
Added ATTR_NOWARN_UNUSED_RESULT, which expands to nowarn_unused_result attribute if supported.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
124 # define ATTR_NOWARN_UNUSED_RESULT |
b39e93576e6a
Added ATTR_NOWARN_UNUSED_RESULT, which expands to nowarn_unused_result attribute if supported.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
125 #endif |
7204
c3cb23ed73d1
Use malloc attribute, __builtin_expect() and __builtin_types_compatible_p()
Timo Sirainen <tss@iki.fi>
parents:
6862
diff
changeset
|
126 #if __GNUC__ > 2 |
c3cb23ed73d1
Use malloc attribute, __builtin_expect() and __builtin_types_compatible_p()
Timo Sirainen <tss@iki.fi>
parents:
6862
diff
changeset
|
127 # define ATTR_MALLOC __attribute__((malloc)) |
c3cb23ed73d1
Use malloc attribute, __builtin_expect() and __builtin_types_compatible_p()
Timo Sirainen <tss@iki.fi>
parents:
6862
diff
changeset
|
128 #else |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
129 # define ATTR_MALLOC |
5672
ca38eb4189e8
Added missing __attr_*__ macros for non-GCC compilers.
Timo Sirainen <tss@iki.fi>
parents:
5449
diff
changeset
|
130 #endif |
ca38eb4189e8
Added missing __attr_*__ macros for non-GCC compilers.
Timo Sirainen <tss@iki.fi>
parents:
5449
diff
changeset
|
131 #if __GNUC__ > 3 |
ca38eb4189e8
Added missing __attr_*__ macros for non-GCC compilers.
Timo Sirainen <tss@iki.fi>
parents:
5449
diff
changeset
|
132 /* GCC 4.0 and later */ |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
133 # define ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
134 # define ATTR_SENTINEL __attribute__((sentinel)) |
5672
ca38eb4189e8
Added missing __attr_*__ macros for non-GCC compilers.
Timo Sirainen <tss@iki.fi>
parents:
5449
diff
changeset
|
135 #else |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
136 # define ATTR_WARN_UNUSED_RESULT |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
137 # define ATTR_SENTINEL |
0 | 138 #endif |
9946
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
139 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
140 /* GCC 4.3 and later */ |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
141 # define ATTR_HOT __attribute__((hot)) |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
142 # define ATTR_COLD __attribute__((cold)) |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
143 #else |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
144 # define ATTR_HOT |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
145 # define ATTR_COLD |
12089b6343bf
Added ATTR_HOT and ATTR_COLD macros. Use them in a couple of places.
Timo Sirainen <tss@iki.fi>
parents:
7911
diff
changeset
|
146 #endif |
17502
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
147 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) |
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
148 /* GCC 4.9 and later */ |
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
149 # define ATTR_RETURNS_NONNULL __attribute__((returns_nonnull)) |
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
150 #else |
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
151 # define ATTR_RETURNS_NONNULL |
6abf982c268d
lib: Use __attribute__((returns_nonnull)) for the common memory/string functions.
Timo Sirainen <tss@iki.fi>
parents:
17446
diff
changeset
|
152 #endif |
0 | 153 |
4903
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4821
diff
changeset
|
154 /* Macros to provide type safety for callback functions' context parameters */ |
21910
0bcfd58922bd
lib: Add a common HAVE_TYPE_CHECKS
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21357
diff
changeset
|
155 #ifdef HAVE_TYPE_CHECKS |
14921
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14918
diff
changeset
|
156 # define CALLBACK_TYPECHECK(callback, type) \ |
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14918
diff
changeset
|
157 (COMPILE_ERROR_IF_TRUE(!__builtin_types_compatible_p( \ |
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14918
diff
changeset
|
158 typeof(&callback), type)) ? 1 : 0) |
4903
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4821
diff
changeset
|
159 #else |
14921
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14918
diff
changeset
|
160 # define CALLBACK_TYPECHECK(callback, type) 0 |
4903
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4821
diff
changeset
|
161 #endif |
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4821
diff
changeset
|
162 |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
163 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0)) && !defined(__cplusplus) |
5449
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
164 # define COMPILE_ERROR_IF_TRUE(condition) \ |
22463
eb2db4c6e574
lib: Fix compiler warning about mixing boolean with multiplication
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21910
diff
changeset
|
165 (sizeof(char[1 - 2 * ((condition) ? 1 : 0)]) - 1) |
19707
55397852db0a
lib: Avoid typeof() if HAVE_TYPEOF isn't set, even if gcc version is high enough.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
17624
diff
changeset
|
166 #else |
55397852db0a
lib: Avoid typeof() if HAVE_TYPEOF isn't set, even if gcc version is high enough.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
17624
diff
changeset
|
167 # define COMPILE_ERROR_IF_TRUE(condition) 0 |
55397852db0a
lib: Avoid typeof() if HAVE_TYPEOF isn't set, even if gcc version is high enough.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
17624
diff
changeset
|
168 #endif |
55397852db0a
lib: Avoid typeof() if HAVE_TYPEOF isn't set, even if gcc version is high enough.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
17624
diff
changeset
|
169 |
21910
0bcfd58922bd
lib: Add a common HAVE_TYPE_CHECKS
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21357
diff
changeset
|
170 #ifdef HAVE_TYPE_CHECKS |
5449
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
171 # define COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(_a, _b) \ |
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
172 COMPILE_ERROR_IF_TRUE( \ |
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
173 !__builtin_types_compatible_p(typeof(_a), typeof(_b))) |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
174 #define COMPILE_ERROR_IF_TYPES2_NOT_COMPATIBLE(_a1, _a2, _b) \ |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
175 COMPILE_ERROR_IF_TRUE( \ |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
176 !__builtin_types_compatible_p(typeof(_a1), typeof(_b)) && \ |
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
177 !__builtin_types_compatible_p(typeof(_a2), typeof(_b))) |
5449
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
178 #else |
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
179 # define COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(_a, _b) 0 |
14918
8eae4e205c82
Hash table API is now (mostly) type safe.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
180 # define COMPILE_ERROR_IF_TYPES2_NOT_COMPATIBLE(_a1, _a2, _b) 0 |
5449
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
181 #endif |
bca896b09c7a
Added HAVE_TYPEOF, COMPILE_ERROR_IF_TRUE() and
Timo Sirainen <tss@iki.fi>
parents:
5190
diff
changeset
|
182 |
7204
c3cb23ed73d1
Use malloc attribute, __builtin_expect() and __builtin_types_compatible_p()
Timo Sirainen <tss@iki.fi>
parents:
6862
diff
changeset
|
183 #if __GNUC__ > 2 |
6788
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
184 # define unlikely(expr) __builtin_expect(!!(expr), 0) |
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
185 # define likely(expr) __builtin_expect(!!(expr), 1) |
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
186 #else |
6862
ff884d70197b
unlikely() and likely() were broken with non-gcc.
Timo Sirainen <tss@iki.fi>
parents:
6788
diff
changeset
|
187 # define unlikely(expr) expr |
ff884d70197b
unlikely() and likely() were broken with non-gcc.
Timo Sirainen <tss@iki.fi>
parents:
6788
diff
changeset
|
188 # define likely(expr) expr |
6788
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
189 #endif |
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
190 |
21269
b3cb705f1dc6
lib/macros.h - protect old compilers from use of modern features
Phil Carmody <phil@dovecot.fi>
parents:
21208
diff
changeset
|
191 #if defined(__clang__) && ((__clang_major__ > 4) || (__clang_major__ == 3 && __clang_minor__ >= 9)) |
21208
0563b9a51df3
lib: Add ATTR_UNSIGNED_WRAPS for disabling clang -fsanitize=integer
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21095
diff
changeset
|
192 # define ATTR_UNSIGNED_WRAPS __attribute__((no_sanitize("integer"))) |
0563b9a51df3
lib: Add ATTR_UNSIGNED_WRAPS for disabling clang -fsanitize=integer
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21095
diff
changeset
|
193 #else |
0563b9a51df3
lib: Add ATTR_UNSIGNED_WRAPS for disabling clang -fsanitize=integer
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21095
diff
changeset
|
194 # define ATTR_UNSIGNED_WRAPS |
0563b9a51df3
lib: Add ATTR_UNSIGNED_WRAPS for disabling clang -fsanitize=integer
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21095
diff
changeset
|
195 #endif |
0563b9a51df3
lib: Add ATTR_UNSIGNED_WRAPS for disabling clang -fsanitize=integer
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21095
diff
changeset
|
196 |
0 | 197 /* 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
|
198 #ifdef DISABLE_ASSERTS |
0 | 199 # define i_assert(expr) |
200 #elif defined (__GNUC__) && !defined (__STRICT_ANSI__) | |
201 | |
202 #define i_assert(expr) STMT_START{ \ | |
6788
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
203 if (unlikely(!(expr))) \ |
0 | 204 i_panic("file %s: line %d (%s): assertion failed: (%s)", \ |
6788
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
205 __FILE__, \ |
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
206 __LINE__, \ |
10967
56fd824b1d65
Asserts now use __FUNCTION__ instead of __PRETTY_FUNCTION__.
Timo Sirainen <tss@iki.fi>
parents:
10966
diff
changeset
|
207 __FUNCTION__, \ |
0 | 208 #expr); }STMT_END |
209 | |
210 #else /* !__GNUC__ */ | |
211 | |
212 #define i_assert(expr) STMT_START{ \ | |
6788
993760061348
Added likely() and unlikely() macros. Use unlikely() with asserts.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
213 if (unlikely(!(expr))) \ |
0 | 214 i_panic("file %s: line %d: assertion failed: (%s)", \ |
215 __FILE__, \ | |
216 __LINE__, \ | |
217 #expr); }STMT_END | |
218 | |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
219 #endif |
0 | 220 |
21095
2baad5f9d56c
lib: Assert-crash in i_close_fd() if fd == 0
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20474
diff
changeset
|
221 /* Close the fd and set it to -1. This assert-crashes if fd == 0. Normally |
2baad5f9d56c
lib: Assert-crash in i_close_fd() if fd == 0
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20474
diff
changeset
|
222 this would happen only if an uninitialized fd is attempted to be closed, |
2baad5f9d56c
lib: Assert-crash in i_close_fd() if fd == 0
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20474
diff
changeset
|
223 which is a bug. */ |
14687
7c058aa05b0a
Replaced (void)close(fd) and close_keep_errno() with i_close_fd().
Timo Sirainen <tss@iki.fi>
parents:
14684
diff
changeset
|
224 #define i_close_fd(fd) STMT_START { \ |
21095
2baad5f9d56c
lib: Assert-crash in i_close_fd() if fd == 0
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20474
diff
changeset
|
225 i_assert(*fd > 0); \ |
16708
39972f75b143
i_close_fd() didn't handle errors correctly with gcc.
Timo Sirainen <tss@iki.fi>
parents:
16410
diff
changeset
|
226 if (unlikely(close_keep_errno(fd) < 0)) \ |
14687
7c058aa05b0a
Replaced (void)close(fd) and close_keep_errno() with i_close_fd().
Timo Sirainen <tss@iki.fi>
parents:
14684
diff
changeset
|
227 i_error("close(%d[%s:%d]) failed: %m", \ |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
228 *(fd), __FILE__, __LINE__); \ |
14687
7c058aa05b0a
Replaced (void)close(fd) and close_keep_errno() with i_close_fd().
Timo Sirainen <tss@iki.fi>
parents:
14684
diff
changeset
|
229 } STMT_END |
7c058aa05b0a
Replaced (void)close(fd) and close_keep_errno() with i_close_fd().
Timo Sirainen <tss@iki.fi>
parents:
14684
diff
changeset
|
230 |
546
e1254b838e0b
Added --enable-asserts (default) and fixed some warnings when building
Timo Sirainen <tss@iki.fi>
parents:
195
diff
changeset
|
231 #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
|
232 i_panic("file %s: line %d: unreached", __FILE__, __LINE__) |
0 | 233 |
16181
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
234 /* Convenience macros to test the versions of dovecot. */ |
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
235 #if defined DOVECOT_VERSION_MAJOR && defined DOVECOT_VERSION_MINOR |
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
236 # define DOVECOT_PREREQ(maj, min) \ |
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
237 ((DOVECOT_VERSION_MAJOR << 16) + DOVECOT_VERSION_MINOR >= ((maj) << 16) + (min)) |
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
238 #else |
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
239 # define DOVECOT_PREREQ(maj, min) 0 |
0 | 240 #endif |
16181
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
241 |
20328
7e016f5e8cb4
[LEN] to [static LEN] on some function parameters
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents:
19707
diff
changeset
|
242 #ifdef __cplusplus |
20474
3f63a4e80fe1
configure: Check if C99 "static" keyword is supported for array sizes.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20328
diff
changeset
|
243 # undef STATIC_ARRAY |
20328
7e016f5e8cb4
[LEN] to [static LEN] on some function parameters
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents:
19707
diff
changeset
|
244 # define STATIC_ARRAY |
16181
a588b743d695
Add DOVECOT_PREREQ to src/lib/macros.h - convenience macro to test the version of dovecot
Dennis Schridde <devurandom@gmx.net>
parents:
13923
diff
changeset
|
245 #endif |
20328
7e016f5e8cb4
[LEN] to [static LEN] on some function parameters
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents:
19707
diff
changeset
|
246 |
21357
33ff22751f44
lib: Created i_zero() wrapper for memset(p, 0, sizeof(*p)).
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21269
diff
changeset
|
247 /* Convenience wrappers for initializing a struct */ |
33ff22751f44
lib: Created i_zero() wrapper for memset(p, 0, sizeof(*p)).
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21269
diff
changeset
|
248 #define i_zero(p) memset(p, 0, sizeof(*(p))) |
33ff22751f44
lib: Created i_zero() wrapper for memset(p, 0, sizeof(*p)).
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21269
diff
changeset
|
249 #define i_zero_safe(p) safe_memset(p, 0, sizeof(*(p))) |
33ff22751f44
lib: Created i_zero() wrapper for memset(p, 0, sizeof(*p)).
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21269
diff
changeset
|
250 |
20328
7e016f5e8cb4
[LEN] to [static LEN] on some function parameters
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents:
19707
diff
changeset
|
251 #endif |