1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 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.
25 #include "gtypeplugin.h"
26 #include "gvaluecollector.h"
30 /* NOTE: some functions (some internal variants and exported ones)
31 * invalidate data portions of the TypeNodes. if external functions/callbacks
32 * are called, pointers to memory maintained by TypeNodes have to be looked up
33 * again. this affects most of the struct TypeNode fields, e.g. ->children or
34 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
35 * not ->supers[]), as all those memory portions can get realloc()ed during
36 * callback invocation.
39 * - g_type_from_name() should do an ordered array lookup after fetching the
40 * the quark, instead of a second hashtable lookup.
41 * - speedup checks for virtual types, steal a bit somewhere
44 * - force interface initialization for already existing classes
47 * lock handling issues when calling static functions are indicated by
48 * uppercase letter postfixes, all static functions have to have
49 * one of the below postfixes:
50 * - _I: [Indifferent about locking]
51 * function doesn't care about locks at all
52 * - _U: [Unlocked invocation]
53 * no read or write lock has to be held across function invocation
54 * (locks may be acquired and released during invocation though)
55 * - _L: [Locked invocation]
56 * a write lock or more than 0 read locks have to be held across
58 * - _W: [Write-locked invocation]
59 * a write lock has to be held across function invokation
60 * - _Wm: [Write-locked invocation, mutatable]
61 * like _W, but the write lock might be released and reacquired
62 * during invocation, watch your pointers
65 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
66 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
67 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
68 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
69 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
70 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
71 static const gchar *_action = " invalidly modified type "; \
72 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
74 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
76 g_error ("%s()%s`%s'", _fname, _action, _tname); \
78 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
81 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
82 "%s: initialization assertion failed, use %s() prior to this function", \
83 G_STRLOC, G_STRINGIFY (init_function)); \
84 return (return_value); \
89 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
90 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
93 #else /* !G_ENABLE_DEBUG */
94 #define DEBUG_CODE(debug_type, code_block) /* code_block */
95 #endif /* G_ENABLE_DEBUG */
97 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
98 G_TYPE_FLAG_INSTANTIATABLE | \
99 G_TYPE_FLAG_DERIVABLE | \
100 G_TYPE_FLAG_DEEP_DERIVABLE)
101 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
102 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
103 sizeof (gpointer)), \
107 /* --- typedefs --- */
108 typedef struct _TypeNode TypeNode;
109 typedef struct _CommonData CommonData;
110 typedef struct _IFaceData IFaceData;
111 typedef struct _ClassData ClassData;
112 typedef struct _InstanceData InstanceData;
113 typedef union _TypeData TypeData;
114 typedef struct _IFaceEntry IFaceEntry;
115 typedef struct _IFaceHolder IFaceHolder;
118 /* --- prototypes --- */
119 static inline GTypeFundamentalInfo* type_node_fundamental_info_L (TypeNode *node);
120 static void type_add_flags_W (TypeNode *node,
122 static void type_data_make_W (TypeNode *node,
123 const GTypeInfo *info,
124 const GTypeValueTable *value_table);
125 static inline void type_data_ref_Wm (TypeNode *node);
126 static inline void type_data_unref_Wm (TypeNode *node,
128 static void type_data_last_unref_Wm (GType type,
130 static inline gpointer type_get_qdata_L (TypeNode *node,
132 static inline void type_set_qdata_W (TypeNode *node,
137 /* --- structures --- */
138 struct _GValue /* kludge, keep in sync with gvalue.h */
145 guint n_children : 12;
147 guint _prot_n_ifaces_prerequisites : 9;
148 guint is_classed : 1;
149 guint is_instantiatable : 1;
156 IFaceEntry *iface_entries; /* for !iface types */
157 GType *prerequisistes;
159 GType supers[1]; /* flexible array */
161 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
162 #define MAX_N_SUPERS (255)
163 #define MAX_N_CHILDREN (4095)
164 #define MAX_N_IFACES (511)
165 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
166 #define NODE_TYPE(node) (node->supers[0])
167 #define NODE_PARENT_TYPE(node) (node->supers[1])
168 #define NODE_NAME(node) (g_quark_to_string (node->qname))
169 #define NODE_IS_IFACE(node) (G_TYPE_IS_INTERFACE (NODE_TYPE (node)))
170 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
171 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
172 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
173 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
174 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
175 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
176 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
177 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
182 GInterfaceInfo *info;
189 GTypeInterface *vtable;
194 GTypeValueTable *value_table;
200 GBaseInitFunc vtable_init_base;
201 GBaseFinalizeFunc vtable_finalize_base;
207 GBaseInitFunc class_init_base;
208 GBaseFinalizeFunc class_finalize_base;
209 GClassInitFunc class_init;
210 GClassFinalizeFunc class_finalize;
211 gconstpointer class_data;
218 GBaseInitFunc class_init_base;
219 GBaseFinalizeFunc class_finalize_base;
220 GClassInitFunc class_init;
221 GClassFinalizeFunc class_finalize;
222 gconstpointer class_data;
224 guint16 instance_size;
226 GInstanceInitFunc instance_init;
227 GMemChunk *mem_chunk;
234 InstanceData instance;
238 GTypeClassCacheFunc cache_func;
242 /* --- variables --- */
243 static guint static_n_class_cache_funcs = 0;
244 static ClassCacheFunc *static_class_cache_funcs = NULL;
245 static GType static_last_fundamental_id = 0;
246 static GQuark static_quark_type_flags = 0;
247 static GQuark static_quark_iface_holder = 0;
248 static GQuark static_quark_dependants_array = 0;
249 GTypeDebugFlags _g_type_debug_flags = 0;
252 /* --- externs --- */
253 const char *g_log_domain_gruntime = "GRuntime";
256 /* --- type nodes --- */
257 static GHashTable *static_type_nodes_ht = NULL;
258 static GType *static_branch_seqnos = NULL;
259 static TypeNode ***static_type_nodes = NULL;
261 static inline TypeNode*
262 lookup_type_node_L (register GType utype)
264 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
265 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
267 if (ftype < static_last_fundamental_id && b_seqno < static_branch_seqnos[ftype])
268 return static_type_nodes[ftype][b_seqno];
274 type_node_any_new_W (TypeNode *pnode,
278 GTypeFundamentalFlags type_flags)
280 guint branch_last, n_supers;
283 guint i, node_size = 0;
285 n_supers = pnode ? pnode->n_supers + 1 : 0;
286 branch_last = static_branch_seqnos[ftype]++;
287 type = G_TYPE_DERIVE_ID (ftype, branch_last);
288 g_assert ((type & G_TYPE_FLAG_RESERVED_ID_BIT) == 0);
289 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (static_branch_seqnos[ftype] - 1))
290 static_type_nodes[ftype] = g_renew (TypeNode*, static_type_nodes[ftype], 1 << g_bit_storage (static_branch_seqnos[ftype] - 1));
293 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
294 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
295 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
296 node = g_malloc0 (node_size);
297 if (!pnode) /* offset fundamental types */
298 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
299 static_type_nodes[ftype][branch_last] = node;
301 node->n_supers = n_supers;
304 node->supers[0] = type;
307 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
308 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
310 if (NODE_IS_IFACE (node))
312 IFACE_NODE_N_PREREQUISITES (node) = 0;
313 IFACE_NODE_PREREQUISITES (node) = NULL;
317 CLASSED_NODE_N_IFACES (node) = 0;
318 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
323 node->supers[0] = type;
324 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
326 node->is_classed = pnode->is_classed;
327 node->is_instantiatable = pnode->is_instantiatable;
329 if (NODE_IS_IFACE (node))
331 IFACE_NODE_N_PREREQUISITES (node) = 0;
332 IFACE_NODE_PREREQUISITES (node) = NULL;
336 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
337 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
338 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
339 CLASSED_NODE_N_IFACES (node));
342 i = pnode->n_children++;
343 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
344 pnode->children[i] = type;
347 node->plugin = plugin;
348 node->n_children = 0;
349 node->children = NULL;
351 node->qname = g_quark_from_string (name);
352 node->global_gdata = NULL;
354 g_hash_table_insert (static_type_nodes_ht,
355 GUINT_TO_POINTER (node->qname),
356 GUINT_TO_POINTER (type));
360 static inline GTypeFundamentalInfo*
361 type_node_fundamental_info_L (TypeNode *node)
363 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
365 if (ftype != NODE_TYPE (node))
366 node = lookup_type_node_L (ftype);
368 return node ? G_STRUCT_MEMBER_P (node, - SIZEOF_FUNDAMENTAL_INFO) : NULL;
372 type_node_fundamental_new_W (GType ftype,
374 GTypeFundamentalFlags type_flags)
376 GTypeFundamentalInfo *finfo;
380 flast = static_last_fundamental_id;
382 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
384 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
386 static_last_fundamental_id = MAX (static_last_fundamental_id, ftype + 1);
387 if (static_last_fundamental_id > flast)
389 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
390 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
391 for (i = flast; i < static_last_fundamental_id; i++)
393 static_type_nodes[i] = NULL;
394 static_branch_seqnos[i] = 0;
397 g_assert (static_branch_seqnos[ftype] == 0);
399 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
401 finfo = type_node_fundamental_info_L (node);
402 finfo->type_flags = type_flags;
408 type_node_new_W (TypeNode *pnode,
414 g_assert (pnode->n_supers < MAX_N_SUPERS);
415 g_assert (pnode->n_children < MAX_N_CHILDREN);
417 return type_node_any_new_W (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
420 static inline IFaceEntry*
421 type_lookup_iface_entry_L (TypeNode *node,
422 TypeNode *iface_node)
424 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
426 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
427 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
428 GType iface_type = NODE_TYPE (iface_node);
435 i = (n_ifaces + 1) >> 1;
437 if (iface_type == check->iface_type)
439 else if (iface_type > check->iface_type)
444 else /* if (iface_type < check->iface_type) */
453 static inline gboolean
454 type_lookup_prerequisite (TypeNode *iface,
455 GType prerequisite_type)
457 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
459 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
460 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
467 i = (n_prerequisites + 1) >> 1;
468 check = prerequisites + i;
469 if (prerequisite_type == *check)
471 else if (prerequisite_type > *check)
473 n_prerequisites -= i;
474 prerequisites = check;
476 else /* if (prerequisite_type < *check) */
477 n_prerequisites = i - 1;
479 while (n_prerequisites);
485 type_descriptive_name_L (GType type)
489 TypeNode *node = lookup_type_node_L (type);
491 return node ? NODE_NAME (node) : "<unknown>";
498 type_descriptive_name_U (GType type)
502 G_READ_LOCK (&type_rw_lock);
503 name = type_descriptive_name_L (type);
504 G_READ_UNLOCK (&type_rw_lock);
506 return (gchar *)name;
510 /* --- type consistency checks --- */
512 check_plugin_U (GTypePlugin *plugin,
513 gboolean need_complete_type_info,
514 gboolean need_complete_interface_info,
515 const gchar *type_name)
517 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
521 g_warning ("plugin handle for type `%s' is NULL",
525 if (!G_IS_TYPE_PLUGIN (plugin))
527 g_warning ("plugin pointer (%p) for type `%s' is invalid",
531 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
533 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
537 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
539 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
547 check_type_name_U (const gchar *type_name)
549 static const gchar *extra_chars = "-_+";
550 const gchar *p = type_name;
553 if (!type_name[0] || !type_name[1] || !type_name[2])
555 g_warning ("type name `%s' is too short", type_name);
558 /* check the first letter */
559 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
560 for (p = type_name + 1; *p; p++)
561 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
562 (p[0] >= 'a' && p[0] <= 'z') ||
563 (p[0] >= '0' && p[0] <= '9') ||
564 strchr (extra_chars, p[0]));
567 g_warning ("type name `%s' contains invalid characters", type_name);
570 if (g_type_from_name (type_name))
572 g_warning ("cannot register existing type `%s'", type_name);
580 check_derivation_U (GType parent_type,
581 const gchar *type_name)
584 GTypeFundamentalInfo* finfo;
586 G_READ_LOCK (&type_rw_lock);
587 pnode = lookup_type_node_L (parent_type);
590 G_READ_UNLOCK (&type_rw_lock);
591 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
593 type_descriptive_name_U (parent_type));
596 finfo = type_node_fundamental_info_L (pnode);
597 /* ensure flat derivability */
598 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
600 G_READ_UNLOCK (&type_rw_lock);
601 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
606 /* ensure deep derivability */
607 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
608 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
610 G_READ_UNLOCK (&type_rw_lock);
611 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
616 G_READ_UNLOCK (&type_rw_lock);
622 check_collect_format_I (const gchar *collect_format)
624 const gchar *p = collect_format;
625 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
626 G_VALUE_COLLECT_DOUBLE, G_VALUE_COLLECT_POINTER,
630 if (!strchr (valid_format, *p++))
632 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
636 check_value_table_I (const gchar *type_name,
637 const GTypeValueTable *value_table)
641 else if (value_table->value_init == NULL)
643 if (value_table->value_free || value_table->value_copy ||
644 value_table->value_peek_pointer ||
645 value_table->collect_format || value_table->collect_value ||
646 value_table->lcopy_format || value_table->lcopy_value)
647 g_warning ("cannot handle uninitializable values of type `%s'",
651 else /* value_table->value_init != NULL */
653 if (!value_table->value_free)
656 * g_warning ("missing `value_free()' for type `%s'", type_name);
660 if (!value_table->value_copy)
662 g_warning ("missing `value_copy()' for type `%s'", type_name);
665 if ((value_table->collect_format || value_table->collect_value) &&
666 (!value_table->collect_format || !value_table->collect_value))
668 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
672 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
674 g_warning ("the `%s' specification for type `%s' is too long or invalid",
679 if ((value_table->lcopy_format || value_table->lcopy_value) &&
680 (!value_table->lcopy_format || !value_table->lcopy_value))
682 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
686 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
688 g_warning ("the `%s' specification for type `%s' is too long or invalid",
698 check_type_info_L (TypeNode *pnode,
700 const gchar *type_name,
701 const GTypeInfo *info)
703 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (lookup_type_node_L (ftype));
704 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
706 /* check instance members */
707 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
708 (info->instance_size || info->n_preallocs || info->instance_init))
711 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
715 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
719 /* check class & interface members */
720 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
721 (info->class_init || info->class_finalize || info->class_data ||
722 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
725 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
729 g_warning ("cannot create class for `%s' as non-classed fundamental",
733 /* check interface size */
734 if (is_interface && info->class_size < sizeof (GTypeInterface))
736 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
740 /* check class size */
741 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
743 if (info->class_size < sizeof (GTypeClass))
745 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
749 if (pnode && info->class_size < pnode->data->class.class_size)
751 g_warning ("specified class size for type `%s' is smaller "
752 "than the parent type's `%s' class size",
758 /* check instance size */
759 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
761 if (info->instance_size < sizeof (GTypeInstance))
763 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
767 if (pnode && info->instance_size < pnode->data->instance.instance_size)
769 g_warning ("specified instance size for type `%s' is smaller "
770 "than the parent type's `%s' instance size",
781 find_conforming_type_L (TypeNode *pnode,
784 TypeNode *node = NULL;
787 if (type_lookup_iface_entry_L (pnode, iface))
790 for (i = 0; i < pnode->n_children && !node; i++)
791 node = find_conforming_type_L (lookup_type_node_L (pnode->children[i]), iface);
797 check_add_interface_L (GType instance_type,
800 TypeNode *node = lookup_type_node_L (instance_type);
801 TypeNode *iface = lookup_type_node_L (iface_type);
804 if (!node || !node->is_instantiatable)
806 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
807 type_descriptive_name_L (instance_type));
810 if (!iface || !NODE_IS_IFACE (iface))
812 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
813 type_descriptive_name_L (iface_type),
817 tnode = lookup_type_node_L (NODE_PARENT_TYPE (iface));
818 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
820 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
826 tnode = find_conforming_type_L (node, iface);
829 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
839 check_interface_info_L (TypeNode *iface,
841 const GInterfaceInfo *info)
843 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
845 g_warning ("interface type `%s' for type `%s' comes without initializer",
847 type_descriptive_name_L (instance_type));
854 /* --- type info (type node data) --- */
856 type_data_make_W (TypeNode *node,
857 const GTypeInfo *info,
858 const GTypeValueTable *value_table)
861 GTypeValueTable *vtable = NULL;
862 guint vtable_size = 0;
864 g_assert (node->data == NULL && info != NULL);
868 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
871 vtable = pnode->data->common.value_table;
874 static const GTypeValueTable zero_vtable = { NULL, };
876 value_table = &zero_vtable;
881 /* need to setup vtable_size since we have to allocate it with data in one chunk */
882 vtable_size = sizeof (GTypeValueTable);
883 if (value_table->collect_format)
884 vtable_size += strlen (value_table->collect_format);
885 if (value_table->lcopy_format)
886 vtable_size += strlen (value_table->lcopy_format);
890 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
892 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
894 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
895 data->instance.class_size = info->class_size;
896 data->instance.class_init_base = info->base_init;
897 data->instance.class_finalize_base = info->base_finalize;
898 data->instance.class_init = info->class_init;
899 data->instance.class_finalize = info->class_finalize;
900 data->instance.class_data = info->class_data;
901 data->instance.class = NULL;
902 data->instance.instance_size = info->instance_size;
903 #ifdef DISABLE_MEM_POOLS
904 data->instance.n_preallocs = 0;
905 #else /* !DISABLE_MEM_POOLS */
906 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
907 #endif /* !DISABLE_MEM_POOLS */
908 data->instance.instance_init = info->instance_init;
909 data->instance.mem_chunk = NULL;
911 else if (node->is_classed) /* only classed */
913 data = g_malloc0 (sizeof (ClassData) + vtable_size);
915 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
916 data->class.class_size = info->class_size;
917 data->class.class_init_base = info->base_init;
918 data->class.class_finalize_base = info->base_finalize;
919 data->class.class_init = info->class_init;
920 data->class.class_finalize = info->class_finalize;
921 data->class.class_data = info->class_data;
922 data->class.class = NULL;
924 else if (NODE_IS_IFACE (node))
926 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
928 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
929 data->iface.vtable_size = info->class_size;
930 data->iface.vtable_init_base = info->base_init;
931 data->iface.vtable_finalize_base = info->base_finalize;
935 data = g_malloc0 (sizeof (CommonData) + vtable_size);
937 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
941 node->data->common.ref_count = 1;
947 /* we allocate the vtable and its strings together with the type data, so
948 * children can take over their parent's vtable pointer, and we don't
949 * need to worry freeing it or not when the child data is destroyed
951 *vtable = *value_table;
952 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
954 vtable->collect_format = p;
955 if (value_table->collect_format)
957 strcat (p, value_table->collect_format);
958 p += strlen (value_table->collect_format);
962 vtable->lcopy_format = p;
963 if (value_table->lcopy_format)
964 strcat (p, value_table->lcopy_format);
966 node->data->common.value_table = vtable;
968 g_assert (node->data->common.value_table != NULL); /* paranoid */
972 type_data_ref_Wm (TypeNode *node)
976 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
978 GTypeValueTable tmp_value_table;
980 g_assert (node->plugin != NULL);
984 type_data_ref_Wm (pnode);
986 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
989 memset (&tmp_info, 0, sizeof (tmp_info));
990 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
992 G_WRITE_UNLOCK (&type_rw_lock);
993 g_type_plugin_use (node->plugin);
994 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
995 G_WRITE_LOCK (&type_rw_lock);
997 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
999 check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
1000 type_data_make_W (node, &tmp_info,
1001 check_value_table_I (NODE_NAME (node),
1002 &tmp_value_table) ? &tmp_value_table : NULL);
1006 g_assert (node->data->common.ref_count > 0);
1008 node->data->common.ref_count += 1;
1013 type_data_unref_Wm (TypeNode *node,
1016 g_assert (node->data && node->data->common.ref_count);
1018 if (node->data->common.ref_count > 1)
1019 node->data->common.ref_count -= 1;
1024 g_warning ("static type `%s' unreferenced too often",
1029 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1034 type_node_add_iface_entry_W (TypeNode *node,
1037 IFaceEntry *entries;
1040 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1042 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1043 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1044 if (entries[i].iface_type == iface_type)
1046 g_warning ("failed to add interface, type `%s' already conforms to interface type `%s'",
1047 type_descriptive_name_L (NODE_TYPE (node)),
1048 type_descriptive_name_L (iface_type));
1051 else if (entries[i].iface_type > iface_type)
1053 CLASSED_NODE_N_IFACES (node) += 1;
1054 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1055 CLASSED_NODE_IFACES_ENTRIES (node),
1056 CLASSED_NODE_N_IFACES (node));
1057 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1058 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1059 entries[i].iface_type = iface_type;
1060 entries[i].vtable = NULL;
1062 for (i = 0; i < node->n_children; i++)
1063 type_node_add_iface_entry_W (lookup_type_node_L (node->children[i]), iface_type);
1067 type_add_interface_W (TypeNode *node,
1069 const GInterfaceInfo *info,
1070 GTypePlugin *plugin)
1072 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1074 /* we must not call any functions of GInterfaceInfo from within here, since
1075 * we got most probably called from _within_ a type registration function
1077 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1079 iholder->next = iface_node_get_holders_L (iface);
1080 iface_node_set_holders_W (iface, iholder);
1081 iholder->instance_type = NODE_TYPE (node);
1082 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1083 iholder->plugin = plugin;
1085 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1089 type_iface_add_prerequisite_W (TypeNode *iface,
1090 TypeNode *prerequisite_node)
1092 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1093 GType *prerequisites, *dependants;
1094 guint n_dependants, i;
1096 g_assert (NODE_IS_IFACE (iface) &&
1097 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1098 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1100 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1101 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1102 if (prerequisites[i] == prerequisite_type)
1103 return; /* we already have that prerequisiste */
1104 else if (prerequisites[i] > prerequisite_type)
1106 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1107 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1108 IFACE_NODE_PREREQUISITES (iface),
1109 IFACE_NODE_N_PREREQUISITES (iface));
1110 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1111 g_memmove (prerequisites + i + 1, prerequisites + i,
1112 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1113 prerequisites[i] = prerequisite_type;
1115 /* we want to get notified when prerequisites get added to prerequisite_node */
1116 if (NODE_IS_IFACE (prerequisite_node))
1118 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1119 n_dependants = dependants ? dependants[0] : 0;
1121 dependants = g_renew (GType, dependants, n_dependants + 1);
1122 dependants[n_dependants] = NODE_TYPE (iface);
1123 dependants[0] = n_dependants;
1124 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1127 /* we need to notify all dependants */
1128 dependants = iface_node_get_dependants_array_L (iface);
1129 n_dependants = dependants ? dependants[0] : 0;
1130 for (i = 1; i <= n_dependants; i++)
1131 type_iface_add_prerequisite_W (lookup_type_node_L (dependants[i]), prerequisite_node);
1135 g_type_interface_add_prerequisite (GType interface_type,
1136 GType prerequisite_type)
1138 TypeNode *iface, *prerequisite_node;
1139 IFaceHolder *holders;
1141 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));
1142 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1143 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1145 G_WRITE_LOCK (&type_rw_lock);
1146 iface = lookup_type_node_L (interface_type);
1147 prerequisite_node = lookup_type_node_L (prerequisite_type);
1148 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1150 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1151 type_descriptive_name_L (interface_type),
1152 type_descriptive_name_L (prerequisite_type));
1153 G_WRITE_UNLOCK (&type_rw_lock);
1156 holders = iface_node_get_holders_L (iface);
1159 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1160 type_descriptive_name_L (prerequisite_type),
1161 type_descriptive_name_L (interface_type),
1162 type_descriptive_name_L (holders->instance_type));
1163 G_WRITE_UNLOCK (&type_rw_lock);
1166 if (prerequisite_node->is_instantiatable)
1170 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1172 TypeNode *prnode = lookup_type_node_L (IFACE_NODE_PREREQUISITES (iface)[i]);
1174 if (prnode->is_instantiatable)
1176 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1177 type_descriptive_name_L (prerequisite_type),
1178 type_descriptive_name_L (interface_type),
1179 type_descriptive_name_L (NODE_TYPE (prnode)));
1180 G_WRITE_UNLOCK (&type_rw_lock);
1185 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1186 type_iface_add_prerequisite_W (iface, lookup_type_node_L (prerequisite_node->supers[i]));
1188 else if (NODE_IS_IFACE (prerequisite_node))
1191 guint n_dependants, i;
1193 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1194 n_dependants = dependants ? dependants[0] : 0;
1195 for (i = 1; i <= n_dependants; i++)
1196 type_iface_add_prerequisite_W (iface, lookup_type_node_L (dependants[i]));
1197 type_iface_add_prerequisite_W (iface, prerequisite_node);
1200 g_warning ("prerequisite `%s' for interface `%s' is not instantiatable or interface",
1201 type_descriptive_name_L (prerequisite_type),
1202 type_descriptive_name_L (interface_type));
1203 G_WRITE_UNLOCK (&type_rw_lock);
1207 type_iface_retrive_holder_info_Wm (TypeNode *iface,
1208 GType instance_type)
1210 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1212 g_assert (NODE_IS_IFACE (iface));
1214 while (iholder->instance_type != instance_type)
1215 iholder = iholder->next;
1219 GInterfaceInfo tmp_info;
1221 g_assert (iholder->plugin != NULL);
1223 type_data_ref_Wm (iface);
1225 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1227 memset (&tmp_info, 0, sizeof (tmp_info));
1229 G_WRITE_UNLOCK (&type_rw_lock);
1230 g_type_plugin_use (iholder->plugin);
1231 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1232 G_WRITE_LOCK (&type_rw_lock);
1234 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1236 check_interface_info_L (iface, instance_type, &tmp_info);
1237 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1244 type_iface_blow_holder_info_Wm (TypeNode *iface,
1245 GType instance_type)
1247 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1249 g_assert (NODE_IS_IFACE (iface));
1251 while (iholder->instance_type != instance_type)
1252 iholder = iholder->next;
1254 if (iholder->info && iholder->plugin)
1256 g_free (iholder->info);
1257 iholder->info = NULL;
1259 G_WRITE_UNLOCK (&type_rw_lock);
1260 g_type_plugin_unuse (iholder->plugin);
1261 G_WRITE_LOCK (&type_rw_lock);
1263 type_data_unref_Wm (iface, FALSE);
1268 /* --- type structure creation/destruction --- */
1270 g_type_create_instance (GType type)
1273 GTypeInstance *instance;
1277 G_READ_LOCK (&type_rw_lock);
1278 node = lookup_type_node_L (type);
1279 G_READ_UNLOCK (&type_rw_lock);
1280 if (!node || !node->is_instantiatable)
1282 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1283 type_descriptive_name_U (type));
1286 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1287 if (G_TYPE_IS_ABSTRACT (type))
1289 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1290 type_descriptive_name_U (type));
1294 class = g_type_class_ref (type);
1296 if (node->data->instance.n_preallocs)
1298 G_WRITE_LOCK (&type_rw_lock);
1299 if (!node->data->instance.mem_chunk)
1300 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1301 node->data->instance.instance_size,
1302 (node->data->instance.instance_size *
1303 node->data->instance.n_preallocs),
1305 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1306 G_WRITE_UNLOCK (&type_rw_lock);
1309 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1310 for (i = node->n_supers; i > 0; i--)
1314 G_READ_LOCK (&type_rw_lock);
1315 pnode = lookup_type_node_L (node->supers[i]);
1316 G_READ_UNLOCK (&type_rw_lock);
1318 if (pnode->data->instance.instance_init)
1320 instance->g_class = pnode->data->instance.class;
1321 pnode->data->instance.instance_init (instance, class);
1324 instance->g_class = class;
1326 if (node->data->instance.instance_init)
1327 node->data->instance.instance_init (instance, class);
1333 g_type_free_instance (GTypeInstance *instance)
1338 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1340 G_READ_LOCK (&type_rw_lock);
1341 class = instance->g_class;
1342 node = lookup_type_node_L (class->g_type);
1343 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1345 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1346 type_descriptive_name_L (class->g_type));
1347 G_READ_UNLOCK (&type_rw_lock);
1350 G_READ_UNLOCK (&type_rw_lock);
1351 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1352 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1354 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1359 instance->g_class = NULL;
1360 memset (instance, 0xaa, node->data->instance.instance_size); /* FIXME: debugging hack */
1361 if (node->data->instance.n_preallocs)
1363 G_WRITE_LOCK (&type_rw_lock);
1364 g_chunk_free (instance, node->data->instance.mem_chunk);
1365 G_WRITE_UNLOCK (&type_rw_lock);
1370 g_type_class_unref (class);
1374 type_propagate_iface_vtable_W (TypeNode *pnode,
1376 GTypeInterface *vtable)
1378 IFaceEntry *entry = type_lookup_iface_entry_L (pnode, iface);
1381 entry->vtable = vtable;
1382 for (i = 0; i < pnode->n_children; i++)
1384 TypeNode *node = lookup_type_node_L (pnode->children[i]);
1386 type_propagate_iface_vtable_W (node, iface, vtable);
1391 type_iface_vtable_init_Wm (TypeNode *iface,
1394 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1395 IFaceHolder *iholder = type_iface_retrive_holder_info_Wm (iface, NODE_TYPE (node));
1396 GTypeInterface *vtable;
1398 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1400 vtable = g_malloc0 (iface->data->iface.vtable_size);
1401 type_propagate_iface_vtable_W (node, iface, vtable);
1402 vtable->g_type = NODE_TYPE (iface);
1403 vtable->g_instance_type = NODE_TYPE (node);
1405 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1407 G_WRITE_UNLOCK (&type_rw_lock);
1408 if (iface->data->iface.vtable_init_base)
1409 iface->data->iface.vtable_init_base (vtable);
1410 if (iholder->info->interface_init)
1411 iholder->info->interface_init (vtable, iholder->info->interface_data);
1412 G_WRITE_LOCK (&type_rw_lock);
1417 type_iface_vtable_finalize_Wm (TypeNode *iface,
1419 GTypeInterface *vtable)
1421 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1422 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1424 g_assert (entry && entry->vtable == vtable);
1426 while (iholder->instance_type != NODE_TYPE (node))
1427 iholder = iholder->next;
1428 g_assert (iholder && iholder->info);
1430 type_propagate_iface_vtable_W (node, iface, NULL);
1431 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1433 G_WRITE_UNLOCK (&type_rw_lock);
1434 if (iholder->info->interface_finalize)
1435 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1436 if (iface->data->iface.vtable_finalize_base)
1437 iface->data->iface.vtable_finalize_base (vtable);
1438 G_WRITE_LOCK (&type_rw_lock);
1441 vtable->g_instance_type = 0;
1444 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1448 type_class_init_Wm (TypeNode *node,
1451 GSList *slist, *init_slist = NULL;
1457 g_assert (node->is_classed && node->data &&
1458 node->data->class.class_size &&
1459 !node->data->class.class);
1461 class = g_malloc0 (node->data->class.class_size);
1462 node->data->class.class = class;
1466 TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1468 memcpy (class, pclass, pnode->data->class.class_size);
1470 class->g_type = NODE_TYPE (node);
1472 G_WRITE_UNLOCK (&type_rw_lock);
1474 /* stack all base class initialization functions, so we
1475 * call them in ascending order.
1477 G_READ_LOCK (&type_rw_lock);
1478 for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1479 if (bnode->data->class.class_init_base)
1480 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1481 G_READ_UNLOCK (&type_rw_lock);
1482 for (slist = init_slist; slist; slist = slist->next)
1484 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1486 class_init_base (class);
1488 g_slist_free (init_slist);
1490 if (node->data->class.class_init)
1491 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1493 G_WRITE_LOCK (&type_rw_lock);
1495 /* ok, we got the class done, now initialize all interfaces */
1496 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1497 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1498 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1501 type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node);
1503 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1504 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1505 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1510 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1515 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1517 g_message ("finalizing interfaces for %sClass `%s'",
1518 type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1519 type_descriptive_name_L (NODE_TYPE (node)));
1521 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1522 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1523 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1524 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1527 type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable);
1529 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1530 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1531 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1532 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1537 type_data_finalize_class_U (TypeNode *node,
1540 GTypeClass *class = cdata->class;
1543 g_assert (cdata->class && cdata->common.ref_count == 0);
1545 if (cdata->class_finalize)
1546 cdata->class_finalize (class, (gpointer) cdata->class_data);
1548 /* call all base class destruction functions in descending order
1550 if (cdata->class_finalize_base)
1551 cdata->class_finalize_base (class);
1552 G_READ_LOCK (&type_rw_lock);
1553 for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1554 if (bnode->data->class.class_finalize_base)
1556 G_READ_UNLOCK (&type_rw_lock);
1557 bnode->data->class.class_finalize_base (class);
1558 G_READ_LOCK (&type_rw_lock);
1560 G_READ_UNLOCK (&type_rw_lock);
1563 g_free (cdata->class);
1567 type_data_last_unref_Wm (GType type,
1570 TypeNode *node = lookup_type_node_L (type);
1572 g_return_if_fail (node != NULL && node->plugin != NULL);
1574 if (!node->data || node->data->common.ref_count == 0)
1576 g_warning ("cannot drop last reference to unreferenced type `%s'",
1577 type_descriptive_name_U (type));
1581 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1585 G_WRITE_UNLOCK (&type_rw_lock);
1586 G_READ_LOCK (&type_rw_lock);
1587 for (i = 0; i < static_n_class_cache_funcs; i++)
1589 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1590 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1591 gboolean need_break;
1593 G_READ_UNLOCK (&type_rw_lock);
1594 need_break = cache_func (cache_data, node->data->class.class);
1595 G_READ_LOCK (&type_rw_lock);
1596 if (!node->data || node->data->common.ref_count == 0)
1597 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1601 G_READ_UNLOCK (&type_rw_lock);
1602 G_WRITE_LOCK (&type_rw_lock);
1605 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1606 node->data->common.ref_count -= 1;
1609 GType ptype = NODE_PARENT_TYPE (node);
1612 node->data->common.ref_count = 0;
1614 if (node->is_instantiatable && node->data->instance.mem_chunk)
1616 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1617 node->data->instance.mem_chunk = NULL;
1621 if (node->is_classed && tdata->class.class)
1623 if (CLASSED_NODE_N_IFACES (node))
1624 type_data_finalize_class_ifaces_Wm (node);
1626 G_WRITE_UNLOCK (&type_rw_lock);
1627 type_data_finalize_class_U (node, &tdata->class);
1628 G_WRITE_LOCK (&type_rw_lock);
1633 /* freeing tdata->common.value_table and its contents is taking care of
1634 * by allocating it in one chunk with tdata
1639 type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1640 G_WRITE_UNLOCK (&type_rw_lock);
1641 g_type_plugin_unuse (node->plugin);
1642 G_WRITE_LOCK (&type_rw_lock);
1647 g_type_add_class_cache_func (gpointer cache_data,
1648 GTypeClassCacheFunc cache_func)
1652 g_return_if_fail (cache_func != NULL);
1654 G_WRITE_LOCK (&type_rw_lock);
1655 i = static_n_class_cache_funcs++;
1656 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1657 static_class_cache_funcs[i].cache_data = cache_data;
1658 static_class_cache_funcs[i].cache_func = cache_func;
1659 G_WRITE_UNLOCK (&type_rw_lock);
1663 g_type_remove_class_cache_func (gpointer cache_data,
1664 GTypeClassCacheFunc cache_func)
1666 gboolean found_it = FALSE;
1669 g_return_if_fail (cache_func != NULL);
1671 G_WRITE_LOCK (&type_rw_lock);
1672 for (i = 0; i < static_n_class_cache_funcs; i++)
1673 if (static_class_cache_funcs[i].cache_data == cache_data &&
1674 static_class_cache_funcs[i].cache_func == cache_func)
1676 static_n_class_cache_funcs--;
1677 g_memmove (static_class_cache_funcs + i,
1678 static_class_cache_funcs + i + 1,
1679 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1680 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1684 G_WRITE_UNLOCK (&type_rw_lock);
1687 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1688 cache_func, cache_data);
1692 /* --- type registration --- */
1694 g_type_register_fundamental (GType type_id,
1695 const gchar *type_name,
1696 const GTypeInfo *info,
1697 const GTypeFundamentalInfo *finfo,
1700 GTypeFundamentalInfo *node_finfo;
1703 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1704 g_return_val_if_fail (type_id > 0, 0);
1705 g_return_val_if_fail (type_name != NULL, 0);
1706 g_return_val_if_fail (info != NULL, 0);
1707 g_return_val_if_fail (finfo != NULL, 0);
1709 if (!check_type_name_U (type_name))
1711 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1713 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1718 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1719 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1721 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1725 G_WRITE_LOCK (&type_rw_lock);
1726 if (lookup_type_node_L (type_id))
1728 G_WRITE_UNLOCK (&type_rw_lock);
1729 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1730 type_descriptive_name_U (type_id),
1735 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1736 node_finfo = type_node_fundamental_info_L (node);
1737 type_add_flags_W (node, flags);
1739 if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1740 type_data_make_W (node, info,
1741 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1742 G_WRITE_UNLOCK (&type_rw_lock);
1744 return NODE_TYPE (node);
1748 g_type_register_static (GType parent_type,
1749 const gchar *type_name,
1750 const GTypeInfo *info,
1753 TypeNode *pnode, *node;
1756 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1757 g_return_val_if_fail (parent_type > 0, 0);
1758 g_return_val_if_fail (type_name != NULL, 0);
1759 g_return_val_if_fail (info != NULL, 0);
1761 if (!check_type_name_U (type_name) ||
1762 !check_derivation_U (parent_type, type_name))
1764 if (info->class_finalize)
1766 g_warning ("class finalizer specified for static type `%s'",
1771 G_WRITE_LOCK (&type_rw_lock);
1772 pnode = lookup_type_node_L (parent_type);
1773 type_data_ref_Wm (pnode);
1774 if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1776 node = type_node_new_W (pnode, type_name, NULL);
1777 type_add_flags_W (node, flags);
1778 type = NODE_TYPE (node);
1779 type_data_make_W (node, info,
1780 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1782 G_WRITE_UNLOCK (&type_rw_lock);
1788 g_type_register_dynamic (GType parent_type,
1789 const gchar *type_name,
1790 GTypePlugin *plugin,
1793 TypeNode *pnode, *node;
1796 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1797 g_return_val_if_fail (parent_type > 0, 0);
1798 g_return_val_if_fail (type_name != NULL, 0);
1799 g_return_val_if_fail (plugin != NULL, 0);
1801 if (!check_type_name_U (type_name) ||
1802 !check_derivation_U (parent_type, type_name) ||
1803 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1806 G_WRITE_LOCK (&type_rw_lock);
1807 pnode = lookup_type_node_L (parent_type);
1808 node = type_node_new_W (pnode, type_name, plugin);
1809 type_add_flags_W (node, flags);
1810 type = NODE_TYPE (node);
1811 G_WRITE_UNLOCK (&type_rw_lock);
1817 g_type_add_interface_static (GType instance_type,
1818 GType interface_type,
1819 const GInterfaceInfo *info)
1821 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1822 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1823 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1825 G_WRITE_LOCK (&type_rw_lock);
1826 if (check_add_interface_L (instance_type, interface_type))
1828 TypeNode *node = lookup_type_node_L (instance_type);
1829 TypeNode *iface = lookup_type_node_L (interface_type);
1831 if (check_interface_info_L (iface, NODE_TYPE (node), info))
1832 type_add_interface_W (node, iface, info, NULL);
1834 G_WRITE_UNLOCK (&type_rw_lock);
1838 g_type_add_interface_dynamic (GType instance_type,
1839 GType interface_type,
1840 GTypePlugin *plugin)
1844 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1845 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1846 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1848 G_READ_LOCK (&type_rw_lock);
1849 node = lookup_type_node_L (instance_type);
1850 G_READ_UNLOCK (&type_rw_lock);
1851 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1854 G_WRITE_LOCK (&type_rw_lock);
1855 if (check_add_interface_L (instance_type, interface_type))
1857 TypeNode *iface = lookup_type_node_L (interface_type);
1859 type_add_interface_W (node, iface, NULL, plugin);
1861 G_WRITE_UNLOCK (&type_rw_lock);
1865 /* --- public API functions --- */
1867 g_type_class_ref (GType type)
1871 /* optimize for common code path
1873 G_WRITE_LOCK (&type_rw_lock);
1874 node = lookup_type_node_L (type);
1875 if (node && node->is_classed && node->data &&
1876 node->data->class.class && node->data->common.ref_count > 0)
1878 type_data_ref_Wm (node);
1879 G_WRITE_UNLOCK (&type_rw_lock);
1881 return node->data->class.class;
1884 if (!node || !node->is_classed ||
1885 (node->data && node->data->common.ref_count < 1))
1887 G_WRITE_UNLOCK (&type_rw_lock);
1888 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1889 type_descriptive_name_U (type));
1893 type_data_ref_Wm (node);
1895 if (!node->data->class.class)
1897 GType ptype = NODE_PARENT_TYPE (node);
1898 GTypeClass *pclass = NULL;
1902 G_WRITE_UNLOCK (&type_rw_lock);
1903 pclass = g_type_class_ref (ptype);
1904 if (node->data->class.class)
1905 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1906 G_WRITE_LOCK (&type_rw_lock);
1909 type_class_init_Wm (node, pclass);
1911 G_WRITE_UNLOCK (&type_rw_lock);
1913 return node->data->class.class;
1917 g_type_class_unref (gpointer g_class)
1920 GTypeClass *class = g_class;
1922 g_return_if_fail (g_class != NULL);
1924 G_WRITE_LOCK (&type_rw_lock);
1925 node = lookup_type_node_L (class->g_type);
1926 if (node && node->is_classed && node->data &&
1927 node->data->class.class == class && node->data->common.ref_count > 0)
1928 type_data_unref_Wm (node, FALSE);
1930 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1931 type_descriptive_name_L (class->g_type));
1932 G_WRITE_UNLOCK (&type_rw_lock);
1936 g_type_class_unref_uncached (gpointer g_class)
1939 GTypeClass *class = g_class;
1941 g_return_if_fail (g_class != NULL);
1943 G_WRITE_LOCK (&type_rw_lock);
1944 node = lookup_type_node_L (class->g_type);
1945 if (node && node->is_classed && node->data &&
1946 node->data->class.class == class && node->data->common.ref_count > 0)
1947 type_data_unref_Wm (node, TRUE);
1949 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1950 type_descriptive_name_L (class->g_type));
1951 G_WRITE_UNLOCK (&type_rw_lock);
1955 g_type_class_peek (GType type)
1960 G_READ_LOCK (&type_rw_lock);
1961 node = lookup_type_node_L (type);
1962 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1963 class = node->data->class.class;
1966 G_READ_UNLOCK (&type_rw_lock);
1972 g_type_class_peek_parent (gpointer g_class)
1977 g_return_val_if_fail (g_class != NULL, NULL);
1979 G_READ_LOCK (&type_rw_lock);
1980 node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
1981 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1983 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
1984 class = node->data->class.class;
1988 G_READ_UNLOCK (&type_rw_lock);
1994 g_type_interface_peek (gpointer instance_class,
1999 gpointer vtable = NULL;
2000 GTypeClass *class = instance_class;
2002 g_return_val_if_fail (instance_class != NULL, NULL);
2004 G_READ_LOCK (&type_rw_lock);
2005 node = lookup_type_node_L (class->g_type);
2006 iface = lookup_type_node_L (iface_type);
2007 if (node && node->is_instantiatable && iface)
2009 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2011 if (entry && entry->vtable)
2012 vtable = entry->vtable;
2014 G_READ_UNLOCK (&type_rw_lock);
2019 G_CONST_RETURN gchar*
2020 g_type_name (GType type)
2024 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, NULL);
2026 G_READ_LOCK (&type_rw_lock);
2027 node = lookup_type_node_L (type);
2028 G_READ_UNLOCK (&type_rw_lock);
2030 return node ? NODE_NAME (node) : NULL;
2034 g_type_qname (GType type)
2038 G_READ_LOCK (&type_rw_lock);
2039 node = lookup_type_node_L (type);
2040 G_READ_UNLOCK (&type_rw_lock);
2042 return node ? node->qname : 0;
2046 g_type_from_name (const gchar *name)
2051 g_return_val_if_fail (name != NULL, 0);
2053 quark = g_quark_try_string (name);
2056 G_READ_LOCK (&type_rw_lock);
2057 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
2058 G_READ_UNLOCK (&type_rw_lock);
2065 g_type_parent (GType type)
2069 G_READ_LOCK (&type_rw_lock);
2070 node = lookup_type_node_L (type);
2071 G_READ_UNLOCK (&type_rw_lock);
2073 return node ? NODE_PARENT_TYPE (node) : 0;
2077 g_type_next_base (GType type,
2083 G_READ_LOCK (&type_rw_lock);
2084 node = lookup_type_node_L (type);
2087 TypeNode *base_node = lookup_type_node_L (base_type);
2089 if (base_node && base_node->n_supers < node->n_supers)
2091 guint n = node->n_supers - base_node->n_supers;
2093 if (node->supers[n] == base_type)
2094 atype = node->supers[n - 1];
2097 G_READ_UNLOCK (&type_rw_lock);
2102 static inline gboolean
2103 type_node_is_a_L (TypeNode *node,
2104 TypeNode *iface_node,
2105 /* support_inheritance */
2106 gboolean support_interfaces,
2107 gboolean support_prerequisites)
2109 if (support_interfaces &&
2110 node->is_instantiatable && NODE_IS_IFACE (iface_node) &&
2111 type_lookup_iface_entry_L (node, iface_node) != NULL)
2113 else if (/* support_inheritance && */
2114 iface_node->n_supers <= node->n_supers &&
2115 node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2117 else if (support_prerequisites &&
2118 NODE_IS_IFACE (node) &&
2119 type_lookup_prerequisite (node, NODE_TYPE (iface_node)))
2126 g_type_is_a (GType type,
2129 TypeNode *node, *iface_node;
2132 G_READ_LOCK (&type_rw_lock);
2133 node = lookup_type_node_L (type);
2134 iface_node = lookup_type_node_L (iface_type);
2135 is_a = node && iface_node && type_node_is_a_L (node, iface_node, TRUE, TRUE);
2136 G_READ_UNLOCK (&type_rw_lock);
2142 g_type_fundamental_branch_last (GType type)
2144 GType ftype = G_TYPE_FUNDAMENTAL (type);
2147 G_READ_LOCK (&type_rw_lock);
2148 last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
2149 G_READ_UNLOCK (&type_rw_lock);
2154 GType* /* free result */
2155 g_type_children (GType type,
2160 G_READ_LOCK (&type_rw_lock);
2161 node = lookup_type_node_L (type);
2164 GType *children = g_new (GType, node->n_children + 1);
2166 memcpy (children, node->children, sizeof (GType) * node->n_children);
2167 children[node->n_children] = 0;
2170 *n_children = node->n_children;
2171 G_READ_UNLOCK (&type_rw_lock);
2177 G_READ_UNLOCK (&type_rw_lock);
2185 GType* /* free result */
2186 g_type_interfaces (GType type,
2187 guint *n_interfaces)
2191 G_READ_LOCK (&type_rw_lock);
2192 node = lookup_type_node_L (type);
2193 if (node && node->is_instantiatable)
2195 GType *ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2198 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2199 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2203 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2204 G_READ_UNLOCK (&type_rw_lock);
2210 G_READ_UNLOCK (&type_rw_lock);
2218 typedef struct _QData QData;
2230 static inline gpointer
2231 type_get_qdata_L (TypeNode *node,
2234 GData *gdata = node->global_gdata;
2236 if (quark && gdata && gdata->n_qdatas)
2238 QData *qdatas = gdata->qdatas - 1;
2239 guint n_qdatas = gdata->n_qdatas;
2246 i = (n_qdatas + 1) / 2;
2248 if (quark == check->quark)
2250 else if (quark > check->quark)
2255 else /* if (quark < check->quark) */
2264 g_type_get_qdata (GType type,
2270 G_READ_LOCK (&type_rw_lock);
2271 node = lookup_type_node_L (type);
2274 data = type_get_qdata_L (node, quark);
2275 G_READ_UNLOCK (&type_rw_lock);
2279 G_READ_UNLOCK (&type_rw_lock);
2280 g_return_val_if_fail (node != NULL, NULL);
2287 type_set_qdata_W (TypeNode *node,
2295 /* setup qdata list if necessary */
2296 if (!node->global_gdata)
2297 node->global_gdata = g_new0 (GData, 1);
2298 gdata = node->global_gdata;
2300 /* try resetting old data */
2301 qdata = gdata->qdatas;
2302 for (i = 0; i < gdata->n_qdatas; i++)
2303 if (qdata[i].quark == quark)
2305 qdata[i].data = data;
2311 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2312 qdata = gdata->qdatas;
2313 for (i = 0; i < gdata->n_qdatas - 1; i++)
2314 if (qdata[i].quark > quark)
2316 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2317 qdata[i].quark = quark;
2318 qdata[i].data = data;
2322 g_type_set_qdata (GType type,
2328 g_return_if_fail (quark != 0);
2330 G_WRITE_LOCK (&type_rw_lock);
2331 node = lookup_type_node_L (type);
2334 type_set_qdata_W (node, quark, data);
2335 G_WRITE_UNLOCK (&type_rw_lock);
2339 G_WRITE_UNLOCK (&type_rw_lock);
2340 g_return_if_fail (node != NULL);
2345 type_add_flags_W (TypeNode *node,
2350 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2351 g_return_if_fail (node != NULL);
2353 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2354 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2355 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2357 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2361 g_type_query (GType type,
2366 g_return_if_fail (query != NULL);
2368 G_READ_LOCK (&type_rw_lock);
2369 node = lookup_type_node_L (type);
2370 if (node && node->is_classed && !node->plugin && node->data)
2372 /* type is classed and static, probably even instantiatable */
2374 query->type = NODE_TYPE (node);
2375 query->type_name = NODE_NAME (node);
2376 query->class_size = node->data->class.class_size;
2377 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2381 /* node is not static and classed, won't allow query */
2384 G_READ_UNLOCK (&type_rw_lock);
2388 /* --- implementation details --- */
2390 g_type_check_flags (GType type,
2394 gboolean result = FALSE;
2396 G_READ_LOCK (&type_rw_lock);
2397 node = lookup_type_node_L (type);
2400 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2401 guint tflags = flags & TYPE_FLAG_MASK;
2405 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2407 fflags = (finfo->type_flags & fflags) == fflags;
2413 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2417 result = tflags && fflags;
2419 G_READ_UNLOCK (&type_rw_lock);
2425 g_type_get_plugin (GType type)
2429 G_READ_LOCK (&type_rw_lock);
2430 node = lookup_type_node_L (type);
2431 G_READ_UNLOCK (&type_rw_lock);
2433 return node ? node->plugin : NULL;
2437 g_type_interface_get_plugin (GType instance_type,
2438 GType interface_type)
2443 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2445 G_READ_LOCK (&type_rw_lock);
2446 node = lookup_type_node_L (instance_type);
2447 iface = lookup_type_node_L (interface_type);
2450 IFaceHolder *iholder = iface_node_get_holders_L (iface);
2452 while (iholder && iholder->instance_type != instance_type)
2453 iholder = iholder->next;
2454 G_READ_UNLOCK (&type_rw_lock);
2457 return iholder->plugin;
2460 G_READ_UNLOCK (&type_rw_lock);
2462 g_return_val_if_fail (node == NULL, NULL);
2463 g_return_val_if_fail (iface == NULL, NULL);
2465 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2471 g_type_fundamental_last (void)
2475 G_READ_LOCK (&type_rw_lock);
2476 type = static_last_fundamental_id;
2477 G_READ_UNLOCK (&type_rw_lock);
2483 g_type_instance_is_a (GTypeInstance *type_instance,
2486 TypeNode *node, *iface;
2489 if (!type_instance || !type_instance->g_class)
2492 G_READ_LOCK (&type_rw_lock);
2493 node = lookup_type_node_L (type_instance->g_class->g_type);
2494 iface = lookup_type_node_L (iface_type);
2495 check = node && node->is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2496 G_READ_UNLOCK (&type_rw_lock);
2502 g_type_class_is_a (GTypeClass *type_class,
2505 TypeNode *node, *iface;
2511 G_READ_LOCK (&type_rw_lock);
2512 node = lookup_type_node_L (type_class->g_type);
2513 iface = lookup_type_node_L (is_a_type);
2514 check = node && node->is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2515 G_READ_UNLOCK (&type_rw_lock);
2521 g_type_check_instance_cast (GTypeInstance *type_instance,
2526 if (type_instance->g_class)
2528 TypeNode *node, *iface;
2529 gboolean is_instantiatable, check;
2531 G_READ_LOCK (&type_rw_lock);
2532 node = lookup_type_node_L (type_instance->g_class->g_type);
2533 is_instantiatable = node && node->is_instantiatable;
2534 iface = lookup_type_node_L (iface_type);
2535 check = is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2536 G_READ_UNLOCK (&type_rw_lock);
2538 return type_instance;
2540 if (is_instantiatable)
2541 g_warning ("invalid cast from `%s' to `%s'",
2542 type_descriptive_name_U (type_instance->g_class->g_type),
2543 type_descriptive_name_U (iface_type));
2545 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2546 type_descriptive_name_U (type_instance->g_class->g_type),
2547 type_descriptive_name_U (iface_type));
2550 g_warning ("invalid unclassed pointer in cast to `%s'",
2551 type_descriptive_name_U (iface_type));
2554 g_warning ("invalid cast from (NULL) pointer to `%s'",
2555 type_descriptive_name_U (iface_type));
2557 return type_instance;
2561 g_type_check_class_cast (GTypeClass *type_class,
2566 TypeNode *node, *iface;
2567 gboolean is_classed, check;
2569 G_READ_LOCK (&type_rw_lock);
2570 node = lookup_type_node_L (type_class->g_type);
2571 is_classed = node && node->is_classed;
2572 iface = lookup_type_node_L (is_a_type);
2573 check = is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2574 G_READ_UNLOCK (&type_rw_lock);
2579 g_warning ("invalid class cast from `%s' to `%s'",
2580 type_descriptive_name_U (type_class->g_type),
2581 type_descriptive_name_U (is_a_type));
2583 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2584 type_descriptive_name_U (type_class->g_type),
2585 type_descriptive_name_U (is_a_type));
2588 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2589 type_descriptive_name_U (is_a_type));
2594 g_type_check_instance (GTypeInstance *type_instance)
2596 /* this function is just here to make the signal system
2597 * conveniently elaborated on instance checks
2601 if (type_instance->g_class)
2604 gboolean is_instantiatable;
2606 G_READ_LOCK (&type_rw_lock);
2607 node = lookup_type_node_L (type_instance->g_class->g_type);
2608 is_instantiatable = node && node->is_instantiatable;
2609 G_READ_UNLOCK (&type_rw_lock);
2610 if (is_instantiatable)
2613 g_warning ("instance of invalid non-instantiatable type `%s'",
2614 type_descriptive_name_U (type_instance->g_class->g_type));
2617 g_warning ("instance with invalid (NULL) class pointer");
2620 g_warning ("invalid (NULL) pointer instance");
2625 static inline gboolean
2626 type_check_is_value_type_U (GType type)
2628 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2631 G_READ_LOCK (&type_rw_lock);
2633 node = lookup_type_node_L (type);
2636 if (node->data && node->data->common.ref_count > 0 &&
2637 node->data->common.value_table->value_init)
2638 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2639 else if (NODE_IS_IFACE (node))
2643 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2645 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2646 TypeNode *prnode = lookup_type_node_L (prtype);
2648 if (prnode->is_instantiatable)
2656 G_READ_UNLOCK (&type_rw_lock);
2658 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2662 g_type_check_is_value_type (GType type)
2664 return type_check_is_value_type_U (type);
2668 g_type_check_value (GValue *value)
2670 return value && type_check_is_value_type_U (value->g_type);
2674 g_type_check_value_holds (GValue *value,
2677 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2681 g_type_value_table_peek (GType type)
2684 GTypeValueTable *vtable;
2686 G_READ_LOCK (&type_rw_lock);
2688 node = lookup_type_node_L (type);
2691 g_warning ("type id `%u' is invalid", type);
2692 G_READ_UNLOCK (&type_rw_lock);
2695 if (!node->data || node->data->common.ref_count < 1)
2697 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2698 type_descriptive_name_L (type));
2699 G_READ_UNLOCK (&type_rw_lock);
2702 if (node->data->common.value_table->value_init)
2703 vtable = node->data->common.value_table;
2704 else if (NODE_IS_IFACE (node))
2708 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2710 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2711 TypeNode *prnode = lookup_type_node_L (prtype);
2713 if (prnode->is_instantiatable)
2716 goto restart_table_peek;
2723 G_READ_UNLOCK (&type_rw_lock);
2729 /* --- foreign prototypes --- */
2730 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2731 extern void g_enum_types_init (void); /* sync with genums.c */
2732 extern void g_param_type_init (void); /* sync with gparam.c */
2733 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2734 extern void g_object_type_init (void); /* sync with gobject.c */
2735 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2736 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2737 extern void g_signal_init (void); /* sync with gsignal.c */
2740 /* --- initialization --- */
2742 g_type_init (GTypeDebugFlags debug_flags)
2744 G_LOCK_DEFINE_STATIC (type_init_lock);
2745 static TypeNode *type0_node = NULL;
2746 const gchar *env_string;
2751 G_LOCK (type_init_lock);
2753 G_WRITE_LOCK (&type_rw_lock);
2755 if (static_last_fundamental_id)
2757 G_WRITE_UNLOCK (&type_rw_lock);
2758 G_UNLOCK (type_init_lock);
2762 /* setup GRuntime wide debugging flags */
2763 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2764 env_string = g_getenv ("GRUNTIME_DEBUG");
2765 if (env_string != NULL)
2767 static GDebugKey debug_keys[] = {
2768 { "objects", G_TYPE_DEBUG_OBJECTS },
2769 { "signals", G_TYPE_DEBUG_SIGNALS },
2772 _g_type_debug_flags |= g_parse_debug_string (env_string,
2774 sizeof (debug_keys) / sizeof (debug_keys[0]));
2779 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
2780 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
2781 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
2783 /* type qname hash table */
2784 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2786 /* invalid type G_TYPE_INVALID (0)
2788 static_last_fundamental_id = 1;
2789 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2790 static_type_nodes[0] = &type0_node;
2791 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2792 static_branch_seqnos[0] = 1;
2794 /* void type G_TYPE_NONE
2796 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2797 type = NODE_TYPE (node);
2798 g_assert (type == G_TYPE_NONE);
2800 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2802 memset (&info, 0, sizeof (info));
2803 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2804 type = NODE_TYPE (node);
2805 type_data_make_W (node, &info, NULL);
2806 g_assert (type == G_TYPE_INTERFACE);
2808 G_WRITE_UNLOCK (&type_rw_lock);
2810 /* G_TYPE_TYPE_PLUGIN
2812 g_type_plugin_get_type ();
2814 /* G_TYPE_* value types
2816 g_value_types_init ();
2818 /* G_TYPE_ENUM & G_TYPE_FLAGS
2820 g_enum_types_init ();
2824 g_boxed_type_init ();
2828 g_param_type_init ();
2832 g_object_type_init ();
2834 /* G_TYPE_PARAM_* pspec types
2836 g_param_spec_types_init ();
2838 /* Value Transformations
2840 g_value_transforms_init ();
2846 G_UNLOCK (type_init_lock);