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