turned all modifications to the first 32 integer bits in a closure into
[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  * Copyright (C) 2005 Imendio AB
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 #include        "gclosure.h"
21
22 /*
23  * MT safe with regards to reference counting.
24  */
25
26 #include        "gvalue.h"
27 #include        "gobjectalias.h"
28 #include        <string.h>
29
30
31 #define CLOSURE_MAX_REF_COUNT           ((1 << 15) - 1)
32 #define CLOSURE_MAX_N_GUARDS            ((1 << 1) - 1)
33 #define CLOSURE_MAX_N_FNOTIFIERS        ((1 << 2) - 1)
34 #define CLOSURE_MAX_N_INOTIFIERS        ((1 << 8) - 1)
35 #define CLOSURE_N_MFUNCS(cl)            ((cl)->meta_marshal + \
36                                          ((cl)->n_guards << 1L))
37 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
38 #define CLOSURE_N_NOTIFIERS(cl)         (CLOSURE_N_MFUNCS (cl) + \
39                                          (cl)->n_fnotifiers + \
40                                          (cl)->n_inotifiers)
41
42 typedef union {
43   GClosure closure;
44   gint     vint;
45 } ClosureInt;
46
47 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW)      \
48 G_STMT_START {                                                                          \
49   ClosureInt *cunion = (ClosureInt*) _closure;                                          \
50   gint new_int, old_int, success;                                                       \
51   do                                                                                    \
52     {                                                                                   \
53       ClosureInt tmp;                                                                   \
54       tmp.vint = old_int = cunion->vint;                                                \
55       _SET_OLD tmp.closure._field;                                                      \
56       tmp.closure._field _OP _value;                                                    \
57       _SET_NEW tmp.closure._field;                                                      \
58       new_int = tmp.vint;                                                               \
59       success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int);    \
60     }                                                                                   \
61   while (!success && _must_set);                                                        \
62 } G_STMT_END
63
64 #define SWAP(_closure, _field, _value, _oldv)   CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =,     (void) )
65 #define SET(_closure, _field, _value)           CHANGE_FIELD (_closure, _field, =, _value, TRUE,     (void),     (void) )
66 #define INC(_closure, _field)                   CHANGE_FIELD (_closure, _field, +=,     1, TRUE,     (void),     (void) )
67 #define INC_ASSIGN(_closure, _field, _newv)     CHANGE_FIELD (_closure, _field, +=,     1, TRUE,     (void), *(_newv) = )
68 #define DEC(_closure, _field)                   CHANGE_FIELD (_closure, _field, -=,     1, TRUE,     (void),     (void) )
69 #define DEC_ASSIGN(_closure, _field, _newv)     CHANGE_FIELD (_closure, _field, -=,     1, TRUE,     (void), *(_newv) = )
70
71 #if 0   // for non-thread-safe closures
72 #define SWAP(cl,f,v,o)     (void) (*(o) = cl->f, cl->f = v)
73 #define SET(cl,f,v)        (void) (cl->f = v)
74 #define INC(cl,f)          (void) (cl->f += 1)
75 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
76 #define DEC(cl,f)          (void) (cl->f -= 1)
77 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
78 #endif
79
80 enum {
81   FNOTIFY,
82   INOTIFY,
83   PRE_NOTIFY,
84   POST_NOTIFY
85 };
86
87
88 /* --- functions --- */
89 GClosure*
90 g_closure_new_simple (guint           sizeof_closure,
91                       gpointer        data)
92 {
93   GClosure *closure;
94
95   g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
96
97   closure = g_malloc0 (sizeof_closure);
98   SET (closure, ref_count, 1);
99   SET (closure, meta_marshal, 0);
100   SET (closure, n_guards, 0);
101   SET (closure, n_fnotifiers, 0);
102   SET (closure, n_inotifiers, 0);
103   SET (closure, in_inotify, FALSE);
104   SET (closure, floating, TRUE);
105   SET (closure, derivative_flag, 0);
106   SET (closure, in_marshal, FALSE);
107   SET (closure, is_invalid, FALSE);
108   closure->marshal = NULL;
109   closure->data = data;
110   closure->notifiers = NULL;
111   memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
112
113   return closure;
114 }
115
116 static inline void
117 closure_invoke_notifiers (GClosure *closure,
118                           guint     notify_type)
119 {
120   /* notifier layout:
121    *     meta_marshal  n_guards    n_guards     n_fnotif.  n_inotifiers
122    * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
123    *
124    * CLOSURE_N_MFUNCS(cl)    = meta_marshal + n_guards + n_guards;
125    * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
126    *
127    * constrains/catches:
128    * - closure->notifiers may be reloacted during callback
129    * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
130    * - i.e. callbacks can be removed/added during invocation
131    * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
132    * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
133    * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
134    * + closure->meta_marshal is const for all cases
135    * + none of the callbacks can cause recursion
136    * + closure->n_inotifiers is const 0 during FNOTIFY
137    */
138   switch (notify_type)
139     {
140       GClosureNotifyData *ndata;
141       guint i, offs;
142     case FNOTIFY:
143       while (closure->n_fnotifiers)
144         {
145           guint n;
146           DEC_ASSIGN (closure, n_fnotifiers, &n);
147
148           ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
149           closure->marshal = (GClosureMarshal) ndata->notify;
150           closure->data = ndata->data;
151           ndata->notify (ndata->data, closure);
152         }
153       closure->marshal = NULL;
154       closure->data = NULL;
155       break;
156     case INOTIFY:
157       SET (closure, in_inotify, TRUE);
158       while (closure->n_inotifiers)
159         {
160           guint n;
161           DEC_ASSIGN (closure, n_inotifiers, &n);
162
163           ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
164           closure->marshal = (GClosureMarshal) ndata->notify;
165           closure->data = ndata->data;
166           ndata->notify (ndata->data, closure);
167         }
168       closure->marshal = NULL;
169       closure->data = NULL;
170       SET (closure, in_inotify, FALSE);
171       break;
172     case PRE_NOTIFY:
173       i = closure->n_guards;
174       offs = closure->meta_marshal;
175       while (i--)
176         {
177           ndata = closure->notifiers + offs + i;
178           ndata->notify (ndata->data, closure);
179         }
180       break;
181     case POST_NOTIFY:
182       i = closure->n_guards;
183       offs = closure->meta_marshal + i;
184       while (i--)
185         {
186           ndata = closure->notifiers + offs + i;
187           ndata->notify (ndata->data, closure);
188         }
189       break;
190     }
191 }
192
193 void
194 g_closure_set_meta_marshal (GClosure       *closure,
195                             gpointer        marshal_data,
196                             GClosureMarshal meta_marshal)
197 {
198   GClosureNotifyData *notifiers;
199   guint n;
200
201   g_return_if_fail (closure != NULL);
202   g_return_if_fail (meta_marshal != NULL);
203   g_return_if_fail (closure->is_invalid == FALSE);
204   g_return_if_fail (closure->in_marshal == FALSE);
205   g_return_if_fail (closure->meta_marshal == 0);
206
207   n = CLOSURE_N_NOTIFIERS (closure);
208   notifiers = closure->notifiers;
209   closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
210   if (notifiers)
211     {
212       /* usually the meta marshal will be setup right after creation, so the
213        * g_memmove() should be rare-case scenario
214        */
215       g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
216       g_free (notifiers);
217     }
218   closure->notifiers[0].data = marshal_data;
219   closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
220   SET (closure, meta_marshal, 1);
221 }
222
223 void
224 g_closure_add_marshal_guards (GClosure      *closure,
225                               gpointer       pre_marshal_data,
226                               GClosureNotify pre_marshal_notify,
227                               gpointer       post_marshal_data,
228                               GClosureNotify post_marshal_notify)
229 {
230   guint i;
231
232   g_return_if_fail (closure != NULL);
233   g_return_if_fail (pre_marshal_notify != NULL);
234   g_return_if_fail (post_marshal_notify != NULL);
235   g_return_if_fail (closure->is_invalid == FALSE);
236   g_return_if_fail (closure->in_marshal == FALSE);
237   g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
238
239   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
240   if (closure->n_inotifiers)
241     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
242                         closure->n_fnotifiers +
243                         closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
244                                                                           closure->n_fnotifiers + 0)];
245   if (closure->n_inotifiers > 1)
246     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
247                         closure->n_fnotifiers +
248                         closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
249                                                                       closure->n_fnotifiers + 1)];
250   if (closure->n_fnotifiers)
251     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
252                         closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
253   if (closure->n_fnotifiers > 1)
254     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
255                         closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
256   if (closure->n_guards)
257     closure->notifiers[(closure->meta_marshal +
258                         closure->n_guards +
259                         closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
260   i = closure->n_guards;
261   closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
262   closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
263   closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
264   closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
265   INC (closure, n_guards);
266 }
267
268 void
269 g_closure_add_finalize_notifier (GClosure      *closure,
270                                  gpointer       notify_data,
271                                  GClosureNotify notify_func)
272 {
273   guint i;
274
275   g_return_if_fail (closure != NULL);
276   g_return_if_fail (notify_func != NULL);
277   g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
278
279   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
280   if (closure->n_inotifiers)
281     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
282                         closure->n_fnotifiers +
283                         closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
284                                                                       closure->n_fnotifiers + 0)];
285   i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
286   closure->notifiers[i].data = notify_data;
287   closure->notifiers[i].notify = notify_func;
288   INC (closure, n_fnotifiers);
289 }
290
291 void
292 g_closure_add_invalidate_notifier (GClosure      *closure,
293                                    gpointer       notify_data,
294                                    GClosureNotify notify_func)
295 {
296   guint i;
297
298   g_return_if_fail (closure != NULL);
299   g_return_if_fail (notify_func != NULL);
300   g_return_if_fail (closure->is_invalid == FALSE);
301   g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
302
303   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
304   i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
305   closure->notifiers[i].data = notify_data;
306   closure->notifiers[i].notify = notify_func;
307   INC (closure, n_inotifiers);
308 }
309
310 static inline gboolean
311 closure_try_remove_inotify (GClosure       *closure,
312                             gpointer       notify_data,
313                             GClosureNotify notify_func)
314 {
315   GClosureNotifyData *ndata, *nlast;
316
317   nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
318   for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
319     if (ndata->notify == notify_func && ndata->data == notify_data)
320       {
321         DEC (closure, n_inotifiers);
322         if (ndata < nlast)
323           *ndata = *nlast;
324
325         return TRUE;
326       }
327   return FALSE;
328 }
329
330 static inline gboolean
331 closure_try_remove_fnotify (GClosure       *closure,
332                             gpointer       notify_data,
333                             GClosureNotify notify_func)
334 {
335   GClosureNotifyData *ndata, *nlast;
336
337   nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
338   for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
339     if (ndata->notify == notify_func && ndata->data == notify_data)
340       {
341         DEC (closure, n_fnotifiers);
342         if (ndata < nlast)
343           *ndata = *nlast;
344         if (closure->n_inotifiers)
345           closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
346                               closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
347                                                                             closure->n_fnotifiers +
348                                                                             closure->n_inotifiers)];
349         return TRUE;
350       }
351   return FALSE;
352 }
353
354 GClosure*
355 g_closure_ref (GClosure *closure)
356 {
357   guint new_ref_count;
358   g_return_val_if_fail (closure != NULL, NULL);
359   g_return_val_if_fail (closure->ref_count > 0, NULL);
360   g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
361
362   INC_ASSIGN (closure, ref_count, &new_ref_count);
363   g_return_val_if_fail (new_ref_count > 1, NULL);
364
365   return closure;
366 }
367
368 void
369 g_closure_invalidate (GClosure *closure)
370 {
371   g_return_if_fail (closure != NULL);
372
373   if (!closure->is_invalid)
374     {
375       gboolean was_invalid;
376       g_closure_ref (closure);           /* preserve floating flag */
377       SWAP (closure, is_invalid, TRUE, &was_invalid);
378       /* invalidate only once */
379       if (!was_invalid)
380         closure_invoke_notifiers (closure, INOTIFY);
381       g_closure_unref (closure);
382     }
383 }
384
385 void
386 g_closure_unref (GClosure *closure)
387 {
388   guint new_ref_count;
389
390   g_return_if_fail (closure != NULL);
391   g_return_if_fail (closure->ref_count > 0);
392
393   if (closure->ref_count == 1)  /* last unref, invalidate first */
394     g_closure_invalidate (closure);
395
396   DEC_ASSIGN (closure, ref_count, &new_ref_count);
397
398   if (new_ref_count == 0)
399     {
400       closure_invoke_notifiers (closure, FNOTIFY);
401       g_free (closure->notifiers);
402       g_free (closure);
403     }
404 }
405
406 void
407 g_closure_sink (GClosure *closure)
408 {
409   g_return_if_fail (closure != NULL);
410   g_return_if_fail (closure->ref_count > 0);
411
412   /* floating is basically a kludge to avoid creating closures
413    * with a ref_count of 0. so the intial ref_count a closure has
414    * is unowned. with invoking g_closure_sink() code may
415    * indicate that it takes over that intiial ref_count.
416    */
417   if (closure->floating)
418     {
419       gboolean was_floating;
420       SWAP (closure, floating, FALSE, &was_floating);
421       /* unref floating flag only once */
422       if (was_floating)
423         g_closure_unref (closure);
424     }
425 }
426
427 void
428 g_closure_remove_invalidate_notifier (GClosure      *closure,
429                                       gpointer       notify_data,
430                                       GClosureNotify notify_func)
431 {
432   g_return_if_fail (closure != NULL);
433   g_return_if_fail (notify_func != NULL);
434
435   if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
436       ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
437       closure->data == notify_data)
438     closure->marshal = NULL;
439   else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
440     g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
441                notify_func, notify_data);
442 }
443
444 void
445 g_closure_remove_finalize_notifier (GClosure      *closure,
446                                     gpointer       notify_data,
447                                     GClosureNotify notify_func)
448 {
449   g_return_if_fail (closure != NULL);
450   g_return_if_fail (notify_func != NULL);
451
452   if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
453       ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
454       closure->data == notify_data)
455     closure->marshal = NULL;
456   else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
457     g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
458                notify_func, notify_data);
459 }
460
461 void
462 g_closure_invoke (GClosure       *closure,
463                   GValue /*out*/ *return_value,
464                   guint           n_param_values,
465                   const GValue   *param_values,
466                   gpointer        invocation_hint)
467 {
468   g_return_if_fail (closure != NULL);
469
470   g_closure_ref (closure);      /* preserve floating flag */
471   if (!closure->is_invalid)
472     {
473       GClosureMarshal marshal;
474       gpointer marshal_data;
475       gboolean in_marshal = closure->in_marshal;
476
477       g_return_if_fail (closure->marshal || closure->meta_marshal);
478
479       SET (closure, in_marshal, TRUE);
480       if (closure->meta_marshal)
481         {
482           marshal_data = closure->notifiers[0].data;
483           marshal = (GClosureMarshal) closure->notifiers[0].notify;
484         }
485       else
486         {
487           marshal_data = NULL;
488           marshal = closure->marshal;
489         }
490       if (!in_marshal)
491         closure_invoke_notifiers (closure, PRE_NOTIFY);
492       marshal (closure,
493                return_value,
494                n_param_values, param_values,
495                invocation_hint,
496                marshal_data);
497       if (!in_marshal)
498         closure_invoke_notifiers (closure, POST_NOTIFY);
499       SET (closure, in_marshal, in_marshal);
500     }
501   g_closure_unref (closure);
502 }
503
504 void
505 g_closure_set_marshal (GClosure       *closure,
506                        GClosureMarshal marshal)
507 {
508   g_return_if_fail (closure != NULL);
509   g_return_if_fail (marshal != NULL);
510
511   if (closure->marshal && closure->marshal != marshal)
512     g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
513                closure->marshal, marshal);
514   else
515     closure->marshal = marshal;
516 }
517
518 GClosure*
519 g_cclosure_new (GCallback      callback_func,
520                 gpointer       user_data,
521                 GClosureNotify destroy_data)
522 {
523   GClosure *closure;
524   
525   g_return_val_if_fail (callback_func != NULL, NULL);
526   
527   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
528   if (destroy_data)
529     g_closure_add_finalize_notifier (closure, user_data, destroy_data);
530   ((GCClosure*) closure)->callback = (gpointer) callback_func;
531   
532   return closure;
533 }
534
535 GClosure*
536 g_cclosure_new_swap (GCallback      callback_func,
537                      gpointer       user_data,
538                      GClosureNotify destroy_data)
539 {
540   GClosure *closure;
541   
542   g_return_val_if_fail (callback_func != NULL, NULL);
543   
544   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
545   if (destroy_data)
546     g_closure_add_finalize_notifier (closure, user_data, destroy_data);
547   ((GCClosure*) closure)->callback = (gpointer) callback_func;
548   SET (closure, derivative_flag, TRUE);
549   
550   return closure;
551 }
552
553 static void
554 g_type_class_meta_marshal (GClosure       *closure,
555                            GValue /*out*/ *return_value,
556                            guint           n_param_values,
557                            const GValue   *param_values,
558                            gpointer        invocation_hint,
559                            gpointer        marshal_data)
560 {
561   GTypeClass *class;
562   gpointer callback;
563   /* GType itype = (GType) closure->data; */
564   guint offset = GPOINTER_TO_UINT (marshal_data);
565   
566   class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
567   callback = G_STRUCT_MEMBER (gpointer, class, offset);
568   if (callback)
569     closure->marshal (closure,
570                       return_value,
571                       n_param_values, param_values,
572                       invocation_hint,
573                       callback);
574 }
575
576 static void
577 g_type_iface_meta_marshal (GClosure       *closure,
578                            GValue /*out*/ *return_value,
579                            guint           n_param_values,
580                            const GValue   *param_values,
581                            gpointer        invocation_hint,
582                            gpointer        marshal_data)
583 {
584   GTypeClass *class;
585   gpointer callback;
586   GType itype = (GType) closure->data;
587   guint offset = GPOINTER_TO_UINT (marshal_data);
588   
589   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
590   callback = G_STRUCT_MEMBER (gpointer, class, offset);
591   if (callback)
592     closure->marshal (closure,
593                       return_value,
594                       n_param_values, param_values,
595                       invocation_hint,
596                       callback);
597 }
598
599 GClosure*
600 g_signal_type_cclosure_new (GType    itype,
601                             guint    struct_offset)
602 {
603   GClosure *closure;
604   
605   g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
606   g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
607   
608   closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
609   if (G_TYPE_IS_INTERFACE (itype))
610     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
611   else
612     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
613   
614   return closure;
615 }
616
617 #define __G_CLOSURE_C__
618 #include "gobjectaliasdef.c"