1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998, 1999, 2000 Tim Janik and 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.
21 #include "gtypeplugin.h"
25 /* NOTE: some functions (some internal variants and exported ones)
26 * invalidate data portions of the TypeNodes. if external functions/callbacks
27 * are called, pointers to memory maintained by TypeNodes have to be looked up
28 * again. this affects most of the struct TypeNode fields, e.g. ->children or
29 * ->iface_entries (not ->supers[] as of recently), as all those memory portions can
30 * get realloc()ed during callback invocation.
33 * - g_type_from_name() should do an ordered array lookup after fetching the
34 * the quark, instead of a second hashtable lookup.
35 * - speedup checks for virtual types, steal a bit somewhere
38 * - force interface initialization for already existing classes
41 * lock handling issues when calling static functions are indicated by
42 * uppercase letter postfixes, all static functions have to have
43 * one of the below postfixes:
44 * - _I: [Indifferent about locking]
45 * function doesn't care about locks at all
46 * - _U: [Unlocked invocation]
47 * no read or write lock has to be held across function invocation
48 * (locks may be acquired and released during invocation though)
49 * - _L: [Locked invocation]
50 * a write lock or more than 0 read locks have to be held across
52 * - _W: [Write-locked invocation]
53 * a write lock has to be held across function invokation
54 * - _Wm: [Write-locked invocation, mutatable]
55 * like _W, but the write lock might be released and reacquired
56 * during invocation, watch your pointers
59 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
60 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
61 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
62 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
63 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
64 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
65 static const gchar *_action = " invalidly modified type "; \
66 gpointer _arg = (gpointer) (arg); gchar *_tname = (type_name), *_fname = (func); \
68 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
70 g_error ("%s()%s`%s'", _fname, _action, _tname); \
73 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
74 G_TYPE_FLAG_INSTANTIATABLE | \
75 G_TYPE_FLAG_DERIVABLE | \
76 G_TYPE_FLAG_DEEP_DERIVABLE)
77 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT)
80 /* --- typedefs --- */
81 typedef struct _TypeNode TypeNode;
82 typedef struct _CommonData CommonData;
83 typedef struct _IFaceData IFaceData;
84 typedef struct _ClassData ClassData;
85 typedef struct _InstanceData InstanceData;
86 typedef union _TypeData TypeData;
87 typedef struct _IFaceEntry IFaceEntry;
88 typedef struct _IFaceHolder IFaceHolder;
91 /* --- prototypes --- */
92 static inline GTypeFundamentalInfo* type_node_fundamental_info_L (TypeNode *node);
93 static void type_add_flags_W (TypeNode *node,
95 static void type_data_make_W (TypeNode *node,
96 const GTypeInfo *info,
97 const GTypeValueTable *value_table);
98 static inline void type_data_ref_Wm (TypeNode *node);
99 static inline void type_data_unref_Wm (TypeNode *node,
101 static void type_data_last_unref_Wm (GType type,
105 /* --- structures --- */
106 struct _GValue /* kludge, keep in sync with gvalue.h */
113 guint n_children : 12;
116 guint is_classed : 1;
117 guint is_instantiatable : 1;
124 IFaceEntry *iface_entries;
125 IFaceHolder *iface_conformants;
127 GType supers[1]; /* flexible array */
129 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
130 #define MAX_N_SUPERS (255)
131 #define MAX_N_CHILDREN (4095)
132 #define MAX_N_IFACES (511)
133 #define NODE_TYPE(node) (node->supers[0])
134 #define NODE_PARENT_TYPE(node) (node->supers[1])
135 #define NODE_NAME(node) (g_quark_to_string (node->qname))
140 GInterfaceInfo *info;
147 GTypeInterface *vtable;
152 GTypeValueTable *value_table;
158 GBaseInitFunc vtable_init_base;
159 GBaseFinalizeFunc vtable_finalize_base;
165 GBaseInitFunc class_init_base;
166 GBaseFinalizeFunc class_finalize_base;
167 GClassInitFunc class_init;
168 GClassFinalizeFunc class_finalize;
169 gconstpointer class_data;
176 GBaseInitFunc class_init_base;
177 GBaseFinalizeFunc class_finalize_base;
178 GClassInitFunc class_init;
179 GClassFinalizeFunc class_finalize;
180 gconstpointer class_data;
182 guint16 instance_size;
184 GInstanceInitFunc instance_init;
185 GMemChunk *mem_chunk;
192 InstanceData instance;
196 GTypeClassCacheFunc cache_func;
200 /* --- variables --- */
201 static guint static_n_class_cache_funcs = 0;
202 static ClassCacheFunc *static_class_cache_funcs = NULL;
203 static GType static_last_fundamental_id = 0;
204 static GQuark static_quark_type_flags = 0;
207 /* --- externs --- */
208 const char *g_log_domain_gruntime = "GRuntime";
211 /* --- type nodes --- */
212 static GHashTable *static_type_nodes_ht = NULL;
213 static GType *static_branch_seqnos = NULL;
214 static TypeNode ***static_type_nodes = NULL;
216 static inline TypeNode*
217 lookup_type_node_L (register GType utype)
219 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
220 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
222 if (ftype < static_last_fundamental_id && b_seqno < static_branch_seqnos[ftype])
223 return static_type_nodes[ftype][b_seqno];
229 type_node_any_new_W (TypeNode *pnode,
233 GTypeFundamentalFlags type_flags)
235 guint branch_last, n_supers;
238 guint i, node_size = 0;
240 n_supers = pnode ? pnode->n_supers + 1 : 0;
241 branch_last = static_branch_seqnos[ftype]++;
242 type = G_TYPE_DERIVE_ID (ftype, branch_last);
243 g_assert ((type & G_TYPE_FLAG_RESERVED_ID_BIT) == 0);
244 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (static_branch_seqnos[ftype] - 1))
245 static_type_nodes[ftype] = g_renew (TypeNode*, static_type_nodes[ftype], 1 << g_bit_storage (static_branch_seqnos[ftype] - 1));
248 node_size += sizeof (GTypeFundamentalInfo); /* fundamental type info */
249 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
250 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
251 node = g_malloc0 (node_size);
252 if (!pnode) /* offset fundamental types */
253 node = G_STRUCT_MEMBER_P (node, sizeof (GTypeFundamentalInfo));
254 static_type_nodes[ftype][branch_last] = node;
256 node->n_supers = n_supers;
259 node->supers[0] = type;
262 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
263 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
264 node->is_iface = G_TYPE_IS_INTERFACE (type);
268 node->private.iface_conformants = NULL;
270 node->private.iface_entries = NULL;
274 node->supers[0] = type;
275 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
277 node->is_classed = pnode->is_classed;
278 node->is_instantiatable = pnode->is_instantiatable;
279 node->is_iface = pnode->is_iface;
284 node->private.iface_conformants = NULL;
288 node->n_ifaces = pnode->n_ifaces;
289 node->private.iface_entries = g_memdup (pnode->private.iface_entries,
290 sizeof (pnode->private.iface_entries[0]) * node->n_ifaces);
293 i = pnode->n_children++;
294 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
295 pnode->children[i] = type;
298 node->plugin = plugin;
299 node->n_children = 0;
300 node->children = NULL;
302 node->qname = g_quark_from_string (name);
303 node->global_gdata = NULL;
305 g_hash_table_insert (static_type_nodes_ht,
306 GUINT_TO_POINTER (node->qname),
307 GUINT_TO_POINTER (type));
311 static inline GTypeFundamentalInfo*
312 type_node_fundamental_info_L (TypeNode *node)
314 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
316 if (ftype != NODE_TYPE (node))
317 node = lookup_type_node_L (ftype);
319 return node ? G_STRUCT_MEMBER_P (node, - (gssize) sizeof (GTypeFundamentalInfo)) : NULL;
323 type_node_fundamental_new_W (GType ftype,
325 GTypeFundamentalFlags type_flags)
327 GTypeFundamentalInfo *finfo;
331 flast = static_last_fundamental_id;
333 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
335 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
337 static_last_fundamental_id = MAX (static_last_fundamental_id, ftype + 1);
338 if (static_last_fundamental_id > flast)
340 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
341 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
342 for (i = flast; i < static_last_fundamental_id; i++)
344 static_type_nodes[i] = NULL;
345 static_branch_seqnos[i] = 0;
348 g_assert (static_branch_seqnos[ftype] == 0);
350 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
352 finfo = type_node_fundamental_info_L (node);
353 finfo->type_flags = type_flags;
359 type_node_new_W (TypeNode *pnode,
365 g_assert (pnode->n_supers < MAX_N_SUPERS);
366 g_assert (pnode->n_children < MAX_N_CHILDREN);
368 return type_node_any_new_W (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
371 static inline IFaceEntry*
372 type_lookup_iface_entry_L (TypeNode *node,
375 if (iface->is_iface && node->n_ifaces)
377 IFaceEntry *ifaces = node->private.iface_entries - 1;
378 guint n_ifaces = node->n_ifaces;
379 GType iface_type = NODE_TYPE (iface);
386 i = (n_ifaces + 1) / 2;
388 if (iface_type == check->iface_type)
390 else if (iface_type > check->iface_type)
395 else /* if (iface_type < check->iface_type) */
405 type_descriptive_name_L (GType type)
409 TypeNode *node = lookup_type_node_L (type);
411 return node ? NODE_NAME (node) : "<unknown>";
418 type_descriptive_name_U (GType type)
422 G_READ_LOCK (&type_rw_lock);
423 name = type_descriptive_name_L (type);
424 G_READ_UNLOCK (&type_rw_lock);
430 /* --- type consistency checks --- */
432 check_plugin_U (GTypePlugin *plugin,
433 gboolean need_complete_type_info,
434 gboolean need_complete_interface_info,
435 const gchar *type_name)
437 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
441 g_warning ("plugin handle for type `%s' is NULL",
445 if (!G_IS_TYPE_PLUGIN (plugin))
447 g_warning ("plugin pointer (%p) for type `%s' is invalid",
451 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
453 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
457 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
459 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
467 check_type_name_U (const gchar *type_name)
469 static const gchar *extra_chars = "-_+";
470 const gchar *p = type_name;
473 if (!type_name[0] || !type_name[1] || !type_name[2])
475 g_warning ("type name `%s' is too short", type_name);
478 /* check the first letter */
479 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
480 for (p = type_name + 1; *p; p++)
481 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
482 (p[0] >= 'a' && p[0] <= 'z') ||
483 (p[0] >= '0' && p[0] <= '9') ||
484 strchr (extra_chars, p[0]));
487 g_warning ("type name `%s' contains invalid characters", type_name);
490 if (g_type_from_name (type_name))
492 g_warning ("cannot register existing type `%s'", type_name);
500 check_derivation_U (GType parent_type,
501 const gchar *type_name)
504 GTypeFundamentalInfo* finfo;
506 G_READ_LOCK (&type_rw_lock);
507 pnode = lookup_type_node_L (parent_type);
510 G_READ_UNLOCK (&type_rw_lock);
511 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
513 type_descriptive_name_U (parent_type));
516 finfo = type_node_fundamental_info_L (pnode);
517 /* ensure flat derivability */
518 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
520 G_READ_UNLOCK (&type_rw_lock);
521 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
526 /* ensure deep derivability */
527 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
528 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
530 G_READ_UNLOCK (&type_rw_lock);
531 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
536 G_READ_UNLOCK (&type_rw_lock);
542 check_value_table_I (const gchar *type_name,
543 const GTypeValueTable *value_table)
547 else if (value_table->value_init == NULL)
549 if (value_table->value_free || value_table->value_copy ||
550 value_table->value_peek_pointer ||
551 value_table->collect_type || value_table->collect_value ||
552 value_table->lcopy_type || value_table->lcopy_value)
553 g_warning ("cannot handle uninitializable values of type `%s'",
557 else /* value_table->value_init != NULL */
559 if (!value_table->value_free)
562 * g_warning ("missing `value_free()' for type `%s'", type_name);
566 if (!value_table->value_copy)
568 g_warning ("missing `value_copy()' for type `%s'", type_name);
571 if ((value_table->collect_type || value_table->collect_value) &&
572 (!value_table->collect_type || !value_table->collect_value))
574 g_warning ("one of `collect_type' and `collect_value()' is unspecified for type `%s'",
578 if ((value_table->lcopy_type || value_table->lcopy_value) &&
579 (!value_table->lcopy_type || !value_table->lcopy_value))
581 g_warning ("one of `lcopy_type' and `lcopy_value()' is unspecified for type `%s'",
590 check_type_info_L (TypeNode *pnode,
592 const gchar *type_name,
593 const GTypeInfo *info)
595 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (lookup_type_node_L (ftype));
596 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
598 /* check instance members */
599 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
600 (info->instance_size || info->n_preallocs || info->instance_init))
603 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
607 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
611 /* check class & interface members */
612 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
613 (info->class_init || info->class_finalize || info->class_data ||
614 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
617 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
621 g_warning ("cannot create class for `%s' as non-classed fundamental",
625 /* check interface size */
626 if (is_interface && info->class_size < sizeof (GTypeInterface))
628 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
632 /* check class size */
633 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
635 if (info->class_size < sizeof (GTypeClass))
637 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
641 if (pnode && info->class_size < pnode->data->class.class_size)
643 g_warning ("specified class size for type `%s' is smaller "
644 "than the parent type's `%s' class size",
650 /* check instance size */
651 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
653 if (info->instance_size < sizeof (GTypeInstance))
655 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
659 if (pnode && info->instance_size < pnode->data->instance.instance_size)
661 g_warning ("specified instance size for type `%s' is smaller "
662 "than the parent type's `%s' instance size",
673 find_conforming_type_L (TypeNode *pnode,
676 TypeNode *node = NULL;
679 if (type_lookup_iface_entry_L (pnode, iface))
682 for (i = 0; i < pnode->n_children && !node; i++)
683 node = find_conforming_type_L (lookup_type_node_L (pnode->children[i]), iface);
689 check_add_interface_L (GType instance_type,
692 TypeNode *node = lookup_type_node_L (instance_type);
693 TypeNode *iface = lookup_type_node_L (iface_type);
696 if (!node || !node->is_instantiatable)
698 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
699 type_descriptive_name_L (instance_type));
702 if (!iface || !iface->is_iface)
704 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
705 type_descriptive_name_L (iface_type),
709 tnode = lookup_type_node_L (NODE_PARENT_TYPE (iface));
710 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
712 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
718 tnode = find_conforming_type_L (node, iface);
721 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
731 check_interface_info_L (TypeNode *iface,
733 const GInterfaceInfo *info)
735 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
737 g_warning ("interface type `%s' for type `%s' comes without initializer",
739 type_descriptive_name_L (instance_type));
746 /* --- type info (type node data) --- */
748 type_data_make_W (TypeNode *node,
749 const GTypeInfo *info,
750 const GTypeValueTable *value_table)
753 GTypeValueTable *vtable = NULL;
754 guint vtable_size = 0;
756 g_assert (node->data == NULL && info != NULL);
760 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
763 vtable = pnode->data->common.value_table;
766 static const GTypeValueTable zero_vtable = { NULL, };
768 value_table = &zero_vtable;
772 vtable_size = sizeof (GTypeValueTable);
774 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
776 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
778 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
779 data->instance.class_size = info->class_size;
780 data->instance.class_init_base = info->base_init;
781 data->instance.class_finalize_base = info->base_finalize;
782 data->instance.class_init = info->class_init;
783 data->instance.class_finalize = info->class_finalize;
784 data->instance.class_data = info->class_data;
785 data->instance.class = NULL;
786 data->instance.instance_size = info->instance_size;
787 #ifdef DISABLE_MEM_POOLS
788 data->instance.n_preallocs = 0;
789 #else /* !DISABLE_MEM_POOLS */
790 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
791 #endif /* !DISABLE_MEM_POOLS */
792 data->instance.instance_init = info->instance_init;
793 data->instance.mem_chunk = NULL;
795 else if (node->is_classed) /* only classed */
797 data = g_malloc0 (sizeof (ClassData) + vtable_size);
799 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
800 data->class.class_size = info->class_size;
801 data->class.class_init_base = info->base_init;
802 data->class.class_finalize_base = info->base_finalize;
803 data->class.class_init = info->class_init;
804 data->class.class_finalize = info->class_finalize;
805 data->class.class_data = info->class_data;
806 data->class.class = NULL;
808 else if (node->is_iface)
810 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
812 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
813 data->iface.vtable_size = info->class_size;
814 data->iface.vtable_init_base = info->base_init;
815 data->iface.vtable_finalize_base = info->base_finalize;
819 data = g_malloc0 (sizeof (CommonData) + vtable_size);
821 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
825 node->data->common.ref_count = 1;
828 *vtable = *value_table;
829 node->data->common.value_table = vtable;
831 g_assert (node->data->common.value_table != NULL); /* paranoid */
835 type_data_ref_Wm (TypeNode *node)
839 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
841 GTypeValueTable tmp_value_table;
843 g_assert (node->plugin != NULL);
847 type_data_ref_Wm (pnode);
849 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
852 memset (&tmp_info, 0, sizeof (tmp_info));
853 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
855 G_WRITE_UNLOCK (&type_rw_lock);
856 g_type_plugin_use (node->plugin);
857 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
858 G_WRITE_LOCK (&type_rw_lock);
860 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
862 check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
863 type_data_make_W (node, &tmp_info,
864 check_value_table_I (NODE_NAME (node),
865 &tmp_value_table) ? &tmp_value_table : NULL);
869 g_assert (node->data->common.ref_count > 0);
871 node->data->common.ref_count += 1;
876 type_data_unref_Wm (TypeNode *node,
879 g_assert (node->data && node->data->common.ref_count);
881 if (node->data->common.ref_count > 1)
882 node->data->common.ref_count -= 1;
887 g_warning ("static type `%s' unreferenced too often",
892 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
897 type_node_add_iface_entry_W (TypeNode *node,
903 g_assert (node->is_instantiatable && node->n_ifaces < MAX_N_IFACES);
906 node->private.iface_entries = g_renew (IFaceEntry, node->private.iface_entries, node->n_ifaces);
907 entries = node->private.iface_entries;
908 for (i = 0; i < node->n_ifaces - 1; i++)
909 if (entries[i].iface_type > iface_type)
911 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (node->n_ifaces - i - 1));
912 entries[i].iface_type = iface_type;
913 entries[i].vtable = NULL;
915 for (i = 0; i < node->n_children; i++)
916 type_node_add_iface_entry_W (lookup_type_node_L (node->children[i]), iface_type);
920 type_add_interface_W (TypeNode *node,
922 const GInterfaceInfo *info,
925 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
927 /* we must not call any functions of GInterfaceInfo from within here, since
928 * we got most probably called from _within_ a type registration function
930 g_assert (node->is_instantiatable && iface->is_iface && ((info && !plugin) || (!info && plugin)));
932 iholder->next = iface->private.iface_conformants;
933 iface->private.iface_conformants = iholder;
934 iholder->instance_type = NODE_TYPE (node);
935 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
936 iholder->plugin = plugin;
938 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
942 type_iface_retrive_holder_info_Wm (TypeNode *iface,
945 IFaceHolder *iholder = iface->private.iface_conformants;
947 g_assert (iface->is_iface);
949 while (iholder->instance_type != instance_type)
950 iholder = iholder->next;
954 GInterfaceInfo tmp_info;
956 g_assert (iholder->plugin != NULL);
958 type_data_ref_Wm (iface);
960 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
962 memset (&tmp_info, 0, sizeof (tmp_info));
964 G_WRITE_UNLOCK (&type_rw_lock);
965 g_type_plugin_use (iholder->plugin);
966 g_type_plugin_complete_interface_info (iholder->plugin, NODE_TYPE (iface), instance_type, &tmp_info);
967 G_WRITE_LOCK (&type_rw_lock);
969 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
971 check_interface_info_L (iface, instance_type, &tmp_info);
972 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
979 type_iface_blow_holder_info_Wm (TypeNode *iface,
982 IFaceHolder *iholder = iface->private.iface_conformants;
984 g_assert (iface->is_iface);
986 while (iholder->instance_type != instance_type)
987 iholder = iholder->next;
989 if (iholder->info && iholder->plugin)
991 g_free (iholder->info);
992 iholder->info = NULL;
994 G_WRITE_UNLOCK (&type_rw_lock);
995 g_type_plugin_unuse (iholder->plugin);
996 G_WRITE_LOCK (&type_rw_lock);
998 type_data_unref_Wm (iface, FALSE);
1003 /* --- type structure creation/destruction --- */
1005 g_type_create_instance (GType type)
1008 GTypeInstance *instance;
1012 G_READ_LOCK (&type_rw_lock);
1013 node = lookup_type_node_L (type);
1014 G_READ_UNLOCK (&type_rw_lock);
1015 if (!node || !node->is_instantiatable)
1017 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1018 type_descriptive_name_U (type));
1021 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1022 if (G_TYPE_IS_ABSTRACT (type))
1024 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1025 type_descriptive_name_U (type));
1029 class = g_type_class_ref (type);
1031 if (node->data->instance.n_preallocs)
1033 G_WRITE_LOCK (&type_rw_lock);
1034 if (!node->data->instance.mem_chunk)
1035 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1036 node->data->instance.instance_size,
1037 (node->data->instance.instance_size *
1038 node->data->instance.n_preallocs),
1040 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1041 G_WRITE_UNLOCK (&type_rw_lock);
1044 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1045 for (i = node->n_supers; i > 0; i--)
1049 G_READ_LOCK (&type_rw_lock);
1050 pnode = lookup_type_node_L (node->supers[i]);
1051 G_READ_UNLOCK (&type_rw_lock);
1053 if (pnode->data->instance.instance_init)
1055 instance->g_class = pnode->data->instance.class;
1056 pnode->data->instance.instance_init (instance, class);
1059 instance->g_class = class;
1061 if (node->data->instance.instance_init)
1062 node->data->instance.instance_init (instance, class);
1068 g_type_free_instance (GTypeInstance *instance)
1073 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1075 G_READ_LOCK (&type_rw_lock);
1076 class = instance->g_class;
1077 node = lookup_type_node_L (class->g_type);
1078 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1080 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1081 type_descriptive_name_L (class->g_type));
1082 G_READ_UNLOCK (&type_rw_lock);
1085 G_READ_UNLOCK (&type_rw_lock);
1086 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1087 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1089 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1094 instance->g_class = NULL;
1095 memset (instance, 0xaa, node->data->instance.instance_size); // FIXME: debugging hack
1096 if (node->data->instance.n_preallocs)
1098 G_WRITE_LOCK (&type_rw_lock);
1099 g_chunk_free (instance, node->data->instance.mem_chunk);
1100 G_WRITE_UNLOCK (&type_rw_lock);
1105 g_type_class_unref (class);
1109 type_propagate_iface_vtable_W (TypeNode *pnode,
1111 GTypeInterface *vtable)
1113 IFaceEntry *entry = type_lookup_iface_entry_L (pnode, iface);
1116 entry->vtable = vtable;
1117 for (i = 0; i < pnode->n_children; i++)
1119 TypeNode *node = lookup_type_node_L (pnode->children[i]);
1121 type_propagate_iface_vtable_W (node, iface, vtable);
1126 type_iface_vtable_init_Wm (TypeNode *iface,
1129 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1130 IFaceHolder *iholder = type_iface_retrive_holder_info_Wm (iface, NODE_TYPE (node));
1131 GTypeInterface *vtable;
1133 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1135 vtable = g_malloc0 (iface->data->iface.vtable_size);
1136 type_propagate_iface_vtable_W (node, iface, vtable);
1137 vtable->g_type = NODE_TYPE (iface);
1138 vtable->g_instance_type = NODE_TYPE (node);
1140 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1142 G_WRITE_UNLOCK (&type_rw_lock);
1143 if (iface->data->iface.vtable_init_base)
1144 iface->data->iface.vtable_init_base (vtable);
1145 if (iholder->info->interface_init)
1146 iholder->info->interface_init (vtable, iholder->info->interface_data);
1147 G_WRITE_LOCK (&type_rw_lock);
1152 type_iface_vtable_finalize_Wm (TypeNode *iface,
1154 GTypeInterface *vtable)
1156 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1157 IFaceHolder *iholder = iface->private.iface_conformants;
1159 g_assert (entry && entry->vtable == vtable);
1161 while (iholder->instance_type != NODE_TYPE (node))
1162 iholder = iholder->next;
1163 g_assert (iholder && iholder->info);
1165 type_propagate_iface_vtable_W (node, iface, NULL);
1166 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1168 G_WRITE_UNLOCK (&type_rw_lock);
1169 if (iholder->info->interface_finalize)
1170 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1171 if (iface->data->iface.vtable_finalize_base)
1172 iface->data->iface.vtable_finalize_base (vtable);
1173 G_WRITE_LOCK (&type_rw_lock);
1176 vtable->g_instance_type = 0;
1179 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1183 type_class_init_Wm (TypeNode *node,
1186 GSList *slist, *init_slist = NULL;
1192 g_assert (node->is_classed && node->data &&
1193 node->data->class.class_size &&
1194 !node->data->class.class);
1196 class = g_malloc0 (node->data->class.class_size);
1197 node->data->class.class = class;
1201 TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1203 memcpy (class, pclass, pnode->data->class.class_size);
1205 class->g_type = NODE_TYPE (node);
1207 G_WRITE_UNLOCK (&type_rw_lock);
1209 /* stack all base class initialization functions, so we
1210 * call them in ascending order.
1212 G_READ_LOCK (&type_rw_lock);
1213 for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1214 if (bnode->data->class.class_init_base)
1215 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1216 G_READ_UNLOCK (&type_rw_lock);
1217 for (slist = init_slist; slist; slist = slist->next)
1219 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1221 class_init_base (class);
1223 g_slist_free (init_slist);
1225 if (node->data->class.class_init)
1226 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1228 G_WRITE_LOCK (&type_rw_lock);
1230 /* ok, we got the class done, now initialize all interfaces */
1231 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1232 if (!node->private.iface_entries[i].vtable)
1233 entry = node->private.iface_entries + i;
1236 type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node);
1238 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1239 if (!node->private.iface_entries[i].vtable)
1240 entry = node->private.iface_entries + i;
1245 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1250 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1252 g_message ("finalizing interfaces for %sClass `%s'",
1253 type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1254 type_descriptive_name_L (NODE_TYPE (node)));
1256 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1257 if (node->private.iface_entries[i].vtable &&
1258 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1259 entry = node->private.iface_entries + i;
1262 type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable);
1264 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1265 if (node->private.iface_entries[i].vtable &&
1266 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1267 entry = node->private.iface_entries + i;
1272 type_data_finalize_class_U (TypeNode *node,
1275 GTypeClass *class = cdata->class;
1278 g_assert (cdata->class && cdata->common.ref_count == 0);
1280 g_message ("finalizing %sClass `%s'",
1281 type_descriptive_name_U (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1282 type_descriptive_name_U (NODE_TYPE (node)));
1284 if (cdata->class_finalize)
1285 cdata->class_finalize (class, (gpointer) cdata->class_data);
1287 /* call all base class destruction functions in descending order
1289 if (cdata->class_finalize_base)
1290 cdata->class_finalize_base (class);
1291 G_READ_LOCK (&type_rw_lock);
1292 for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1293 if (bnode->data->class.class_finalize_base)
1295 G_READ_UNLOCK (&type_rw_lock);
1296 bnode->data->class.class_finalize_base (class);
1297 G_READ_LOCK (&type_rw_lock);
1299 G_READ_UNLOCK (&type_rw_lock);
1302 g_free (cdata->class);
1306 type_data_last_unref_Wm (GType type,
1309 TypeNode *node = lookup_type_node_L (type);
1311 g_return_if_fail (node != NULL && node->plugin != NULL);
1313 if (!node->data || node->data->common.ref_count == 0)
1315 g_warning ("cannot drop last reference to unreferenced type `%s'",
1316 type_descriptive_name_U (type));
1320 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1324 G_WRITE_UNLOCK (&type_rw_lock);
1325 G_READ_LOCK (&type_rw_lock);
1326 for (i = 0; i < static_n_class_cache_funcs; i++)
1328 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1329 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1330 gboolean need_break;
1332 G_READ_UNLOCK (&type_rw_lock);
1333 need_break = cache_func (cache_data, node->data->class.class);
1334 G_READ_LOCK (&type_rw_lock);
1335 if (!node->data || node->data->common.ref_count == 0)
1336 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1340 G_READ_UNLOCK (&type_rw_lock);
1341 G_WRITE_LOCK (&type_rw_lock);
1344 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1345 node->data->common.ref_count -= 1;
1348 GType ptype = NODE_PARENT_TYPE (node);
1351 node->data->common.ref_count = 0;
1353 if (node->is_instantiatable && node->data->instance.mem_chunk)
1355 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1356 node->data->instance.mem_chunk = NULL;
1360 if (node->is_classed && tdata->class.class)
1363 type_data_finalize_class_ifaces_Wm (node);
1365 G_WRITE_UNLOCK (&type_rw_lock);
1366 type_data_finalize_class_U (node, &tdata->class);
1367 G_WRITE_LOCK (&type_rw_lock);
1375 type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1376 G_WRITE_UNLOCK (&type_rw_lock);
1377 g_type_plugin_unuse (node->plugin);
1378 G_WRITE_LOCK (&type_rw_lock);
1383 g_type_add_class_cache_func (gpointer cache_data,
1384 GTypeClassCacheFunc cache_func)
1388 g_return_if_fail (cache_func != NULL);
1390 G_WRITE_LOCK (&type_rw_lock);
1391 i = static_n_class_cache_funcs++;
1392 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1393 static_class_cache_funcs[i].cache_data = cache_data;
1394 static_class_cache_funcs[i].cache_func = cache_func;
1395 G_WRITE_UNLOCK (&type_rw_lock);
1399 g_type_remove_class_cache_func (gpointer cache_data,
1400 GTypeClassCacheFunc cache_func)
1402 gboolean found_it = FALSE;
1405 g_return_if_fail (cache_func != NULL);
1407 G_WRITE_LOCK (&type_rw_lock);
1408 for (i = 0; i < static_n_class_cache_funcs; i++)
1409 if (static_class_cache_funcs[i].cache_data == cache_data &&
1410 static_class_cache_funcs[i].cache_func == cache_func)
1412 static_n_class_cache_funcs--;
1413 g_memmove (static_class_cache_funcs + i,
1414 static_class_cache_funcs + i + 1,
1415 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1416 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1420 G_WRITE_UNLOCK (&type_rw_lock);
1423 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1424 cache_func, cache_data);
1428 /* --- type registration --- */
1430 g_type_register_fundamental (GType type_id,
1431 const gchar *type_name,
1432 const GTypeInfo *info,
1433 const GTypeFundamentalInfo *finfo,
1436 GTypeFundamentalInfo *node_finfo;
1439 g_return_val_if_fail (type_id > 0, 0);
1440 g_return_val_if_fail (type_name != NULL, 0);
1441 g_return_val_if_fail (info != NULL, 0);
1442 g_return_val_if_fail (finfo != NULL, 0);
1444 if (!check_type_name_U (type_name))
1446 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1448 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1453 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1454 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1456 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1460 G_WRITE_LOCK (&type_rw_lock);
1461 if (lookup_type_node_L (type_id))
1463 G_WRITE_UNLOCK (&type_rw_lock);
1464 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1465 type_descriptive_name_U (type_id),
1470 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1471 node_finfo = type_node_fundamental_info_L (node);
1472 type_add_flags_W (node, flags);
1474 if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1475 type_data_make_W (node, info,
1476 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1477 G_WRITE_UNLOCK (&type_rw_lock);
1479 return NODE_TYPE (node);
1483 g_type_register_static (GType parent_type,
1484 const gchar *type_name,
1485 const GTypeInfo *info,
1488 TypeNode *pnode, *node;
1491 g_return_val_if_fail (parent_type > 0, 0);
1492 g_return_val_if_fail (type_name != NULL, 0);
1493 g_return_val_if_fail (info != NULL, 0);
1495 if (!check_type_name_U (type_name) ||
1496 !check_derivation_U (parent_type, type_name))
1498 if (info->class_finalize)
1500 g_warning ("class finalizer specified for static type `%s'",
1505 G_WRITE_LOCK (&type_rw_lock);
1506 pnode = lookup_type_node_L (parent_type);
1507 type_data_ref_Wm (pnode);
1508 if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1510 node = type_node_new_W (pnode, type_name, NULL);
1511 type_add_flags_W (node, flags);
1512 type = NODE_TYPE (node);
1513 type_data_make_W (node, info,
1514 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1516 G_WRITE_UNLOCK (&type_rw_lock);
1522 g_type_register_dynamic (GType parent_type,
1523 const gchar *type_name,
1524 GTypePlugin *plugin,
1527 TypeNode *pnode, *node;
1530 g_return_val_if_fail (parent_type > 0, 0);
1531 g_return_val_if_fail (type_name != NULL, 0);
1532 g_return_val_if_fail (plugin != NULL, 0);
1534 if (!check_type_name_U (type_name) ||
1535 !check_derivation_U (parent_type, type_name) ||
1536 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1539 G_WRITE_LOCK (&type_rw_lock);
1540 pnode = lookup_type_node_L (parent_type);
1541 node = type_node_new_W (pnode, type_name, plugin);
1542 type_add_flags_W (node, flags);
1543 type = NODE_TYPE (node);
1544 G_WRITE_UNLOCK (&type_rw_lock);
1550 g_type_add_interface_static (GType instance_type,
1551 GType interface_type,
1552 const GInterfaceInfo *info)
1554 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1555 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1556 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1558 G_WRITE_LOCK (&type_rw_lock);
1559 if (check_add_interface_L (instance_type, interface_type))
1561 TypeNode *node = lookup_type_node_L (instance_type);
1562 TypeNode *iface = lookup_type_node_L (interface_type);
1564 if (check_interface_info_L (iface, NODE_TYPE (node), info))
1565 type_add_interface_W (node, iface, info, NULL);
1567 G_WRITE_UNLOCK (&type_rw_lock);
1571 g_type_add_interface_dynamic (GType instance_type,
1572 GType interface_type,
1573 GTypePlugin *plugin)
1577 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1578 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1579 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1581 G_READ_LOCK (&type_rw_lock);
1582 node = lookup_type_node_L (instance_type);
1583 G_READ_UNLOCK (&type_rw_lock);
1584 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1587 G_WRITE_LOCK (&type_rw_lock);
1588 if (check_add_interface_L (instance_type, interface_type))
1590 TypeNode *iface = lookup_type_node_L (interface_type);
1592 type_add_interface_W (node, iface, NULL, plugin);
1594 G_WRITE_UNLOCK (&type_rw_lock);
1598 /* --- public API functions --- */
1600 g_type_class_ref (GType type)
1604 /* optimize for common code path
1606 G_WRITE_LOCK (&type_rw_lock);
1607 node = lookup_type_node_L (type);
1608 if (node && node->is_classed && node->data &&
1609 node->data->class.class && node->data->common.ref_count > 0)
1611 type_data_ref_Wm (node);
1612 G_WRITE_UNLOCK (&type_rw_lock);
1614 return node->data->class.class;
1617 if (!node || !node->is_classed ||
1618 (node->data && node->data->common.ref_count < 1))
1620 G_WRITE_UNLOCK (&type_rw_lock);
1621 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1622 type_descriptive_name_U (type));
1626 type_data_ref_Wm (node);
1628 if (!node->data->class.class)
1630 GType ptype = NODE_PARENT_TYPE (node);
1631 GTypeClass *pclass = NULL;
1635 G_WRITE_UNLOCK (&type_rw_lock);
1636 pclass = g_type_class_ref (ptype);
1637 if (node->data->class.class)
1638 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1639 G_WRITE_LOCK (&type_rw_lock);
1642 type_class_init_Wm (node, pclass);
1644 G_WRITE_UNLOCK (&type_rw_lock);
1646 return node->data->class.class;
1650 g_type_class_unref (gpointer g_class)
1653 GTypeClass *class = g_class;
1655 g_return_if_fail (g_class != NULL);
1657 G_WRITE_LOCK (&type_rw_lock);
1658 node = lookup_type_node_L (class->g_type);
1659 if (node && node->is_classed && node->data &&
1660 node->data->class.class == class && node->data->common.ref_count > 0)
1661 type_data_unref_Wm (node, FALSE);
1663 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1664 type_descriptive_name_L (class->g_type));
1665 G_WRITE_UNLOCK (&type_rw_lock);
1669 g_type_class_unref_uncached (gpointer g_class)
1672 GTypeClass *class = g_class;
1674 g_return_if_fail (g_class != NULL);
1676 G_WRITE_LOCK (&type_rw_lock);
1677 node = lookup_type_node_L (class->g_type);
1678 if (node && node->is_classed && node->data &&
1679 node->data->class.class == class && node->data->common.ref_count > 0)
1680 type_data_unref_Wm (node, TRUE);
1682 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1683 type_descriptive_name_L (class->g_type));
1684 G_WRITE_UNLOCK (&type_rw_lock);
1688 g_type_class_peek (GType type)
1693 G_READ_LOCK (&type_rw_lock);
1694 node = lookup_type_node_L (type);
1695 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1696 class = node->data->class.class;
1699 G_READ_UNLOCK (&type_rw_lock);
1705 g_type_class_peek_parent (gpointer g_class)
1710 g_return_val_if_fail (g_class != NULL, NULL);
1712 G_READ_LOCK (&type_rw_lock);
1713 node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
1714 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1716 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
1717 class = node->data->class.class;
1721 G_READ_UNLOCK (&type_rw_lock);
1727 g_type_interface_peek (gpointer instance_class,
1732 gpointer vtable = NULL;
1733 GTypeClass *class = instance_class;
1735 g_return_val_if_fail (instance_class != NULL, NULL);
1737 G_READ_LOCK (&type_rw_lock);
1738 node = lookup_type_node_L (class->g_type);
1739 iface = lookup_type_node_L (iface_type);
1740 if (node && node->is_instantiatable && iface)
1742 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1744 if (entry && entry->vtable)
1745 vtable = entry->vtable;
1747 G_READ_UNLOCK (&type_rw_lock);
1753 g_type_value_table_peek (GType type)
1756 GTypeValueTable *vtable = NULL;
1758 G_READ_LOCK (&type_rw_lock);
1759 node = lookup_type_node_L (type);
1760 if (node && node->data && node->data->common.ref_count > 0 &&
1761 node->data->common.value_table->value_init)
1762 vtable = node->data->common.value_table;
1763 G_READ_UNLOCK (&type_rw_lock);
1769 g_type_name (GType type)
1773 G_READ_LOCK (&type_rw_lock);
1774 node = lookup_type_node_L (type);
1775 G_READ_UNLOCK (&type_rw_lock);
1777 return node ? NODE_NAME (node) : NULL;
1781 g_type_qname (GType type)
1785 G_READ_LOCK (&type_rw_lock);
1786 node = lookup_type_node_L (type);
1787 G_READ_UNLOCK (&type_rw_lock);
1789 return node ? node->qname : 0;
1793 g_type_from_name (const gchar *name)
1798 g_return_val_if_fail (name != NULL, 0);
1800 quark = g_quark_try_string (name);
1803 G_READ_LOCK (&type_rw_lock);
1804 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
1805 G_READ_UNLOCK (&type_rw_lock);
1812 g_type_parent (GType type)
1816 G_READ_LOCK (&type_rw_lock);
1817 node = lookup_type_node_L (type);
1818 G_READ_UNLOCK (&type_rw_lock);
1820 return node ? NODE_PARENT_TYPE (node) : 0;
1824 g_type_next_base (GType type,
1830 G_READ_LOCK (&type_rw_lock);
1831 node = lookup_type_node_L (type);
1834 TypeNode *base_node = lookup_type_node_L (base_type);
1836 if (base_node && base_node->n_supers < node->n_supers)
1838 guint n = node->n_supers - base_node->n_supers;
1840 if (node->supers[n] == base_type)
1841 atype = node->supers[n - 1];
1844 G_READ_UNLOCK (&type_rw_lock);
1850 g_type_is_a (GType type,
1853 gboolean is_a = FALSE;
1855 G_READ_LOCK (&type_rw_lock);
1856 if (type != iface_type)
1858 TypeNode *node = lookup_type_node_L (type);
1862 TypeNode *iface_node = lookup_type_node_L (iface_type);
1866 if (iface_node->is_iface && node->is_instantiatable)
1867 is_a = type_lookup_iface_entry_L (node, iface_node) != NULL;
1868 else if (iface_node->n_supers <= node->n_supers)
1869 is_a = node->supers[node->n_supers - iface_node->n_supers] == iface_type;
1874 is_a = lookup_type_node_L (type) != NULL;
1875 G_READ_UNLOCK (&type_rw_lock);
1881 g_type_fundamental_branch_last (GType type)
1883 GType ftype = G_TYPE_FUNDAMENTAL (type);
1886 G_READ_LOCK (&type_rw_lock);
1887 last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
1888 G_READ_UNLOCK (&type_rw_lock);
1893 GType* /* free result */
1894 g_type_children (GType type,
1899 G_READ_LOCK (&type_rw_lock);
1900 node = lookup_type_node_L (type);
1903 GType *children = g_new (GType, node->n_children + 1);
1905 memcpy (children, node->children, sizeof (GType) * node->n_children);
1906 children[node->n_children] = 0;
1909 *n_children = node->n_children;
1910 G_READ_UNLOCK (&type_rw_lock);
1916 G_READ_UNLOCK (&type_rw_lock);
1924 GType* /* free result */
1925 g_type_interfaces (GType type,
1926 guint *n_interfaces)
1930 G_READ_LOCK (&type_rw_lock);
1931 node = lookup_type_node_L (type);
1932 if (node && node->is_instantiatable)
1934 GType *ifaces = g_new (GType, node->n_ifaces + 1);
1937 for (i = 0; i < node->n_ifaces; i++)
1938 ifaces[i] = node->private.iface_entries[i].iface_type;
1942 *n_interfaces = node->n_ifaces;
1943 G_READ_UNLOCK (&type_rw_lock);
1949 G_READ_UNLOCK (&type_rw_lock);
1957 typedef struct _QData QData;
1969 static inline gpointer
1970 type_get_qdata_L (TypeNode *node,
1973 GData *gdata = node->global_gdata;
1975 if (quark && gdata && gdata->n_qdatas)
1977 QData *qdatas = gdata->qdatas - 1;
1978 guint n_qdatas = gdata->n_qdatas;
1985 i = (n_qdatas + 1) / 2;
1987 if (quark == check->quark)
1989 else if (quark > check->quark)
1994 else /* if (quark < check->quark) */
2003 g_type_get_qdata (GType type,
2009 G_READ_LOCK (&type_rw_lock);
2010 node = lookup_type_node_L (type);
2013 data = type_get_qdata_L (node, quark);
2014 G_READ_UNLOCK (&type_rw_lock);
2018 G_READ_UNLOCK (&type_rw_lock);
2019 g_return_val_if_fail (node != NULL, NULL);
2026 type_set_qdata_W (TypeNode *node,
2034 /* setup qdata list if necessary */
2035 if (!node->global_gdata)
2036 node->global_gdata = g_new0 (GData, 1);
2037 gdata = node->global_gdata;
2039 /* try resetting old data */
2040 qdata = gdata->qdatas;
2041 for (i = 0; i < gdata->n_qdatas; i++)
2042 if (qdata[i].quark == quark)
2044 qdata[i].data = data;
2050 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2051 qdata = gdata->qdatas;
2052 for (i = 0; i < gdata->n_qdatas - 1; i++)
2053 if (qdata[i].quark > quark)
2055 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2056 qdata[i].quark = quark;
2057 qdata[i].data = data;
2061 g_type_set_qdata (GType type,
2067 g_return_if_fail (quark != 0);
2069 G_WRITE_LOCK (&type_rw_lock);
2070 node = lookup_type_node_L (type);
2073 type_set_qdata_W (node, quark, data);
2074 G_WRITE_UNLOCK (&type_rw_lock);
2078 G_WRITE_UNLOCK (&type_rw_lock);
2079 g_return_if_fail (node != NULL);
2084 type_add_flags_W (TypeNode *node,
2089 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2090 g_return_if_fail (node != NULL);
2092 if ((flags & G_TYPE_FLAG_ABSTRACT) && node->is_classed && node->data && node->data->class.class)
2093 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2094 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2096 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2100 /* --- implementation details --- */
2102 g_type_check_flags (GType type,
2106 gboolean result = FALSE;
2108 G_READ_LOCK (&type_rw_lock);
2109 node = lookup_type_node_L (type);
2112 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2113 guint tflags = flags & TYPE_FLAG_MASK;
2117 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2119 fflags = (finfo->type_flags & fflags) == fflags;
2125 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2129 result = tflags && fflags;
2131 G_READ_UNLOCK (&type_rw_lock);
2137 g_type_get_plugin (GType type)
2141 G_READ_LOCK (&type_rw_lock);
2142 node = lookup_type_node_L (type);
2143 G_READ_UNLOCK (&type_rw_lock);
2145 return node ? node->plugin : NULL;
2149 g_type_interface_get_plugin (GType instance_type,
2150 GType interface_type)
2155 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2157 G_READ_LOCK (&type_rw_lock);
2158 node = lookup_type_node_L (instance_type);
2159 iface = lookup_type_node_L (interface_type);
2162 IFaceHolder *iholder = iface->private.iface_conformants;
2164 while (iholder && iholder->instance_type != instance_type)
2165 iholder = iholder->next;
2166 G_READ_UNLOCK (&type_rw_lock);
2169 return iholder->plugin;
2172 G_READ_UNLOCK (&type_rw_lock);
2174 g_return_val_if_fail (node == NULL, NULL);
2175 g_return_val_if_fail (iface == NULL, NULL);
2177 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2183 g_type_fundamental_last (void)
2187 G_READ_LOCK (&type_rw_lock);
2188 type = static_last_fundamental_id;
2189 G_READ_UNLOCK (&type_rw_lock);
2195 g_type_instance_is_a (GTypeInstance *type_instance,
2198 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2199 return (type_instance && type_instance->g_class &&
2200 G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type) &&
2201 g_type_is_a (type_instance->g_class->g_type, iface_type));
2205 g_type_class_is_a (GTypeClass *type_class,
2208 /* G_TYPE_IS_CLASSED() is an external call: _U */
2209 return (type_class && G_TYPE_IS_CLASSED (type_class->g_type) &&
2210 g_type_is_a (type_class->g_type, is_a_type));
2214 g_type_value_is_a (GValue *value,
2222 G_READ_LOCK (&type_rw_lock);
2223 node = lookup_type_node_L (value->g_type);
2225 if (!G_TYPE_IS_FUNDAMENTAL (value->g_type) && !node || !node->data)
2226 node = lookup_type_node_L (G_TYPE_FUNDAMENTAL (value->g_type));
2228 if (!node || !node->data || node->data->common.ref_count < 1 ||
2229 !node->data->common.value_table->value_init)
2231 G_READ_UNLOCK (&type_rw_lock);
2235 G_READ_UNLOCK (&type_rw_lock);
2237 return g_type_is_a (value->g_type, type);
2241 g_type_check_value (GValue *value)
2243 return value && g_type_value_is_a (value, value->g_type);
2247 g_type_check_instance_cast (GTypeInstance *type_instance,
2252 g_warning ("invalid cast from (NULL) pointer to `%s'",
2253 type_descriptive_name_U (iface_type));
2254 return type_instance;
2256 if (!type_instance->g_class)
2258 g_warning ("invalid unclassed pointer in cast to `%s'",
2259 type_descriptive_name_U (iface_type));
2260 return type_instance;
2262 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2263 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2265 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2266 type_descriptive_name_U (type_instance->g_class->g_type),
2267 type_descriptive_name_U (iface_type));
2268 return type_instance;
2270 if (!g_type_is_a (type_instance->g_class->g_type, iface_type))
2272 g_warning ("invalid cast from `%s' to `%s'",
2273 type_descriptive_name_U (type_instance->g_class->g_type),
2274 type_descriptive_name_U (iface_type));
2275 return type_instance;
2278 return type_instance;
2282 g_type_check_class_cast (GTypeClass *type_class,
2287 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2288 type_descriptive_name_U (is_a_type));
2291 /* G_TYPE_IS_CLASSED() is an external call: _U */
2292 if (!G_TYPE_IS_CLASSED (type_class->g_type))
2294 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2295 type_descriptive_name_U (type_class->g_type),
2296 type_descriptive_name_U (is_a_type));
2299 if (!g_type_is_a (type_class->g_type, is_a_type))
2301 g_warning ("invalid class cast from `%s' to `%s'",
2302 type_descriptive_name_U (type_class->g_type),
2303 type_descriptive_name_U (is_a_type));
2311 g_type_check_instance (GTypeInstance *type_instance)
2313 /* this function is just here to make the signal system
2314 * conveniently elaborated on instance checks
2318 g_warning ("instance is invalid (NULL) pointer");
2321 if (!type_instance->g_class)
2323 g_warning ("instance with invalid (NULL) class pointer");
2326 /* G_TYPE_IS_CLASSED() is an external call: _U */
2327 if (!G_TYPE_IS_CLASSED (type_instance->g_class->g_type))
2329 g_warning ("instance of invalid unclassed type `%s'",
2330 type_descriptive_name_U (type_instance->g_class->g_type));
2333 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2334 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2336 g_warning ("instance of invalid non-instantiatable type `%s'",
2337 type_descriptive_name_U (type_instance->g_class->g_type));
2345 /* --- foreign prototypes --- */
2346 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2347 extern void g_enum_types_init (void); /* sync with genums.c */
2348 extern void g_param_type_init (void); /* sync with gparam.c */
2349 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2350 extern void g_object_type_init (void); /* sync with gobject.c */
2351 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2352 extern void g_signal_init (void); /* sync with gsignal.c */
2355 /* --- initialization --- */
2359 G_LOCK_DEFINE_STATIC (type_init_lock);
2360 static TypeNode *type0_node = NULL;
2365 G_LOCK (type_init_lock);
2367 G_WRITE_LOCK (&type_rw_lock);
2369 if (static_last_fundamental_id)
2371 G_WRITE_UNLOCK (&type_rw_lock);
2372 G_UNLOCK (type_init_lock);
2377 static_quark_type_flags = g_quark_from_static_string ("GTypeFlags");
2379 /* type qname hash table */
2380 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2382 /* invalid type G_TYPE_INVALID (0)
2384 static_last_fundamental_id = 1;
2385 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2386 static_type_nodes[0] = &type0_node;
2387 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2388 static_branch_seqnos[0] = 1;
2390 /* void type G_TYPE_NONE
2392 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2393 type = NODE_TYPE (node);
2394 g_assert (type == G_TYPE_NONE);
2396 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2398 memset (&info, 0, sizeof (info));
2399 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2400 type = NODE_TYPE (node);
2401 type_data_make_W (node, &info, NULL);
2402 g_assert (type == G_TYPE_INTERFACE);
2404 G_WRITE_UNLOCK (&type_rw_lock);
2406 /* G_TYPE_TYPE_PLUGIN
2408 g_type_plugin_get_type ();
2410 /* G_TYPE_* value types
2412 g_value_types_init ();
2414 /* G_TYPE_ENUM & G_TYPE_FLAGS
2416 g_enum_types_init ();
2420 g_param_type_init ();
2424 g_boxed_type_init ();
2428 g_object_type_init ();
2430 /* G_TYPE_PARAM_* pspec types
2432 g_param_spec_types_init ();
2438 G_UNLOCK (type_init_lock);