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,
135 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
136 GType instance_type);
139 /* --- structures --- */
143 guint n_children : 12;
145 guint _prot_n_ifaces_prerequisites : 9;
146 guint is_classed : 1;
147 guint is_instantiatable : 1;
154 IFaceEntry *iface_entries; /* for !iface types */
155 GType *prerequisistes;
157 GType supers[1]; /* flexible array */
159 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
160 #define MAX_N_SUPERS (255)
161 #define MAX_N_CHILDREN (4095)
162 #define MAX_N_IFACES (511)
163 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
164 #define NODE_TYPE(node) (node->supers[0])
165 #define NODE_PARENT_TYPE(node) (node->supers[1])
166 #define NODE_NAME(node) (g_quark_to_string (node->qname))
167 #define NODE_IS_IFACE(node) (G_TYPE_IS_INTERFACE (NODE_TYPE (node)))
168 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
169 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
170 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
171 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
172 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
173 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
174 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
175 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
180 GInterfaceInfo *info;
187 GTypeInterface *vtable;
192 GTypeValueTable *value_table;
198 GBaseInitFunc vtable_init_base;
199 GBaseFinalizeFunc vtable_finalize_base;
205 GBaseInitFunc class_init_base;
206 GBaseFinalizeFunc class_finalize_base;
207 GClassInitFunc class_init;
208 GClassFinalizeFunc class_finalize;
209 gconstpointer class_data;
216 GBaseInitFunc class_init_base;
217 GBaseFinalizeFunc class_finalize_base;
218 GClassInitFunc class_init;
219 GClassFinalizeFunc class_finalize;
220 gconstpointer class_data;
222 guint16 instance_size;
224 GInstanceInitFunc instance_init;
225 GMemChunk *mem_chunk;
232 InstanceData instance;
236 GTypeClassCacheFunc cache_func;
240 /* --- variables --- */
241 static guint static_n_class_cache_funcs = 0;
242 static ClassCacheFunc *static_class_cache_funcs = NULL;
243 static GType static_last_fundamental_id = 0;
244 static GQuark static_quark_type_flags = 0;
245 static GQuark static_quark_iface_holder = 0;
246 static GQuark static_quark_dependants_array = 0;
247 GTypeDebugFlags _g_type_debug_flags = 0;
250 /* --- type nodes --- */
251 static GHashTable *static_type_nodes_ht = NULL;
252 static GType *static_branch_seqnos = NULL;
253 static TypeNode ***static_type_nodes = NULL;
255 static inline TypeNode*
256 lookup_type_node_L (register GType utype)
258 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
259 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
261 if (ftype < static_last_fundamental_id && b_seqno < static_branch_seqnos[ftype])
262 return static_type_nodes[ftype][b_seqno];
268 type_node_any_new_W (TypeNode *pnode,
272 GTypeFundamentalFlags type_flags)
274 guint branch_last, n_supers;
277 guint i, node_size = 0;
279 n_supers = pnode ? pnode->n_supers + 1 : 0;
280 branch_last = static_branch_seqnos[ftype]++;
281 type = G_TYPE_DERIVE_ID (ftype, branch_last);
282 g_assert ((type & G_TYPE_FLAG_RESERVED_ID_BIT) == 0);
283 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (static_branch_seqnos[ftype] - 1))
284 static_type_nodes[ftype] = g_renew (TypeNode*, static_type_nodes[ftype], 1 << g_bit_storage (static_branch_seqnos[ftype] - 1));
287 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
288 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
289 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
290 node = g_malloc0 (node_size);
291 if (!pnode) /* offset fundamental types */
292 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
293 static_type_nodes[ftype][branch_last] = node;
295 node->n_supers = n_supers;
298 node->supers[0] = type;
301 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
302 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
304 if (NODE_IS_IFACE (node))
306 IFACE_NODE_N_PREREQUISITES (node) = 0;
307 IFACE_NODE_PREREQUISITES (node) = NULL;
311 CLASSED_NODE_N_IFACES (node) = 0;
312 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
317 node->supers[0] = type;
318 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
320 node->is_classed = pnode->is_classed;
321 node->is_instantiatable = pnode->is_instantiatable;
323 if (NODE_IS_IFACE (node))
325 IFACE_NODE_N_PREREQUISITES (node) = 0;
326 IFACE_NODE_PREREQUISITES (node) = NULL;
332 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
333 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
334 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
335 CLASSED_NODE_N_IFACES (node));
336 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
337 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
340 i = pnode->n_children++;
341 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
342 pnode->children[i] = type;
345 node->plugin = plugin;
346 node->n_children = 0;
347 node->children = NULL;
349 node->qname = g_quark_from_string (name);
350 node->global_gdata = NULL;
352 g_hash_table_insert (static_type_nodes_ht,
353 GUINT_TO_POINTER (node->qname),
354 GUINT_TO_POINTER (type));
358 static inline GTypeFundamentalInfo*
359 type_node_fundamental_info_L (TypeNode *node)
361 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
363 if (ftype != NODE_TYPE (node))
364 node = lookup_type_node_L (ftype);
366 return node ? G_STRUCT_MEMBER_P (node, - SIZEOF_FUNDAMENTAL_INFO) : NULL;
370 type_node_fundamental_new_W (GType ftype,
372 GTypeFundamentalFlags type_flags)
374 GTypeFundamentalInfo *finfo;
378 flast = static_last_fundamental_id;
380 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
382 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
384 static_last_fundamental_id = MAX (static_last_fundamental_id, ftype + 1);
385 if (static_last_fundamental_id > flast)
387 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
388 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
389 for (i = flast; i < static_last_fundamental_id; i++)
391 static_type_nodes[i] = NULL;
392 static_branch_seqnos[i] = 0;
395 g_assert (static_branch_seqnos[ftype] == 0);
397 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
399 finfo = type_node_fundamental_info_L (node);
400 finfo->type_flags = type_flags;
406 type_node_new_W (TypeNode *pnode,
412 g_assert (pnode->n_supers < MAX_N_SUPERS);
413 g_assert (pnode->n_children < MAX_N_CHILDREN);
415 return type_node_any_new_W (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
418 static inline IFaceEntry*
419 type_lookup_iface_entry_L (TypeNode *node,
420 TypeNode *iface_node)
422 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
424 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
425 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
426 GType iface_type = NODE_TYPE (iface_node);
433 i = (n_ifaces + 1) >> 1;
435 if (iface_type == check->iface_type)
437 else if (iface_type > check->iface_type)
442 else /* if (iface_type < check->iface_type) */
451 static inline gboolean
452 type_lookup_prerequisite_L (TypeNode *iface,
453 GType prerequisite_type)
455 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
457 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
458 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
465 i = (n_prerequisites + 1) >> 1;
466 check = prerequisites + i;
467 if (prerequisite_type == *check)
469 else if (prerequisite_type > *check)
471 n_prerequisites -= i;
472 prerequisites = check;
474 else /* if (prerequisite_type < *check) */
475 n_prerequisites = i - 1;
477 while (n_prerequisites);
483 type_descriptive_name_L (GType type)
487 TypeNode *node = lookup_type_node_L (type);
489 return node ? NODE_NAME (node) : "<unknown>";
496 type_descriptive_name_U (GType type)
500 G_READ_LOCK (&type_rw_lock);
501 name = type_descriptive_name_L (type);
502 G_READ_UNLOCK (&type_rw_lock);
504 return (gchar *)name;
508 /* --- type consistency checks --- */
510 check_plugin_U (GTypePlugin *plugin,
511 gboolean need_complete_type_info,
512 gboolean need_complete_interface_info,
513 const gchar *type_name)
515 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
519 g_warning ("plugin handle for type `%s' is NULL",
523 if (!G_IS_TYPE_PLUGIN (plugin))
525 g_warning ("plugin pointer (%p) for type `%s' is invalid",
529 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
531 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
535 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
537 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
545 check_type_name_U (const gchar *type_name)
547 static const gchar *extra_chars = "-_+";
548 const gchar *p = type_name;
551 if (!type_name[0] || !type_name[1] || !type_name[2])
553 g_warning ("type name `%s' is too short", type_name);
556 /* check the first letter */
557 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
558 for (p = type_name + 1; *p; p++)
559 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
560 (p[0] >= 'a' && p[0] <= 'z') ||
561 (p[0] >= '0' && p[0] <= '9') ||
562 strchr (extra_chars, p[0]));
565 g_warning ("type name `%s' contains invalid characters", type_name);
568 if (g_type_from_name (type_name))
570 g_warning ("cannot register existing type `%s'", type_name);
578 check_derivation_U (GType parent_type,
579 const gchar *type_name)
582 GTypeFundamentalInfo* finfo;
584 G_READ_LOCK (&type_rw_lock);
585 pnode = lookup_type_node_L (parent_type);
588 G_READ_UNLOCK (&type_rw_lock);
589 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
591 type_descriptive_name_U (parent_type));
594 finfo = type_node_fundamental_info_L (pnode);
595 /* ensure flat derivability */
596 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
598 G_READ_UNLOCK (&type_rw_lock);
599 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
604 /* ensure deep derivability */
605 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
606 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
608 G_READ_UNLOCK (&type_rw_lock);
609 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
614 G_READ_UNLOCK (&type_rw_lock);
620 check_collect_format_I (const gchar *collect_format)
622 const gchar *p = collect_format;
623 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
624 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
625 G_VALUE_COLLECT_POINTER, 0 };
628 if (!strchr (valid_format, *p++))
630 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
634 check_value_table_I (const gchar *type_name,
635 const GTypeValueTable *value_table)
639 else if (value_table->value_init == NULL)
641 if (value_table->value_free || value_table->value_copy ||
642 value_table->value_peek_pointer ||
643 value_table->collect_format || value_table->collect_value ||
644 value_table->lcopy_format || value_table->lcopy_value)
645 g_warning ("cannot handle uninitializable values of type `%s'",
649 else /* value_table->value_init != NULL */
651 if (!value_table->value_free)
654 * g_warning ("missing `value_free()' for type `%s'", type_name);
658 if (!value_table->value_copy)
660 g_warning ("missing `value_copy()' for type `%s'", type_name);
663 if ((value_table->collect_format || value_table->collect_value) &&
664 (!value_table->collect_format || !value_table->collect_value))
666 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
670 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
672 g_warning ("the `%s' specification for type `%s' is too long or invalid",
677 if ((value_table->lcopy_format || value_table->lcopy_value) &&
678 (!value_table->lcopy_format || !value_table->lcopy_value))
680 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
684 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
686 g_warning ("the `%s' specification for type `%s' is too long or invalid",
696 check_type_info_L (TypeNode *pnode,
698 const gchar *type_name,
699 const GTypeInfo *info)
701 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (lookup_type_node_L (ftype));
702 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
704 /* check instance members */
705 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
706 (info->instance_size || info->n_preallocs || info->instance_init))
709 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
713 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
717 /* check class & interface members */
718 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
719 (info->class_init || info->class_finalize || info->class_data ||
720 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
723 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
727 g_warning ("cannot create class for `%s' as non-classed fundamental",
731 /* check interface size */
732 if (is_interface && info->class_size < sizeof (GTypeInterface))
734 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
738 /* check class size */
739 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
741 if (info->class_size < sizeof (GTypeClass))
743 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
747 if (pnode && info->class_size < pnode->data->class.class_size)
749 g_warning ("specified class size for type `%s' is smaller "
750 "than the parent type's `%s' class size",
756 /* check instance size */
757 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
759 if (info->instance_size < sizeof (GTypeInstance))
761 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
765 if (pnode && info->instance_size < pnode->data->instance.instance_size)
767 g_warning ("specified instance size for type `%s' is smaller "
768 "than the parent type's `%s' instance size",
779 find_conforming_child_type_L (TypeNode *pnode,
782 TypeNode *node = NULL;
785 if (type_lookup_iface_entry_L (pnode, iface))
788 for (i = 0; i < pnode->n_children && !node; i++)
789 node = find_conforming_child_type_L (lookup_type_node_L (pnode->children[i]), iface);
795 check_add_interface_L (GType instance_type,
798 TypeNode *node = lookup_type_node_L (instance_type);
799 TypeNode *iface = lookup_type_node_L (iface_type);
803 if (!node || !node->is_instantiatable)
805 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
806 type_descriptive_name_L (instance_type));
809 if (!iface || !NODE_IS_IFACE (iface))
811 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
812 type_descriptive_name_L (iface_type),
816 tnode = lookup_type_node_L (NODE_PARENT_TYPE (iface));
817 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
819 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
820 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
826 /* allow overriding of interface type introduced for parent type */
827 entry = type_lookup_iface_entry_L (node, iface);
828 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
830 /* ok, we do conform to this interface already, but the interface vtable was not
831 * yet intialized, and we just conform to the interface because it got added to
832 * one of our parents. so we allow overriding of holder info here.
836 /* check whether one of our children already conforms (or whether the interface
837 * got added to this node already)
839 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
842 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
852 check_interface_info_L (TypeNode *iface,
854 const GInterfaceInfo *info)
856 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
858 g_warning ("interface type `%s' for type `%s' comes without initializer",
860 type_descriptive_name_L (instance_type));
867 /* --- type info (type node data) --- */
869 type_data_make_W (TypeNode *node,
870 const GTypeInfo *info,
871 const GTypeValueTable *value_table)
874 GTypeValueTable *vtable = NULL;
875 guint vtable_size = 0;
877 g_assert (node->data == NULL && info != NULL);
881 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
884 vtable = pnode->data->common.value_table;
887 static const GTypeValueTable zero_vtable = { NULL, };
889 value_table = &zero_vtable;
894 /* need to setup vtable_size since we have to allocate it with data in one chunk */
895 vtable_size = sizeof (GTypeValueTable);
896 if (value_table->collect_format)
897 vtable_size += strlen (value_table->collect_format);
898 if (value_table->lcopy_format)
899 vtable_size += strlen (value_table->lcopy_format);
903 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
905 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
907 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
908 data->instance.class_size = info->class_size;
909 data->instance.class_init_base = info->base_init;
910 data->instance.class_finalize_base = info->base_finalize;
911 data->instance.class_init = info->class_init;
912 data->instance.class_finalize = info->class_finalize;
913 data->instance.class_data = info->class_data;
914 data->instance.class = NULL;
915 data->instance.instance_size = info->instance_size;
916 #ifdef DISABLE_MEM_POOLS
917 data->instance.n_preallocs = 0;
918 #else /* !DISABLE_MEM_POOLS */
919 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
920 #endif /* !DISABLE_MEM_POOLS */
921 data->instance.instance_init = info->instance_init;
922 data->instance.mem_chunk = NULL;
924 else if (node->is_classed) /* only classed */
926 data = g_malloc0 (sizeof (ClassData) + vtable_size);
928 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
929 data->class.class_size = info->class_size;
930 data->class.class_init_base = info->base_init;
931 data->class.class_finalize_base = info->base_finalize;
932 data->class.class_init = info->class_init;
933 data->class.class_finalize = info->class_finalize;
934 data->class.class_data = info->class_data;
935 data->class.class = NULL;
937 else if (NODE_IS_IFACE (node))
939 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
941 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
942 data->iface.vtable_size = info->class_size;
943 data->iface.vtable_init_base = info->base_init;
944 data->iface.vtable_finalize_base = info->base_finalize;
948 data = g_malloc0 (sizeof (CommonData) + vtable_size);
950 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
954 node->data->common.ref_count = 1;
960 /* we allocate the vtable and its strings together with the type data, so
961 * children can take over their parent's vtable pointer, and we don't
962 * need to worry freeing it or not when the child data is destroyed
964 *vtable = *value_table;
965 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
967 vtable->collect_format = p;
968 if (value_table->collect_format)
970 strcat (p, value_table->collect_format);
971 p += strlen (value_table->collect_format);
975 vtable->lcopy_format = p;
976 if (value_table->lcopy_format)
977 strcat (p, value_table->lcopy_format);
979 node->data->common.value_table = vtable;
981 g_assert (node->data->common.value_table != NULL); /* paranoid */
985 type_data_ref_Wm (TypeNode *node)
989 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
991 GTypeValueTable tmp_value_table;
993 g_assert (node->plugin != NULL);
997 type_data_ref_Wm (pnode);
999 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1002 memset (&tmp_info, 0, sizeof (tmp_info));
1003 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1005 G_WRITE_UNLOCK (&type_rw_lock);
1006 g_type_plugin_use (node->plugin);
1007 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1008 G_WRITE_LOCK (&type_rw_lock);
1010 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1012 check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
1013 type_data_make_W (node, &tmp_info,
1014 check_value_table_I (NODE_NAME (node),
1015 &tmp_value_table) ? &tmp_value_table : NULL);
1019 g_assert (node->data->common.ref_count > 0);
1021 node->data->common.ref_count += 1;
1026 type_data_unref_Wm (TypeNode *node,
1029 g_assert (node->data && node->data->common.ref_count);
1031 if (node->data->common.ref_count > 1)
1032 node->data->common.ref_count -= 1;
1037 g_warning ("static type `%s' unreferenced too often",
1042 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1047 type_node_add_iface_entry_W (TypeNode *node,
1050 IFaceEntry *entries;
1053 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1055 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1056 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1057 if (entries[i].iface_type == iface_type)
1059 /* this can (should) only happen if our parent type already conformed
1060 * to iface_type and node got it's own holder info. here, our
1061 * children should already have entries with NULL vtables, so
1062 * we're actually done.
1064 g_assert (entries[i].vtable == NULL);
1067 else if (entries[i].iface_type > iface_type)
1069 CLASSED_NODE_N_IFACES (node) += 1;
1070 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1071 CLASSED_NODE_IFACES_ENTRIES (node),
1072 CLASSED_NODE_N_IFACES (node));
1073 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1074 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1075 entries[i].iface_type = iface_type;
1076 entries[i].vtable = NULL;
1078 for (i = 0; i < node->n_children; i++)
1079 type_node_add_iface_entry_W (lookup_type_node_L (node->children[i]), iface_type);
1083 type_add_interface_W (TypeNode *node,
1085 const GInterfaceInfo *info,
1086 GTypePlugin *plugin)
1088 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1090 /* we must not call any functions of GInterfaceInfo from within here, since
1091 * we got most probably called from _within_ a type registration function
1093 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1095 iholder->next = iface_node_get_holders_L (iface);
1096 iface_node_set_holders_W (iface, iholder);
1097 iholder->instance_type = NODE_TYPE (node);
1098 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1099 iholder->plugin = plugin;
1101 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1105 type_iface_add_prerequisite_W (TypeNode *iface,
1106 TypeNode *prerequisite_node)
1108 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1109 GType *prerequisites, *dependants;
1110 guint n_dependants, i;
1112 g_assert (NODE_IS_IFACE (iface) &&
1113 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1114 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1116 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1117 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1118 if (prerequisites[i] == prerequisite_type)
1119 return; /* we already have that prerequisiste */
1120 else if (prerequisites[i] > prerequisite_type)
1122 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1123 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1124 IFACE_NODE_PREREQUISITES (iface),
1125 IFACE_NODE_N_PREREQUISITES (iface));
1126 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1127 g_memmove (prerequisites + i + 1, prerequisites + i,
1128 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1129 prerequisites[i] = prerequisite_type;
1131 /* we want to get notified when prerequisites get added to prerequisite_node */
1132 if (NODE_IS_IFACE (prerequisite_node))
1134 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1135 n_dependants = dependants ? dependants[0] : 0;
1137 dependants = g_renew (GType, dependants, n_dependants + 1);
1138 dependants[n_dependants] = NODE_TYPE (iface);
1139 dependants[0] = n_dependants;
1140 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1143 /* we need to notify all dependants */
1144 dependants = iface_node_get_dependants_array_L (iface);
1145 n_dependants = dependants ? dependants[0] : 0;
1146 for (i = 1; i <= n_dependants; i++)
1147 type_iface_add_prerequisite_W (lookup_type_node_L (dependants[i]), prerequisite_node);
1151 g_type_interface_add_prerequisite (GType interface_type,
1152 GType prerequisite_type)
1154 TypeNode *iface, *prerequisite_node;
1155 IFaceHolder *holders;
1157 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));
1158 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1159 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1161 G_WRITE_LOCK (&type_rw_lock);
1162 iface = lookup_type_node_L (interface_type);
1163 prerequisite_node = lookup_type_node_L (prerequisite_type);
1164 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1166 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1167 type_descriptive_name_L (interface_type),
1168 type_descriptive_name_L (prerequisite_type));
1169 G_WRITE_UNLOCK (&type_rw_lock);
1172 holders = iface_node_get_holders_L (iface);
1175 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1176 type_descriptive_name_L (prerequisite_type),
1177 type_descriptive_name_L (interface_type),
1178 type_descriptive_name_L (holders->instance_type));
1179 G_WRITE_UNLOCK (&type_rw_lock);
1182 if (prerequisite_node->is_instantiatable)
1186 /* can have at most one publically installable instantiatable prerequisite */
1187 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1189 TypeNode *prnode = lookup_type_node_L (IFACE_NODE_PREREQUISITES (iface)[i]);
1191 if (prnode->is_instantiatable)
1193 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1194 type_descriptive_name_L (prerequisite_type),
1195 type_descriptive_name_L (interface_type),
1196 type_descriptive_name_L (NODE_TYPE (prnode)));
1197 G_WRITE_UNLOCK (&type_rw_lock);
1202 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1203 type_iface_add_prerequisite_W (iface, lookup_type_node_L (prerequisite_node->supers[i]));
1205 else if (NODE_IS_IFACE (prerequisite_node))
1208 guint n_dependants, i;
1210 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1211 n_dependants = dependants ? dependants[0] : 0;
1212 for (i = 1; i <= n_dependants; i++)
1213 type_iface_add_prerequisite_W (iface, lookup_type_node_L (dependants[i]));
1214 type_iface_add_prerequisite_W (iface, prerequisite_node);
1217 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1218 type_descriptive_name_L (prerequisite_type),
1219 type_descriptive_name_L (interface_type));
1220 G_WRITE_UNLOCK (&type_rw_lock);
1224 type_iface_peek_holder_L (TypeNode *iface,
1225 GType instance_type)
1227 IFaceHolder *iholder;
1229 g_assert (NODE_IS_IFACE (iface));
1231 iholder = iface_node_get_holders_L (iface);
1232 while (iholder && iholder->instance_type != instance_type)
1233 iholder = iholder->next;
1238 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1239 GType instance_type,
1242 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1244 if (iholder && !iholder->info && need_info)
1246 GInterfaceInfo tmp_info;
1248 g_assert (iholder->plugin != NULL);
1250 type_data_ref_Wm (iface);
1252 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1254 memset (&tmp_info, 0, sizeof (tmp_info));
1256 G_WRITE_UNLOCK (&type_rw_lock);
1257 g_type_plugin_use (iholder->plugin);
1258 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1259 G_WRITE_LOCK (&type_rw_lock);
1261 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1263 check_interface_info_L (iface, instance_type, &tmp_info);
1264 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1267 return iholder; /* we don't modify write lock upon returning NULL */
1271 type_iface_blow_holder_info_Wm (TypeNode *iface,
1272 GType instance_type)
1274 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1276 g_assert (NODE_IS_IFACE (iface));
1278 while (iholder->instance_type != instance_type)
1279 iholder = iholder->next;
1281 if (iholder->info && iholder->plugin)
1283 g_free (iholder->info);
1284 iholder->info = NULL;
1286 G_WRITE_UNLOCK (&type_rw_lock);
1287 g_type_plugin_unuse (iholder->plugin);
1288 G_WRITE_LOCK (&type_rw_lock);
1290 type_data_unref_Wm (iface, FALSE);
1295 /* --- type structure creation/destruction --- */
1297 g_type_create_instance (GType type)
1300 GTypeInstance *instance;
1304 G_READ_LOCK (&type_rw_lock);
1305 node = lookup_type_node_L (type);
1306 G_READ_UNLOCK (&type_rw_lock);
1307 if (!node || !node->is_instantiatable)
1309 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1310 type_descriptive_name_U (type));
1313 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1314 if (G_TYPE_IS_ABSTRACT (type))
1316 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1317 type_descriptive_name_U (type));
1321 class = g_type_class_ref (type);
1323 if (node->data->instance.n_preallocs)
1325 G_WRITE_LOCK (&type_rw_lock);
1326 if (!node->data->instance.mem_chunk)
1327 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1328 node->data->instance.instance_size,
1329 (node->data->instance.instance_size *
1330 node->data->instance.n_preallocs),
1332 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1333 G_WRITE_UNLOCK (&type_rw_lock);
1336 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1337 for (i = node->n_supers; i > 0; i--)
1341 G_READ_LOCK (&type_rw_lock);
1342 pnode = lookup_type_node_L (node->supers[i]);
1343 G_READ_UNLOCK (&type_rw_lock);
1345 if (pnode->data->instance.instance_init)
1347 instance->g_class = pnode->data->instance.class;
1348 pnode->data->instance.instance_init (instance, class);
1351 instance->g_class = class;
1353 if (node->data->instance.instance_init)
1354 node->data->instance.instance_init (instance, class);
1360 g_type_free_instance (GTypeInstance *instance)
1365 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1367 G_READ_LOCK (&type_rw_lock);
1368 class = instance->g_class;
1369 node = lookup_type_node_L (class->g_type);
1370 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1372 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1373 type_descriptive_name_L (class->g_type));
1374 G_READ_UNLOCK (&type_rw_lock);
1377 G_READ_UNLOCK (&type_rw_lock);
1378 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1379 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1381 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1386 instance->g_class = NULL;
1387 memset (instance, 0xaa, node->data->instance.instance_size); /* FIXME: debugging hack */
1388 if (node->data->instance.n_preallocs)
1390 G_WRITE_LOCK (&type_rw_lock);
1391 g_chunk_free (instance, node->data->instance.mem_chunk);
1392 G_WRITE_UNLOCK (&type_rw_lock);
1397 g_type_class_unref (class);
1401 type_iface_vtable_init_Wm (TypeNode *iface,
1404 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1405 IFaceHolder *iholder;
1406 GTypeInterface *vtable = NULL;
1409 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1410 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1412 return FALSE; /* we don't modify write lock upon FALSE */
1414 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1416 pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
1417 if (pnode) /* want to copy over parent iface contents */
1419 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1422 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1425 vtable = g_malloc0 (iface->data->iface.vtable_size);
1426 entry->vtable = vtable;
1427 vtable->g_type = NODE_TYPE (iface);
1428 vtable->g_instance_type = NODE_TYPE (node);
1430 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1432 G_WRITE_UNLOCK (&type_rw_lock);
1433 if (iface->data->iface.vtable_init_base)
1434 iface->data->iface.vtable_init_base (vtable);
1435 if (iholder->info->interface_init)
1436 iholder->info->interface_init (vtable, iholder->info->interface_data);
1437 G_WRITE_LOCK (&type_rw_lock);
1439 return TRUE; /* write lock modified */
1443 type_iface_vtable_finalize_Wm (TypeNode *iface,
1445 GTypeInterface *vtable)
1447 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1448 IFaceHolder *iholder;
1450 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1451 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1453 return FALSE; /* we don't modify write lock upon FALSE */
1455 g_assert (entry && entry->vtable == vtable && iholder->info);
1457 entry->vtable = NULL;
1458 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1460 G_WRITE_UNLOCK (&type_rw_lock);
1461 if (iholder->info->interface_finalize)
1462 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1463 if (iface->data->iface.vtable_finalize_base)
1464 iface->data->iface.vtable_finalize_base (vtable);
1465 G_WRITE_LOCK (&type_rw_lock);
1468 vtable->g_instance_type = 0;
1471 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1473 return TRUE; /* write lock modified */
1477 type_class_init_Wm (TypeNode *node,
1480 GSList *slist, *init_slist = NULL;
1483 TypeNode *bnode, *pnode;
1486 g_assert (node->is_classed && node->data &&
1487 node->data->class.class_size &&
1488 !node->data->class.class);
1490 class = g_malloc0 (node->data->class.class_size);
1491 node->data->class.class = class;
1495 TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1497 memcpy (class, pclass, pnode->data->class.class_size);
1499 class->g_type = NODE_TYPE (node);
1501 G_WRITE_UNLOCK (&type_rw_lock);
1503 /* stack all base class initialization functions, so we
1504 * call them in ascending order.
1506 G_READ_LOCK (&type_rw_lock);
1507 for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1508 if (bnode->data->class.class_init_base)
1509 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1510 G_READ_UNLOCK (&type_rw_lock);
1511 for (slist = init_slist; slist; slist = slist->next)
1513 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1515 class_init_base (class);
1517 g_slist_free (init_slist);
1519 if (node->data->class.class_init)
1520 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1522 G_WRITE_LOCK (&type_rw_lock);
1524 /* ok, we got the class done, now initialize all interfaces, either
1525 * from parent, or through our holder info
1527 pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
1528 entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1531 g_assert (entry->vtable == NULL);
1533 if (!type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node))
1537 /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1538 * need to get this interface from parent
1540 g_assert (pnode != NULL);
1542 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1544 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1546 if (pentry->iface_type == entry->iface_type)
1548 entry->vtable = pentry->vtable;
1552 g_assert (entry->vtable != NULL);
1555 /* refetch entry, IFACES_ENTRIES might be modified */
1556 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1557 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1558 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1563 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1568 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1570 g_message ("finalizing interfaces for %sClass `%s'",
1571 type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1572 type_descriptive_name_L (NODE_TYPE (node)));
1574 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1575 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1576 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1577 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1580 if (!type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable))
1582 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1583 * iface vtable came from parent
1585 entry->vtable = NULL;
1588 /* refetch entry, IFACES_ENTRIES might be modified */
1589 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1590 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1591 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1592 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1597 type_data_finalize_class_U (TypeNode *node,
1600 GTypeClass *class = cdata->class;
1603 g_assert (cdata->class && cdata->common.ref_count == 0);
1605 if (cdata->class_finalize)
1606 cdata->class_finalize (class, (gpointer) cdata->class_data);
1608 /* call all base class destruction functions in descending order
1610 if (cdata->class_finalize_base)
1611 cdata->class_finalize_base (class);
1612 G_READ_LOCK (&type_rw_lock);
1613 for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1614 if (bnode->data->class.class_finalize_base)
1616 G_READ_UNLOCK (&type_rw_lock);
1617 bnode->data->class.class_finalize_base (class);
1618 G_READ_LOCK (&type_rw_lock);
1620 G_READ_UNLOCK (&type_rw_lock);
1623 g_free (cdata->class);
1627 type_data_last_unref_Wm (GType type,
1630 TypeNode *node = lookup_type_node_L (type);
1632 g_return_if_fail (node != NULL && node->plugin != NULL);
1634 if (!node->data || node->data->common.ref_count == 0)
1636 g_warning ("cannot drop last reference to unreferenced type `%s'",
1637 type_descriptive_name_U (type));
1641 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1645 G_WRITE_UNLOCK (&type_rw_lock);
1646 G_READ_LOCK (&type_rw_lock);
1647 for (i = 0; i < static_n_class_cache_funcs; i++)
1649 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1650 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1651 gboolean need_break;
1653 G_READ_UNLOCK (&type_rw_lock);
1654 need_break = cache_func (cache_data, node->data->class.class);
1655 G_READ_LOCK (&type_rw_lock);
1656 if (!node->data || node->data->common.ref_count == 0)
1657 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1661 G_READ_UNLOCK (&type_rw_lock);
1662 G_WRITE_LOCK (&type_rw_lock);
1665 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1666 node->data->common.ref_count -= 1;
1669 GType ptype = NODE_PARENT_TYPE (node);
1672 node->data->common.ref_count = 0;
1674 if (node->is_instantiatable && node->data->instance.mem_chunk)
1676 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1677 node->data->instance.mem_chunk = NULL;
1681 if (node->is_classed && tdata->class.class)
1683 if (CLASSED_NODE_N_IFACES (node))
1684 type_data_finalize_class_ifaces_Wm (node);
1686 G_WRITE_UNLOCK (&type_rw_lock);
1687 type_data_finalize_class_U (node, &tdata->class);
1688 G_WRITE_LOCK (&type_rw_lock);
1693 /* freeing tdata->common.value_table and its contents is taking care of
1694 * by allocating it in one chunk with tdata
1699 type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1700 G_WRITE_UNLOCK (&type_rw_lock);
1701 g_type_plugin_unuse (node->plugin);
1702 G_WRITE_LOCK (&type_rw_lock);
1707 g_type_add_class_cache_func (gpointer cache_data,
1708 GTypeClassCacheFunc cache_func)
1712 g_return_if_fail (cache_func != NULL);
1714 G_WRITE_LOCK (&type_rw_lock);
1715 i = static_n_class_cache_funcs++;
1716 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1717 static_class_cache_funcs[i].cache_data = cache_data;
1718 static_class_cache_funcs[i].cache_func = cache_func;
1719 G_WRITE_UNLOCK (&type_rw_lock);
1723 g_type_remove_class_cache_func (gpointer cache_data,
1724 GTypeClassCacheFunc cache_func)
1726 gboolean found_it = FALSE;
1729 g_return_if_fail (cache_func != NULL);
1731 G_WRITE_LOCK (&type_rw_lock);
1732 for (i = 0; i < static_n_class_cache_funcs; i++)
1733 if (static_class_cache_funcs[i].cache_data == cache_data &&
1734 static_class_cache_funcs[i].cache_func == cache_func)
1736 static_n_class_cache_funcs--;
1737 g_memmove (static_class_cache_funcs + i,
1738 static_class_cache_funcs + i + 1,
1739 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1740 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1744 G_WRITE_UNLOCK (&type_rw_lock);
1747 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1748 cache_func, cache_data);
1752 /* --- type registration --- */
1754 g_type_register_fundamental (GType type_id,
1755 const gchar *type_name,
1756 const GTypeInfo *info,
1757 const GTypeFundamentalInfo *finfo,
1760 GTypeFundamentalInfo *node_finfo;
1763 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1764 g_return_val_if_fail (type_id > 0, 0);
1765 g_return_val_if_fail (type_name != NULL, 0);
1766 g_return_val_if_fail (info != NULL, 0);
1767 g_return_val_if_fail (finfo != NULL, 0);
1769 if (!check_type_name_U (type_name))
1771 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1773 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1778 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1779 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1781 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1785 G_WRITE_LOCK (&type_rw_lock);
1786 if (lookup_type_node_L (type_id))
1788 G_WRITE_UNLOCK (&type_rw_lock);
1789 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1790 type_descriptive_name_U (type_id),
1795 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1796 node_finfo = type_node_fundamental_info_L (node);
1797 type_add_flags_W (node, flags);
1799 if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1800 type_data_make_W (node, info,
1801 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1802 G_WRITE_UNLOCK (&type_rw_lock);
1804 return NODE_TYPE (node);
1808 g_type_register_static (GType parent_type,
1809 const gchar *type_name,
1810 const GTypeInfo *info,
1813 TypeNode *pnode, *node;
1816 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1817 g_return_val_if_fail (parent_type > 0, 0);
1818 g_return_val_if_fail (type_name != NULL, 0);
1819 g_return_val_if_fail (info != NULL, 0);
1821 if (!check_type_name_U (type_name) ||
1822 !check_derivation_U (parent_type, type_name))
1824 if (info->class_finalize)
1826 g_warning ("class finalizer specified for static type `%s'",
1831 G_WRITE_LOCK (&type_rw_lock);
1832 pnode = lookup_type_node_L (parent_type);
1833 type_data_ref_Wm (pnode);
1834 if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1836 node = type_node_new_W (pnode, type_name, NULL);
1837 type_add_flags_W (node, flags);
1838 type = NODE_TYPE (node);
1839 type_data_make_W (node, info,
1840 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1842 G_WRITE_UNLOCK (&type_rw_lock);
1848 g_type_register_dynamic (GType parent_type,
1849 const gchar *type_name,
1850 GTypePlugin *plugin,
1853 TypeNode *pnode, *node;
1856 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1857 g_return_val_if_fail (parent_type > 0, 0);
1858 g_return_val_if_fail (type_name != NULL, 0);
1859 g_return_val_if_fail (plugin != NULL, 0);
1861 if (!check_type_name_U (type_name) ||
1862 !check_derivation_U (parent_type, type_name) ||
1863 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1866 G_WRITE_LOCK (&type_rw_lock);
1867 pnode = lookup_type_node_L (parent_type);
1868 node = type_node_new_W (pnode, type_name, plugin);
1869 type_add_flags_W (node, flags);
1870 type = NODE_TYPE (node);
1871 G_WRITE_UNLOCK (&type_rw_lock);
1877 g_type_add_interface_static (GType instance_type,
1878 GType interface_type,
1879 const GInterfaceInfo *info)
1881 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1882 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1883 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1885 G_WRITE_LOCK (&type_rw_lock);
1886 if (check_add_interface_L (instance_type, interface_type))
1888 TypeNode *node = lookup_type_node_L (instance_type);
1889 TypeNode *iface = lookup_type_node_L (interface_type);
1891 if (check_interface_info_L (iface, NODE_TYPE (node), info))
1893 type_add_interface_W (node, iface, info, NULL);
1894 /* if we have a class already, the interface vtable needs to
1895 * be initialized as well
1897 if (node->data && node->data->class.class)
1898 type_iface_vtable_init_Wm (iface, node);
1901 G_WRITE_UNLOCK (&type_rw_lock);
1905 g_type_add_interface_dynamic (GType instance_type,
1906 GType interface_type,
1907 GTypePlugin *plugin)
1911 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1912 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1913 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1915 G_READ_LOCK (&type_rw_lock);
1916 node = lookup_type_node_L (instance_type);
1917 G_READ_UNLOCK (&type_rw_lock);
1918 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1921 G_WRITE_LOCK (&type_rw_lock);
1922 if (check_add_interface_L (instance_type, interface_type))
1924 TypeNode *iface = lookup_type_node_L (interface_type);
1926 type_add_interface_W (node, iface, NULL, plugin);
1927 /* if we have a class already, the interface vtable needs to
1928 * be initialized as well
1930 if (node->data && node->data->class.class)
1931 type_iface_vtable_init_Wm (iface, node);
1933 G_WRITE_UNLOCK (&type_rw_lock);
1937 /* --- public API functions --- */
1939 g_type_class_ref (GType type)
1943 /* optimize for common code path
1945 G_WRITE_LOCK (&type_rw_lock);
1946 node = lookup_type_node_L (type);
1947 if (node && node->is_classed && node->data &&
1948 node->data->class.class && node->data->common.ref_count > 0)
1950 type_data_ref_Wm (node);
1951 G_WRITE_UNLOCK (&type_rw_lock);
1953 return node->data->class.class;
1956 if (!node || !node->is_classed ||
1957 (node->data && node->data->common.ref_count < 1))
1959 G_WRITE_UNLOCK (&type_rw_lock);
1960 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
1961 type_descriptive_name_U (type));
1965 type_data_ref_Wm (node);
1967 if (!node->data->class.class)
1969 GType ptype = NODE_PARENT_TYPE (node);
1970 GTypeClass *pclass = NULL;
1974 G_WRITE_UNLOCK (&type_rw_lock);
1975 pclass = g_type_class_ref (ptype);
1976 if (node->data->class.class)
1977 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1978 G_WRITE_LOCK (&type_rw_lock);
1981 type_class_init_Wm (node, pclass);
1983 G_WRITE_UNLOCK (&type_rw_lock);
1985 return node->data->class.class;
1989 g_type_class_unref (gpointer g_class)
1992 GTypeClass *class = g_class;
1994 g_return_if_fail (g_class != NULL);
1996 G_WRITE_LOCK (&type_rw_lock);
1997 node = lookup_type_node_L (class->g_type);
1998 if (node && node->is_classed && node->data &&
1999 node->data->class.class == class && node->data->common.ref_count > 0)
2000 type_data_unref_Wm (node, FALSE);
2002 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2003 type_descriptive_name_L (class->g_type));
2004 G_WRITE_UNLOCK (&type_rw_lock);
2008 g_type_class_unref_uncached (gpointer g_class)
2011 GTypeClass *class = g_class;
2013 g_return_if_fail (g_class != NULL);
2015 G_WRITE_LOCK (&type_rw_lock);
2016 node = lookup_type_node_L (class->g_type);
2017 if (node && node->is_classed && node->data &&
2018 node->data->class.class == class && node->data->common.ref_count > 0)
2019 type_data_unref_Wm (node, TRUE);
2021 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2022 type_descriptive_name_L (class->g_type));
2023 G_WRITE_UNLOCK (&type_rw_lock);
2027 g_type_class_peek (GType type)
2032 G_READ_LOCK (&type_rw_lock);
2033 node = lookup_type_node_L (type);
2034 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2035 class = node->data->class.class;
2038 G_READ_UNLOCK (&type_rw_lock);
2044 g_type_class_peek_parent (gpointer g_class)
2047 gpointer class = NULL;
2049 g_return_val_if_fail (g_class != NULL, NULL);
2051 G_READ_LOCK (&type_rw_lock);
2052 node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
2053 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2055 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
2056 class = node->data->class.class;
2058 else if (NODE_PARENT_TYPE (node))
2059 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2060 G_READ_UNLOCK (&type_rw_lock);
2066 g_type_interface_peek (gpointer instance_class,
2071 gpointer vtable = NULL;
2072 GTypeClass *class = instance_class;
2074 g_return_val_if_fail (instance_class != NULL, NULL);
2076 G_READ_LOCK (&type_rw_lock);
2077 node = lookup_type_node_L (class->g_type);
2078 iface = lookup_type_node_L (iface_type);
2079 if (node && node->is_instantiatable && iface)
2081 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2083 if (entry && entry->vtable)
2084 vtable = entry->vtable;
2087 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2088 G_READ_UNLOCK (&type_rw_lock);
2094 g_type_interface_peek_parent (gpointer g_iface)
2098 gpointer vtable = NULL;
2099 GTypeInterface *iface_class = g_iface;
2101 g_return_val_if_fail (g_iface != NULL, NULL);
2103 G_READ_LOCK (&type_rw_lock);
2104 iface = lookup_type_node_L (iface_class->g_type);
2105 node = lookup_type_node_L (iface_class->g_instance_type);
2107 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
2108 if (node && node->is_instantiatable && iface)
2110 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2112 if (entry && entry->vtable)
2113 vtable = entry->vtable;
2116 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2117 G_READ_UNLOCK (&type_rw_lock);
2122 G_CONST_RETURN gchar*
2123 g_type_name (GType type)
2127 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, NULL);
2129 G_READ_LOCK (&type_rw_lock);
2130 node = lookup_type_node_L (type);
2131 G_READ_UNLOCK (&type_rw_lock);
2133 return node ? NODE_NAME (node) : NULL;
2137 g_type_qname (GType type)
2141 G_READ_LOCK (&type_rw_lock);
2142 node = lookup_type_node_L (type);
2143 G_READ_UNLOCK (&type_rw_lock);
2145 return node ? node->qname : 0;
2149 g_type_from_name (const gchar *name)
2154 g_return_val_if_fail (name != NULL, 0);
2156 quark = g_quark_try_string (name);
2159 G_READ_LOCK (&type_rw_lock);
2160 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
2161 G_READ_UNLOCK (&type_rw_lock);
2168 g_type_parent (GType type)
2172 G_READ_LOCK (&type_rw_lock);
2173 node = lookup_type_node_L (type);
2174 G_READ_UNLOCK (&type_rw_lock);
2176 return node ? NODE_PARENT_TYPE (node) : 0;
2180 g_type_depth (GType type)
2184 G_READ_LOCK (&type_rw_lock);
2185 node = lookup_type_node_L (type);
2186 G_READ_UNLOCK (&type_rw_lock);
2188 return node ? node->n_supers + 1 : 0;
2192 g_type_next_base (GType type,
2198 G_READ_LOCK (&type_rw_lock);
2199 node = lookup_type_node_L (type);
2202 TypeNode *base_node = lookup_type_node_L (base_type);
2204 if (base_node && base_node->n_supers < node->n_supers)
2206 guint n = node->n_supers - base_node->n_supers;
2208 if (node->supers[n] == base_type)
2209 atype = node->supers[n - 1];
2212 G_READ_UNLOCK (&type_rw_lock);
2217 static inline gboolean
2218 type_node_is_a_L (TypeNode *node,
2219 TypeNode *iface_node,
2220 /* support_inheritance */
2221 gboolean support_interfaces,
2222 gboolean support_prerequisites)
2224 if (support_interfaces &&
2225 node->is_instantiatable && NODE_IS_IFACE (iface_node) &&
2226 type_lookup_iface_entry_L (node, iface_node) != NULL)
2228 else if (/* support_inheritance && */
2229 iface_node->n_supers <= node->n_supers &&
2230 node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2232 else if (support_prerequisites &&
2233 NODE_IS_IFACE (node) &&
2234 type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2241 g_type_is_a (GType type,
2244 TypeNode *node, *iface_node;
2247 G_READ_LOCK (&type_rw_lock);
2248 node = lookup_type_node_L (type);
2249 iface_node = lookup_type_node_L (iface_type);
2250 is_a = node && iface_node && type_node_is_a_L (node, iface_node, TRUE, TRUE);
2251 G_READ_UNLOCK (&type_rw_lock);
2257 g_type_fundamental_branch_last (GType type)
2259 GType ftype = G_TYPE_FUNDAMENTAL (type);
2262 G_READ_LOCK (&type_rw_lock);
2263 last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
2264 G_READ_UNLOCK (&type_rw_lock);
2269 GType* /* free result */
2270 g_type_children (GType type,
2275 G_READ_LOCK (&type_rw_lock);
2276 node = lookup_type_node_L (type);
2279 GType *children = g_new (GType, node->n_children + 1);
2281 memcpy (children, node->children, sizeof (GType) * node->n_children);
2282 children[node->n_children] = 0;
2285 *n_children = node->n_children;
2286 G_READ_UNLOCK (&type_rw_lock);
2292 G_READ_UNLOCK (&type_rw_lock);
2300 GType* /* free result */
2301 g_type_interfaces (GType type,
2302 guint *n_interfaces)
2306 G_READ_LOCK (&type_rw_lock);
2307 node = lookup_type_node_L (type);
2308 if (node && node->is_instantiatable)
2310 GType *ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2313 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2314 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2318 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2319 G_READ_UNLOCK (&type_rw_lock);
2325 G_READ_UNLOCK (&type_rw_lock);
2333 typedef struct _QData QData;
2345 static inline gpointer
2346 type_get_qdata_L (TypeNode *node,
2349 GData *gdata = node->global_gdata;
2351 if (quark && gdata && gdata->n_qdatas)
2353 QData *qdatas = gdata->qdatas - 1;
2354 guint n_qdatas = gdata->n_qdatas;
2361 i = (n_qdatas + 1) / 2;
2363 if (quark == check->quark)
2365 else if (quark > check->quark)
2370 else /* if (quark < check->quark) */
2379 g_type_get_qdata (GType type,
2385 G_READ_LOCK (&type_rw_lock);
2386 node = lookup_type_node_L (type);
2389 data = type_get_qdata_L (node, quark);
2390 G_READ_UNLOCK (&type_rw_lock);
2394 G_READ_UNLOCK (&type_rw_lock);
2395 g_return_val_if_fail (node != NULL, NULL);
2402 type_set_qdata_W (TypeNode *node,
2410 /* setup qdata list if necessary */
2411 if (!node->global_gdata)
2412 node->global_gdata = g_new0 (GData, 1);
2413 gdata = node->global_gdata;
2415 /* try resetting old data */
2416 qdata = gdata->qdatas;
2417 for (i = 0; i < gdata->n_qdatas; i++)
2418 if (qdata[i].quark == quark)
2420 qdata[i].data = data;
2426 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2427 qdata = gdata->qdatas;
2428 for (i = 0; i < gdata->n_qdatas - 1; i++)
2429 if (qdata[i].quark > quark)
2431 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2432 qdata[i].quark = quark;
2433 qdata[i].data = data;
2437 g_type_set_qdata (GType type,
2443 g_return_if_fail (quark != 0);
2445 G_WRITE_LOCK (&type_rw_lock);
2446 node = lookup_type_node_L (type);
2449 type_set_qdata_W (node, quark, data);
2450 G_WRITE_UNLOCK (&type_rw_lock);
2454 G_WRITE_UNLOCK (&type_rw_lock);
2455 g_return_if_fail (node != NULL);
2460 type_add_flags_W (TypeNode *node,
2465 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2466 g_return_if_fail (node != NULL);
2468 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2469 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2470 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2472 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2476 g_type_query (GType type,
2481 g_return_if_fail (query != NULL);
2483 G_READ_LOCK (&type_rw_lock);
2484 node = lookup_type_node_L (type);
2485 if (node && node->is_classed && !node->plugin && node->data)
2487 /* type is classed and static, probably even instantiatable */
2489 query->type = NODE_TYPE (node);
2490 query->type_name = NODE_NAME (node);
2491 query->class_size = node->data->class.class_size;
2492 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2496 /* node is not static and classed, won't allow query */
2499 G_READ_UNLOCK (&type_rw_lock);
2503 /* --- implementation details --- */
2505 g_type_test_flags (GType type,
2509 gboolean result = FALSE;
2511 G_READ_LOCK (&type_rw_lock);
2512 node = lookup_type_node_L (type);
2515 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2516 guint tflags = flags & TYPE_FLAG_MASK;
2520 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2522 fflags = (finfo->type_flags & fflags) == fflags;
2528 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2532 result = tflags && fflags;
2534 G_READ_UNLOCK (&type_rw_lock);
2540 g_type_get_plugin (GType type)
2544 G_READ_LOCK (&type_rw_lock);
2545 node = lookup_type_node_L (type);
2546 G_READ_UNLOCK (&type_rw_lock);
2548 return node ? node->plugin : NULL;
2552 g_type_interface_get_plugin (GType instance_type,
2553 GType interface_type)
2558 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2560 G_READ_LOCK (&type_rw_lock);
2561 node = lookup_type_node_L (instance_type);
2562 iface = lookup_type_node_L (interface_type);
2565 IFaceHolder *iholder = iface_node_get_holders_L (iface);
2567 while (iholder && iholder->instance_type != instance_type)
2568 iholder = iholder->next;
2569 G_READ_UNLOCK (&type_rw_lock);
2572 return iholder->plugin;
2575 G_READ_UNLOCK (&type_rw_lock);
2577 g_return_val_if_fail (node == NULL, NULL);
2578 g_return_val_if_fail (iface == NULL, NULL);
2580 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2586 g_type_fundamental_last (void)
2590 G_READ_LOCK (&type_rw_lock);
2591 type = static_last_fundamental_id;
2592 G_READ_UNLOCK (&type_rw_lock);
2598 g_type_check_instance_is_a (GTypeInstance *type_instance,
2601 TypeNode *node, *iface;
2604 if (!type_instance || !type_instance->g_class)
2607 G_READ_LOCK (&type_rw_lock);
2608 node = lookup_type_node_L (type_instance->g_class->g_type);
2609 iface = lookup_type_node_L (iface_type);
2610 check = node && node->is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2611 G_READ_UNLOCK (&type_rw_lock);
2617 g_type_check_class_is_a (GTypeClass *type_class,
2620 TypeNode *node, *iface;
2626 G_READ_LOCK (&type_rw_lock);
2627 node = lookup_type_node_L (type_class->g_type);
2628 iface = lookup_type_node_L (is_a_type);
2629 check = node && node->is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2630 G_READ_UNLOCK (&type_rw_lock);
2636 g_type_check_instance_cast (GTypeInstance *type_instance,
2641 if (type_instance->g_class)
2643 TypeNode *node, *iface;
2644 gboolean is_instantiatable, check;
2646 G_READ_LOCK (&type_rw_lock);
2647 node = lookup_type_node_L (type_instance->g_class->g_type);
2648 is_instantiatable = node && node->is_instantiatable;
2649 iface = lookup_type_node_L (iface_type);
2650 check = is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2651 G_READ_UNLOCK (&type_rw_lock);
2653 return type_instance;
2655 if (is_instantiatable)
2656 g_warning ("invalid cast from `%s' to `%s'",
2657 type_descriptive_name_U (type_instance->g_class->g_type),
2658 type_descriptive_name_U (iface_type));
2660 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2661 type_descriptive_name_U (type_instance->g_class->g_type),
2662 type_descriptive_name_U (iface_type));
2665 g_warning ("invalid unclassed pointer in cast to `%s'",
2666 type_descriptive_name_U (iface_type));
2669 g_warning ("invalid cast from (NULL) pointer to `%s'",
2670 type_descriptive_name_U (iface_type));
2672 return type_instance;
2676 g_type_check_class_cast (GTypeClass *type_class,
2681 TypeNode *node, *iface;
2682 gboolean is_classed, check;
2684 G_READ_LOCK (&type_rw_lock);
2685 node = lookup_type_node_L (type_class->g_type);
2686 is_classed = node && node->is_classed;
2687 iface = lookup_type_node_L (is_a_type);
2688 check = is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2689 G_READ_UNLOCK (&type_rw_lock);
2694 g_warning ("invalid class cast from `%s' to `%s'",
2695 type_descriptive_name_U (type_class->g_type),
2696 type_descriptive_name_U (is_a_type));
2698 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2699 type_descriptive_name_U (type_class->g_type),
2700 type_descriptive_name_U (is_a_type));
2703 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2704 type_descriptive_name_U (is_a_type));
2709 g_type_check_instance (GTypeInstance *type_instance)
2711 /* this function is just here to make the signal system
2712 * conveniently elaborated on instance checks
2716 if (type_instance->g_class)
2719 gboolean is_instantiatable;
2721 G_READ_LOCK (&type_rw_lock);
2722 node = lookup_type_node_L (type_instance->g_class->g_type);
2723 is_instantiatable = node && node->is_instantiatable;
2724 G_READ_UNLOCK (&type_rw_lock);
2725 if (is_instantiatable)
2728 g_warning ("instance of invalid non-instantiatable type `%s'",
2729 type_descriptive_name_U (type_instance->g_class->g_type));
2732 g_warning ("instance with invalid (NULL) class pointer");
2735 g_warning ("invalid (NULL) pointer instance");
2740 static inline gboolean
2741 type_check_is_value_type_U (GType type)
2743 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2746 G_READ_LOCK (&type_rw_lock);
2748 node = lookup_type_node_L (type);
2751 if (node->data && node->data->common.ref_count > 0 &&
2752 node->data->common.value_table->value_init)
2753 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2754 else if (NODE_IS_IFACE (node))
2758 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2760 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2761 TypeNode *prnode = lookup_type_node_L (prtype);
2763 if (prnode->is_instantiatable)
2771 G_READ_UNLOCK (&type_rw_lock);
2773 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2777 g_type_check_is_value_type (GType type)
2779 return type_check_is_value_type_U (type);
2783 g_type_check_value (GValue *value)
2785 return value && type_check_is_value_type_U (value->g_type);
2789 g_type_check_value_holds (GValue *value,
2792 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2796 g_type_value_table_peek (GType type)
2799 GTypeValueTable *vtable;
2801 G_READ_LOCK (&type_rw_lock);
2803 node = lookup_type_node_L (type);
2806 g_warning (G_STRLOC ": type id `%u' is invalid", type);
2807 G_READ_UNLOCK (&type_rw_lock);
2810 if (!node->data || node->data->common.ref_count < 1)
2812 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2813 type_descriptive_name_L (type));
2814 G_READ_UNLOCK (&type_rw_lock);
2817 if (node->data->common.value_table->value_init)
2818 vtable = node->data->common.value_table;
2819 else if (NODE_IS_IFACE (node))
2823 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2825 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2826 TypeNode *prnode = lookup_type_node_L (prtype);
2828 if (prnode->is_instantiatable)
2831 goto restart_table_peek;
2838 G_READ_UNLOCK (&type_rw_lock);
2843 G_CONST_RETURN gchar*
2844 g_type_name_from_instance (GTypeInstance *instance)
2847 return "<NULL-instance>";
2849 return g_type_name_from_class (instance->g_class);
2852 G_CONST_RETURN gchar*
2853 g_type_name_from_class (GTypeClass *g_class)
2856 return "<NULL-class>";
2858 return g_type_name (g_class->g_type);
2862 /* --- foreign prototypes --- */
2863 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2864 extern void g_enum_types_init (void); /* sync with genums.c */
2865 extern void g_param_type_init (void); /* sync with gparam.c */
2866 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2867 extern void g_object_type_init (void); /* sync with gobject.c */
2868 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2869 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2870 extern void g_signal_init (void); /* sync with gsignal.c */
2873 /* --- initialization --- */
2875 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2877 G_LOCK_DEFINE_STATIC (type_init_lock);
2878 static TypeNode *type0_node = NULL;
2879 const gchar *env_string;
2884 G_LOCK (type_init_lock);
2886 G_WRITE_LOCK (&type_rw_lock);
2888 if (static_last_fundamental_id)
2890 G_WRITE_UNLOCK (&type_rw_lock);
2891 G_UNLOCK (type_init_lock);
2895 /* setup GRuntime wide debugging flags */
2896 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2897 env_string = g_getenv ("GRUNTIME_DEBUG");
2898 if (env_string != NULL)
2900 static GDebugKey debug_keys[] = {
2901 { "objects", G_TYPE_DEBUG_OBJECTS },
2902 { "signals", G_TYPE_DEBUG_SIGNALS },
2905 _g_type_debug_flags |= g_parse_debug_string (env_string,
2907 sizeof (debug_keys) / sizeof (debug_keys[0]));
2912 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
2913 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
2914 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
2916 /* type qname hash table */
2917 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2919 /* invalid type G_TYPE_INVALID (0)
2921 static_last_fundamental_id = 1;
2922 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2923 static_type_nodes[0] = &type0_node;
2924 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2925 static_branch_seqnos[0] = 1;
2927 /* void type G_TYPE_NONE
2929 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2930 type = NODE_TYPE (node);
2931 g_assert (type == G_TYPE_NONE);
2933 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2935 memset (&info, 0, sizeof (info));
2936 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2937 type = NODE_TYPE (node);
2938 type_data_make_W (node, &info, NULL);
2939 g_assert (type == G_TYPE_INTERFACE);
2941 G_WRITE_UNLOCK (&type_rw_lock);
2943 /* G_TYPE_TYPE_PLUGIN
2945 g_type_plugin_get_type ();
2947 /* G_TYPE_* value types
2949 g_value_types_init ();
2951 /* G_TYPE_ENUM & G_TYPE_FLAGS
2953 g_enum_types_init ();
2957 g_boxed_type_init ();
2961 g_param_type_init ();
2965 g_object_type_init ();
2967 /* G_TYPE_PARAM_* pspec types
2969 g_param_spec_types_init ();
2971 /* Value Transformations
2973 g_value_transforms_init ();
2979 G_UNLOCK (type_init_lock);
2985 g_type_init_with_debug_flags (0);