adapt to work with new CVS gtk-doc, leaving the old rules in place caused
[platform/upstream/glib.git] / gobject / gclosure.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include        "gclosure.h"
21
22 #include        "gvalue.h"
23 #include        <string.h>
24
25
26 /* FIXME: need caching allocators
27  */
28
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 + \
37                                          (cl)->n_inotifiers)
38 enum {
39   FNOTIFY,
40   INOTIFY,
41   PRE_NOTIFY,
42   POST_NOTIFY
43 };
44
45
46 /* --- functions --- */
47 GClosure*
48 g_closure_new_simple (guint           sizeof_closure,
49                       gpointer        data)
50 {
51   GClosure *closure;
52
53   g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
54
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;
67   closure->data = data;
68   closure->notifiers = NULL;
69   memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
70
71   return closure;
72 }
73
74 static inline void
75 closure_invoke_notifiers (GClosure *closure,
76                           guint     notify_type)
77 {
78   /* notifier layout:
79    *     meta_marshal  n_guards    n_guards     n_fnotif.  n_inotifiers
80    * ->[[meta_marshal][pre_guards][post_guards][fnotifers][inotifiers]]
81    *
82    * CLOSURE_N_MFUNCS(cl)    = meta_marshal + n_guards + n_guards;
83    * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
84    *
85    * constrains/catches:
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
95    */
96   switch (notify_type)
97     {
98       GClosureNotifyData *ndata;
99       guint i, offs;
100     case FNOTIFY:
101       while (closure->n_fnotifiers)
102         {
103           register guint n = --closure->n_fnotifiers;
104
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);
109         }
110       closure->marshal = NULL;
111       closure->data = NULL;
112       break;
113     case INOTIFY:
114       closure->in_inotify = TRUE;
115       while (closure->n_inotifiers)
116         {
117           register guint n = --closure->n_inotifiers;
118
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);
123         }
124       closure->marshal = NULL;
125       closure->data = NULL;
126       closure->in_inotify = FALSE;
127       break;
128     case PRE_NOTIFY:
129       i = closure->n_guards;
130       offs = closure->meta_marshal;
131       while (i--)
132         {
133           ndata = closure->notifiers + offs + i;
134           ndata->notify (ndata->data, closure);
135         }
136       break;
137     case POST_NOTIFY:
138       i = closure->n_guards;
139       offs = closure->meta_marshal + i;
140       while (i--)
141         {
142           ndata = closure->notifiers + offs + i;
143           ndata->notify (ndata->data, closure);
144         }
145       break;
146     }
147 }
148
149 void
150 g_closure_set_meta_marshal (GClosure       *closure,
151                             gpointer        marshal_data,
152                             GClosureMarshal meta_marshal)
153 {
154   GClosureNotifyData *notifiers;
155   guint n;
156
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 == FALSE);
162
163   n = CLOSURE_N_NOTIFIERS (closure);
164   notifiers = closure->notifiers;
165   closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
166   if (notifiers)
167     {
168       /* usually the meta marshal will be setup right after creation, so the
169        * g_memmove() should be rare-case scenario
170        */
171       g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
172       g_free (notifiers);
173     }
174   closure->notifiers[0].data = marshal_data;
175   closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
176   closure->meta_marshal = 1;
177 }
178
179 void
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)
185 {
186   guint i;
187
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);
194
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 +
214                         closure->n_guards +
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;
221 }
222
223 void
224 g_closure_add_fnotify (GClosure      *closure,
225                        gpointer       notify_data,
226                        GClosureNotify notify_func)
227 {
228   guint i;
229
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);
233
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;
243 }
244
245 void
246 g_closure_add_inotify (GClosure      *closure,
247                        gpointer       notify_data,
248                        GClosureNotify notify_func)
249 {
250   guint i;
251
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);
256
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;
261 }
262
263 static inline gboolean
264 closure_try_remove_inotify (GClosure       *closure,
265                             gpointer       notify_data,
266                             GClosureNotify notify_func)
267 {
268   GClosureNotifyData *ndata, *nlast;
269
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)
273       {
274         closure->n_inotifiers -= 1;
275         if (ndata < nlast)
276           *ndata = *nlast;
277
278         return TRUE;
279       }
280   return FALSE;
281 }
282
283 static inline gboolean
284 closure_try_remove_fnotify (GClosure       *closure,
285                             gpointer       notify_data,
286                             GClosureNotify notify_func)
287 {
288   GClosureNotifyData *ndata, *nlast;
289
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)
293       {
294         closure->n_fnotifiers -= 1;
295         if (ndata < nlast)
296           *ndata = *nlast;
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)];
302         return TRUE;
303       }
304   return FALSE;
305 }
306
307 GClosure*
308 g_closure_ref (GClosure *closure)
309 {
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);
313
314   /* floating is basically a kludge to avoid creating closures
315    * with a ref_count of 0. so the first one doing _ref() will
316    * own the closure's initial ref_count
317    */
318   if (closure->floating)
319     closure->floating = FALSE;
320   else
321     closure->ref_count += 1;
322
323   return closure;
324 }
325
326 void
327 g_closure_invalidate (GClosure *closure)
328 {
329   g_return_if_fail (closure != NULL);
330
331   if (!closure->is_invalid)
332     {
333       closure->ref_count += 1;  /* preserve floating flag */
334       closure->is_invalid = TRUE;
335       closure_invoke_notifiers (closure, INOTIFY);
336       g_closure_unref (closure);
337     }
338 }
339
340 void
341 g_closure_unref (GClosure *closure)
342 {
343   g_return_if_fail (closure != NULL);
344   g_return_if_fail (closure->ref_count > 0);
345
346   if (closure->ref_count == 1)  /* last unref, invalidate first */
347     g_closure_invalidate (closure);
348
349   closure->ref_count -= 1;
350
351   if (closure->ref_count == 0)
352     {
353       closure_invoke_notifiers (closure, FNOTIFY);
354       g_free (closure->notifiers);
355       g_free (closure);
356     }
357 }
358
359 void
360 g_closure_remove_inotify (GClosure      *closure,
361                           gpointer       notify_data,
362                           GClosureNotify notify_func)
363 {
364   g_return_if_fail (closure != NULL);
365   g_return_if_fail (notify_func != NULL);
366
367   if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
368       ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
369     closure->marshal = NULL;
370   else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
371     g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
372                notify_func, notify_data);
373 }
374
375 void
376 g_closure_remove_fnotify (GClosure      *closure,
377                           gpointer       notify_data,
378                           GClosureNotify notify_func)
379 {
380   g_return_if_fail (closure != NULL);
381   g_return_if_fail (notify_func != NULL);
382
383   if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
384       ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
385     closure->marshal = NULL;
386   else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
387     g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
388                notify_func, notify_data);
389 }
390
391 void
392 g_closure_invoke (GClosure       *closure,
393                   GValue /*out*/ *return_value,
394                   guint           n_param_values,
395                   const GValue   *param_values,
396                   gpointer        invocation_hint)
397 {
398   g_return_if_fail (closure != NULL);
399   g_return_if_fail (closure->marshal || closure->meta_marshal);
400
401   if (!closure->is_invalid)
402     {
403       GClosureMarshal marshal;
404       gpointer marshal_data;
405       gboolean in_marshal = closure->in_marshal;
406
407       closure->ref_count += 1;  /* preserve floating flag */
408       closure->in_marshal = TRUE;
409       if (closure->meta_marshal)
410         {
411           marshal_data = closure->notifiers[0].data;
412           marshal = (GClosureMarshal) closure->notifiers[0].notify;
413         }
414       else
415         {
416           marshal_data = NULL;
417           marshal = closure->marshal;
418         }
419       if (!in_marshal)
420         closure_invoke_notifiers (closure, PRE_NOTIFY);
421       marshal (closure,
422                return_value,
423                n_param_values, param_values,
424                invocation_hint,
425                marshal_data);
426       if (!in_marshal)
427         closure_invoke_notifiers (closure, POST_NOTIFY);
428       closure->in_marshal = in_marshal;
429       g_closure_unref (closure);
430     }
431 }
432
433 void
434 g_closure_set_marshal (GClosure       *closure,
435                        GClosureMarshal marshal)
436 {
437   g_return_if_fail (closure != NULL);
438   g_return_if_fail (marshal != NULL);
439
440   if (closure->marshal && closure->marshal != marshal)
441     g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
442                closure->marshal, marshal);
443   else
444     closure->marshal = marshal;
445 }
446
447 GClosure*
448 g_cclosure_new (GCallback      callback_func,
449                 gpointer       user_data,
450                 GClosureNotify destroy_data)
451 {
452   GClosure *closure;
453   
454   g_return_val_if_fail (callback_func != NULL, NULL);
455   
456   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
457   if (destroy_data)
458     g_closure_add_fnotify (closure, user_data, destroy_data);
459   ((GCClosure*) closure)->callback = callback_func;
460   
461   return closure;
462 }
463
464 GClosure*
465 g_cclosure_new_swap (GCallback      callback_func,
466                      gpointer       user_data,
467                      GClosureNotify destroy_data)
468 {
469   GClosure *closure;
470   
471   g_return_val_if_fail (callback_func != NULL, NULL);
472   
473   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
474   if (destroy_data)
475     g_closure_add_fnotify (closure, user_data, destroy_data);
476   ((GCClosure*) closure)->callback = callback_func;
477   closure->derivative_flag = TRUE;
478   
479   return closure;
480 }
481
482 static void
483 g_type_class_meta_marshal (GClosure       *closure,
484                            GValue /*out*/ *return_value,
485                            guint           n_param_values,
486                            const GValue   *param_values,
487                            gpointer        invocation_hint,
488                            gpointer        marshal_data)
489 {
490   GTypeClass *class;
491   gpointer callback;
492   /* GType itype = GPOINTER_TO_UINT (closure->data); */
493   guint offset = GPOINTER_TO_UINT (marshal_data);
494   
495   class = G_TYPE_INSTANCE_GET_CLASS (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
496   callback = G_STRUCT_MEMBER (gpointer, class, offset);
497   if (callback)
498     closure->marshal (closure,
499                       return_value,
500                       n_param_values, param_values,
501                       invocation_hint,
502                       callback);
503 }
504
505 static void
506 g_type_iface_meta_marshal (GClosure       *closure,
507                            GValue /*out*/ *return_value,
508                            guint           n_param_values,
509                            const GValue   *param_values,
510                            gpointer        invocation_hint,
511                            gpointer        marshal_data)
512 {
513   GTypeClass *class;
514   gpointer callback;
515   GType itype = GPOINTER_TO_UINT (closure->data);
516   guint offset = GPOINTER_TO_UINT (marshal_data);
517   
518   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
519   callback = G_STRUCT_MEMBER (gpointer, class, offset);
520   if (callback)
521     closure->marshal (closure,
522                       return_value,
523                       n_param_values, param_values,
524                       invocation_hint,
525                       callback);
526 }
527
528 GClosure*
529 g_signal_type_cclosure_new (GType    itype,
530                             guint    struct_offset)
531 {
532   GClosure *closure;
533   
534   g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
535   g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
536   
537   closure = g_closure_new_simple (sizeof (GClosure), GUINT_TO_POINTER (itype));
538   if (G_TYPE_IS_INTERFACE (itype))
539     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
540   else
541     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
542   
543   return closure;
544 }