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_L (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 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
821 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
827 tnode = find_conforming_type_L (node, iface); // FIXME: iface overriding
830 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
840 check_interface_info_L (TypeNode *iface,
842 const GInterfaceInfo *info)
844 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
846 g_warning ("interface type `%s' for type `%s' comes without initializer",
848 type_descriptive_name_L (instance_type));
855 /* --- type info (type node data) --- */
857 type_data_make_W (TypeNode *node,
858 const GTypeInfo *info,
859 const GTypeValueTable *value_table)
862 GTypeValueTable *vtable = NULL;
863 guint vtable_size = 0;
865 g_assert (node->data == NULL && info != NULL);
869 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
872 vtable = pnode->data->common.value_table;
875 static const GTypeValueTable zero_vtable = { NULL, };
877 value_table = &zero_vtable;
882 /* need to setup vtable_size since we have to allocate it with data in one chunk */
883 vtable_size = sizeof (GTypeValueTable);
884 if (value_table->collect_format)
885 vtable_size += strlen (value_table->collect_format);
886 if (value_table->lcopy_format)
887 vtable_size += strlen (value_table->lcopy_format);
891 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
893 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
895 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
896 data->instance.class_size = info->class_size;
897 data->instance.class_init_base = info->base_init;
898 data->instance.class_finalize_base = info->base_finalize;
899 data->instance.class_init = info->class_init;
900 data->instance.class_finalize = info->class_finalize;
901 data->instance.class_data = info->class_data;
902 data->instance.class = NULL;
903 data->instance.instance_size = info->instance_size;
904 #ifdef DISABLE_MEM_POOLS
905 data->instance.n_preallocs = 0;
906 #else /* !DISABLE_MEM_POOLS */
907 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
908 #endif /* !DISABLE_MEM_POOLS */
909 data->instance.instance_init = info->instance_init;
910 data->instance.mem_chunk = NULL;
912 else if (node->is_classed) /* only classed */
914 data = g_malloc0 (sizeof (ClassData) + vtable_size);
916 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
917 data->class.class_size = info->class_size;
918 data->class.class_init_base = info->base_init;
919 data->class.class_finalize_base = info->base_finalize;
920 data->class.class_init = info->class_init;
921 data->class.class_finalize = info->class_finalize;
922 data->class.class_data = info->class_data;
923 data->class.class = NULL;
925 else if (NODE_IS_IFACE (node))
927 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
929 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
930 data->iface.vtable_size = info->class_size;
931 data->iface.vtable_init_base = info->base_init;
932 data->iface.vtable_finalize_base = info->base_finalize;
936 data = g_malloc0 (sizeof (CommonData) + vtable_size);
938 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
942 node->data->common.ref_count = 1;
948 /* we allocate the vtable and its strings together with the type data, so
949 * children can take over their parent's vtable pointer, and we don't
950 * need to worry freeing it or not when the child data is destroyed
952 *vtable = *value_table;
953 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
955 vtable->collect_format = p;
956 if (value_table->collect_format)
958 strcat (p, value_table->collect_format);
959 p += strlen (value_table->collect_format);
963 vtable->lcopy_format = p;
964 if (value_table->lcopy_format)
965 strcat (p, value_table->lcopy_format);
967 node->data->common.value_table = vtable;
969 g_assert (node->data->common.value_table != NULL); /* paranoid */
973 type_data_ref_Wm (TypeNode *node)
977 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
979 GTypeValueTable tmp_value_table;
981 g_assert (node->plugin != NULL);
985 type_data_ref_Wm (pnode);
987 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
990 memset (&tmp_info, 0, sizeof (tmp_info));
991 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
993 G_WRITE_UNLOCK (&type_rw_lock);
994 g_type_plugin_use (node->plugin);
995 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
996 G_WRITE_LOCK (&type_rw_lock);
998 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1000 check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
1001 type_data_make_W (node, &tmp_info,
1002 check_value_table_I (NODE_NAME (node),
1003 &tmp_value_table) ? &tmp_value_table : NULL);
1007 g_assert (node->data->common.ref_count > 0);
1009 node->data->common.ref_count += 1;
1014 type_data_unref_Wm (TypeNode *node,
1017 g_assert (node->data && node->data->common.ref_count);
1019 if (node->data->common.ref_count > 1)
1020 node->data->common.ref_count -= 1;
1025 g_warning ("static type `%s' unreferenced too often",
1030 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1035 type_node_add_iface_entry_W (TypeNode *node,
1038 IFaceEntry *entries;
1041 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1043 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1044 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1045 if (entries[i].iface_type == iface_type)
1047 g_warning ("failed to add interface, type `%s' already conforms to interface type `%s'",
1048 type_descriptive_name_L (NODE_TYPE (node)),
1049 type_descriptive_name_L (iface_type));
1052 else if (entries[i].iface_type > iface_type)
1054 CLASSED_NODE_N_IFACES (node) += 1;
1055 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1056 CLASSED_NODE_IFACES_ENTRIES (node),
1057 CLASSED_NODE_N_IFACES (node));
1058 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1059 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1060 entries[i].iface_type = iface_type;
1061 entries[i].vtable = NULL;
1063 for (i = 0; i < node->n_children; i++)
1064 type_node_add_iface_entry_W (lookup_type_node_L (node->children[i]), iface_type);
1068 type_add_interface_W (TypeNode *node,
1070 const GInterfaceInfo *info,
1071 GTypePlugin *plugin)
1073 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1075 /* we must not call any functions of GInterfaceInfo from within here, since
1076 * we got most probably called from _within_ a type registration function
1078 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1080 iholder->next = iface_node_get_holders_L (iface);
1081 iface_node_set_holders_W (iface, iholder);
1082 iholder->instance_type = NODE_TYPE (node);
1083 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1084 iholder->plugin = plugin;
1086 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1090 type_iface_add_prerequisite_W (TypeNode *iface,
1091 TypeNode *prerequisite_node)
1093 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1094 GType *prerequisites, *dependants;
1095 guint n_dependants, i;
1097 g_assert (NODE_IS_IFACE (iface) &&
1098 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1099 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1101 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1102 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1103 if (prerequisites[i] == prerequisite_type)
1104 return; /* we already have that prerequisiste */
1105 else if (prerequisites[i] > prerequisite_type)
1107 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1108 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1109 IFACE_NODE_PREREQUISITES (iface),
1110 IFACE_NODE_N_PREREQUISITES (iface));
1111 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1112 g_memmove (prerequisites + i + 1, prerequisites + i,
1113 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1114 prerequisites[i] = prerequisite_type;
1116 /* we want to get notified when prerequisites get added to prerequisite_node */
1117 if (NODE_IS_IFACE (prerequisite_node))
1119 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1120 n_dependants = dependants ? dependants[0] : 0;
1122 dependants = g_renew (GType, dependants, n_dependants + 1);
1123 dependants[n_dependants] = NODE_TYPE (iface);
1124 dependants[0] = n_dependants;
1125 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1128 /* we need to notify all dependants */
1129 dependants = iface_node_get_dependants_array_L (iface);
1130 n_dependants = dependants ? dependants[0] : 0;
1131 for (i = 1; i <= n_dependants; i++)
1132 type_iface_add_prerequisite_W (lookup_type_node_L (dependants[i]), prerequisite_node);
1136 g_type_interface_add_prerequisite (GType interface_type,
1137 GType prerequisite_type)
1139 TypeNode *iface, *prerequisite_node;
1140 IFaceHolder *holders;
1142 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));
1143 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1144 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1146 G_WRITE_LOCK (&type_rw_lock);
1147 iface = lookup_type_node_L (interface_type);
1148 prerequisite_node = lookup_type_node_L (prerequisite_type);
1149 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1151 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1152 type_descriptive_name_L (interface_type),
1153 type_descriptive_name_L (prerequisite_type));
1154 G_WRITE_UNLOCK (&type_rw_lock);
1157 holders = iface_node_get_holders_L (iface);
1160 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1161 type_descriptive_name_L (prerequisite_type),
1162 type_descriptive_name_L (interface_type),
1163 type_descriptive_name_L (holders->instance_type));
1164 G_WRITE_UNLOCK (&type_rw_lock);
1167 if (prerequisite_node->is_instantiatable)
1171 /* can have at most one publically installable instantiatable prerequisite */
1172 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1174 TypeNode *prnode = lookup_type_node_L (IFACE_NODE_PREREQUISITES (iface)[i]);
1176 if (prnode->is_instantiatable)
1178 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1179 type_descriptive_name_L (prerequisite_type),
1180 type_descriptive_name_L (interface_type),
1181 type_descriptive_name_L (NODE_TYPE (prnode)));
1182 G_WRITE_UNLOCK (&type_rw_lock);
1187 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1188 type_iface_add_prerequisite_W (iface, lookup_type_node_L (prerequisite_node->supers[i]));
1190 else if (NODE_IS_IFACE (prerequisite_node))
1193 guint n_dependants, i;
1195 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1196 n_dependants = dependants ? dependants[0] : 0;
1197 for (i = 1; i <= n_dependants; i++)
1198 type_iface_add_prerequisite_W (iface, lookup_type_node_L (dependants[i]));
1199 type_iface_add_prerequisite_W (iface, prerequisite_node);
1202 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1203 type_descriptive_name_L (prerequisite_type),
1204 type_descriptive_name_L (interface_type));
1205 G_WRITE_UNLOCK (&type_rw_lock);
1209 type_iface_retrive_holder_info_Wm (TypeNode *iface,
1210 GType instance_type)
1212 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1214 g_assert (NODE_IS_IFACE (iface));
1216 while (iholder->instance_type != instance_type)
1217 iholder = iholder->next;
1221 GInterfaceInfo tmp_info;
1223 g_assert (iholder->plugin != NULL);
1225 type_data_ref_Wm (iface);
1227 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1229 memset (&tmp_info, 0, sizeof (tmp_info));
1231 G_WRITE_UNLOCK (&type_rw_lock);
1232 g_type_plugin_use (iholder->plugin);
1233 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1234 G_WRITE_LOCK (&type_rw_lock);
1236 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1238 check_interface_info_L (iface, instance_type, &tmp_info);
1239 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1246 type_iface_blow_holder_info_Wm (TypeNode *iface,
1247 GType instance_type)
1249 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1251 g_assert (NODE_IS_IFACE (iface));
1253 while (iholder->instance_type != instance_type)
1254 iholder = iholder->next;
1256 if (iholder->info && iholder->plugin)
1258 g_free (iholder->info);
1259 iholder->info = NULL;
1261 G_WRITE_UNLOCK (&type_rw_lock);
1262 g_type_plugin_unuse (iholder->plugin);
1263 G_WRITE_LOCK (&type_rw_lock);
1265 type_data_unref_Wm (iface, FALSE);
1270 /* --- type structure creation/destruction --- */
1272 g_type_create_instance (GType type)
1275 GTypeInstance *instance;
1279 G_READ_LOCK (&type_rw_lock);
1280 node = lookup_type_node_L (type);
1281 G_READ_UNLOCK (&type_rw_lock);
1282 if (!node || !node->is_instantiatable)
1284 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1285 type_descriptive_name_U (type));
1288 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1289 if (G_TYPE_IS_ABSTRACT (type))
1291 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1292 type_descriptive_name_U (type));
1296 class = g_type_class_ref (type);
1298 if (node->data->instance.n_preallocs)
1300 G_WRITE_LOCK (&type_rw_lock);
1301 if (!node->data->instance.mem_chunk)
1302 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1303 node->data->instance.instance_size,
1304 (node->data->instance.instance_size *
1305 node->data->instance.n_preallocs),
1307 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1308 G_WRITE_UNLOCK (&type_rw_lock);
1311 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1312 for (i = node->n_supers; i > 0; i--)
1316 G_READ_LOCK (&type_rw_lock);
1317 pnode = lookup_type_node_L (node->supers[i]);
1318 G_READ_UNLOCK (&type_rw_lock);
1320 if (pnode->data->instance.instance_init)
1322 instance->g_class = pnode->data->instance.class;
1323 pnode->data->instance.instance_init (instance, class);
1326 instance->g_class = class;
1328 if (node->data->instance.instance_init)
1329 node->data->instance.instance_init (instance, class);
1335 g_type_free_instance (GTypeInstance *instance)
1340 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1342 G_READ_LOCK (&type_rw_lock);
1343 class = instance->g_class;
1344 node = lookup_type_node_L (class->g_type);
1345 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1347 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1348 type_descriptive_name_L (class->g_type));
1349 G_READ_UNLOCK (&type_rw_lock);
1352 G_READ_UNLOCK (&type_rw_lock);
1353 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1354 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1356 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1361 instance->g_class = NULL;
1362 memset (instance, 0xaa, node->data->instance.instance_size); /* FIXME: debugging hack */
1363 if (node->data->instance.n_preallocs)
1365 G_WRITE_LOCK (&type_rw_lock);
1366 g_chunk_free (instance, node->data->instance.mem_chunk);
1367 G_WRITE_UNLOCK (&type_rw_lock);
1372 g_type_class_unref (class);
1376 type_propagate_iface_vtable_W (TypeNode *pnode,
1378 GTypeInterface *vtable)
1380 IFaceEntry *entry = type_lookup_iface_entry_L (pnode, iface);
1383 entry->vtable = vtable;
1384 for (i = 0; i < pnode->n_children; i++)
1386 TypeNode *node = lookup_type_node_L (pnode->children[i]);
1388 type_propagate_iface_vtable_W (node, iface, vtable);
1393 type_iface_vtable_init_Wm (TypeNode *iface,
1396 #ifndef G_DISABLE_ASSERT
1397 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1399 IFaceHolder *iholder = type_iface_retrive_holder_info_Wm (iface, NODE_TYPE (node));
1400 GTypeInterface *vtable;
1402 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1404 vtable = g_malloc0 (iface->data->iface.vtable_size);
1405 type_propagate_iface_vtable_W (node, iface, vtable);
1406 vtable->g_type = NODE_TYPE (iface);
1407 vtable->g_instance_type = NODE_TYPE (node);
1409 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1411 G_WRITE_UNLOCK (&type_rw_lock);
1412 if (iface->data->iface.vtable_init_base)
1413 iface->data->iface.vtable_init_base (vtable);
1414 if (iholder->info->interface_init)
1415 iholder->info->interface_init (vtable, iholder->info->interface_data);
1416 G_WRITE_LOCK (&type_rw_lock);
1421 type_iface_vtable_finalize_Wm (TypeNode *iface,
1423 GTypeInterface *vtable)
1425 #ifndef G_DISABLE_ASSERT
1426 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1428 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1430 g_assert (entry && entry->vtable == vtable);
1432 while (iholder->instance_type != NODE_TYPE (node))
1433 iholder = iholder->next;
1434 g_assert (iholder && iholder->info);
1436 type_propagate_iface_vtable_W (node, iface, NULL);
1437 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1439 G_WRITE_UNLOCK (&type_rw_lock);
1440 if (iholder->info->interface_finalize)
1441 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1442 if (iface->data->iface.vtable_finalize_base)
1443 iface->data->iface.vtable_finalize_base (vtable);
1444 G_WRITE_LOCK (&type_rw_lock);
1447 vtable->g_instance_type = 0;
1450 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1454 type_class_init_Wm (TypeNode *node,
1457 GSList *slist, *init_slist = NULL;
1463 g_assert (node->is_classed && node->data &&
1464 node->data->class.class_size &&
1465 !node->data->class.class);
1467 class = g_malloc0 (node->data->class.class_size);
1468 node->data->class.class = class;
1472 TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1474 memcpy (class, pclass, pnode->data->class.class_size);
1476 class->g_type = NODE_TYPE (node);
1478 G_WRITE_UNLOCK (&type_rw_lock);
1480 /* stack all base class initialization functions, so we
1481 * call them in ascending order.
1483 G_READ_LOCK (&type_rw_lock);
1484 for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1485 if (bnode->data->class.class_init_base)
1486 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1487 G_READ_UNLOCK (&type_rw_lock);
1488 for (slist = init_slist; slist; slist = slist->next)
1490 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1492 class_init_base (class);
1494 g_slist_free (init_slist);
1496 if (node->data->class.class_init)
1497 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1499 G_WRITE_LOCK (&type_rw_lock);
1501 /* ok, we got the class done, now initialize all interfaces */
1502 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1503 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1504 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1507 type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node);
1509 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1510 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1511 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1516 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1521 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1523 g_message ("finalizing interfaces for %sClass `%s'",
1524 type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1525 type_descriptive_name_L (NODE_TYPE (node)));
1527 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1528 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1529 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1530 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1533 type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable);
1535 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1536 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1537 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1538 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1543 type_data_finalize_class_U (TypeNode *node,
1546 GTypeClass *class = cdata->class;
1549 g_assert (cdata->class && cdata->common.ref_count == 0);
1551 if (cdata->class_finalize)
1552 cdata->class_finalize (class, (gpointer) cdata->class_data);
1554 /* call all base class destruction functions in descending order
1556 if (cdata->class_finalize_base)
1557 cdata->class_finalize_base (class);
1558 G_READ_LOCK (&type_rw_lock);
1559 for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1560 if (bnode->data->class.class_finalize_base)
1562 G_READ_UNLOCK (&type_rw_lock);
1563 bnode->data->class.class_finalize_base (class);
1564 G_READ_LOCK (&type_rw_lock);
1566 G_READ_UNLOCK (&type_rw_lock);
1569 g_free (cdata->class);
1573 type_data_last_unref_Wm (GType type,
1576 TypeNode *node = lookup_type_node_L (type);
1578 g_return_if_fail (node != NULL && node->plugin != NULL);
1580 if (!node->data || node->data->common.ref_count == 0)
1582 g_warning ("cannot drop last reference to unreferenced type `%s'",
1583 type_descriptive_name_U (type));
1587 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1591 G_WRITE_UNLOCK (&type_rw_lock);
1592 G_READ_LOCK (&type_rw_lock);
1593 for (i = 0; i < static_n_class_cache_funcs; i++)
1595 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1596 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1597 gboolean need_break;
1599 G_READ_UNLOCK (&type_rw_lock);
1600 need_break = cache_func (cache_data, node->data->class.class);
1601 G_READ_LOCK (&type_rw_lock);
1602 if (!node->data || node->data->common.ref_count == 0)
1603 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1607 G_READ_UNLOCK (&type_rw_lock);
1608 G_WRITE_LOCK (&type_rw_lock);
1611 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1612 node->data->common.ref_count -= 1;
1615 GType ptype = NODE_PARENT_TYPE (node);
1618 node->data->common.ref_count = 0;
1620 if (node->is_instantiatable && node->data->instance.mem_chunk)
1622 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1623 node->data->instance.mem_chunk = NULL;
1627 if (node->is_classed && tdata->class.class)
1629 if (CLASSED_NODE_N_IFACES (node))
1630 type_data_finalize_class_ifaces_Wm (node);
1632 G_WRITE_UNLOCK (&type_rw_lock);
1633 type_data_finalize_class_U (node, &tdata->class);
1634 G_WRITE_LOCK (&type_rw_lock);
1639 /* freeing tdata->common.value_table and its contents is taking care of
1640 * by allocating it in one chunk with tdata
1645 type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1646 G_WRITE_UNLOCK (&type_rw_lock);
1647 g_type_plugin_unuse (node->plugin);
1648 G_WRITE_LOCK (&type_rw_lock);
1653 g_type_add_class_cache_func (gpointer cache_data,
1654 GTypeClassCacheFunc cache_func)
1658 g_return_if_fail (cache_func != NULL);
1660 G_WRITE_LOCK (&type_rw_lock);
1661 i = static_n_class_cache_funcs++;
1662 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1663 static_class_cache_funcs[i].cache_data = cache_data;
1664 static_class_cache_funcs[i].cache_func = cache_func;
1665 G_WRITE_UNLOCK (&type_rw_lock);
1669 g_type_remove_class_cache_func (gpointer cache_data,
1670 GTypeClassCacheFunc cache_func)
1672 gboolean found_it = FALSE;
1675 g_return_if_fail (cache_func != NULL);
1677 G_WRITE_LOCK (&type_rw_lock);
1678 for (i = 0; i < static_n_class_cache_funcs; i++)
1679 if (static_class_cache_funcs[i].cache_data == cache_data &&
1680 static_class_cache_funcs[i].cache_func == cache_func)
1682 static_n_class_cache_funcs--;
1683 g_memmove (static_class_cache_funcs + i,
1684 static_class_cache_funcs + i + 1,
1685 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1686 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1690 G_WRITE_UNLOCK (&type_rw_lock);
1693 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1694 cache_func, cache_data);
1698 /* --- type registration --- */
1700 g_type_register_fundamental (GType type_id,
1701 const gchar *type_name,
1702 const GTypeInfo *info,
1703 const GTypeFundamentalInfo *finfo,
1706 GTypeFundamentalInfo *node_finfo;
1709 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1710 g_return_val_if_fail (type_id > 0, 0);
1711 g_return_val_if_fail (type_name != NULL, 0);
1712 g_return_val_if_fail (info != NULL, 0);
1713 g_return_val_if_fail (finfo != NULL, 0);
1715 if (!check_type_name_U (type_name))
1717 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1719 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1724 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1725 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1727 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1731 G_WRITE_LOCK (&type_rw_lock);
1732 if (lookup_type_node_L (type_id))
1734 G_WRITE_UNLOCK (&type_rw_lock);
1735 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1736 type_descriptive_name_U (type_id),
1741 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1742 node_finfo = type_node_fundamental_info_L (node);
1743 type_add_flags_W (node, flags);
1745 if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1746 type_data_make_W (node, info,
1747 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1748 G_WRITE_UNLOCK (&type_rw_lock);
1750 return NODE_TYPE (node);
1754 g_type_register_static (GType parent_type,
1755 const gchar *type_name,
1756 const GTypeInfo *info,
1759 TypeNode *pnode, *node;
1762 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1763 g_return_val_if_fail (parent_type > 0, 0);
1764 g_return_val_if_fail (type_name != NULL, 0);
1765 g_return_val_if_fail (info != NULL, 0);
1767 if (!check_type_name_U (type_name) ||
1768 !check_derivation_U (parent_type, type_name))
1770 if (info->class_finalize)
1772 g_warning ("class finalizer specified for static type `%s'",
1777 G_WRITE_LOCK (&type_rw_lock);
1778 pnode = lookup_type_node_L (parent_type);
1779 type_data_ref_Wm (pnode);
1780 if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1782 node = type_node_new_W (pnode, type_name, NULL);
1783 type_add_flags_W (node, flags);
1784 type = NODE_TYPE (node);
1785 type_data_make_W (node, info,
1786 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1788 G_WRITE_UNLOCK (&type_rw_lock);
1794 g_type_register_dynamic (GType parent_type,
1795 const gchar *type_name,
1796 GTypePlugin *plugin,
1799 TypeNode *pnode, *node;
1802 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1803 g_return_val_if_fail (parent_type > 0, 0);
1804 g_return_val_if_fail (type_name != NULL, 0);
1805 g_return_val_if_fail (plugin != NULL, 0);
1807 if (!check_type_name_U (type_name) ||
1808 !check_derivation_U (parent_type, type_name) ||
1809 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1812 G_WRITE_LOCK (&type_rw_lock);
1813 pnode = lookup_type_node_L (parent_type);
1814 node = type_node_new_W (pnode, type_name, plugin);
1815 type_add_flags_W (node, flags);
1816 type = NODE_TYPE (node);
1817 G_WRITE_UNLOCK (&type_rw_lock);
1823 g_type_add_interface_static (GType instance_type,
1824 GType interface_type,
1825 const GInterfaceInfo *info)
1827 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1828 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1829 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1831 G_WRITE_LOCK (&type_rw_lock);
1832 if (check_add_interface_L (instance_type, interface_type))
1834 TypeNode *node = lookup_type_node_L (instance_type);
1835 TypeNode *iface = lookup_type_node_L (interface_type);
1837 if (check_interface_info_L (iface, NODE_TYPE (node), info))
1838 type_add_interface_W (node, iface, info, NULL);
1840 G_WRITE_UNLOCK (&type_rw_lock);
1844 g_type_add_interface_dynamic (GType instance_type,
1845 GType interface_type,
1846 GTypePlugin *plugin)
1850 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1851 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1852 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1854 G_READ_LOCK (&type_rw_lock);
1855 node = lookup_type_node_L (instance_type);
1856 G_READ_UNLOCK (&type_rw_lock);
1857 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1860 G_WRITE_LOCK (&type_rw_lock);
1861 if (check_add_interface_L (instance_type, interface_type))
1863 TypeNode *iface = lookup_type_node_L (interface_type);
1865 type_add_interface_W (node, iface, NULL, plugin);
1867 G_WRITE_UNLOCK (&type_rw_lock);
1871 /* --- public API functions --- */
1873 g_type_class_ref (GType type)
1877 /* optimize for common code path
1879 G_WRITE_LOCK (&type_rw_lock);
1880 node = lookup_type_node_L (type);
1881 if (node && node->is_classed && node->data &&
1882 node->data->class.class && node->data->common.ref_count > 0)
1884 type_data_ref_Wm (node);
1885 G_WRITE_UNLOCK (&type_rw_lock);
1887 return node->data->class.class;
1890 if (!node || !node->is_classed ||
1891 (node->data && node->data->common.ref_count < 1))
1893 G_WRITE_UNLOCK (&type_rw_lock);
1894 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1895 type_descriptive_name_U (type));
1899 type_data_ref_Wm (node);
1901 if (!node->data->class.class)
1903 GType ptype = NODE_PARENT_TYPE (node);
1904 GTypeClass *pclass = NULL;
1908 G_WRITE_UNLOCK (&type_rw_lock);
1909 pclass = g_type_class_ref (ptype);
1910 if (node->data->class.class)
1911 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1912 G_WRITE_LOCK (&type_rw_lock);
1915 type_class_init_Wm (node, pclass);
1917 G_WRITE_UNLOCK (&type_rw_lock);
1919 return node->data->class.class;
1923 g_type_class_unref (gpointer g_class)
1926 GTypeClass *class = g_class;
1928 g_return_if_fail (g_class != NULL);
1930 G_WRITE_LOCK (&type_rw_lock);
1931 node = lookup_type_node_L (class->g_type);
1932 if (node && node->is_classed && node->data &&
1933 node->data->class.class == class && node->data->common.ref_count > 0)
1934 type_data_unref_Wm (node, FALSE);
1936 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1937 type_descriptive_name_L (class->g_type));
1938 G_WRITE_UNLOCK (&type_rw_lock);
1942 g_type_class_unref_uncached (gpointer g_class)
1945 GTypeClass *class = g_class;
1947 g_return_if_fail (g_class != NULL);
1949 G_WRITE_LOCK (&type_rw_lock);
1950 node = lookup_type_node_L (class->g_type);
1951 if (node && node->is_classed && node->data &&
1952 node->data->class.class == class && node->data->common.ref_count > 0)
1953 type_data_unref_Wm (node, TRUE);
1955 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1956 type_descriptive_name_L (class->g_type));
1957 G_WRITE_UNLOCK (&type_rw_lock);
1961 g_type_class_peek (GType type)
1966 G_READ_LOCK (&type_rw_lock);
1967 node = lookup_type_node_L (type);
1968 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1969 class = node->data->class.class;
1972 G_READ_UNLOCK (&type_rw_lock);
1978 g_type_class_peek_parent (gpointer g_class)
1983 g_return_val_if_fail (g_class != NULL, NULL);
1985 G_READ_LOCK (&type_rw_lock);
1986 node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
1987 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1989 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
1990 class = node->data->class.class;
1994 G_READ_UNLOCK (&type_rw_lock);
2000 g_type_interface_peek (gpointer instance_class,
2005 gpointer vtable = NULL;
2006 GTypeClass *class = instance_class;
2008 g_return_val_if_fail (instance_class != NULL, NULL);
2010 G_READ_LOCK (&type_rw_lock);
2011 node = lookup_type_node_L (class->g_type);
2012 iface = lookup_type_node_L (iface_type);
2013 if (node && node->is_instantiatable && iface)
2015 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2017 if (entry && entry->vtable)
2018 vtable = entry->vtable;
2020 G_READ_UNLOCK (&type_rw_lock);
2025 G_CONST_RETURN gchar*
2026 g_type_name (GType type)
2030 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, NULL);
2032 G_READ_LOCK (&type_rw_lock);
2033 node = lookup_type_node_L (type);
2034 G_READ_UNLOCK (&type_rw_lock);
2036 return node ? NODE_NAME (node) : NULL;
2040 g_type_qname (GType type)
2044 G_READ_LOCK (&type_rw_lock);
2045 node = lookup_type_node_L (type);
2046 G_READ_UNLOCK (&type_rw_lock);
2048 return node ? node->qname : 0;
2052 g_type_from_name (const gchar *name)
2057 g_return_val_if_fail (name != NULL, 0);
2059 quark = g_quark_try_string (name);
2062 G_READ_LOCK (&type_rw_lock);
2063 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
2064 G_READ_UNLOCK (&type_rw_lock);
2071 g_type_parent (GType type)
2075 G_READ_LOCK (&type_rw_lock);
2076 node = lookup_type_node_L (type);
2077 G_READ_UNLOCK (&type_rw_lock);
2079 return node ? NODE_PARENT_TYPE (node) : 0;
2083 g_type_depth (GType type)
2087 G_READ_LOCK (&type_rw_lock);
2088 node = lookup_type_node_L (type);
2089 G_READ_UNLOCK (&type_rw_lock);
2091 return node ? node->n_supers + 1 : 0;
2095 g_type_next_base (GType type,
2101 G_READ_LOCK (&type_rw_lock);
2102 node = lookup_type_node_L (type);
2105 TypeNode *base_node = lookup_type_node_L (base_type);
2107 if (base_node && base_node->n_supers < node->n_supers)
2109 guint n = node->n_supers - base_node->n_supers;
2111 if (node->supers[n] == base_type)
2112 atype = node->supers[n - 1];
2115 G_READ_UNLOCK (&type_rw_lock);
2120 static inline gboolean
2121 type_node_is_a_L (TypeNode *node,
2122 TypeNode *iface_node,
2123 /* support_inheritance */
2124 gboolean support_interfaces,
2125 gboolean support_prerequisites)
2127 if (support_interfaces &&
2128 node->is_instantiatable && NODE_IS_IFACE (iface_node) &&
2129 type_lookup_iface_entry_L (node, iface_node) != NULL)
2131 else if (/* support_inheritance && */
2132 iface_node->n_supers <= node->n_supers &&
2133 node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2135 else if (support_prerequisites &&
2136 NODE_IS_IFACE (node) &&
2137 type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2144 g_type_is_a (GType type,
2147 TypeNode *node, *iface_node;
2150 G_READ_LOCK (&type_rw_lock);
2151 node = lookup_type_node_L (type);
2152 iface_node = lookup_type_node_L (iface_type);
2153 is_a = node && iface_node && type_node_is_a_L (node, iface_node, TRUE, TRUE);
2154 G_READ_UNLOCK (&type_rw_lock);
2160 g_type_fundamental_branch_last (GType type)
2162 GType ftype = G_TYPE_FUNDAMENTAL (type);
2165 G_READ_LOCK (&type_rw_lock);
2166 last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
2167 G_READ_UNLOCK (&type_rw_lock);
2172 GType* /* free result */
2173 g_type_children (GType type,
2178 G_READ_LOCK (&type_rw_lock);
2179 node = lookup_type_node_L (type);
2182 GType *children = g_new (GType, node->n_children + 1);
2184 memcpy (children, node->children, sizeof (GType) * node->n_children);
2185 children[node->n_children] = 0;
2188 *n_children = node->n_children;
2189 G_READ_UNLOCK (&type_rw_lock);
2195 G_READ_UNLOCK (&type_rw_lock);
2203 GType* /* free result */
2204 g_type_interfaces (GType type,
2205 guint *n_interfaces)
2209 G_READ_LOCK (&type_rw_lock);
2210 node = lookup_type_node_L (type);
2211 if (node && node->is_instantiatable)
2213 GType *ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2216 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2217 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2221 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2222 G_READ_UNLOCK (&type_rw_lock);
2228 G_READ_UNLOCK (&type_rw_lock);
2236 typedef struct _QData QData;
2248 static inline gpointer
2249 type_get_qdata_L (TypeNode *node,
2252 GData *gdata = node->global_gdata;
2254 if (quark && gdata && gdata->n_qdatas)
2256 QData *qdatas = gdata->qdatas - 1;
2257 guint n_qdatas = gdata->n_qdatas;
2264 i = (n_qdatas + 1) / 2;
2266 if (quark == check->quark)
2268 else if (quark > check->quark)
2273 else /* if (quark < check->quark) */
2282 g_type_get_qdata (GType type,
2288 G_READ_LOCK (&type_rw_lock);
2289 node = lookup_type_node_L (type);
2292 data = type_get_qdata_L (node, quark);
2293 G_READ_UNLOCK (&type_rw_lock);
2297 G_READ_UNLOCK (&type_rw_lock);
2298 g_return_val_if_fail (node != NULL, NULL);
2305 type_set_qdata_W (TypeNode *node,
2313 /* setup qdata list if necessary */
2314 if (!node->global_gdata)
2315 node->global_gdata = g_new0 (GData, 1);
2316 gdata = node->global_gdata;
2318 /* try resetting old data */
2319 qdata = gdata->qdatas;
2320 for (i = 0; i < gdata->n_qdatas; i++)
2321 if (qdata[i].quark == quark)
2323 qdata[i].data = data;
2329 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2330 qdata = gdata->qdatas;
2331 for (i = 0; i < gdata->n_qdatas - 1; i++)
2332 if (qdata[i].quark > quark)
2334 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2335 qdata[i].quark = quark;
2336 qdata[i].data = data;
2340 g_type_set_qdata (GType type,
2346 g_return_if_fail (quark != 0);
2348 G_WRITE_LOCK (&type_rw_lock);
2349 node = lookup_type_node_L (type);
2352 type_set_qdata_W (node, quark, data);
2353 G_WRITE_UNLOCK (&type_rw_lock);
2357 G_WRITE_UNLOCK (&type_rw_lock);
2358 g_return_if_fail (node != NULL);
2363 type_add_flags_W (TypeNode *node,
2368 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2369 g_return_if_fail (node != NULL);
2371 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2372 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2373 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2375 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2379 g_type_query (GType type,
2384 g_return_if_fail (query != NULL);
2386 G_READ_LOCK (&type_rw_lock);
2387 node = lookup_type_node_L (type);
2388 if (node && node->is_classed && !node->plugin && node->data)
2390 /* type is classed and static, probably even instantiatable */
2392 query->type = NODE_TYPE (node);
2393 query->type_name = NODE_NAME (node);
2394 query->class_size = node->data->class.class_size;
2395 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2399 /* node is not static and classed, won't allow query */
2402 G_READ_UNLOCK (&type_rw_lock);
2406 /* --- implementation details --- */
2408 g_type_check_flags (GType type,
2412 gboolean result = FALSE;
2414 G_READ_LOCK (&type_rw_lock);
2415 node = lookup_type_node_L (type);
2418 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2419 guint tflags = flags & TYPE_FLAG_MASK;
2423 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2425 fflags = (finfo->type_flags & fflags) == fflags;
2431 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2435 result = tflags && fflags;
2437 G_READ_UNLOCK (&type_rw_lock);
2443 g_type_get_plugin (GType type)
2447 G_READ_LOCK (&type_rw_lock);
2448 node = lookup_type_node_L (type);
2449 G_READ_UNLOCK (&type_rw_lock);
2451 return node ? node->plugin : NULL;
2455 g_type_interface_get_plugin (GType instance_type,
2456 GType interface_type)
2461 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2463 G_READ_LOCK (&type_rw_lock);
2464 node = lookup_type_node_L (instance_type);
2465 iface = lookup_type_node_L (interface_type);
2468 IFaceHolder *iholder = iface_node_get_holders_L (iface);
2470 while (iholder && iholder->instance_type != instance_type)
2471 iholder = iholder->next;
2472 G_READ_UNLOCK (&type_rw_lock);
2475 return iholder->plugin;
2478 G_READ_UNLOCK (&type_rw_lock);
2480 g_return_val_if_fail (node == NULL, NULL);
2481 g_return_val_if_fail (iface == NULL, NULL);
2483 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2489 g_type_fundamental_last (void)
2493 G_READ_LOCK (&type_rw_lock);
2494 type = static_last_fundamental_id;
2495 G_READ_UNLOCK (&type_rw_lock);
2501 g_type_instance_is_a (GTypeInstance *type_instance,
2504 TypeNode *node, *iface;
2507 if (!type_instance || !type_instance->g_class)
2510 G_READ_LOCK (&type_rw_lock);
2511 node = lookup_type_node_L (type_instance->g_class->g_type);
2512 iface = lookup_type_node_L (iface_type);
2513 check = node && node->is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2514 G_READ_UNLOCK (&type_rw_lock);
2520 g_type_class_is_a (GTypeClass *type_class,
2523 TypeNode *node, *iface;
2529 G_READ_LOCK (&type_rw_lock);
2530 node = lookup_type_node_L (type_class->g_type);
2531 iface = lookup_type_node_L (is_a_type);
2532 check = node && node->is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2533 G_READ_UNLOCK (&type_rw_lock);
2539 g_type_check_instance_cast (GTypeInstance *type_instance,
2544 if (type_instance->g_class)
2546 TypeNode *node, *iface;
2547 gboolean is_instantiatable, check;
2549 G_READ_LOCK (&type_rw_lock);
2550 node = lookup_type_node_L (type_instance->g_class->g_type);
2551 is_instantiatable = node && node->is_instantiatable;
2552 iface = lookup_type_node_L (iface_type);
2553 check = is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2554 G_READ_UNLOCK (&type_rw_lock);
2556 return type_instance;
2558 if (is_instantiatable)
2559 g_warning ("invalid cast from `%s' to `%s'",
2560 type_descriptive_name_U (type_instance->g_class->g_type),
2561 type_descriptive_name_U (iface_type));
2563 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2564 type_descriptive_name_U (type_instance->g_class->g_type),
2565 type_descriptive_name_U (iface_type));
2568 g_warning ("invalid unclassed pointer in cast to `%s'",
2569 type_descriptive_name_U (iface_type));
2572 g_warning ("invalid cast from (NULL) pointer to `%s'",
2573 type_descriptive_name_U (iface_type));
2575 return type_instance;
2579 g_type_check_class_cast (GTypeClass *type_class,
2584 TypeNode *node, *iface;
2585 gboolean is_classed, check;
2587 G_READ_LOCK (&type_rw_lock);
2588 node = lookup_type_node_L (type_class->g_type);
2589 is_classed = node && node->is_classed;
2590 iface = lookup_type_node_L (is_a_type);
2591 check = is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2592 G_READ_UNLOCK (&type_rw_lock);
2597 g_warning ("invalid class cast from `%s' to `%s'",
2598 type_descriptive_name_U (type_class->g_type),
2599 type_descriptive_name_U (is_a_type));
2601 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2602 type_descriptive_name_U (type_class->g_type),
2603 type_descriptive_name_U (is_a_type));
2606 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2607 type_descriptive_name_U (is_a_type));
2612 g_type_check_instance (GTypeInstance *type_instance)
2614 /* this function is just here to make the signal system
2615 * conveniently elaborated on instance checks
2619 if (type_instance->g_class)
2622 gboolean is_instantiatable;
2624 G_READ_LOCK (&type_rw_lock);
2625 node = lookup_type_node_L (type_instance->g_class->g_type);
2626 is_instantiatable = node && node->is_instantiatable;
2627 G_READ_UNLOCK (&type_rw_lock);
2628 if (is_instantiatable)
2631 g_warning ("instance of invalid non-instantiatable type `%s'",
2632 type_descriptive_name_U (type_instance->g_class->g_type));
2635 g_warning ("instance with invalid (NULL) class pointer");
2638 g_warning ("invalid (NULL) pointer instance");
2643 static inline gboolean
2644 type_check_is_value_type_U (GType type)
2646 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2649 G_READ_LOCK (&type_rw_lock);
2651 node = lookup_type_node_L (type);
2654 if (node->data && node->data->common.ref_count > 0 &&
2655 node->data->common.value_table->value_init)
2656 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2657 else if (NODE_IS_IFACE (node))
2661 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2663 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2664 TypeNode *prnode = lookup_type_node_L (prtype);
2666 if (prnode->is_instantiatable)
2674 G_READ_UNLOCK (&type_rw_lock);
2676 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2680 g_type_check_is_value_type (GType type)
2682 return type_check_is_value_type_U (type);
2686 g_type_check_value (GValue *value)
2688 return value && type_check_is_value_type_U (value->g_type);
2692 g_type_check_value_holds (GValue *value,
2695 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2699 g_type_value_table_peek (GType type)
2702 GTypeValueTable *vtable;
2704 G_READ_LOCK (&type_rw_lock);
2706 node = lookup_type_node_L (type);
2709 g_warning (G_STRLOC ": type id `%u' is invalid", type);
2710 G_READ_UNLOCK (&type_rw_lock);
2713 if (!node->data || node->data->common.ref_count < 1)
2715 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2716 type_descriptive_name_L (type));
2717 G_READ_UNLOCK (&type_rw_lock);
2720 if (node->data->common.value_table->value_init)
2721 vtable = node->data->common.value_table;
2722 else if (NODE_IS_IFACE (node))
2726 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2728 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2729 TypeNode *prnode = lookup_type_node_L (prtype);
2731 if (prnode->is_instantiatable)
2734 goto restart_table_peek;
2741 G_READ_UNLOCK (&type_rw_lock);
2746 G_CONST_RETURN gchar*
2747 g_type_name_from_instance (GTypeInstance *instance)
2750 return "<NULL-instance>";
2752 return g_type_name_from_class (instance->g_class);
2755 G_CONST_RETURN gchar*
2756 g_type_name_from_class (GTypeClass *g_class)
2759 return "<NULL-class>";
2761 return g_type_name (g_class->g_type);
2765 /* --- foreign prototypes --- */
2766 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2767 extern void g_enum_types_init (void); /* sync with genums.c */
2768 extern void g_param_type_init (void); /* sync with gparam.c */
2769 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2770 extern void g_object_type_init (void); /* sync with gobject.c */
2771 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2772 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2773 extern void g_signal_init (void); /* sync with gsignal.c */
2776 /* --- initialization --- */
2778 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2780 G_LOCK_DEFINE_STATIC (type_init_lock);
2781 static TypeNode *type0_node = NULL;
2782 const gchar *env_string;
2787 G_LOCK (type_init_lock);
2789 G_WRITE_LOCK (&type_rw_lock);
2791 if (static_last_fundamental_id)
2793 G_WRITE_UNLOCK (&type_rw_lock);
2794 G_UNLOCK (type_init_lock);
2798 /* setup GRuntime wide debugging flags */
2799 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2800 env_string = g_getenv ("GRUNTIME_DEBUG");
2801 if (env_string != NULL)
2803 static GDebugKey debug_keys[] = {
2804 { "objects", G_TYPE_DEBUG_OBJECTS },
2805 { "signals", G_TYPE_DEBUG_SIGNALS },
2808 _g_type_debug_flags |= g_parse_debug_string (env_string,
2810 sizeof (debug_keys) / sizeof (debug_keys[0]));
2815 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
2816 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
2817 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
2819 /* type qname hash table */
2820 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2822 /* invalid type G_TYPE_INVALID (0)
2824 static_last_fundamental_id = 1;
2825 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2826 static_type_nodes[0] = &type0_node;
2827 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2828 static_branch_seqnos[0] = 1;
2830 /* void type G_TYPE_NONE
2832 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2833 type = NODE_TYPE (node);
2834 g_assert (type == G_TYPE_NONE);
2836 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2838 memset (&info, 0, sizeof (info));
2839 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2840 type = NODE_TYPE (node);
2841 type_data_make_W (node, &info, NULL);
2842 g_assert (type == G_TYPE_INTERFACE);
2844 G_WRITE_UNLOCK (&type_rw_lock);
2846 /* G_TYPE_TYPE_PLUGIN
2848 g_type_plugin_get_type ();
2850 /* G_TYPE_* value types
2852 g_value_types_init ();
2854 /* G_TYPE_ENUM & G_TYPE_FLAGS
2856 g_enum_types_init ();
2860 g_boxed_type_init ();
2864 g_param_type_init ();
2868 g_object_type_init ();
2870 /* G_TYPE_PARAM_* pspec types
2872 g_param_spec_types_init ();
2874 /* Value Transformations
2876 g_value_transforms_init ();
2882 G_UNLOCK (type_init_lock);
2888 g_type_init_with_debug_flags (0);