Remove all docs from gobject at Tims request. Documentation is only for
[platform/upstream/glib.git] / gobject / gsignal.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  * this code is based on the original GtkSignal implementation
20  * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
21  */
22
23 /*
24  * MT safe
25  */
26
27 #include <config.h>
28
29 #include        "gsignal.h"
30 #include        "gbsearcharray.h"
31 #include        "gvaluecollector.h"
32 #include        "gvaluetypes.h"
33 #include        "gboxed.h"
34 #include        <string.h> 
35 #include        <signal.h>
36
37
38 /* pre allocation configurations
39  */
40 #define MAX_STACK_VALUES        (16)
41 #define HANDLER_PRE_ALLOC       (48)
42
43 #define REPORT_BUG      "please report occourance circumstances to gtk-devel-list@gnome.org"
44 #ifdef  G_ENABLE_DEBUG
45 #define IF_DEBUG(debug_type, cond)      if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
46 static volatile gpointer g_trace_instance_signals = NULL;
47 static volatile gpointer g_trap_instance_signals = NULL;
48 #endif  /* G_ENABLE_DEBUG */
49
50
51 /* --- generic allocation --- */
52 /* we special case allocations generically by replacing
53  * these functions with more speed/memory aware variants
54  */
55 #ifndef DISABLE_MEM_POOLS
56 static inline gpointer
57 g_generic_node_alloc (GTrashStack **trash_stack_p,
58                       guint         sizeof_node,
59                       guint         nodes_pre_alloc)
60 {
61   gpointer node = g_trash_stack_pop (trash_stack_p);
62   
63   if (!node)
64     {
65       guint8 *block;
66       
67       nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
68       block = g_malloc (sizeof_node * nodes_pre_alloc);
69       while (--nodes_pre_alloc)
70         {
71           g_trash_stack_push (trash_stack_p, block);
72           block += sizeof_node;
73         }
74       node = block;
75     }
76   
77   return node;
78 }
79 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
80 #else   /* !DISABLE_MEM_POOLS */
81 #define g_generic_node_alloc(t,sizeof_node,p)    g_malloc (sizeof_node)
82 #define g_generic_node_free(t,node)              g_free (node)
83 #endif  /* !DISABLE_MEM_POOLS */
84
85
86 /* --- typedefs --- */
87 typedef struct _SignalNode   SignalNode;
88 typedef struct _SignalKey    SignalKey;
89 typedef struct _Emission     Emission;
90 typedef struct _Handler      Handler;
91 typedef struct _HandlerList  HandlerList;
92 typedef struct _HandlerMatch HandlerMatch;
93 typedef enum
94 {
95   EMISSION_STOP,
96   EMISSION_RUN,
97   EMISSION_HOOK,
98   EMISSION_RESTART
99 } EmissionState;
100
101
102 /* --- prototypes --- */
103 static inline guint             signal_id_lookup        (GQuark           quark,
104                                                          GType            itype);
105 static        void              signal_destroy_R        (SignalNode      *signal_node);
106 static inline HandlerList*      handler_list_ensure     (guint            signal_id,
107                                                          gpointer         instance);
108 static inline HandlerList*      handler_list_lookup     (guint            signal_id,
109                                                          gpointer         instance);
110 static inline Handler*          handler_new             (gboolean         after);
111 static        void              handler_insert          (guint            signal_id,
112                                                          gpointer         instance,
113                                                          Handler         *handler);
114 static        Handler*          handler_lookup          (gpointer         instance,
115                                                          gulong           handler_id,
116                                                          guint           *signal_id_p);
117 static inline HandlerMatch*     handler_match_prepend   (HandlerMatch    *list,
118                                                          Handler         *handler,
119                                                          guint            signal_id);
120 static inline HandlerMatch*     handler_match_free1_R   (HandlerMatch    *node,
121                                                          gpointer         instance);
122 static        HandlerMatch*     handlers_find           (gpointer         instance,
123                                                          GSignalMatchType mask,
124                                                          guint            signal_id,
125                                                          GQuark           detail,
126                                                          GClosure        *closure,
127                                                          gpointer         func,
128                                                          gpointer         data,
129                                                          gboolean         one_and_only);
130 static inline void              handler_ref             (Handler         *handler);
131 static inline void              handler_unref_R         (guint            signal_id,
132                                                          gpointer         instance,
133                                                          Handler         *handler);
134 static gint                     handler_lists_cmp       (gconstpointer    node1,
135                                                          gconstpointer    node2);
136 static inline void              emission_push           (Emission       **emission_list_p,
137                                                          Emission        *emission);
138 static inline void              emission_pop            (Emission       **emission_list_p,
139                                                          Emission        *emission);
140 static inline Emission*         emission_find           (Emission        *emission_list,
141                                                          guint            signal_id,
142                                                          GQuark           detail,
143                                                          gpointer         instance);
144 static gint                     class_closures_cmp      (gconstpointer    node1,
145                                                          gconstpointer    node2);
146 static gint                     signal_key_cmp          (gconstpointer    node1,
147                                                          gconstpointer    node2);
148 static        gboolean          signal_emit_unlocked_R  (SignalNode      *node,
149                                                          GQuark           detail,
150                                                          gpointer         instance,
151                                                          GValue          *return_value,
152                                                          const GValue    *instance_and_params);
153 static const gchar *            type_debug_name         (GType            type);
154
155
156 /* --- structures --- */
157 typedef struct
158 {
159   GSignalAccumulator func;
160   gpointer           data;
161 } SignalAccumulator;
162 typedef struct
163 {
164   GHook hook;
165   GQuark detail;
166 } SignalHook;
167 #define SIGNAL_HOOK(hook)       ((SignalHook*) (hook))
168
169 struct _SignalNode
170 {
171   /* permanent portion */
172   guint              signal_id;
173   GType              itype;
174   gchar             *name;
175   guint              destroyed : 1;
176   
177   /* reinitializable portion */
178   guint              flags : 8;
179   guint              n_params : 8;
180   GType             *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
181   GType              return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
182   GBSearchArray     *class_closure_bsa;
183   SignalAccumulator *accumulator;
184   GSignalCMarshaller c_marshaller;
185   GHookList         *emission_hooks;
186 };
187
188 struct _SignalKey
189 {
190   GType  itype;
191   GQuark quark;
192   guint  signal_id;
193 };
194
195 struct _Emission
196 {
197   Emission             *next;
198   gpointer              instance;
199   GSignalInvocationHint ihint;
200   EmissionState         state;
201   GType                 chain_type;
202 };
203
204 struct _HandlerList
205 {
206   guint    signal_id;
207   Handler *handlers;
208 };
209 struct _Handler
210 {
211   gulong        sequential_number;
212   Handler      *next;
213   Handler      *prev;
214   GQuark        detail;
215   guint         ref_count : 16;
216 #define HANDLER_MAX_REF_COUNT   (1 << 16)
217   guint         block_count : 12;
218 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
219   guint         after : 1;
220   GClosure     *closure;
221 };
222 struct _HandlerMatch
223 {
224   Handler      *handler;
225   HandlerMatch *next;
226   union {
227     guint       signal_id;
228     gpointer    dummy;
229   } d;
230 };
231
232 typedef struct
233 {
234   GType     instance_type; /* 0 for default closure */
235   GClosure *closure;
236 } ClassClosure;
237
238
239 /* --- variables --- */
240 static GBSearchArray *g_signal_key_bsa = NULL;
241 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
242                                                                signal_key_cmp,
243                                                                G_BSEARCH_ARRAY_ALIGN_POWER2);
244 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
245                                                                  handler_lists_cmp,
246                                                                  G_BSEARCH_ARRAY_DEFER_SHRINK);
247 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
248                                                                   class_closures_cmp,
249                                                                   0);
250 static GHashTable    *g_handler_list_bsa_ht = NULL;
251 static Emission      *g_recursive_emissions = NULL;
252 static Emission      *g_restart_emissions = NULL;
253 #ifndef DISABLE_MEM_POOLS
254 static GTrashStack   *g_handler_ts = NULL;
255 #endif
256 static gulong         g_handler_sequential_number = 1;
257 G_LOCK_DEFINE_STATIC (g_signal_mutex);
258 #define SIGNAL_LOCK()           G_LOCK (g_signal_mutex)
259 #define SIGNAL_UNLOCK()         G_UNLOCK (g_signal_mutex)
260
261
262 /* --- signal nodes --- */
263 static guint          g_n_signal_nodes = 0;
264 static SignalNode   **g_signal_nodes = NULL;
265
266 static inline SignalNode*
267 LOOKUP_SIGNAL_NODE (register guint signal_id)
268 {
269   if (signal_id < g_n_signal_nodes)
270     return g_signal_nodes[signal_id];
271   else
272     return NULL;
273 }
274
275
276 /* --- functions --- */
277 static inline guint
278 signal_id_lookup (GQuark quark,
279                   GType  itype)
280 {
281   GType *ifaces, type = itype;
282   SignalKey key;
283   guint n_ifaces;
284
285   key.quark = quark;
286
287   /* try looking up signals for this type and its anchestors */
288   do
289     {
290       SignalKey *signal_key;
291       
292       key.itype = type;
293       signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
294       
295       if (signal_key)
296         return signal_key->signal_id;
297       
298       type = g_type_parent (type);
299     }
300   while (type);
301
302   /* no luck, try interfaces it exports */
303   ifaces = g_type_interfaces (itype, &n_ifaces);
304   while (n_ifaces--)
305     {
306       SignalKey *signal_key;
307
308       key.itype = ifaces[n_ifaces];
309       signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
310
311       if (signal_key)
312         {
313           g_free (ifaces);
314           return signal_key->signal_id;
315         }
316     }
317   g_free (ifaces);
318   
319   return 0;
320 }
321
322 static gint
323 class_closures_cmp (gconstpointer node1,
324                     gconstpointer node2)
325 {
326   const ClassClosure *c1 = node1, *c2 = node2;
327   
328   return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
329 }
330
331 static gint
332 handler_lists_cmp (gconstpointer node1,
333                    gconstpointer node2)
334 {
335   const HandlerList *hlist1 = node1, *hlist2 = node2;
336   
337   return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
338 }
339
340 static inline HandlerList*
341 handler_list_ensure (guint    signal_id,
342                      gpointer instance)
343 {
344   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
345   HandlerList key;
346   
347   key.signal_id = signal_id;
348   key.handlers = NULL;
349   if (!hlbsa)
350     {
351       hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
352       hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
353       g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
354     }
355   else
356     {
357       GBSearchArray *o = hlbsa;
358
359       hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
360       if (hlbsa != o)
361         g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
362     }
363   return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
364 }
365
366 static inline HandlerList*
367 handler_list_lookup (guint    signal_id,
368                      gpointer instance)
369 {
370   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
371   HandlerList key;
372   
373   key.signal_id = signal_id;
374   
375   return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
376 }
377
378 static Handler*
379 handler_lookup (gpointer instance,
380                 gulong   handler_id,
381                 guint   *signal_id_p)
382 {
383   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
384   
385   if (hlbsa)
386     {
387       guint i;
388       
389       for (i = 0; i < hlbsa->n_nodes; i++)
390         {
391           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
392           Handler *handler;
393           
394           for (handler = hlist->handlers; handler; handler = handler->next)
395             if (handler->sequential_number == handler_id)
396               {
397                 if (signal_id_p)
398                   *signal_id_p = hlist->signal_id;
399                 
400                 return handler;
401               }
402         }
403     }
404   
405   return NULL;
406 }
407
408 static inline HandlerMatch*
409 handler_match_prepend (HandlerMatch *list,
410                        Handler      *handler,
411                        guint         signal_id)
412 {
413   HandlerMatch *node;
414   
415   /* yeah, we could use our own memchunk here, introducing yet more
416    * rarely used cached nodes and extra allocation overhead.
417    * instead, we use GList* nodes, since they are exactly the size
418    * we need and are already cached. g_signal_init() asserts this.
419    */
420   node = (HandlerMatch*) g_list_alloc ();
421   node->handler = handler;
422   node->next = list;
423   node->d.signal_id = signal_id;
424   handler_ref (handler);
425   
426   return node;
427 }
428 static inline HandlerMatch*
429 handler_match_free1_R (HandlerMatch *node,
430                        gpointer      instance)
431 {
432   HandlerMatch *next = node->next;
433   
434   handler_unref_R (node->d.signal_id, instance, node->handler);
435   g_list_free_1 ((GList*) node);
436   
437   return next;
438 }
439
440 static HandlerMatch*
441 handlers_find (gpointer         instance,
442                GSignalMatchType mask,
443                guint            signal_id,
444                GQuark           detail,
445                GClosure        *closure,
446                gpointer         func,
447                gpointer         data,
448                gboolean         one_and_only)
449 {
450   HandlerMatch *mlist = NULL;
451   
452   if (mask & G_SIGNAL_MATCH_ID)
453     {
454       HandlerList *hlist = handler_list_lookup (signal_id, instance);
455       Handler *handler;
456       SignalNode *node = NULL;
457       
458       if (mask & G_SIGNAL_MATCH_FUNC)
459         {
460           node = LOOKUP_SIGNAL_NODE (signal_id);
461           if (!node || !node->c_marshaller)
462             return NULL;
463         }
464       
465       mask = ~mask;
466       for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
467         if (handler->sequential_number &&
468             ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
469             ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
470             ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
471             ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
472             ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
473                                               handler->closure->meta_marshal == 0 &&
474                                               ((GCClosure*) handler->closure)->callback == func)))
475           {
476             mlist = handler_match_prepend (mlist, handler, signal_id);
477             if (one_and_only)
478               return mlist;
479           }
480     }
481   else
482     {
483       GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
484       
485       mask = ~mask;
486       if (hlbsa)
487         {
488           guint i;
489           
490           for (i = 0; i < hlbsa->n_nodes; i++)
491             {
492               HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
493               SignalNode *node = NULL;
494               Handler *handler;
495               
496               if (!(mask & G_SIGNAL_MATCH_FUNC))
497                 {
498                   node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
499                   if (!node->c_marshaller)
500                     continue;
501                 }
502               
503               for (handler = hlist->handlers; handler; handler = handler->next)
504                 if (handler->sequential_number &&
505                     ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
506                     ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
507                     ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
508                     ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
509                     ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
510                                                       handler->closure->meta_marshal == 0 &&
511                                                       ((GCClosure*) handler->closure)->callback == func)))
512                   {
513                     mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
514                     if (one_and_only)
515                       return mlist;
516                   }
517             }
518         }
519     }
520   
521   return mlist;
522 }
523
524 static inline Handler*
525 handler_new (gboolean after)
526 {
527   Handler *handler = g_generic_node_alloc (&g_handler_ts,
528                                            sizeof (Handler),
529                                            HANDLER_PRE_ALLOC);
530 #ifndef G_DISABLE_CHECKS
531   if (g_handler_sequential_number < 1)
532     g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
533 #endif
534   
535   handler->sequential_number = g_handler_sequential_number++;
536   handler->prev = NULL;
537   handler->next = NULL;
538   handler->detail = 0;
539   handler->ref_count = 1;
540   handler->block_count = 0;
541   handler->after = after != FALSE;
542   handler->closure = NULL;
543   
544   return handler;
545 }
546
547 static inline void
548 handler_ref (Handler *handler)
549 {
550   g_return_if_fail (handler->ref_count > 0);
551   
552 #ifndef G_DISABLE_CHECKS
553   if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
554     g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
555 #endif
556   
557   handler->ref_count += 1;
558 }
559
560 static inline void
561 handler_unref_R (guint    signal_id,
562                  gpointer instance,
563                  Handler *handler)
564 {
565   g_return_if_fail (handler->ref_count > 0);
566   
567   handler->ref_count -= 1;
568   if (!handler->ref_count)
569     {
570       if (handler->next)
571         handler->next->prev = handler->prev;
572       if (handler->prev)        /* watch out for g_signal_handlers_destroy()! */
573         handler->prev->next = handler->next;
574       else
575         {
576           HandlerList *hlist = handler_list_lookup (signal_id, instance);
577           
578           hlist->handlers = handler->next;
579         }
580       SIGNAL_UNLOCK ();
581       g_closure_unref (handler->closure);
582       SIGNAL_LOCK ();
583       g_generic_node_free (&g_handler_ts, handler);
584     }
585 }
586
587 static void
588 handler_insert (guint    signal_id,
589                 gpointer instance,
590                 Handler  *handler)
591 {
592   HandlerList *hlist;
593   
594   g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
595   
596   hlist = handler_list_ensure (signal_id, instance);
597   if (!hlist->handlers)
598     hlist->handlers = handler;
599   else if (hlist->handlers->after && !handler->after)
600     {
601       handler->next = hlist->handlers;
602       hlist->handlers->prev = handler;
603       hlist->handlers = handler;
604     }
605   else
606     {
607       Handler *tmp = hlist->handlers;
608       
609       if (handler->after)
610         while (tmp->next)
611           tmp = tmp->next;
612       else
613         while (tmp->next && !tmp->next->after)
614           tmp = tmp->next;
615       if (tmp->next)
616         tmp->next->prev = handler;
617       handler->next = tmp->next;
618       handler->prev = tmp;
619       tmp->next = handler;
620     }
621 }
622
623 static inline void
624 emission_push (Emission **emission_list_p,
625                Emission  *emission)
626 {
627   emission->next = *emission_list_p;
628   *emission_list_p = emission;
629 }
630
631 static inline void
632 emission_pop (Emission **emission_list_p,
633               Emission  *emission)
634 {
635   Emission *node, *last = NULL;
636
637   for (node = *emission_list_p; node; last = node, node = last->next)
638     if (node == emission)
639       {
640         if (last)
641           last->next = node->next;
642         else
643           *emission_list_p = node->next;
644         return;
645       }
646   g_assert_not_reached ();
647 }
648
649 static inline Emission*
650 emission_find (Emission *emission_list,
651                guint     signal_id,
652                GQuark    detail,
653                gpointer  instance)
654 {
655   Emission *emission;
656   
657   for (emission = emission_list; emission; emission = emission->next)
658     if (emission->instance == instance &&
659         emission->ihint.signal_id == signal_id &&
660         emission->ihint.detail == detail)
661       return emission;
662   return NULL;
663 }
664
665 static inline Emission*
666 emission_find_innermost (gpointer instance)
667 {
668   Emission *emission, *s = NULL, *c = NULL;
669   
670   for (emission = g_restart_emissions; emission; emission = emission->next)
671     if (emission->instance == instance)
672       {
673         s = emission;
674         break;
675       }
676   for (emission = g_recursive_emissions; emission; emission = emission->next)
677     if (emission->instance == instance)
678       {
679         c = emission;
680         break;
681       }
682   if (!s)
683     return c;
684   else if (!c)
685     return s;
686   else
687     return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
688 }
689
690 static gint
691 signal_key_cmp (gconstpointer node1,
692                 gconstpointer node2)
693 {
694   const SignalKey *key1 = node1, *key2 = node2;
695   
696   if (key1->itype == key2->itype)
697     return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
698   else
699     return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
700 }
701
702 void
703 g_signal_init (void) /* sync with gtype.c */
704 {
705   SIGNAL_LOCK ();
706   if (!g_n_signal_nodes)
707     {
708       /* handler_id_node_prepend() requires this */
709       g_assert (sizeof (GList) == sizeof (HandlerMatch));
710       
711       /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
712       g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
713       g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
714       
715       /* invalid (0) signal_id */
716       g_n_signal_nodes = 1;
717       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
718       g_signal_nodes[0] = NULL;
719     }
720   SIGNAL_UNLOCK ();
721 }
722
723 void
724 _g_signals_destroy (GType itype)
725 {
726   guint i;
727   
728   SIGNAL_LOCK ();
729   for (i = 1; i < g_n_signal_nodes; i++)
730     {
731       SignalNode *node = g_signal_nodes[i];
732       
733       if (node->itype == itype)
734         {
735           if (node->destroyed)
736             g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
737                        node->name,
738                        type_debug_name (node->itype));
739           else
740             signal_destroy_R (node);
741         }
742     }
743   SIGNAL_UNLOCK ();
744 }
745
746 void
747 g_signal_stop_emission (gpointer instance,
748                         guint    signal_id,
749                         GQuark   detail)
750 {
751   SignalNode *node;
752   
753   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
754   g_return_if_fail (signal_id > 0);
755   
756   SIGNAL_LOCK ();
757   node = LOOKUP_SIGNAL_NODE (signal_id);
758   if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
759     {
760       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
761       SIGNAL_UNLOCK ();
762       return;
763     }
764   if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
765     {
766       Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
767       Emission *emission = emission_find (emission_list, signal_id, detail, instance);
768       
769       if (emission)
770         {
771           if (emission->state == EMISSION_HOOK)
772             g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
773                        node->name, instance);
774           else if (emission->state == EMISSION_RUN)
775             emission->state = EMISSION_STOP;
776         }
777       else
778         g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
779                    node->name, instance);
780     }
781   else
782     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
783   SIGNAL_UNLOCK ();
784 }
785
786 static void
787 signal_finalize_hook (GHookList *hook_list,
788                       GHook     *hook)
789 {
790   GDestroyNotify destroy = hook->destroy;
791
792   if (destroy)
793     {
794       hook->destroy = NULL;
795       SIGNAL_UNLOCK ();
796       destroy (hook->data);
797       SIGNAL_LOCK ();
798     }
799 }
800
801 gulong
802 g_signal_add_emission_hook (guint               signal_id,
803                             GQuark              detail,
804                             GSignalEmissionHook hook_func,
805                             gpointer            hook_data,
806                             GDestroyNotify      data_destroy)
807 {
808   static gulong seq_hook_id = 1;
809   SignalNode *node;
810   GHook *hook;
811   SignalHook *signal_hook;
812
813   g_return_val_if_fail (signal_id > 0, 0);
814   g_return_val_if_fail (hook_func != NULL, 0);
815
816   SIGNAL_LOCK ();
817   node = LOOKUP_SIGNAL_NODE (signal_id);
818   if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
819     {
820       g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
821       SIGNAL_UNLOCK ();
822       return 0;
823     }
824   if (detail && !(node->flags & G_SIGNAL_DETAILED))
825     {
826       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
827       SIGNAL_UNLOCK ();
828       return 0;
829     }
830   if (!node->emission_hooks)
831     {
832       node->emission_hooks = g_new (GHookList, 1);
833       g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
834       node->emission_hooks->finalize_hook = signal_finalize_hook;
835     }
836   hook = g_hook_alloc (node->emission_hooks);
837   hook->data = hook_data;
838   hook->func = (gpointer) hook_func;
839   hook->destroy = data_destroy;
840   signal_hook = SIGNAL_HOOK (hook);
841   signal_hook->detail = detail;
842   node->emission_hooks->seq_id = seq_hook_id;
843   g_hook_append (node->emission_hooks, hook);
844   seq_hook_id = node->emission_hooks->seq_id;
845   SIGNAL_UNLOCK ();
846
847   return hook->hook_id;
848 }
849
850 void
851 g_signal_remove_emission_hook (guint  signal_id,
852                                gulong hook_id)
853 {
854   SignalNode *node;
855
856   g_return_if_fail (signal_id > 0);
857   g_return_if_fail (hook_id > 0);
858
859   SIGNAL_LOCK ();
860   node = LOOKUP_SIGNAL_NODE (signal_id);
861   if (!node || node->destroyed)
862     g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
863   else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
864     g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
865   SIGNAL_UNLOCK ();
866 }
867
868 static inline guint
869 signal_parse_name (const gchar *name,
870                    GType        itype,
871                    GQuark      *detail_p,
872                    gboolean     force_quark)
873 {
874   const gchar *colon = strchr (name, ':');
875   guint signal_id;
876   
877   if (!colon)
878     {
879       signal_id = signal_id_lookup (g_quark_try_string (name), itype);
880       if (signal_id && detail_p)
881         *detail_p = 0;
882     }
883   else if (colon[1] == ':')
884     {
885       gchar buffer[32];
886       guint l = colon - name;
887       
888       if (l < 32)
889         {
890           memcpy (buffer, name, l);
891           buffer[l] = 0;
892           signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
893         }
894       else
895         {
896           gchar *signal = g_new (gchar, l + 1);
897           
898           memcpy (signal, name, l);
899           signal[l] = 0;
900           signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
901           g_free (signal);
902         }
903       
904       if (signal_id && detail_p)
905         *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
906     }
907   else
908     signal_id = 0;
909   return signal_id;
910 }
911
912 gboolean
913 g_signal_parse_name (const gchar *detailed_signal,
914                      GType        itype,
915                      guint       *signal_id_p,
916                      GQuark      *detail_p,
917                      gboolean     force_detail_quark)
918 {
919   SignalNode *node;
920   GQuark detail = 0;
921   guint signal_id;
922   
923   g_return_val_if_fail (detailed_signal != NULL, FALSE);
924   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
925   
926   SIGNAL_LOCK ();
927   signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
928   SIGNAL_UNLOCK ();
929
930   node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
931   if (!node || node->destroyed ||
932       (detail && !(node->flags & G_SIGNAL_DETAILED)))
933     return FALSE;
934
935   if (signal_id_p)
936     *signal_id_p = signal_id;
937   if (detail_p)
938     *detail_p = detail;
939   
940   return TRUE;
941 }
942
943 void
944 g_signal_stop_emission_by_name (gpointer     instance,
945                                 const gchar *detailed_signal)
946 {
947   guint signal_id;
948   GQuark detail = 0;
949   GType itype;
950   
951   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
952   g_return_if_fail (detailed_signal != NULL);
953   
954   SIGNAL_LOCK ();
955   itype = G_TYPE_FROM_INSTANCE (instance);
956   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
957   if (signal_id)
958     {
959       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
960       
961       if (detail && !(node->flags & G_SIGNAL_DETAILED))
962         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
963       else if (!g_type_is_a (itype, node->itype))
964         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
965       else
966         {
967           Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
968           Emission *emission = emission_find (emission_list, signal_id, detail, instance);
969           
970           if (emission)
971             {
972               if (emission->state == EMISSION_HOOK)
973                 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
974                            node->name, instance);
975               else if (emission->state == EMISSION_RUN)
976                 emission->state = EMISSION_STOP;
977             }
978           else
979             g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
980                        node->name, instance);
981         }
982     }
983   else
984     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
985   SIGNAL_UNLOCK ();
986 }
987
988 guint
989 g_signal_lookup (const gchar *name,
990                  GType        itype)
991 {
992   guint signal_id;
993   g_return_val_if_fail (name != NULL, 0);
994   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
995   
996   SIGNAL_LOCK ();
997   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
998   SIGNAL_UNLOCK ();
999   if (!signal_id)
1000     {
1001       /* give elaborate warnings */
1002       if (!g_type_name (itype))
1003         g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1004                    name, itype);
1005       else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1006         g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1007                    name, g_type_name (itype));
1008       else if (!g_type_class_peek (itype))
1009         g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1010                    name, g_type_name (itype));
1011     }
1012   
1013   return signal_id;
1014 }
1015
1016 guint*
1017 g_signal_list_ids (GType  itype,
1018                    guint *n_ids)
1019 {
1020   SignalKey *keys;
1021   GArray *result;
1022   guint n_nodes;
1023   guint i;
1024   
1025   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1026   g_return_val_if_fail (n_ids != NULL, NULL);
1027   
1028   SIGNAL_LOCK ();
1029   keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1030   n_nodes = g_signal_key_bsa->n_nodes;
1031   result = g_array_new (FALSE, FALSE, sizeof (guint));
1032   
1033   for (i = 0; i < n_nodes; i++)
1034     if (keys[i].itype == itype)
1035       {
1036         const gchar *name = g_quark_to_string (keys[i].quark);
1037         
1038         /* Signal names with "_" in them are aliases to the same
1039          * name with "-" instead of "_".
1040          */
1041         if (!strchr (name, '_'))
1042           g_array_append_val (result, keys[i].signal_id);
1043       }
1044   *n_ids = result->len;
1045   SIGNAL_UNLOCK ();
1046   if (!n_nodes)
1047     {
1048       /* give elaborate warnings */
1049       if (!g_type_name (itype))
1050         g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1051                    itype);
1052       else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1053         g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1054                    g_type_name (itype));
1055       else if (!g_type_class_peek (itype))
1056         g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1057                    g_type_name (itype));
1058     }
1059   
1060   return (guint*) g_array_free (result, FALSE);
1061 }
1062
1063 G_CONST_RETURN gchar*
1064 g_signal_name (guint signal_id)
1065 {
1066   SignalNode *node;
1067   gchar *name;
1068   
1069   SIGNAL_LOCK ();
1070   node = LOOKUP_SIGNAL_NODE (signal_id);
1071   name = node ? node->name : NULL;
1072   SIGNAL_UNLOCK ();
1073   
1074   return name;
1075 }
1076
1077 void
1078 g_signal_query (guint         signal_id,
1079                 GSignalQuery *query)
1080 {
1081   SignalNode *node;
1082   
1083   g_return_if_fail (query != NULL);
1084   
1085   SIGNAL_LOCK ();
1086   node = LOOKUP_SIGNAL_NODE (signal_id);
1087   if (!node || node->destroyed)
1088     query->signal_id = 0;
1089   else
1090     {
1091       query->signal_id = node->signal_id;
1092       query->signal_name = node->name;
1093       query->itype = node->itype;
1094       query->signal_flags = node->flags;
1095       query->return_type = node->return_type;
1096       query->n_params = node->n_params;
1097       query->param_types = node->param_types;
1098     }
1099   SIGNAL_UNLOCK ();
1100 }
1101
1102 guint
1103 g_signal_new (const gchar        *signal_name,
1104               GType               itype,
1105               GSignalFlags        signal_flags,
1106               guint               class_offset,
1107               GSignalAccumulator  accumulator,
1108               gpointer            accu_data,
1109               GSignalCMarshaller  c_marshaller,
1110               GType               return_type,
1111               guint               n_params,
1112               ...)
1113 {
1114   va_list args;
1115   guint signal_id;
1116
1117   g_return_val_if_fail (signal_name != NULL, 0);
1118   
1119   va_start (args, n_params);
1120
1121   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1122                                    class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1123                                    accumulator, accu_data, c_marshaller,
1124                                    return_type, n_params, args);
1125
1126   va_end (args);
1127  
1128   return signal_id;
1129 }
1130
1131 static inline ClassClosure*
1132 signal_find_class_closure (SignalNode *node,
1133                            GType       itype)
1134 {
1135   GBSearchArray *bsa = node->class_closure_bsa;
1136   ClassClosure *cc;
1137
1138   if (bsa)
1139     {
1140       ClassClosure key;
1141
1142       /* cc->instance_type is 0 for default closure */
1143       
1144       key.instance_type = itype;
1145       cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1146       while (!cc && key.instance_type)
1147         {
1148           key.instance_type = g_type_parent (key.instance_type);
1149           cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1150         }
1151     }
1152   else
1153     cc = NULL;
1154   return cc;
1155 }
1156
1157 static inline GClosure*
1158 signal_lookup_closure (SignalNode    *node,
1159                        GTypeInstance *instance)
1160 {
1161   ClassClosure *cc;
1162
1163   if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1164     cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1165   else
1166     cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1167   return cc ? cc->closure : NULL;
1168 }
1169
1170 static void
1171 signal_add_class_closure (SignalNode *node,
1172                           GType       itype,
1173                           GClosure   *closure)
1174 {
1175   ClassClosure key;
1176
1177   if (!node->class_closure_bsa)
1178     node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1179   key.instance_type = itype;
1180   key.closure = g_closure_ref (closure);
1181   node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1182                                                     &g_class_closure_bconfig,
1183                                                     &key,
1184                                                     FALSE);
1185   g_closure_sink (closure);
1186   if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1187     g_closure_set_marshal (closure, node->c_marshaller);
1188 }
1189
1190 guint
1191 g_signal_newv (const gchar       *signal_name,
1192                GType              itype,
1193                GSignalFlags       signal_flags,
1194                GClosure          *class_closure,
1195                GSignalAccumulator accumulator,
1196                gpointer           accu_data,
1197                GSignalCMarshaller c_marshaller,
1198                GType              return_type,
1199                guint              n_params,
1200                GType             *param_types)
1201 {
1202   gchar *name;
1203   guint signal_id, i;
1204   SignalNode *node;
1205   
1206   g_return_val_if_fail (signal_name != NULL, 0);
1207   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1208   if (n_params)
1209     g_return_val_if_fail (param_types != NULL, 0);
1210   g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1211   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1212     g_return_val_if_fail (accumulator == NULL, 0);
1213   if (!accumulator)
1214     g_return_val_if_fail (accu_data == NULL, 0);
1215
1216   name = g_strdup (signal_name);
1217   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  /* FIXME do character checks like for types */
1218   
1219   SIGNAL_LOCK ();
1220   
1221   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1222   node = LOOKUP_SIGNAL_NODE (signal_id);
1223   if (node && !node->destroyed)
1224     {
1225       g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1226                  name,
1227                  type_debug_name (node->itype),
1228                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1229       g_free (name);
1230       SIGNAL_UNLOCK ();
1231       return 0;
1232     }
1233   if (node && node->itype != itype)
1234     {
1235       g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1236                  name,
1237                  type_debug_name (itype),
1238                  type_debug_name (node->itype));
1239       g_free (name);
1240       SIGNAL_UNLOCK ();
1241       return 0;
1242     }
1243   for (i = 0; i < n_params; i++)
1244     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1245       {
1246         g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1247                    i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1248         g_free (name);
1249         SIGNAL_UNLOCK ();
1250         return 0;
1251       }
1252   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1253     {
1254       g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1255                  type_debug_name (return_type), type_debug_name (itype), name);
1256       g_free (name);
1257       SIGNAL_UNLOCK ();
1258       return 0;
1259     }
1260   if (return_type != G_TYPE_NONE &&
1261       (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1262     {
1263       g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1264                  type_debug_name (itype), name, type_debug_name (return_type));
1265       g_free (name);
1266       SIGNAL_UNLOCK ();
1267       return 0;
1268     }
1269   
1270   /* setup permanent portion of signal node */
1271   if (!node)
1272     {
1273       SignalKey key;
1274       
1275       signal_id = g_n_signal_nodes++;
1276       node = g_new (SignalNode, 1);
1277       node->signal_id = signal_id;
1278       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1279       g_signal_nodes[signal_id] = node;
1280       node->itype = itype;
1281       node->name = name;
1282       key.itype = itype;
1283       key.quark = g_quark_from_string (node->name);
1284       key.signal_id = signal_id;
1285       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1286       g_strdelimit (node->name, "_", '-');
1287       key.quark = g_quark_from_static_string (node->name);
1288       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1289     }
1290   node->destroyed = FALSE;
1291   
1292   /* setup reinitializable portion */
1293   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1294   node->n_params = n_params;
1295   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1296   node->return_type = return_type;
1297   node->class_closure_bsa = NULL;
1298   if (accumulator)
1299     {
1300       node->accumulator = g_new (SignalAccumulator, 1);
1301       node->accumulator->func = accumulator;
1302       node->accumulator->data = accu_data;
1303     }
1304   else
1305     node->accumulator = NULL;
1306   node->c_marshaller = c_marshaller;
1307   node->emission_hooks = NULL;
1308   if (class_closure)
1309     signal_add_class_closure (node, 0, class_closure);
1310   SIGNAL_UNLOCK ();
1311
1312   return signal_id;
1313 }
1314
1315 guint
1316 g_signal_new_valist (const gchar       *signal_name,
1317                      GType              itype,
1318                      GSignalFlags       signal_flags,
1319                      GClosure          *class_closure,
1320                      GSignalAccumulator accumulator,
1321                      gpointer           accu_data,
1322                      GSignalCMarshaller c_marshaller,
1323                      GType              return_type,
1324                      guint              n_params,
1325                      va_list            args)
1326 {
1327   GType *param_types;
1328   guint i;
1329   guint signal_id;
1330
1331   if (n_params > 0)
1332     {
1333       param_types = g_new (GType, n_params);
1334
1335       for (i = 0; i < n_params; i++)
1336         param_types[i] = va_arg (args, GType);
1337     }
1338   else
1339     param_types = NULL;
1340
1341   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1342                              class_closure, accumulator, accu_data, c_marshaller,
1343                              return_type, n_params, param_types);
1344   g_free (param_types);
1345
1346   return signal_id;
1347 }
1348
1349 static void
1350 signal_destroy_R (SignalNode *signal_node)
1351 {
1352   SignalNode node = *signal_node;
1353
1354   signal_node->destroyed = TRUE;
1355   
1356   /* reentrancy caution, zero out real contents first */
1357   signal_node->n_params = 0;
1358   signal_node->param_types = NULL;
1359   signal_node->return_type = 0;
1360   signal_node->class_closure_bsa = NULL;
1361   signal_node->accumulator = NULL;
1362   signal_node->c_marshaller = NULL;
1363   signal_node->emission_hooks = NULL;
1364   
1365 #ifdef  G_ENABLE_DEBUG
1366   /* check current emissions */
1367   {
1368     Emission *emission;
1369     
1370     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1371          emission; emission = emission->next)
1372       if (emission->ihint.signal_id == node.signal_id)
1373         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1374                     node.name, emission->instance);
1375   }
1376 #endif
1377   
1378   /* free contents that need to
1379    */
1380   SIGNAL_UNLOCK ();
1381   g_free (node.param_types);
1382   if (node.class_closure_bsa)
1383     {
1384       guint i;
1385
1386       for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1387         {
1388           ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1389
1390           g_closure_unref (cc->closure);
1391         }
1392       g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1393     }
1394   g_free (node.accumulator);
1395   if (node.emission_hooks)
1396     {
1397       g_hook_list_clear (node.emission_hooks);
1398       g_free (node.emission_hooks);
1399     }
1400   SIGNAL_LOCK ();
1401 }
1402
1403 void
1404 g_signal_override_class_closure (guint     signal_id,
1405                                  GType     instance_type,
1406                                  GClosure *class_closure)
1407 {
1408   SignalNode *node;
1409   
1410   g_return_if_fail (signal_id > 0);
1411   g_return_if_fail (class_closure != NULL);
1412   
1413   SIGNAL_LOCK ();
1414   node = LOOKUP_SIGNAL_NODE (signal_id);
1415   if (!g_type_is_a (instance_type, node->itype))
1416     g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1417   else
1418     {
1419       ClassClosure *cc = signal_find_class_closure (node, instance_type);
1420       
1421       if (cc && cc->instance_type == instance_type)
1422         g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1423       else
1424         signal_add_class_closure (node, instance_type, class_closure);
1425     }
1426   SIGNAL_UNLOCK ();
1427 }
1428
1429 void
1430 g_signal_chain_from_overridden (const GValue *instance_and_params,
1431                                 GValue       *return_value)
1432 {
1433   GType chain_type = 0, restore_type = 0;
1434   Emission *emission = NULL;
1435   GClosure *closure = NULL;
1436   guint n_params = 0;
1437   gpointer instance;
1438   
1439   g_return_if_fail (instance_and_params != NULL);
1440   instance = g_value_peek_pointer (instance_and_params);
1441   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1442   
1443   SIGNAL_LOCK ();
1444   emission = emission_find_innermost (instance);
1445   if (emission)
1446     {
1447       SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1448       
1449       g_assert (node != NULL);  /* paranoid */
1450       
1451       /* we should probably do the same parameter checks as g_signal_emit() here.
1452        */
1453       if (emission->chain_type != G_TYPE_NONE)
1454         {
1455           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1456           
1457           g_assert (cc != NULL);        /* closure currently in call stack */
1458
1459           n_params = node->n_params;
1460           restore_type = cc->instance_type;
1461           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1462           if (cc && cc->instance_type != restore_type)
1463             {
1464               closure = cc->closure;
1465               chain_type = cc->instance_type;
1466             }
1467         }
1468       else
1469         g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1470     }
1471   else
1472     g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1473   if (closure)
1474     {
1475       emission->chain_type = chain_type;
1476       SIGNAL_UNLOCK ();
1477       g_closure_invoke (closure,
1478                         return_value,
1479                         n_params + 1,
1480                         instance_and_params,
1481                         &emission->ihint);
1482       SIGNAL_LOCK ();
1483       emission->chain_type = restore_type;
1484     }
1485   SIGNAL_UNLOCK ();
1486 }
1487
1488 GSignalInvocationHint*
1489 g_signal_get_invocation_hint (gpointer instance)
1490 {
1491   Emission *emission = NULL;
1492   
1493   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1494
1495   SIGNAL_LOCK ();
1496   emission = emission_find_innermost (instance);
1497   SIGNAL_UNLOCK ();
1498   
1499   return emission ? &emission->ihint : NULL;
1500 }
1501
1502 gulong
1503 g_signal_connect_closure_by_id (gpointer  instance,
1504                                 guint     signal_id,
1505                                 GQuark    detail,
1506                                 GClosure *closure,
1507                                 gboolean  after)
1508 {
1509   SignalNode *node;
1510   gulong handler_seq_no = 0;
1511   
1512   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1513   g_return_val_if_fail (signal_id > 0, 0);
1514   g_return_val_if_fail (closure != NULL, 0);
1515   
1516   SIGNAL_LOCK ();
1517   node = LOOKUP_SIGNAL_NODE (signal_id);
1518   if (node)
1519     {
1520       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1521         g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1522       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1523         g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1524       else
1525         {
1526           Handler *handler = handler_new (after);
1527           
1528           handler_seq_no = handler->sequential_number;
1529           handler->detail = detail;
1530           handler->closure = g_closure_ref (closure);
1531           g_closure_sink (closure);
1532           handler_insert (signal_id, instance, handler);
1533           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1534             g_closure_set_marshal (closure, node->c_marshaller);
1535         }
1536     }
1537   else
1538     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1539   SIGNAL_UNLOCK ();
1540   
1541   return handler_seq_no;
1542 }
1543
1544 gulong
1545 g_signal_connect_closure (gpointer     instance,
1546                           const gchar *detailed_signal,
1547                           GClosure    *closure,
1548                           gboolean     after)
1549 {
1550   guint signal_id;
1551   gulong handler_seq_no = 0;
1552   GQuark detail = 0;
1553   GType itype;
1554
1555   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1556   g_return_val_if_fail (detailed_signal != NULL, 0);
1557   g_return_val_if_fail (closure != NULL, 0);
1558
1559   SIGNAL_LOCK ();
1560   itype = G_TYPE_FROM_INSTANCE (instance);
1561   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1562   if (signal_id)
1563     {
1564       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1565
1566       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1567         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1568       else if (!g_type_is_a (itype, node->itype))
1569         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1570       else
1571         {
1572           Handler *handler = handler_new (after);
1573
1574           handler_seq_no = handler->sequential_number;
1575           handler->detail = detail;
1576           handler->closure = g_closure_ref (closure);
1577           g_closure_sink (closure);
1578           handler_insert (signal_id, instance, handler);
1579           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1580             g_closure_set_marshal (handler->closure, node->c_marshaller);
1581         }
1582     }
1583   else
1584     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1585   SIGNAL_UNLOCK ();
1586
1587   return handler_seq_no;
1588 }
1589
1590 gulong
1591 g_signal_connect_data (gpointer       instance,
1592                        const gchar   *detailed_signal,
1593                        GCallback      c_handler,
1594                        gpointer       data,
1595                        GClosureNotify destroy_data,
1596                        GConnectFlags  connect_flags)
1597 {
1598   guint signal_id;
1599   gulong handler_seq_no = 0;
1600   GQuark detail = 0;
1601   GType itype;
1602   gboolean swapped, after;
1603   
1604   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1605   g_return_val_if_fail (detailed_signal != NULL, 0);
1606   g_return_val_if_fail (c_handler != NULL, 0);
1607
1608   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1609   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1610
1611   SIGNAL_LOCK ();
1612   itype = G_TYPE_FROM_INSTANCE (instance);
1613   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1614   if (signal_id)
1615     {
1616       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1617
1618       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1619         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1620       else if (!g_type_is_a (itype, node->itype))
1621         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1622       else
1623         {
1624           Handler *handler = handler_new (after);
1625
1626           handler_seq_no = handler->sequential_number;
1627           handler->detail = detail;
1628           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1629           g_closure_sink (handler->closure);
1630           handler_insert (signal_id, instance, handler);
1631           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1632             g_closure_set_marshal (handler->closure, node->c_marshaller);
1633         }
1634     }
1635   else
1636     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1637   SIGNAL_UNLOCK ();
1638
1639   return handler_seq_no;
1640 }
1641
1642 void
1643 g_signal_handler_block (gpointer instance,
1644                         gulong   handler_id)
1645 {
1646   Handler *handler;
1647   
1648   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1649   g_return_if_fail (handler_id > 0);
1650   
1651   SIGNAL_LOCK ();
1652   handler = handler_lookup (instance, handler_id, NULL);
1653   if (handler)
1654     {
1655 #ifndef G_DISABLE_CHECKS
1656       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1657         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1658 #endif
1659       handler->block_count += 1;
1660     }
1661   else
1662     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1663   SIGNAL_UNLOCK ();
1664 }
1665
1666 void
1667 g_signal_handler_unblock (gpointer instance,
1668                           gulong   handler_id)
1669 {
1670   Handler *handler;
1671   
1672   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1673   g_return_if_fail (handler_id > 0);
1674   
1675   SIGNAL_LOCK ();
1676   handler = handler_lookup (instance, handler_id, NULL);
1677   if (handler)
1678     {
1679       if (handler->block_count)
1680         handler->block_count -= 1;
1681       else
1682         g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1683     }
1684   else
1685     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1686   SIGNAL_UNLOCK ();
1687 }
1688
1689 void
1690 g_signal_handler_disconnect (gpointer instance,
1691                              gulong   handler_id)
1692 {
1693   Handler *handler;
1694   guint signal_id;
1695   
1696   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1697   g_return_if_fail (handler_id > 0);
1698   
1699   SIGNAL_LOCK ();
1700   handler = handler_lookup (instance, handler_id, &signal_id);
1701   if (handler)
1702     {
1703       handler->sequential_number = 0;
1704       handler->block_count = 1;
1705       handler_unref_R (signal_id, instance, handler);
1706     }
1707   else
1708     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1709   SIGNAL_UNLOCK ();
1710 }
1711
1712 gboolean
1713 g_signal_handler_is_connected (gpointer instance,
1714                                gulong   handler_id)
1715 {
1716   Handler *handler;
1717   gboolean connected;
1718
1719   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1720   g_return_val_if_fail (handler_id > 0, FALSE);
1721
1722   SIGNAL_LOCK ();
1723   handler = handler_lookup (instance, handler_id, NULL);
1724   connected = handler != NULL;
1725   SIGNAL_UNLOCK ();
1726
1727   return connected;
1728 }
1729
1730 void
1731 g_signal_handlers_destroy (gpointer instance)
1732 {
1733   GBSearchArray *hlbsa;
1734   
1735   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1736   
1737   SIGNAL_LOCK ();
1738   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1739   if (hlbsa)
1740     {
1741       guint i;
1742       
1743       /* reentrancy caution, delete instance trace first */
1744       g_hash_table_remove (g_handler_list_bsa_ht, instance);
1745       
1746       for (i = 0; i < hlbsa->n_nodes; i++)
1747         {
1748           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1749           Handler *handler = hlist->handlers;
1750           
1751           while (handler)
1752             {
1753               Handler *tmp = handler;
1754               
1755               handler = tmp->next;
1756               tmp->block_count = 1;
1757               /* cruel unlink, this works because _all_ handlers vanish */
1758               tmp->next = NULL;
1759               tmp->prev = tmp;
1760               if (tmp->sequential_number)
1761                 {
1762                   tmp->sequential_number = 0;
1763                   handler_unref_R (0, NULL, tmp);
1764                 }
1765             }
1766         }
1767       g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1768     }
1769   SIGNAL_UNLOCK ();
1770 }
1771
1772 gulong
1773 g_signal_handler_find (gpointer         instance,
1774                        GSignalMatchType mask,
1775                        guint            signal_id,
1776                        GQuark           detail,
1777                        GClosure        *closure,
1778                        gpointer         func,
1779                        gpointer         data)
1780 {
1781   gulong handler_seq_no = 0;
1782   
1783   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1784   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1785   
1786   if (mask & G_SIGNAL_MATCH_MASK)
1787     {
1788       HandlerMatch *mlist;
1789       
1790       SIGNAL_LOCK ();
1791       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1792       if (mlist)
1793         {
1794           handler_seq_no = mlist->handler->sequential_number;
1795           handler_match_free1_R (mlist, instance);
1796         }
1797       SIGNAL_UNLOCK ();
1798     }
1799   
1800   return handler_seq_no;
1801 }
1802
1803 static guint
1804 signal_handlers_foreach_matched_R (gpointer         instance,
1805                                    GSignalMatchType mask,
1806                                    guint            signal_id,
1807                                    GQuark           detail,
1808                                    GClosure        *closure,
1809                                    gpointer         func,
1810                                    gpointer         data,
1811                                    void           (*callback) (gpointer instance,
1812                                                                gulong   handler_seq_no))
1813 {
1814   HandlerMatch *mlist;
1815   guint n_handlers = 0;
1816   
1817   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1818   while (mlist)
1819     {
1820       n_handlers++;
1821       if (mlist->handler->sequential_number)
1822         {
1823           SIGNAL_UNLOCK ();
1824           callback (instance, mlist->handler->sequential_number);
1825           SIGNAL_LOCK ();
1826         }
1827       mlist = handler_match_free1_R (mlist, instance);
1828     }
1829   
1830   return n_handlers;
1831 }
1832
1833 guint
1834 g_signal_handlers_block_matched (gpointer         instance,
1835                                  GSignalMatchType mask,
1836                                  guint            signal_id,
1837                                  GQuark           detail,
1838                                  GClosure        *closure,
1839                                  gpointer         func,
1840                                  gpointer         data)
1841 {
1842   guint n_handlers = 0;
1843   
1844   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1845   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1846   
1847   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1848     {
1849       SIGNAL_LOCK ();
1850       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1851                                                       closure, func, data,
1852                                                       g_signal_handler_block);
1853       SIGNAL_UNLOCK ();
1854     }
1855   
1856   return n_handlers;
1857 }
1858
1859 guint
1860 g_signal_handlers_unblock_matched (gpointer         instance,
1861                                    GSignalMatchType mask,
1862                                    guint            signal_id,
1863                                    GQuark           detail,
1864                                    GClosure        *closure,
1865                                    gpointer         func,
1866                                    gpointer         data)
1867 {
1868   guint n_handlers = 0;
1869   
1870   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1871   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1872   
1873   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1874     {
1875       SIGNAL_LOCK ();
1876       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1877                                                       closure, func, data,
1878                                                       g_signal_handler_unblock);
1879       SIGNAL_UNLOCK ();
1880     }
1881   
1882   return n_handlers;
1883 }
1884
1885 guint
1886 g_signal_handlers_disconnect_matched (gpointer         instance,
1887                                       GSignalMatchType mask,
1888                                       guint            signal_id,
1889                                       GQuark           detail,
1890                                       GClosure        *closure,
1891                                       gpointer         func,
1892                                       gpointer         data)
1893 {
1894   guint n_handlers = 0;
1895   
1896   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1897   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1898   
1899   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1900     {
1901       SIGNAL_LOCK ();
1902       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1903                                                       closure, func, data,
1904                                                       g_signal_handler_disconnect);
1905       SIGNAL_UNLOCK ();
1906     }
1907   
1908   return n_handlers;
1909 }
1910
1911 gboolean
1912 g_signal_has_handler_pending (gpointer instance,
1913                               guint    signal_id,
1914                               GQuark   detail,
1915                               gboolean may_be_blocked)
1916 {
1917   HandlerMatch *mlist;
1918   gboolean has_pending;
1919   
1920   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1921   g_return_val_if_fail (signal_id > 0, FALSE);
1922   
1923   SIGNAL_LOCK ();
1924   if (detail)
1925     {
1926       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1927       
1928       if (!(node->flags & G_SIGNAL_DETAILED))
1929         {
1930           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1931           SIGNAL_UNLOCK ();
1932           return FALSE;
1933         }
1934     }
1935   mlist = handlers_find (instance,
1936                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1937                          signal_id, detail, NULL, NULL, NULL, TRUE);
1938   if (mlist)
1939     {
1940       has_pending = TRUE;
1941       handler_match_free1_R (mlist, instance);
1942     }
1943   else
1944     has_pending = FALSE;
1945   SIGNAL_UNLOCK ();
1946   
1947   return has_pending;
1948 }
1949
1950 void
1951 g_signal_emitv (const GValue *instance_and_params,
1952                 guint         signal_id,
1953                 GQuark        detail,
1954                 GValue       *return_value)
1955 {
1956   const GValue *param_values;
1957   gpointer instance;
1958   SignalNode *node;
1959 #ifdef G_ENABLE_DEBUG
1960   guint i;
1961 #endif
1962   
1963   g_return_if_fail (instance_and_params != NULL);
1964   instance = g_value_peek_pointer (instance_and_params);
1965   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1966   g_return_if_fail (signal_id > 0);
1967
1968   param_values = instance_and_params + 1;
1969   
1970   SIGNAL_LOCK ();
1971   node = LOOKUP_SIGNAL_NODE (signal_id);
1972   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1973     {
1974       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1975       SIGNAL_UNLOCK ();
1976       return;
1977     }
1978 #ifdef G_ENABLE_DEBUG
1979   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1980     {
1981       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1982       SIGNAL_UNLOCK ();
1983       return;
1984     }
1985   for (i = 0; i < node->n_params; i++)
1986     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1987       {
1988         g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1989                     G_STRLOC,
1990                     type_debug_name (node->param_types[i]),
1991                     i,
1992                     node->name,
1993                     G_VALUE_TYPE_NAME (param_values + i));
1994         SIGNAL_UNLOCK ();
1995         return;
1996       }
1997   if (node->return_type != G_TYPE_NONE)
1998     {
1999       if (!return_value)
2000         {
2001           g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2002                       G_STRLOC,
2003                       type_debug_name (node->return_type),
2004                       node->name);
2005           SIGNAL_UNLOCK ();
2006           return;
2007         }
2008       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2009         {
2010           g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2011                       G_STRLOC,
2012                       type_debug_name (node->return_type),
2013                       node->name,
2014                       G_VALUE_TYPE_NAME (return_value));
2015           SIGNAL_UNLOCK ();
2016           return;
2017         }
2018     }
2019   else
2020     return_value = NULL;
2021 #endif  /* G_ENABLE_DEBUG */
2022
2023   SIGNAL_UNLOCK ();
2024   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2025 }
2026
2027 void
2028 g_signal_emit_valist (gpointer instance,
2029                       guint    signal_id,
2030                       GQuark   detail,
2031                       va_list  var_args)
2032 {
2033   GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2034   GType signal_return_type;
2035   GValue *param_values;
2036   SignalNode *node;
2037   guint i, n_params;
2038   
2039   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2040   g_return_if_fail (signal_id > 0);
2041
2042   SIGNAL_LOCK ();
2043   node = LOOKUP_SIGNAL_NODE (signal_id);
2044   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2045     {
2046       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2047       SIGNAL_UNLOCK ();
2048       return;
2049     }
2050 #ifndef G_DISABLE_CHECKS
2051   if (detail && !(node->flags & G_SIGNAL_DETAILED))
2052     {
2053       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2054       SIGNAL_UNLOCK ();
2055       return;
2056     }
2057 #endif  /* !G_DISABLE_CHECKS */
2058
2059   n_params = node->n_params;
2060   signal_return_type = node->return_type;
2061   if (node->n_params < MAX_STACK_VALUES)
2062     instance_and_params = stack_values;
2063   else
2064     {
2065       free_me = g_new (GValue, node->n_params + 1);
2066       instance_and_params = free_me;
2067     }
2068   param_values = instance_and_params + 1;
2069   for (i = 0; i < node->n_params; i++)
2070     {
2071       gchar *error;
2072       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2073       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2074       
2075       param_values[i].g_type = 0;
2076       SIGNAL_UNLOCK ();
2077       g_value_init (param_values + i, ptype);
2078       G_VALUE_COLLECT (param_values + i,
2079                        var_args,
2080                        static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2081                        &error);
2082       if (error)
2083         {
2084           g_warning ("%s: %s", G_STRLOC, error);
2085           g_free (error);
2086
2087           /* we purposely leak the value here, it might not be
2088            * in a sane state if an error condition occoured
2089            */
2090           while (i--)
2091             g_value_unset (param_values + i);
2092
2093           g_free (free_me);
2094           return;
2095         }
2096       SIGNAL_LOCK ();
2097     }
2098   SIGNAL_UNLOCK ();
2099   instance_and_params->g_type = 0;
2100   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2101   g_value_set_instance (instance_and_params, instance);
2102   if (signal_return_type == G_TYPE_NONE)
2103     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2104   else
2105     {
2106       GValue return_value = { 0, };
2107       gchar *error = NULL;
2108       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2109       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2110       
2111       g_value_init (&return_value, rtype);
2112
2113       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2114
2115       G_VALUE_LCOPY (&return_value,
2116                      var_args,
2117                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2118                      &error);
2119       if (!error)
2120         g_value_unset (&return_value);
2121       else
2122         {
2123           g_warning ("%s: %s", G_STRLOC, error);
2124           g_free (error);
2125           
2126           /* we purposely leak the value here, it might not be
2127            * in a sane state if an error condition occoured
2128            */
2129         }
2130     }
2131   for (i = 0; i < n_params; i++)
2132     g_value_unset (param_values + i);
2133   g_value_unset (instance_and_params);
2134   if (free_me)
2135     g_free (free_me);
2136 }
2137
2138 void
2139 g_signal_emit (gpointer instance,
2140                guint    signal_id,
2141                GQuark   detail,
2142                ...)
2143 {
2144   va_list var_args;
2145
2146   va_start (var_args, detail);
2147   g_signal_emit_valist (instance, signal_id, detail, var_args);
2148   va_end (var_args);
2149 }
2150
2151 void
2152 g_signal_emit_by_name (gpointer     instance,
2153                        const gchar *detailed_signal,
2154                        ...)
2155 {
2156   GQuark detail = 0;
2157   guint signal_id;
2158
2159   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2160   g_return_if_fail (detailed_signal != NULL);
2161
2162   SIGNAL_LOCK ();
2163   signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2164   SIGNAL_UNLOCK ();
2165
2166   if (signal_id)
2167     {
2168       va_list var_args;
2169
2170       va_start (var_args, detailed_signal);
2171       g_signal_emit_valist (instance, signal_id, detail, var_args);
2172       va_end (var_args);
2173     }
2174   else
2175     g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2176 }
2177
2178 static inline gboolean
2179 accumulate (GSignalInvocationHint *ihint,
2180             GValue                *return_accu,
2181             GValue                *handler_return,
2182             SignalAccumulator     *accumulator)
2183 {
2184   gboolean continue_emission;
2185
2186   if (!accumulator)
2187     return TRUE;
2188
2189   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2190   g_value_reset (handler_return);
2191
2192   return continue_emission;
2193 }
2194
2195 static gboolean
2196 signal_emit_unlocked_R (SignalNode   *node,
2197                         GQuark        detail,
2198                         gpointer      instance,
2199                         GValue       *emission_return,
2200                         const GValue *instance_and_params)
2201 {
2202   SignalAccumulator *accumulator;
2203   Emission emission;
2204   GClosure *class_closure;
2205   HandlerList *hlist;
2206   Handler *handler_list = NULL;
2207   GValue *return_accu, accu = { 0, };
2208   guint signal_id;
2209   gulong max_sequential_handler_number;
2210   gboolean return_value_altered = FALSE;
2211   
2212 #ifdef  G_ENABLE_DEBUG
2213   IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2214     {
2215       g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2216                  g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2217                  node->name, detail,
2218                  instance, node);
2219       if (g_trap_instance_signals == instance)
2220         G_BREAKPOINT ();
2221     }
2222 #endif  /* G_ENABLE_DEBUG */
2223   
2224   SIGNAL_LOCK ();
2225   signal_id = node->signal_id;
2226   if (node->flags & G_SIGNAL_NO_RECURSE)
2227     {
2228       Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2229       
2230       if (node)
2231         {
2232           node->state = EMISSION_RESTART;
2233           SIGNAL_UNLOCK ();
2234           return return_value_altered;
2235         }
2236     }
2237   accumulator = node->accumulator;
2238   if (accumulator)
2239     {
2240       SIGNAL_UNLOCK ();
2241       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2242       return_accu = &accu;
2243       SIGNAL_LOCK ();
2244     }
2245   else
2246     return_accu = emission_return;
2247   emission.instance = instance;
2248   emission.ihint.signal_id = node->signal_id;
2249   emission.ihint.detail = detail;
2250   emission.ihint.run_type = 0;
2251   emission.state = 0;
2252   emission.chain_type = G_TYPE_NONE;
2253   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2254   class_closure = signal_lookup_closure (node, instance);
2255   
2256  EMIT_RESTART:
2257   
2258   if (handler_list)
2259     handler_unref_R (signal_id, instance, handler_list);
2260   max_sequential_handler_number = g_handler_sequential_number;
2261   hlist = handler_list_lookup (signal_id, instance);
2262   handler_list = hlist ? hlist->handlers : NULL;
2263   if (handler_list)
2264     handler_ref (handler_list);
2265   
2266   emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2267   
2268   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2269     {
2270       emission.state = EMISSION_RUN;
2271
2272       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2273       SIGNAL_UNLOCK ();
2274       g_closure_invoke (class_closure,
2275                         return_accu,
2276                         node->n_params + 1,
2277                         instance_and_params,
2278                         &emission.ihint);
2279       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2280           emission.state == EMISSION_RUN)
2281         emission.state = EMISSION_STOP;
2282       SIGNAL_LOCK ();
2283       emission.chain_type = G_TYPE_NONE;
2284       return_value_altered = TRUE;
2285       
2286       if (emission.state == EMISSION_STOP)
2287         goto EMIT_CLEANUP;
2288       else if (emission.state == EMISSION_RESTART)
2289         goto EMIT_RESTART;
2290     }
2291   
2292   if (node->emission_hooks)
2293     {
2294       gboolean need_destroy, was_in_call, may_recurse = TRUE;
2295       GHook *hook;
2296
2297       emission.state = EMISSION_HOOK;
2298       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2299       while (hook)
2300         {
2301           SignalHook *signal_hook = SIGNAL_HOOK (hook);
2302           
2303           if (!signal_hook->detail || signal_hook->detail == detail)
2304             {
2305               GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2306               
2307               was_in_call = G_HOOK_IN_CALL (hook);
2308               hook->flags |= G_HOOK_FLAG_IN_CALL;
2309               SIGNAL_UNLOCK ();
2310               need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2311               SIGNAL_LOCK ();
2312               if (!was_in_call)
2313                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2314               if (need_destroy)
2315                 g_hook_destroy_link (node->emission_hooks, hook);
2316             }
2317           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2318         }
2319       
2320       if (emission.state == EMISSION_RESTART)
2321         goto EMIT_RESTART;
2322     }
2323   
2324   if (handler_list)
2325     {
2326       Handler *handler = handler_list;
2327       
2328       emission.state = EMISSION_RUN;
2329       handler_ref (handler);
2330       do
2331         {
2332           Handler *tmp;
2333           
2334           if (handler->after)
2335             {
2336               handler_unref_R (signal_id, instance, handler_list);
2337               handler_list = handler;
2338               break;
2339             }
2340           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2341                    handler->sequential_number < max_sequential_handler_number)
2342             {
2343               SIGNAL_UNLOCK ();
2344               g_closure_invoke (handler->closure,
2345                                 return_accu,
2346                                 node->n_params + 1,
2347                                 instance_and_params,
2348                                 &emission.ihint);
2349               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2350                   emission.state == EMISSION_RUN)
2351                 emission.state = EMISSION_STOP;
2352               SIGNAL_LOCK ();
2353               return_value_altered = TRUE;
2354               
2355               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2356             }
2357           else
2358             tmp = handler->next;
2359           
2360           if (tmp)
2361             handler_ref (tmp);
2362           handler_unref_R (signal_id, instance, handler_list);
2363           handler_list = handler;
2364           handler = tmp;
2365         }
2366       while (handler);
2367       
2368       if (emission.state == EMISSION_STOP)
2369         goto EMIT_CLEANUP;
2370       else if (emission.state == EMISSION_RESTART)
2371         goto EMIT_RESTART;
2372     }
2373   
2374   emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2375   
2376   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2377     {
2378       emission.state = EMISSION_RUN;
2379       
2380       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2381       SIGNAL_UNLOCK ();
2382       g_closure_invoke (class_closure,
2383                         return_accu,
2384                         node->n_params + 1,
2385                         instance_and_params,
2386                         &emission.ihint);
2387       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2388           emission.state == EMISSION_RUN)
2389         emission.state = EMISSION_STOP;
2390       SIGNAL_LOCK ();
2391       emission.chain_type = G_TYPE_NONE;
2392       return_value_altered = TRUE;
2393       
2394       if (emission.state == EMISSION_STOP)
2395         goto EMIT_CLEANUP;
2396       else if (emission.state == EMISSION_RESTART)
2397         goto EMIT_RESTART;
2398     }
2399   
2400   if (handler_list)
2401     {
2402       Handler *handler = handler_list;
2403       
2404       emission.state = EMISSION_RUN;
2405       handler_ref (handler);
2406       do
2407         {
2408           Handler *tmp;
2409           
2410           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2411               handler->sequential_number < max_sequential_handler_number)
2412             {
2413               SIGNAL_UNLOCK ();
2414               g_closure_invoke (handler->closure,
2415                                 return_accu,
2416                                 node->n_params + 1,
2417                                 instance_and_params,
2418                                 &emission.ihint);
2419               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2420                   emission.state == EMISSION_RUN)
2421                 emission.state = EMISSION_STOP;
2422               SIGNAL_LOCK ();
2423               return_value_altered = TRUE;
2424               
2425               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2426             }
2427           else
2428             tmp = handler->next;
2429           
2430           if (tmp)
2431             handler_ref (tmp);
2432           handler_unref_R (signal_id, instance, handler);
2433           handler = tmp;
2434         }
2435       while (handler);
2436       
2437       if (emission.state == EMISSION_STOP)
2438         goto EMIT_CLEANUP;
2439       else if (emission.state == EMISSION_RESTART)
2440         goto EMIT_RESTART;
2441     }
2442   
2443  EMIT_CLEANUP:
2444   
2445   emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2446   
2447   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2448     {
2449       gboolean need_unset = FALSE;
2450       
2451       emission.state = EMISSION_STOP;
2452       
2453       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2454       SIGNAL_UNLOCK ();
2455       if (node->return_type != G_TYPE_NONE && !accumulator)
2456         {
2457           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2458           need_unset = TRUE;
2459         }
2460       g_closure_invoke (class_closure,
2461                         node->return_type != G_TYPE_NONE ? &accu : NULL,
2462                         node->n_params + 1,
2463                         instance_and_params,
2464                         &emission.ihint);
2465       if (need_unset)
2466         g_value_unset (&accu);
2467       SIGNAL_LOCK ();
2468       emission.chain_type = G_TYPE_NONE;
2469       
2470       if (emission.state == EMISSION_RESTART)
2471         goto EMIT_RESTART;
2472     }
2473   
2474   if (handler_list)
2475     handler_unref_R (signal_id, instance, handler_list);
2476   
2477   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2478   SIGNAL_UNLOCK ();
2479   if (accumulator)
2480     g_value_unset (&accu);
2481   
2482   return return_value_altered;
2483 }
2484
2485 static const gchar*
2486 type_debug_name (GType type)
2487 {
2488   if (type)
2489     {
2490       const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2491       return name ? name : "<unknown>";
2492     }
2493   else
2494     return "<invalid>";
2495 }
2496
2497 /* --- compile standard marshallers --- */
2498 #include        "gobject.h"
2499 #include        "genums.h"
2500 #include        "gmarshal.c"