1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
26 /* FIXME: need caching allocators
29 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
30 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
31 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
32 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
33 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
34 ((cl)->n_guards << 1L))
35 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
36 (cl)->n_fnotifiers + \
46 /* --- functions --- */
48 g_closure_new_simple (guint sizeof_closure,
53 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
55 closure = g_malloc (sizeof_closure);
56 closure->ref_count = 1;
57 closure->meta_marshal = 0;
58 closure->n_guards = 0;
59 closure->n_fnotifiers = 0;
60 closure->n_inotifiers = 0;
61 closure->in_inotify = FALSE;
62 closure->floating = TRUE;
63 closure->derivative_flag = 0;
64 closure->in_marshal = FALSE;
65 closure->is_invalid = FALSE;
66 closure->marshal = NULL;
68 closure->notifiers = NULL;
69 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
75 closure_invoke_notifiers (GClosure *closure,
79 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
80 * ->[[meta_marshal][pre_guards][post_guards][fnotifers][inotifiers]]
82 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
83 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
86 * - closure->notifiers may be reloacted during callback
87 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
88 * - i.e. callbacks can be removed/added during invocation
89 * - have to prepare for callback removal during invocation (->marshal & ->data)
90 * - have to distinguish (->marshal & ->data) for INOTIFY/FNOTIFY (->in_inotify)
91 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
92 * + closure->meta_marshal is const for all cases
93 * + none of the callbacks can cause recursion
94 * + closure->n_inotifiers is const 0 during FNOTIFY
98 GClosureNotifyData *ndata;
101 while (closure->n_fnotifiers)
103 register guint n = --closure->n_fnotifiers;
105 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
106 closure->marshal = (gpointer) ndata->notify;
107 closure->data = ndata->data;
108 ndata->notify (ndata->data, closure);
110 closure->marshal = NULL;
111 closure->data = NULL;
114 closure->in_inotify = TRUE;
115 while (closure->n_inotifiers)
117 register guint n = --closure->n_inotifiers;
119 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
120 closure->marshal = (gpointer) ndata->notify;
121 closure->data = ndata->data;
122 ndata->notify (ndata->data, closure);
124 closure->marshal = NULL;
125 closure->data = NULL;
126 closure->in_inotify = FALSE;
129 i = closure->n_guards;
130 offs = closure->meta_marshal;
133 ndata = closure->notifiers + offs + i;
134 ndata->notify (ndata->data, closure);
138 i = closure->n_guards;
139 offs = closure->meta_marshal + i;
142 ndata = closure->notifiers + offs + i;
143 ndata->notify (ndata->data, closure);
150 g_closure_set_meta_marshal (GClosure *closure,
151 gpointer marshal_data,
152 GClosureMarshal meta_marshal)
154 GClosureNotifyData *notifiers;
157 g_return_if_fail (closure != NULL);
158 g_return_if_fail (meta_marshal != NULL);
159 g_return_if_fail (closure->is_invalid == FALSE);
160 g_return_if_fail (closure->in_marshal == FALSE);
161 g_return_if_fail (closure->meta_marshal == 0);
163 n = CLOSURE_N_NOTIFIERS (closure);
164 notifiers = closure->notifiers;
165 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
168 /* usually the meta marshal will be setup right after creation, so the
169 * g_memmove() should be rare-case scenario
171 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
174 closure->notifiers[0].data = marshal_data;
175 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
176 closure->meta_marshal = 1;
180 g_closure_add_marshal_guards (GClosure *closure,
181 gpointer pre_marshal_data,
182 GClosureNotify pre_marshal_notify,
183 gpointer post_marshal_data,
184 GClosureNotify post_marshal_notify)
188 g_return_if_fail (closure != NULL);
189 g_return_if_fail (pre_marshal_notify != NULL);
190 g_return_if_fail (post_marshal_notify != NULL);
191 g_return_if_fail (closure->is_invalid == FALSE);
192 g_return_if_fail (closure->in_marshal == FALSE);
193 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
195 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
196 if (closure->n_inotifiers)
197 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
198 closure->n_fnotifiers +
199 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
200 closure->n_fnotifiers + 0)];
201 if (closure->n_inotifiers > 1)
202 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
203 closure->n_fnotifiers +
204 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
205 closure->n_fnotifiers + 1)];
206 if (closure->n_fnotifiers)
207 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
208 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
209 if (closure->n_fnotifiers > 1)
210 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
211 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
212 if (closure->n_guards)
213 closure->notifiers[(closure->meta_marshal +
215 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
216 i = closure->n_guards++;
217 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
218 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
219 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
220 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
224 g_closure_add_fnotify (GClosure *closure,
225 gpointer notify_data,
226 GClosureNotify notify_func)
230 g_return_if_fail (closure != NULL);
231 g_return_if_fail (notify_func != NULL);
232 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
234 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
235 if (closure->n_inotifiers)
236 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
237 closure->n_fnotifiers +
238 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
239 closure->n_fnotifiers + 0)];
240 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers++;
241 closure->notifiers[i].data = notify_data;
242 closure->notifiers[i].notify = notify_func;
246 g_closure_add_inotify (GClosure *closure,
247 gpointer notify_data,
248 GClosureNotify notify_func)
252 g_return_if_fail (closure != NULL);
253 g_return_if_fail (notify_func != NULL);
254 g_return_if_fail (closure->is_invalid == FALSE);
255 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
257 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
258 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers++;
259 closure->notifiers[i].data = notify_data;
260 closure->notifiers[i].notify = notify_func;
263 static inline gboolean
264 closure_try_remove_inotify (GClosure *closure,
265 gpointer notify_data,
266 GClosureNotify notify_func)
268 GClosureNotifyData *ndata, *nlast;
270 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
271 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
272 if (ndata->notify == notify_func && ndata->data == notify_data)
274 closure->n_inotifiers -= 1;
283 static inline gboolean
284 closure_try_remove_fnotify (GClosure *closure,
285 gpointer notify_data,
286 GClosureNotify notify_func)
288 GClosureNotifyData *ndata, *nlast;
290 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
291 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
292 if (ndata->notify == notify_func && ndata->data == notify_data)
294 closure->n_fnotifiers -= 1;
297 if (closure->n_inotifiers)
298 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
299 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
300 closure->n_fnotifiers +
301 closure->n_inotifiers)];
308 g_closure_ref (GClosure *closure)
310 g_return_val_if_fail (closure != NULL, NULL);
311 g_return_val_if_fail (closure->ref_count > 0, NULL);
312 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
314 closure->ref_count += 1;
320 g_closure_invalidate (GClosure *closure)
322 g_return_if_fail (closure != NULL);
324 if (!closure->is_invalid)
326 closure->ref_count += 1; /* preserve floating flag */
327 closure->is_invalid = TRUE;
328 closure_invoke_notifiers (closure, INOTIFY);
329 g_closure_unref (closure);
334 g_closure_unref (GClosure *closure)
336 g_return_if_fail (closure != NULL);
337 g_return_if_fail (closure->ref_count > 0);
339 if (closure->ref_count == 1) /* last unref, invalidate first */
340 g_closure_invalidate (closure);
342 closure->ref_count -= 1;
344 if (closure->ref_count == 0)
346 closure_invoke_notifiers (closure, FNOTIFY);
347 g_free (closure->notifiers);
353 g_closure_sink (GClosure *closure)
355 g_return_if_fail (closure != NULL);
356 g_return_if_fail (closure->ref_count > 0);
358 /* floating is basically a kludge to avoid creating closures
359 * with a ref_count of 0. so the intial ref_count a closure has
360 * is unowned. with invoking g_closure_sink() code may
361 * indicate that it takes over that intiial ref_count.
363 if (closure->floating)
365 closure->floating = FALSE;
366 if (closure->ref_count > 1)
367 closure->ref_count -= 1;
369 g_closure_unref (closure);
374 g_closure_remove_inotify (GClosure *closure,
375 gpointer notify_data,
376 GClosureNotify notify_func)
378 g_return_if_fail (closure != NULL);
379 g_return_if_fail (notify_func != NULL);
381 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
382 ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
383 closure->marshal = NULL;
384 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
385 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
386 notify_func, notify_data);
390 g_closure_remove_fnotify (GClosure *closure,
391 gpointer notify_data,
392 GClosureNotify notify_func)
394 g_return_if_fail (closure != NULL);
395 g_return_if_fail (notify_func != NULL);
397 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
398 ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
399 closure->marshal = NULL;
400 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
401 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
402 notify_func, notify_data);
406 g_closure_invoke (GClosure *closure,
407 GValue /*out*/ *return_value,
408 guint n_param_values,
409 const GValue *param_values,
410 gpointer invocation_hint)
412 g_return_if_fail (closure != NULL);
413 g_return_if_fail (closure->marshal || closure->meta_marshal);
415 if (!closure->is_invalid)
417 GClosureMarshal marshal;
418 gpointer marshal_data;
419 gboolean in_marshal = closure->in_marshal;
421 closure->ref_count += 1; /* preserve floating flag */
422 closure->in_marshal = TRUE;
423 if (closure->meta_marshal)
425 marshal_data = closure->notifiers[0].data;
426 marshal = (GClosureMarshal) closure->notifiers[0].notify;
431 marshal = closure->marshal;
434 closure_invoke_notifiers (closure, PRE_NOTIFY);
437 n_param_values, param_values,
441 closure_invoke_notifiers (closure, POST_NOTIFY);
442 closure->in_marshal = in_marshal;
443 g_closure_unref (closure);
448 g_closure_set_marshal (GClosure *closure,
449 GClosureMarshal marshal)
451 g_return_if_fail (closure != NULL);
452 g_return_if_fail (marshal != NULL);
454 if (closure->marshal && closure->marshal != marshal)
455 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
456 closure->marshal, marshal);
458 closure->marshal = marshal;
462 g_cclosure_new (GCallback callback_func,
464 GClosureNotify destroy_data)
468 g_return_val_if_fail (callback_func != NULL, NULL);
470 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
472 g_closure_add_fnotify (closure, user_data, destroy_data);
473 ((GCClosure*) closure)->callback = callback_func;
479 g_cclosure_new_swap (GCallback callback_func,
481 GClosureNotify destroy_data)
485 g_return_val_if_fail (callback_func != NULL, NULL);
487 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
489 g_closure_add_fnotify (closure, user_data, destroy_data);
490 ((GCClosure*) closure)->callback = callback_func;
491 closure->derivative_flag = TRUE;
497 g_type_class_meta_marshal (GClosure *closure,
498 GValue /*out*/ *return_value,
499 guint n_param_values,
500 const GValue *param_values,
501 gpointer invocation_hint,
502 gpointer marshal_data)
506 /* GType itype = GPOINTER_TO_UINT (closure->data); */
507 guint offset = GPOINTER_TO_UINT (marshal_data);
509 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
510 callback = G_STRUCT_MEMBER (gpointer, class, offset);
512 closure->marshal (closure,
514 n_param_values, param_values,
520 g_type_iface_meta_marshal (GClosure *closure,
521 GValue /*out*/ *return_value,
522 guint n_param_values,
523 const GValue *param_values,
524 gpointer invocation_hint,
525 gpointer marshal_data)
529 GType itype = GPOINTER_TO_UINT (closure->data);
530 guint offset = GPOINTER_TO_UINT (marshal_data);
532 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
533 callback = G_STRUCT_MEMBER (gpointer, class, offset);
535 closure->marshal (closure,
537 n_param_values, param_values,
543 g_signal_type_cclosure_new (GType itype,
548 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
549 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
551 closure = g_closure_new_simple (sizeof (GClosure), GUINT_TO_POINTER (itype));
552 if (G_TYPE_IS_INTERFACE (itype))
553 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
555 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);