1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 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.
24 /* FIXME: need caching allocators
27 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
28 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
29 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
30 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
31 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
32 ((cl)->n_guards << 1L))
33 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
34 (cl)->n_fnotifiers + \
44 /* --- functions --- */
46 g_closure_new_simple (guint sizeof_closure,
51 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
53 closure = g_malloc (sizeof_closure);
54 closure->ref_count = 1;
55 closure->meta_marshal = 0;
56 closure->n_guards = 0;
57 closure->n_fnotifiers = 0;
58 closure->n_inotifiers = 0;
59 closure->in_inotify = FALSE;
60 closure->floating = TRUE;
61 closure->derivative_flag = 0;
62 closure->in_marshal = FALSE;
63 closure->is_invalid = FALSE;
64 closure->marshal = NULL;
66 closure->notifiers = NULL;
67 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
73 closure_invoke_notifiers (GClosure *closure,
77 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
78 * ->[[meta_marshal][pre_guards][post_guards][fnotifers][inotifiers]]
80 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
81 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
84 * - closure->notifiers may be reloacted during callback
85 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
86 * - i.e. callbacks can be removed/added during invocation
87 * - have to prepare for callback removal during invocation (->marshal & ->data)
88 * - have to distinguish (->marshal & ->data) for INOTIFY/FNOTIFY (->in_inotify)
89 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
90 * + closure->meta_marshal is const for all cases
91 * + none of the callbacks can cause recursion
92 * + closure->n_inotifiers is const 0 during FNOTIFY
96 GClosureNotifyData *ndata;
99 while (closure->n_fnotifiers)
101 register guint n = --closure->n_fnotifiers;
103 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
104 closure->marshal = (gpointer) ndata->notify;
105 closure->data = ndata->data;
106 ndata->notify (ndata->data, closure);
108 closure->marshal = NULL;
109 closure->data = NULL;
112 closure->in_inotify = TRUE;
113 while (closure->n_inotifiers)
115 register guint n = --closure->n_inotifiers;
117 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
118 closure->marshal = (gpointer) ndata->notify;
119 closure->data = ndata->data;
120 ndata->notify (ndata->data, closure);
122 closure->marshal = NULL;
123 closure->data = NULL;
124 closure->in_inotify = FALSE;
127 i = closure->n_guards;
128 offs = closure->meta_marshal;
131 ndata = closure->notifiers + offs + i;
132 ndata->notify (ndata->data, closure);
136 i = closure->n_guards;
137 offs = closure->meta_marshal + i;
140 ndata = closure->notifiers + offs + i;
141 ndata->notify (ndata->data, closure);
148 g_closure_set_meta_marshal (GClosure *closure,
149 gpointer marshal_data,
150 GClosureMarshal meta_marshal)
152 GClosureNotifyData *notifiers;
155 g_return_if_fail (closure != NULL);
156 g_return_if_fail (meta_marshal != NULL);
157 g_return_if_fail (closure->is_invalid == FALSE);
158 g_return_if_fail (closure->in_marshal == FALSE);
159 g_return_if_fail (closure->meta_marshal == FALSE);
161 n = CLOSURE_N_NOTIFIERS (closure);
162 notifiers = closure->notifiers;
163 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
164 closure->notifiers[0].data = marshal_data;
165 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
168 /* usually the meta marshal will be setup right after creation, so the
169 * memcpy() should be rare-case scenario
171 memcpy (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
174 closure->meta_marshal = 1;
178 g_closure_add_marshal_guards (GClosure *closure,
179 gpointer pre_marshal_data,
180 GClosureNotify pre_marshal_notify,
181 gpointer post_marshal_data,
182 GClosureNotify post_marshal_notify)
186 g_return_if_fail (closure != NULL);
187 g_return_if_fail (pre_marshal_notify != NULL);
188 g_return_if_fail (post_marshal_notify != NULL);
189 g_return_if_fail (closure->is_invalid == FALSE);
190 g_return_if_fail (closure->in_marshal == FALSE);
191 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
193 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
194 if (closure->n_inotifiers)
195 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
196 closure->n_fnotifiers +
197 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
198 closure->n_fnotifiers + 0)];
199 if (closure->n_inotifiers > 1)
200 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
201 closure->n_fnotifiers +
202 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
203 closure->n_fnotifiers + 1)];
204 if (closure->n_fnotifiers)
205 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
206 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
207 if (closure->n_fnotifiers > 1)
208 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
209 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
210 if (closure->n_guards)
211 closure->notifiers[(closure->meta_marshal +
213 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
214 i = closure->n_guards++;
215 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
216 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
217 closure->notifiers[closure->meta_marshal + i + i].data = post_marshal_data;
218 closure->notifiers[closure->meta_marshal + i + i].notify = post_marshal_notify;
222 g_closure_add_fnotify (GClosure *closure,
223 gpointer notify_data,
224 GClosureNotify notify_func)
228 g_return_if_fail (closure != NULL);
229 g_return_if_fail (notify_func != NULL);
230 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
232 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
233 if (closure->n_inotifiers)
234 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
235 closure->n_fnotifiers +
236 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
237 closure->n_fnotifiers + 0)];
238 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers++;
239 closure->notifiers[i].data = notify_data;
240 closure->notifiers[i].notify = notify_func;
244 g_closure_add_inotify (GClosure *closure,
245 gpointer notify_data,
246 GClosureNotify notify_func)
250 g_return_if_fail (closure != NULL);
251 g_return_if_fail (notify_func != NULL);
252 g_return_if_fail (closure->is_invalid == FALSE);
253 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
255 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
256 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers++;
257 closure->notifiers[i].data = notify_data;
258 closure->notifiers[i].notify = notify_func;
261 static inline gboolean
262 closure_try_remove_inotify (GClosure *closure,
263 gpointer notify_data,
264 GClosureNotify notify_func)
266 GClosureNotifyData *ndata, *nlast;
268 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
269 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
270 if (ndata->notify == notify_func && ndata->data == notify_data)
272 closure->n_inotifiers -= 1;
281 static inline gboolean
282 closure_try_remove_fnotify (GClosure *closure,
283 gpointer notify_data,
284 GClosureNotify notify_func)
286 GClosureNotifyData *ndata, *nlast;
288 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
289 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
290 if (ndata->notify == notify_func && ndata->data == notify_data)
292 closure->n_fnotifiers -= 1;
295 if (closure->n_inotifiers)
296 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
297 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
298 closure->n_fnotifiers +
299 closure->n_inotifiers)];
306 g_closure_ref (GClosure *closure)
308 g_return_val_if_fail (closure != NULL, NULL);
309 g_return_val_if_fail (closure->ref_count > 0, NULL);
310 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
312 /* floating is basically a kludge to avoid creating closures
313 * with a ref_count of 0. so the first one doing _ref() will
314 * own the closure's initial ref_count
316 if (closure->floating)
317 closure->floating = FALSE;
319 closure->ref_count += 1;
325 g_closure_invalidate (GClosure *closure)
327 g_return_if_fail (closure != NULL);
329 if (!closure->is_invalid)
331 closure->ref_count += 1; /* preserve floating flag */
332 closure->is_invalid = TRUE;
333 closure_invoke_notifiers (closure, INOTIFY);
334 g_closure_unref (closure);
339 g_closure_unref (GClosure *closure)
341 g_return_if_fail (closure != NULL);
342 g_return_if_fail (closure->ref_count > 0);
344 if (closure->ref_count == 1) /* last unref, invalidate first */
345 g_closure_invalidate (closure);
347 closure->ref_count -= 1;
349 if (closure->ref_count == 0)
351 closure_invoke_notifiers (closure, FNOTIFY);
357 g_closure_remove_inotify (GClosure *closure,
358 gpointer notify_data,
359 GClosureNotify notify_func)
361 g_return_if_fail (closure != NULL);
362 g_return_if_fail (notify_func != NULL);
364 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
365 ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
366 closure->marshal = NULL;
367 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
368 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
369 notify_func, notify_data);
373 g_closure_remove_fnotify (GClosure *closure,
374 gpointer notify_data,
375 GClosureNotify notify_func)
377 g_return_if_fail (closure != NULL);
378 g_return_if_fail (notify_func != NULL);
380 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
381 ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
382 closure->marshal = NULL;
383 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
384 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
385 notify_func, notify_data);
389 g_closure_invoke (GClosure *closure,
390 guint invocation_hint,
391 GValue /*out*/ *return_value,
392 guint n_param_values,
393 const GValue *param_values)
395 g_return_if_fail (closure != NULL);
396 g_return_if_fail (closure->marshal || closure->meta_marshal);
398 if (!closure->is_invalid)
400 GClosureMarshal marshal;
401 gpointer marshal_data;
402 gboolean in_marshal = closure->in_marshal;
404 closure->ref_count += 1; /* preserve floating flag */
405 closure->in_marshal = TRUE;
406 if (closure->meta_marshal)
408 marshal_data = closure->notifiers[0].data;
409 marshal = (GClosureMarshal) closure->notifiers[0].notify;
414 marshal = closure->marshal;
417 closure_invoke_notifiers (closure, PRE_NOTIFY);
418 marshal (closure, invocation_hint,
420 n_param_values, param_values,
423 closure_invoke_notifiers (closure, POST_NOTIFY);
424 closure->in_marshal = in_marshal;
425 g_closure_unref (closure);
430 g_closure_set_marshal (GClosure *closure,
431 GClosureMarshal marshal)
433 g_return_if_fail (closure != NULL);
434 g_return_if_fail (marshal != NULL);
436 if (closure->marshal && closure->marshal != marshal)
437 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
438 closure->marshal, marshal);
440 closure->marshal = marshal;
444 g_cclosure_new (GCallback callback_func,
446 GClosureNotify destroy_data)
450 g_return_val_if_fail (callback_func != NULL, NULL);
452 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
454 g_closure_add_fnotify (closure, user_data, destroy_data);
455 ((GCClosure*) closure)->callback = callback_func;
461 g_cclosure_new_swap (GCallback callback_func,
463 GClosureNotify destroy_data)
467 g_return_val_if_fail (callback_func != NULL, NULL);
469 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
471 g_closure_add_fnotify (closure, user_data, destroy_data);
472 ((GCClosure*) closure)->callback = callback_func;
473 closure->derivative_flag = TRUE;
479 g_type_class_meta_marshal (GClosure *closure,
480 guint invocation_hint,
481 GValue /*out*/ *return_value,
482 guint n_param_values,
483 const GValue *param_values,
484 gpointer marshal_data)
488 /* GType itype = GPOINTER_TO_UINT (closure->data); */
489 guint offset = GPOINTER_TO_UINT (marshal_data);
491 class = G_TYPE_INSTANCE_GET_CLASS (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
492 callback = G_STRUCT_MEMBER (gpointer, class, offset);
494 closure->marshal (closure, invocation_hint, return_value,
495 n_param_values, param_values, callback);
499 g_type_iface_meta_marshal (GClosure *closure,
500 guint invocation_hint,
501 GValue /*out*/ *return_value,
502 guint n_param_values,
503 const GValue *param_values,
504 gpointer marshal_data)
508 GType itype = GPOINTER_TO_UINT (closure->data);
509 guint offset = GPOINTER_TO_UINT (marshal_data);
511 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
512 callback = G_STRUCT_MEMBER (gpointer, class, offset);
514 closure->marshal (closure, invocation_hint, return_value,
515 n_param_values, param_values, callback);
519 g_signal_type_closure_new (GType itype,
524 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
525 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
527 closure = g_closure_new_simple (sizeof (GClosure), GUINT_TO_POINTER (itype));
528 if (G_TYPE_IS_INTERFACE (itype))
529 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
531 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);