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