gkdbus: Fix underflow and unreachable code bug
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 <string.h>
30 #include <signal.h>
31
32 #include "gsignal.h"
33 #include "gtype-private.h"
34 #include "gbsearcharray.h"
35 #include "gvaluecollector.h"
36 #include "gvaluetypes.h"
37 #include "gobject.h"
38 #include "genums.h"
39 #include "gobject_trace.h"
40
41
42 /**
43  * SECTION:signals
44  * @short_description: A means for customization of object behaviour
45  *     and a general purpose notification mechanism
46  * @title: Signals
47  *
48  * The basic concept of the signal system is that of the emission
49  * of a signal. Signals are introduced per-type and are identified
50  * through strings. Signals introduced for a parent type are available
51  * in derived types as well, so basically they are a per-type facility
52  * that is inherited.
53  *
54  * A signal emission mainly involves invocation of a certain set of
55  * callbacks in precisely defined manner. There are two main categories
56  * of such callbacks, per-object ones and user provided ones.
57  * (Although signals can deal with any kind of instantiatable type, I'm
58  * referring to those types as "object types" in the following, simply
59  * because that is the context most users will encounter signals in.)
60  * The per-object callbacks are most often referred to as "object method
61  * handler" or "default (signal) handler", while user provided callbacks are
62  * usually just called "signal handler".
63  *
64  * The object method handler is provided at signal creation time (this most
65  * frequently happens at the end of an object class' creation), while user
66  * provided handlers are frequently connected and disconnected to/from a
67  * certain signal on certain object instances.
68  *
69  * A signal emission consists of five stages, unless prematurely stopped:
70  *
71  * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
72  *
73  * 2. Invocation of normal user-provided signal handlers (where the @after
74  *    flag is not set)
75  *
76  * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
77  *
78  * 4. Invocation of user provided signal handlers (where the @after flag is set)
79  *
80  * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
81  *
82  * The user-provided signal handlers are called in the order they were
83  * connected in.
84  *
85  * All handlers may prematurely stop a signal emission, and any number of
86  * handlers may be connected, disconnected, blocked or unblocked during
87  * a signal emission.
88  *
89  * There are certain criteria for skipping user handlers in stages 2 and 4
90  * of a signal emission.
91  *
92  * First, user handlers may be blocked. Blocked handlers are omitted during
93  * callback invocation, to return from the blocked state, a handler has to
94  * get unblocked exactly the same amount of times it has been blocked before.
95  *
96  * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
97  * @detail argument passed in to g_signal_emit() has to match the detail
98  * argument of the signal handler currently subject to invocation.
99  * Specification of no detail argument for signal handlers (omission of the
100  * detail part of the signal specification upon connection) serves as a
101  * wildcard and matches any detail argument passed in to emission.
102  *
103  * While the @detail argument is typically used to pass an object property name
104  * (as with #GObject::notify), no specific format is mandated for the detail
105  * string, other than that it must be non-empty.
106  *
107  * ## Memory management of signal handlers # {#signal-memory-management}
108  *
109  * If you are connecting handlers to signals and using a #GObject instance as
110  * your signal handler user data, you should remember to pair calls to
111  * g_signal_connect() with calls to g_signal_handler_disconnect() or
112  * g_signal_handlers_disconnect_by_func(). While signal handlers are
113  * automatically disconnected when the object emitting the signal is finalised,
114  * they are not automatically disconnected when the signal handler user data is
115  * destroyed. If this user data is a #GObject instance, using it from a
116  * signal handler after it has been finalised is an error.
117  *
118  * There are two strategies for managing such user data. The first is to
119  * disconnect the signal handler (using g_signal_handler_disconnect() or
120  * g_signal_handlers_disconnect_by_func()) when the user data (object) is
121  * finalised; this has to be implemented manually. For non-threaded programs,
122  * g_signal_connect_object() can be used to implement this automatically.
123  * Currently, however, it is unsafe to use in threaded programs.
124  *
125  * The second is to hold a strong reference on the user data until after the
126  * signal is disconnected for other reasons. This can be implemented
127  * automatically using g_signal_connect_data().
128  *
129  * The first approach is recommended, as the second approach can result in
130  * effective memory leaks of the user data if the signal handler is never
131  * disconnected for some reason.
132  */
133
134
135 #define REPORT_BUG      "please report occurrence circumstances to https://gitlab.gnome.org/GNOME/glib/issues/new"
136
137 /* --- typedefs --- */
138 typedef struct _SignalNode   SignalNode;
139 typedef struct _SignalKey    SignalKey;
140 typedef struct _Emission     Emission;
141 typedef struct _Handler      Handler;
142 typedef struct _HandlerList  HandlerList;
143 typedef struct _HandlerMatch HandlerMatch;
144 typedef enum
145 {
146   EMISSION_STOP,
147   EMISSION_RUN,
148   EMISSION_HOOK,
149   EMISSION_RESTART
150 } EmissionState;
151
152
153 /* --- prototypes --- */
154 static inline guint   signal_id_lookup  (const gchar *name,
155                                          GType        itype);
156 static        void              signal_destroy_R        (SignalNode      *signal_node);
157 static inline HandlerList*      handler_list_ensure     (guint            signal_id,
158                                                          gpointer         instance);
159 static inline HandlerList*      handler_list_lookup     (guint            signal_id,
160                                                          gpointer         instance);
161 static inline Handler*          handler_new             (guint            signal_id,
162                                                          gpointer         instance,
163                                                          gboolean         after);
164 static        void              handler_insert          (guint            signal_id,
165                                                          gpointer         instance,
166                                                          Handler         *handler);
167 static        Handler*          handler_lookup          (gpointer         instance,
168                                                          gulong           handler_id,
169                                                          GClosure        *closure,
170                                                          guint           *signal_id_p);
171 static inline HandlerMatch*     handler_match_prepend   (HandlerMatch    *list,
172                                                          Handler         *handler,
173                                                          guint            signal_id);
174 static inline HandlerMatch*     handler_match_free1_R   (HandlerMatch    *node,
175                                                          gpointer         instance);
176 static        HandlerMatch*     handlers_find           (gpointer         instance,
177                                                          GSignalMatchType mask,
178                                                          guint            signal_id,
179                                                          GQuark           detail,
180                                                          GClosure        *closure,
181                                                          gpointer         func,
182                                                          gpointer         data,
183                                                          gboolean         one_and_only);
184 static inline void              handler_ref             (Handler         *handler);
185 static inline void              handler_unref_R         (guint            signal_id,
186                                                          gpointer         instance,
187                                                          Handler         *handler);
188 static gint                     handler_lists_cmp       (gconstpointer    node1,
189                                                          gconstpointer    node2);
190 static inline void              emission_push           (Emission        *emission);
191 static inline void              emission_pop            (Emission        *emission);
192 static inline Emission*         emission_find           (guint            signal_id,
193                                                          GQuark           detail,
194                                                          gpointer         instance);
195 static gint                     class_closures_cmp      (gconstpointer    node1,
196                                                          gconstpointer    node2);
197 static gint                     signal_key_cmp          (gconstpointer    node1,
198                                                          gconstpointer    node2);
199 static        gboolean          signal_emit_unlocked_R  (SignalNode      *node,
200                                                          GQuark           detail,
201                                                          gpointer         instance,
202                                                          GValue          *return_value,
203                                                          const GValue    *instance_and_params);
204 static       void               add_invalid_closure_notify    (Handler         *handler,
205                                                                gpointer         instance);
206 static       void               remove_invalid_closure_notify (Handler         *handler,
207                                                                gpointer         instance);
208 static       void               invalid_closure_notify  (gpointer         data,
209                                                          GClosure        *closure);
210 static const gchar *            type_debug_name         (GType            type);
211 static void                     node_check_deprecated   (const SignalNode *node);
212 static void                     node_update_single_va_closure (SignalNode *node);
213
214
215 /* --- structures --- */
216 typedef struct
217 {
218   GSignalAccumulator func;
219   gpointer           data;
220 } SignalAccumulator;
221 typedef struct
222 {
223   GHook hook;
224   GQuark detail;
225 } SignalHook;
226 #define SIGNAL_HOOK(hook)       ((SignalHook*) (hook))
227
228 struct _SignalNode
229 {
230   /* permanent portion */
231   guint              signal_id;
232   GType              itype;
233   const gchar       *name;
234   guint              destroyed : 1;
235   
236   /* reinitializable portion */
237   guint              flags : 9;
238   guint              n_params : 8;
239   guint              single_va_closure_is_valid : 1;
240   guint              single_va_closure_is_after : 1;
241   GType             *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
242   GType              return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
243   GBSearchArray     *class_closure_bsa;
244   SignalAccumulator *accumulator;
245   GSignalCMarshaller c_marshaller;
246   GSignalCVaMarshaller va_marshaller;
247   GHookList         *emission_hooks;
248
249   GClosure *single_va_closure;
250 };
251
252 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1)        /* indicates single_va_closure is valid but empty */
253
254 struct _SignalKey
255 {
256   GType  itype;
257   GQuark quark;
258   guint  signal_id;
259 };
260
261 struct _Emission
262 {
263   Emission             *next;
264   gpointer              instance;
265   GSignalInvocationHint ihint;
266   EmissionState         state;
267   GType                 chain_type;
268 };
269
270 struct _HandlerList
271 {
272   guint    signal_id;
273   Handler *handlers;
274   Handler *tail_before;  /* normal signal handlers are appended here  */
275   Handler *tail_after;   /* CONNECT_AFTER handlers are appended here  */
276 };
277
278 struct _Handler
279 {
280   gulong        sequential_number;
281   Handler      *next;
282   Handler      *prev;
283   GQuark        detail;
284   guint         signal_id;
285   guint         ref_count;
286   guint         block_count : 16;
287 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
288   guint         after : 1;
289   guint         has_invalid_closure_notify : 1;
290   GClosure     *closure;
291   gpointer      instance;
292 };
293 struct _HandlerMatch
294 {
295   Handler      *handler;
296   HandlerMatch *next;
297   guint         signal_id;
298 };
299
300 typedef struct
301 {
302   GType     instance_type; /* 0 for default closure */
303   GClosure *closure;
304 } ClassClosure;
305
306
307 /* --- variables --- */
308 static GBSearchArray *g_signal_key_bsa = NULL;
309 static const GBSearchConfig g_signal_key_bconfig = {
310   sizeof (SignalKey),
311   signal_key_cmp,
312   G_BSEARCH_ARRAY_ALIGN_POWER2,
313 };
314 static GBSearchConfig g_signal_hlbsa_bconfig = {
315   sizeof (HandlerList),
316   handler_lists_cmp,
317   0,
318 };
319 static GBSearchConfig g_class_closure_bconfig = {
320   sizeof (ClassClosure),
321   class_closures_cmp,
322   0,
323 };
324 static GHashTable    *g_handler_list_bsa_ht = NULL;
325 static Emission      *g_emissions = NULL;
326 static gulong         g_handler_sequential_number = 1;
327 static GHashTable    *g_handlers = NULL;
328
329 G_LOCK_DEFINE_STATIC (g_signal_mutex);
330 #define SIGNAL_LOCK()           G_LOCK (g_signal_mutex)
331 #define SIGNAL_UNLOCK()         G_UNLOCK (g_signal_mutex)
332
333
334 /* --- signal nodes --- */
335 static guint          g_n_signal_nodes = 0;
336 static SignalNode   **g_signal_nodes = NULL;
337
338 static inline SignalNode*
339 LOOKUP_SIGNAL_NODE (guint signal_id)
340 {
341   if (signal_id < g_n_signal_nodes)
342     return g_signal_nodes[signal_id];
343   else
344     return NULL;
345 }
346
347
348 /* --- functions --- */
349 /* @key must have already been validated with is_valid()
350  * Modifies @key in place. */
351 static void
352 canonicalize_key (gchar *key)
353 {
354   gchar *p;
355
356   for (p = key; *p != 0; p++)
357     {
358       gchar c = *p;
359
360       if (c == '_')
361         *p = '-';
362     }
363 }
364
365 /* @key must have already been validated with is_valid() */
366 static gboolean
367 is_canonical (const gchar *key)
368 {
369   return (strchr (key, '_') == NULL);
370 }
371
372 /**
373  * g_signal_is_valid_name:
374  * @name: the canonical name of the signal
375  *
376  * Validate a signal name. This can be useful for dynamically-generated signals
377  * which need to be validated at run-time before actually trying to create them.
378  *
379  * See [canonical parameter names][canonical-parameter-names] for details of
380  * the rules for valid names. The rules for signal names are the same as those
381  * for property names.
382  *
383  * Returns: %TRUE if @name is a valid signal name, %FALSE otherwise.
384  * Since: 2.66
385  */
386 gboolean
387 g_signal_is_valid_name (const gchar *name)
388 {
389   /* FIXME: We allow this, against our own documentation (the leading `-` is
390    * invalid), because GTK has historically used this. */
391   if (g_str_equal (name, "-gtk-private-changed"))
392     return TRUE;
393
394   return g_param_spec_is_valid_name (name);
395 }
396
397 static inline guint
398 signal_id_lookup (const gchar *name,
399                   GType  itype)
400 {
401   GQuark quark;
402   GType *ifaces, type = itype;
403   SignalKey key;
404   guint n_ifaces;
405
406   quark = g_quark_try_string (name);
407   key.quark = quark;
408
409   /* try looking up signals for this type and its ancestors */
410   do
411     {
412       SignalKey *signal_key;
413       
414       key.itype = type;
415       signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
416       
417       if (signal_key)
418         return signal_key->signal_id;
419       
420       type = g_type_parent (type);
421     }
422   while (type);
423
424   /* no luck, try interfaces it exports */
425   ifaces = g_type_interfaces (itype, &n_ifaces);
426   while (n_ifaces--)
427     {
428       SignalKey *signal_key;
429
430       key.itype = ifaces[n_ifaces];
431       signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
432
433       if (signal_key)
434         {
435           g_free (ifaces);
436           return signal_key->signal_id;
437         }
438     }
439   g_free (ifaces);
440
441   /* If the @name is non-canonical, try again. This is the slow path â€” people
442    * should use canonical names in their queries if they want performance. */
443   if (!is_canonical (name))
444     {
445       guint signal_id;
446       gchar *name_copy = g_strdup (name);
447       canonicalize_key (name_copy);
448
449       signal_id = signal_id_lookup (name_copy, itype);
450
451       g_free (name_copy);
452
453       return signal_id;
454     }
455
456   return 0;
457 }
458
459 static gint
460 class_closures_cmp (gconstpointer node1,
461                     gconstpointer node2)
462 {
463   const ClassClosure *c1 = node1, *c2 = node2;
464   
465   return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
466 }
467
468 static gint
469 handler_lists_cmp (gconstpointer node1,
470                    gconstpointer node2)
471 {
472   const HandlerList *hlist1 = node1, *hlist2 = node2;
473   
474   return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
475 }
476
477 static inline HandlerList*
478 handler_list_ensure (guint    signal_id,
479                      gpointer instance)
480 {
481   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
482   HandlerList key;
483   
484   key.signal_id = signal_id;
485   key.handlers    = NULL;
486   key.tail_before = NULL;
487   key.tail_after  = NULL;
488   if (!hlbsa)
489     {
490       hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
491       hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
492       g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
493     }
494   else
495     {
496       GBSearchArray *o = hlbsa;
497
498       hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
499       if (hlbsa != o)
500         g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
501     }
502   return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
503 }
504
505 static inline HandlerList*
506 handler_list_lookup (guint    signal_id,
507                      gpointer instance)
508 {
509   GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
510   HandlerList key;
511   
512   key.signal_id = signal_id;
513   
514   return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
515 }
516
517 static guint
518 handler_hash (gconstpointer key)
519 {
520   return (guint)((Handler*)key)->sequential_number;
521 }
522
523 static gboolean
524 handler_equal (gconstpointer a, gconstpointer b)
525 {
526   Handler *ha = (Handler *)a;
527   Handler *hb = (Handler *)b;
528   return (ha->sequential_number == hb->sequential_number) &&
529       (ha->instance  == hb->instance);
530 }
531
532 static Handler*
533 handler_lookup (gpointer  instance,
534                 gulong    handler_id,
535                 GClosure *closure,
536                 guint    *signal_id_p)
537 {
538   GBSearchArray *hlbsa;
539
540   if (handler_id)
541     {
542       Handler key;
543       key.sequential_number = handler_id;
544       key.instance = instance;
545       return g_hash_table_lookup (g_handlers, &key);
546
547     }
548
549   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
550   
551   if (hlbsa)
552     {
553       guint i;
554       
555       for (i = 0; i < hlbsa->n_nodes; i++)
556         {
557           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
558           Handler *handler;
559           
560           for (handler = hlist->handlers; handler; handler = handler->next)
561             if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
562               {
563                 if (signal_id_p)
564                   *signal_id_p = hlist->signal_id;
565
566                 return handler;
567               }
568         }
569     }
570   
571   return NULL;
572 }
573
574 static inline HandlerMatch*
575 handler_match_prepend (HandlerMatch *list,
576                        Handler      *handler,
577                        guint         signal_id)
578 {
579   HandlerMatch *node;
580   
581   node = g_slice_new (HandlerMatch);
582   node->handler = handler;
583   node->next = list;
584   node->signal_id = signal_id;
585   handler_ref (handler);
586   
587   return node;
588 }
589 static inline HandlerMatch*
590 handler_match_free1_R (HandlerMatch *node,
591                        gpointer      instance)
592 {
593   HandlerMatch *next = node->next;
594   
595   handler_unref_R (node->signal_id, instance, node->handler);
596   g_slice_free (HandlerMatch, node);
597   
598   return next;
599 }
600
601 static HandlerMatch*
602 handlers_find (gpointer         instance,
603                GSignalMatchType mask,
604                guint            signal_id,
605                GQuark           detail,
606                GClosure        *closure,
607                gpointer         func,
608                gpointer         data,
609                gboolean         one_and_only)
610 {
611   HandlerMatch *mlist = NULL;
612   
613   if (mask & G_SIGNAL_MATCH_ID)
614     {
615       HandlerList *hlist = handler_list_lookup (signal_id, instance);
616       Handler *handler;
617       SignalNode *node = NULL;
618       
619       if (mask & G_SIGNAL_MATCH_FUNC)
620         {
621           node = LOOKUP_SIGNAL_NODE (signal_id);
622           if (!node || !node->c_marshaller)
623             return NULL;
624         }
625       
626       mask = ~mask;
627       for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
628         if (handler->sequential_number &&
629             ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
630             ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
631             ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
632             ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
633             ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
634                                               G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
635                                               ((GCClosure*) handler->closure)->callback == func)))
636           {
637             mlist = handler_match_prepend (mlist, handler, signal_id);
638             if (one_and_only)
639               return mlist;
640           }
641     }
642   else
643     {
644       GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
645       
646       mask = ~mask;
647       if (hlbsa)
648         {
649           guint i;
650           
651           for (i = 0; i < hlbsa->n_nodes; i++)
652             {
653               HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
654               SignalNode *node = NULL;
655               Handler *handler;
656               
657               if (!(mask & G_SIGNAL_MATCH_FUNC))
658                 {
659                   node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
660                   if (!node->c_marshaller)
661                     continue;
662                 }
663               
664               for (handler = hlist->handlers; handler; handler = handler->next)
665                 if (handler->sequential_number &&
666                     ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
667                     ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
668                     ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
669                     ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
670                     ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
671                                                       G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
672                                                       ((GCClosure*) handler->closure)->callback == func)))
673                   {
674                     mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
675                     if (one_and_only)
676                       return mlist;
677                   }
678             }
679         }
680     }
681   
682   return mlist;
683 }
684
685 static inline Handler*
686 handler_new (guint signal_id, gpointer instance, gboolean after)
687 {
688   Handler *handler = g_slice_new (Handler);
689 #ifndef G_DISABLE_CHECKS
690   if (g_handler_sequential_number < 1)
691     g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
692 #endif
693   
694   handler->sequential_number = g_handler_sequential_number++;
695   handler->prev = NULL;
696   handler->next = NULL;
697   handler->detail = 0;
698   handler->signal_id = signal_id;
699   handler->instance = instance;
700   handler->ref_count = 1;
701   handler->block_count = 0;
702   handler->after = after != FALSE;
703   handler->closure = NULL;
704   handler->has_invalid_closure_notify = 0;
705
706   g_hash_table_add (g_handlers, handler);
707   
708   return handler;
709 }
710
711 static inline void
712 handler_ref (Handler *handler)
713 {
714   g_return_if_fail (handler->ref_count > 0);
715   
716   handler->ref_count++;
717 }
718
719 static inline void
720 handler_unref_R (guint    signal_id,
721                  gpointer instance,
722                  Handler *handler)
723 {
724   g_return_if_fail (handler->ref_count > 0);
725
726   handler->ref_count--;
727
728   if (G_UNLIKELY (handler->ref_count == 0))
729     {
730       HandlerList *hlist = NULL;
731
732       if (handler->next)
733         handler->next->prev = handler->prev;
734       if (handler->prev)    /* watch out for g_signal_handlers_destroy()! */
735         handler->prev->next = handler->next;
736       else
737         {
738           hlist = handler_list_lookup (signal_id, instance);
739           g_assert (hlist != NULL);
740           hlist->handlers = handler->next;
741         }
742
743       if (instance)
744         {
745           /*  check if we are removing the handler pointed to by tail_before  */
746           if (!handler->after && (!handler->next || handler->next->after))
747             {
748               if (!hlist)
749                 hlist = handler_list_lookup (signal_id, instance);
750               if (hlist)
751                 {
752                   g_assert (hlist->tail_before == handler); /* paranoid */
753                   hlist->tail_before = handler->prev;
754                 }
755             }
756
757           /*  check if we are removing the handler pointed to by tail_after  */
758           if (!handler->next)
759             {
760               if (!hlist)
761                 hlist = handler_list_lookup (signal_id, instance);
762               if (hlist)
763                 {
764                   g_assert (hlist->tail_after == handler); /* paranoid */
765                   hlist->tail_after = handler->prev;
766                 }
767             }
768         }
769
770       SIGNAL_UNLOCK ();
771       g_closure_unref (handler->closure);
772       SIGNAL_LOCK ();
773       g_slice_free (Handler, handler);
774     }
775 }
776
777 static void
778 handler_insert (guint    signal_id,
779                 gpointer instance,
780                 Handler  *handler)
781 {
782   HandlerList *hlist;
783   
784   g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
785
786   hlist = handler_list_ensure (signal_id, instance);
787   if (!hlist->handlers)
788     {
789       hlist->handlers = handler;
790       if (!handler->after)
791         hlist->tail_before = handler;
792     }
793   else if (handler->after)
794     {
795       handler->prev = hlist->tail_after;
796       hlist->tail_after->next = handler;
797     }
798   else
799     {
800       if (hlist->tail_before)
801         {
802           handler->next = hlist->tail_before->next;
803           if (handler->next)
804             handler->next->prev = handler;
805           handler->prev = hlist->tail_before;
806           hlist->tail_before->next = handler;
807         }
808       else /* insert !after handler into a list of only after handlers */
809         {
810           handler->next = hlist->handlers;
811           if (handler->next)
812             handler->next->prev = handler;
813           hlist->handlers = handler;
814         }
815       hlist->tail_before = handler;
816     }
817
818   if (!handler->next)
819     hlist->tail_after = handler;
820 }
821
822 static void
823 node_update_single_va_closure (SignalNode *node)
824 {
825   GClosure *closure = NULL;
826   gboolean is_after = FALSE;
827
828   /* Fast path single-handler without boxing the arguments in GValues */
829   if (G_TYPE_IS_OBJECT (node->itype) &&
830       (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
831       (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
832     {
833       GSignalFlags run_type;
834       ClassClosure * cc; 
835       GBSearchArray *bsa = node->class_closure_bsa;
836
837       if (bsa == NULL || bsa->n_nodes == 0)
838         closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
839       else if (bsa->n_nodes == 1)
840         {
841           /* Look for default class closure (can't support non-default as it
842              chains up using GValues */
843           cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
844           if (cc->instance_type == 0)
845             {
846               run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
847               /* Only support *one* of run-first or run-last, not multiple or cleanup */
848               if (run_type == G_SIGNAL_RUN_FIRST ||
849                   run_type == G_SIGNAL_RUN_LAST)
850                 {
851                   closure = cc->closure;
852                   is_after = (run_type == G_SIGNAL_RUN_LAST);
853                 }
854             }
855         }
856     }
857
858   node->single_va_closure_is_valid = TRUE;
859   node->single_va_closure = closure;
860   node->single_va_closure_is_after = is_after;
861 }
862
863 static inline void
864 emission_push (Emission  *emission)
865 {
866   emission->next = g_emissions;
867   g_emissions = emission;
868 }
869
870 static inline void
871 emission_pop (Emission  *emission)
872 {
873   Emission *node, *last = NULL;
874
875   for (node = g_emissions; node; last = node, node = last->next)
876     if (node == emission)
877       {
878         if (last)
879           last->next = node->next;
880         else
881           g_emissions = node->next;
882         return;
883       }
884   g_assert_not_reached ();
885 }
886
887 static inline Emission*
888 emission_find (guint     signal_id,
889                GQuark    detail,
890                gpointer  instance)
891 {
892   Emission *emission;
893   
894   for (emission = g_emissions; emission; emission = emission->next)
895     if (emission->instance == instance &&
896         emission->ihint.signal_id == signal_id &&
897         emission->ihint.detail == detail)
898       return emission;
899   return NULL;
900 }
901
902 static inline Emission*
903 emission_find_innermost (gpointer instance)
904 {
905   Emission *emission;
906   
907   for (emission = g_emissions; emission; emission = emission->next)
908     if (emission->instance == instance)
909       return emission;
910
911   return NULL;
912 }
913
914 static gint
915 signal_key_cmp (gconstpointer node1,
916                 gconstpointer node2)
917 {
918   const SignalKey *key1 = node1, *key2 = node2;
919   
920   if (key1->itype == key2->itype)
921     return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
922   else
923     return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
924 }
925
926 void
927 _g_signal_init (void)
928 {
929   SIGNAL_LOCK ();
930   if (!g_n_signal_nodes)
931     {
932       /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
933       g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
934       g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
935       
936       /* invalid (0) signal_id */
937       g_n_signal_nodes = 1;
938       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
939       g_signal_nodes[0] = NULL;
940       g_handlers = g_hash_table_new (handler_hash, handler_equal);
941     }
942   SIGNAL_UNLOCK ();
943 }
944
945 void
946 _g_signals_destroy (GType itype)
947 {
948   guint i;
949   
950   SIGNAL_LOCK ();
951   for (i = 1; i < g_n_signal_nodes; i++)
952     {
953       SignalNode *node = g_signal_nodes[i];
954       
955       if (node->itype == itype)
956         {
957           if (node->destroyed)
958             g_critical (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
959                         node->name,
960                         type_debug_name (node->itype));
961           else
962             signal_destroy_R (node);
963         }
964     }
965   SIGNAL_UNLOCK ();
966 }
967
968 /**
969  * g_signal_stop_emission:
970  * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
971  * @signal_id: the signal identifier, as returned by g_signal_lookup().
972  * @detail: the detail which the signal was emitted with.
973  *
974  * Stops a signal's current emission.
975  *
976  * This will prevent the default method from running, if the signal was
977  * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
978  * flag).
979  *
980  * Prints a warning if used on a signal which isn't being emitted.
981  */
982 void
983 g_signal_stop_emission (gpointer instance,
984                         guint    signal_id,
985                         GQuark   detail)
986 {
987   SignalNode *node;
988   
989   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
990   g_return_if_fail (signal_id > 0);
991   
992   SIGNAL_LOCK ();
993   node = LOOKUP_SIGNAL_NODE (signal_id);
994   if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
995     {
996       g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
997       SIGNAL_UNLOCK ();
998       return;
999     }
1000   if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1001     {
1002       Emission *emission = emission_find (signal_id, detail, instance);
1003       
1004       if (emission)
1005         {
1006           if (emission->state == EMISSION_HOOK)
1007             g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1008                         node->name, instance);
1009           else if (emission->state == EMISSION_RUN)
1010             emission->state = EMISSION_STOP;
1011         }
1012       else
1013         g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1014                     node->name, instance);
1015     }
1016   else
1017     g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
1018   SIGNAL_UNLOCK ();
1019 }
1020
1021 static void
1022 signal_finalize_hook (GHookList *hook_list,
1023                       GHook     *hook)
1024 {
1025   GDestroyNotify destroy = hook->destroy;
1026
1027   if (destroy)
1028     {
1029       hook->destroy = NULL;
1030       SIGNAL_UNLOCK ();
1031       destroy (hook->data);
1032       SIGNAL_LOCK ();
1033     }
1034 }
1035
1036 /**
1037  * g_signal_add_emission_hook:
1038  * @signal_id: the signal identifier, as returned by g_signal_lookup().
1039  * @detail: the detail on which to call the hook.
1040  * @hook_func: (not nullable): a #GSignalEmissionHook function.
1041  * @hook_data: (nullable) (closure hook_func): user data for @hook_func.
1042  * @data_destroy: (nullable) (destroy hook_data): a #GDestroyNotify for @hook_data.
1043  *
1044  * Adds an emission hook for a signal, which will get called for any emission
1045  * of that signal, independent of the instance. This is possible only
1046  * for signals which don't have %G_SIGNAL_NO_HOOKS flag set.
1047  *
1048  * Returns: the hook id, for later use with g_signal_remove_emission_hook().
1049  */
1050 gulong
1051 g_signal_add_emission_hook (guint               signal_id,
1052                             GQuark              detail,
1053                             GSignalEmissionHook hook_func,
1054                             gpointer            hook_data,
1055                             GDestroyNotify      data_destroy)
1056 {
1057   static gulong seq_hook_id = 1;
1058   SignalNode *node;
1059   GHook *hook;
1060   SignalHook *signal_hook;
1061
1062   g_return_val_if_fail (signal_id > 0, 0);
1063   g_return_val_if_fail (hook_func != NULL, 0);
1064
1065   SIGNAL_LOCK ();
1066   node = LOOKUP_SIGNAL_NODE (signal_id);
1067   if (!node || node->destroyed)
1068     {
1069       g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1070       SIGNAL_UNLOCK ();
1071       return 0;
1072     }
1073   if (node->flags & G_SIGNAL_NO_HOOKS) 
1074     {
1075       g_critical ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
1076       SIGNAL_UNLOCK ();
1077       return 0;
1078     }
1079   if (detail && !(node->flags & G_SIGNAL_DETAILED))
1080     {
1081       g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1082       SIGNAL_UNLOCK ();
1083       return 0;
1084     }
1085     node->single_va_closure_is_valid = FALSE;
1086   if (!node->emission_hooks)
1087     {
1088       node->emission_hooks = g_new (GHookList, 1);
1089       g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
1090       node->emission_hooks->finalize_hook = signal_finalize_hook;
1091     }
1092
1093   node_check_deprecated (node);
1094
1095   hook = g_hook_alloc (node->emission_hooks);
1096   hook->data = hook_data;
1097   hook->func = (gpointer) hook_func;
1098   hook->destroy = data_destroy;
1099   signal_hook = SIGNAL_HOOK (hook);
1100   signal_hook->detail = detail;
1101   node->emission_hooks->seq_id = seq_hook_id;
1102   g_hook_append (node->emission_hooks, hook);
1103   seq_hook_id = node->emission_hooks->seq_id;
1104
1105   SIGNAL_UNLOCK ();
1106
1107   return hook->hook_id;
1108 }
1109
1110 /**
1111  * g_signal_remove_emission_hook:
1112  * @signal_id: the id of the signal
1113  * @hook_id: the id of the emission hook, as returned by
1114  *  g_signal_add_emission_hook()
1115  *
1116  * Deletes an emission hook.
1117  */
1118 void
1119 g_signal_remove_emission_hook (guint  signal_id,
1120                                gulong hook_id)
1121 {
1122   SignalNode *node;
1123
1124   g_return_if_fail (signal_id > 0);
1125   g_return_if_fail (hook_id > 0);
1126
1127   SIGNAL_LOCK ();
1128   node = LOOKUP_SIGNAL_NODE (signal_id);
1129   if (!node || node->destroyed)
1130     {
1131       g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1132       goto out;
1133     }
1134   else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1135     g_critical ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1136
1137   node->single_va_closure_is_valid = FALSE;
1138
1139  out:
1140   SIGNAL_UNLOCK ();
1141 }
1142
1143 static inline guint
1144 signal_parse_name (const gchar *name,
1145                    GType        itype,
1146                    GQuark      *detail_p,
1147                    gboolean     force_quark)
1148 {
1149   const gchar *colon = strchr (name, ':');
1150   guint signal_id;
1151   
1152   if (!colon)
1153     {
1154       signal_id = signal_id_lookup (name, itype);
1155       if (signal_id && detail_p)
1156         *detail_p = 0;
1157     }
1158   else if (colon[1] == ':')
1159     {
1160       gchar buffer[32];
1161       guint l = colon - name;
1162       
1163       if (colon[2] == '\0')
1164         return 0;
1165
1166       if (l < 32)
1167         {
1168           memcpy (buffer, name, l);
1169           buffer[l] = 0;
1170           signal_id = signal_id_lookup (buffer, itype);
1171         }
1172       else
1173         {
1174           gchar *signal = g_new (gchar, l + 1);
1175           
1176           memcpy (signal, name, l);
1177           signal[l] = 0;
1178           signal_id = signal_id_lookup (signal, itype);
1179           g_free (signal);
1180         }
1181       
1182       if (signal_id && detail_p)
1183         *detail_p = (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2);
1184     }
1185   else
1186     signal_id = 0;
1187   return signal_id;
1188 }
1189
1190 /**
1191  * g_signal_parse_name:
1192  * @detailed_signal: a string of the form "signal-name::detail".
1193  * @itype: The interface/instance type that introduced "signal-name".
1194  * @signal_id_p: (out): Location to store the signal id.
1195  * @detail_p: (out): Location to store the detail quark.
1196  * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1197  *
1198  * Internal function to parse a signal name into its @signal_id
1199  * and @detail quark.
1200  *
1201  * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1202  */
1203 gboolean
1204 g_signal_parse_name (const gchar *detailed_signal,
1205                      GType        itype,
1206                      guint       *signal_id_p,
1207                      GQuark      *detail_p,
1208                      gboolean     force_detail_quark)
1209 {
1210   SignalNode *node;
1211   GQuark detail = 0;
1212   guint signal_id;
1213   
1214   g_return_val_if_fail (detailed_signal != NULL, FALSE);
1215   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1216   
1217   SIGNAL_LOCK ();
1218   signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1219
1220   node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1221
1222   if (!node || node->destroyed ||
1223       (detail && !(node->flags & G_SIGNAL_DETAILED)))
1224     {
1225       SIGNAL_UNLOCK ();
1226       return FALSE;
1227     }
1228
1229   SIGNAL_UNLOCK ();
1230
1231   if (signal_id_p)
1232     *signal_id_p = signal_id;
1233   if (detail_p)
1234     *detail_p = detail;
1235   
1236   return TRUE;
1237 }
1238
1239 /**
1240  * g_signal_stop_emission_by_name:
1241  * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1242  * @detailed_signal: a string of the form "signal-name::detail".
1243  *
1244  * Stops a signal's current emission.
1245  *
1246  * This is just like g_signal_stop_emission() except it will look up the
1247  * signal id for you.
1248  */
1249 void
1250 g_signal_stop_emission_by_name (gpointer     instance,
1251                                 const gchar *detailed_signal)
1252 {
1253   guint signal_id;
1254   GQuark detail = 0;
1255   GType itype;
1256   
1257   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1258   g_return_if_fail (detailed_signal != NULL);
1259   
1260   SIGNAL_LOCK ();
1261   itype = G_TYPE_FROM_INSTANCE (instance);
1262   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1263   if (signal_id)
1264     {
1265       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1266       
1267       if (detail && !(node->flags & G_SIGNAL_DETAILED))
1268         g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
1269       else if (!g_type_is_a (itype, node->itype))
1270         g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1271                     G_STRLOC, detailed_signal, instance, g_type_name (itype));
1272       else
1273         {
1274           Emission *emission = emission_find (signal_id, detail, instance);
1275           
1276           if (emission)
1277             {
1278               if (emission->state == EMISSION_HOOK)
1279                 g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1280                             node->name, instance);
1281               else if (emission->state == EMISSION_RUN)
1282                 emission->state = EMISSION_STOP;
1283             }
1284           else
1285             g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1286                         node->name, instance);
1287         }
1288     }
1289   else
1290     g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1291                 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1292   SIGNAL_UNLOCK ();
1293 }
1294
1295 /**
1296  * g_signal_lookup:
1297  * @name: the signal's name.
1298  * @itype: the type that the signal operates on.
1299  *
1300  * Given the name of the signal and the type of object it connects to, gets
1301  * the signal's identifying integer. Emitting the signal by number is
1302  * somewhat faster than using the name each time.
1303  *
1304  * Also tries the ancestors of the given type.
1305  *
1306  * The type class passed as @itype must already have been instantiated (for
1307  * example, using g_type_class_ref()) for this function to work, as signals are
1308  * always installed during class initialization.
1309  *
1310  * See g_signal_new() for details on allowed signal names.
1311  *
1312  * Returns: the signal's identifying number, or 0 if no signal was found.
1313  */
1314 guint
1315 g_signal_lookup (const gchar *name,
1316                  GType        itype)
1317 {
1318   guint signal_id;
1319   g_return_val_if_fail (name != NULL, 0);
1320   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1321   
1322   SIGNAL_LOCK ();
1323   signal_id = signal_id_lookup (name, itype);
1324   SIGNAL_UNLOCK ();
1325   if (!signal_id)
1326     {
1327       /* give elaborate warnings */
1328       if (!g_type_name (itype))
1329         g_critical (G_STRLOC ": unable to look up signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'",
1330                     name, itype);
1331       else if (!g_signal_is_valid_name (name))
1332         g_critical (G_STRLOC ": unable to look up invalid signal name \"%s\" on type '%s'",
1333                     name, g_type_name (itype));
1334     }
1335   
1336   return signal_id;
1337 }
1338
1339 /**
1340  * g_signal_list_ids:
1341  * @itype: Instance or interface type.
1342  * @n_ids: Location to store the number of signal ids for @itype.
1343  *
1344  * Lists the signals by id that a certain instance or interface type
1345  * created. Further information about the signals can be acquired through
1346  * g_signal_query().
1347  *
1348  * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs.
1349  */
1350 guint*
1351 g_signal_list_ids (GType  itype,
1352                    guint *n_ids)
1353 {
1354   SignalKey *keys;
1355   GArray *result;
1356   guint n_nodes;
1357   guint i;
1358   
1359   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1360   g_return_val_if_fail (n_ids != NULL, NULL);
1361   
1362   SIGNAL_LOCK ();
1363   keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1364   n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1365   result = g_array_new (FALSE, FALSE, sizeof (guint));
1366   
1367   for (i = 0; i < n_nodes; i++)
1368     if (keys[i].itype == itype)
1369       {
1370         g_array_append_val (result, keys[i].signal_id);
1371       }
1372   *n_ids = result->len;
1373   SIGNAL_UNLOCK ();
1374   if (!n_nodes)
1375     {
1376       /* give elaborate warnings */
1377       if (!g_type_name (itype))
1378         g_critical (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'",
1379                     itype);
1380       else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1381         g_critical (G_STRLOC ": unable to list signals of non instantiatable type '%s'",
1382                     g_type_name (itype));
1383       else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1384         g_critical (G_STRLOC ": unable to list signals of unloaded type '%s'",
1385                     g_type_name (itype));
1386     }
1387   
1388   return (guint*) g_array_free (result, FALSE);
1389 }
1390
1391 /**
1392  * g_signal_name:
1393  * @signal_id: the signal's identifying number.
1394  *
1395  * Given the signal's identifier, finds its name.
1396  *
1397  * Two different signals may have the same name, if they have differing types.
1398  *
1399  * Returns: (nullable): the signal name, or %NULL if the signal number was invalid.
1400  */
1401 const gchar *
1402 g_signal_name (guint signal_id)
1403 {
1404   SignalNode *node;
1405   const gchar *name;
1406   
1407   SIGNAL_LOCK ();
1408   node = LOOKUP_SIGNAL_NODE (signal_id);
1409   name = node ? node->name : NULL;
1410   SIGNAL_UNLOCK ();
1411   
1412   return (char*) name;
1413 }
1414
1415 /**
1416  * g_signal_query:
1417  * @signal_id: The signal id of the signal to query information for.
1418  * @query: (out caller-allocates) (not optional): A user provided structure that is
1419  *  filled in with constant values upon success.
1420  *
1421  * Queries the signal system for in-depth information about a
1422  * specific signal. This function will fill in a user-provided
1423  * structure to hold signal-specific information. If an invalid
1424  * signal id is passed in, the @signal_id member of the #GSignalQuery
1425  * is 0. All members filled into the #GSignalQuery structure should
1426  * be considered constant and have to be left untouched.
1427  */
1428 void
1429 g_signal_query (guint         signal_id,
1430                 GSignalQuery *query)
1431 {
1432   SignalNode *node;
1433   
1434   g_return_if_fail (query != NULL);
1435   
1436   SIGNAL_LOCK ();
1437   node = LOOKUP_SIGNAL_NODE (signal_id);
1438   if (!node || node->destroyed)
1439     query->signal_id = 0;
1440   else
1441     {
1442       query->signal_id = node->signal_id;
1443       query->signal_name = node->name;
1444       query->itype = node->itype;
1445       query->signal_flags = node->flags;
1446       query->return_type = node->return_type;
1447       query->n_params = node->n_params;
1448       query->param_types = node->param_types;
1449     }
1450   SIGNAL_UNLOCK ();
1451 }
1452
1453 /**
1454  * g_signal_new:
1455  * @signal_name: the name for the signal
1456  * @itype: the type this signal pertains to. It will also pertain to
1457  *  types which are derived from this type.
1458  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1459  *  the default handler is to be invoked. You should at least specify
1460  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1461  * @class_offset: The offset of the function pointer in the class structure
1462  *  for this type. Used to invoke a class method generically. Pass 0 to
1463  *  not associate a class method slot with this signal.
1464  * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1465  * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1466  * @c_marshaller: (nullable): the function to translate arrays of parameter
1467  *  values to signal emissions into C language callback invocations or %NULL.
1468  * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1469  *  without a return value.
1470  * @n_params: the number of parameter types to follow.
1471  * @...: a list of types, one for each parameter.
1472  *
1473  * Creates a new signal. (This is usually done in the class initializer.)
1474  *
1475  * A signal name consists of segments consisting of ASCII letters and
1476  * digits, separated by either the `-` or `_` character. The first
1477  * character of a signal name must be a letter. Names which violate these
1478  * rules lead to undefined behaviour. These are the same rules as for property
1479  * naming (see g_param_spec_internal()).
1480  *
1481  * When registering a signal and looking up a signal, either separator can
1482  * be used, but they cannot be mixed. Using `-` is considerably more efficient.
1483  * Using `_` is discouraged.
1484  *
1485  * If 0 is used for @class_offset subclasses cannot override the class handler
1486  * in their class_init method by doing super_class->signal_handler = my_signal_handler.
1487  * Instead they will have to use g_signal_override_class_handler().
1488  *
1489  * If @c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1490  * the marshaller for this signal. In some simple cases, g_signal_new()
1491  * will use a more optimized c_marshaller and va_marshaller for the signal
1492  * instead of g_cclosure_marshal_generic().
1493  *
1494  * If @c_marshaller is non-%NULL, you need to also specify a va_marshaller
1495  * using g_signal_set_va_marshaller() or the generic va_marshaller will
1496  * be used.
1497  *
1498  * Returns: the signal id
1499  */
1500 guint
1501 g_signal_new (const gchar        *signal_name,
1502               GType               itype,
1503               GSignalFlags        signal_flags,
1504               guint               class_offset,
1505               GSignalAccumulator  accumulator,
1506               gpointer            accu_data,
1507               GSignalCMarshaller  c_marshaller,
1508               GType               return_type,
1509               guint               n_params,
1510               ...)
1511 {
1512   va_list args;
1513   guint signal_id;
1514
1515   g_return_val_if_fail (signal_name != NULL, 0);
1516   
1517   va_start (args, n_params);
1518
1519   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1520                                    class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1521                                    accumulator, accu_data, c_marshaller,
1522                                    return_type, n_params, args);
1523
1524   va_end (args);
1525
1526   return signal_id;
1527 }
1528
1529 /**
1530  * g_signal_new_class_handler:
1531  * @signal_name: the name for the signal
1532  * @itype: the type this signal pertains to. It will also pertain to
1533  *  types which are derived from this type.
1534  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1535  *  the default handler is to be invoked. You should at least specify
1536  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1537  * @class_handler: (nullable): a #GCallback which acts as class implementation of
1538  *  this signal. Used to invoke a class method generically. Pass %NULL to
1539  *  not associate a class method with this signal.
1540  * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1541  * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1542  * @c_marshaller: (nullable): the function to translate arrays of parameter
1543  *  values to signal emissions into C language callback invocations or %NULL.
1544  * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1545  *  without a return value.
1546  * @n_params: the number of parameter types to follow.
1547  * @...: a list of types, one for each parameter.
1548  *
1549  * Creates a new signal. (This is usually done in the class initializer.)
1550  *
1551  * This is a variant of g_signal_new() that takes a C callback instead
1552  * of a class offset for the signal's class handler. This function
1553  * doesn't need a function pointer exposed in the class structure of
1554  * an object definition, instead the function pointer is passed
1555  * directly and can be overridden by derived classes with
1556  * g_signal_override_class_closure() or
1557  * g_signal_override_class_handler() and chained to with
1558  * g_signal_chain_from_overridden() or
1559  * g_signal_chain_from_overridden_handler().
1560  *
1561  * See g_signal_new() for information about signal names.
1562  *
1563  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1564  * the marshaller for this signal.
1565  *
1566  * Returns: the signal id
1567  *
1568  * Since: 2.18
1569  */
1570 guint
1571 g_signal_new_class_handler (const gchar        *signal_name,
1572                             GType               itype,
1573                             GSignalFlags        signal_flags,
1574                             GCallback           class_handler,
1575                             GSignalAccumulator  accumulator,
1576                             gpointer            accu_data,
1577                             GSignalCMarshaller  c_marshaller,
1578                             GType               return_type,
1579                             guint               n_params,
1580                             ...)
1581 {
1582   va_list args;
1583   guint signal_id;
1584
1585   g_return_val_if_fail (signal_name != NULL, 0);
1586
1587   va_start (args, n_params);
1588
1589   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1590                                    class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1591                                    accumulator, accu_data, c_marshaller,
1592                                    return_type, n_params, args);
1593
1594   va_end (args);
1595
1596   return signal_id;
1597 }
1598
1599 static inline ClassClosure*
1600 signal_find_class_closure (SignalNode *node,
1601                            GType       itype)
1602 {
1603   GBSearchArray *bsa = node->class_closure_bsa;
1604   ClassClosure *cc;
1605
1606   if (bsa)
1607     {
1608       ClassClosure key;
1609
1610       /* cc->instance_type is 0 for default closure */
1611
1612       if (g_bsearch_array_get_n_nodes (bsa) == 1)
1613         {
1614           cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
1615           if (cc && cc->instance_type == 0) /* check for default closure */
1616             return cc;
1617         }
1618
1619       key.instance_type = itype;
1620       cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1621       while (!cc && key.instance_type)
1622         {
1623           key.instance_type = g_type_parent (key.instance_type);
1624           cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1625         }
1626     }
1627   else
1628     cc = NULL;
1629   return cc;
1630 }
1631
1632 static inline GClosure*
1633 signal_lookup_closure (SignalNode    *node,
1634                        GTypeInstance *instance)
1635 {
1636   ClassClosure *cc;
1637
1638   cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1639   return cc ? cc->closure : NULL;
1640 }
1641
1642 static void
1643 signal_add_class_closure (SignalNode *node,
1644                           GType       itype,
1645                           GClosure   *closure)
1646 {
1647   ClassClosure key;
1648
1649   node->single_va_closure_is_valid = FALSE;
1650
1651   if (!node->class_closure_bsa)
1652     node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1653   key.instance_type = itype;
1654   key.closure = g_closure_ref (closure);
1655   node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1656                                                     &g_class_closure_bconfig,
1657                                                     &key);
1658   g_closure_sink (closure);
1659   if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1660     {
1661       g_closure_set_marshal (closure, node->c_marshaller);
1662       if (node->va_marshaller)
1663         _g_closure_set_va_marshal (closure, node->va_marshaller);
1664     }
1665 }
1666
1667 /**
1668  * g_signal_newv:
1669  * @signal_name: the name for the signal
1670  * @itype: the type this signal pertains to. It will also pertain to
1671  *     types which are derived from this type
1672  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1673  *     the default handler is to be invoked. You should at least specify
1674  *     %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1675  * @class_closure: (nullable): The closure to invoke on signal emission;
1676  *     may be %NULL
1677  * @accumulator: (nullable): the accumulator for this signal; may be %NULL
1678  * @accu_data: (nullable) (closure accumulator): user data for the @accumulator
1679  * @c_marshaller: (nullable): the function to translate arrays of
1680  *     parameter values to signal emissions into C language callback
1681  *     invocations or %NULL
1682  * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1683  *     without a return value
1684  * @n_params: the length of @param_types
1685  * @param_types: (array length=n_params) (nullable): an array of types, one for
1686  *     each parameter (may be %NULL if @n_params is zero)
1687  *
1688  * Creates a new signal. (This is usually done in the class initializer.)
1689  *
1690  * See g_signal_new() for details on allowed signal names.
1691  *
1692  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1693  * the marshaller for this signal.
1694  *
1695  * Returns: the signal id
1696  */
1697 guint
1698 g_signal_newv (const gchar       *signal_name,
1699                GType              itype,
1700                GSignalFlags       signal_flags,
1701                GClosure          *class_closure,
1702                GSignalAccumulator accumulator,
1703                gpointer           accu_data,
1704                GSignalCMarshaller c_marshaller,
1705                GType              return_type,
1706                guint              n_params,
1707                GType             *param_types)
1708 {
1709   const gchar *name;
1710   gchar *signal_name_copy = NULL;
1711   guint signal_id, i;
1712   SignalNode *node;
1713   GSignalCMarshaller builtin_c_marshaller;
1714   GSignalCVaMarshaller builtin_va_marshaller;
1715   GSignalCVaMarshaller va_marshaller;
1716   
1717   g_return_val_if_fail (signal_name != NULL, 0);
1718   g_return_val_if_fail (g_signal_is_valid_name (signal_name), 0);
1719   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1720   if (n_params)
1721     g_return_val_if_fail (param_types != NULL, 0);
1722   g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1723   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1724     g_return_val_if_fail (accumulator == NULL, 0);
1725   if (!accumulator)
1726     g_return_val_if_fail (accu_data == NULL, 0);
1727   g_return_val_if_fail ((signal_flags & G_SIGNAL_ACCUMULATOR_FIRST_RUN) == 0, 0);
1728
1729   if (!is_canonical (signal_name))
1730     {
1731       signal_name_copy = g_strdup (signal_name);
1732       canonicalize_key (signal_name_copy);
1733       name = signal_name_copy;
1734     }
1735   else
1736     {
1737       name = signal_name;
1738     }
1739   
1740   SIGNAL_LOCK ();
1741   
1742   signal_id = signal_id_lookup (name, itype);
1743   node = LOOKUP_SIGNAL_NODE (signal_id);
1744   if (node && !node->destroyed)
1745     {
1746       g_critical (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1747                   name,
1748                   type_debug_name (node->itype),
1749                   G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1750       g_free (signal_name_copy);
1751       SIGNAL_UNLOCK ();
1752       return 0;
1753     }
1754   if (node && node->itype != itype)
1755     {
1756       g_critical (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1757                   name,
1758                   type_debug_name (itype),
1759                   type_debug_name (node->itype));
1760       g_free (signal_name_copy);
1761       SIGNAL_UNLOCK ();
1762       return 0;
1763     }
1764   for (i = 0; i < n_params; i++)
1765     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1766       {
1767         g_critical (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1768                     i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1769         g_free (signal_name_copy);
1770         SIGNAL_UNLOCK ();
1771         return 0;
1772       }
1773   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1774     {
1775       g_critical (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1776                   type_debug_name (return_type), type_debug_name (itype), name);
1777       g_free (signal_name_copy);
1778       SIGNAL_UNLOCK ();
1779       return 0;
1780     }
1781   
1782   /* setup permanent portion of signal node */
1783   if (!node)
1784     {
1785       SignalKey key;
1786       
1787       signal_id = g_n_signal_nodes++;
1788       node = g_new (SignalNode, 1);
1789       node->signal_id = signal_id;
1790       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1791       g_signal_nodes[signal_id] = node;
1792       node->itype = itype;
1793       key.itype = itype;
1794       key.signal_id = signal_id;
1795       node->name = g_intern_string (name);
1796       key.quark = g_quark_from_string (name);
1797       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1798
1799       TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1800     }
1801   node->destroyed = FALSE;
1802
1803   /* setup reinitializable portion */
1804   node->single_va_closure_is_valid = FALSE;
1805   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1806   node->n_params = n_params;
1807   node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params);
1808   node->return_type = return_type;
1809   node->class_closure_bsa = NULL;
1810   if (accumulator)
1811     {
1812       node->accumulator = g_new (SignalAccumulator, 1);
1813       node->accumulator->func = accumulator;
1814       node->accumulator->data = accu_data;
1815     }
1816   else
1817     node->accumulator = NULL;
1818
1819   builtin_c_marshaller = NULL;
1820   builtin_va_marshaller = NULL;
1821
1822   /* Pick up built-in va marshallers for standard types, and
1823      instead of generic marshaller if no marshaller specified */
1824   if (n_params == 0 && return_type == G_TYPE_NONE)
1825     {
1826       builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1827       builtin_va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1828     }
1829   else if (n_params == 1 && return_type == G_TYPE_NONE)
1830     {
1831 #define ADD_CHECK(__type__) \
1832       else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__))         \
1833         {                                                                \
1834           builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__;  \
1835           builtin_va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v;     \
1836         }
1837
1838       if (0) {}
1839       ADD_CHECK (BOOLEAN)
1840       ADD_CHECK (CHAR)
1841       ADD_CHECK (UCHAR)
1842       ADD_CHECK (INT)
1843       ADD_CHECK (UINT)
1844       ADD_CHECK (LONG)
1845       ADD_CHECK (ULONG)
1846       ADD_CHECK (ENUM)
1847       ADD_CHECK (FLAGS)
1848       ADD_CHECK (FLOAT)
1849       ADD_CHECK (DOUBLE)
1850       ADD_CHECK (STRING)
1851       ADD_CHECK (PARAM)
1852       ADD_CHECK (BOXED)
1853       ADD_CHECK (POINTER)
1854       ADD_CHECK (OBJECT)
1855       ADD_CHECK (VARIANT)
1856     }
1857
1858   if (c_marshaller == NULL)
1859     {
1860       if (builtin_c_marshaller)
1861         {
1862           c_marshaller = builtin_c_marshaller;
1863           va_marshaller = builtin_va_marshaller;
1864         }
1865       else
1866         {
1867           c_marshaller = g_cclosure_marshal_generic;
1868           va_marshaller = g_cclosure_marshal_generic_va;
1869         }
1870     }
1871   else
1872     va_marshaller = NULL;
1873
1874   node->c_marshaller = c_marshaller;
1875   node->va_marshaller = va_marshaller;
1876   node->emission_hooks = NULL;
1877   if (class_closure)
1878     signal_add_class_closure (node, 0, class_closure);
1879
1880   SIGNAL_UNLOCK ();
1881
1882   g_free (signal_name_copy);
1883
1884   return signal_id;
1885 }
1886
1887 /**
1888  * g_signal_set_va_marshaller:
1889  * @signal_id: the signal id
1890  * @instance_type: the instance type on which to set the marshaller.
1891  * @va_marshaller: the marshaller to set.
1892  *
1893  * Change the #GSignalCVaMarshaller used for a given signal.  This is a
1894  * specialised form of the marshaller that can often be used for the
1895  * common case of a single connected signal handler and avoids the
1896  * overhead of #GValue.  Its use is optional.
1897  *
1898  * Since: 2.32
1899  */
1900 void
1901 g_signal_set_va_marshaller (guint              signal_id,
1902                             GType              instance_type,
1903                             GSignalCVaMarshaller va_marshaller)
1904 {
1905   SignalNode *node;
1906   
1907   g_return_if_fail (signal_id > 0);
1908   g_return_if_fail (va_marshaller != NULL);
1909   
1910   SIGNAL_LOCK ();
1911   node = LOOKUP_SIGNAL_NODE (signal_id);
1912   if (node)
1913     {
1914       node->va_marshaller = va_marshaller;
1915       if (node->class_closure_bsa)
1916         {
1917           ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1918           if (cc->closure->marshal == node->c_marshaller)
1919             _g_closure_set_va_marshal (cc->closure, va_marshaller);
1920         }
1921
1922       node->single_va_closure_is_valid = FALSE;
1923     }
1924
1925   SIGNAL_UNLOCK ();
1926 }
1927
1928
1929 /**
1930  * g_signal_new_valist:
1931  * @signal_name: the name for the signal
1932  * @itype: the type this signal pertains to. It will also pertain to
1933  *  types which are derived from this type.
1934  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1935  *  the default handler is to be invoked. You should at least specify
1936  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1937  * @class_closure: (nullable): The closure to invoke on signal emission; may be %NULL.
1938  * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1939  * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1940  * @c_marshaller: (nullable): the function to translate arrays of parameter
1941  *  values to signal emissions into C language callback invocations or %NULL.
1942  * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1943  *  without a return value.
1944  * @n_params: the number of parameter types in @args.
1945  * @args: va_list of #GType, one for each parameter.
1946  *
1947  * Creates a new signal. (This is usually done in the class initializer.)
1948  *
1949  * See g_signal_new() for details on allowed signal names.
1950  *
1951  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1952  * the marshaller for this signal.
1953  *
1954  * Returns: the signal id
1955  */
1956 guint
1957 g_signal_new_valist (const gchar       *signal_name,
1958                      GType              itype,
1959                      GSignalFlags       signal_flags,
1960                      GClosure          *class_closure,
1961                      GSignalAccumulator accumulator,
1962                      gpointer           accu_data,
1963                      GSignalCMarshaller c_marshaller,
1964                      GType              return_type,
1965                      guint              n_params,
1966                      va_list            args)
1967 {
1968   /* Somewhat arbitrarily reserve 200 bytes. That should cover the majority
1969    * of cases where n_params is small and still be small enough for what we
1970    * want to put on the stack. */
1971   GType param_types_stack[200 / sizeof (GType)];
1972   GType *param_types_heap = NULL;
1973   GType *param_types;
1974   guint i;
1975   guint signal_id;
1976
1977   param_types = param_types_stack;
1978   if (n_params > 0)
1979     {
1980       if (G_UNLIKELY (n_params > G_N_ELEMENTS (param_types_stack)))
1981         {
1982           param_types_heap = g_new (GType, n_params);
1983           param_types = param_types_heap;
1984         }
1985
1986       for (i = 0; i < n_params; i++)
1987         param_types[i] = va_arg (args, GType);
1988     }
1989
1990   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1991                              class_closure, accumulator, accu_data, c_marshaller,
1992                              return_type, n_params, param_types);
1993   g_free (param_types_heap);
1994
1995   return signal_id;
1996 }
1997
1998 static void
1999 signal_destroy_R (SignalNode *signal_node)
2000 {
2001   SignalNode node = *signal_node;
2002
2003   signal_node->destroyed = TRUE;
2004   
2005   /* reentrancy caution, zero out real contents first */
2006   signal_node->single_va_closure_is_valid = FALSE;
2007   signal_node->n_params = 0;
2008   signal_node->param_types = NULL;
2009   signal_node->return_type = 0;
2010   signal_node->class_closure_bsa = NULL;
2011   signal_node->accumulator = NULL;
2012   signal_node->c_marshaller = NULL;
2013   signal_node->va_marshaller = NULL;
2014   signal_node->emission_hooks = NULL;
2015   
2016 #ifdef  G_ENABLE_DEBUG
2017   /* check current emissions */
2018   {
2019     Emission *emission;
2020     
2021     for (emission = g_emissions; emission; emission = emission->next)
2022       if (emission->ihint.signal_id == node.signal_id)
2023         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
2024                     node.name, emission->instance);
2025   }
2026 #endif
2027   
2028   /* free contents that need to
2029    */
2030   SIGNAL_UNLOCK ();
2031   g_free (node.param_types);
2032   if (node.class_closure_bsa)
2033     {
2034       guint i;
2035
2036       for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
2037         {
2038           ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
2039
2040           g_closure_unref (cc->closure);
2041         }
2042       g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
2043     }
2044   g_free (node.accumulator);
2045   if (node.emission_hooks)
2046     {
2047       g_hook_list_clear (node.emission_hooks);
2048       g_free (node.emission_hooks);
2049     }
2050   SIGNAL_LOCK ();
2051 }
2052
2053 /**
2054  * g_signal_override_class_closure:
2055  * @signal_id: the signal id
2056  * @instance_type: the instance type on which to override the class closure
2057  *  for the signal.
2058  * @class_closure: the closure.
2059  *
2060  * Overrides the class closure (i.e. the default handler) for the given signal
2061  * for emissions on instances of @instance_type. @instance_type must be derived
2062  * from the type to which the signal belongs.
2063  *
2064  * See g_signal_chain_from_overridden() and
2065  * g_signal_chain_from_overridden_handler() for how to chain up to the
2066  * parent class closure from inside the overridden one.
2067  */
2068 void
2069 g_signal_override_class_closure (guint     signal_id,
2070                                  GType     instance_type,
2071                                  GClosure *class_closure)
2072 {
2073   SignalNode *node;
2074   
2075   g_return_if_fail (signal_id > 0);
2076   g_return_if_fail (class_closure != NULL);
2077   
2078   SIGNAL_LOCK ();
2079   node = LOOKUP_SIGNAL_NODE (signal_id);
2080   node_check_deprecated (node);
2081   if (!g_type_is_a (instance_type, node->itype))
2082     g_critical ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2083   else
2084     {
2085       ClassClosure *cc = signal_find_class_closure (node, instance_type);
2086       
2087       if (cc && cc->instance_type == instance_type)
2088         g_critical ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2089       else
2090         signal_add_class_closure (node, instance_type, class_closure);
2091     }
2092   SIGNAL_UNLOCK ();
2093 }
2094
2095 /**
2096  * g_signal_override_class_handler:
2097  * @signal_name: the name for the signal
2098  * @instance_type: the instance type on which to override the class handler
2099  *  for the signal.
2100  * @class_handler: the handler.
2101  *
2102  * Overrides the class closure (i.e. the default handler) for the
2103  * given signal for emissions on instances of @instance_type with
2104  * callback @class_handler. @instance_type must be derived from the
2105  * type to which the signal belongs.
2106  *
2107  * See g_signal_chain_from_overridden() and
2108  * g_signal_chain_from_overridden_handler() for how to chain up to the
2109  * parent class closure from inside the overridden one.
2110  *
2111  * Since: 2.18
2112  */
2113 void
2114 g_signal_override_class_handler (const gchar *signal_name,
2115                                  GType        instance_type,
2116                                  GCallback    class_handler)
2117 {
2118   guint signal_id;
2119
2120   g_return_if_fail (signal_name != NULL);
2121   g_return_if_fail (instance_type != G_TYPE_NONE);
2122   g_return_if_fail (class_handler != NULL);
2123
2124   signal_id = g_signal_lookup (signal_name, instance_type);
2125
2126   if (signal_id)
2127     g_signal_override_class_closure (signal_id, instance_type,
2128                                      g_cclosure_new (class_handler, NULL, NULL));
2129   else
2130     g_critical ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
2131                 G_STRLOC, signal_name, instance_type);
2132
2133 }
2134
2135 /**
2136  * g_signal_chain_from_overridden:
2137  * @instance_and_params: (array) the argument list of the signal emission.
2138  *  The first element in the array is a #GValue for the instance the signal
2139  *  is being emitted on. The rest are any arguments to be passed to the signal.
2140  * @return_value: Location for the return value.
2141  *
2142  * Calls the original class closure of a signal. This function should only
2143  * be called from an overridden class closure; see
2144  * g_signal_override_class_closure() and
2145  * g_signal_override_class_handler().
2146  */
2147 void
2148 g_signal_chain_from_overridden (const GValue *instance_and_params,
2149                                 GValue       *return_value)
2150 {
2151   GType chain_type = 0, restore_type = 0;
2152   Emission *emission = NULL;
2153   GClosure *closure = NULL;
2154   guint n_params = 0;
2155   gpointer instance;
2156   
2157   g_return_if_fail (instance_and_params != NULL);
2158   instance = g_value_peek_pointer (instance_and_params);
2159   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2160   
2161   SIGNAL_LOCK ();
2162   emission = emission_find_innermost (instance);
2163   if (emission)
2164     {
2165       SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2166       
2167       g_assert (node != NULL);  /* paranoid */
2168       
2169       /* we should probably do the same parameter checks as g_signal_emit() here.
2170        */
2171       if (emission->chain_type != G_TYPE_NONE)
2172         {
2173           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2174           
2175           g_assert (cc != NULL);        /* closure currently in call stack */
2176
2177           n_params = node->n_params;
2178           restore_type = cc->instance_type;
2179           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2180           if (cc && cc->instance_type != restore_type)
2181             {
2182               closure = cc->closure;
2183               chain_type = cc->instance_type;
2184             }
2185         }
2186       else
2187         g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2188     }
2189   else
2190     g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2191
2192   if (closure)
2193     {
2194       emission->chain_type = chain_type;
2195       SIGNAL_UNLOCK ();
2196       g_closure_invoke (closure,
2197                         return_value,
2198                         n_params + 1,
2199                         instance_and_params,
2200                         &emission->ihint);
2201       SIGNAL_LOCK ();
2202       emission->chain_type = restore_type;
2203     }
2204   SIGNAL_UNLOCK ();
2205 }
2206
2207 /**
2208  * g_signal_chain_from_overridden_handler: (skip)
2209  * @instance: (type GObject.TypeInstance): the instance the signal is being
2210  *    emitted on.
2211  * @...: parameters to be passed to the parent class closure, followed by a
2212  *  location for the return value. If the return type of the signal
2213  *  is %G_TYPE_NONE, the return value location can be omitted.
2214  *
2215  * Calls the original class closure of a signal. This function should
2216  * only be called from an overridden class closure; see
2217  * g_signal_override_class_closure() and
2218  * g_signal_override_class_handler().
2219  *
2220  * Since: 2.18
2221  */
2222 void
2223 g_signal_chain_from_overridden_handler (gpointer instance,
2224                                         ...)
2225 {
2226   GType chain_type = 0, restore_type = 0;
2227   Emission *emission = NULL;
2228   GClosure *closure = NULL;
2229   SignalNode *node = NULL;
2230   guint n_params = 0;
2231
2232   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2233
2234   SIGNAL_LOCK ();
2235   emission = emission_find_innermost (instance);
2236   if (emission)
2237     {
2238       node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2239
2240       g_assert (node != NULL);  /* paranoid */
2241
2242       /* we should probably do the same parameter checks as g_signal_emit() here.
2243        */
2244       if (emission->chain_type != G_TYPE_NONE)
2245         {
2246           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2247
2248           g_assert (cc != NULL);        /* closure currently in call stack */
2249
2250           n_params = node->n_params;
2251           restore_type = cc->instance_type;
2252           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2253           if (cc && cc->instance_type != restore_type)
2254             {
2255               closure = cc->closure;
2256               chain_type = cc->instance_type;
2257             }
2258         }
2259       else
2260         g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2261     }
2262   else
2263     g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2264
2265   if (closure)
2266     {
2267       GValue *instance_and_params;
2268       GType signal_return_type;
2269       GValue *param_values;
2270       va_list var_args;
2271       guint i;
2272
2273       va_start (var_args, instance);
2274
2275       signal_return_type = node->return_type;
2276       instance_and_params = g_newa0 (GValue, n_params + 1);
2277       param_values = instance_and_params + 1;
2278
2279       for (i = 0; i < node->n_params; i++)
2280         {
2281           gchar *error;
2282           GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2283           gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2284
2285           SIGNAL_UNLOCK ();
2286           G_VALUE_COLLECT_INIT (param_values + i, ptype,
2287                                 var_args,
2288                                 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2289                                 &error);
2290           if (error)
2291             {
2292               g_critical ("%s: %s", G_STRLOC, error);
2293               g_free (error);
2294
2295               /* we purposely leak the value here, it might not be
2296                * in a correct state if an error condition occurred
2297                */
2298               while (i--)
2299                 g_value_unset (param_values + i);
2300
2301               va_end (var_args);
2302               return;
2303             }
2304           SIGNAL_LOCK ();
2305         }
2306
2307       SIGNAL_UNLOCK ();
2308       g_value_init_from_instance (instance_and_params, instance);
2309       SIGNAL_LOCK ();
2310
2311       emission->chain_type = chain_type;
2312       SIGNAL_UNLOCK ();
2313
2314       if (signal_return_type == G_TYPE_NONE)
2315         {
2316           g_closure_invoke (closure,
2317                             NULL,
2318                             n_params + 1,
2319                             instance_and_params,
2320                             &emission->ihint);
2321         }
2322       else
2323         {
2324           GValue return_value = G_VALUE_INIT;
2325           gchar *error = NULL;
2326           GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2327           gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2328
2329           g_value_init (&return_value, rtype);
2330
2331           g_closure_invoke (closure,
2332                             &return_value,
2333                             n_params + 1,
2334                             instance_and_params,
2335                             &emission->ihint);
2336
2337           G_VALUE_LCOPY (&return_value,
2338                          var_args,
2339                          static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2340                          &error);
2341           if (!error)
2342             {
2343               g_value_unset (&return_value);
2344             }
2345           else
2346             {
2347               g_critical ("%s: %s", G_STRLOC, error);
2348               g_free (error);
2349
2350               /* we purposely leak the value here, it might not be
2351                * in a correct state if an error condition occurred
2352                */
2353             }
2354         }
2355
2356       for (i = 0; i < n_params; i++)
2357         g_value_unset (param_values + i);
2358       g_value_unset (instance_and_params);
2359
2360       va_end (var_args);
2361
2362       SIGNAL_LOCK ();
2363       emission->chain_type = restore_type;
2364     }
2365   SIGNAL_UNLOCK ();
2366 }
2367
2368 /**
2369  * g_signal_get_invocation_hint:
2370  * @instance: (type GObject.Object): the instance to query
2371  *
2372  * Returns the invocation hint of the innermost signal emission of instance.
2373  *
2374  * Returns: (transfer none) (nullable): the invocation hint of the innermost
2375  *     signal emission, or %NULL if not found.
2376  */
2377 GSignalInvocationHint*
2378 g_signal_get_invocation_hint (gpointer instance)
2379 {
2380   Emission *emission = NULL;
2381   
2382   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2383
2384   SIGNAL_LOCK ();
2385   emission = emission_find_innermost (instance);
2386   SIGNAL_UNLOCK ();
2387   
2388   return emission ? &emission->ihint : NULL;
2389 }
2390
2391 /**
2392  * g_signal_connect_closure_by_id:
2393  * @instance: (type GObject.Object): the instance to connect to.
2394  * @signal_id: the id of the signal.
2395  * @detail: the detail.
2396  * @closure: (not nullable): the closure to connect.
2397  * @after: whether the handler should be called before or after the
2398  *  default handler of the signal.
2399  *
2400  * Connects a closure to a signal for a particular object.
2401  *
2402  * If @closure is a floating reference (see g_closure_sink()), this function
2403  * takes ownership of @closure.
2404  *
2405  * Returns: the handler ID (always greater than 0 for successful connections)
2406  */
2407 gulong
2408 g_signal_connect_closure_by_id (gpointer  instance,
2409                                 guint     signal_id,
2410                                 GQuark    detail,
2411                                 GClosure *closure,
2412                                 gboolean  after)
2413 {
2414   SignalNode *node;
2415   gulong handler_seq_no = 0;
2416   
2417   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2418   g_return_val_if_fail (signal_id > 0, 0);
2419   g_return_val_if_fail (closure != NULL, 0);
2420   
2421   SIGNAL_LOCK ();
2422   node = LOOKUP_SIGNAL_NODE (signal_id);
2423   if (node)
2424     {
2425       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2426         g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2427       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2428         g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2429       else
2430         {
2431           Handler *handler = handler_new (signal_id, instance, after);
2432
2433           if (G_TYPE_IS_OBJECT (node->itype))
2434             _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2435
2436           handler_seq_no = handler->sequential_number;
2437           handler->detail = detail;
2438           handler->closure = g_closure_ref (closure);
2439           g_closure_sink (closure);
2440           add_invalid_closure_notify (handler, instance);
2441           handler_insert (signal_id, instance, handler);
2442           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2443             {
2444               g_closure_set_marshal (closure, node->c_marshaller);
2445               if (node->va_marshaller)
2446                 _g_closure_set_va_marshal (closure, node->va_marshaller);
2447             }
2448         }
2449     }
2450   else
2451     g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2452   SIGNAL_UNLOCK ();
2453   
2454   return handler_seq_no;
2455 }
2456
2457 /**
2458  * g_signal_connect_closure:
2459  * @instance: (type GObject.Object): the instance to connect to.
2460  * @detailed_signal: a string of the form "signal-name::detail".
2461  * @closure: (not nullable): the closure to connect.
2462  * @after: whether the handler should be called before or after the
2463  *  default handler of the signal.
2464  *
2465  * Connects a closure to a signal for a particular object.
2466  *
2467  * If @closure is a floating reference (see g_closure_sink()), this function
2468  * takes ownership of @closure.
2469  *
2470  * Returns: the handler ID (always greater than 0 for successful connections)
2471  */
2472 gulong
2473 g_signal_connect_closure (gpointer     instance,
2474                           const gchar *detailed_signal,
2475                           GClosure    *closure,
2476                           gboolean     after)
2477 {
2478   guint signal_id;
2479   gulong handler_seq_no = 0;
2480   GQuark detail = 0;
2481   GType itype;
2482
2483   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2484   g_return_val_if_fail (detailed_signal != NULL, 0);
2485   g_return_val_if_fail (closure != NULL, 0);
2486
2487   SIGNAL_LOCK ();
2488   itype = G_TYPE_FROM_INSTANCE (instance);
2489   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2490   if (signal_id)
2491     {
2492       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2493
2494       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2495         g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2496       else if (!g_type_is_a (itype, node->itype))
2497         g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2498                     G_STRLOC, detailed_signal, instance, g_type_name (itype));
2499       else
2500         {
2501           Handler *handler = handler_new (signal_id, instance, after);
2502
2503           if (G_TYPE_IS_OBJECT (node->itype))
2504             _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2505
2506           handler_seq_no = handler->sequential_number;
2507           handler->detail = detail;
2508           handler->closure = g_closure_ref (closure);
2509           g_closure_sink (closure);
2510           add_invalid_closure_notify (handler, instance);
2511           handler_insert (signal_id, instance, handler);
2512           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2513             {
2514               g_closure_set_marshal (handler->closure, node->c_marshaller);
2515               if (node->va_marshaller)
2516                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2517             }
2518         }
2519     }
2520   else
2521     g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2522                 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2523   SIGNAL_UNLOCK ();
2524
2525   return handler_seq_no;
2526 }
2527
2528 static void
2529 node_check_deprecated (const SignalNode *node)
2530 {
2531   static const gchar * g_enable_diagnostic = NULL;
2532
2533   if (G_UNLIKELY (!g_enable_diagnostic))
2534     {
2535       g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2536       if (!g_enable_diagnostic)
2537         g_enable_diagnostic = "0";
2538     }
2539
2540   if (g_enable_diagnostic[0] == '1')
2541     {
2542       if (node->flags & G_SIGNAL_DEPRECATED)
2543         {
2544           g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2545                      "anymore. It will be removed in a future version.",
2546                      type_debug_name (node->itype), node->name);
2547         }
2548     }
2549 }
2550
2551 /**
2552  * g_signal_connect_data:
2553  * @instance: (type GObject.Object): the instance to connect to.
2554  * @detailed_signal: a string of the form "signal-name::detail".
2555  * @c_handler: (not nullable): the #GCallback to connect.
2556  * @data: (nullable) (closure c_handler): data to pass to @c_handler calls.
2557  * @destroy_data: (nullable) (destroy data): a #GClosureNotify for @data.
2558  * @connect_flags: a combination of #GConnectFlags.
2559  *
2560  * Connects a #GCallback function to a signal for a particular object. Similar
2561  * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2562  * which will be called when the signal handler is disconnected and no longer
2563  * used. Specify @connect_flags if you need `..._after()` or
2564  * `..._swapped()` variants of this function.
2565  *
2566  * Returns: the handler ID (always greater than 0 for successful connections)
2567  */
2568 gulong
2569 g_signal_connect_data (gpointer       instance,
2570                        const gchar   *detailed_signal,
2571                        GCallback      c_handler,
2572                        gpointer       data,
2573                        GClosureNotify destroy_data,
2574                        GConnectFlags  connect_flags)
2575 {
2576   guint signal_id;
2577   gulong handler_seq_no = 0;
2578   GQuark detail = 0;
2579   GType itype;
2580   gboolean swapped, after;
2581   
2582   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2583   g_return_val_if_fail (detailed_signal != NULL, 0);
2584   g_return_val_if_fail (c_handler != NULL, 0);
2585
2586   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2587   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2588
2589   SIGNAL_LOCK ();
2590   itype = G_TYPE_FROM_INSTANCE (instance);
2591   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2592   if (signal_id)
2593     {
2594       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2595
2596       node_check_deprecated (node);
2597
2598       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2599         g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2600       else if (!g_type_is_a (itype, node->itype))
2601         g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2602                     G_STRLOC, detailed_signal, instance, g_type_name (itype));
2603       else
2604         {
2605           Handler *handler = handler_new (signal_id, instance, after);
2606
2607           if (G_TYPE_IS_OBJECT (node->itype))
2608             _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2609
2610           handler_seq_no = handler->sequential_number;
2611           handler->detail = detail;
2612           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2613           g_closure_sink (handler->closure);
2614           handler_insert (signal_id, instance, handler);
2615           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2616             {
2617               g_closure_set_marshal (handler->closure, node->c_marshaller);
2618               if (node->va_marshaller)
2619                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2620             }
2621         }
2622     }
2623   else
2624     g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2625                 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2626   SIGNAL_UNLOCK ();
2627
2628   return handler_seq_no;
2629 }
2630
2631 static void
2632 signal_handler_block_unlocked (gpointer instance,
2633                                gulong   handler_id);
2634
2635 /**
2636  * g_signal_handler_block:
2637  * @instance: (type GObject.Object): The instance to block the signal handler of.
2638  * @handler_id: Handler id of the handler to be blocked.
2639  *
2640  * Blocks a handler of an instance so it will not be called during any
2641  * signal emissions unless it is unblocked again. Thus "blocking" a
2642  * signal handler means to temporarily deactivate it, a signal handler
2643  * has to be unblocked exactly the same amount of times it has been
2644  * blocked before to become active again.
2645  *
2646  * The @handler_id has to be a valid signal handler id, connected to a
2647  * signal of @instance.
2648  */
2649 void
2650 g_signal_handler_block (gpointer instance,
2651                         gulong   handler_id)
2652 {
2653   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2654   g_return_if_fail (handler_id > 0);
2655   
2656   SIGNAL_LOCK ();
2657   signal_handler_block_unlocked (instance, handler_id);
2658   SIGNAL_UNLOCK ();
2659 }
2660
2661 static void
2662 signal_handler_block_unlocked (gpointer instance,
2663                                gulong   handler_id)
2664 {
2665   Handler *handler;
2666
2667   handler = handler_lookup (instance, handler_id, NULL, NULL);
2668   if (handler)
2669     {
2670 #ifndef G_DISABLE_CHECKS
2671       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2672         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2673 #endif
2674       handler->block_count += 1;
2675     }
2676   else
2677     g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2678 }
2679
2680 static void
2681 signal_handler_unblock_unlocked (gpointer instance,
2682                                  gulong   handler_id);
2683
2684 /**
2685  * g_signal_handler_unblock:
2686  * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2687  * @handler_id: Handler id of the handler to be unblocked.
2688  *
2689  * Undoes the effect of a previous g_signal_handler_block() call.  A
2690  * blocked handler is skipped during signal emissions and will not be
2691  * invoked, unblocking it (for exactly the amount of times it has been
2692  * blocked before) reverts its "blocked" state, so the handler will be
2693  * recognized by the signal system and is called upon future or
2694  * currently ongoing signal emissions (since the order in which
2695  * handlers are called during signal emissions is deterministic,
2696  * whether the unblocked handler in question is called as part of a
2697  * currently ongoing emission depends on how far that emission has
2698  * proceeded yet).
2699  *
2700  * The @handler_id has to be a valid id of a signal handler that is
2701  * connected to a signal of @instance and is currently blocked.
2702  */
2703 void
2704 g_signal_handler_unblock (gpointer instance,
2705                           gulong   handler_id)
2706 {
2707   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2708   g_return_if_fail (handler_id > 0);
2709   
2710   SIGNAL_LOCK ();
2711   signal_handler_unblock_unlocked (instance, handler_id);
2712   SIGNAL_UNLOCK ();
2713 }
2714
2715 static void
2716 signal_handler_unblock_unlocked (gpointer instance,
2717                                  gulong   handler_id)
2718 {
2719   Handler *handler;
2720
2721   handler = handler_lookup (instance, handler_id, NULL, NULL);
2722   if (handler)
2723     {
2724       if (handler->block_count)
2725         handler->block_count -= 1;
2726       else
2727         g_critical (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2728     }
2729   else
2730     g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2731 }
2732
2733 static void
2734 signal_handler_disconnect_unlocked (gpointer instance,
2735                                     gulong   handler_id);
2736
2737 /**
2738  * g_signal_handler_disconnect:
2739  * @instance: (type GObject.Object): The instance to remove the signal handler from.
2740  * @handler_id: Handler id of the handler to be disconnected.
2741  *
2742  * Disconnects a handler from an instance so it will not be called during
2743  * any future or currently ongoing emissions of the signal it has been
2744  * connected to. The @handler_id becomes invalid and may be reused.
2745  *
2746  * The @handler_id has to be a valid signal handler id, connected to a
2747  * signal of @instance.
2748  */
2749 void
2750 g_signal_handler_disconnect (gpointer instance,
2751                              gulong   handler_id)
2752 {
2753   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2754   g_return_if_fail (handler_id > 0);
2755   
2756   SIGNAL_LOCK ();
2757   signal_handler_disconnect_unlocked (instance, handler_id);
2758   SIGNAL_UNLOCK ();
2759 }
2760
2761 static void
2762 signal_handler_disconnect_unlocked (gpointer instance,
2763                                     gulong   handler_id)
2764 {
2765   Handler *handler;
2766
2767   handler = handler_lookup (instance, handler_id, 0, 0);
2768   if (handler)
2769     {
2770       g_hash_table_remove (g_handlers, handler);
2771       handler->sequential_number = 0;
2772       handler->block_count = 1;
2773       remove_invalid_closure_notify (handler, instance);
2774       handler_unref_R (handler->signal_id, instance, handler);
2775     }
2776   else
2777     g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2778 }
2779
2780 /**
2781  * g_signal_handler_is_connected:
2782  * @instance: (type GObject.Object): The instance where a signal handler is sought.
2783  * @handler_id: the handler ID.
2784  *
2785  * Returns whether @handler_id is the ID of a handler connected to @instance.
2786  *
2787  * Returns: whether @handler_id identifies a handler connected to @instance.
2788  */
2789 gboolean
2790 g_signal_handler_is_connected (gpointer instance,
2791                                gulong   handler_id)
2792 {
2793   Handler *handler;
2794   gboolean connected;
2795
2796   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2797
2798   SIGNAL_LOCK ();
2799   handler = handler_lookup (instance, handler_id, NULL, NULL);
2800   connected = handler != NULL;
2801   SIGNAL_UNLOCK ();
2802
2803   return connected;
2804 }
2805
2806 /**
2807  * g_signal_handlers_destroy:
2808  * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2809  *
2810  * Destroy all signal handlers of a type instance. This function is
2811  * an implementation detail of the #GObject dispose implementation,
2812  * and should not be used outside of the type system.
2813  */
2814 void
2815 g_signal_handlers_destroy (gpointer instance)
2816 {
2817   GBSearchArray *hlbsa;
2818   
2819   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2820   
2821   SIGNAL_LOCK ();
2822   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2823   if (hlbsa)
2824     {
2825       guint i;
2826       
2827       /* reentrancy caution, delete instance trace first */
2828       g_hash_table_remove (g_handler_list_bsa_ht, instance);
2829       
2830       for (i = 0; i < hlbsa->n_nodes; i++)
2831         {
2832           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2833           Handler *handler = hlist->handlers;
2834           
2835           while (handler)
2836             {
2837               Handler *tmp = handler;
2838               
2839               handler = tmp->next;
2840               tmp->block_count = 1;
2841               /* cruel unlink, this works because _all_ handlers vanish */
2842               tmp->next = NULL;
2843               tmp->prev = tmp;
2844               if (tmp->sequential_number)
2845                 {
2846                   g_hash_table_remove (g_handlers, tmp);
2847                   remove_invalid_closure_notify (tmp, instance);
2848                   tmp->sequential_number = 0;
2849                   handler_unref_R (0, NULL, tmp);
2850                 }
2851             }
2852         }
2853       g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2854     }
2855   SIGNAL_UNLOCK ();
2856 }
2857
2858 /**
2859  * g_signal_handler_find:
2860  * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2861  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2862  *  and/or @data the handler has to match.
2863  * @signal_id: Signal the handler has to be connected to.
2864  * @detail: Signal detail the handler has to be connected to.
2865  * @closure: (nullable): The closure the handler will invoke.
2866  * @func: The C closure callback of the handler (useless for non-C closures).
2867  * @data: (nullable) (closure closure): The closure data of the handler's closure.
2868  *
2869  * Finds the first signal handler that matches certain selection criteria.
2870  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2871  * flags, and the criteria values are passed as arguments.
2872  * The match @mask has to be non-0 for successful matches.
2873  * If no handler was found, 0 is returned.
2874  *
2875  * Returns: A valid non-0 signal handler id for a successful match.
2876  */
2877 gulong
2878 g_signal_handler_find (gpointer         instance,
2879                        GSignalMatchType mask,
2880                        guint            signal_id,
2881                        GQuark           detail,
2882                        GClosure        *closure,
2883                        gpointer         func,
2884                        gpointer         data)
2885 {
2886   gulong handler_seq_no = 0;
2887   
2888   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2889   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2890   
2891   if (mask & G_SIGNAL_MATCH_MASK)
2892     {
2893       HandlerMatch *mlist;
2894       
2895       SIGNAL_LOCK ();
2896       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2897       if (mlist)
2898         {
2899           handler_seq_no = mlist->handler->sequential_number;
2900           handler_match_free1_R (mlist, instance);
2901         }
2902       SIGNAL_UNLOCK ();
2903     }
2904   
2905   return handler_seq_no;
2906 }
2907
2908 typedef void (*CallbackHandlerFunc) (gpointer instance, gulong handler_seq_no);
2909
2910 static guint
2911 signal_handlers_foreach_matched_unlocked_R (gpointer             instance,
2912                                             GSignalMatchType     mask,
2913                                             guint                signal_id,
2914                                             GQuark               detail,
2915                                             GClosure            *closure,
2916                                             gpointer             func,
2917                                             gpointer             data,
2918                                             CallbackHandlerFunc  callback)
2919 {
2920   HandlerMatch *mlist;
2921   guint n_handlers = 0;
2922
2923   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2924   while (mlist)
2925     {
2926       n_handlers++;
2927       if (mlist->handler->sequential_number)
2928         callback (instance, mlist->handler->sequential_number);
2929
2930       mlist = handler_match_free1_R (mlist, instance);
2931     }
2932
2933   return n_handlers;
2934 }
2935
2936 /**
2937  * g_signal_handlers_block_matched:
2938  * @instance: (type GObject.Object): The instance to block handlers from.
2939  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2940  *  and/or @data the handlers have to match.
2941  * @signal_id: Signal the handlers have to be connected to.
2942  * @detail: Signal detail the handlers have to be connected to.
2943  * @closure: (nullable): The closure the handlers will invoke.
2944  * @func: The C closure callback of the handlers (useless for non-C closures).
2945  * @data: (nullable) (closure closure): The closure data of the handlers' closures.
2946  *
2947  * Blocks all handlers on an instance that match a certain selection criteria.
2948  *
2949  * The criteria mask is passed as a combination of #GSignalMatchType flags, and
2950  * the criteria values are passed as arguments. A handler must match on all
2951  * flags set in @mask to be blocked (i.e. the match is conjunctive).
2952  *
2953  * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
2954  * %G_SIGNAL_MATCH_FUNC
2955  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2956  * If no handlers were found, 0 is returned, the number of blocked handlers
2957  * otherwise.
2958  *
2959  * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
2960  *
2961  * Returns: The number of handlers that matched.
2962  */
2963 guint
2964 g_signal_handlers_block_matched (gpointer         instance,
2965                                  GSignalMatchType mask,
2966                                  guint            signal_id,
2967                                  GQuark           detail,
2968                                  GClosure        *closure,
2969                                  gpointer         func,
2970                                  gpointer         data)
2971 {
2972   guint n_handlers = 0;
2973   
2974   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2975   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2976   
2977   if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2978     {
2979       SIGNAL_LOCK ();
2980       n_handlers =
2981         signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
2982                                                     closure, func, data,
2983                                                     signal_handler_block_unlocked);
2984       SIGNAL_UNLOCK ();
2985     }
2986   
2987   return n_handlers;
2988 }
2989
2990 /**
2991  * g_signal_handlers_unblock_matched:
2992  * @instance: (type GObject.Object): The instance to unblock handlers from.
2993  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2994  *  and/or @data the handlers have to match.
2995  * @signal_id: Signal the handlers have to be connected to.
2996  * @detail: Signal detail the handlers have to be connected to.
2997  * @closure: (nullable): The closure the handlers will invoke.
2998  * @func: The C closure callback of the handlers (useless for non-C closures).
2999  * @data: (nullable) (closure closure): The closure data of the handlers' closures.
3000  *
3001  * Unblocks all handlers on an instance that match a certain selection
3002  * criteria.
3003  *
3004  * The criteria mask is passed as a combination of #GSignalMatchType flags, and
3005  * the criteria values are passed as arguments. A handler must match on all
3006  * flags set in @mask to be unblocked (i.e. the match is conjunctive).
3007  *
3008  * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
3009  * %G_SIGNAL_MATCH_FUNC
3010  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
3011  * If no handlers were found, 0 is returned, the number of unblocked handlers
3012  * otherwise. The match criteria should not apply to any handlers that are
3013  * not currently blocked.
3014  *
3015  * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
3016  *
3017  * Returns: The number of handlers that matched.
3018  */
3019 guint
3020 g_signal_handlers_unblock_matched (gpointer         instance,
3021                                    GSignalMatchType mask,
3022                                    guint            signal_id,
3023                                    GQuark           detail,
3024                                    GClosure        *closure,
3025                                    gpointer         func,
3026                                    gpointer         data)
3027 {
3028   guint n_handlers = 0;
3029   
3030   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3031   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
3032   
3033   if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
3034     {
3035       SIGNAL_LOCK ();
3036       n_handlers =
3037         signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
3038                                                     closure, func, data,
3039                                                     signal_handler_unblock_unlocked);
3040       SIGNAL_UNLOCK ();
3041     }
3042   
3043   return n_handlers;
3044 }
3045
3046 /**
3047  * g_signal_handlers_disconnect_matched:
3048  * @instance: (type GObject.Object): The instance to remove handlers from.
3049  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
3050  *  and/or @data the handlers have to match.
3051  * @signal_id: Signal the handlers have to be connected to.
3052  * @detail: Signal detail the handlers have to be connected to.
3053  * @closure: (nullable): The closure the handlers will invoke.
3054  * @func: The C closure callback of the handlers (useless for non-C closures).
3055  * @data: (nullable) (closure closure): The closure data of the handlers' closures.
3056  *
3057  * Disconnects all handlers on an instance that match a certain
3058  * selection criteria.
3059  *
3060  * The criteria mask is passed as a combination of #GSignalMatchType flags, and
3061  * the criteria values are passed as arguments. A handler must match on all
3062  * flags set in @mask to be disconnected (i.e. the match is conjunctive).
3063  *
3064  * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
3065  * %G_SIGNAL_MATCH_FUNC or
3066  * %G_SIGNAL_MATCH_DATA match flags is required for successful
3067  * matches.  If no handlers were found, 0 is returned, the number of
3068  * disconnected handlers otherwise.
3069  *
3070  * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
3071  *
3072  * Returns: The number of handlers that matched.
3073  */
3074 guint
3075 g_signal_handlers_disconnect_matched (gpointer         instance,
3076                                       GSignalMatchType mask,
3077                                       guint            signal_id,
3078                                       GQuark           detail,
3079                                       GClosure        *closure,
3080                                       gpointer         func,
3081                                       gpointer         data)
3082 {
3083   guint n_handlers = 0;
3084   
3085   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3086   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
3087   
3088   if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
3089     {
3090       SIGNAL_LOCK ();
3091       n_handlers =
3092         signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
3093                                                     closure, func, data,
3094                                                     signal_handler_disconnect_unlocked);
3095       SIGNAL_UNLOCK ();
3096     }
3097   
3098   return n_handlers;
3099 }
3100
3101 /**
3102  * g_signal_has_handler_pending:
3103  * @instance: (type GObject.Object): the object whose signal handlers are sought.
3104  * @signal_id: the signal id.
3105  * @detail: the detail.
3106  * @may_be_blocked: whether blocked handlers should count as match.
3107  *
3108  * Returns whether there are any handlers connected to @instance for the
3109  * given signal id and detail.
3110  *
3111  * If @detail is 0 then it will only match handlers that were connected
3112  * without detail.  If @detail is non-zero then it will match handlers
3113  * connected both without detail and with the given detail.  This is
3114  * consistent with how a signal emitted with @detail would be delivered
3115  * to those handlers.
3116  *
3117  * Since 2.46 this also checks for a non-default class closure being
3118  * installed, as this is basically always what you want.
3119  *
3120  * One example of when you might use this is when the arguments to the
3121  * signal are difficult to compute. A class implementor may opt to not
3122  * emit the signal if no one is attached anyway, thus saving the cost
3123  * of building the arguments.
3124  *
3125  * Returns: %TRUE if a handler is connected to the signal, %FALSE
3126  *          otherwise.
3127  */
3128 gboolean
3129 g_signal_has_handler_pending (gpointer instance,
3130                               guint    signal_id,
3131                               GQuark   detail,
3132                               gboolean may_be_blocked)
3133 {
3134   HandlerMatch *mlist;
3135   gboolean has_pending;
3136   SignalNode *node;
3137   
3138   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
3139   g_return_val_if_fail (signal_id > 0, FALSE);
3140   
3141   SIGNAL_LOCK ();
3142
3143   node = LOOKUP_SIGNAL_NODE (signal_id);
3144   if (detail)
3145     {
3146       if (!(node->flags & G_SIGNAL_DETAILED))
3147         {
3148           g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3149           SIGNAL_UNLOCK ();
3150           return FALSE;
3151         }
3152     }
3153   mlist = handlers_find (instance,
3154                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
3155                          signal_id, detail, NULL, NULL, NULL, TRUE);
3156   if (mlist)
3157     {
3158       has_pending = TRUE;
3159       handler_match_free1_R (mlist, instance);
3160     }
3161   else
3162     {
3163       ClassClosure *class_closure = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
3164       if (class_closure != NULL && class_closure->instance_type != 0)
3165         has_pending = TRUE;
3166       else
3167         has_pending = FALSE;
3168     }
3169   SIGNAL_UNLOCK ();
3170
3171   return has_pending;
3172 }
3173
3174 static void
3175 signal_emitv_unlocked (const GValue *instance_and_params,
3176                        guint         signal_id,
3177                        GQuark        detail,
3178                        GValue       *return_value);
3179
3180 /**
3181  * g_signal_emitv:
3182  * @instance_and_params: (array): argument list for the signal emission.
3183  *  The first element in the array is a #GValue for the instance the signal
3184  *  is being emitted on. The rest are any arguments to be passed to the signal.
3185  * @signal_id: the signal id
3186  * @detail: the detail
3187  * @return_value: (inout) (optional): Location to
3188  * store the return value of the signal emission. This must be provided if the
3189  * specified signal returns a value, but may be ignored otherwise.
3190  *
3191  * Emits a signal. Signal emission is done synchronously.
3192  * The method will only return control after all handlers are called or signal emission was stopped.
3193  *
3194  * Note that g_signal_emitv() doesn't change @return_value if no handlers are
3195  * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
3196  */
3197 void
3198 g_signal_emitv (const GValue *instance_and_params,
3199                 guint         signal_id,
3200                 GQuark        detail,
3201                 GValue       *return_value)
3202 {
3203   SIGNAL_LOCK ();
3204   signal_emitv_unlocked (instance_and_params, signal_id, detail, return_value);
3205   SIGNAL_UNLOCK ();
3206 }
3207
3208 static void
3209 signal_emitv_unlocked (const GValue *instance_and_params,
3210                        guint         signal_id,
3211                        GQuark        detail,
3212                        GValue       *return_value)
3213 {
3214   gpointer instance;
3215   SignalNode *node;
3216 #ifdef G_ENABLE_DEBUG
3217   const GValue *param_values;
3218   guint i;
3219 #endif
3220   
3221   g_return_if_fail (instance_and_params != NULL);
3222   instance = g_value_peek_pointer (instance_and_params);
3223   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3224   g_return_if_fail (signal_id > 0);
3225
3226 #ifdef G_ENABLE_DEBUG
3227   param_values = instance_and_params + 1;
3228 #endif
3229
3230   node = LOOKUP_SIGNAL_NODE (signal_id);
3231   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3232     {
3233       g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3234       return;
3235     }
3236 #ifdef G_ENABLE_DEBUG
3237   if (detail && !(node->flags & G_SIGNAL_DETAILED))
3238     {
3239       g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3240       return;
3241     }
3242   for (i = 0; i < node->n_params; i++)
3243     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3244       {
3245         g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
3246                     G_STRLOC,
3247                     type_debug_name (node->param_types[i]),
3248                     i,
3249                     node->name,
3250                     G_VALUE_TYPE_NAME (param_values + i));
3251         return;
3252       }
3253   if (node->return_type != G_TYPE_NONE)
3254     {
3255       if (!return_value)
3256         {
3257           g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3258                       G_STRLOC,
3259                       type_debug_name (node->return_type),
3260                       node->name);
3261           return;
3262         }
3263       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3264         {
3265           g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3266                       G_STRLOC,
3267                       type_debug_name (node->return_type),
3268                       node->name,
3269                       G_VALUE_TYPE_NAME (return_value));
3270           return;
3271         }
3272     }
3273   else
3274     return_value = NULL;
3275 #endif  /* G_ENABLE_DEBUG */
3276
3277   /* optimize NOP emissions */
3278   if (!node->single_va_closure_is_valid)
3279     node_update_single_va_closure (node);
3280
3281   if (node->single_va_closure != NULL &&
3282       (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3283        _g_closure_is_void (node->single_va_closure, instance)))
3284     {
3285       HandlerList* hlist;
3286
3287       /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3288       if (_g_object_has_signal_handler ((GObject *)instance))
3289         hlist = handler_list_lookup (node->signal_id, instance);
3290       else
3291         hlist = NULL;
3292
3293       if (hlist == NULL || hlist->handlers == NULL)
3294         {
3295           /* nothing to do to emit this signal */
3296           /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3297           return;
3298         }
3299     }
3300
3301   /* Pass a stable node pointer, whose address can't change even if the
3302    * g_signal_nodes array gets reallocated. */
3303   SignalNode node_copy = *node;
3304   signal_emit_unlocked_R (&node_copy, detail, instance, return_value, instance_and_params);
3305 }
3306
3307 static inline gboolean
3308 accumulate (GSignalInvocationHint *ihint,
3309             GValue                *return_accu,
3310             GValue                *handler_return,
3311             SignalAccumulator     *accumulator)
3312 {
3313   gboolean continue_emission;
3314
3315   if (!accumulator)
3316     return TRUE;
3317
3318   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3319   g_value_reset (handler_return);
3320
3321   ihint->run_type &= ~G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3322
3323   return continue_emission;
3324 }
3325
3326 static gboolean
3327 signal_emit_valist_unlocked (gpointer instance,
3328                              guint    signal_id,
3329                              GQuark   detail,
3330                              va_list  var_args);
3331
3332 /**
3333  * g_signal_emit_valist: (skip)
3334  * @instance: (type GObject.TypeInstance): the instance the signal is being
3335  *    emitted on.
3336  * @signal_id: the signal id
3337  * @detail: the detail
3338  * @var_args: a list of parameters to be passed to the signal, followed by a
3339  *  location for the return value. If the return type of the signal
3340  *  is %G_TYPE_NONE, the return value location can be omitted.
3341  *
3342  * Emits a signal. Signal emission is done synchronously.
3343  * The method will only return control after all handlers are called or signal emission was stopped.
3344  *
3345  * Note that g_signal_emit_valist() resets the return value to the default
3346  * if no handlers are connected, in contrast to g_signal_emitv().
3347  */
3348 void
3349 g_signal_emit_valist (gpointer instance,
3350                       guint    signal_id,
3351                       GQuark   detail,
3352                       va_list  var_args)
3353 {
3354   SIGNAL_LOCK ();
3355   if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args))
3356     SIGNAL_UNLOCK ();
3357 }
3358
3359 /*<private>
3360  * signal_emit_valist_unlocked:
3361  * @instance: The instance to emit from
3362  * @signal_id: Signal id to emit
3363  * @detail: Signal detail
3364  * @var_args: Call arguments
3365  *
3366  * Returns: %TRUE if the signal mutex has been left locked
3367  */
3368 static gboolean
3369 signal_emit_valist_unlocked (gpointer instance,
3370                              guint    signal_id,
3371                              GQuark   detail,
3372                              va_list  var_args)
3373 {
3374   GValue *instance_and_params;
3375   GValue *param_values;
3376   SignalNode *node;
3377   guint i;
3378
3379   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), TRUE);
3380   g_return_val_if_fail (signal_id > 0, TRUE);
3381
3382   node = LOOKUP_SIGNAL_NODE (signal_id);
3383   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3384     {
3385       g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3386       return TRUE;
3387     }
3388 #ifndef G_DISABLE_CHECKS
3389   if (detail && !(node->flags & G_SIGNAL_DETAILED))
3390     {
3391       g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3392       return TRUE;
3393     }
3394 #endif  /* !G_DISABLE_CHECKS */
3395
3396   if (!node->single_va_closure_is_valid)
3397     node_update_single_va_closure (node);
3398
3399   /* There's no need to deep copy this, because a SignalNode instance won't
3400    * ever be destroyed, given that _g_signals_destroy() is not called in any
3401    * real program, however the SignalNode pointer could change, so just store
3402    * the struct contents references, so that we won't try to deference a
3403    * potentially invalid (or changed) pointer;
3404    */
3405   SignalNode node_copy = *node;
3406
3407   if (node->single_va_closure != NULL)
3408     {
3409       HandlerList* hlist;
3410       Handler *fastpath_handler = NULL;
3411       Handler *l;
3412       GClosure *closure = NULL;
3413       gboolean fastpath = TRUE;
3414       GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3415
3416       if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3417           !_g_closure_is_void (node->single_va_closure, instance))
3418         {
3419           if (_g_closure_supports_invoke_va (node->single_va_closure))
3420             {
3421               closure = node->single_va_closure;
3422               if (node->single_va_closure_is_after)
3423                 run_type = G_SIGNAL_RUN_LAST;
3424               else
3425                 run_type = G_SIGNAL_RUN_FIRST;
3426             }
3427           else
3428             fastpath = FALSE;
3429         }
3430
3431       /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3432       if (_g_object_has_signal_handler ((GObject *)instance))
3433         hlist = handler_list_lookup (node->signal_id, instance);
3434       else
3435         hlist = NULL;
3436
3437       for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3438         {
3439           if (!l->block_count &&
3440               (!l->detail || l->detail == detail))
3441             {
3442               if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3443                 {
3444                   fastpath = FALSE;
3445                   break;
3446                 }
3447               else
3448                 {
3449                   fastpath_handler = l;
3450                   closure = l->closure;
3451                   if (l->after)
3452                     run_type = G_SIGNAL_RUN_LAST;
3453                   else
3454                     run_type = G_SIGNAL_RUN_FIRST;
3455                 }
3456             }
3457         }
3458
3459       if (fastpath && closure == NULL && node_copy.return_type == G_TYPE_NONE)
3460         return TRUE;
3461
3462       /* Don't allow no-recurse emission as we might have to restart, which means
3463          we will run multiple handlers and thus must ref all arguments */
3464       if (closure != NULL && (node_copy.flags & (G_SIGNAL_NO_RECURSE)) != 0)
3465         fastpath = FALSE;
3466       
3467       if (fastpath)
3468         {
3469           Emission emission;
3470           GValue *return_accu, accu = G_VALUE_INIT;
3471           GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3472           GValue emission_return = G_VALUE_INIT;
3473           GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3474           gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3475
3476           if (rtype == G_TYPE_NONE)
3477             return_accu = NULL;
3478           else if (node_copy.accumulator)
3479             return_accu = &accu;
3480           else
3481             return_accu = &emission_return;
3482
3483           emission.instance = instance;
3484           emission.ihint.signal_id = signal_id;
3485           emission.ihint.detail = detail;
3486           emission.ihint.run_type = run_type | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3487           emission.state = EMISSION_RUN;
3488           emission.chain_type = instance_type;
3489           emission_push (&emission);
3490
3491           if (fastpath_handler)
3492             handler_ref (fastpath_handler);
3493
3494           if (closure != NULL)
3495             {
3496           TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3497
3498               SIGNAL_UNLOCK ();
3499
3500           if (rtype != G_TYPE_NONE)
3501             g_value_init (&emission_return, rtype);
3502
3503               if (node_copy.accumulator)
3504                 g_value_init (&accu, rtype);
3505
3506               /*
3507                * Coverity doesn’t understand the paired ref/unref here and seems
3508                * to ignore the ref, thus reports every call to g_signal_emit()
3509                * as causing a double-free. That’s incorrect, but I can’t get a
3510                * model file to work for avoiding the false positives, so instead
3511                * comment out the ref/unref when doing static analysis.
3512                */
3513 #ifndef __COVERITY__
3514               g_object_ref (instance);
3515 #endif
3516               _g_closure_invoke_va (closure,
3517                                     return_accu,
3518                                     instance,
3519                                     var_args,
3520                                     node_copy.n_params,
3521                                     node_copy.param_types);
3522               accumulate (&emission.ihint, &emission_return, &accu, node_copy.accumulator);
3523
3524               if (node_copy.accumulator)
3525                 g_value_unset (&accu);
3526
3527               SIGNAL_LOCK ();
3528             }
3529
3530           emission.chain_type = G_TYPE_NONE;
3531           emission_pop (&emission);
3532
3533           if (fastpath_handler)
3534             handler_unref_R (signal_id, instance, fastpath_handler);
3535
3536           SIGNAL_UNLOCK ();
3537
3538           if (rtype != G_TYPE_NONE)
3539             {
3540               gchar *error = NULL;
3541               for (i = 0; i < node_copy.n_params; i++)
3542                 {
3543                   GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3544                   G_VALUE_COLLECT_SKIP (ptype, var_args);
3545                 }
3546
3547               if (closure == NULL)
3548                 g_value_init (&emission_return, rtype);
3549
3550               G_VALUE_LCOPY (&emission_return,
3551                              var_args,
3552                              static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3553                              &error);
3554               if (!error)
3555                 g_value_unset (&emission_return);
3556               else
3557                 {
3558                   g_critical ("%s: %s", G_STRLOC, error);
3559                   g_free (error);
3560                   /* we purposely leak the value here, it might not be
3561                    * in a correct state if an error condition occurred
3562                    */
3563                 }
3564             }
3565           
3566           TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3567
3568           /* See comment above paired ref above */
3569 #ifndef __COVERITY__
3570           if (closure != NULL)
3571             g_object_unref (instance);
3572 #endif
3573
3574           return FALSE;
3575         }
3576     }
3577
3578   SIGNAL_UNLOCK ();
3579
3580   instance_and_params = g_newa0 (GValue, node_copy.n_params + 1);
3581   param_values = instance_and_params + 1;
3582
3583   for (i = 0; i < node_copy.n_params; i++)
3584     {
3585       gchar *error;
3586       GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3587       gboolean static_scope = node_copy.param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3588
3589       G_VALUE_COLLECT_INIT (param_values + i, ptype,
3590                             var_args,
3591                             static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3592                             &error);
3593       if (error)
3594         {
3595           g_critical ("%s: %s", G_STRLOC, error);
3596           g_free (error);
3597
3598           /* we purposely leak the value here, it might not be
3599            * in a correct state if an error condition occurred
3600            */
3601           while (i--)
3602             g_value_unset (param_values + i);
3603
3604           return FALSE;
3605         }
3606     }
3607
3608   g_value_init_from_instance (instance_and_params, instance);
3609   if (node_copy.return_type == G_TYPE_NONE)
3610     {
3611       SIGNAL_LOCK ();
3612       signal_emit_unlocked_R (&node_copy, detail, instance, NULL, instance_and_params);
3613       SIGNAL_UNLOCK ();
3614     }
3615   else
3616     {
3617       GValue return_value = G_VALUE_INIT;
3618       gchar *error = NULL;
3619       GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3620       gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3621       
3622       g_value_init (&return_value, rtype);
3623
3624       SIGNAL_LOCK ();
3625       signal_emit_unlocked_R (&node_copy, detail, instance, &return_value, instance_and_params);
3626       SIGNAL_UNLOCK ();
3627
3628       G_VALUE_LCOPY (&return_value,
3629                      var_args,
3630                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3631                      &error);
3632       if (!error)
3633         g_value_unset (&return_value);
3634       else
3635         {
3636           g_critical ("%s: %s", G_STRLOC, error);
3637           g_free (error);
3638           
3639           /* we purposely leak the value here, it might not be
3640            * in a correct state if an error condition occurred
3641            */
3642         }
3643     }
3644   for (i = 0; i < node_copy.n_params; i++)
3645     g_value_unset (param_values + i);
3646   g_value_unset (instance_and_params);
3647
3648   return FALSE;
3649 }
3650
3651 /**
3652  * g_signal_emit:
3653  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3654  * @signal_id: the signal id
3655  * @detail: the detail
3656  * @...: parameters to be passed to the signal, followed by a
3657  *  location for the return value. If the return type of the signal
3658  *  is %G_TYPE_NONE, the return value location can be omitted.
3659  *
3660  * Emits a signal. Signal emission is done synchronously.
3661  * The method will only return control after all handlers are called or signal emission was stopped.
3662  *
3663  * Note that g_signal_emit() resets the return value to the default
3664  * if no handlers are connected, in contrast to g_signal_emitv().
3665  */
3666 void
3667 g_signal_emit (gpointer instance,
3668                guint    signal_id,
3669                GQuark   detail,
3670                ...)
3671 {
3672   va_list var_args;
3673
3674   va_start (var_args, detail);
3675   g_signal_emit_valist (instance, signal_id, detail, var_args);
3676   va_end (var_args);
3677 }
3678
3679 /**
3680  * g_signal_emit_by_name:
3681  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3682  * @detailed_signal: a string of the form "signal-name::detail".
3683  * @...: parameters to be passed to the signal, followed by a
3684  *  location for the return value. If the return type of the signal
3685  *  is %G_TYPE_NONE, the return value location can be omitted. The
3686  *  number of parameters to pass to this function is defined when creating the signal.
3687  *
3688  * Emits a signal. Signal emission is done synchronously.
3689  * The method will only return control after all handlers are called or signal emission was stopped.
3690  *
3691  * Note that g_signal_emit_by_name() resets the return value to the default
3692  * if no handlers are connected, in contrast to g_signal_emitv().
3693  */
3694 void
3695 g_signal_emit_by_name (gpointer     instance,
3696                        const gchar *detailed_signal,
3697                        ...)
3698 {
3699   GQuark detail = 0;
3700   guint signal_id;
3701   GType itype;
3702
3703   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3704   g_return_if_fail (detailed_signal != NULL);
3705
3706   itype = G_TYPE_FROM_INSTANCE (instance);
3707
3708   SIGNAL_LOCK ();
3709   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3710
3711   if (signal_id)
3712     {
3713       va_list var_args;
3714
3715       va_start (var_args, detailed_signal);
3716       if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args))
3717         SIGNAL_UNLOCK ();
3718       va_end (var_args);
3719     }
3720   else
3721     {
3722       SIGNAL_UNLOCK ();
3723
3724       g_critical ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3725                   G_STRLOC, detailed_signal, instance, g_type_name (itype));
3726     }
3727 }
3728
3729 G_ALWAYS_INLINE static inline GValue *
3730 maybe_init_accumulator_unlocked (SignalNode *node,
3731                                  GValue     *emission_return,
3732                                  GValue     *accumulator_value)
3733 {
3734   if (node->accumulator)
3735     {
3736       if (accumulator_value->g_type)
3737         return accumulator_value;
3738
3739       g_value_init (accumulator_value,
3740                     node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3741       return accumulator_value;
3742     }
3743
3744   return emission_return;
3745 }
3746
3747 static gboolean
3748 signal_emit_unlocked_R (SignalNode   *node,
3749                         GQuark        detail,
3750                         gpointer      instance,
3751                         GValue       *emission_return,
3752                         const GValue *instance_and_params)
3753 {
3754   SignalAccumulator *accumulator;
3755   Emission emission;
3756   GClosure *class_closure;
3757   HandlerList *hlist;
3758   Handler *handler_list = NULL;
3759   GValue *return_accu, accu = G_VALUE_INIT;
3760   guint signal_id;
3761   gulong max_sequential_handler_number;
3762   gboolean return_value_altered = FALSE;
3763   guint n_params;
3764
3765   TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3766
3767   /* We expect this function to be called with a stable SignalNode pointer
3768    * that cannot change location, so accessing its stable members should
3769    * always work even after a lock/unlock.
3770    */
3771   signal_id = node->signal_id;
3772   n_params = node->n_params + 1;
3773
3774   if (node->flags & G_SIGNAL_NO_RECURSE)
3775     {
3776       Emission *emission_node = emission_find (signal_id, detail, instance);
3777
3778       if (emission_node)
3779         {
3780           emission_node->state = EMISSION_RESTART;
3781           return return_value_altered;
3782         }
3783     }
3784   accumulator = node->accumulator;
3785   emission.instance = instance;
3786   emission.ihint.signal_id = node->signal_id;
3787   emission.ihint.detail = detail;
3788   emission.ihint.run_type = 0;
3789   emission.state = 0;
3790   emission.chain_type = G_TYPE_NONE;
3791   emission_push (&emission);
3792   class_closure = signal_lookup_closure (node, instance);
3793   
3794  EMIT_RESTART:
3795   
3796   if (handler_list)
3797     handler_unref_R (signal_id, instance, handler_list);
3798   max_sequential_handler_number = g_handler_sequential_number;
3799   hlist = handler_list_lookup (signal_id, instance);
3800   handler_list = hlist ? hlist->handlers : NULL;
3801   if (handler_list)
3802     handler_ref (handler_list);
3803   
3804   emission.ihint.run_type = G_SIGNAL_RUN_FIRST | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3805   
3806   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3807     {
3808       emission.state = EMISSION_RUN;
3809
3810       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3811       SIGNAL_UNLOCK ();
3812       return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
3813       g_closure_invoke (class_closure,
3814                         return_accu,
3815                         n_params,
3816                         instance_and_params,
3817                         &emission.ihint);
3818       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3819           emission.state == EMISSION_RUN)
3820         emission.state = EMISSION_STOP;
3821       SIGNAL_LOCK ();
3822       emission.chain_type = G_TYPE_NONE;
3823       return_value_altered = TRUE;
3824       
3825       if (emission.state == EMISSION_STOP)
3826         goto EMIT_CLEANUP;
3827       else if (emission.state == EMISSION_RESTART)
3828         goto EMIT_RESTART;
3829     }
3830
3831   if (node->emission_hooks)
3832     {
3833       GHook *hook;
3834       GHook *static_emission_hooks[3];
3835       size_t n_emission_hooks = 0;
3836       const gboolean may_recurse = TRUE;
3837       guint i;
3838
3839       emission.state = EMISSION_HOOK;
3840
3841       /* Quick check to determine whether any hooks match this emission,
3842        * before committing to the more complex work of calling those hooks.
3843        * We save a few of them into a static array, to try to avoid further
3844        * allocations.
3845        */
3846       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3847       while (hook)
3848         {
3849           SignalHook *signal_hook = SIGNAL_HOOK (hook);
3850
3851           if (!signal_hook->detail || signal_hook->detail == detail)
3852             {
3853               if (n_emission_hooks < G_N_ELEMENTS (static_emission_hooks))
3854                 {
3855                   static_emission_hooks[n_emission_hooks] =
3856                     g_hook_ref (node->emission_hooks, hook);
3857                 }
3858
3859               n_emission_hooks += 1;
3860             }
3861
3862           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3863         }
3864
3865       /* Re-iterate back through the matching hooks and copy them into
3866        * an array which won’t change when we unlock to call the
3867        * user-provided hook functions.
3868        * These functions may change hook configuration for this signal,
3869        * add / remove signal handlers, etc.
3870        */
3871       if G_UNLIKELY (n_emission_hooks > 0)
3872         {
3873           guint8 static_hook_returns[G_N_ELEMENTS (static_emission_hooks)];
3874           GHook **emission_hooks = NULL;
3875           guint8 *hook_returns = NULL;
3876
3877           if G_LIKELY (n_emission_hooks <= G_N_ELEMENTS (static_emission_hooks))
3878             {
3879               emission_hooks = static_emission_hooks;
3880               hook_returns = static_hook_returns;
3881             }
3882           else
3883             {
3884               emission_hooks = g_newa (GHook *, n_emission_hooks);
3885               hook_returns = g_newa (guint8, n_emission_hooks);
3886
3887               /* We can't just memcpy the ones we have in the static array,
3888                * to the alloca()'d one because otherwise we'd get an invalid
3889                * ID assertion during unref
3890                */
3891               i = 0;
3892               for (hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3893                    hook != NULL;
3894                    hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse))
3895                 {
3896                   SignalHook *signal_hook = SIGNAL_HOOK (hook);
3897
3898                   if (!signal_hook->detail || signal_hook->detail == detail)
3899                     {
3900                        if (i < G_N_ELEMENTS (static_emission_hooks))
3901                          {
3902                             emission_hooks[i] = g_steal_pointer (&static_emission_hooks[i]);
3903                             g_assert (emission_hooks[i] == hook);
3904                          }
3905                        else
3906                          {
3907                             emission_hooks[i] = g_hook_ref (node->emission_hooks, hook);
3908                          }
3909
3910                       i += 1;
3911                     }
3912                 }
3913
3914                g_assert (i == n_emission_hooks);
3915             }
3916
3917             SIGNAL_UNLOCK ();
3918
3919             for (i = 0; i < n_emission_hooks; ++i)
3920               {
3921                 GSignalEmissionHook hook_func;
3922                 gboolean need_destroy;
3923                 guint old_flags;
3924
3925                 hook = emission_hooks[i];
3926                 hook_func = (GSignalEmissionHook) hook->func;
3927
3928                 old_flags = g_atomic_int_or (&hook->flags, G_HOOK_FLAG_IN_CALL);
3929                 need_destroy = !hook_func (&emission.ihint, n_params,
3930                                            instance_and_params, hook->data);
3931
3932                 if (!(old_flags & G_HOOK_FLAG_IN_CALL))
3933                   {
3934                     g_atomic_int_compare_and_exchange (&hook->flags,
3935                                                        old_flags | G_HOOK_FLAG_IN_CALL,
3936                                                        old_flags);
3937                   }
3938
3939                 hook_returns[i] = !!need_destroy;
3940               }
3941
3942             SIGNAL_LOCK ();
3943
3944             for (i = 0; i < n_emission_hooks; i++)
3945               {
3946                 hook = emission_hooks[i];
3947
3948                 g_hook_unref (node->emission_hooks, hook);
3949
3950                 if (hook_returns[i])
3951                   g_hook_destroy_link (node->emission_hooks, hook);
3952               }
3953         }
3954
3955       if (emission.state == EMISSION_RESTART)
3956         goto EMIT_RESTART;
3957     }
3958   
3959   if (handler_list)
3960     {
3961       Handler *handler = handler_list;
3962       
3963       emission.state = EMISSION_RUN;
3964       handler_ref (handler);
3965       do
3966         {
3967           Handler *tmp;
3968           
3969           if (handler->after)
3970             {
3971               handler_unref_R (signal_id, instance, handler_list);
3972               handler_list = handler;
3973               break;
3974             }
3975           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3976                    handler->sequential_number < max_sequential_handler_number)
3977             {
3978               SIGNAL_UNLOCK ();
3979               return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
3980               g_closure_invoke (handler->closure,
3981                                 return_accu,
3982                                 n_params,
3983                                 instance_and_params,
3984                                 &emission.ihint);
3985               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3986                   emission.state == EMISSION_RUN)
3987                 emission.state = EMISSION_STOP;
3988               SIGNAL_LOCK ();
3989               return_value_altered = TRUE;
3990               
3991               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3992             }
3993           else
3994             tmp = handler->next;
3995           
3996           if (tmp)
3997             handler_ref (tmp);
3998           handler_unref_R (signal_id, instance, handler_list);
3999           handler_list = handler;
4000           handler = tmp;
4001         }
4002       while (handler);
4003       
4004       if (emission.state == EMISSION_STOP)
4005         goto EMIT_CLEANUP;
4006       else if (emission.state == EMISSION_RESTART)
4007         goto EMIT_RESTART;
4008     }
4009   
4010   emission.ihint.run_type &= ~G_SIGNAL_RUN_FIRST;
4011   emission.ihint.run_type |= G_SIGNAL_RUN_LAST;
4012   
4013   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
4014     {
4015       emission.state = EMISSION_RUN;
4016       
4017       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
4018       SIGNAL_UNLOCK ();
4019       return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
4020       g_closure_invoke (class_closure,
4021                         return_accu,
4022                         n_params,
4023                         instance_and_params,
4024                         &emission.ihint);
4025       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4026           emission.state == EMISSION_RUN)
4027         emission.state = EMISSION_STOP;
4028       SIGNAL_LOCK ();
4029       emission.chain_type = G_TYPE_NONE;
4030       return_value_altered = TRUE;
4031       
4032       if (emission.state == EMISSION_STOP)
4033         goto EMIT_CLEANUP;
4034       else if (emission.state == EMISSION_RESTART)
4035         goto EMIT_RESTART;
4036     }
4037   
4038   if (handler_list)
4039     {
4040       Handler *handler = handler_list;
4041       
4042       emission.state = EMISSION_RUN;
4043       handler_ref (handler);
4044       do
4045         {
4046           Handler *tmp;
4047           
4048           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
4049               handler->sequential_number < max_sequential_handler_number)
4050             {
4051               SIGNAL_UNLOCK ();
4052               return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
4053               g_closure_invoke (handler->closure,
4054                                 return_accu,
4055                                 n_params,
4056                                 instance_and_params,
4057                                 &emission.ihint);
4058               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4059                   emission.state == EMISSION_RUN)
4060                 emission.state = EMISSION_STOP;
4061               SIGNAL_LOCK ();
4062               return_value_altered = TRUE;
4063               
4064               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
4065             }
4066           else
4067             tmp = handler->next;
4068           
4069           if (tmp)
4070             handler_ref (tmp);
4071           handler_unref_R (signal_id, instance, handler);
4072           handler = tmp;
4073         }
4074       while (handler);
4075       
4076       if (emission.state == EMISSION_STOP)
4077         goto EMIT_CLEANUP;
4078       else if (emission.state == EMISSION_RESTART)
4079         goto EMIT_RESTART;
4080     }
4081   
4082  EMIT_CLEANUP:
4083   
4084   emission.ihint.run_type &= ~G_SIGNAL_RUN_LAST;
4085   emission.ihint.run_type |= G_SIGNAL_RUN_CLEANUP;
4086   
4087   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
4088     {
4089       gboolean need_unset = FALSE;
4090       
4091       emission.state = EMISSION_STOP;
4092       
4093       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
4094       SIGNAL_UNLOCK ();
4095       if (node->return_type != G_TYPE_NONE && !accumulator)
4096         {
4097           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
4098           need_unset = TRUE;
4099         }
4100       g_closure_invoke (class_closure,
4101                         node->return_type != G_TYPE_NONE ? &accu : NULL,
4102                         n_params,
4103                         instance_and_params,
4104                         &emission.ihint);
4105       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4106           emission.state == EMISSION_RUN)
4107         emission.state = EMISSION_STOP;
4108       if (need_unset)
4109         g_value_unset (&accu);
4110       SIGNAL_LOCK ();
4111       return_value_altered = TRUE;
4112
4113       emission.chain_type = G_TYPE_NONE;
4114       
4115       if (emission.state == EMISSION_RESTART)
4116         goto EMIT_RESTART;
4117     }
4118   
4119   if (handler_list)
4120     handler_unref_R (signal_id, instance, handler_list);
4121   
4122   emission_pop (&emission);
4123   if (accumulator)
4124     g_value_unset (&accu);
4125
4126   TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
4127
4128   return return_value_altered;
4129 }
4130
4131 static void
4132 add_invalid_closure_notify (Handler  *handler,
4133                             gpointer  instance)
4134 {
4135   g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
4136   handler->has_invalid_closure_notify = 1;
4137 }
4138
4139 static void
4140 remove_invalid_closure_notify (Handler  *handler,
4141                                gpointer  instance)
4142 {
4143   if (handler->has_invalid_closure_notify)
4144     {
4145       g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
4146       handler->has_invalid_closure_notify = 0;
4147     }
4148 }
4149
4150 static void
4151 invalid_closure_notify (gpointer  instance,
4152                         GClosure *closure)
4153 {
4154   Handler *handler;
4155   guint signal_id;
4156
4157   SIGNAL_LOCK ();
4158
4159   handler = handler_lookup (instance, 0, closure, &signal_id);
4160   /* See https://bugzilla.gnome.org/show_bug.cgi?id=730296 for discussion about this... */
4161   g_assert (handler != NULL);
4162   g_assert (handler->closure == closure);
4163
4164   g_hash_table_remove (g_handlers, handler);
4165   handler->sequential_number = 0;
4166   handler->block_count = 1;
4167   handler_unref_R (signal_id, instance, handler);
4168
4169   SIGNAL_UNLOCK ();
4170 }
4171
4172 static const gchar*
4173 type_debug_name (GType type)
4174 {
4175   if (type)
4176     {
4177       const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
4178       return name ? name : "<unknown>";
4179     }
4180   else
4181     return "<invalid>";
4182 }
4183
4184 /**
4185  * g_signal_accumulator_true_handled:
4186  * @ihint: standard #GSignalAccumulator parameter
4187  * @return_accu: standard #GSignalAccumulator parameter
4188  * @handler_return: standard #GSignalAccumulator parameter
4189  * @dummy: standard #GSignalAccumulator parameter
4190  *
4191  * A predefined #GSignalAccumulator for signals that return a
4192  * boolean values. The behavior that this accumulator gives is
4193  * that a return of %TRUE stops the signal emission: no further
4194  * callbacks will be invoked, while a return of %FALSE allows
4195  * the emission to continue. The idea here is that a %TRUE return
4196  * indicates that the callback handled the signal, and no further
4197  * handling is needed.
4198  *
4199  * Since: 2.4
4200  *
4201  * Returns: standard #GSignalAccumulator result
4202  */
4203 gboolean
4204 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
4205                                    GValue                *return_accu,
4206                                    const GValue          *handler_return,
4207                                    gpointer               dummy)
4208 {
4209   gboolean continue_emission;
4210   gboolean signal_handled;
4211   
4212   signal_handled = g_value_get_boolean (handler_return);
4213   g_value_set_boolean (return_accu, signal_handled);
4214   continue_emission = !signal_handled;
4215   
4216   return continue_emission;
4217 }
4218
4219 /**
4220  * g_signal_accumulator_first_wins:
4221  * @ihint: standard #GSignalAccumulator parameter
4222  * @return_accu: standard #GSignalAccumulator parameter
4223  * @handler_return: standard #GSignalAccumulator parameter
4224  * @dummy: standard #GSignalAccumulator parameter
4225  *
4226  * A predefined #GSignalAccumulator for signals intended to be used as a
4227  * hook for application code to provide a particular value.  Usually
4228  * only one such value is desired and multiple handlers for the same
4229  * signal don't make much sense (except for the case of the default
4230  * handler defined in the class structure, in which case you will
4231  * usually want the signal connection to override the class handler).
4232  *
4233  * This accumulator will use the return value from the first signal
4234  * handler that is run as the return value for the signal and not run
4235  * any further handlers (ie: the first handler "wins").
4236  *
4237  * Returns: standard #GSignalAccumulator result
4238  *
4239  * Since: 2.28
4240  **/
4241 gboolean
4242 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
4243                                  GValue                *return_accu,
4244                                  const GValue          *handler_return,
4245                                  gpointer               dummy)
4246 {
4247   g_value_copy (handler_return, return_accu);
4248   return FALSE;
4249 }
4250
4251 /**
4252  * g_clear_signal_handler:
4253  * @handler_id_ptr: A pointer to a handler ID (of type #gulong) of the handler to be disconnected.
4254  * @instance: (type GObject.Object): The instance to remove the signal handler from.
4255  *   This pointer may be %NULL or invalid, if the handler ID is zero.
4256  *
4257  * Disconnects a handler from @instance so it will not be called during
4258  * any future or currently ongoing emissions of the signal it has been
4259  * connected to. The @handler_id_ptr is then set to zero, which is never a valid handler ID value (see g_signal_connect()).
4260  *
4261  * If the handler ID is 0 then this function does nothing.
4262  *
4263  * There is also a macro version of this function so that the code
4264  * will be inlined.
4265  *
4266  * Since: 2.62
4267  */
4268 void
4269 (g_clear_signal_handler) (gulong   *handler_id_ptr,
4270                           gpointer  instance)
4271 {
4272   g_return_if_fail (handler_id_ptr != NULL);
4273
4274 #ifndef g_clear_signal_handler
4275 #error g_clear_signal_handler() macro is not defined
4276 #endif
4277
4278   g_clear_signal_handler (handler_id_ptr, instance);
4279 }