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