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.
43 * lock handling issues when calling static functions are indicated by
44 * uppercase letter postfixes, all static functions have to have
45 * one of the below postfixes:
46 * - _I: [Indifferent about locking]
47 * function doesn't care about locks at all
48 * - _U: [Unlocked invocation]
49 * no read or write lock has to be held across function invocation
50 * (locks may be acquired and released during invocation though)
51 * - _L: [Locked invocation]
52 * a write lock or more than 0 read locks have to be held across
54 * - _W: [Write-locked invocation]
55 * a write lock has to be held across function invokation
56 * - _Wm: [Write-locked invocation, mutatable]
57 * like _W, but the write lock might be released and reacquired
58 * during invocation, watch your pointers
61 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
63 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
64 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
65 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
66 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
68 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
69 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
70 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
71 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
73 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
74 static const gchar *_action = " invalidly modified type "; \
75 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
77 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
79 g_error ("%s()%s`%s'", _fname, _action, _tname); \
81 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
84 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
85 "%s: initialization assertion failed, use %s() prior to this function", \
86 G_STRLOC, G_STRINGIFY (init_function)); \
87 return (return_value); \
92 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
93 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
96 #else /* !G_ENABLE_DEBUG */
97 #define DEBUG_CODE(debug_type, code_block) /* code_block */
98 #endif /* G_ENABLE_DEBUG */
100 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
101 G_TYPE_FLAG_INSTANTIATABLE | \
102 G_TYPE_FLAG_DERIVABLE | \
103 G_TYPE_FLAG_DEEP_DERIVABLE)
104 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
105 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
106 sizeof (gpointer)), \
110 /* --- typedefs --- */
111 typedef struct _TypeNode TypeNode;
112 typedef struct _CommonData CommonData;
113 typedef struct _IFaceData IFaceData;
114 typedef struct _ClassData ClassData;
115 typedef struct _InstanceData InstanceData;
116 typedef union _TypeData TypeData;
117 typedef struct _IFaceEntry IFaceEntry;
118 typedef struct _IFaceHolder IFaceHolder;
121 /* --- prototypes --- */
122 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
123 static void type_add_flags_W (TypeNode *node,
125 static void type_data_make_W (TypeNode *node,
126 const GTypeInfo *info,
127 const GTypeValueTable *value_table);
128 static inline void type_data_ref_Wm (TypeNode *node);
129 static inline void type_data_unref_Wm (TypeNode *node,
131 static void type_data_last_unref_Wm (GType type,
133 static inline gpointer type_get_qdata_L (TypeNode *node,
135 static inline void type_set_qdata_W (TypeNode *node,
138 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
139 GType instance_type);
140 static gboolean type_node_is_a_L (TypeNode *node,
141 TypeNode *iface_node);
144 /* --- structures --- */
148 guint n_children : 12;
150 guint _prot_n_ifaces_prerequisites : 9;
151 guint is_classed : 1;
152 guint is_instantiatable : 1;
153 guint mutatable_check_cache : 1; /* combines some common path checks */
155 TypeData * volatile data;
159 IFaceEntry *iface_entries; /* for !iface types */
160 GType *prerequisistes;
162 GType supers[1]; /* flexible array */
164 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
165 #define MAX_N_SUPERS (255)
166 #define MAX_N_CHILDREN (4095)
167 #define MAX_N_IFACES (511)
168 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
169 #define NODE_TYPE(node) (node->supers[0])
170 #define NODE_PARENT_TYPE(node) (node->supers[1])
171 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
172 #define NODE_NAME(node) (g_quark_to_string (node->qname))
173 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
174 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
175 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
176 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
177 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
178 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
179 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
180 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
181 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
182 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
188 GInterfaceInfo *info;
195 GTypeInterface *vtable;
200 GTypeValueTable *value_table;
206 GBaseInitFunc vtable_init_base;
207 GBaseFinalizeFunc vtable_finalize_base;
213 GBaseInitFunc class_init_base;
214 GBaseFinalizeFunc class_finalize_base;
215 GClassInitFunc class_init;
216 GClassFinalizeFunc class_finalize;
217 gconstpointer class_data;
224 GBaseInitFunc class_init_base;
225 GBaseFinalizeFunc class_finalize_base;
226 GClassInitFunc class_init;
227 GClassFinalizeFunc class_finalize;
228 gconstpointer class_data;
230 guint16 instance_size;
232 GInstanceInitFunc instance_init;
233 GMemChunk *mem_chunk;
240 InstanceData instance;
244 GTypeClassCacheFunc cache_func;
248 /* --- variables --- */
249 static guint static_n_class_cache_funcs = 0;
250 static ClassCacheFunc *static_class_cache_funcs = NULL;
251 static GQuark static_quark_type_flags = 0;
252 static GQuark static_quark_iface_holder = 0;
253 static GQuark static_quark_dependants_array = 0;
254 GTypeDebugFlags _g_type_debug_flags = 0;
257 /* --- type nodes --- */
258 static GHashTable *static_type_nodes_ht = NULL;
259 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { 0, };
260 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
262 static inline TypeNode*
263 lookup_type_node_I (register GType utype)
265 if (utype > G_TYPE_FUNDAMENTAL_MAX)
266 return (TypeNode*) (utype & ~TYPE_ID_MASK);
268 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
272 type_node_any_new_W (TypeNode *pnode,
276 GTypeFundamentalFlags type_flags)
281 guint i, node_size = 0;
283 n_supers = pnode ? pnode->n_supers + 1 : 0;
286 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
287 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
288 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
289 node = g_malloc0 (node_size);
290 if (!pnode) /* offset fundamental types */
292 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
293 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
299 g_assert ((type & TYPE_ID_MASK) == 0);
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;
338 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
339 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
340 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
341 CLASSED_NODE_N_IFACES (node));
342 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
343 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
346 i = pnode->n_children++;
347 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
348 pnode->children[i] = type;
351 node->plugin = plugin;
352 node->n_children = 0;
353 node->children = NULL;
355 node->qname = g_quark_from_string (name);
356 node->global_gdata = NULL;
358 g_hash_table_insert (static_type_nodes_ht,
359 GUINT_TO_POINTER (node->qname),
364 static inline GTypeFundamentalInfo*
365 type_node_fundamental_info_I (TypeNode *node)
367 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
369 if (ftype != NODE_TYPE (node))
370 node = lookup_type_node_I (ftype);
372 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
376 type_node_fundamental_new_W (GType ftype,
378 GTypeFundamentalFlags type_flags)
380 GTypeFundamentalInfo *finfo;
383 g_assert ((ftype & TYPE_ID_MASK) == 0);
384 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
386 if (ftype == static_fundamental_next)
387 static_fundamental_next += 1 << G_TYPE_FUNDAMENTAL_SHIFT;
389 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
391 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
393 finfo = type_node_fundamental_info_I (node);
394 finfo->type_flags = type_flags;
400 type_node_new_W (TypeNode *pnode,
406 g_assert (pnode->n_supers < MAX_N_SUPERS);
407 g_assert (pnode->n_children < MAX_N_CHILDREN);
409 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
412 static inline IFaceEntry*
413 type_lookup_iface_entry_L (TypeNode *node,
414 TypeNode *iface_node)
416 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
418 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
419 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
420 GType iface_type = NODE_TYPE (iface_node);
427 i = (n_ifaces + 1) >> 1;
429 if (iface_type == check->iface_type)
431 else if (iface_type > check->iface_type)
436 else /* if (iface_type < check->iface_type) */
445 static inline gboolean
446 type_lookup_prerequisite_L (TypeNode *iface,
447 GType prerequisite_type)
449 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
451 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
452 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
459 i = (n_prerequisites + 1) >> 1;
460 check = prerequisites + i;
461 if (prerequisite_type == *check)
463 else if (prerequisite_type > *check)
465 n_prerequisites -= i;
466 prerequisites = check;
468 else /* if (prerequisite_type < *check) */
469 n_prerequisites = i - 1;
471 while (n_prerequisites);
477 type_descriptive_name_I (GType type)
481 TypeNode *node = lookup_type_node_I (type);
483 return node ? NODE_NAME (node) : "<unknown>";
490 /* --- type consistency checks --- */
492 check_plugin_U (GTypePlugin *plugin,
493 gboolean need_complete_type_info,
494 gboolean need_complete_interface_info,
495 const gchar *type_name)
497 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
501 g_warning ("plugin handle for type `%s' is NULL",
505 if (!G_IS_TYPE_PLUGIN (plugin))
507 g_warning ("plugin pointer (%p) for type `%s' is invalid",
511 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
513 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
517 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
519 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
527 check_type_name_I (const gchar *type_name)
529 static const gchar *extra_chars = "-_+";
530 const gchar *p = type_name;
533 if (!type_name[0] || !type_name[1] || !type_name[2])
535 g_warning ("type name `%s' is too short", type_name);
538 /* check the first letter */
539 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
540 for (p = type_name + 1; *p; p++)
541 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
542 (p[0] >= 'a' && p[0] <= 'z') ||
543 (p[0] >= '0' && p[0] <= '9') ||
544 strchr (extra_chars, p[0]));
547 g_warning ("type name `%s' contains invalid characters", type_name);
550 if (g_type_from_name (type_name))
552 g_warning ("cannot register existing type `%s'", type_name);
560 check_derivation_I (GType parent_type,
561 const gchar *type_name)
564 GTypeFundamentalInfo* finfo;
566 pnode = lookup_type_node_I (parent_type);
569 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
571 type_descriptive_name_I (parent_type));
574 finfo = type_node_fundamental_info_I (pnode);
575 /* ensure flat derivability */
576 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
578 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
583 /* ensure deep derivability */
584 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
585 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
587 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
597 check_collect_format_I (const gchar *collect_format)
599 const gchar *p = collect_format;
600 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
601 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
602 G_VALUE_COLLECT_POINTER, 0 };
605 if (!strchr (valid_format, *p++))
607 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
611 check_value_table_I (const gchar *type_name,
612 const GTypeValueTable *value_table)
616 else if (value_table->value_init == NULL)
618 if (value_table->value_free || value_table->value_copy ||
619 value_table->value_peek_pointer ||
620 value_table->collect_format || value_table->collect_value ||
621 value_table->lcopy_format || value_table->lcopy_value)
622 g_warning ("cannot handle uninitializable values of type `%s'",
626 else /* value_table->value_init != NULL */
628 if (!value_table->value_free)
631 * g_warning ("missing `value_free()' for type `%s'", type_name);
635 if (!value_table->value_copy)
637 g_warning ("missing `value_copy()' for type `%s'", type_name);
640 if ((value_table->collect_format || value_table->collect_value) &&
641 (!value_table->collect_format || !value_table->collect_value))
643 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
647 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
649 g_warning ("the `%s' specification for type `%s' is too long or invalid",
654 if ((value_table->lcopy_format || value_table->lcopy_value) &&
655 (!value_table->lcopy_format || !value_table->lcopy_value))
657 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
661 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
663 g_warning ("the `%s' specification for type `%s' is too long or invalid",
673 check_type_info_I (TypeNode *pnode,
675 const gchar *type_name,
676 const GTypeInfo *info)
678 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
679 gboolean is_interface = ftype == G_TYPE_INTERFACE;
681 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
683 /* check instance members */
684 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
685 (info->instance_size || info->n_preallocs || info->instance_init))
688 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
692 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
696 /* check class & interface members */
697 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
698 (info->class_init || info->class_finalize || info->class_data ||
699 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
702 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
706 g_warning ("cannot create class for `%s' as non-classed fundamental",
710 /* check interface size */
711 if (is_interface && info->class_size < sizeof (GTypeInterface))
713 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
717 /* check class size */
718 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
720 if (info->class_size < sizeof (GTypeClass))
722 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
726 if (pnode && info->class_size < pnode->data->class.class_size)
728 g_warning ("specified class size for type `%s' is smaller "
729 "than the parent type's `%s' class size",
735 /* check instance size */
736 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
738 if (info->instance_size < sizeof (GTypeInstance))
740 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
744 if (pnode && info->instance_size < pnode->data->instance.instance_size)
746 g_warning ("specified instance size for type `%s' is smaller "
747 "than the parent type's `%s' instance size",
758 find_conforming_child_type_L (TypeNode *pnode,
761 TypeNode *node = NULL;
764 if (type_lookup_iface_entry_L (pnode, iface))
767 for (i = 0; i < pnode->n_children && !node; i++)
768 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
774 check_add_interface_L (GType instance_type,
777 TypeNode *node = lookup_type_node_I (instance_type);
778 TypeNode *iface = lookup_type_node_I (iface_type);
781 GType *prerequisites;
785 if (!node || !node->is_instantiatable)
787 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
788 type_descriptive_name_I (instance_type));
791 if (!iface || !NODE_IS_IFACE (iface))
793 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
794 type_descriptive_name_I (iface_type),
798 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
799 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
801 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
802 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
808 /* allow overriding of interface type introduced for parent type */
809 entry = type_lookup_iface_entry_L (node, iface);
810 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
812 /* ok, we do conform to this interface already, but the interface vtable was not
813 * yet intialized, and we just conform to the interface because it got added to
814 * one of our parents. so we allow overriding of holder info here.
818 /* check whether one of our children already conforms (or whether the interface
819 * got added to this node already)
821 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
824 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
830 prerequisites = IFACE_NODE_PREREQUISITES (iface);
831 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
833 tnode = lookup_type_node_I (prerequisites[i]);
834 if (!type_node_is_a_L (node, tnode))
836 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
847 check_interface_info_I (TypeNode *iface,
849 const GInterfaceInfo *info)
851 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
853 g_warning ("interface type `%s' for type `%s' comes without initializer",
855 type_descriptive_name_I (instance_type));
862 /* --- type info (type node data) --- */
864 type_data_make_W (TypeNode *node,
865 const GTypeInfo *info,
866 const GTypeValueTable *value_table)
869 GTypeValueTable *vtable = NULL;
870 guint vtable_size = 0;
872 g_assert (node->data == NULL && info != NULL);
876 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
879 vtable = pnode->data->common.value_table;
882 static const GTypeValueTable zero_vtable = { NULL, };
884 value_table = &zero_vtable;
889 /* need to setup vtable_size since we have to allocate it with data in one chunk */
890 vtable_size = sizeof (GTypeValueTable);
891 if (value_table->collect_format)
892 vtable_size += strlen (value_table->collect_format);
893 if (value_table->lcopy_format)
894 vtable_size += strlen (value_table->lcopy_format);
898 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
900 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
902 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
903 data->instance.class_size = info->class_size;
904 data->instance.class_init_base = info->base_init;
905 data->instance.class_finalize_base = info->base_finalize;
906 data->instance.class_init = info->class_init;
907 data->instance.class_finalize = info->class_finalize;
908 data->instance.class_data = info->class_data;
909 data->instance.class = NULL;
910 data->instance.instance_size = info->instance_size;
911 #ifdef DISABLE_MEM_POOLS
912 data->instance.n_preallocs = 0;
913 #else /* !DISABLE_MEM_POOLS */
914 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
915 #endif /* !DISABLE_MEM_POOLS */
916 data->instance.instance_init = info->instance_init;
917 data->instance.mem_chunk = NULL;
919 else if (node->is_classed) /* only classed */
921 data = g_malloc0 (sizeof (ClassData) + vtable_size);
923 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
924 data->class.class_size = info->class_size;
925 data->class.class_init_base = info->base_init;
926 data->class.class_finalize_base = info->base_finalize;
927 data->class.class_init = info->class_init;
928 data->class.class_finalize = info->class_finalize;
929 data->class.class_data = info->class_data;
930 data->class.class = NULL;
932 else if (NODE_IS_IFACE (node))
934 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
936 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
937 data->iface.vtable_size = info->class_size;
938 data->iface.vtable_init_base = info->base_init;
939 data->iface.vtable_finalize_base = info->base_finalize;
943 data = g_malloc0 (sizeof (CommonData) + vtable_size);
945 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
949 node->data->common.ref_count = 1;
955 /* we allocate the vtable and its strings together with the type data, so
956 * children can take over their parent's vtable pointer, and we don't
957 * need to worry freeing it or not when the child data is destroyed
959 *vtable = *value_table;
960 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
962 vtable->collect_format = p;
963 if (value_table->collect_format)
965 strcat (p, value_table->collect_format);
966 p += strlen (value_table->collect_format);
970 vtable->lcopy_format = p;
971 if (value_table->lcopy_format)
972 strcat (p, value_table->lcopy_format);
974 node->data->common.value_table = vtable;
975 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
976 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
977 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
979 g_assert (node->data->common.value_table != NULL); /* paranoid */
983 type_data_ref_Wm (TypeNode *node)
987 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
989 GTypeValueTable tmp_value_table;
991 g_assert (node->plugin != NULL);
995 type_data_ref_Wm (pnode);
997 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1000 memset (&tmp_info, 0, sizeof (tmp_info));
1001 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1003 G_WRITE_UNLOCK (&type_rw_lock);
1004 g_type_plugin_use (node->plugin);
1005 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1006 G_WRITE_LOCK (&type_rw_lock);
1008 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1010 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1011 type_data_make_W (node, &tmp_info,
1012 check_value_table_I (NODE_NAME (node),
1013 &tmp_value_table) ? &tmp_value_table : NULL);
1017 g_assert (node->data->common.ref_count > 0);
1019 node->data->common.ref_count += 1;
1024 type_data_unref_Wm (TypeNode *node,
1027 g_assert (node->data && node->data->common.ref_count);
1029 if (node->data->common.ref_count > 1)
1030 node->data->common.ref_count -= 1;
1035 g_warning ("static type `%s' unreferenced too often",
1040 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1045 type_node_add_iface_entry_W (TypeNode *node,
1048 IFaceEntry *entries;
1051 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1053 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1054 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1055 if (entries[i].iface_type == iface_type)
1057 /* this can (should) only happen if our parent type already conformed
1058 * to iface_type and node got it's own holder info. here, our
1059 * children should already have entries with NULL vtables, so
1060 * we're actually done.
1062 g_assert (entries[i].vtable == NULL);
1065 else if (entries[i].iface_type > iface_type)
1067 CLASSED_NODE_N_IFACES (node) += 1;
1068 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1069 CLASSED_NODE_IFACES_ENTRIES (node),
1070 CLASSED_NODE_N_IFACES (node));
1071 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1072 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1073 entries[i].iface_type = iface_type;
1074 entries[i].vtable = NULL;
1076 for (i = 0; i < node->n_children; i++)
1077 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type);
1081 type_add_interface_W (TypeNode *node,
1083 const GInterfaceInfo *info,
1084 GTypePlugin *plugin)
1086 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1088 /* we must not call any functions of GInterfaceInfo from within here, since
1089 * we got most probably called from _within_ a type registration function
1091 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1093 iholder->next = iface_node_get_holders_L (iface);
1094 iface_node_set_holders_W (iface, iholder);
1095 iholder->instance_type = NODE_TYPE (node);
1096 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1097 iholder->plugin = plugin;
1099 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1103 type_iface_add_prerequisite_W (TypeNode *iface,
1104 TypeNode *prerequisite_node)
1106 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1107 GType *prerequisites, *dependants;
1108 guint n_dependants, i;
1110 g_assert (NODE_IS_IFACE (iface) &&
1111 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1112 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1114 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1115 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1116 if (prerequisites[i] == prerequisite_type)
1117 return; /* we already have that prerequisiste */
1118 else if (prerequisites[i] > prerequisite_type)
1120 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1121 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1122 IFACE_NODE_PREREQUISITES (iface),
1123 IFACE_NODE_N_PREREQUISITES (iface));
1124 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1125 g_memmove (prerequisites + i + 1, prerequisites + i,
1126 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1127 prerequisites[i] = prerequisite_type;
1129 /* we want to get notified when prerequisites get added to prerequisite_node */
1130 if (NODE_IS_IFACE (prerequisite_node))
1132 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1133 n_dependants = dependants ? dependants[0] : 0;
1135 dependants = g_renew (GType, dependants, n_dependants + 1);
1136 dependants[n_dependants] = NODE_TYPE (iface);
1137 dependants[0] = n_dependants;
1138 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1141 /* we need to notify all dependants */
1142 dependants = iface_node_get_dependants_array_L (iface);
1143 n_dependants = dependants ? dependants[0] : 0;
1144 for (i = 1; i <= n_dependants; i++)
1145 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1149 g_type_interface_add_prerequisite (GType interface_type,
1150 GType prerequisite_type)
1152 TypeNode *iface, *prerequisite_node;
1153 IFaceHolder *holders;
1155 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1156 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1157 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1159 iface = lookup_type_node_I (interface_type);
1160 prerequisite_node = lookup_type_node_I (prerequisite_type);
1161 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1163 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1164 type_descriptive_name_I (interface_type),
1165 type_descriptive_name_I (prerequisite_type));
1168 G_WRITE_LOCK (&type_rw_lock);
1169 holders = iface_node_get_holders_L (iface);
1172 G_WRITE_UNLOCK (&type_rw_lock);
1173 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1174 type_descriptive_name_I (prerequisite_type),
1175 type_descriptive_name_I (interface_type),
1176 type_descriptive_name_I (holders->instance_type));
1179 if (prerequisite_node->is_instantiatable)
1183 /* can have at most one publically installable instantiatable prerequisite */
1184 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1186 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1188 if (prnode->is_instantiatable)
1190 G_WRITE_UNLOCK (&type_rw_lock);
1191 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1192 type_descriptive_name_I (prerequisite_type),
1193 type_descriptive_name_I (interface_type),
1194 type_descriptive_name_I (NODE_TYPE (prnode)));
1199 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1200 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1201 G_WRITE_UNLOCK (&type_rw_lock);
1203 else if (NODE_IS_IFACE (prerequisite_node))
1205 GType *prerequisites;
1208 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1209 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1210 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1211 type_iface_add_prerequisite_W (iface, prerequisite_node);
1212 G_WRITE_UNLOCK (&type_rw_lock);
1216 G_WRITE_UNLOCK (&type_rw_lock);
1217 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1218 type_descriptive_name_I (prerequisite_type),
1219 type_descriptive_name_I (interface_type));
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_I (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 node = lookup_type_node_I (type);
1305 if (!node || !node->is_instantiatable)
1307 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1308 type_descriptive_name_I (type));
1311 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1312 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1314 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1315 type_descriptive_name_I (type));
1319 class = g_type_class_ref (type);
1321 if (node->data->instance.n_preallocs)
1323 G_WRITE_LOCK (&type_rw_lock);
1324 if (!node->data->instance.mem_chunk)
1325 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1326 node->data->instance.instance_size,
1327 (node->data->instance.instance_size *
1328 node->data->instance.n_preallocs),
1330 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1331 G_WRITE_UNLOCK (&type_rw_lock);
1334 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1335 for (i = node->n_supers; i > 0; i--)
1339 pnode = lookup_type_node_I (node->supers[i]);
1340 if (pnode->data->instance.instance_init)
1342 instance->g_class = pnode->data->instance.class;
1343 pnode->data->instance.instance_init (instance, class);
1346 instance->g_class = class;
1348 if (node->data->instance.instance_init)
1349 node->data->instance.instance_init (instance, class);
1355 g_type_free_instance (GTypeInstance *instance)
1360 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1362 class = instance->g_class;
1363 node = lookup_type_node_I (class->g_type);
1364 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1366 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1367 type_descriptive_name_I (class->g_type));
1370 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1371 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1373 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1378 instance->g_class = NULL;
1379 #ifdef G_ENABLE_DEBUG
1380 memset (instance, 0xaa, node->data->instance.instance_size); /* debugging hack */
1382 if (node->data->instance.n_preallocs)
1384 G_WRITE_LOCK (&type_rw_lock);
1385 g_chunk_free (instance, node->data->instance.mem_chunk);
1386 G_WRITE_UNLOCK (&type_rw_lock);
1391 g_type_class_unref (class);
1395 type_iface_vtable_init_Wm (TypeNode *iface,
1398 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1399 IFaceHolder *iholder;
1400 GTypeInterface *vtable = NULL;
1403 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1404 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1406 return FALSE; /* we don't modify write lock upon FALSE */
1408 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1410 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1411 if (pnode) /* want to copy over parent iface contents */
1413 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1416 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1419 vtable = g_malloc0 (iface->data->iface.vtable_size);
1420 entry->vtable = vtable;
1421 vtable->g_type = NODE_TYPE (iface);
1422 vtable->g_instance_type = NODE_TYPE (node);
1424 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1426 G_WRITE_UNLOCK (&type_rw_lock);
1427 if (iface->data->iface.vtable_init_base)
1428 iface->data->iface.vtable_init_base (vtable);
1429 if (iholder->info->interface_init)
1430 iholder->info->interface_init (vtable, iholder->info->interface_data);
1431 G_WRITE_LOCK (&type_rw_lock);
1433 return TRUE; /* write lock modified */
1437 type_iface_vtable_finalize_Wm (TypeNode *iface,
1439 GTypeInterface *vtable)
1441 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1442 IFaceHolder *iholder;
1444 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1445 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1447 return FALSE; /* we don't modify write lock upon FALSE */
1449 g_assert (entry && entry->vtable == vtable && iholder->info);
1451 entry->vtable = NULL;
1452 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1454 G_WRITE_UNLOCK (&type_rw_lock);
1455 if (iholder->info->interface_finalize)
1456 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1457 if (iface->data->iface.vtable_finalize_base)
1458 iface->data->iface.vtable_finalize_base (vtable);
1459 G_WRITE_LOCK (&type_rw_lock);
1462 vtable->g_instance_type = 0;
1465 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1467 return TRUE; /* write lock modified */
1471 type_class_init_Wm (TypeNode *node,
1474 GSList *slist, *init_slist = NULL;
1477 TypeNode *bnode, *pnode;
1480 g_assert (node->is_classed && node->data &&
1481 node->data->class.class_size &&
1482 !node->data->class.class);
1484 class = g_malloc0 (node->data->class.class_size);
1485 node->data->class.class = class;
1489 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1491 memcpy (class, pclass, pnode->data->class.class_size);
1493 class->g_type = NODE_TYPE (node);
1495 G_WRITE_UNLOCK (&type_rw_lock);
1497 /* stack all base class initialization functions, so we
1498 * call them in ascending order.
1500 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1501 if (bnode->data->class.class_init_base)
1502 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1503 for (slist = init_slist; slist; slist = slist->next)
1505 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1507 class_init_base (class);
1509 g_slist_free (init_slist);
1511 if (node->data->class.class_init)
1512 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1514 G_WRITE_LOCK (&type_rw_lock);
1516 /* ok, we got the class done, now initialize all interfaces, either
1517 * from parent, or through our holder info
1519 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1520 entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1523 g_assert (entry->vtable == NULL);
1525 if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1529 /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1530 * need to get this interface from parent
1532 g_assert (pnode != NULL);
1534 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1536 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1538 if (pentry->iface_type == entry->iface_type)
1540 entry->vtable = pentry->vtable;
1544 g_assert (entry->vtable != NULL);
1547 /* refetch entry, IFACES_ENTRIES might be modified */
1548 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1549 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1550 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1555 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1560 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1562 g_message ("finalizing interfaces for %sClass `%s'",
1563 type_descriptive_name_I (NODE_FUNDAMENTAL_TYPE (node)),
1564 type_descriptive_name_I (NODE_TYPE (node)));
1566 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1567 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1568 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1569 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1572 if (!type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1574 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1575 * iface vtable came from parent
1577 entry->vtable = NULL;
1580 /* refetch entry, IFACES_ENTRIES might be modified */
1581 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1582 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1583 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1584 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1589 type_data_finalize_class_U (TypeNode *node,
1592 GTypeClass *class = cdata->class;
1595 g_assert (cdata->class && cdata->common.ref_count == 0);
1597 if (cdata->class_finalize)
1598 cdata->class_finalize (class, (gpointer) cdata->class_data);
1600 /* call all base class destruction functions in descending order
1602 if (cdata->class_finalize_base)
1603 cdata->class_finalize_base (class);
1604 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1605 if (bnode->data->class.class_finalize_base)
1606 bnode->data->class.class_finalize_base (class);
1609 g_free (cdata->class);
1613 type_data_last_unref_Wm (GType type,
1616 TypeNode *node = lookup_type_node_I (type);
1618 g_return_if_fail (node != NULL && node->plugin != NULL);
1620 if (!node->data || node->data->common.ref_count == 0)
1622 g_warning ("cannot drop last reference to unreferenced type `%s'",
1623 type_descriptive_name_I (type));
1627 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1631 G_WRITE_UNLOCK (&type_rw_lock);
1632 G_READ_LOCK (&type_rw_lock);
1633 for (i = 0; i < static_n_class_cache_funcs; i++)
1635 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1636 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1637 gboolean need_break;
1639 G_READ_UNLOCK (&type_rw_lock);
1640 need_break = cache_func (cache_data, node->data->class.class);
1641 G_READ_LOCK (&type_rw_lock);
1642 if (!node->data || node->data->common.ref_count == 0)
1643 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1647 G_READ_UNLOCK (&type_rw_lock);
1648 G_WRITE_LOCK (&type_rw_lock);
1651 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1652 node->data->common.ref_count -= 1;
1655 GType ptype = NODE_PARENT_TYPE (node);
1658 node->data->common.ref_count = 0;
1660 if (node->is_instantiatable && node->data->instance.mem_chunk)
1662 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1663 node->data->instance.mem_chunk = NULL;
1667 if (node->is_classed && tdata->class.class)
1669 if (CLASSED_NODE_N_IFACES (node))
1670 type_data_finalize_class_ifaces_Wm (node);
1671 node->mutatable_check_cache = FALSE;
1673 G_WRITE_UNLOCK (&type_rw_lock);
1674 type_data_finalize_class_U (node, &tdata->class);
1675 G_WRITE_LOCK (&type_rw_lock);
1679 node->mutatable_check_cache = FALSE;
1683 /* freeing tdata->common.value_table and its contents is taking care of
1684 * by allocating it in one chunk with tdata
1689 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1690 G_WRITE_UNLOCK (&type_rw_lock);
1691 g_type_plugin_unuse (node->plugin);
1692 G_WRITE_LOCK (&type_rw_lock);
1697 g_type_add_class_cache_func (gpointer cache_data,
1698 GTypeClassCacheFunc cache_func)
1702 g_return_if_fail (cache_func != NULL);
1704 G_WRITE_LOCK (&type_rw_lock);
1705 i = static_n_class_cache_funcs++;
1706 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1707 static_class_cache_funcs[i].cache_data = cache_data;
1708 static_class_cache_funcs[i].cache_func = cache_func;
1709 G_WRITE_UNLOCK (&type_rw_lock);
1713 g_type_remove_class_cache_func (gpointer cache_data,
1714 GTypeClassCacheFunc cache_func)
1716 gboolean found_it = FALSE;
1719 g_return_if_fail (cache_func != NULL);
1721 G_WRITE_LOCK (&type_rw_lock);
1722 for (i = 0; i < static_n_class_cache_funcs; i++)
1723 if (static_class_cache_funcs[i].cache_data == cache_data &&
1724 static_class_cache_funcs[i].cache_func == cache_func)
1726 static_n_class_cache_funcs--;
1727 g_memmove (static_class_cache_funcs + i,
1728 static_class_cache_funcs + i + 1,
1729 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1730 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1734 G_WRITE_UNLOCK (&type_rw_lock);
1737 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1738 cache_func, cache_data);
1742 /* --- type registration --- */
1744 g_type_register_fundamental (GType type_id,
1745 const gchar *type_name,
1746 const GTypeInfo *info,
1747 const GTypeFundamentalInfo *finfo,
1750 GTypeFundamentalInfo *node_finfo;
1753 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1754 g_return_val_if_fail (type_id > 0, 0);
1755 g_return_val_if_fail (type_name != NULL, 0);
1756 g_return_val_if_fail (info != NULL, 0);
1757 g_return_val_if_fail (finfo != NULL, 0);
1759 if (!check_type_name_I (type_name))
1761 if ((type_id & TYPE_ID_MASK) ||
1762 type_id > G_TYPE_FUNDAMENTAL_MAX)
1764 g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
1769 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1770 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1772 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1776 if (lookup_type_node_I (type_id))
1778 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1779 type_descriptive_name_I (type_id),
1784 G_WRITE_LOCK (&type_rw_lock);
1785 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1786 node_finfo = type_node_fundamental_info_I (node);
1787 type_add_flags_W (node, flags);
1789 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1790 type_data_make_W (node, info,
1791 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1792 G_WRITE_UNLOCK (&type_rw_lock);
1794 return NODE_TYPE (node);
1798 g_type_register_static (GType parent_type,
1799 const gchar *type_name,
1800 const GTypeInfo *info,
1803 TypeNode *pnode, *node;
1806 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1807 g_return_val_if_fail (parent_type > 0, 0);
1808 g_return_val_if_fail (type_name != NULL, 0);
1809 g_return_val_if_fail (info != NULL, 0);
1811 if (!check_type_name_I (type_name) ||
1812 !check_derivation_I (parent_type, type_name))
1814 if (info->class_finalize)
1816 g_warning ("class finalizer specified for static type `%s'",
1821 pnode = lookup_type_node_I (parent_type);
1822 G_WRITE_LOCK (&type_rw_lock);
1823 type_data_ref_Wm (pnode);
1824 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1826 node = type_node_new_W (pnode, type_name, NULL);
1827 type_add_flags_W (node, flags);
1828 type = NODE_TYPE (node);
1829 type_data_make_W (node, info,
1830 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1832 G_WRITE_UNLOCK (&type_rw_lock);
1838 g_type_register_dynamic (GType parent_type,
1839 const gchar *type_name,
1840 GTypePlugin *plugin,
1843 TypeNode *pnode, *node;
1846 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1847 g_return_val_if_fail (parent_type > 0, 0);
1848 g_return_val_if_fail (type_name != NULL, 0);
1849 g_return_val_if_fail (plugin != NULL, 0);
1851 if (!check_type_name_I (type_name) ||
1852 !check_derivation_I (parent_type, type_name) ||
1853 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1856 G_WRITE_LOCK (&type_rw_lock);
1857 pnode = lookup_type_node_I (parent_type);
1858 node = type_node_new_W (pnode, type_name, plugin);
1859 type_add_flags_W (node, flags);
1860 type = NODE_TYPE (node);
1861 G_WRITE_UNLOCK (&type_rw_lock);
1867 g_type_add_interface_static (GType instance_type,
1868 GType interface_type,
1869 const GInterfaceInfo *info)
1871 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1872 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1873 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1875 G_WRITE_LOCK (&type_rw_lock);
1876 if (check_add_interface_L (instance_type, interface_type))
1878 TypeNode *node = lookup_type_node_I (instance_type);
1879 TypeNode *iface = lookup_type_node_I (interface_type);
1881 if (check_interface_info_I (iface, NODE_TYPE (node), info))
1883 type_add_interface_W (node, iface, info, NULL);
1884 /* if we have a class already, the interface vtable needs to
1885 * be initialized as well
1887 if (node->data && node->data->class.class)
1888 type_iface_vtable_init_Wm (iface, node);
1891 G_WRITE_UNLOCK (&type_rw_lock);
1895 g_type_add_interface_dynamic (GType instance_type,
1896 GType interface_type,
1897 GTypePlugin *plugin)
1901 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1902 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1903 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1905 node = lookup_type_node_I (instance_type);
1906 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1909 G_WRITE_LOCK (&type_rw_lock);
1910 if (check_add_interface_L (instance_type, interface_type))
1912 TypeNode *iface = lookup_type_node_I (interface_type);
1914 type_add_interface_W (node, iface, NULL, plugin);
1915 /* if we have a class already, the interface vtable needs to
1916 * be initialized as well
1918 if (node->data && node->data->class.class)
1919 type_iface_vtable_init_Wm (iface, node);
1921 G_WRITE_UNLOCK (&type_rw_lock);
1925 /* --- public API functions --- */
1927 g_type_class_ref (GType type)
1931 /* optimize for common code path
1933 G_WRITE_LOCK (&type_rw_lock);
1934 node = lookup_type_node_I (type);
1935 if (node && node->is_classed && node->data &&
1936 node->data->class.class && node->data->common.ref_count > 0)
1938 type_data_ref_Wm (node);
1939 G_WRITE_UNLOCK (&type_rw_lock);
1941 return node->data->class.class;
1944 if (!node || !node->is_classed ||
1945 (node->data && node->data->common.ref_count < 1))
1947 G_WRITE_UNLOCK (&type_rw_lock);
1948 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
1949 type_descriptive_name_I (type));
1953 type_data_ref_Wm (node);
1955 if (!node->data->class.class)
1957 GType ptype = NODE_PARENT_TYPE (node);
1958 GTypeClass *pclass = NULL;
1962 G_WRITE_UNLOCK (&type_rw_lock);
1963 pclass = g_type_class_ref (ptype);
1964 if (node->data->class.class)
1965 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1966 G_WRITE_LOCK (&type_rw_lock);
1969 type_class_init_Wm (node, pclass);
1971 G_WRITE_UNLOCK (&type_rw_lock);
1973 return node->data->class.class;
1977 g_type_class_unref (gpointer g_class)
1980 GTypeClass *class = g_class;
1982 g_return_if_fail (g_class != NULL);
1984 node = lookup_type_node_I (class->g_type);
1985 G_WRITE_LOCK (&type_rw_lock);
1986 if (node && node->is_classed && node->data &&
1987 node->data->class.class == class && node->data->common.ref_count > 0)
1988 type_data_unref_Wm (node, FALSE);
1990 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1991 type_descriptive_name_I (class->g_type));
1992 G_WRITE_UNLOCK (&type_rw_lock);
1996 g_type_class_unref_uncached (gpointer g_class)
1999 GTypeClass *class = g_class;
2001 g_return_if_fail (g_class != NULL);
2003 G_WRITE_LOCK (&type_rw_lock);
2004 node = lookup_type_node_I (class->g_type);
2005 if (node && node->is_classed && node->data &&
2006 node->data->class.class == class && node->data->common.ref_count > 0)
2007 type_data_unref_Wm (node, TRUE);
2009 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2010 type_descriptive_name_I (class->g_type));
2011 G_WRITE_UNLOCK (&type_rw_lock);
2015 g_type_class_peek (GType type)
2020 node = lookup_type_node_I (type);
2021 G_READ_LOCK (&type_rw_lock);
2022 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2023 class = node->data->class.class;
2026 G_READ_UNLOCK (&type_rw_lock);
2032 g_type_class_peek_parent (gpointer g_class)
2035 gpointer class = NULL;
2037 g_return_val_if_fail (g_class != NULL, NULL);
2039 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2040 G_READ_LOCK (&type_rw_lock);
2041 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2043 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2044 class = node->data->class.class;
2046 else if (NODE_PARENT_TYPE (node))
2047 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2048 G_READ_UNLOCK (&type_rw_lock);
2054 g_type_interface_peek (gpointer instance_class,
2059 gpointer vtable = NULL;
2060 GTypeClass *class = instance_class;
2062 g_return_val_if_fail (instance_class != NULL, NULL);
2064 node = lookup_type_node_I (class->g_type);
2065 iface = lookup_type_node_I (iface_type);
2066 if (node && node->is_instantiatable && iface)
2070 G_READ_LOCK (&type_rw_lock);
2072 entry = type_lookup_iface_entry_L (node, iface);
2073 if (entry && entry->vtable) /* entry is relocatable */
2074 vtable = entry->vtable;
2076 G_READ_UNLOCK (&type_rw_lock);
2079 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2085 g_type_interface_peek_parent (gpointer g_iface)
2089 gpointer vtable = NULL;
2090 GTypeInterface *iface_class = g_iface;
2092 g_return_val_if_fail (g_iface != NULL, NULL);
2094 iface = lookup_type_node_I (iface_class->g_type);
2095 node = lookup_type_node_I (iface_class->g_instance_type);
2097 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2098 if (node && node->is_instantiatable && iface)
2102 G_READ_LOCK (&type_rw_lock);
2104 entry = type_lookup_iface_entry_L (node, iface);
2105 if (entry && entry->vtable) /* entry is relocatable */
2106 vtable = entry->vtable;
2108 G_READ_UNLOCK (&type_rw_lock);
2111 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2116 G_CONST_RETURN gchar*
2117 g_type_name (GType type)
2121 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2123 node = lookup_type_node_I (type);
2125 return node ? NODE_NAME (node) : NULL;
2129 g_type_qname (GType type)
2133 node = lookup_type_node_I (type);
2135 return node ? node->qname : 0;
2139 g_type_from_name (const gchar *name)
2144 g_return_val_if_fail (name != NULL, 0);
2146 quark = g_quark_try_string (name);
2149 G_READ_LOCK (&type_rw_lock);
2150 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2151 G_READ_UNLOCK (&type_rw_lock);
2158 g_type_parent (GType type)
2162 node = lookup_type_node_I (type);
2164 return node ? NODE_PARENT_TYPE (node) : 0;
2168 g_type_depth (GType type)
2172 node = lookup_type_node_I (type);
2174 return node ? node->n_supers + 1 : 0;
2178 g_type_next_base (GType type,
2184 node = lookup_type_node_I (type);
2187 TypeNode *base_node = lookup_type_node_I (base_type);
2189 if (base_node && base_node->n_supers < node->n_supers)
2191 guint n = node->n_supers - base_node->n_supers;
2193 if (node->supers[n] == base_type)
2194 atype = node->supers[n - 1];
2201 static inline gboolean
2202 type_node_check_conformities_UorL (TypeNode *node,
2203 TypeNode *iface_node,
2204 /* support_inheritance */
2205 gboolean support_interfaces,
2206 gboolean support_prerequisites,
2211 if (/* support_inheritance && */
2212 iface_node->n_supers <= node->n_supers &&
2213 node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2216 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2217 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2219 if (support_interfaces || support_prerequisites)
2222 G_READ_LOCK (&type_rw_lock);
2223 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2225 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2228 G_READ_UNLOCK (&type_rw_lock);
2234 type_node_is_a_L (TypeNode *node,
2235 TypeNode *iface_node)
2237 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2240 static inline gboolean
2241 type_node_conforms_to_U (TypeNode *node,
2242 TypeNode *iface_node,
2243 gboolean support_interfaces,
2244 gboolean support_prerequisites)
2246 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2250 g_type_is_a (GType type,
2253 TypeNode *node, *iface_node;
2256 node = lookup_type_node_I (type);
2257 iface_node = lookup_type_node_I (iface_type);
2258 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2263 GType* /* free result */
2264 g_type_children (GType type,
2269 node = lookup_type_node_I (type);
2274 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2275 children = g_new (GType, node->n_children + 1);
2276 memcpy (children, node->children, sizeof (GType) * node->n_children);
2277 children[node->n_children] = 0;
2280 *n_children = node->n_children;
2281 G_READ_UNLOCK (&type_rw_lock);
2294 GType* /* free result */
2295 g_type_interfaces (GType type,
2296 guint *n_interfaces)
2300 node = lookup_type_node_I (type);
2301 if (node && node->is_instantiatable)
2306 G_READ_LOCK (&type_rw_lock);
2307 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2308 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2309 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2313 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2314 G_READ_UNLOCK (&type_rw_lock);
2327 typedef struct _QData QData;
2339 static inline gpointer
2340 type_get_qdata_L (TypeNode *node,
2343 GData *gdata = node->global_gdata;
2345 if (quark && gdata && gdata->n_qdatas)
2347 QData *qdatas = gdata->qdatas - 1;
2348 guint n_qdatas = gdata->n_qdatas;
2355 i = (n_qdatas + 1) / 2;
2357 if (quark == check->quark)
2359 else if (quark > check->quark)
2364 else /* if (quark < check->quark) */
2373 g_type_get_qdata (GType type,
2379 node = lookup_type_node_I (type);
2382 G_READ_LOCK (&type_rw_lock);
2383 data = type_get_qdata_L (node, quark);
2384 G_READ_UNLOCK (&type_rw_lock);
2388 g_return_val_if_fail (node != NULL, NULL);
2395 type_set_qdata_W (TypeNode *node,
2403 /* setup qdata list if necessary */
2404 if (!node->global_gdata)
2405 node->global_gdata = g_new0 (GData, 1);
2406 gdata = node->global_gdata;
2408 /* try resetting old data */
2409 qdata = gdata->qdatas;
2410 for (i = 0; i < gdata->n_qdatas; i++)
2411 if (qdata[i].quark == quark)
2413 qdata[i].data = data;
2419 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2420 qdata = gdata->qdatas;
2421 for (i = 0; i < gdata->n_qdatas - 1; i++)
2422 if (qdata[i].quark > quark)
2424 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2425 qdata[i].quark = quark;
2426 qdata[i].data = data;
2430 g_type_set_qdata (GType type,
2436 g_return_if_fail (quark != 0);
2438 node = lookup_type_node_I (type);
2441 G_WRITE_LOCK (&type_rw_lock);
2442 type_set_qdata_W (node, quark, data);
2443 G_WRITE_UNLOCK (&type_rw_lock);
2446 g_return_if_fail (node != NULL);
2450 type_add_flags_W (TypeNode *node,
2455 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2456 g_return_if_fail (node != NULL);
2458 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2459 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2460 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2462 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2466 g_type_query (GType type,
2471 g_return_if_fail (query != NULL);
2473 /* if node is not static and classed, we won't allow query */
2475 node = lookup_type_node_I (type);
2476 if (node && node->is_classed && !node->plugin)
2478 /* type is classed and probably even instantiatable */
2479 G_READ_LOCK (&type_rw_lock);
2480 if (node->data) /* type is static or referenced */
2482 query->type = NODE_TYPE (node);
2483 query->type_name = NODE_NAME (node);
2484 query->class_size = node->data->class.class_size;
2485 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2487 G_READ_UNLOCK (&type_rw_lock);
2492 /* --- implementation details --- */
2494 g_type_test_flags (GType type,
2498 gboolean result = FALSE;
2500 node = lookup_type_node_I (type);
2503 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2504 guint tflags = flags & TYPE_FLAG_MASK;
2508 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2510 fflags = (finfo->type_flags & fflags) == fflags;
2517 G_READ_LOCK (&type_rw_lock);
2518 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2519 G_READ_UNLOCK (&type_rw_lock);
2524 result = tflags && fflags;
2531 g_type_get_plugin (GType type)
2535 node = lookup_type_node_I (type);
2537 return node ? node->plugin : NULL;
2541 g_type_interface_get_plugin (GType instance_type,
2542 GType interface_type)
2547 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
2549 node = lookup_type_node_I (instance_type);
2550 iface = lookup_type_node_I (interface_type);
2553 IFaceHolder *iholder;
2554 GTypePlugin *plugin;
2556 G_READ_LOCK (&type_rw_lock);
2558 iholder = iface_node_get_holders_L (iface);
2559 while (iholder && iholder->instance_type != instance_type)
2560 iholder = iholder->next;
2561 plugin = iholder ? iholder->plugin : NULL;
2563 G_READ_UNLOCK (&type_rw_lock);
2568 g_return_val_if_fail (node == NULL, NULL);
2569 g_return_val_if_fail (iface == NULL, NULL);
2571 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2577 g_type_fundamental_next (void)
2581 G_READ_LOCK (&type_rw_lock);
2582 type = static_fundamental_next;
2583 G_READ_UNLOCK (&type_rw_lock);
2585 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2589 g_type_fundamental (GType type_id)
2591 TypeNode *node = lookup_type_node_I (type_id);
2593 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2597 g_type_check_instance_is_a (GTypeInstance *type_instance,
2600 TypeNode *node, *iface;
2603 if (!type_instance || !type_instance->g_class)
2606 node = lookup_type_node_I (type_instance->g_class->g_type);
2607 iface = lookup_type_node_I (iface_type);
2608 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2614 g_type_check_class_is_a (GTypeClass *type_class,
2617 TypeNode *node, *iface;
2623 node = lookup_type_node_I (type_class->g_type);
2624 iface = lookup_type_node_I (is_a_type);
2625 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2631 g_type_check_instance_cast (GTypeInstance *type_instance,
2636 if (type_instance->g_class)
2638 TypeNode *node, *iface;
2639 gboolean is_instantiatable, check;
2641 node = lookup_type_node_I (type_instance->g_class->g_type);
2642 is_instantiatable = node && node->is_instantiatable;
2643 iface = lookup_type_node_I (iface_type);
2644 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2646 return type_instance;
2648 if (is_instantiatable)
2649 g_warning ("invalid cast from `%s' to `%s'",
2650 type_descriptive_name_I (type_instance->g_class->g_type),
2651 type_descriptive_name_I (iface_type));
2653 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2654 type_descriptive_name_I (type_instance->g_class->g_type),
2655 type_descriptive_name_I (iface_type));
2658 g_warning ("invalid unclassed pointer in cast to `%s'",
2659 type_descriptive_name_I (iface_type));
2662 g_warning ("invalid cast from (NULL) pointer to `%s'",
2663 type_descriptive_name_I (iface_type));
2665 return type_instance;
2669 g_type_check_class_cast (GTypeClass *type_class,
2674 TypeNode *node, *iface;
2675 gboolean is_classed, check;
2677 node = lookup_type_node_I (type_class->g_type);
2678 is_classed = node && node->is_classed;
2679 iface = lookup_type_node_I (is_a_type);
2680 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2685 g_warning ("invalid class cast from `%s' to `%s'",
2686 type_descriptive_name_I (type_class->g_type),
2687 type_descriptive_name_I (is_a_type));
2689 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2690 type_descriptive_name_I (type_class->g_type),
2691 type_descriptive_name_I (is_a_type));
2694 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2695 type_descriptive_name_I (is_a_type));
2700 g_type_check_instance (GTypeInstance *type_instance)
2702 /* this function is just here to make the signal system
2703 * conveniently elaborated on instance checks
2707 if (type_instance->g_class)
2709 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2711 if (node && node->is_instantiatable)
2714 g_warning ("instance of invalid non-instantiatable type `%s'",
2715 type_descriptive_name_I (type_instance->g_class->g_type));
2718 g_warning ("instance with invalid (NULL) class pointer");
2721 g_warning ("invalid (NULL) pointer instance");
2726 static inline gboolean
2727 type_check_is_value_type_U (GType type)
2729 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2732 /* common path speed up */
2733 node = lookup_type_node_I (type);
2734 if (node && node->mutatable_check_cache)
2737 G_READ_LOCK (&type_rw_lock);
2741 if (node->data && node->data->common.ref_count > 0 &&
2742 node->data->common.value_table->value_init)
2743 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2744 else if (NODE_IS_IFACE (node))
2748 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2750 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2751 TypeNode *prnode = lookup_type_node_I (prtype);
2753 if (prnode->is_instantiatable)
2756 node = lookup_type_node_I (type);
2762 G_READ_UNLOCK (&type_rw_lock);
2764 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2768 g_type_check_is_value_type (GType type)
2770 return type_check_is_value_type_U (type);
2774 g_type_check_value (GValue *value)
2776 return value && type_check_is_value_type_U (value->g_type);
2780 g_type_check_value_holds (GValue *value,
2783 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2787 g_type_value_table_peek (GType type)
2789 GTypeValueTable *vtable = NULL;
2790 TypeNode *node = lookup_type_node_I (type);
2791 gboolean has_refed_data, has_table;
2794 /* speed up common code path, we're not 100% safe here,
2795 * but we should only get called with referenced types anyway
2797 data = node ? node->data : NULL;
2798 if (node && node->mutatable_check_cache)
2799 return data->common.value_table;
2801 G_READ_LOCK (&type_rw_lock);
2804 has_refed_data = node && node->data && node->data->common.ref_count;
2805 has_table = has_refed_data && node->data->common.value_table->value_init;
2809 vtable = node->data->common.value_table;
2810 else if (NODE_IS_IFACE (node))
2814 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2816 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2817 TypeNode *prnode = lookup_type_node_I (prtype);
2819 if (prnode->is_instantiatable)
2822 node = lookup_type_node_I (type);
2823 goto restart_table_peek;
2829 G_READ_UNLOCK (&type_rw_lock);
2835 g_warning (G_STRLOC ": type id `%lu' is invalid", type);
2836 if (!has_refed_data)
2837 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2838 type_descriptive_name_I (type));
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_c_init (void); /* sync with gvalue.c */
2864 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2865 extern void g_enum_types_init (void); /* sync with genums.c */
2866 extern void g_param_type_init (void); /* sync with gparam.c */
2867 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2868 extern void g_object_type_init (void); /* sync with gobject.c */
2869 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2870 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2871 extern void g_signal_init (void); /* sync with gsignal.c */
2874 /* --- initialization --- */
2876 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2878 G_LOCK_DEFINE_STATIC (type_init_lock);
2879 const gchar *env_string;
2884 G_LOCK (type_init_lock);
2886 G_WRITE_LOCK (&type_rw_lock);
2888 if (static_quark_type_flags)
2890 G_WRITE_UNLOCK (&type_rw_lock);
2891 G_UNLOCK (type_init_lock);
2895 /* setup GObject library wide debugging flags */
2896 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2897 env_string = g_getenv ("GOBJECT_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_fundamental_type_nodes[0] = NULL;
2923 /* void type G_TYPE_NONE
2925 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2926 type = NODE_TYPE (node);
2927 g_assert (type == G_TYPE_NONE);
2929 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2931 memset (&info, 0, sizeof (info));
2932 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2933 type = NODE_TYPE (node);
2934 type_data_make_W (node, &info, NULL);
2935 g_assert (type == G_TYPE_INTERFACE);
2937 G_WRITE_UNLOCK (&type_rw_lock);
2941 /* G_TYPE_TYPE_PLUGIN
2943 g_type_plugin_get_type ();
2945 /* G_TYPE_* value types
2947 g_value_types_init ();
2949 /* G_TYPE_ENUM & G_TYPE_FLAGS
2951 g_enum_types_init ();
2955 g_boxed_type_init ();
2959 g_param_type_init ();
2963 g_object_type_init ();
2965 /* G_TYPE_PARAM_* pspec types
2967 g_param_spec_types_init ();
2969 /* Value Transformations
2971 g_value_transforms_init ();
2977 G_UNLOCK (type_init_lock);
2983 g_type_init_with_debug_flags (0);