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