changed prototype of g_boxed_type_register_static() to contain an optional
[platform/upstream/glib.git] / gobject / gclosure.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 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 == 0);
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   closure->ref_count += 1;
315
316   return closure;
317 }
318
319 void
320 g_closure_invalidate (GClosure *closure)
321 {
322   g_return_if_fail (closure != NULL);
323
324   if (!closure->is_invalid)
325     {
326       closure->ref_count += 1;  /* preserve floating flag */
327       closure->is_invalid = TRUE;
328       closure_invoke_notifiers (closure, INOTIFY);
329       g_closure_unref (closure);
330     }
331 }
332
333 void
334 g_closure_unref (GClosure *closure)
335 {
336   g_return_if_fail (closure != NULL);
337   g_return_if_fail (closure->ref_count > 0);
338
339   if (closure->ref_count == 1)  /* last unref, invalidate first */
340     g_closure_invalidate (closure);
341
342   closure->ref_count -= 1;
343
344   if (closure->ref_count == 0)
345     {
346       closure_invoke_notifiers (closure, FNOTIFY);
347       g_free (closure->notifiers);
348       g_free (closure);
349     }
350 }
351
352 void
353 g_closure_sink (GClosure *closure)
354 {
355   g_return_if_fail (closure != NULL);
356   g_return_if_fail (closure->ref_count > 0);
357
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.
362    */
363   if (closure->floating)
364     {
365       closure->floating = FALSE;
366       if (closure->ref_count > 1)
367         closure->ref_count -= 1;
368       else
369         g_closure_unref (closure);
370     }
371 }
372
373 void
374 g_closure_remove_inotify (GClosure      *closure,
375                           gpointer       notify_data,
376                           GClosureNotify notify_func)
377 {
378   g_return_if_fail (closure != NULL);
379   g_return_if_fail (notify_func != NULL);
380
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);
387 }
388
389 void
390 g_closure_remove_fnotify (GClosure      *closure,
391                           gpointer       notify_data,
392                           GClosureNotify notify_func)
393 {
394   g_return_if_fail (closure != NULL);
395   g_return_if_fail (notify_func != NULL);
396
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);
403 }
404
405 void
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)
411 {
412   g_return_if_fail (closure != NULL);
413   g_return_if_fail (closure->marshal || closure->meta_marshal);
414
415   if (!closure->is_invalid)
416     {
417       GClosureMarshal marshal;
418       gpointer marshal_data;
419       gboolean in_marshal = closure->in_marshal;
420
421       closure->ref_count += 1;  /* preserve floating flag */
422       closure->in_marshal = TRUE;
423       if (closure->meta_marshal)
424         {
425           marshal_data = closure->notifiers[0].data;
426           marshal = (GClosureMarshal) closure->notifiers[0].notify;
427         }
428       else
429         {
430           marshal_data = NULL;
431           marshal = closure->marshal;
432         }
433       if (!in_marshal)
434         closure_invoke_notifiers (closure, PRE_NOTIFY);
435       marshal (closure,
436                return_value,
437                n_param_values, param_values,
438                invocation_hint,
439                marshal_data);
440       if (!in_marshal)
441         closure_invoke_notifiers (closure, POST_NOTIFY);
442       closure->in_marshal = in_marshal;
443       g_closure_unref (closure);
444     }
445 }
446
447 void
448 g_closure_set_marshal (GClosure       *closure,
449                        GClosureMarshal marshal)
450 {
451   g_return_if_fail (closure != NULL);
452   g_return_if_fail (marshal != NULL);
453
454   if (closure->marshal && closure->marshal != marshal)
455     g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
456                closure->marshal, marshal);
457   else
458     closure->marshal = marshal;
459 }
460
461 GClosure*
462 g_cclosure_new (GCallback      callback_func,
463                 gpointer       user_data,
464                 GClosureNotify destroy_data)
465 {
466   GClosure *closure;
467   
468   g_return_val_if_fail (callback_func != NULL, NULL);
469   
470   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
471   if (destroy_data)
472     g_closure_add_fnotify (closure, user_data, destroy_data);
473   ((GCClosure*) closure)->callback = callback_func;
474   
475   return closure;
476 }
477
478 GClosure*
479 g_cclosure_new_swap (GCallback      callback_func,
480                      gpointer       user_data,
481                      GClosureNotify destroy_data)
482 {
483   GClosure *closure;
484   
485   g_return_val_if_fail (callback_func != NULL, NULL);
486   
487   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
488   if (destroy_data)
489     g_closure_add_fnotify (closure, user_data, destroy_data);
490   ((GCClosure*) closure)->callback = callback_func;
491   closure->derivative_flag = TRUE;
492   
493   return closure;
494 }
495
496 static void
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)
503 {
504   GTypeClass *class;
505   gpointer callback;
506   /* GType itype = GPOINTER_TO_UINT (closure->data); */
507   guint offset = GPOINTER_TO_UINT (marshal_data);
508   
509   class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
510   callback = G_STRUCT_MEMBER (gpointer, class, offset);
511   if (callback)
512     closure->marshal (closure,
513                       return_value,
514                       n_param_values, param_values,
515                       invocation_hint,
516                       callback);
517 }
518
519 static void
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)
526 {
527   GTypeClass *class;
528   gpointer callback;
529   GType itype = GPOINTER_TO_UINT (closure->data);
530   guint offset = GPOINTER_TO_UINT (marshal_data);
531   
532   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
533   callback = G_STRUCT_MEMBER (gpointer, class, offset);
534   if (callback)
535     closure->marshal (closure,
536                       return_value,
537                       n_param_values, param_values,
538                       invocation_hint,
539                       callback);
540 }
541
542 GClosure*
543 g_signal_type_cclosure_new (GType    itype,
544                             guint    struct_offset)
545 {
546   GClosure *closure;
547   
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);
550   
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);
554   else
555     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
556   
557   return closure;
558 }