/src/glib/gobject/gclosure.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 2000-2001 Red Hat, Inc. |
3 | | * Copyright (C) 2005 Imendio AB |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU Lesser General |
16 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * MT safe with regards to reference counting. |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include "../glib/gvalgrind.h" |
26 | | #include <string.h> |
27 | | |
28 | | #include <ffi.h> |
29 | | |
30 | | #include "gclosure.h" |
31 | | #include "gboxed.h" |
32 | | #include "gobject.h" |
33 | | #include "genums.h" |
34 | | #include "gvalue.h" |
35 | | #include "gvaluetypes.h" |
36 | | #include "gtype-private.h" |
37 | | |
38 | | |
39 | | /** |
40 | | * SECTION:gclosure |
41 | | * @short_description: Functions as first-class objects |
42 | | * @title: Closures |
43 | | * |
44 | | * A #GClosure represents a callback supplied by the programmer. It |
45 | | * will generally comprise a function of some kind and a marshaller |
46 | | * used to call it. It is the responsibility of the marshaller to |
47 | | * convert the arguments for the invocation from #GValues into |
48 | | * a suitable form, perform the callback on the converted arguments, |
49 | | * and transform the return value back into a #GValue. |
50 | | * |
51 | | * In the case of C programs, a closure usually just holds a pointer |
52 | | * to a function and maybe a data argument, and the marshaller |
53 | | * converts between #GValue and native C types. The GObject |
54 | | * library provides the #GCClosure type for this purpose. Bindings for |
55 | | * other languages need marshallers which convert between #GValues |
56 | | * and suitable representations in the runtime of the language in |
57 | | * order to use functions written in that language as callbacks. Use |
58 | | * g_closure_set_marshal() to set the marshaller on such a custom |
59 | | * closure implementation. |
60 | | * |
61 | | * Within GObject, closures play an important role in the |
62 | | * implementation of signals. When a signal is registered, the |
63 | | * @c_marshaller argument to g_signal_new() specifies the default C |
64 | | * marshaller for any closure which is connected to this |
65 | | * signal. GObject provides a number of C marshallers for this |
66 | | * purpose, see the g_cclosure_marshal_*() functions. Additional C |
67 | | * marshallers can be generated with the [glib-genmarshal][glib-genmarshal] |
68 | | * utility. Closures can be explicitly connected to signals with |
69 | | * g_signal_connect_closure(), but it usually more convenient to let |
70 | | * GObject create a closure automatically by using one of the |
71 | | * g_signal_connect_*() functions which take a callback function/user |
72 | | * data pair. |
73 | | * |
74 | | * Using closures has a number of important advantages over a simple |
75 | | * callback function/data pointer combination: |
76 | | * |
77 | | * - Closures allow the callee to get the types of the callback parameters, |
78 | | * which means that language bindings don't have to write individual glue |
79 | | * for each callback type. |
80 | | * |
81 | | * - The reference counting of #GClosure makes it easy to handle reentrancy |
82 | | * right; if a callback is removed while it is being invoked, the closure |
83 | | * and its parameters won't be freed until the invocation finishes. |
84 | | * |
85 | | * - g_closure_invalidate() and invalidation notifiers allow callbacks to be |
86 | | * automatically removed when the objects they point to go away. |
87 | | */ |
88 | | |
89 | | #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1) |
90 | | #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1) |
91 | | #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1) |
92 | | #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1) |
93 | 0 | #define CLOSURE_N_MFUNCS(cl) (((cl)->n_guards << 1L)) |
94 | | /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */ |
95 | 0 | #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \ |
96 | 0 | (cl)->n_fnotifiers + \ |
97 | 0 | (cl)->n_inotifiers) |
98 | | |
99 | | typedef union { |
100 | | GClosure closure; |
101 | | gint vint; |
102 | | } ClosureInt; |
103 | | |
104 | 180 | #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \ |
105 | 180 | G_STMT_START { \ |
106 | 180 | ClosureInt *cunion = (ClosureInt*) _closure; \ |
107 | 180 | gint new_int, old_int, success; \ |
108 | 180 | do \ |
109 | 180 | { \ |
110 | 180 | ClosureInt tmp; \ |
111 | 180 | tmp.vint = old_int = cunion->vint; \ |
112 | 180 | _SET_OLD tmp.closure._field; \ |
113 | 180 | tmp.closure._field _OP _value; \ |
114 | 180 | _SET_NEW tmp.closure._field; \ |
115 | 180 | new_int = tmp.vint; \ |
116 | 180 | success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \ |
117 | 180 | } \ |
118 | 180 | while (!success && _must_set); \ |
119 | 180 | } G_STMT_END |
120 | | |
121 | 36 | #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) ) |
122 | 72 | #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) ) |
123 | 0 | #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) ) |
124 | 36 | #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = ) |
125 | 0 | #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) ) |
126 | 36 | #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = ) |
127 | | |
128 | | #if 0 /* for non-thread-safe closures */ |
129 | | #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v) |
130 | | #define SET(cl,f,v) (void) (cl->f = v) |
131 | | #define INC(cl,f) (void) (cl->f += 1) |
132 | | #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f) |
133 | | #define DEC(cl,f) (void) (cl->f -= 1) |
134 | | #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f) |
135 | | #endif |
136 | | |
137 | | enum { |
138 | | FNOTIFY, |
139 | | INOTIFY, |
140 | | PRE_NOTIFY, |
141 | | POST_NOTIFY |
142 | | }; |
143 | | |
144 | | |
145 | | /* --- functions --- */ |
146 | | /** |
147 | | * g_closure_new_simple: |
148 | | * @sizeof_closure: the size of the structure to allocate, must be at least |
149 | | * `sizeof (GClosure)` |
150 | | * @data: data to store in the @data field of the newly allocated #GClosure |
151 | | * |
152 | | * Allocates a struct of the given size and initializes the initial |
153 | | * part as a #GClosure. This function is mainly useful when |
154 | | * implementing new types of closures. |
155 | | * |
156 | | * |[<!-- language="C" --> |
157 | | * typedef struct _MyClosure MyClosure; |
158 | | * struct _MyClosure |
159 | | * { |
160 | | * GClosure closure; |
161 | | * // extra data goes here |
162 | | * }; |
163 | | * |
164 | | * static void |
165 | | * my_closure_finalize (gpointer notify_data, |
166 | | * GClosure *closure) |
167 | | * { |
168 | | * MyClosure *my_closure = (MyClosure *)closure; |
169 | | * |
170 | | * // free extra data here |
171 | | * } |
172 | | * |
173 | | * MyClosure *my_closure_new (gpointer data) |
174 | | * { |
175 | | * GClosure *closure; |
176 | | * MyClosure *my_closure; |
177 | | * |
178 | | * closure = g_closure_new_simple (sizeof (MyClosure), data); |
179 | | * my_closure = (MyClosure *) closure; |
180 | | * |
181 | | * // initialize extra data here |
182 | | * |
183 | | * g_closure_add_finalize_notifier (closure, notify_data, |
184 | | * my_closure_finalize); |
185 | | * return my_closure; |
186 | | * } |
187 | | * ]| |
188 | | * |
189 | | * Returns: (transfer none): a floating reference to a new #GClosure |
190 | | */ |
191 | | GClosure* |
192 | | g_closure_new_simple (guint sizeof_closure, |
193 | | gpointer data) |
194 | 36 | { |
195 | 36 | GClosure *closure; |
196 | 36 | gint private_size; |
197 | 36 | gchar *allocated; |
198 | | |
199 | 36 | g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL); |
200 | | |
201 | 36 | private_size = sizeof (GRealClosure) - sizeof (GClosure); |
202 | | |
203 | 36 | #ifdef ENABLE_VALGRIND |
204 | | /* See comments in gtype.c about what's going on here... */ |
205 | 36 | if (RUNNING_ON_VALGRIND) |
206 | 0 | { |
207 | 0 | private_size += sizeof (gpointer); |
208 | |
|
209 | 0 | allocated = g_malloc0 (private_size + sizeof_closure + sizeof (gpointer)); |
210 | |
|
211 | 0 | *(gpointer *) (allocated + private_size + sizeof_closure) = allocated + sizeof (gpointer); |
212 | |
|
213 | 0 | VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, sizeof_closure + sizeof (gpointer), 0, TRUE); |
214 | 0 | VALGRIND_MALLOCLIKE_BLOCK (allocated + sizeof (gpointer), private_size - sizeof (gpointer), 0, TRUE); |
215 | 0 | } |
216 | 36 | else |
217 | 36 | #endif |
218 | 36 | allocated = g_malloc0 (private_size + sizeof_closure); |
219 | | |
220 | 36 | closure = (GClosure *) (allocated + private_size); |
221 | | |
222 | 36 | SET (closure, ref_count, 1); |
223 | 36 | SET (closure, floating, TRUE); |
224 | 36 | closure->data = data; |
225 | | |
226 | 36 | return closure; |
227 | 36 | } |
228 | | |
229 | | static inline void |
230 | | closure_invoke_notifiers (GClosure *closure, |
231 | | guint notify_type) |
232 | 0 | { |
233 | | /* notifier layout: |
234 | | * n_guards n_guards n_fnotif. n_inotifiers |
235 | | * ->[[pre_guards][post_guards][fnotifiers][inotifiers]] |
236 | | * |
237 | | * CLOSURE_N_MFUNCS(cl) = n_guards + n_guards; |
238 | | * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers |
239 | | * |
240 | | * constrains/catches: |
241 | | * - closure->notifiers may be reloacted during callback |
242 | | * - closure->n_fnotifiers and closure->n_inotifiers may change during callback |
243 | | * - i.e. callbacks can be removed/added during invocation |
244 | | * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=) |
245 | | * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify) |
246 | | * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY |
247 | | * + none of the callbacks can cause recursion |
248 | | * + closure->n_inotifiers is const 0 during FNOTIFY |
249 | | */ |
250 | 0 | switch (notify_type) |
251 | 0 | { |
252 | 0 | GClosureNotifyData *ndata; |
253 | 0 | guint i, offs; |
254 | 0 | case FNOTIFY: |
255 | 0 | while (closure->n_fnotifiers) |
256 | 0 | { |
257 | 0 | guint n; |
258 | 0 | DEC_ASSIGN (closure, n_fnotifiers, &n); |
259 | |
|
260 | 0 | ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n; |
261 | 0 | closure->marshal = (GClosureMarshal) ndata->notify; |
262 | 0 | closure->data = ndata->data; |
263 | 0 | ndata->notify (ndata->data, closure); |
264 | 0 | } |
265 | 0 | closure->marshal = NULL; |
266 | 0 | closure->data = NULL; |
267 | 0 | break; |
268 | 0 | case INOTIFY: |
269 | 0 | SET (closure, in_inotify, TRUE); |
270 | 0 | while (closure->n_inotifiers) |
271 | 0 | { |
272 | 0 | guint n; |
273 | 0 | DEC_ASSIGN (closure, n_inotifiers, &n); |
274 | |
|
275 | 0 | ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n; |
276 | 0 | closure->marshal = (GClosureMarshal) ndata->notify; |
277 | 0 | closure->data = ndata->data; |
278 | 0 | ndata->notify (ndata->data, closure); |
279 | 0 | } |
280 | 0 | closure->marshal = NULL; |
281 | 0 | closure->data = NULL; |
282 | 0 | SET (closure, in_inotify, FALSE); |
283 | 0 | break; |
284 | 0 | case PRE_NOTIFY: |
285 | 0 | i = closure->n_guards; |
286 | 0 | offs = 0; |
287 | 0 | while (i--) |
288 | 0 | { |
289 | 0 | ndata = closure->notifiers + offs + i; |
290 | 0 | ndata->notify (ndata->data, closure); |
291 | 0 | } |
292 | 0 | break; |
293 | 0 | case POST_NOTIFY: |
294 | 0 | i = closure->n_guards; |
295 | 0 | offs = i; |
296 | 0 | while (i--) |
297 | 0 | { |
298 | 0 | ndata = closure->notifiers + offs + i; |
299 | 0 | ndata->notify (ndata->data, closure); |
300 | 0 | } |
301 | 0 | break; |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | | static void |
306 | | g_closure_set_meta_va_marshal (GClosure *closure, |
307 | | GVaClosureMarshal va_meta_marshal) |
308 | 36 | { |
309 | 36 | GRealClosure *real_closure; |
310 | | |
311 | 36 | g_return_if_fail (closure != NULL); |
312 | 36 | g_return_if_fail (va_meta_marshal != NULL); |
313 | 36 | g_return_if_fail (closure->is_invalid == FALSE); |
314 | 36 | g_return_if_fail (closure->in_marshal == FALSE); |
315 | | |
316 | 36 | real_closure = G_REAL_CLOSURE (closure); |
317 | | |
318 | 36 | g_return_if_fail (real_closure->meta_marshal != NULL); |
319 | | |
320 | 36 | real_closure->va_meta_marshal = va_meta_marshal; |
321 | 36 | } |
322 | | |
323 | | /** |
324 | | * g_closure_set_meta_marshal: (skip) |
325 | | * @closure: a #GClosure |
326 | | * @marshal_data: (closure meta_marshal): context-dependent data to pass |
327 | | * to @meta_marshal |
328 | | * @meta_marshal: a #GClosureMarshal function |
329 | | * |
330 | | * Sets the meta marshaller of @closure. A meta marshaller wraps |
331 | | * @closure->marshal and modifies the way it is called in some |
332 | | * fashion. The most common use of this facility is for C callbacks. |
333 | | * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]), |
334 | | * are used everywhere, but the way that we get the callback function |
335 | | * differs. In most cases we want to use @closure->callback, but in |
336 | | * other cases we want to use some different technique to retrieve the |
337 | | * callback function. |
338 | | * |
339 | | * For example, class closures for signals (see |
340 | | * g_signal_type_cclosure_new()) retrieve the callback function from a |
341 | | * fixed offset in the class structure. The meta marshaller retrieves |
342 | | * the right callback and passes it to the marshaller as the |
343 | | * @marshal_data argument. |
344 | | */ |
345 | | void |
346 | | g_closure_set_meta_marshal (GClosure *closure, |
347 | | gpointer marshal_data, |
348 | | GClosureMarshal meta_marshal) |
349 | 36 | { |
350 | 36 | GRealClosure *real_closure; |
351 | | |
352 | 36 | g_return_if_fail (closure != NULL); |
353 | 36 | g_return_if_fail (meta_marshal != NULL); |
354 | 36 | g_return_if_fail (closure->is_invalid == FALSE); |
355 | 36 | g_return_if_fail (closure->in_marshal == FALSE); |
356 | | |
357 | 36 | real_closure = G_REAL_CLOSURE (closure); |
358 | | |
359 | 36 | g_return_if_fail (real_closure->meta_marshal == NULL); |
360 | | |
361 | 36 | real_closure->meta_marshal = meta_marshal; |
362 | 36 | real_closure->meta_marshal_data = marshal_data; |
363 | 36 | } |
364 | | |
365 | | /** |
366 | | * g_closure_add_marshal_guards: (skip) |
367 | | * @closure: a #GClosure |
368 | | * @pre_marshal_data: (closure pre_marshal_notify): data to pass |
369 | | * to @pre_marshal_notify |
370 | | * @pre_marshal_notify: a function to call before the closure callback |
371 | | * @post_marshal_data: (closure post_marshal_notify): data to pass |
372 | | * to @post_marshal_notify |
373 | | * @post_marshal_notify: a function to call after the closure callback |
374 | | * |
375 | | * Adds a pair of notifiers which get invoked before and after the |
376 | | * closure callback, respectively. This is typically used to protect |
377 | | * the extra arguments for the duration of the callback. See |
378 | | * g_object_watch_closure() for an example of marshal guards. |
379 | | */ |
380 | | void |
381 | | g_closure_add_marshal_guards (GClosure *closure, |
382 | | gpointer pre_marshal_data, |
383 | | GClosureNotify pre_marshal_notify, |
384 | | gpointer post_marshal_data, |
385 | | GClosureNotify post_marshal_notify) |
386 | 0 | { |
387 | 0 | guint i; |
388 | |
|
389 | 0 | g_return_if_fail (closure != NULL); |
390 | 0 | g_return_if_fail (pre_marshal_notify != NULL); |
391 | 0 | g_return_if_fail (post_marshal_notify != NULL); |
392 | 0 | g_return_if_fail (closure->is_invalid == FALSE); |
393 | 0 | g_return_if_fail (closure->in_marshal == FALSE); |
394 | 0 | g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS); |
395 | | |
396 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2); |
397 | 0 | if (closure->n_inotifiers) |
398 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
399 | 0 | closure->n_fnotifiers + |
400 | 0 | closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
401 | 0 | closure->n_fnotifiers + 0)]; |
402 | 0 | if (closure->n_inotifiers > 1) |
403 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
404 | 0 | closure->n_fnotifiers + |
405 | 0 | closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
406 | 0 | closure->n_fnotifiers + 1)]; |
407 | 0 | if (closure->n_fnotifiers) |
408 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
409 | 0 | closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0]; |
410 | 0 | if (closure->n_fnotifiers > 1) |
411 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
412 | 0 | closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1]; |
413 | 0 | if (closure->n_guards) |
414 | 0 | closure->notifiers[(closure->n_guards + |
415 | 0 | closure->n_guards + 1)] = closure->notifiers[closure->n_guards]; |
416 | 0 | i = closure->n_guards; |
417 | 0 | closure->notifiers[i].data = pre_marshal_data; |
418 | 0 | closure->notifiers[i].notify = pre_marshal_notify; |
419 | 0 | closure->notifiers[i + 1].data = post_marshal_data; |
420 | 0 | closure->notifiers[i + 1].notify = post_marshal_notify; |
421 | 0 | INC (closure, n_guards); |
422 | 0 | } |
423 | | |
424 | | /** |
425 | | * g_closure_add_finalize_notifier: (skip) |
426 | | * @closure: a #GClosure |
427 | | * @notify_data: (closure notify_func): data to pass to @notify_func |
428 | | * @notify_func: the callback function to register |
429 | | * |
430 | | * Registers a finalization notifier which will be called when the |
431 | | * reference count of @closure goes down to 0. Multiple finalization |
432 | | * notifiers on a single closure are invoked in unspecified order. If |
433 | | * a single call to g_closure_unref() results in the closure being |
434 | | * both invalidated and finalized, then the invalidate notifiers will |
435 | | * be run before the finalize notifiers. |
436 | | */ |
437 | | void |
438 | | g_closure_add_finalize_notifier (GClosure *closure, |
439 | | gpointer notify_data, |
440 | | GClosureNotify notify_func) |
441 | 0 | { |
442 | 0 | guint i; |
443 | |
|
444 | 0 | g_return_if_fail (closure != NULL); |
445 | 0 | g_return_if_fail (notify_func != NULL); |
446 | 0 | g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS); |
447 | | |
448 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); |
449 | 0 | if (closure->n_inotifiers) |
450 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
451 | 0 | closure->n_fnotifiers + |
452 | 0 | closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
453 | 0 | closure->n_fnotifiers + 0)]; |
454 | 0 | i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers; |
455 | 0 | closure->notifiers[i].data = notify_data; |
456 | 0 | closure->notifiers[i].notify = notify_func; |
457 | 0 | INC (closure, n_fnotifiers); |
458 | 0 | } |
459 | | |
460 | | /** |
461 | | * g_closure_add_invalidate_notifier: (skip) |
462 | | * @closure: a #GClosure |
463 | | * @notify_data: (closure notify_func): data to pass to @notify_func |
464 | | * @notify_func: the callback function to register |
465 | | * |
466 | | * Registers an invalidation notifier which will be called when the |
467 | | * @closure is invalidated with g_closure_invalidate(). Invalidation |
468 | | * notifiers are invoked before finalization notifiers, in an |
469 | | * unspecified order. |
470 | | */ |
471 | | void |
472 | | g_closure_add_invalidate_notifier (GClosure *closure, |
473 | | gpointer notify_data, |
474 | | GClosureNotify notify_func) |
475 | 0 | { |
476 | 0 | guint i; |
477 | |
|
478 | 0 | g_return_if_fail (closure != NULL); |
479 | 0 | g_return_if_fail (notify_func != NULL); |
480 | 0 | g_return_if_fail (closure->is_invalid == FALSE); |
481 | 0 | g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS); |
482 | | |
483 | 0 | closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1); |
484 | 0 | i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers; |
485 | 0 | closure->notifiers[i].data = notify_data; |
486 | 0 | closure->notifiers[i].notify = notify_func; |
487 | 0 | INC (closure, n_inotifiers); |
488 | 0 | } |
489 | | |
490 | | static inline gboolean |
491 | | closure_try_remove_inotify (GClosure *closure, |
492 | | gpointer notify_data, |
493 | | GClosureNotify notify_func) |
494 | 0 | { |
495 | 0 | GClosureNotifyData *ndata, *nlast; |
496 | |
|
497 | 0 | nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1; |
498 | 0 | for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++) |
499 | 0 | if (ndata->notify == notify_func && ndata->data == notify_data) |
500 | 0 | { |
501 | 0 | DEC (closure, n_inotifiers); |
502 | 0 | if (ndata < nlast) |
503 | 0 | *ndata = *nlast; |
504 | |
|
505 | 0 | return TRUE; |
506 | 0 | } |
507 | 0 | return FALSE; |
508 | 0 | } |
509 | | |
510 | | static inline gboolean |
511 | | closure_try_remove_fnotify (GClosure *closure, |
512 | | gpointer notify_data, |
513 | | GClosureNotify notify_func) |
514 | 0 | { |
515 | 0 | GClosureNotifyData *ndata, *nlast; |
516 | |
|
517 | 0 | nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1; |
518 | 0 | for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++) |
519 | 0 | if (ndata->notify == notify_func && ndata->data == notify_data) |
520 | 0 | { |
521 | 0 | DEC (closure, n_fnotifiers); |
522 | 0 | if (ndata < nlast) |
523 | 0 | *ndata = *nlast; |
524 | 0 | if (closure->n_inotifiers) |
525 | 0 | closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
526 | 0 | closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) + |
527 | 0 | closure->n_fnotifiers + |
528 | 0 | closure->n_inotifiers)]; |
529 | 0 | return TRUE; |
530 | 0 | } |
531 | 0 | return FALSE; |
532 | 0 | } |
533 | | |
534 | | /** |
535 | | * g_closure_ref: |
536 | | * @closure: #GClosure to increment the reference count on |
537 | | * |
538 | | * Increments the reference count on a closure to force it staying |
539 | | * alive while the caller holds a pointer to it. |
540 | | * |
541 | | * Returns: (transfer none): The @closure passed in, for convenience |
542 | | */ |
543 | | GClosure* |
544 | | g_closure_ref (GClosure *closure) |
545 | 36 | { |
546 | 36 | guint new_ref_count; |
547 | 36 | g_return_val_if_fail (closure != NULL, NULL); |
548 | 36 | g_return_val_if_fail (closure->ref_count > 0, NULL); |
549 | 36 | g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL); |
550 | | |
551 | 36 | INC_ASSIGN (closure, ref_count, &new_ref_count); |
552 | 36 | g_return_val_if_fail (new_ref_count > 1, NULL); |
553 | | |
554 | 36 | return closure; |
555 | 36 | } |
556 | | |
557 | | /** |
558 | | * g_closure_invalidate: |
559 | | * @closure: #GClosure to invalidate |
560 | | * |
561 | | * Sets a flag on the closure to indicate that its calling |
562 | | * environment has become invalid, and thus causes any future |
563 | | * invocations of g_closure_invoke() on this @closure to be |
564 | | * ignored. Also, invalidation notifiers installed on the closure will |
565 | | * be called at this point. Note that unless you are holding a |
566 | | * reference to the closure yourself, the invalidation notifiers may |
567 | | * unref the closure and cause it to be destroyed, so if you need to |
568 | | * access the closure after calling g_closure_invalidate(), make sure |
569 | | * that you've previously called g_closure_ref(). |
570 | | * |
571 | | * Note that g_closure_invalidate() will also be called when the |
572 | | * reference count of a closure drops to zero (unless it has already |
573 | | * been invalidated before). |
574 | | */ |
575 | | void |
576 | | g_closure_invalidate (GClosure *closure) |
577 | 0 | { |
578 | 0 | g_return_if_fail (closure != NULL); |
579 | | |
580 | 0 | if (!closure->is_invalid) |
581 | 0 | { |
582 | 0 | gboolean was_invalid; |
583 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
584 | 0 | SWAP (closure, is_invalid, TRUE, &was_invalid); |
585 | | /* invalidate only once */ |
586 | 0 | if (!was_invalid) |
587 | 0 | closure_invoke_notifiers (closure, INOTIFY); |
588 | 0 | g_closure_unref (closure); |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | | /** |
593 | | * g_closure_unref: |
594 | | * @closure: #GClosure to decrement the reference count on |
595 | | * |
596 | | * Decrements the reference count of a closure after it was previously |
597 | | * incremented by the same caller. If no other callers are using the |
598 | | * closure, then the closure will be destroyed and freed. |
599 | | */ |
600 | | void |
601 | | g_closure_unref (GClosure *closure) |
602 | 36 | { |
603 | 36 | guint new_ref_count; |
604 | | |
605 | 36 | g_return_if_fail (closure != NULL); |
606 | 36 | g_return_if_fail (closure->ref_count > 0); |
607 | | |
608 | 36 | if (closure->ref_count == 1) /* last unref, invalidate first */ |
609 | 0 | g_closure_invalidate (closure); |
610 | | |
611 | 36 | DEC_ASSIGN (closure, ref_count, &new_ref_count); |
612 | | |
613 | 36 | if (new_ref_count == 0) |
614 | 0 | { |
615 | 0 | closure_invoke_notifiers (closure, FNOTIFY); |
616 | 0 | g_free (closure->notifiers); |
617 | |
|
618 | 0 | #ifdef ENABLE_VALGRIND |
619 | | /* See comments in gtype.c about what's going on here... */ |
620 | 0 | if (RUNNING_ON_VALGRIND) |
621 | 0 | { |
622 | 0 | gchar *allocated; |
623 | |
|
624 | 0 | allocated = (gchar *) G_REAL_CLOSURE (closure); |
625 | 0 | allocated -= sizeof (gpointer); |
626 | |
|
627 | 0 | g_free (allocated); |
628 | |
|
629 | 0 | VALGRIND_FREELIKE_BLOCK (allocated + sizeof (gpointer), 0); |
630 | 0 | VALGRIND_FREELIKE_BLOCK (closure, 0); |
631 | 0 | } |
632 | 0 | else |
633 | 0 | #endif |
634 | 0 | g_free (G_REAL_CLOSURE (closure)); |
635 | 0 | } |
636 | 36 | } |
637 | | |
638 | | /** |
639 | | * g_closure_sink: |
640 | | * @closure: #GClosure to decrement the initial reference count on, if it's |
641 | | * still being held |
642 | | * |
643 | | * Takes over the initial ownership of a closure. Each closure is |
644 | | * initially created in a "floating" state, which means that the initial |
645 | | * reference count is not owned by any caller. g_closure_sink() checks |
646 | | * to see if the object is still floating, and if so, unsets the |
647 | | * floating state and decreases the reference count. If the closure |
648 | | * is not floating, g_closure_sink() does nothing. The reason for the |
649 | | * existence of the floating state is to prevent cumbersome code |
650 | | * sequences like: |
651 | | * |[<!-- language="C" --> |
652 | | * closure = g_cclosure_new (cb_func, cb_data); |
653 | | * g_source_set_closure (source, closure); |
654 | | * g_closure_unref (closure); // GObject doesn't really need this |
655 | | * ]| |
656 | | * Because g_source_set_closure() (and similar functions) take ownership of the |
657 | | * initial reference count, if it is unowned, we instead can write: |
658 | | * |[<!-- language="C" --> |
659 | | * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data)); |
660 | | * ]| |
661 | | * |
662 | | * Generally, this function is used together with g_closure_ref(). An example |
663 | | * of storing a closure for later notification looks like: |
664 | | * |[<!-- language="C" --> |
665 | | * static GClosure *notify_closure = NULL; |
666 | | * void |
667 | | * foo_notify_set_closure (GClosure *closure) |
668 | | * { |
669 | | * if (notify_closure) |
670 | | * g_closure_unref (notify_closure); |
671 | | * notify_closure = closure; |
672 | | * if (notify_closure) |
673 | | * { |
674 | | * g_closure_ref (notify_closure); |
675 | | * g_closure_sink (notify_closure); |
676 | | * } |
677 | | * } |
678 | | * ]| |
679 | | * |
680 | | * Because g_closure_sink() may decrement the reference count of a closure |
681 | | * (if it hasn't been called on @closure yet) just like g_closure_unref(), |
682 | | * g_closure_ref() should be called prior to this function. |
683 | | */ |
684 | | void |
685 | | g_closure_sink (GClosure *closure) |
686 | 36 | { |
687 | 36 | g_return_if_fail (closure != NULL); |
688 | 36 | g_return_if_fail (closure->ref_count > 0); |
689 | | |
690 | | /* floating is basically a kludge to avoid creating closures |
691 | | * with a ref_count of 0. so the initial ref_count a closure has |
692 | | * is unowned. with invoking g_closure_sink() code may |
693 | | * indicate that it takes over that initial ref_count. |
694 | | */ |
695 | 36 | if (closure->floating) |
696 | 36 | { |
697 | 36 | gboolean was_floating; |
698 | 36 | SWAP (closure, floating, FALSE, &was_floating); |
699 | | /* unref floating flag only once */ |
700 | 36 | if (was_floating) |
701 | 36 | g_closure_unref (closure); |
702 | 36 | } |
703 | 36 | } |
704 | | |
705 | | /** |
706 | | * g_closure_remove_invalidate_notifier: (skip) |
707 | | * @closure: a #GClosure |
708 | | * @notify_data: data which was passed to g_closure_add_invalidate_notifier() |
709 | | * when registering @notify_func |
710 | | * @notify_func: the callback function to remove |
711 | | * |
712 | | * Removes an invalidation notifier. |
713 | | * |
714 | | * Notice that notifiers are automatically removed after they are run. |
715 | | */ |
716 | | void |
717 | | g_closure_remove_invalidate_notifier (GClosure *closure, |
718 | | gpointer notify_data, |
719 | | GClosureNotify notify_func) |
720 | 0 | { |
721 | 0 | g_return_if_fail (closure != NULL); |
722 | 0 | g_return_if_fail (notify_func != NULL); |
723 | | |
724 | 0 | if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while it's called */ |
725 | 0 | ((gpointer) closure->marshal) == ((gpointer) notify_func) && |
726 | 0 | closure->data == notify_data) |
727 | 0 | closure->marshal = NULL; |
728 | 0 | else if (!closure_try_remove_inotify (closure, notify_data, notify_func)) |
729 | 0 | g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)", |
730 | 0 | notify_func, notify_data); |
731 | 0 | } |
732 | | |
733 | | /** |
734 | | * g_closure_remove_finalize_notifier: (skip) |
735 | | * @closure: a #GClosure |
736 | | * @notify_data: data which was passed to g_closure_add_finalize_notifier() |
737 | | * when registering @notify_func |
738 | | * @notify_func: the callback function to remove |
739 | | * |
740 | | * Removes a finalization notifier. |
741 | | * |
742 | | * Notice that notifiers are automatically removed after they are run. |
743 | | */ |
744 | | void |
745 | | g_closure_remove_finalize_notifier (GClosure *closure, |
746 | | gpointer notify_data, |
747 | | GClosureNotify notify_func) |
748 | 0 | { |
749 | 0 | g_return_if_fail (closure != NULL); |
750 | 0 | g_return_if_fail (notify_func != NULL); |
751 | | |
752 | 0 | if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while it's called */ |
753 | 0 | ((gpointer) closure->marshal) == ((gpointer) notify_func) && |
754 | 0 | closure->data == notify_data) |
755 | 0 | closure->marshal = NULL; |
756 | 0 | else if (!closure_try_remove_fnotify (closure, notify_data, notify_func)) |
757 | 0 | g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)", |
758 | 0 | notify_func, notify_data); |
759 | 0 | } |
760 | | |
761 | | /** |
762 | | * g_closure_invoke: |
763 | | * @closure: a #GClosure |
764 | | * @return_value: (optional) (out): a #GValue to store the return |
765 | | * value. May be %NULL if the callback of @closure |
766 | | * doesn't return a value. |
767 | | * @n_param_values: the length of the @param_values array |
768 | | * @param_values: (array length=n_param_values): an array of |
769 | | * #GValues holding the arguments on which to |
770 | | * invoke the callback of @closure |
771 | | * @invocation_hint: (nullable): a context-dependent invocation hint |
772 | | * |
773 | | * Invokes the closure, i.e. executes the callback represented by the @closure. |
774 | | */ |
775 | | void |
776 | | g_closure_invoke (GClosure *closure, |
777 | | GValue /*out*/ *return_value, |
778 | | guint n_param_values, |
779 | | const GValue *param_values, |
780 | | gpointer invocation_hint) |
781 | 0 | { |
782 | 0 | GRealClosure *real_closure; |
783 | |
|
784 | 0 | g_return_if_fail (closure != NULL); |
785 | | |
786 | 0 | real_closure = G_REAL_CLOSURE (closure); |
787 | |
|
788 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
789 | 0 | if (!closure->is_invalid) |
790 | 0 | { |
791 | 0 | GClosureMarshal marshal; |
792 | 0 | gpointer marshal_data; |
793 | 0 | gboolean in_marshal = closure->in_marshal; |
794 | |
|
795 | 0 | g_return_if_fail (closure->marshal || real_closure->meta_marshal); |
796 | | |
797 | 0 | SET (closure, in_marshal, TRUE); |
798 | 0 | if (real_closure->meta_marshal) |
799 | 0 | { |
800 | 0 | marshal_data = real_closure->meta_marshal_data; |
801 | 0 | marshal = real_closure->meta_marshal; |
802 | 0 | } |
803 | 0 | else |
804 | 0 | { |
805 | 0 | marshal_data = NULL; |
806 | 0 | marshal = closure->marshal; |
807 | 0 | } |
808 | 0 | if (!in_marshal) |
809 | 0 | closure_invoke_notifiers (closure, PRE_NOTIFY); |
810 | 0 | marshal (closure, |
811 | 0 | return_value, |
812 | 0 | n_param_values, param_values, |
813 | 0 | invocation_hint, |
814 | 0 | marshal_data); |
815 | 0 | if (!in_marshal) |
816 | 0 | closure_invoke_notifiers (closure, POST_NOTIFY); |
817 | 0 | SET (closure, in_marshal, in_marshal); |
818 | 0 | } |
819 | 0 | g_closure_unref (closure); |
820 | 0 | } |
821 | | |
822 | | gboolean |
823 | | _g_closure_supports_invoke_va (GClosure *closure) |
824 | 0 | { |
825 | 0 | GRealClosure *real_closure; |
826 | |
|
827 | 0 | g_return_val_if_fail (closure != NULL, FALSE); |
828 | | |
829 | 0 | real_closure = G_REAL_CLOSURE (closure); |
830 | |
|
831 | 0 | return |
832 | 0 | real_closure->va_marshal != NULL && |
833 | 0 | (real_closure->meta_marshal == NULL || |
834 | 0 | real_closure->va_meta_marshal != NULL); |
835 | 0 | } |
836 | | |
837 | | void |
838 | | _g_closure_invoke_va (GClosure *closure, |
839 | | GValue /*out*/ *return_value, |
840 | | gpointer instance, |
841 | | va_list args, |
842 | | int n_params, |
843 | | GType *param_types) |
844 | 0 | { |
845 | 0 | GRealClosure *real_closure; |
846 | |
|
847 | 0 | g_return_if_fail (closure != NULL); |
848 | | |
849 | 0 | real_closure = G_REAL_CLOSURE (closure); |
850 | |
|
851 | 0 | g_closure_ref (closure); /* preserve floating flag */ |
852 | 0 | if (!closure->is_invalid) |
853 | 0 | { |
854 | 0 | GVaClosureMarshal marshal; |
855 | 0 | gpointer marshal_data; |
856 | 0 | gboolean in_marshal = closure->in_marshal; |
857 | |
|
858 | 0 | g_return_if_fail (closure->marshal || real_closure->meta_marshal); |
859 | | |
860 | 0 | SET (closure, in_marshal, TRUE); |
861 | 0 | if (real_closure->va_meta_marshal) |
862 | 0 | { |
863 | 0 | marshal_data = real_closure->meta_marshal_data; |
864 | 0 | marshal = real_closure->va_meta_marshal; |
865 | 0 | } |
866 | 0 | else |
867 | 0 | { |
868 | 0 | marshal_data = NULL; |
869 | 0 | marshal = real_closure->va_marshal; |
870 | 0 | } |
871 | 0 | if (!in_marshal) |
872 | 0 | closure_invoke_notifiers (closure, PRE_NOTIFY); |
873 | 0 | marshal (closure, |
874 | 0 | return_value, |
875 | 0 | instance, args, |
876 | 0 | marshal_data, |
877 | 0 | n_params, param_types); |
878 | 0 | if (!in_marshal) |
879 | 0 | closure_invoke_notifiers (closure, POST_NOTIFY); |
880 | 0 | SET (closure, in_marshal, in_marshal); |
881 | 0 | } |
882 | 0 | g_closure_unref (closure); |
883 | 0 | } |
884 | | |
885 | | |
886 | | /** |
887 | | * g_closure_set_marshal: (skip) |
888 | | * @closure: a #GClosure |
889 | | * @marshal: a #GClosureMarshal function |
890 | | * |
891 | | * Sets the marshaller of @closure. The `marshal_data` |
892 | | * of @marshal provides a way for a meta marshaller to provide additional |
893 | | * information to the marshaller. (See g_closure_set_meta_marshal().) For |
894 | | * GObject's C predefined marshallers (the g_cclosure_marshal_*() |
895 | | * functions), what it provides is a callback function to use instead of |
896 | | * @closure->callback. |
897 | | */ |
898 | | void |
899 | | g_closure_set_marshal (GClosure *closure, |
900 | | GClosureMarshal marshal) |
901 | 36 | { |
902 | 36 | g_return_if_fail (closure != NULL); |
903 | 36 | g_return_if_fail (marshal != NULL); |
904 | | |
905 | 36 | if (closure->marshal && closure->marshal != marshal) |
906 | 0 | g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)", |
907 | 36 | closure->marshal, marshal); |
908 | 36 | else |
909 | 36 | closure->marshal = marshal; |
910 | 36 | } |
911 | | |
912 | | void |
913 | | _g_closure_set_va_marshal (GClosure *closure, |
914 | | GVaClosureMarshal marshal) |
915 | 36 | { |
916 | 36 | GRealClosure *real_closure; |
917 | | |
918 | 36 | g_return_if_fail (closure != NULL); |
919 | 36 | g_return_if_fail (marshal != NULL); |
920 | | |
921 | 36 | real_closure = G_REAL_CLOSURE (closure); |
922 | | |
923 | 36 | if (real_closure->va_marshal && real_closure->va_marshal != marshal) |
924 | 0 | g_warning ("attempt to override closure->va_marshal (%p) with new marshal (%p)", |
925 | 36 | real_closure->va_marshal, marshal); |
926 | 36 | else |
927 | 36 | real_closure->va_marshal = marshal; |
928 | 36 | } |
929 | | |
930 | | /** |
931 | | * g_cclosure_new: (skip) |
932 | | * @callback_func: the function to invoke |
933 | | * @user_data: (closure callback_func): user data to pass to @callback_func |
934 | | * @destroy_data: destroy notify to be called when @user_data is no longer used |
935 | | * |
936 | | * Creates a new closure which invokes @callback_func with @user_data as |
937 | | * the last parameter. |
938 | | * |
939 | | * @destroy_data will be called as a finalize notifier on the #GClosure. |
940 | | * |
941 | | * Returns: (transfer none): a floating reference to a new #GCClosure |
942 | | */ |
943 | | GClosure* |
944 | | g_cclosure_new (GCallback callback_func, |
945 | | gpointer user_data, |
946 | | GClosureNotify destroy_data) |
947 | 0 | { |
948 | 0 | GClosure *closure; |
949 | | |
950 | 0 | g_return_val_if_fail (callback_func != NULL, NULL); |
951 | | |
952 | 0 | closure = g_closure_new_simple (sizeof (GCClosure), user_data); |
953 | 0 | if (destroy_data) |
954 | 0 | g_closure_add_finalize_notifier (closure, user_data, destroy_data); |
955 | 0 | ((GCClosure*) closure)->callback = (gpointer) callback_func; |
956 | | |
957 | 0 | return closure; |
958 | 0 | } |
959 | | |
960 | | /** |
961 | | * g_cclosure_new_swap: (skip) |
962 | | * @callback_func: the function to invoke |
963 | | * @user_data: (closure callback_func): user data to pass to @callback_func |
964 | | * @destroy_data: destroy notify to be called when @user_data is no longer used |
965 | | * |
966 | | * Creates a new closure which invokes @callback_func with @user_data as |
967 | | * the first parameter. |
968 | | * |
969 | | * @destroy_data will be called as a finalize notifier on the #GClosure. |
970 | | * |
971 | | * Returns: (transfer none): a floating reference to a new #GCClosure |
972 | | */ |
973 | | GClosure* |
974 | | g_cclosure_new_swap (GCallback callback_func, |
975 | | gpointer user_data, |
976 | | GClosureNotify destroy_data) |
977 | 0 | { |
978 | 0 | GClosure *closure; |
979 | | |
980 | 0 | g_return_val_if_fail (callback_func != NULL, NULL); |
981 | | |
982 | 0 | closure = g_closure_new_simple (sizeof (GCClosure), user_data); |
983 | 0 | if (destroy_data) |
984 | 0 | g_closure_add_finalize_notifier (closure, user_data, destroy_data); |
985 | 0 | ((GCClosure*) closure)->callback = (gpointer) callback_func; |
986 | 0 | SET (closure, derivative_flag, TRUE); |
987 | | |
988 | 0 | return closure; |
989 | 0 | } |
990 | | |
991 | | static void |
992 | | g_type_class_meta_marshal (GClosure *closure, |
993 | | GValue /*out*/ *return_value, |
994 | | guint n_param_values, |
995 | | const GValue *param_values, |
996 | | gpointer invocation_hint, |
997 | | gpointer marshal_data) |
998 | 0 | { |
999 | 0 | GTypeClass *class; |
1000 | 0 | gpointer callback; |
1001 | | /* GType itype = (GType) closure->data; */ |
1002 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1003 | | |
1004 | 0 | class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass); |
1005 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1006 | 0 | if (callback) |
1007 | 0 | closure->marshal (closure, |
1008 | 0 | return_value, |
1009 | 0 | n_param_values, param_values, |
1010 | 0 | invocation_hint, |
1011 | 0 | callback); |
1012 | 0 | } |
1013 | | |
1014 | | static void |
1015 | | g_type_class_meta_marshalv (GClosure *closure, |
1016 | | GValue *return_value, |
1017 | | gpointer instance, |
1018 | | va_list args, |
1019 | | gpointer marshal_data, |
1020 | | int n_params, |
1021 | | GType *param_types) |
1022 | 0 | { |
1023 | 0 | GRealClosure *real_closure; |
1024 | 0 | GTypeClass *class; |
1025 | 0 | gpointer callback; |
1026 | | /* GType itype = (GType) closure->data; */ |
1027 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1028 | |
|
1029 | 0 | real_closure = G_REAL_CLOSURE (closure); |
1030 | |
|
1031 | 0 | class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass); |
1032 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1033 | 0 | if (callback) |
1034 | 0 | real_closure->va_marshal (closure, |
1035 | 0 | return_value, |
1036 | 0 | instance, args, |
1037 | 0 | callback, |
1038 | 0 | n_params, |
1039 | 0 | param_types); |
1040 | 0 | } |
1041 | | |
1042 | | static void |
1043 | | g_type_iface_meta_marshal (GClosure *closure, |
1044 | | GValue /*out*/ *return_value, |
1045 | | guint n_param_values, |
1046 | | const GValue *param_values, |
1047 | | gpointer invocation_hint, |
1048 | | gpointer marshal_data) |
1049 | 0 | { |
1050 | 0 | GTypeClass *class; |
1051 | 0 | gpointer callback; |
1052 | 0 | GType itype = (GType) closure->data; |
1053 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1054 | | |
1055 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass); |
1056 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1057 | 0 | if (callback) |
1058 | 0 | closure->marshal (closure, |
1059 | 0 | return_value, |
1060 | 0 | n_param_values, param_values, |
1061 | 0 | invocation_hint, |
1062 | 0 | callback); |
1063 | 0 | } |
1064 | | |
1065 | | gboolean |
1066 | | _g_closure_is_void (GClosure *closure, |
1067 | | gpointer instance) |
1068 | 9.88M | { |
1069 | 9.88M | GRealClosure *real_closure; |
1070 | 9.88M | GTypeClass *class; |
1071 | 9.88M | gpointer callback; |
1072 | 9.88M | GType itype; |
1073 | 9.88M | guint offset; |
1074 | | |
1075 | 9.88M | if (closure->is_invalid) |
1076 | 0 | return TRUE; |
1077 | | |
1078 | 9.88M | real_closure = G_REAL_CLOSURE (closure); |
1079 | | |
1080 | 9.88M | if (real_closure->meta_marshal == g_type_iface_meta_marshal) |
1081 | 0 | { |
1082 | 0 | itype = (GType) closure->data; |
1083 | 0 | offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data); |
1084 | |
|
1085 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass); |
1086 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1087 | 0 | return callback == NULL; |
1088 | 0 | } |
1089 | 9.88M | else if (real_closure->meta_marshal == g_type_class_meta_marshal) |
1090 | 9.88M | { |
1091 | 9.88M | offset = GPOINTER_TO_UINT (real_closure->meta_marshal_data); |
1092 | | |
1093 | 9.88M | class = G_TYPE_INSTANCE_GET_CLASS (instance, itype, GTypeClass); |
1094 | 9.88M | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1095 | 9.88M | return callback == NULL; |
1096 | 9.88M | } |
1097 | | |
1098 | 0 | return FALSE; |
1099 | 9.88M | } |
1100 | | |
1101 | | static void |
1102 | | g_type_iface_meta_marshalv (GClosure *closure, |
1103 | | GValue *return_value, |
1104 | | gpointer instance, |
1105 | | va_list args, |
1106 | | gpointer marshal_data, |
1107 | | int n_params, |
1108 | | GType *param_types) |
1109 | 0 | { |
1110 | 0 | GRealClosure *real_closure; |
1111 | 0 | GTypeClass *class; |
1112 | 0 | gpointer callback; |
1113 | 0 | GType itype = (GType) closure->data; |
1114 | 0 | guint offset = GPOINTER_TO_UINT (marshal_data); |
1115 | |
|
1116 | 0 | real_closure = G_REAL_CLOSURE (closure); |
1117 | |
|
1118 | 0 | class = G_TYPE_INSTANCE_GET_INTERFACE (instance, itype, GTypeClass); |
1119 | 0 | callback = G_STRUCT_MEMBER (gpointer, class, offset); |
1120 | 0 | if (callback) |
1121 | 0 | real_closure->va_marshal (closure, |
1122 | 0 | return_value, |
1123 | 0 | instance, args, |
1124 | 0 | callback, |
1125 | 0 | n_params, |
1126 | 0 | param_types); |
1127 | 0 | } |
1128 | | |
1129 | | /** |
1130 | | * g_signal_type_cclosure_new: |
1131 | | * @itype: the #GType identifier of an interface or classed type |
1132 | | * @struct_offset: the offset of the member function of @itype's class |
1133 | | * structure which is to be invoked by the new closure |
1134 | | * |
1135 | | * Creates a new closure which invokes the function found at the offset |
1136 | | * @struct_offset in the class structure of the interface or classed type |
1137 | | * identified by @itype. |
1138 | | * |
1139 | | * Returns: (transfer none): a floating reference to a new #GCClosure |
1140 | | */ |
1141 | | GClosure* |
1142 | | g_signal_type_cclosure_new (GType itype, |
1143 | | guint struct_offset) |
1144 | 36 | { |
1145 | 36 | GClosure *closure; |
1146 | | |
1147 | 36 | g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL); |
1148 | 36 | g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL); |
1149 | | |
1150 | 36 | closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype); |
1151 | 36 | if (G_TYPE_IS_INTERFACE (itype)) |
1152 | 0 | { |
1153 | 0 | g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal); |
1154 | 0 | g_closure_set_meta_va_marshal (closure, g_type_iface_meta_marshalv); |
1155 | 0 | } |
1156 | 36 | else |
1157 | 36 | { |
1158 | 36 | g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal); |
1159 | 36 | g_closure_set_meta_va_marshal (closure, g_type_class_meta_marshalv); |
1160 | 36 | } |
1161 | 36 | return closure; |
1162 | 36 | } |
1163 | | |
1164 | | #include <ffi.h> |
1165 | | static ffi_type * |
1166 | | value_to_ffi_type (const GValue *gvalue, |
1167 | | gpointer *value, |
1168 | | gint *enum_tmpval, |
1169 | | gboolean *tmpval_used) |
1170 | 0 | { |
1171 | 0 | ffi_type *rettype = NULL; |
1172 | 0 | GType type = g_type_fundamental (G_VALUE_TYPE (gvalue)); |
1173 | 0 | g_assert (type != G_TYPE_INVALID); |
1174 | | |
1175 | 0 | if (enum_tmpval) |
1176 | 0 | { |
1177 | 0 | g_assert (tmpval_used != NULL); |
1178 | 0 | *tmpval_used = FALSE; |
1179 | 0 | } |
1180 | | |
1181 | 0 | switch (type) |
1182 | 0 | { |
1183 | 0 | case G_TYPE_BOOLEAN: |
1184 | 0 | case G_TYPE_CHAR: |
1185 | 0 | case G_TYPE_INT: |
1186 | 0 | rettype = &ffi_type_sint; |
1187 | 0 | *value = (gpointer)&(gvalue->data[0].v_int); |
1188 | 0 | break; |
1189 | 0 | case G_TYPE_ENUM: |
1190 | | /* enums are stored in v_long even though they are integers, which makes |
1191 | | * marshalling through libffi somewhat complicated. They need to be |
1192 | | * marshalled as signed ints, but we need to use a temporary int sized |
1193 | | * value to pass to libffi otherwise it'll pull the wrong value on |
1194 | | * BE machines with 32-bit integers when treating v_long as 32-bit int. |
1195 | | */ |
1196 | 0 | g_assert (enum_tmpval != NULL); |
1197 | 0 | rettype = &ffi_type_sint; |
1198 | 0 | *enum_tmpval = g_value_get_enum (gvalue); |
1199 | 0 | *value = enum_tmpval; |
1200 | 0 | *tmpval_used = TRUE; |
1201 | 0 | break; |
1202 | 0 | case G_TYPE_FLAGS: |
1203 | 0 | g_assert (enum_tmpval != NULL); |
1204 | 0 | rettype = &ffi_type_uint; |
1205 | 0 | *enum_tmpval = g_value_get_flags (gvalue); |
1206 | 0 | *value = enum_tmpval; |
1207 | 0 | *tmpval_used = TRUE; |
1208 | 0 | break; |
1209 | 0 | case G_TYPE_UCHAR: |
1210 | 0 | case G_TYPE_UINT: |
1211 | 0 | rettype = &ffi_type_uint; |
1212 | 0 | *value = (gpointer)&(gvalue->data[0].v_uint); |
1213 | 0 | break; |
1214 | 0 | case G_TYPE_STRING: |
1215 | 0 | case G_TYPE_OBJECT: |
1216 | 0 | case G_TYPE_BOXED: |
1217 | 0 | case G_TYPE_PARAM: |
1218 | 0 | case G_TYPE_POINTER: |
1219 | 0 | case G_TYPE_INTERFACE: |
1220 | 0 | case G_TYPE_VARIANT: |
1221 | 0 | rettype = &ffi_type_pointer; |
1222 | 0 | *value = (gpointer)&(gvalue->data[0].v_pointer); |
1223 | 0 | break; |
1224 | 0 | case G_TYPE_FLOAT: |
1225 | 0 | rettype = &ffi_type_float; |
1226 | 0 | *value = (gpointer)&(gvalue->data[0].v_float); |
1227 | 0 | break; |
1228 | 0 | case G_TYPE_DOUBLE: |
1229 | 0 | rettype = &ffi_type_double; |
1230 | 0 | *value = (gpointer)&(gvalue->data[0].v_double); |
1231 | 0 | break; |
1232 | 0 | case G_TYPE_LONG: |
1233 | 0 | rettype = &ffi_type_slong; |
1234 | 0 | *value = (gpointer)&(gvalue->data[0].v_long); |
1235 | 0 | break; |
1236 | 0 | case G_TYPE_ULONG: |
1237 | 0 | rettype = &ffi_type_ulong; |
1238 | 0 | *value = (gpointer)&(gvalue->data[0].v_ulong); |
1239 | 0 | break; |
1240 | 0 | case G_TYPE_INT64: |
1241 | 0 | rettype = &ffi_type_sint64; |
1242 | 0 | *value = (gpointer)&(gvalue->data[0].v_int64); |
1243 | 0 | break; |
1244 | 0 | case G_TYPE_UINT64: |
1245 | 0 | rettype = &ffi_type_uint64; |
1246 | 0 | *value = (gpointer)&(gvalue->data[0].v_uint64); |
1247 | 0 | break; |
1248 | 0 | default: |
1249 | 0 | rettype = &ffi_type_pointer; |
1250 | 0 | *value = NULL; |
1251 | 0 | g_warning ("value_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type)); |
1252 | 0 | break; |
1253 | 0 | } |
1254 | 0 | return rettype; |
1255 | 0 | } |
1256 | | |
1257 | | static void |
1258 | | value_from_ffi_type (GValue *gvalue, gpointer *value) |
1259 | 0 | { |
1260 | 0 | ffi_arg *int_val = (ffi_arg*) value; |
1261 | 0 | GType type; |
1262 | |
|
1263 | 0 | type = G_VALUE_TYPE (gvalue); |
1264 | |
|
1265 | 0 | restart: |
1266 | 0 | switch (g_type_fundamental (type)) |
1267 | 0 | { |
1268 | 0 | case G_TYPE_INT: |
1269 | 0 | g_value_set_int (gvalue, (gint) *int_val); |
1270 | 0 | break; |
1271 | 0 | case G_TYPE_FLOAT: |
1272 | 0 | g_value_set_float (gvalue, *(gfloat*)value); |
1273 | 0 | break; |
1274 | 0 | case G_TYPE_DOUBLE: |
1275 | 0 | g_value_set_double (gvalue, *(gdouble*)value); |
1276 | 0 | break; |
1277 | 0 | case G_TYPE_BOOLEAN: |
1278 | 0 | g_value_set_boolean (gvalue, (gboolean) *int_val); |
1279 | 0 | break; |
1280 | 0 | case G_TYPE_STRING: |
1281 | 0 | g_value_take_string (gvalue, *(gchar**)value); |
1282 | 0 | break; |
1283 | 0 | case G_TYPE_CHAR: |
1284 | 0 | g_value_set_schar (gvalue, (gint8) *int_val); |
1285 | 0 | break; |
1286 | 0 | case G_TYPE_UCHAR: |
1287 | 0 | g_value_set_uchar (gvalue, (guchar) *int_val); |
1288 | 0 | break; |
1289 | 0 | case G_TYPE_UINT: |
1290 | 0 | g_value_set_uint (gvalue, (guint) *int_val); |
1291 | 0 | break; |
1292 | 0 | case G_TYPE_POINTER: |
1293 | 0 | g_value_set_pointer (gvalue, *(gpointer*)value); |
1294 | 0 | break; |
1295 | 0 | case G_TYPE_LONG: |
1296 | 0 | g_value_set_long (gvalue, (glong) *int_val); |
1297 | 0 | break; |
1298 | 0 | case G_TYPE_ULONG: |
1299 | 0 | g_value_set_ulong (gvalue, (gulong) *int_val); |
1300 | 0 | break; |
1301 | 0 | case G_TYPE_INT64: |
1302 | 0 | g_value_set_int64 (gvalue, (gint64) *int_val); |
1303 | 0 | break; |
1304 | 0 | case G_TYPE_UINT64: |
1305 | 0 | g_value_set_uint64 (gvalue, (guint64) *int_val); |
1306 | 0 | break; |
1307 | 0 | case G_TYPE_BOXED: |
1308 | 0 | g_value_take_boxed (gvalue, *(gpointer*)value); |
1309 | 0 | break; |
1310 | 0 | case G_TYPE_ENUM: |
1311 | 0 | g_value_set_enum (gvalue, (gint) *int_val); |
1312 | 0 | break; |
1313 | 0 | case G_TYPE_FLAGS: |
1314 | 0 | g_value_set_flags (gvalue, (guint) *int_val); |
1315 | 0 | break; |
1316 | 0 | case G_TYPE_PARAM: |
1317 | 0 | g_value_take_param (gvalue, *(gpointer*)value); |
1318 | 0 | break; |
1319 | 0 | case G_TYPE_OBJECT: |
1320 | 0 | g_value_take_object (gvalue, *(gpointer*)value); |
1321 | 0 | break; |
1322 | 0 | case G_TYPE_VARIANT: |
1323 | 0 | g_value_take_variant (gvalue, *(gpointer*)value); |
1324 | 0 | break; |
1325 | 0 | case G_TYPE_INTERFACE: |
1326 | 0 | type = g_type_interface_instantiatable_prerequisite (type); |
1327 | 0 | if (type) |
1328 | 0 | goto restart; |
1329 | 0 | G_GNUC_FALLTHROUGH; |
1330 | 0 | default: |
1331 | 0 | g_warning ("value_from_ffi_type: Unsupported fundamental type %s for type %s", |
1332 | 0 | g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))), |
1333 | 0 | g_type_name (G_VALUE_TYPE (gvalue))); |
1334 | 0 | } |
1335 | 0 | } |
1336 | | |
1337 | | typedef union { |
1338 | | gpointer _gpointer; |
1339 | | float _float; |
1340 | | double _double; |
1341 | | gint _gint; |
1342 | | guint _guint; |
1343 | | glong _glong; |
1344 | | gulong _gulong; |
1345 | | gint64 _gint64; |
1346 | | guint64 _guint64; |
1347 | | } va_arg_storage; |
1348 | | |
1349 | | static ffi_type * |
1350 | | va_to_ffi_type (GType gtype, |
1351 | | va_list *va, |
1352 | | va_arg_storage *storage) |
1353 | 0 | { |
1354 | 0 | ffi_type *rettype = NULL; |
1355 | 0 | GType type = g_type_fundamental (gtype); |
1356 | 0 | g_assert (type != G_TYPE_INVALID); |
1357 | | |
1358 | 0 | switch (type) |
1359 | 0 | { |
1360 | 0 | case G_TYPE_BOOLEAN: |
1361 | 0 | case G_TYPE_CHAR: |
1362 | 0 | case G_TYPE_INT: |
1363 | 0 | case G_TYPE_ENUM: |
1364 | 0 | rettype = &ffi_type_sint; |
1365 | 0 | storage->_gint = va_arg (*va, gint); |
1366 | 0 | break; |
1367 | 0 | case G_TYPE_UCHAR: |
1368 | 0 | case G_TYPE_UINT: |
1369 | 0 | case G_TYPE_FLAGS: |
1370 | 0 | rettype = &ffi_type_uint; |
1371 | 0 | storage->_guint = va_arg (*va, guint); |
1372 | 0 | break; |
1373 | 0 | case G_TYPE_STRING: |
1374 | 0 | case G_TYPE_OBJECT: |
1375 | 0 | case G_TYPE_BOXED: |
1376 | 0 | case G_TYPE_PARAM: |
1377 | 0 | case G_TYPE_POINTER: |
1378 | 0 | case G_TYPE_INTERFACE: |
1379 | 0 | case G_TYPE_VARIANT: |
1380 | 0 | rettype = &ffi_type_pointer; |
1381 | 0 | storage->_gpointer = va_arg (*va, gpointer); |
1382 | 0 | break; |
1383 | 0 | case G_TYPE_FLOAT: |
1384 | | /* Float args are passed as doubles in varargs */ |
1385 | 0 | rettype = &ffi_type_float; |
1386 | 0 | storage->_float = (float)va_arg (*va, double); |
1387 | 0 | break; |
1388 | 0 | case G_TYPE_DOUBLE: |
1389 | 0 | rettype = &ffi_type_double; |
1390 | 0 | storage->_double = va_arg (*va, double); |
1391 | 0 | break; |
1392 | 0 | case G_TYPE_LONG: |
1393 | 0 | rettype = &ffi_type_slong; |
1394 | 0 | storage->_glong = va_arg (*va, glong); |
1395 | 0 | break; |
1396 | 0 | case G_TYPE_ULONG: |
1397 | 0 | rettype = &ffi_type_ulong; |
1398 | 0 | storage->_gulong = va_arg (*va, gulong); |
1399 | 0 | break; |
1400 | 0 | case G_TYPE_INT64: |
1401 | 0 | rettype = &ffi_type_sint64; |
1402 | 0 | storage->_gint64 = va_arg (*va, gint64); |
1403 | 0 | break; |
1404 | 0 | case G_TYPE_UINT64: |
1405 | 0 | rettype = &ffi_type_uint64; |
1406 | 0 | storage->_guint64 = va_arg (*va, guint64); |
1407 | 0 | break; |
1408 | 0 | default: |
1409 | 0 | rettype = &ffi_type_pointer; |
1410 | 0 | storage->_guint64 = 0; |
1411 | 0 | g_warning ("va_to_ffi_type: Unsupported fundamental type: %s", g_type_name (type)); |
1412 | 0 | break; |
1413 | 0 | } |
1414 | 0 | return rettype; |
1415 | 0 | } |
1416 | | |
1417 | | /** |
1418 | | * g_cclosure_marshal_generic: |
1419 | | * @closure: A #GClosure. |
1420 | | * @return_gvalue: A #GValue to store the return value. May be %NULL |
1421 | | * if the callback of closure doesn't return a value. |
1422 | | * @n_param_values: The length of the @param_values array. |
1423 | | * @param_values: An array of #GValues holding the arguments |
1424 | | * on which to invoke the callback of closure. |
1425 | | * @invocation_hint: The invocation hint given as the last argument to |
1426 | | * g_closure_invoke(). |
1427 | | * @marshal_data: Additional data specified when registering the |
1428 | | * marshaller, see g_closure_set_marshal() and |
1429 | | * g_closure_set_meta_marshal() |
1430 | | * |
1431 | | * A generic marshaller function implemented via |
1432 | | * [libffi](http://sourceware.org/libffi/). |
1433 | | * |
1434 | | * Normally this function is not passed explicitly to g_signal_new(), |
1435 | | * but used automatically by GLib when specifying a %NULL marshaller. |
1436 | | * |
1437 | | * Since: 2.30 |
1438 | | */ |
1439 | | void |
1440 | | g_cclosure_marshal_generic (GClosure *closure, |
1441 | | GValue *return_gvalue, |
1442 | | guint n_param_values, |
1443 | | const GValue *param_values, |
1444 | | gpointer invocation_hint, |
1445 | | gpointer marshal_data) |
1446 | 0 | { |
1447 | 0 | ffi_type *rtype; |
1448 | 0 | void *rvalue; |
1449 | 0 | int n_args; |
1450 | 0 | ffi_type **atypes; |
1451 | 0 | void **args; |
1452 | 0 | int i; |
1453 | 0 | ffi_cif cif; |
1454 | 0 | GCClosure *cc = (GCClosure*) closure; |
1455 | 0 | gint *enum_tmpval; |
1456 | 0 | gboolean tmpval_used = FALSE; |
1457 | |
|
1458 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1459 | 0 | if (return_gvalue && G_VALUE_TYPE (return_gvalue)) |
1460 | 0 | { |
1461 | 0 | rtype = value_to_ffi_type (return_gvalue, &rvalue, enum_tmpval, &tmpval_used); |
1462 | 0 | } |
1463 | 0 | else |
1464 | 0 | { |
1465 | 0 | rtype = &ffi_type_void; |
1466 | 0 | } |
1467 | |
|
1468 | 0 | rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg))); |
1469 | |
|
1470 | 0 | n_args = n_param_values + 1; |
1471 | 0 | atypes = g_alloca (sizeof (ffi_type *) * n_args); |
1472 | 0 | args = g_alloca (sizeof (gpointer) * n_args); |
1473 | |
|
1474 | 0 | if (tmpval_used) |
1475 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1476 | |
|
1477 | 0 | if (G_CCLOSURE_SWAP_DATA (closure)) |
1478 | 0 | { |
1479 | 0 | atypes[n_args-1] = value_to_ffi_type (param_values + 0, |
1480 | 0 | &args[n_args-1], |
1481 | 0 | enum_tmpval, |
1482 | 0 | &tmpval_used); |
1483 | 0 | atypes[0] = &ffi_type_pointer; |
1484 | 0 | args[0] = &closure->data; |
1485 | 0 | } |
1486 | 0 | else |
1487 | 0 | { |
1488 | 0 | atypes[0] = value_to_ffi_type (param_values + 0, |
1489 | 0 | &args[0], |
1490 | 0 | enum_tmpval, |
1491 | 0 | &tmpval_used); |
1492 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1493 | 0 | args[n_args-1] = &closure->data; |
1494 | 0 | } |
1495 | |
|
1496 | 0 | for (i = 1; i < n_args - 1; i++) |
1497 | 0 | { |
1498 | 0 | if (tmpval_used) |
1499 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1500 | |
|
1501 | 0 | atypes[i] = value_to_ffi_type (param_values + i, |
1502 | 0 | &args[i], |
1503 | 0 | enum_tmpval, |
1504 | 0 | &tmpval_used); |
1505 | 0 | } |
1506 | |
|
1507 | 0 | if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK) |
1508 | 0 | return; |
1509 | | |
1510 | 0 | ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args); |
1511 | |
|
1512 | 0 | if (return_gvalue && G_VALUE_TYPE (return_gvalue)) |
1513 | 0 | value_from_ffi_type (return_gvalue, rvalue); |
1514 | 0 | } |
1515 | | |
1516 | | /** |
1517 | | * g_cclosure_marshal_generic_va: |
1518 | | * @closure: the #GClosure to which the marshaller belongs |
1519 | | * @return_value: (nullable): a #GValue to store the return |
1520 | | * value. May be %NULL if the callback of @closure doesn't return a |
1521 | | * value. |
1522 | | * @instance: (type GObject.TypeInstance): the instance on which the closure is |
1523 | | * invoked. |
1524 | | * @args_list: va_list of arguments to be passed to the closure. |
1525 | | * @marshal_data: (nullable): additional data specified when |
1526 | | * registering the marshaller, see g_closure_set_marshal() and |
1527 | | * g_closure_set_meta_marshal() |
1528 | | * @n_params: the length of the @param_types array |
1529 | | * @param_types: (array length=n_params): the #GType of each argument from |
1530 | | * @args_list. |
1531 | | * |
1532 | | * A generic #GVaClosureMarshal function implemented via |
1533 | | * [libffi](http://sourceware.org/libffi/). |
1534 | | * |
1535 | | * Since: 2.30 |
1536 | | */ |
1537 | | void |
1538 | | g_cclosure_marshal_generic_va (GClosure *closure, |
1539 | | GValue *return_value, |
1540 | | gpointer instance, |
1541 | | va_list args_list, |
1542 | | gpointer marshal_data, |
1543 | | int n_params, |
1544 | | GType *param_types) |
1545 | 0 | { |
1546 | 0 | ffi_type *rtype; |
1547 | 0 | void *rvalue; |
1548 | 0 | int n_args; |
1549 | 0 | ffi_type **atypes; |
1550 | 0 | void **args; |
1551 | 0 | va_arg_storage *storage; |
1552 | 0 | int i; |
1553 | 0 | ffi_cif cif; |
1554 | 0 | GCClosure *cc = (GCClosure*) closure; |
1555 | 0 | gint *enum_tmpval; |
1556 | 0 | gboolean tmpval_used = FALSE; |
1557 | 0 | va_list args_copy; |
1558 | |
|
1559 | 0 | enum_tmpval = g_alloca (sizeof (gint)); |
1560 | 0 | if (return_value && G_VALUE_TYPE (return_value)) |
1561 | 0 | { |
1562 | 0 | rtype = value_to_ffi_type (return_value, &rvalue, enum_tmpval, &tmpval_used); |
1563 | 0 | } |
1564 | 0 | else |
1565 | 0 | { |
1566 | 0 | rtype = &ffi_type_void; |
1567 | 0 | } |
1568 | |
|
1569 | 0 | rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg))); |
1570 | |
|
1571 | 0 | n_args = n_params + 2; |
1572 | 0 | atypes = g_alloca (sizeof (ffi_type *) * n_args); |
1573 | 0 | args = g_alloca (sizeof (gpointer) * n_args); |
1574 | 0 | storage = g_alloca (sizeof (va_arg_storage) * n_params); |
1575 | |
|
1576 | 0 | if (G_CCLOSURE_SWAP_DATA (closure)) |
1577 | 0 | { |
1578 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1579 | 0 | args[n_args-1] = &instance; |
1580 | 0 | atypes[0] = &ffi_type_pointer; |
1581 | 0 | args[0] = &closure->data; |
1582 | 0 | } |
1583 | 0 | else |
1584 | 0 | { |
1585 | 0 | atypes[0] = &ffi_type_pointer; |
1586 | 0 | args[0] = &instance; |
1587 | 0 | atypes[n_args-1] = &ffi_type_pointer; |
1588 | 0 | args[n_args-1] = &closure->data; |
1589 | 0 | } |
1590 | |
|
1591 | 0 | G_VA_COPY (args_copy, args_list); |
1592 | | |
1593 | | /* Box non-primitive arguments */ |
1594 | 0 | for (i = 0; i < n_params; i++) |
1595 | 0 | { |
1596 | 0 | GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
1597 | 0 | GType fundamental = G_TYPE_FUNDAMENTAL (type); |
1598 | |
|
1599 | 0 | atypes[i+1] = va_to_ffi_type (type, |
1600 | 0 | &args_copy, |
1601 | 0 | &storage[i]); |
1602 | 0 | args[i+1] = &storage[i]; |
1603 | |
|
1604 | 0 | if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0) |
1605 | 0 | { |
1606 | 0 | if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL) |
1607 | 0 | storage[i]._gpointer = g_strdup (storage[i]._gpointer); |
1608 | 0 | else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL) |
1609 | 0 | storage[i]._gpointer = g_param_spec_ref (storage[i]._gpointer); |
1610 | 0 | else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL) |
1611 | 0 | storage[i]._gpointer = g_boxed_copy (type, storage[i]._gpointer); |
1612 | 0 | else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL) |
1613 | 0 | storage[i]._gpointer = g_variant_ref_sink (storage[i]._gpointer); |
1614 | 0 | } |
1615 | 0 | if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL) |
1616 | 0 | storage[i]._gpointer = g_object_ref (storage[i]._gpointer); |
1617 | 0 | } |
1618 | |
|
1619 | 0 | va_end (args_copy); |
1620 | | |
1621 | 0 | if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK) |
1622 | 0 | return; |
1623 | | |
1624 | 0 | ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args); |
1625 | | |
1626 | | /* Unbox non-primitive arguments */ |
1627 | 0 | for (i = 0; i < n_params; i++) |
1628 | 0 | { |
1629 | 0 | GType type = param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
1630 | 0 | GType fundamental = G_TYPE_FUNDAMENTAL (type); |
1631 | |
|
1632 | 0 | if ((param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0) |
1633 | 0 | { |
1634 | 0 | if (fundamental == G_TYPE_STRING && storage[i]._gpointer != NULL) |
1635 | 0 | g_free (storage[i]._gpointer); |
1636 | 0 | else if (fundamental == G_TYPE_PARAM && storage[i]._gpointer != NULL) |
1637 | 0 | g_param_spec_unref (storage[i]._gpointer); |
1638 | 0 | else if (fundamental == G_TYPE_BOXED && storage[i]._gpointer != NULL) |
1639 | 0 | g_boxed_free (type, storage[i]._gpointer); |
1640 | 0 | else if (fundamental == G_TYPE_VARIANT && storage[i]._gpointer != NULL) |
1641 | 0 | g_variant_unref (storage[i]._gpointer); |
1642 | 0 | } |
1643 | 0 | if (fundamental == G_TYPE_OBJECT && storage[i]._gpointer != NULL) |
1644 | 0 | g_object_unref (storage[i]._gpointer); |
1645 | 0 | } |
1646 | | |
1647 | 0 | if (return_value && G_VALUE_TYPE (return_value)) |
1648 | 0 | value_from_ffi_type (return_value, rvalue); |
1649 | 0 | } |
1650 | | |
1651 | | /** |
1652 | | * g_cclosure_marshal_VOID__VOID: |
1653 | | * @closure: the #GClosure to which the marshaller belongs |
1654 | | * @return_value: ignored |
1655 | | * @n_param_values: 1 |
1656 | | * @param_values: a #GValue array holding only the instance |
1657 | | * @invocation_hint: the invocation hint given as the last argument |
1658 | | * to g_closure_invoke() |
1659 | | * @marshal_data: additional data specified when registering the marshaller |
1660 | | * |
1661 | | * A marshaller for a #GCClosure with a callback of type |
1662 | | * `void (*callback) (gpointer instance, gpointer user_data)`. |
1663 | | */ |
1664 | | |
1665 | | /** |
1666 | | * g_cclosure_marshal_VOID__BOOLEAN: |
1667 | | * @closure: the #GClosure to which the marshaller belongs |
1668 | | * @return_value: ignored |
1669 | | * @n_param_values: 2 |
1670 | | * @param_values: a #GValue array holding the instance and the #gboolean parameter |
1671 | | * @invocation_hint: the invocation hint given as the last argument |
1672 | | * to g_closure_invoke() |
1673 | | * @marshal_data: additional data specified when registering the marshaller |
1674 | | * |
1675 | | * A marshaller for a #GCClosure with a callback of type |
1676 | | * `void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)`. |
1677 | | */ |
1678 | | |
1679 | | /** |
1680 | | * g_cclosure_marshal_VOID__CHAR: |
1681 | | * @closure: the #GClosure to which the marshaller belongs |
1682 | | * @return_value: ignored |
1683 | | * @n_param_values: 2 |
1684 | | * @param_values: a #GValue array holding the instance and the #gchar parameter |
1685 | | * @invocation_hint: the invocation hint given as the last argument |
1686 | | * to g_closure_invoke() |
1687 | | * @marshal_data: additional data specified when registering the marshaller |
1688 | | * |
1689 | | * A marshaller for a #GCClosure with a callback of type |
1690 | | * `void (*callback) (gpointer instance, gchar arg1, gpointer user_data)`. |
1691 | | */ |
1692 | | |
1693 | | /** |
1694 | | * g_cclosure_marshal_VOID__UCHAR: |
1695 | | * @closure: the #GClosure to which the marshaller belongs |
1696 | | * @return_value: ignored |
1697 | | * @n_param_values: 2 |
1698 | | * @param_values: a #GValue array holding the instance and the #guchar parameter |
1699 | | * @invocation_hint: the invocation hint given as the last argument |
1700 | | * to g_closure_invoke() |
1701 | | * @marshal_data: additional data specified when registering the marshaller |
1702 | | * |
1703 | | * A marshaller for a #GCClosure with a callback of type |
1704 | | * `void (*callback) (gpointer instance, guchar arg1, gpointer user_data)`. |
1705 | | */ |
1706 | | |
1707 | | /** |
1708 | | * g_cclosure_marshal_VOID__INT: |
1709 | | * @closure: the #GClosure to which the marshaller belongs |
1710 | | * @return_value: ignored |
1711 | | * @n_param_values: 2 |
1712 | | * @param_values: a #GValue array holding the instance and the #gint parameter |
1713 | | * @invocation_hint: the invocation hint given as the last argument |
1714 | | * to g_closure_invoke() |
1715 | | * @marshal_data: additional data specified when registering the marshaller |
1716 | | * |
1717 | | * A marshaller for a #GCClosure with a callback of type |
1718 | | * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)`. |
1719 | | */ |
1720 | | |
1721 | | /** |
1722 | | * g_cclosure_marshal_VOID__UINT: |
1723 | | * @closure: the #GClosure to which the marshaller belongs |
1724 | | * @return_value: ignored |
1725 | | * @n_param_values: 2 |
1726 | | * @param_values: a #GValue array holding the instance and the #guint parameter |
1727 | | * @invocation_hint: the invocation hint given as the last argument |
1728 | | * to g_closure_invoke() |
1729 | | * @marshal_data: additional data specified when registering the marshaller |
1730 | | * |
1731 | | * A marshaller for a #GCClosure with a callback of type |
1732 | | * `void (*callback) (gpointer instance, guint arg1, gpointer user_data)`. |
1733 | | */ |
1734 | | |
1735 | | /** |
1736 | | * g_cclosure_marshal_VOID__LONG: |
1737 | | * @closure: the #GClosure to which the marshaller belongs |
1738 | | * @return_value: ignored |
1739 | | * @n_param_values: 2 |
1740 | | * @param_values: a #GValue array holding the instance and the #glong parameter |
1741 | | * @invocation_hint: the invocation hint given as the last argument |
1742 | | * to g_closure_invoke() |
1743 | | * @marshal_data: additional data specified when registering the marshaller |
1744 | | * |
1745 | | * A marshaller for a #GCClosure with a callback of type |
1746 | | * `void (*callback) (gpointer instance, glong arg1, gpointer user_data)`. |
1747 | | */ |
1748 | | |
1749 | | /** |
1750 | | * g_cclosure_marshal_VOID__ULONG: |
1751 | | * @closure: the #GClosure to which the marshaller belongs |
1752 | | * @return_value: ignored |
1753 | | * @n_param_values: 2 |
1754 | | * @param_values: a #GValue array holding the instance and the #gulong parameter |
1755 | | * @invocation_hint: the invocation hint given as the last argument |
1756 | | * to g_closure_invoke() |
1757 | | * @marshal_data: additional data specified when registering the marshaller |
1758 | | * |
1759 | | * A marshaller for a #GCClosure with a callback of type |
1760 | | * `void (*callback) (gpointer instance, gulong arg1, gpointer user_data)`. |
1761 | | */ |
1762 | | |
1763 | | /** |
1764 | | * g_cclosure_marshal_VOID__ENUM: |
1765 | | * @closure: the #GClosure to which the marshaller belongs |
1766 | | * @return_value: ignored |
1767 | | * @n_param_values: 2 |
1768 | | * @param_values: a #GValue array holding the instance and the enumeration parameter |
1769 | | * @invocation_hint: the invocation hint given as the last argument |
1770 | | * to g_closure_invoke() |
1771 | | * @marshal_data: additional data specified when registering the marshaller |
1772 | | * |
1773 | | * A marshaller for a #GCClosure with a callback of type |
1774 | | * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes an enumeration type.. |
1775 | | */ |
1776 | | |
1777 | | /** |
1778 | | * g_cclosure_marshal_VOID__FLAGS: |
1779 | | * @closure: the #GClosure to which the marshaller belongs |
1780 | | * @return_value: ignored |
1781 | | * @n_param_values: 2 |
1782 | | * @param_values: a #GValue array holding the instance and the flags parameter |
1783 | | * @invocation_hint: the invocation hint given as the last argument |
1784 | | * to g_closure_invoke() |
1785 | | * @marshal_data: additional data specified when registering the marshaller |
1786 | | * |
1787 | | * A marshaller for a #GCClosure with a callback of type |
1788 | | * `void (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter denotes a flags type. |
1789 | | */ |
1790 | | |
1791 | | /** |
1792 | | * g_cclosure_marshal_VOID__FLOAT: |
1793 | | * @closure: the #GClosure to which the marshaller belongs |
1794 | | * @return_value: ignored |
1795 | | * @n_param_values: 2 |
1796 | | * @param_values: a #GValue array holding the instance and the #gfloat parameter |
1797 | | * @invocation_hint: the invocation hint given as the last argument |
1798 | | * to g_closure_invoke() |
1799 | | * @marshal_data: additional data specified when registering the marshaller |
1800 | | * |
1801 | | * A marshaller for a #GCClosure with a callback of type |
1802 | | * `void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)`. |
1803 | | */ |
1804 | | |
1805 | | /** |
1806 | | * g_cclosure_marshal_VOID__DOUBLE: |
1807 | | * @closure: the #GClosure to which the marshaller belongs |
1808 | | * @return_value: ignored |
1809 | | * @n_param_values: 2 |
1810 | | * @param_values: a #GValue array holding the instance and the #gdouble parameter |
1811 | | * @invocation_hint: the invocation hint given as the last argument |
1812 | | * to g_closure_invoke() |
1813 | | * @marshal_data: additional data specified when registering the marshaller |
1814 | | * |
1815 | | * A marshaller for a #GCClosure with a callback of type |
1816 | | * `void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)`. |
1817 | | */ |
1818 | | |
1819 | | /** |
1820 | | * g_cclosure_marshal_VOID__STRING: |
1821 | | * @closure: the #GClosure to which the marshaller belongs |
1822 | | * @return_value: ignored |
1823 | | * @n_param_values: 2 |
1824 | | * @param_values: a #GValue array holding the instance and the #gchar* parameter |
1825 | | * @invocation_hint: the invocation hint given as the last argument |
1826 | | * to g_closure_invoke() |
1827 | | * @marshal_data: additional data specified when registering the marshaller |
1828 | | * |
1829 | | * A marshaller for a #GCClosure with a callback of type |
1830 | | * `void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)`. |
1831 | | */ |
1832 | | |
1833 | | /** |
1834 | | * g_cclosure_marshal_VOID__PARAM: |
1835 | | * @closure: the #GClosure to which the marshaller belongs |
1836 | | * @return_value: ignored |
1837 | | * @n_param_values: 2 |
1838 | | * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter |
1839 | | * @invocation_hint: the invocation hint given as the last argument |
1840 | | * to g_closure_invoke() |
1841 | | * @marshal_data: additional data specified when registering the marshaller |
1842 | | * |
1843 | | * A marshaller for a #GCClosure with a callback of type |
1844 | | * `void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)`. |
1845 | | */ |
1846 | | |
1847 | | /** |
1848 | | * g_cclosure_marshal_VOID__BOXED: |
1849 | | * @closure: the #GClosure to which the marshaller belongs |
1850 | | * @return_value: ignored |
1851 | | * @n_param_values: 2 |
1852 | | * @param_values: a #GValue array holding the instance and the #GBoxed* parameter |
1853 | | * @invocation_hint: the invocation hint given as the last argument |
1854 | | * to g_closure_invoke() |
1855 | | * @marshal_data: additional data specified when registering the marshaller |
1856 | | * |
1857 | | * A marshaller for a #GCClosure with a callback of type |
1858 | | * `void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)`. |
1859 | | */ |
1860 | | |
1861 | | /** |
1862 | | * g_cclosure_marshal_VOID__POINTER: |
1863 | | * @closure: the #GClosure to which the marshaller belongs |
1864 | | * @return_value: ignored |
1865 | | * @n_param_values: 2 |
1866 | | * @param_values: a #GValue array holding the instance and the #gpointer parameter |
1867 | | * @invocation_hint: the invocation hint given as the last argument |
1868 | | * to g_closure_invoke() |
1869 | | * @marshal_data: additional data specified when registering the marshaller |
1870 | | * |
1871 | | * A marshaller for a #GCClosure with a callback of type |
1872 | | * `void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)`. |
1873 | | */ |
1874 | | |
1875 | | /** |
1876 | | * g_cclosure_marshal_VOID__OBJECT: |
1877 | | * @closure: the #GClosure to which the marshaller belongs |
1878 | | * @return_value: ignored |
1879 | | * @n_param_values: 2 |
1880 | | * @param_values: a #GValue array holding the instance and the #GObject* parameter |
1881 | | * @invocation_hint: the invocation hint given as the last argument |
1882 | | * to g_closure_invoke() |
1883 | | * @marshal_data: additional data specified when registering the marshaller |
1884 | | * |
1885 | | * A marshaller for a #GCClosure with a callback of type |
1886 | | * `void (*callback) (gpointer instance, GObject *arg1, gpointer user_data)`. |
1887 | | */ |
1888 | | |
1889 | | /** |
1890 | | * g_cclosure_marshal_VOID__VARIANT: |
1891 | | * @closure: the #GClosure to which the marshaller belongs |
1892 | | * @return_value: ignored |
1893 | | * @n_param_values: 2 |
1894 | | * @param_values: a #GValue array holding the instance and the #GVariant* parameter |
1895 | | * @invocation_hint: the invocation hint given as the last argument |
1896 | | * to g_closure_invoke() |
1897 | | * @marshal_data: additional data specified when registering the marshaller |
1898 | | * |
1899 | | * A marshaller for a #GCClosure with a callback of type |
1900 | | * `void (*callback) (gpointer instance, GVariant *arg1, gpointer user_data)`. |
1901 | | * |
1902 | | * Since: 2.26 |
1903 | | */ |
1904 | | |
1905 | | /** |
1906 | | * g_cclosure_marshal_VOID__UINT_POINTER: |
1907 | | * @closure: the #GClosure to which the marshaller belongs |
1908 | | * @return_value: ignored |
1909 | | * @n_param_values: 3 |
1910 | | * @param_values: a #GValue array holding instance, arg1 and arg2 |
1911 | | * @invocation_hint: the invocation hint given as the last argument |
1912 | | * to g_closure_invoke() |
1913 | | * @marshal_data: additional data specified when registering the marshaller |
1914 | | * |
1915 | | * A marshaller for a #GCClosure with a callback of type |
1916 | | * `void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)`. |
1917 | | */ |
1918 | | |
1919 | | /** |
1920 | | * g_cclosure_marshal_BOOLEAN__FLAGS: |
1921 | | * @closure: the #GClosure to which the marshaller belongs |
1922 | | * @return_value: a #GValue which can store the returned #gboolean |
1923 | | * @n_param_values: 2 |
1924 | | * @param_values: a #GValue array holding instance and arg1 |
1925 | | * @invocation_hint: the invocation hint given as the last argument |
1926 | | * to g_closure_invoke() |
1927 | | * @marshal_data: additional data specified when registering the marshaller |
1928 | | * |
1929 | | * A marshaller for a #GCClosure with a callback of type |
1930 | | * `gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)` where the #gint parameter |
1931 | | * denotes a flags type. |
1932 | | */ |
1933 | | |
1934 | | /** |
1935 | | * g_cclosure_marshal_BOOL__FLAGS: |
1936 | | * |
1937 | | * Another name for g_cclosure_marshal_BOOLEAN__FLAGS(). |
1938 | | */ |
1939 | | /** |
1940 | | * g_cclosure_marshal_STRING__OBJECT_POINTER: |
1941 | | * @closure: the #GClosure to which the marshaller belongs |
1942 | | * @return_value: a #GValue, which can store the returned string |
1943 | | * @n_param_values: 3 |
1944 | | * @param_values: a #GValue array holding instance, arg1 and arg2 |
1945 | | * @invocation_hint: the invocation hint given as the last argument |
1946 | | * to g_closure_invoke() |
1947 | | * @marshal_data: additional data specified when registering the marshaller |
1948 | | * |
1949 | | * A marshaller for a #GCClosure with a callback of type |
1950 | | * `gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)`. |
1951 | | */ |
1952 | | /** |
1953 | | * g_cclosure_marshal_BOOLEAN__OBJECT_BOXED_BOXED: |
1954 | | * @closure: the #GClosure to which the marshaller belongs |
1955 | | * @return_value: a #GValue, which can store the returned string |
1956 | | * @n_param_values: 3 |
1957 | | * @param_values: a #GValue array holding instance, arg1 and arg2 |
1958 | | * @invocation_hint: the invocation hint given as the last argument |
1959 | | * to g_closure_invoke() |
1960 | | * @marshal_data: additional data specified when registering the marshaller |
1961 | | * |
1962 | | * A marshaller for a #GCClosure with a callback of type |
1963 | | * `gboolean (*callback) (gpointer instance, GBoxed *arg1, GBoxed *arg2, gpointer user_data)`. |
1964 | | * |
1965 | | * Since: 2.26 |
1966 | | */ |