increment version to 1.3.4 (binary 0, interface 0).
[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_finalize_notifier (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_invalidate_notifier (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_invalidate_notifier (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_finalize_notifier (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
414   if (!closure->is_invalid)
415     {
416       GClosureMarshal marshal;
417       gpointer marshal_data;
418       gboolean in_marshal = closure->in_marshal;
419
420       g_return_if_fail (closure->marshal || closure->meta_marshal);
421
422       closure->ref_count += 1;  /* preserve floating flag */
423       closure->in_marshal = TRUE;
424       if (closure->meta_marshal)
425         {
426           marshal_data = closure->notifiers[0].data;
427           marshal = (GClosureMarshal) closure->notifiers[0].notify;
428         }
429       else
430         {
431           marshal_data = NULL;
432           marshal = closure->marshal;
433         }
434       if (!in_marshal)
435         closure_invoke_notifiers (closure, PRE_NOTIFY);
436       marshal (closure,
437                return_value,
438                n_param_values, param_values,
439                invocation_hint,
440                marshal_data);
441       if (!in_marshal)
442         closure_invoke_notifiers (closure, POST_NOTIFY);
443       closure->in_marshal = in_marshal;
444       g_closure_unref (closure);
445     }
446 }
447
448 void
449 g_closure_set_marshal (GClosure       *closure,
450                        GClosureMarshal marshal)
451 {
452   g_return_if_fail (closure != NULL);
453   g_return_if_fail (marshal != NULL);
454
455   if (closure->marshal && closure->marshal != marshal)
456     g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
457                closure->marshal, marshal);
458   else
459     closure->marshal = marshal;
460 }
461
462 GClosure*
463 g_cclosure_new (GCallback      callback_func,
464                 gpointer       user_data,
465                 GClosureNotify destroy_data)
466 {
467   GClosure *closure;
468   
469   g_return_val_if_fail (callback_func != NULL, NULL);
470   
471   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
472   if (destroy_data)
473     g_closure_add_finalize_notifier (closure, user_data, destroy_data);
474   ((GCClosure*) closure)->callback = callback_func;
475   
476   return closure;
477 }
478
479 GClosure*
480 g_cclosure_new_swap (GCallback      callback_func,
481                      gpointer       user_data,
482                      GClosureNotify destroy_data)
483 {
484   GClosure *closure;
485   
486   g_return_val_if_fail (callback_func != NULL, NULL);
487   
488   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
489   if (destroy_data)
490     g_closure_add_finalize_notifier (closure, user_data, destroy_data);
491   ((GCClosure*) closure)->callback = callback_func;
492   closure->derivative_flag = TRUE;
493   
494   return closure;
495 }
496
497 static void
498 g_type_class_meta_marshal (GClosure       *closure,
499                            GValue /*out*/ *return_value,
500                            guint           n_param_values,
501                            const GValue   *param_values,
502                            gpointer        invocation_hint,
503                            gpointer        marshal_data)
504 {
505   GTypeClass *class;
506   gpointer callback;
507   /* GType itype = GPOINTER_TO_UINT (closure->data); */
508   guint offset = GPOINTER_TO_UINT (marshal_data);
509   
510   class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
511   callback = G_STRUCT_MEMBER (gpointer, class, offset);
512   if (callback)
513     closure->marshal (closure,
514                       return_value,
515                       n_param_values, param_values,
516                       invocation_hint,
517                       callback);
518 }
519
520 static void
521 g_type_iface_meta_marshal (GClosure       *closure,
522                            GValue /*out*/ *return_value,
523                            guint           n_param_values,
524                            const GValue   *param_values,
525                            gpointer        invocation_hint,
526                            gpointer        marshal_data)
527 {
528   GTypeClass *class;
529   gpointer callback;
530   GType itype = GPOINTER_TO_UINT (closure->data);
531   guint offset = GPOINTER_TO_UINT (marshal_data);
532   
533   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
534   callback = G_STRUCT_MEMBER (gpointer, class, offset);
535   if (callback)
536     closure->marshal (closure,
537                       return_value,
538                       n_param_values, param_values,
539                       invocation_hint,
540                       callback);
541 }
542
543 GClosure*
544 g_signal_type_cclosure_new (GType    itype,
545                             guint    struct_offset)
546 {
547   GClosure *closure;
548   
549   g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
550   g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
551   
552   closure = g_closure_new_simple (sizeof (GClosure), GUINT_TO_POINTER (itype));
553   if (G_TYPE_IS_INTERFACE (itype))
554     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
555   else
556     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
557   
558   return closure;
559 }