Remove unneeded dependency on glib-2.2.x by g_printf() & <glib/gprintf.h>
[captive.git] / src / libcaptive / include / captive / macros.h
1 /* $Id$
2  * Include file with general macros and typedefs used through Captive project
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #ifndef _CAPTIVE_MACROS_H
21 #define _CAPTIVE_MACROS_H 1
22
23
24 #include <glib/gmacros.h>
25 #include <glib/gmem.h>
26 #include <stdarg.h>
27 #include <string.h>     /* for memset() */
28 #include <glib/galloca.h>
29 #include <glib/gmessages.h>
30 #include <glib/gutils.h>        /* for g_snprintf(); glib-2.2.x+ it also has in <glib/gprintf.h> */
31
32
33 G_BEGIN_DECLS
34
35 /**
36  * CAPTIVE_FAKEUSE:
37  *
38  * Prevent 'might be used uninitialized' warning.
39  * Macro will fakes the use of the variable as sometimes GCC can't code flow
40  * analyse #C correctly.
41  *
42  * <informalexample>
43  * <programlisting>
44  * g_some_type some_variable CAPTIVE_FAKEUSE;
45  * </programlisting>
46  * </informalexample>
47  */
48 #define CAPTIVE_FAKEUSE =0
49
50
51 /**
52  * captive_newn:
53  * @objp: Variable with the pointer to the objects wished to be allocated.
54  * Original value is discarded.
55  * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
56  *
57  * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
58  * Allocated memory may contain garbage.
59  *
60  * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
61  * Value %NULL is returned iff @n==%0;
62  */
63 #define captive_newn(objp,n) ((objp)=g_new(typeof(*(objp)),(n)))
64
65
66 /**
67  * captive_new0n:
68  * @objp: Variable with the pointer to the objects wished to be allocated and precleared.
69  * Original value is discarded.
70  * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
71  *
72  * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
73  * Allocated memory is precleared.
74  * 
75  * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp))*n.
76  * Value %NULL is returned iff @n==%0;
77  */
78 #define captive_new0n(objp,n) ((objp)=g_new0(typeof(*(objp)),(n)))
79
80
81 /**
82  * captive_renewn:
83  * @objp: Variable with the pointer to the objects wished to be reallocated. 
84  * Value %NULL is permitted (g_malloc() effect).
85  * @n: Numbers of objects to be allocated. Value %0 is permitted (g_free() effect).
86  *
87  * Macro to reallocate the original memory stored in @objp
88  * to the size @n objects of type *@objp and to assign the resulting pointer to @objp.
89  * New allocated space may contain garbage. Both @objp and @n can be nonexclusively
90  * passed as zero.
91  * 
92  * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
93  * Value %NULL is returned iff @n==%0;
94  */
95 #define captive_renewn(objp,n) ({ \
96                 typeof(&(objp)) _captive_renewn_objpp=&(objp); \
97                 *_captive_renewn_objpp=g_renew(typeof(*(objp)),*_captive_renewn_objpp,(n)); \
98                 (*_captive_renewn_objpp); \
99                 })
100
101
102 /**
103  * captive_new:
104  * @objp: Variable with the pointer to the object wished to be allocated.
105  * Original value is discarded.
106  *
107  * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
108  * Allocated memory may contain garbage. Equivalent to captive_newn(objp,1) call.
109  *
110  * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp)).
111  * Value %NULL is never returned.
112  */
113 #define captive_new(objp) (captive_newn((objp),1))
114
115
116 /**
117  * captive_new0:
118  * @objp: Variable with the pointer to the object wished to be allocated and precleared.
119  * Original value is discarded.
120  *
121  * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
122  * Allocated memory is precleared. Equivalent to captive_new0n(objp,1) call.
123  * 
124  * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp)).
125  * Value %NULL is never returned.
126  */
127 #define captive_new0(objp) (captive_new0n((objp),1))
128
129
130 /**
131  * captive_newn_alloca:
132  * @objp: Variable with the pointer to the objects wished to be allocated.
133  * Original value is discarded.
134  * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
135  *
136  * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
137  * Allocated memory may contain garbage.
138  * 
139  * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
140  * during exit of current function (or current block if variable sized variables present there).
141  * You cannot deallocate or reallocate such memory in any other way.
142  *
143  * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
144  * Value %NULL is returned iff @n==%0;
145  */
146 #define captive_newn_alloca(objp,n) ({ \
147                 gsize _captive_newn_alloca_n=(n); \
148                 /* Fix 'g_alloca(0)!=NULL': */ \
149                 ((objp)=(!_captive_newn_alloca_n ? NULL : g_alloca(sizeof(*(objp))*_captive_newn_alloca_n))); \
150                 })
151
152
153 /**
154  * captive_new0n_alloca:
155  * @objp: Variable with the pointer to the objects wished to be allocated and precleared.
156  * Original value is discarded.
157  * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
158  *
159  * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
160  * Allocated memory is precleared.
161  * 
162  * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
163  * during exit of current function (or current block if variable sized variables present there).
164  * You cannot deallocate or reallocate such memory in any other way.
165  * 
166  * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp))*n.
167  * Value %NULL is returned iff @n==%0;
168  */
169 #define captive_new0n_alloca(objp,n) ({ \
170                 typeof(&(objp)) _captive_new0n_alloca_objpp=&(objp); \
171                 captive_newn_alloca(*_captive_new0n_alloca_objpp,(n)); \
172                 CAPTIVE_MEMZERO(*_captive_new0n_alloca_objpp); \
173                 (*_captive_new0n_alloca_objpp); \
174                 })
175
176
177 /**
178  * captive_new_alloca:
179  * @objp: Variable with the pointer to the object wished to be allocated.
180  * Original value is discarded.
181  *
182  * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
183  * Allocated memory may contain garbage. Equivalent to captive_newn_alloca(objp,1) call.
184  * 
185  * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
186  * during exit of current function (or current block if variable sized variables present there).
187  * You cannot deallocate or reallocate such memory in any other way.
188  *
189  * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp)).
190  * Value %NULL is never returned.
191  */
192 #define captive_new_alloca(objp) (captive_newn_alloca((objp),1))
193
194
195 /**
196  * captive_new0_alloca:
197  * @objp: Variable with the pointer to the object wished to be allocated and precleared.
198  * Original value is discarded.
199  *
200  * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
201  * Allocated memory is precleared. Equivalent to captive_new0n_alloca(objp,1) call.
202  * 
203  * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp)).
204  */
205 #define captive_new0_alloca(objp) (captive_new0n_alloca((objp),1))
206
207
208 /**
209  * captive_va_arg:
210  * @objp: Variable to be filled from the next argument of @ap.
211  * @ap: Initialized #va_list type.
212  *
213  * Automatically determines the size of @objp.
214  * Equivalent to objp=va_arg(ap,typeof(objp)) call.
215  *
216  * @Returns: Initialized @objp value.
217  */
218 #define captive_va_arg(objp,ap) ((objp)=va_arg((ap),typeof(objp)))
219
220
221 /**
222  * CAPTIVE_MEMZERO:
223  * @objp: Pointer to the variable to be cleared.
224  *
225  * Clears the sizeof(*@objp) bytes of the given pointer with memset().
226  * Pass _pointer_ to the object to be cleared.
227  */
228 #define CAPTIVE_MEMZERO(objp) (memset((objp),0,sizeof(*(objp))))
229
230
231 /**
232  * captive_printf_alloca:
233  * @format: Format string. See the sprintf() documentation.
234  * @args...: Arguments for @format. See the sprintf() documentation.
235  *
236  * Format the given format string @format as in sprintf().
237  * Output buffer is allocated automatically and it does not need to be deallocated
238  * manually as it is managed by g_alloca().
239  *
240  * @Returns: Formatted output string located in g_alloca() memory.
241  */
242 #define captive_printf_alloca(format,args...) ({ \
243                 gsize _captive_printf_alloca_size=captive_nv_printf_string_upper_bound((format) , ## args); \
244                 gchar *_captive_printf_alloca_r=g_alloca(_captive_printf_alloca_size); \
245                 g_snprintf(_captive_printf_alloca_r,_captive_printf_alloca_size,(format) , ## args); \
246                 (const gchar *)_captive_printf_alloca_r; \
247                 })
248 static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...) G_GNUC_PRINTF(1,0) G_GNUC_UNUSED;
249 static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...)
250 {
251 va_list ap;
252 gsize r;
253
254         va_start(ap,format);
255         r=g_printf_string_upper_bound(format,ap);
256         va_end(ap);
257         return r;
258 }
259
260
261 /**
262  * captive_strdup_alloca:
263  * @string: #const #gchar * string to duplicate.
264  *
265  * Macro to do g_strdup() equivalent in g_alloca() style.
266  * 
267  * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
268  * during exit of current function (or current block if variable sized variables present there).
269  * You cannot deallocate or reallocate such memory in any other way.
270  *
271  * @Returns: Duplicated @string. You may modify its items if the length is not changed.
272  */
273 #define captive_strdup_alloca(string) ({ \
274                 const gchar *_captive_strdup_alloca_string=(string); \
275                 gchar *_captive_strdup_alloca_r=g_alloca(strlen(_captive_strdup_alloca_string)+1); \
276                 strcpy(_captive_strdup_alloca_r,_captive_strdup_alloca_string); \
277                 (const gchar *)(_captive_strdup_alloca_string); \
278                 })
279
280
281 /**
282  * CAPTIVE_ROUND_DOWN:
283  * @pointer: Arbitrary pointer type.
284  * @fragment: Amount of 'sizeof(char)' to align @pointer down to.
285  * This size will be typically a power of 2.
286  * Value less or equal to %0 is forbidden.
287  *
288  * General pointer down-rounding macro. Already aligned pointer is left as is.
289  *
290  * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
291  *
292  * @Returns: Down-rounded @pointer to the integer multiple of @fragment.
293  * Resulting pointer has the same type as @pointer.
294  */
295 #define CAPTIVE_ROUND_DOWN(pointer,fragment) \
296                 ((typeof(pointer))(((guint32)(pointer))-CAPTIVE_ROUND_DOWN_EXCEEDING((pointer),(fragment))))
297 #define CAPTIVE_ROUND_DOWN64(pointer,fragment) \
298                 ((typeof(pointer))(((guint64)(pointer))-CAPTIVE_ROUND_DOWN_EXCEEDING64((pointer),(fragment))))
299
300
301 /**
302  * CAPTIVE_ROUND_DOWN_EXCEEDING:
303  * @pointer: Arbitrary pointer type.
304  * @fragment: Amount of 'sizeof(char)' to detect down-alignment amount of @pointer for.
305  * This size will be typically a power of 2.
306  * Value less or equal to %0 is forbidden.
307  *
308  * Detects current non-aligned amount of data exceeding over integer multiple of @fragment.
309  * It will return value %0 for an aligned pointer.
310  *
311  * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
312  *
313  * @Returns: #gsize typed number of bytes exceeding over integer multiple of @fragment.
314  */
315 #define CAPTIVE_ROUND_DOWN_EXCEEDING(pointer,fragment) \
316                 ((gsize)(((guint32)(pointer))%(fragment)))
317 #define CAPTIVE_ROUND_DOWN_EXCEEDING64(pointer,fragment) \
318                 ((gsize)(((guint64)(pointer))%(fragment)))
319
320
321 /**
322  * CAPTIVE_ROUND_UP:
323  * @pointer: Arbitrary pointer type.
324  * @fragment: Amount of 'sizeof(char)' to align @pointer up to.
325  * This size will be typically a power of 2.
326  * Value less or equal to %0 is forbidden.
327  *
328  * General pointer up-rounding macro. Already aligned pointer is left as is.
329  *
330  * glib NOTE: YOU MAY NOT STORE POINTERS IN INTEGERS.
331  *
332  * @Returns: Up-rounded @pointer to the integer multiple of @fragment.
333  * Resulting pointer has the same type as @pointer.
334  */
335 #define CAPTIVE_ROUND_UP(pointer,fragment) \
336                 /* new retyping as 'pointer' gets passed as 'char *' to CAPTIVE_ROUND_DOWN(). */ \
337                 ((typeof(pointer))(CAPTIVE_ROUND_DOWN(((guint32)(pointer))+(fragment)-1,(fragment))))
338 #define CAPTIVE_ROUND_UP64(pointer,fragment) \
339                 /* new retyping as 'pointer' gets passed as 'char *' to CAPTIVE_ROUND_DOWN(). */ \
340                 ((typeof(pointer))(CAPTIVE_ROUND_DOWN64(((guint64)(pointer))+(fragment)-1,(fragment))))
341
342 G_END_DECLS
343
344
345 #endif /* _CAPTIVE_MACROS_H */