1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
30 #include "gbsearcharray.h"
31 #include "gvaluecollector.h"
32 #include "gvaluetypes.h"
38 /* pre allocation configurations
40 #define MAX_STACK_VALUES (16)
41 #define HANDLER_PRE_ALLOC (48)
43 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
45 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
46 static volatile gpointer g_trace_instance_signals = NULL;
47 static volatile gpointer g_trap_instance_signals = NULL;
48 #endif /* G_ENABLE_DEBUG */
51 /* --- generic allocation --- */
52 /* we special case allocations generically by replacing
53 * these functions with more speed/memory aware variants
55 #ifndef DISABLE_MEM_POOLS
56 static inline gpointer
57 g_generic_node_alloc (GTrashStack **trash_stack_p,
59 guint nodes_pre_alloc)
61 gpointer node = g_trash_stack_pop (trash_stack_p);
67 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
68 block = g_malloc (sizeof_node * nodes_pre_alloc);
69 while (--nodes_pre_alloc)
71 g_trash_stack_push (trash_stack_p, block);
79 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
80 #else /* !DISABLE_MEM_POOLS */
81 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
82 #define g_generic_node_free(t,node) g_free (node)
83 #endif /* !DISABLE_MEM_POOLS */
86 /* --- typedefs --- */
87 typedef struct _SignalNode SignalNode;
88 typedef struct _SignalKey SignalKey;
89 typedef struct _Emission Emission;
90 typedef struct _Handler Handler;
91 typedef struct _HandlerList HandlerList;
92 typedef struct _HandlerMatch HandlerMatch;
102 /* --- prototypes --- */
103 static inline guint signal_id_lookup (GQuark quark,
105 static void signal_destroy_R (SignalNode *signal_node);
106 static inline HandlerList* handler_list_ensure (guint signal_id,
108 static inline HandlerList* handler_list_lookup (guint signal_id,
110 static inline Handler* handler_new (gboolean after);
111 static void handler_insert (guint signal_id,
114 static Handler* handler_lookup (gpointer instance,
117 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
120 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
122 static HandlerMatch* handlers_find (gpointer instance,
123 GSignalMatchType mask,
129 gboolean one_and_only);
130 static inline void handler_ref (Handler *handler);
131 static inline void handler_unref_R (guint signal_id,
134 static gint handler_lists_cmp (gconstpointer node1,
135 gconstpointer node2);
136 static inline void emission_push (Emission **emission_list_p,
138 static inline void emission_pop (Emission **emission_list_p,
140 static inline Emission* emission_find (Emission *emission_list,
144 static gint class_closures_cmp (gconstpointer node1,
145 gconstpointer node2);
146 static gint signal_key_cmp (gconstpointer node1,
147 gconstpointer node2);
148 static gboolean signal_emit_unlocked_R (SignalNode *node,
151 GValue *return_value,
152 const GValue *instance_and_params);
153 static const gchar * type_debug_name (GType type);
156 /* --- structures --- */
159 GSignalAccumulator func;
167 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
171 /* permanent portion */
177 /* reinitializable portion */
178 guint test_class_offset : 12;
181 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
182 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
183 GBSearchArray *class_closure_bsa;
184 SignalAccumulator *accumulator;
185 GSignalCMarshaller c_marshaller;
186 GHookList *emission_hooks;
188 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
189 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
202 GSignalInvocationHint ihint;
214 gulong sequential_number;
218 guint ref_count : 16;
219 #define HANDLER_MAX_REF_COUNT (1 << 16)
220 guint block_count : 12;
221 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
237 GType instance_type; /* 0 for default closure */
242 /* --- variables --- */
243 static GBSearchArray *g_signal_key_bsa = NULL;
244 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
246 G_BSEARCH_ARRAY_ALIGN_POWER2);
247 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
249 G_BSEARCH_ARRAY_DEFER_SHRINK);
250 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
253 static GHashTable *g_handler_list_bsa_ht = NULL;
254 static Emission *g_recursive_emissions = NULL;
255 static Emission *g_restart_emissions = NULL;
256 #ifndef DISABLE_MEM_POOLS
257 static GTrashStack *g_handler_ts = NULL;
259 static gulong g_handler_sequential_number = 1;
260 G_LOCK_DEFINE_STATIC (g_signal_mutex);
261 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
262 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
265 /* --- signal nodes --- */
266 static guint g_n_signal_nodes = 0;
267 static SignalNode **g_signal_nodes = NULL;
269 static inline SignalNode*
270 LOOKUP_SIGNAL_NODE (register guint signal_id)
272 if (signal_id < g_n_signal_nodes)
273 return g_signal_nodes[signal_id];
279 /* --- functions --- */
281 signal_id_lookup (GQuark quark,
284 GType *ifaces, type = itype;
290 /* try looking up signals for this type and its anchestors */
293 SignalKey *signal_key;
296 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
299 return signal_key->signal_id;
301 type = g_type_parent (type);
305 /* no luck, try interfaces it exports */
306 ifaces = g_type_interfaces (itype, &n_ifaces);
309 SignalKey *signal_key;
311 key.itype = ifaces[n_ifaces];
312 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
317 return signal_key->signal_id;
326 class_closures_cmp (gconstpointer node1,
329 const ClassClosure *c1 = node1, *c2 = node2;
331 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
335 handler_lists_cmp (gconstpointer node1,
338 const HandlerList *hlist1 = node1, *hlist2 = node2;
340 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
343 static inline HandlerList*
344 handler_list_ensure (guint signal_id,
347 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
350 key.signal_id = signal_id;
354 hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
355 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
356 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
360 GBSearchArray *o = hlbsa;
362 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
364 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
366 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
369 static inline HandlerList*
370 handler_list_lookup (guint signal_id,
373 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
376 key.signal_id = signal_id;
378 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
382 handler_lookup (gpointer instance,
386 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
392 for (i = 0; i < hlbsa->n_nodes; i++)
394 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
397 for (handler = hlist->handlers; handler; handler = handler->next)
398 if (handler->sequential_number == handler_id)
401 *signal_id_p = hlist->signal_id;
411 static inline HandlerMatch*
412 handler_match_prepend (HandlerMatch *list,
418 /* yeah, we could use our own memchunk here, introducing yet more
419 * rarely used cached nodes and extra allocation overhead.
420 * instead, we use GList* nodes, since they are exactly the size
421 * we need and are already cached. g_signal_init() asserts this.
423 node = (HandlerMatch*) g_list_alloc ();
424 node->handler = handler;
426 node->d.signal_id = signal_id;
427 handler_ref (handler);
431 static inline HandlerMatch*
432 handler_match_free1_R (HandlerMatch *node,
435 HandlerMatch *next = node->next;
437 handler_unref_R (node->d.signal_id, instance, node->handler);
438 g_list_free_1 ((GList*) node);
444 handlers_find (gpointer instance,
445 GSignalMatchType mask,
451 gboolean one_and_only)
453 HandlerMatch *mlist = NULL;
455 if (mask & G_SIGNAL_MATCH_ID)
457 HandlerList *hlist = handler_list_lookup (signal_id, instance);
459 SignalNode *node = NULL;
461 if (mask & G_SIGNAL_MATCH_FUNC)
463 node = LOOKUP_SIGNAL_NODE (signal_id);
464 if (!node || !node->c_marshaller)
469 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
470 if (handler->sequential_number &&
471 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
472 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
473 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
474 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
475 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
476 handler->closure->meta_marshal == 0 &&
477 ((GCClosure*) handler->closure)->callback == func)))
479 mlist = handler_match_prepend (mlist, handler, signal_id);
486 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
493 for (i = 0; i < hlbsa->n_nodes; i++)
495 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
496 SignalNode *node = NULL;
499 if (!(mask & G_SIGNAL_MATCH_FUNC))
501 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
502 if (!node->c_marshaller)
506 for (handler = hlist->handlers; handler; handler = handler->next)
507 if (handler->sequential_number &&
508 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
509 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
510 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
511 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
512 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
513 handler->closure->meta_marshal == 0 &&
514 ((GCClosure*) handler->closure)->callback == func)))
516 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
527 static inline Handler*
528 handler_new (gboolean after)
530 Handler *handler = g_generic_node_alloc (&g_handler_ts,
533 #ifndef G_DISABLE_CHECKS
534 if (g_handler_sequential_number < 1)
535 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
538 handler->sequential_number = g_handler_sequential_number++;
539 handler->prev = NULL;
540 handler->next = NULL;
542 handler->ref_count = 1;
543 handler->block_count = 0;
544 handler->after = after != FALSE;
545 handler->closure = NULL;
551 handler_ref (Handler *handler)
553 g_return_if_fail (handler->ref_count > 0);
555 #ifndef G_DISABLE_CHECKS
556 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
557 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
560 handler->ref_count += 1;
564 handler_unref_R (guint signal_id,
568 g_return_if_fail (handler->ref_count > 0);
570 handler->ref_count -= 1;
571 if (!handler->ref_count)
574 handler->next->prev = handler->prev;
575 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
576 handler->prev->next = handler->next;
579 HandlerList *hlist = handler_list_lookup (signal_id, instance);
581 hlist->handlers = handler->next;
584 g_closure_unref (handler->closure);
586 g_generic_node_free (&g_handler_ts, handler);
591 handler_insert (guint signal_id,
597 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
599 hlist = handler_list_ensure (signal_id, instance);
600 if (!hlist->handlers)
601 hlist->handlers = handler;
602 else if (hlist->handlers->after && !handler->after)
604 handler->next = hlist->handlers;
605 hlist->handlers->prev = handler;
606 hlist->handlers = handler;
610 Handler *tmp = hlist->handlers;
616 while (tmp->next && !tmp->next->after)
619 tmp->next->prev = handler;
620 handler->next = tmp->next;
627 emission_push (Emission **emission_list_p,
630 emission->next = *emission_list_p;
631 *emission_list_p = emission;
635 emission_pop (Emission **emission_list_p,
638 Emission *node, *last = NULL;
640 for (node = *emission_list_p; node; last = node, node = last->next)
641 if (node == emission)
644 last->next = node->next;
646 *emission_list_p = node->next;
649 g_assert_not_reached ();
652 static inline Emission*
653 emission_find (Emission *emission_list,
660 for (emission = emission_list; emission; emission = emission->next)
661 if (emission->instance == instance &&
662 emission->ihint.signal_id == signal_id &&
663 emission->ihint.detail == detail)
668 static inline Emission*
669 emission_find_innermost (gpointer instance)
671 Emission *emission, *s = NULL, *c = NULL;
673 for (emission = g_restart_emissions; emission; emission = emission->next)
674 if (emission->instance == instance)
679 for (emission = g_recursive_emissions; emission; emission = emission->next)
680 if (emission->instance == instance)
690 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
694 signal_key_cmp (gconstpointer node1,
697 const SignalKey *key1 = node1, *key2 = node2;
699 if (key1->itype == key2->itype)
700 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
702 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
706 g_signal_init (void) /* sync with gtype.c */
709 if (!g_n_signal_nodes)
711 /* handler_id_node_prepend() requires this */
712 g_assert (sizeof (GList) == sizeof (HandlerMatch));
714 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
715 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
716 g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
718 /* invalid (0) signal_id */
719 g_n_signal_nodes = 1;
720 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
721 g_signal_nodes[0] = NULL;
727 _g_signals_destroy (GType itype)
732 for (i = 1; i < g_n_signal_nodes; i++)
734 SignalNode *node = g_signal_nodes[i];
736 if (node->itype == itype)
739 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
741 type_debug_name (node->itype));
743 signal_destroy_R (node);
750 g_signal_stop_emission (gpointer instance,
756 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
757 g_return_if_fail (signal_id > 0);
760 node = LOOKUP_SIGNAL_NODE (signal_id);
761 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
763 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
767 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
769 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
770 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
774 if (emission->state == EMISSION_HOOK)
775 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
776 node->name, instance);
777 else if (emission->state == EMISSION_RUN)
778 emission->state = EMISSION_STOP;
781 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
782 node->name, instance);
785 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
790 signal_finalize_hook (GHookList *hook_list,
793 GDestroyNotify destroy = hook->destroy;
797 hook->destroy = NULL;
799 destroy (hook->data);
805 g_signal_add_emission_hook (guint signal_id,
807 GSignalEmissionHook hook_func,
809 GDestroyNotify data_destroy)
811 static gulong seq_hook_id = 1;
814 SignalHook *signal_hook;
816 g_return_val_if_fail (signal_id > 0, 0);
817 g_return_val_if_fail (hook_func != NULL, 0);
820 node = LOOKUP_SIGNAL_NODE (signal_id);
821 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
823 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
827 if (detail && !(node->flags & G_SIGNAL_DETAILED))
829 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
833 if (!node->emission_hooks)
835 node->emission_hooks = g_new (GHookList, 1);
836 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
837 node->emission_hooks->finalize_hook = signal_finalize_hook;
839 hook = g_hook_alloc (node->emission_hooks);
840 hook->data = hook_data;
841 hook->func = (gpointer) hook_func;
842 hook->destroy = data_destroy;
843 signal_hook = SIGNAL_HOOK (hook);
844 signal_hook->detail = detail;
845 node->emission_hooks->seq_id = seq_hook_id;
846 g_hook_append (node->emission_hooks, hook);
847 seq_hook_id = node->emission_hooks->seq_id;
850 return hook->hook_id;
854 g_signal_remove_emission_hook (guint signal_id,
859 g_return_if_fail (signal_id > 0);
860 g_return_if_fail (hook_id > 0);
863 node = LOOKUP_SIGNAL_NODE (signal_id);
864 if (!node || node->destroyed)
865 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
866 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
867 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
872 signal_parse_name (const gchar *name,
875 gboolean force_quark)
877 const gchar *colon = strchr (name, ':');
882 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
883 if (signal_id && detail_p)
886 else if (colon[1] == ':')
889 guint l = colon - name;
893 memcpy (buffer, name, l);
895 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
899 gchar *signal = g_new (gchar, l + 1);
901 memcpy (signal, name, l);
903 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
907 if (signal_id && detail_p)
908 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
916 g_signal_parse_name (const gchar *detailed_signal,
920 gboolean force_detail_quark)
926 g_return_val_if_fail (detailed_signal != NULL, FALSE);
927 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
930 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
933 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
934 if (!node || node->destroyed ||
935 (detail && !(node->flags & G_SIGNAL_DETAILED)))
939 *signal_id_p = signal_id;
947 g_signal_stop_emission_by_name (gpointer instance,
948 const gchar *detailed_signal)
954 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
955 g_return_if_fail (detailed_signal != NULL);
958 itype = G_TYPE_FROM_INSTANCE (instance);
959 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
962 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
964 if (detail && !(node->flags & G_SIGNAL_DETAILED))
965 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
966 else if (!g_type_is_a (itype, node->itype))
967 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
970 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
971 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
975 if (emission->state == EMISSION_HOOK)
976 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
977 node->name, instance);
978 else if (emission->state == EMISSION_RUN)
979 emission->state = EMISSION_STOP;
982 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
983 node->name, instance);
987 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
992 g_signal_lookup (const gchar *name,
996 g_return_val_if_fail (name != NULL, 0);
997 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1000 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1004 /* give elaborate warnings */
1005 if (!g_type_name (itype))
1006 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1008 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1009 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1010 name, g_type_name (itype));
1011 else if (!g_type_class_peek (itype))
1012 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1013 name, g_type_name (itype));
1020 g_signal_list_ids (GType itype,
1028 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1029 g_return_val_if_fail (n_ids != NULL, NULL);
1032 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1033 n_nodes = g_signal_key_bsa->n_nodes;
1034 result = g_array_new (FALSE, FALSE, sizeof (guint));
1036 for (i = 0; i < n_nodes; i++)
1037 if (keys[i].itype == itype)
1039 const gchar *name = g_quark_to_string (keys[i].quark);
1041 /* Signal names with "_" in them are aliases to the same
1042 * name with "-" instead of "_".
1044 if (!strchr (name, '_'))
1045 g_array_append_val (result, keys[i].signal_id);
1047 *n_ids = result->len;
1051 /* give elaborate warnings */
1052 if (!g_type_name (itype))
1053 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1055 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1056 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1057 g_type_name (itype));
1058 else if (!g_type_class_peek (itype))
1059 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1060 g_type_name (itype));
1063 return (guint*) g_array_free (result, FALSE);
1066 G_CONST_RETURN gchar*
1067 g_signal_name (guint signal_id)
1073 node = LOOKUP_SIGNAL_NODE (signal_id);
1074 name = node ? node->name : NULL;
1081 g_signal_query (guint signal_id,
1082 GSignalQuery *query)
1086 g_return_if_fail (query != NULL);
1089 node = LOOKUP_SIGNAL_NODE (signal_id);
1090 if (!node || node->destroyed)
1091 query->signal_id = 0;
1094 query->signal_id = node->signal_id;
1095 query->signal_name = node->name;
1096 query->itype = node->itype;
1097 query->signal_flags = node->flags;
1098 query->return_type = node->return_type;
1099 query->n_params = node->n_params;
1100 query->param_types = node->param_types;
1106 g_signal_new (const gchar *signal_name,
1108 GSignalFlags signal_flags,
1110 GSignalAccumulator accumulator,
1112 GSignalCMarshaller c_marshaller,
1120 g_return_val_if_fail (signal_name != NULL, 0);
1122 va_start (args, n_params);
1124 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1125 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1126 accumulator, accu_data, c_marshaller,
1127 return_type, n_params, args);
1131 /* optimize NOP emissions with NULL class handlers */
1132 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1133 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1138 node = LOOKUP_SIGNAL_NODE (signal_id);
1139 node->test_class_offset = class_offset;
1146 static inline ClassClosure*
1147 signal_find_class_closure (SignalNode *node,
1150 GBSearchArray *bsa = node->class_closure_bsa;
1157 /* cc->instance_type is 0 for default closure */
1159 key.instance_type = itype;
1160 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1161 while (!cc && key.instance_type)
1163 key.instance_type = g_type_parent (key.instance_type);
1164 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1172 static inline GClosure*
1173 signal_lookup_closure (SignalNode *node,
1174 GTypeInstance *instance)
1178 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1179 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1181 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1182 return cc ? cc->closure : NULL;
1186 signal_add_class_closure (SignalNode *node,
1192 /* can't optimize NOP emissions with overridden class closures */
1193 node->test_class_offset = 0;
1195 if (!node->class_closure_bsa)
1196 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1197 key.instance_type = itype;
1198 key.closure = g_closure_ref (closure);
1199 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1200 &g_class_closure_bconfig,
1203 g_closure_sink (closure);
1204 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1205 g_closure_set_marshal (closure, node->c_marshaller);
1209 g_signal_newv (const gchar *signal_name,
1211 GSignalFlags signal_flags,
1212 GClosure *class_closure,
1213 GSignalAccumulator accumulator,
1215 GSignalCMarshaller c_marshaller,
1224 g_return_val_if_fail (signal_name != NULL, 0);
1225 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1227 g_return_val_if_fail (param_types != NULL, 0);
1228 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1229 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1230 g_return_val_if_fail (accumulator == NULL, 0);
1232 g_return_val_if_fail (accu_data == NULL, 0);
1234 name = g_strdup (signal_name);
1235 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1239 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1240 node = LOOKUP_SIGNAL_NODE (signal_id);
1241 if (node && !node->destroyed)
1243 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1245 type_debug_name (node->itype),
1246 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1251 if (node && node->itype != itype)
1253 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1255 type_debug_name (itype),
1256 type_debug_name (node->itype));
1261 for (i = 0; i < n_params; i++)
1262 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1264 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1265 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1270 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1272 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1273 type_debug_name (return_type), type_debug_name (itype), name);
1278 if (return_type != G_TYPE_NONE &&
1279 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1281 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1282 type_debug_name (itype), name, type_debug_name (return_type));
1288 /* setup permanent portion of signal node */
1293 signal_id = g_n_signal_nodes++;
1294 node = g_new (SignalNode, 1);
1295 node->signal_id = signal_id;
1296 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1297 g_signal_nodes[signal_id] = node;
1298 node->itype = itype;
1301 key.quark = g_quark_from_string (node->name);
1302 key.signal_id = signal_id;
1303 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1304 g_strdelimit (node->name, "_", '-');
1305 key.quark = g_quark_from_static_string (node->name);
1306 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1308 node->destroyed = FALSE;
1309 node->test_class_offset = 0;
1311 /* setup reinitializable portion */
1312 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1313 node->n_params = n_params;
1314 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1315 node->return_type = return_type;
1316 node->class_closure_bsa = NULL;
1319 node->accumulator = g_new (SignalAccumulator, 1);
1320 node->accumulator->func = accumulator;
1321 node->accumulator->data = accu_data;
1324 node->accumulator = NULL;
1325 node->c_marshaller = c_marshaller;
1326 node->emission_hooks = NULL;
1328 signal_add_class_closure (node, 0, class_closure);
1329 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1331 /* optimize NOP emissions */
1332 node->test_class_offset = TEST_CLASS_MAGIC;
1340 g_signal_new_valist (const gchar *signal_name,
1342 GSignalFlags signal_flags,
1343 GClosure *class_closure,
1344 GSignalAccumulator accumulator,
1346 GSignalCMarshaller c_marshaller,
1357 param_types = g_new (GType, n_params);
1359 for (i = 0; i < n_params; i++)
1360 param_types[i] = va_arg (args, GType);
1365 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1366 class_closure, accumulator, accu_data, c_marshaller,
1367 return_type, n_params, param_types);
1368 g_free (param_types);
1374 signal_destroy_R (SignalNode *signal_node)
1376 SignalNode node = *signal_node;
1378 signal_node->destroyed = TRUE;
1380 /* reentrancy caution, zero out real contents first */
1381 signal_node->test_class_offset = 0;
1382 signal_node->n_params = 0;
1383 signal_node->param_types = NULL;
1384 signal_node->return_type = 0;
1385 signal_node->class_closure_bsa = NULL;
1386 signal_node->accumulator = NULL;
1387 signal_node->c_marshaller = NULL;
1388 signal_node->emission_hooks = NULL;
1390 #ifdef G_ENABLE_DEBUG
1391 /* check current emissions */
1395 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1396 emission; emission = emission->next)
1397 if (emission->ihint.signal_id == node.signal_id)
1398 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1399 node.name, emission->instance);
1403 /* free contents that need to
1406 g_free (node.param_types);
1407 if (node.class_closure_bsa)
1411 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1413 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1415 g_closure_unref (cc->closure);
1417 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1419 g_free (node.accumulator);
1420 if (node.emission_hooks)
1422 g_hook_list_clear (node.emission_hooks);
1423 g_free (node.emission_hooks);
1429 g_signal_override_class_closure (guint signal_id,
1430 GType instance_type,
1431 GClosure *class_closure)
1435 g_return_if_fail (signal_id > 0);
1436 g_return_if_fail (class_closure != NULL);
1439 node = LOOKUP_SIGNAL_NODE (signal_id);
1440 if (!g_type_is_a (instance_type, node->itype))
1441 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1444 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1446 if (cc && cc->instance_type == instance_type)
1447 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1449 signal_add_class_closure (node, instance_type, class_closure);
1455 g_signal_chain_from_overridden (const GValue *instance_and_params,
1456 GValue *return_value)
1458 GType chain_type = 0, restore_type = 0;
1459 Emission *emission = NULL;
1460 GClosure *closure = NULL;
1464 g_return_if_fail (instance_and_params != NULL);
1465 instance = g_value_peek_pointer (instance_and_params);
1466 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1469 emission = emission_find_innermost (instance);
1472 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1474 g_assert (node != NULL); /* paranoid */
1476 /* we should probably do the same parameter checks as g_signal_emit() here.
1478 if (emission->chain_type != G_TYPE_NONE)
1480 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1482 g_assert (cc != NULL); /* closure currently in call stack */
1484 n_params = node->n_params;
1485 restore_type = cc->instance_type;
1486 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1487 if (cc && cc->instance_type != restore_type)
1489 closure = cc->closure;
1490 chain_type = cc->instance_type;
1494 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1497 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1500 emission->chain_type = chain_type;
1502 g_closure_invoke (closure,
1505 instance_and_params,
1508 emission->chain_type = restore_type;
1513 GSignalInvocationHint*
1514 g_signal_get_invocation_hint (gpointer instance)
1516 Emission *emission = NULL;
1518 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1521 emission = emission_find_innermost (instance);
1524 return emission ? &emission->ihint : NULL;
1528 g_signal_connect_closure_by_id (gpointer instance,
1535 gulong handler_seq_no = 0;
1537 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1538 g_return_val_if_fail (signal_id > 0, 0);
1539 g_return_val_if_fail (closure != NULL, 0);
1542 node = LOOKUP_SIGNAL_NODE (signal_id);
1545 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1546 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1547 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1548 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1551 Handler *handler = handler_new (after);
1553 handler_seq_no = handler->sequential_number;
1554 handler->detail = detail;
1555 handler->closure = g_closure_ref (closure);
1556 g_closure_sink (closure);
1557 handler_insert (signal_id, instance, handler);
1558 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1559 g_closure_set_marshal (closure, node->c_marshaller);
1563 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1566 return handler_seq_no;
1570 g_signal_connect_closure (gpointer instance,
1571 const gchar *detailed_signal,
1576 gulong handler_seq_no = 0;
1580 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1581 g_return_val_if_fail (detailed_signal != NULL, 0);
1582 g_return_val_if_fail (closure != NULL, 0);
1585 itype = G_TYPE_FROM_INSTANCE (instance);
1586 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1589 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1591 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1592 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1593 else if (!g_type_is_a (itype, node->itype))
1594 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1597 Handler *handler = handler_new (after);
1599 handler_seq_no = handler->sequential_number;
1600 handler->detail = detail;
1601 handler->closure = g_closure_ref (closure);
1602 g_closure_sink (closure);
1603 handler_insert (signal_id, instance, handler);
1604 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1605 g_closure_set_marshal (handler->closure, node->c_marshaller);
1609 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1612 return handler_seq_no;
1616 g_signal_connect_data (gpointer instance,
1617 const gchar *detailed_signal,
1618 GCallback c_handler,
1620 GClosureNotify destroy_data,
1621 GConnectFlags connect_flags)
1624 gulong handler_seq_no = 0;
1627 gboolean swapped, after;
1629 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1630 g_return_val_if_fail (detailed_signal != NULL, 0);
1631 g_return_val_if_fail (c_handler != NULL, 0);
1633 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1634 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1637 itype = G_TYPE_FROM_INSTANCE (instance);
1638 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1641 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1643 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1644 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1645 else if (!g_type_is_a (itype, node->itype))
1646 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1649 Handler *handler = handler_new (after);
1651 handler_seq_no = handler->sequential_number;
1652 handler->detail = detail;
1653 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1654 g_closure_sink (handler->closure);
1655 handler_insert (signal_id, instance, handler);
1656 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1657 g_closure_set_marshal (handler->closure, node->c_marshaller);
1661 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1664 return handler_seq_no;
1668 g_signal_handler_block (gpointer instance,
1673 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1674 g_return_if_fail (handler_id > 0);
1677 handler = handler_lookup (instance, handler_id, NULL);
1680 #ifndef G_DISABLE_CHECKS
1681 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1682 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1684 handler->block_count += 1;
1687 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1692 g_signal_handler_unblock (gpointer instance,
1697 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1698 g_return_if_fail (handler_id > 0);
1701 handler = handler_lookup (instance, handler_id, NULL);
1704 if (handler->block_count)
1705 handler->block_count -= 1;
1707 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1710 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1715 g_signal_handler_disconnect (gpointer instance,
1721 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1722 g_return_if_fail (handler_id > 0);
1725 handler = handler_lookup (instance, handler_id, &signal_id);
1728 handler->sequential_number = 0;
1729 handler->block_count = 1;
1730 handler_unref_R (signal_id, instance, handler);
1733 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1738 g_signal_handler_is_connected (gpointer instance,
1744 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1745 g_return_val_if_fail (handler_id > 0, FALSE);
1748 handler = handler_lookup (instance, handler_id, NULL);
1749 connected = handler != NULL;
1756 g_signal_handlers_destroy (gpointer instance)
1758 GBSearchArray *hlbsa;
1760 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1763 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1768 /* reentrancy caution, delete instance trace first */
1769 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1771 for (i = 0; i < hlbsa->n_nodes; i++)
1773 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1774 Handler *handler = hlist->handlers;
1778 Handler *tmp = handler;
1780 handler = tmp->next;
1781 tmp->block_count = 1;
1782 /* cruel unlink, this works because _all_ handlers vanish */
1785 if (tmp->sequential_number)
1787 tmp->sequential_number = 0;
1788 handler_unref_R (0, NULL, tmp);
1792 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1798 g_signal_handler_find (gpointer instance,
1799 GSignalMatchType mask,
1806 gulong handler_seq_no = 0;
1808 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1809 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1811 if (mask & G_SIGNAL_MATCH_MASK)
1813 HandlerMatch *mlist;
1816 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1819 handler_seq_no = mlist->handler->sequential_number;
1820 handler_match_free1_R (mlist, instance);
1825 return handler_seq_no;
1829 signal_handlers_foreach_matched_R (gpointer instance,
1830 GSignalMatchType mask,
1836 void (*callback) (gpointer instance,
1837 gulong handler_seq_no))
1839 HandlerMatch *mlist;
1840 guint n_handlers = 0;
1842 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1846 if (mlist->handler->sequential_number)
1849 callback (instance, mlist->handler->sequential_number);
1852 mlist = handler_match_free1_R (mlist, instance);
1859 g_signal_handlers_block_matched (gpointer instance,
1860 GSignalMatchType mask,
1867 guint n_handlers = 0;
1869 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1870 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1872 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1875 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1876 closure, func, data,
1877 g_signal_handler_block);
1885 g_signal_handlers_unblock_matched (gpointer instance,
1886 GSignalMatchType mask,
1893 guint n_handlers = 0;
1895 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1896 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1898 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1901 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1902 closure, func, data,
1903 g_signal_handler_unblock);
1911 g_signal_handlers_disconnect_matched (gpointer instance,
1912 GSignalMatchType mask,
1919 guint n_handlers = 0;
1921 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1922 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1924 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1927 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1928 closure, func, data,
1929 g_signal_handler_disconnect);
1937 g_signal_has_handler_pending (gpointer instance,
1940 gboolean may_be_blocked)
1942 HandlerMatch *mlist;
1943 gboolean has_pending;
1945 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1946 g_return_val_if_fail (signal_id > 0, FALSE);
1951 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1953 if (!(node->flags & G_SIGNAL_DETAILED))
1955 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1960 mlist = handlers_find (instance,
1961 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1962 signal_id, detail, NULL, NULL, NULL, TRUE);
1966 handler_match_free1_R (mlist, instance);
1969 has_pending = FALSE;
1975 static inline gboolean
1976 signal_check_skip_emission (SignalNode *node,
1982 /* are we able to check for NULL class handlers? */
1983 if (!node->test_class_offset)
1986 /* are there emission hooks pending? */
1987 if (node->emission_hooks && node->emission_hooks->hooks)
1990 /* is there a non-NULL class handler? */
1991 if (node->test_class_offset != TEST_CLASS_MAGIC)
1993 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
1995 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
1999 /* are signals being debugged? */
2000 #ifdef G_ENABLE_DEBUG
2001 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2003 #endif /* G_ENABLE_DEBUG */
2005 /* is this a no-recurse signal already in emission? */
2006 if (node->flags & G_SIGNAL_NO_RECURSE &&
2007 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2010 /* do we have pending handlers? */
2011 hlist = handler_list_lookup (node->signal_id, instance);
2012 if (hlist && hlist->handlers)
2015 /* none of the above, no emission required */
2020 g_signal_emitv (const GValue *instance_and_params,
2023 GValue *return_value)
2025 const GValue *param_values;
2028 #ifdef G_ENABLE_DEBUG
2032 g_return_if_fail (instance_and_params != NULL);
2033 instance = g_value_peek_pointer (instance_and_params);
2034 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2035 g_return_if_fail (signal_id > 0);
2037 param_values = instance_and_params + 1;
2040 node = LOOKUP_SIGNAL_NODE (signal_id);
2041 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2043 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2047 #ifdef G_ENABLE_DEBUG
2048 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2050 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2054 for (i = 0; i < node->n_params; i++)
2055 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2057 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2059 type_debug_name (node->param_types[i]),
2062 G_VALUE_TYPE_NAME (param_values + i));
2066 if (node->return_type != G_TYPE_NONE)
2070 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2072 type_debug_name (node->return_type),
2077 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2079 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2081 type_debug_name (node->return_type),
2083 G_VALUE_TYPE_NAME (return_value));
2089 return_value = NULL;
2090 #endif /* G_ENABLE_DEBUG */
2092 /* optimize NOP emissions */
2093 if (signal_check_skip_emission (node, instance, detail))
2095 /* nothing to do to emit this signal */
2097 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2102 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2106 g_signal_emit_valist (gpointer instance,
2111 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2112 GType signal_return_type;
2113 GValue *param_values;
2117 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2118 g_return_if_fail (signal_id > 0);
2121 node = LOOKUP_SIGNAL_NODE (signal_id);
2122 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2124 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2128 #ifndef G_DISABLE_CHECKS
2129 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2131 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2135 #endif /* !G_DISABLE_CHECKS */
2137 /* optimize NOP emissions */
2138 if (signal_check_skip_emission (node, instance, detail))
2140 /* nothing to do to emit this signal */
2142 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2146 n_params = node->n_params;
2147 signal_return_type = node->return_type;
2148 if (node->n_params < MAX_STACK_VALUES)
2149 instance_and_params = stack_values;
2152 free_me = g_new (GValue, node->n_params + 1);
2153 instance_and_params = free_me;
2155 param_values = instance_and_params + 1;
2156 for (i = 0; i < node->n_params; i++)
2159 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2160 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2162 param_values[i].g_type = 0;
2164 g_value_init (param_values + i, ptype);
2165 G_VALUE_COLLECT (param_values + i,
2167 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2171 g_warning ("%s: %s", G_STRLOC, error);
2174 /* we purposely leak the value here, it might not be
2175 * in a sane state if an error condition occoured
2178 g_value_unset (param_values + i);
2186 instance_and_params->g_type = 0;
2187 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2188 g_value_set_instance (instance_and_params, instance);
2189 if (signal_return_type == G_TYPE_NONE)
2190 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2193 GValue return_value = { 0, };
2194 gchar *error = NULL;
2195 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2196 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2198 g_value_init (&return_value, rtype);
2200 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2202 G_VALUE_LCOPY (&return_value,
2204 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2207 g_value_unset (&return_value);
2210 g_warning ("%s: %s", G_STRLOC, error);
2213 /* we purposely leak the value here, it might not be
2214 * in a sane state if an error condition occoured
2218 for (i = 0; i < n_params; i++)
2219 g_value_unset (param_values + i);
2220 g_value_unset (instance_and_params);
2226 g_signal_emit (gpointer instance,
2233 va_start (var_args, detail);
2234 g_signal_emit_valist (instance, signal_id, detail, var_args);
2239 g_signal_emit_by_name (gpointer instance,
2240 const gchar *detailed_signal,
2246 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2247 g_return_if_fail (detailed_signal != NULL);
2250 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2257 va_start (var_args, detailed_signal);
2258 g_signal_emit_valist (instance, signal_id, detail, var_args);
2262 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2265 static inline gboolean
2266 accumulate (GSignalInvocationHint *ihint,
2267 GValue *return_accu,
2268 GValue *handler_return,
2269 SignalAccumulator *accumulator)
2271 gboolean continue_emission;
2276 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2277 g_value_reset (handler_return);
2279 return continue_emission;
2283 signal_emit_unlocked_R (SignalNode *node,
2286 GValue *emission_return,
2287 const GValue *instance_and_params)
2289 SignalAccumulator *accumulator;
2291 GClosure *class_closure;
2293 Handler *handler_list = NULL;
2294 GValue *return_accu, accu = { 0, };
2296 gulong max_sequential_handler_number;
2297 gboolean return_value_altered = FALSE;
2299 #ifdef G_ENABLE_DEBUG
2300 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2302 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2303 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2306 if (g_trap_instance_signals == instance)
2309 #endif /* G_ENABLE_DEBUG */
2312 signal_id = node->signal_id;
2313 if (node->flags & G_SIGNAL_NO_RECURSE)
2315 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2319 node->state = EMISSION_RESTART;
2321 return return_value_altered;
2324 accumulator = node->accumulator;
2328 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2329 return_accu = &accu;
2333 return_accu = emission_return;
2334 emission.instance = instance;
2335 emission.ihint.signal_id = node->signal_id;
2336 emission.ihint.detail = detail;
2337 emission.ihint.run_type = 0;
2339 emission.chain_type = G_TYPE_NONE;
2340 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2341 class_closure = signal_lookup_closure (node, instance);
2346 handler_unref_R (signal_id, instance, handler_list);
2347 max_sequential_handler_number = g_handler_sequential_number;
2348 hlist = handler_list_lookup (signal_id, instance);
2349 handler_list = hlist ? hlist->handlers : NULL;
2351 handler_ref (handler_list);
2353 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2355 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2357 emission.state = EMISSION_RUN;
2359 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2361 g_closure_invoke (class_closure,
2364 instance_and_params,
2366 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2367 emission.state == EMISSION_RUN)
2368 emission.state = EMISSION_STOP;
2370 emission.chain_type = G_TYPE_NONE;
2371 return_value_altered = TRUE;
2373 if (emission.state == EMISSION_STOP)
2375 else if (emission.state == EMISSION_RESTART)
2379 if (node->emission_hooks)
2381 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2384 emission.state = EMISSION_HOOK;
2385 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2388 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2390 if (!signal_hook->detail || signal_hook->detail == detail)
2392 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2394 was_in_call = G_HOOK_IN_CALL (hook);
2395 hook->flags |= G_HOOK_FLAG_IN_CALL;
2397 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2400 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2402 g_hook_destroy_link (node->emission_hooks, hook);
2404 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2407 if (emission.state == EMISSION_RESTART)
2413 Handler *handler = handler_list;
2415 emission.state = EMISSION_RUN;
2416 handler_ref (handler);
2423 handler_unref_R (signal_id, instance, handler_list);
2424 handler_list = handler;
2427 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2428 handler->sequential_number < max_sequential_handler_number)
2431 g_closure_invoke (handler->closure,
2434 instance_and_params,
2436 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2437 emission.state == EMISSION_RUN)
2438 emission.state = EMISSION_STOP;
2440 return_value_altered = TRUE;
2442 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2445 tmp = handler->next;
2449 handler_unref_R (signal_id, instance, handler_list);
2450 handler_list = handler;
2455 if (emission.state == EMISSION_STOP)
2457 else if (emission.state == EMISSION_RESTART)
2461 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2463 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2465 emission.state = EMISSION_RUN;
2467 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2469 g_closure_invoke (class_closure,
2472 instance_and_params,
2474 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2475 emission.state == EMISSION_RUN)
2476 emission.state = EMISSION_STOP;
2478 emission.chain_type = G_TYPE_NONE;
2479 return_value_altered = TRUE;
2481 if (emission.state == EMISSION_STOP)
2483 else if (emission.state == EMISSION_RESTART)
2489 Handler *handler = handler_list;
2491 emission.state = EMISSION_RUN;
2492 handler_ref (handler);
2497 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2498 handler->sequential_number < max_sequential_handler_number)
2501 g_closure_invoke (handler->closure,
2504 instance_and_params,
2506 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2507 emission.state == EMISSION_RUN)
2508 emission.state = EMISSION_STOP;
2510 return_value_altered = TRUE;
2512 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2515 tmp = handler->next;
2519 handler_unref_R (signal_id, instance, handler);
2524 if (emission.state == EMISSION_STOP)
2526 else if (emission.state == EMISSION_RESTART)
2532 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2534 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2536 gboolean need_unset = FALSE;
2538 emission.state = EMISSION_STOP;
2540 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2542 if (node->return_type != G_TYPE_NONE && !accumulator)
2544 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2547 g_closure_invoke (class_closure,
2548 node->return_type != G_TYPE_NONE ? &accu : NULL,
2550 instance_and_params,
2553 g_value_unset (&accu);
2555 emission.chain_type = G_TYPE_NONE;
2557 if (emission.state == EMISSION_RESTART)
2562 handler_unref_R (signal_id, instance, handler_list);
2564 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2567 g_value_unset (&accu);
2569 return return_value_altered;
2573 type_debug_name (GType type)
2577 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2578 return name ? name : "<unknown>";
2584 /* --- compile standard marshallers --- */
2585 #include "gobject.h"
2587 #include "gmarshal.c"