2 * Include file with general macros and typedefs used through Captive project
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
20 #ifndef _CAPTIVE_MACROS_H
21 #define _CAPTIVE_MACROS_H 1
24 #include <glib/gmacros.h>
25 #include <glib/gmem.h>
27 #include <string.h> /* for memset() */
28 #include <glib/galloca.h>
29 #include <glib/gmessages.h>
37 * Prevent 'might be used uninitialized' warning.
38 * Macro will fakes the use of the variable as sometimes GCC can't code flow
39 * analyse #C correctly.
43 * g_some_type some_variable CAPTIVE_FAKEUSE;
47 #define CAPTIVE_FAKEUSE =0
52 * @objp: Variable with the pointer to the objects wished to be allocated.
53 * Original value is discarded.
54 * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
56 * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
57 * Allocated memory may contain garbage.
59 * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
60 * Value %NULL is returned iff @n==%0;
62 #define captive_newn(objp,n) ((objp)=g_new(typeof(*(objp)),(n)))
67 * @objp: Variable with the pointer to the objects wished to be allocated and precleared.
68 * Original value is discarded.
69 * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
71 * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
72 * Allocated memory is precleared.
74 * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp))*n.
75 * Value %NULL is returned iff @n==%0;
77 #define captive_new0n(objp,n) ((objp)=g_new0(typeof(*(objp)),(n)))
82 * @objp: Variable with the pointer to the objects wished to be reallocated.
83 * Value %NULL is permitted (g_malloc() effect).
84 * @n: Numbers of objects to be allocated. Value %0 is permitted (g_free() effect).
86 * Macro to reallocate the original memory stored in @objp
87 * to the size @n objects of type *@objp and to assign the resulting pointer to @objp.
88 * New allocated space may contain garbage. Both @objp and @n can be nonexclusively
91 * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
92 * Value %NULL is returned iff @n==%0;
94 #define captive_renewn(objp,n) ({ \
95 typeof(&(objp)) _captive_renewn_objpp=&(objp); \
96 *_captive_renewn_objpp=g_renew(typeof(*(objp)),*_captive_renewn_objpp,(n)); \
97 (*_captive_renewn_objpp); \
103 * @objp: Variable with the pointer to the object wished to be allocated.
104 * Original value is discarded.
106 * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
107 * Allocated memory may contain garbage. Equivalent to captive_newn(objp,1) call.
109 * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp)).
110 * Value %NULL is never returned.
112 #define captive_new(objp) (captive_newn((objp),1))
117 * @objp: Variable with the pointer to the object wished to be allocated and precleared.
118 * Original value is discarded.
120 * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
121 * Allocated memory is precleared. Equivalent to captive_new0n(objp,1) call.
123 * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp)).
124 * Value %NULL is never returned.
126 #define captive_new0(objp) (captive_new0n((objp),1))
130 * captive_newn_alloca:
131 * @objp: Variable with the pointer to the objects wished to be allocated.
132 * Original value is discarded.
133 * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
135 * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
136 * Allocated memory may contain garbage.
138 * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
139 * during exit of current function (or current block if variable sized variables present there).
140 * You cannot deallocate or reallocate such memory in any other way.
142 * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp))*n.
143 * Value %NULL is returned iff @n==%0;
145 #define captive_newn_alloca(objp,n) ({ \
146 gsize _captive_newn_alloca_n=(n); \
147 /* Fix 'g_alloca(0)!=NULL': */ \
148 ((objp)=(!_captive_newn_alloca_n ? NULL : g_alloca(sizeof(*(objp))*_captive_newn_alloca_n))); \
153 * captive_new0n_alloca:
154 * @objp: Variable with the pointer to the objects wished to be allocated and precleared.
155 * Original value is discarded.
156 * @n: Numbers of objects to be allocated. Value %0 is permitted (%NULL assignment effect).
158 * Macro to allocate @n objects of type *@objp and to assign the resulting pointer to @objp.
159 * Allocated memory is precleared.
161 * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
162 * during exit of current function (or current block if variable sized variables present there).
163 * You cannot deallocate or reallocate such memory in any other way.
165 * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp))*n.
166 * Value %NULL is returned iff @n==%0;
168 #define captive_new0n_alloca(objp,n) ({ \
169 typeof(&(objp)) _captive_new0n_alloca_objpp=&(objp); \
170 captive_newn_alloca(*_captive_new0n_alloca_objpp,(n)); \
171 CAPTIVE_MEMZERO(*_captive_new0n_alloca_objpp); \
172 (*_captive_new0n_alloca_objpp); \
177 * captive_new_alloca:
178 * @objp: Variable with the pointer to the object wished to be allocated.
179 * Original value is discarded.
181 * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
182 * Allocated memory may contain garbage. Equivalent to captive_newn_alloca(objp,1) call.
184 * Memory is allocated on the stack frame by g_alloca() and it will be automatically deallocated
185 * during exit of current function (or current block if variable sized variables present there).
186 * You cannot deallocate or reallocate such memory in any other way.
188 * @Returns: Initialized @objp value as the memory of size #sizeof(typeof(*objp)).
189 * Value %NULL is never returned.
191 #define captive_new_alloca(objp) (captive_newn_alloca((objp),1))
195 * captive_new0_alloca:
196 * @objp: Variable with the pointer to the object wished to be allocated and precleared.
197 * Original value is discarded.
199 * Macro to allocate one object of type *@objp and to assign the resulting pointer to @objp.
200 * Allocated memory is precleared. Equivalent to captive_new0n_alloca(objp,1) call.
202 * @Returns: Initialized @objp value as the cleared memory of size #sizeof(typeof(*objp)).
204 #define captive_new0_alloca(objp) (captive_new0n_alloca((objp),1))
209 * @objp: Variable to be filled from the next argument of @ap.
210 * @ap: Initialized #va_list type.
212 * Automatically determines the size of @objp.
213 * Equivalent to objp=va_arg(ap,typeof(objp)) call.
215 * @Returns: Initialized @objp value.
217 #define captive_va_arg(objp,ap) ((objp)=va_arg((ap),typeof(objp)))
222 * @objp: Pointer to the variable to be cleared.
224 * Clears the sizeof(*@objp) bytes of the given pointer with memset().
225 * Pass _pointer_ to the object to be cleared.
227 #define CAPTIVE_MEMZERO(objp) (memset((objp),0,sizeof(*(objp))))
231 * captive_printf_alloca:
232 * @format: Format string. See the sprintf() documentation.
233 * @args: Arguments for @format. See the sprintf() documentation.
235 * Format the given format string @format as in sprintf().
236 * Output buffer is allocated automatically and it does not need to be deallocated
237 * manually as it is managed by g_alloca().
239 * @Returns: Formatted output string located in g_alloca() memory.
241 #define captive_printf_alloca(format,args...) ({ \
242 gsize _captive_printf_alloca_size=captive_nv_printf_string_upper_bound((format) , ## args); \
243 gchar *_captive_printf_alloca_r=g_alloca(_captive_printf_alloca_size); \
244 g_snprintf(_captive_printf_alloca_r,_captive_printf_alloca_size,(format) , ## args); \
245 (const gchar *)_captive_printf_alloca_r; \
247 static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...) G_GNUC_PRINTF(1,0);
248 static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...)
254 r=g_printf_string_upper_bound(format,ap);
263 #endif /* _CAPTIVE_MACROS_H */