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);
142 /* --- structures --- */
146 guint n_children : 12;
148 guint _prot_n_ifaces_prerequisites : 9;
149 guint is_classed : 1;
150 guint is_instantiatable : 1;
151 guint mutatable_check_cache : 1; /* combines some common path checks */
153 TypeData * volatile data;
157 IFaceEntry *iface_entries; /* for !iface types */
158 GType *prerequisistes;
160 GType supers[1]; /* flexible array */
162 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
163 #define MAX_N_SUPERS (255)
164 #define MAX_N_CHILDREN (4095)
165 #define MAX_N_IFACES (511)
166 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
167 #define NODE_TYPE(node) (node->supers[0])
168 #define NODE_PARENT_TYPE(node) (node->supers[1])
169 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
170 #define NODE_NAME(node) (g_quark_to_string (node->qname))
171 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
172 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
173 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
174 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
175 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
176 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
177 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
178 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
179 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
180 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
186 GInterfaceInfo *info;
193 GTypeInterface *vtable;
198 GTypeValueTable *value_table;
204 GBaseInitFunc vtable_init_base;
205 GBaseFinalizeFunc vtable_finalize_base;
211 GBaseInitFunc class_init_base;
212 GBaseFinalizeFunc class_finalize_base;
213 GClassInitFunc class_init;
214 GClassFinalizeFunc class_finalize;
215 gconstpointer class_data;
222 GBaseInitFunc class_init_base;
223 GBaseFinalizeFunc class_finalize_base;
224 GClassInitFunc class_init;
225 GClassFinalizeFunc class_finalize;
226 gconstpointer class_data;
228 guint16 instance_size;
230 GInstanceInitFunc instance_init;
231 GMemChunk *mem_chunk;
238 InstanceData instance;
242 GTypeClassCacheFunc cache_func;
246 /* --- variables --- */
247 static guint static_n_class_cache_funcs = 0;
248 static ClassCacheFunc *static_class_cache_funcs = NULL;
249 static GQuark static_quark_type_flags = 0;
250 static GQuark static_quark_iface_holder = 0;
251 static GQuark static_quark_dependants_array = 0;
252 GTypeDebugFlags _g_type_debug_flags = 0;
255 /* --- type nodes --- */
256 static GHashTable *static_type_nodes_ht = NULL;
257 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { 0, };
258 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
260 static inline TypeNode*
261 lookup_type_node_I (register GType utype)
263 if (utype > G_TYPE_FUNDAMENTAL_MAX)
264 return (TypeNode*) (utype & ~TYPE_ID_MASK);
266 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
270 type_node_any_new_W (TypeNode *pnode,
274 GTypeFundamentalFlags type_flags)
279 guint i, node_size = 0;
281 n_supers = pnode ? pnode->n_supers + 1 : 0;
284 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
285 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
286 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
287 node = g_malloc0 (node_size);
288 if (!pnode) /* offset fundamental types */
290 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
291 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
297 g_assert ((type & TYPE_ID_MASK) == 0);
299 node->n_supers = n_supers;
302 node->supers[0] = type;
305 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
306 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
308 if (NODE_IS_IFACE (node))
310 IFACE_NODE_N_PREREQUISITES (node) = 0;
311 IFACE_NODE_PREREQUISITES (node) = NULL;
315 CLASSED_NODE_N_IFACES (node) = 0;
316 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
321 node->supers[0] = type;
322 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
324 node->is_classed = pnode->is_classed;
325 node->is_instantiatable = pnode->is_instantiatable;
327 if (NODE_IS_IFACE (node))
329 IFACE_NODE_N_PREREQUISITES (node) = 0;
330 IFACE_NODE_PREREQUISITES (node) = NULL;
336 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
337 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
338 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
339 CLASSED_NODE_N_IFACES (node));
340 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
341 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
344 i = pnode->n_children++;
345 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
346 pnode->children[i] = type;
349 node->plugin = plugin;
350 node->n_children = 0;
351 node->children = NULL;
353 node->qname = g_quark_from_string (name);
354 node->global_gdata = NULL;
356 g_hash_table_insert (static_type_nodes_ht,
357 GUINT_TO_POINTER (node->qname),
358 GUINT_TO_POINTER (type));
362 static inline GTypeFundamentalInfo*
363 type_node_fundamental_info_I (TypeNode *node)
365 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
367 if (ftype != NODE_TYPE (node))
368 node = lookup_type_node_I (ftype);
370 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
374 type_node_fundamental_new_W (GType ftype,
376 GTypeFundamentalFlags type_flags)
378 GTypeFundamentalInfo *finfo;
381 g_assert ((ftype & TYPE_ID_MASK) == 0);
382 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
384 if (ftype == static_fundamental_next)
385 static_fundamental_next += 1 << G_TYPE_FUNDAMENTAL_SHIFT;
387 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
389 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
391 finfo = type_node_fundamental_info_I (node);
392 finfo->type_flags = type_flags;
398 type_node_new_W (TypeNode *pnode,
404 g_assert (pnode->n_supers < MAX_N_SUPERS);
405 g_assert (pnode->n_children < MAX_N_CHILDREN);
407 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
410 static inline IFaceEntry*
411 type_lookup_iface_entry_L (TypeNode *node,
412 TypeNode *iface_node)
414 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
416 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
417 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
418 GType iface_type = NODE_TYPE (iface_node);
425 i = (n_ifaces + 1) >> 1;
427 if (iface_type == check->iface_type)
429 else if (iface_type > check->iface_type)
434 else /* if (iface_type < check->iface_type) */
443 static inline gboolean
444 type_lookup_prerequisite_L (TypeNode *iface,
445 GType prerequisite_type)
447 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
449 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
450 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
457 i = (n_prerequisites + 1) >> 1;
458 check = prerequisites + i;
459 if (prerequisite_type == *check)
461 else if (prerequisite_type > *check)
463 n_prerequisites -= i;
464 prerequisites = check;
466 else /* if (prerequisite_type < *check) */
467 n_prerequisites = i - 1;
469 while (n_prerequisites);
475 type_descriptive_name_I (GType type)
479 TypeNode *node = lookup_type_node_I (type);
481 return node ? NODE_NAME (node) : "<unknown>";
488 /* --- type consistency checks --- */
490 check_plugin_U (GTypePlugin *plugin,
491 gboolean need_complete_type_info,
492 gboolean need_complete_interface_info,
493 const gchar *type_name)
495 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
499 g_warning ("plugin handle for type `%s' is NULL",
503 if (!G_IS_TYPE_PLUGIN (plugin))
505 g_warning ("plugin pointer (%p) for type `%s' is invalid",
509 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
511 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
515 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
517 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
525 check_type_name_I (const gchar *type_name)
527 static const gchar *extra_chars = "-_+";
528 const gchar *p = type_name;
531 if (!type_name[0] || !type_name[1] || !type_name[2])
533 g_warning ("type name `%s' is too short", type_name);
536 /* check the first letter */
537 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
538 for (p = type_name + 1; *p; p++)
539 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
540 (p[0] >= 'a' && p[0] <= 'z') ||
541 (p[0] >= '0' && p[0] <= '9') ||
542 strchr (extra_chars, p[0]));
545 g_warning ("type name `%s' contains invalid characters", type_name);
548 if (g_type_from_name (type_name))
550 g_warning ("cannot register existing type `%s'", type_name);
558 check_derivation_I (GType parent_type,
559 const gchar *type_name)
562 GTypeFundamentalInfo* finfo;
564 pnode = lookup_type_node_I (parent_type);
567 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
569 type_descriptive_name_I (parent_type));
572 finfo = type_node_fundamental_info_I (pnode);
573 /* ensure flat derivability */
574 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
576 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
581 /* ensure deep derivability */
582 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
583 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
585 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
595 check_collect_format_I (const gchar *collect_format)
597 const gchar *p = collect_format;
598 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
599 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
600 G_VALUE_COLLECT_POINTER, 0 };
603 if (!strchr (valid_format, *p++))
605 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
609 check_value_table_I (const gchar *type_name,
610 const GTypeValueTable *value_table)
614 else if (value_table->value_init == NULL)
616 if (value_table->value_free || value_table->value_copy ||
617 value_table->value_peek_pointer ||
618 value_table->collect_format || value_table->collect_value ||
619 value_table->lcopy_format || value_table->lcopy_value)
620 g_warning ("cannot handle uninitializable values of type `%s'",
624 else /* value_table->value_init != NULL */
626 if (!value_table->value_free)
629 * g_warning ("missing `value_free()' for type `%s'", type_name);
633 if (!value_table->value_copy)
635 g_warning ("missing `value_copy()' for type `%s'", type_name);
638 if ((value_table->collect_format || value_table->collect_value) &&
639 (!value_table->collect_format || !value_table->collect_value))
641 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
645 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
647 g_warning ("the `%s' specification for type `%s' is too long or invalid",
652 if ((value_table->lcopy_format || value_table->lcopy_value) &&
653 (!value_table->lcopy_format || !value_table->lcopy_value))
655 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
659 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
661 g_warning ("the `%s' specification for type `%s' is too long or invalid",
671 check_type_info_I (TypeNode *pnode,
673 const gchar *type_name,
674 const GTypeInfo *info)
676 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
677 gboolean is_interface = ftype == G_TYPE_INTERFACE;
679 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
681 /* check instance members */
682 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
683 (info->instance_size || info->n_preallocs || info->instance_init))
686 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
690 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
694 /* check class & interface members */
695 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
696 (info->class_init || info->class_finalize || info->class_data ||
697 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
700 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
704 g_warning ("cannot create class for `%s' as non-classed fundamental",
708 /* check interface size */
709 if (is_interface && info->class_size < sizeof (GTypeInterface))
711 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
715 /* check class size */
716 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
718 if (info->class_size < sizeof (GTypeClass))
720 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
724 if (pnode && info->class_size < pnode->data->class.class_size)
726 g_warning ("specified class size for type `%s' is smaller "
727 "than the parent type's `%s' class size",
733 /* check instance size */
734 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
736 if (info->instance_size < sizeof (GTypeInstance))
738 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
742 if (pnode && info->instance_size < pnode->data->instance.instance_size)
744 g_warning ("specified instance size for type `%s' is smaller "
745 "than the parent type's `%s' instance size",
756 find_conforming_child_type_L (TypeNode *pnode,
759 TypeNode *node = NULL;
762 if (type_lookup_iface_entry_L (pnode, iface))
765 for (i = 0; i < pnode->n_children && !node; i++)
766 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
772 check_add_interface_L (GType instance_type,
775 TypeNode *node = lookup_type_node_I (instance_type);
776 TypeNode *iface = lookup_type_node_I (iface_type);
780 if (!node || !node->is_instantiatable)
782 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
783 type_descriptive_name_I (instance_type));
786 if (!iface || !NODE_IS_IFACE (iface))
788 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
789 type_descriptive_name_I (iface_type),
793 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
794 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
796 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
797 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
803 /* allow overriding of interface type introduced for parent type */
804 entry = type_lookup_iface_entry_L (node, iface);
805 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
807 /* ok, we do conform to this interface already, but the interface vtable was not
808 * yet intialized, and we just conform to the interface because it got added to
809 * one of our parents. so we allow overriding of holder info here.
813 /* check whether one of our children already conforms (or whether the interface
814 * got added to this node already)
816 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
819 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
829 check_interface_info_I (TypeNode *iface,
831 const GInterfaceInfo *info)
833 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
835 g_warning ("interface type `%s' for type `%s' comes without initializer",
837 type_descriptive_name_I (instance_type));
844 /* --- type info (type node data) --- */
846 type_data_make_W (TypeNode *node,
847 const GTypeInfo *info,
848 const GTypeValueTable *value_table)
851 GTypeValueTable *vtable = NULL;
852 guint vtable_size = 0;
854 g_assert (node->data == NULL && info != NULL);
858 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
861 vtable = pnode->data->common.value_table;
864 static const GTypeValueTable zero_vtable = { NULL, };
866 value_table = &zero_vtable;
871 /* need to setup vtable_size since we have to allocate it with data in one chunk */
872 vtable_size = sizeof (GTypeValueTable);
873 if (value_table->collect_format)
874 vtable_size += strlen (value_table->collect_format);
875 if (value_table->lcopy_format)
876 vtable_size += strlen (value_table->lcopy_format);
880 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
882 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
884 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
885 data->instance.class_size = info->class_size;
886 data->instance.class_init_base = info->base_init;
887 data->instance.class_finalize_base = info->base_finalize;
888 data->instance.class_init = info->class_init;
889 data->instance.class_finalize = info->class_finalize;
890 data->instance.class_data = info->class_data;
891 data->instance.class = NULL;
892 data->instance.instance_size = info->instance_size;
893 #ifdef DISABLE_MEM_POOLS
894 data->instance.n_preallocs = 0;
895 #else /* !DISABLE_MEM_POOLS */
896 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
897 #endif /* !DISABLE_MEM_POOLS */
898 data->instance.instance_init = info->instance_init;
899 data->instance.mem_chunk = NULL;
901 else if (node->is_classed) /* only classed */
903 data = g_malloc0 (sizeof (ClassData) + vtable_size);
905 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
906 data->class.class_size = info->class_size;
907 data->class.class_init_base = info->base_init;
908 data->class.class_finalize_base = info->base_finalize;
909 data->class.class_init = info->class_init;
910 data->class.class_finalize = info->class_finalize;
911 data->class.class_data = info->class_data;
912 data->class.class = NULL;
914 else if (NODE_IS_IFACE (node))
916 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
918 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
919 data->iface.vtable_size = info->class_size;
920 data->iface.vtable_init_base = info->base_init;
921 data->iface.vtable_finalize_base = info->base_finalize;
925 data = g_malloc0 (sizeof (CommonData) + vtable_size);
927 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
931 node->data->common.ref_count = 1;
937 /* we allocate the vtable and its strings together with the type data, so
938 * children can take over their parent's vtable pointer, and we don't
939 * need to worry freeing it or not when the child data is destroyed
941 *vtable = *value_table;
942 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
944 vtable->collect_format = p;
945 if (value_table->collect_format)
947 strcat (p, value_table->collect_format);
948 p += strlen (value_table->collect_format);
952 vtable->lcopy_format = p;
953 if (value_table->lcopy_format)
954 strcat (p, value_table->lcopy_format);
956 node->data->common.value_table = vtable;
957 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
958 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
959 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
961 g_assert (node->data->common.value_table != NULL); /* paranoid */
965 type_data_ref_Wm (TypeNode *node)
969 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
971 GTypeValueTable tmp_value_table;
973 g_assert (node->plugin != NULL);
977 type_data_ref_Wm (pnode);
979 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
982 memset (&tmp_info, 0, sizeof (tmp_info));
983 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
985 G_WRITE_UNLOCK (&type_rw_lock);
986 g_type_plugin_use (node->plugin);
987 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
988 G_WRITE_LOCK (&type_rw_lock);
990 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
992 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
993 type_data_make_W (node, &tmp_info,
994 check_value_table_I (NODE_NAME (node),
995 &tmp_value_table) ? &tmp_value_table : NULL);
999 g_assert (node->data->common.ref_count > 0);
1001 node->data->common.ref_count += 1;
1006 type_data_unref_Wm (TypeNode *node,
1009 g_assert (node->data && node->data->common.ref_count);
1011 if (node->data->common.ref_count > 1)
1012 node->data->common.ref_count -= 1;
1017 g_warning ("static type `%s' unreferenced too often",
1022 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1027 type_node_add_iface_entry_W (TypeNode *node,
1030 IFaceEntry *entries;
1033 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1035 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1036 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1037 if (entries[i].iface_type == iface_type)
1039 /* this can (should) only happen if our parent type already conformed
1040 * to iface_type and node got it's own holder info. here, our
1041 * children should already have entries with NULL vtables, so
1042 * we're actually done.
1044 g_assert (entries[i].vtable == NULL);
1047 else if (entries[i].iface_type > iface_type)
1049 CLASSED_NODE_N_IFACES (node) += 1;
1050 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1051 CLASSED_NODE_IFACES_ENTRIES (node),
1052 CLASSED_NODE_N_IFACES (node));
1053 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1054 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1055 entries[i].iface_type = iface_type;
1056 entries[i].vtable = NULL;
1058 for (i = 0; i < node->n_children; i++)
1059 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type);
1063 type_add_interface_W (TypeNode *node,
1065 const GInterfaceInfo *info,
1066 GTypePlugin *plugin)
1068 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1070 /* we must not call any functions of GInterfaceInfo from within here, since
1071 * we got most probably called from _within_ a type registration function
1073 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1075 iholder->next = iface_node_get_holders_L (iface);
1076 iface_node_set_holders_W (iface, iholder);
1077 iholder->instance_type = NODE_TYPE (node);
1078 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1079 iholder->plugin = plugin;
1081 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1085 type_iface_add_prerequisite_W (TypeNode *iface,
1086 TypeNode *prerequisite_node)
1088 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1089 GType *prerequisites, *dependants;
1090 guint n_dependants, i;
1092 g_assert (NODE_IS_IFACE (iface) &&
1093 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1094 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1096 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1097 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1098 if (prerequisites[i] == prerequisite_type)
1099 return; /* we already have that prerequisiste */
1100 else if (prerequisites[i] > prerequisite_type)
1102 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1103 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1104 IFACE_NODE_PREREQUISITES (iface),
1105 IFACE_NODE_N_PREREQUISITES (iface));
1106 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1107 g_memmove (prerequisites + i + 1, prerequisites + i,
1108 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1109 prerequisites[i] = prerequisite_type;
1111 /* we want to get notified when prerequisites get added to prerequisite_node */
1112 if (NODE_IS_IFACE (prerequisite_node))
1114 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1115 n_dependants = dependants ? dependants[0] : 0;
1117 dependants = g_renew (GType, dependants, n_dependants + 1);
1118 dependants[n_dependants] = NODE_TYPE (iface);
1119 dependants[0] = n_dependants;
1120 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1123 /* we need to notify all dependants */
1124 dependants = iface_node_get_dependants_array_L (iface);
1125 n_dependants = dependants ? dependants[0] : 0;
1126 for (i = 1; i <= n_dependants; i++)
1127 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1131 g_type_interface_add_prerequisite (GType interface_type,
1132 GType prerequisite_type)
1134 TypeNode *iface, *prerequisite_node;
1135 IFaceHolder *holders;
1137 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1138 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1139 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1141 iface = lookup_type_node_I (interface_type);
1142 prerequisite_node = lookup_type_node_I (prerequisite_type);
1143 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1145 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1146 type_descriptive_name_I (interface_type),
1147 type_descriptive_name_I (prerequisite_type));
1150 G_WRITE_LOCK (&type_rw_lock);
1151 holders = iface_node_get_holders_L (iface);
1154 G_WRITE_UNLOCK (&type_rw_lock);
1155 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1156 type_descriptive_name_I (prerequisite_type),
1157 type_descriptive_name_I (interface_type),
1158 type_descriptive_name_I (holders->instance_type));
1161 if (prerequisite_node->is_instantiatable)
1165 /* can have at most one publically installable instantiatable prerequisite */
1166 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1168 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1170 if (prnode->is_instantiatable)
1172 G_WRITE_UNLOCK (&type_rw_lock);
1173 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1174 type_descriptive_name_I (prerequisite_type),
1175 type_descriptive_name_I (interface_type),
1176 type_descriptive_name_I (NODE_TYPE (prnode)));
1181 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1182 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1183 G_WRITE_UNLOCK (&type_rw_lock);
1185 else if (NODE_IS_IFACE (prerequisite_node))
1188 guint n_dependants, i;
1190 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1191 n_dependants = dependants ? dependants[0] : 0;
1192 for (i = 1; i <= n_dependants; i++)
1193 type_iface_add_prerequisite_W (iface, lookup_type_node_I (dependants[i]));
1194 type_iface_add_prerequisite_W (iface, prerequisite_node);
1195 G_WRITE_UNLOCK (&type_rw_lock);
1199 G_WRITE_UNLOCK (&type_rw_lock);
1200 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1201 type_descriptive_name_I (prerequisite_type),
1202 type_descriptive_name_I (interface_type));
1207 type_iface_peek_holder_L (TypeNode *iface,
1208 GType instance_type)
1210 IFaceHolder *iholder;
1212 g_assert (NODE_IS_IFACE (iface));
1214 iholder = iface_node_get_holders_L (iface);
1215 while (iholder && iholder->instance_type != instance_type)
1216 iholder = iholder->next;
1221 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1222 GType instance_type,
1225 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1227 if (iholder && !iholder->info && need_info)
1229 GInterfaceInfo tmp_info;
1231 g_assert (iholder->plugin != NULL);
1233 type_data_ref_Wm (iface);
1235 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1237 memset (&tmp_info, 0, sizeof (tmp_info));
1239 G_WRITE_UNLOCK (&type_rw_lock);
1240 g_type_plugin_use (iholder->plugin);
1241 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1242 G_WRITE_LOCK (&type_rw_lock);
1244 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1246 check_interface_info_I (iface, instance_type, &tmp_info);
1247 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1250 return iholder; /* we don't modify write lock upon returning NULL */
1254 type_iface_blow_holder_info_Wm (TypeNode *iface,
1255 GType instance_type)
1257 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1259 g_assert (NODE_IS_IFACE (iface));
1261 while (iholder->instance_type != instance_type)
1262 iholder = iholder->next;
1264 if (iholder->info && iholder->plugin)
1266 g_free (iholder->info);
1267 iholder->info = NULL;
1269 G_WRITE_UNLOCK (&type_rw_lock);
1270 g_type_plugin_unuse (iholder->plugin);
1271 G_WRITE_LOCK (&type_rw_lock);
1273 type_data_unref_Wm (iface, FALSE);
1278 /* --- type structure creation/destruction --- */
1280 g_type_create_instance (GType type)
1283 GTypeInstance *instance;
1287 node = lookup_type_node_I (type);
1288 if (!node || !node->is_instantiatable)
1290 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1291 type_descriptive_name_I (type));
1294 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1295 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1297 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1298 type_descriptive_name_I (type));
1302 class = g_type_class_ref (type);
1304 if (node->data->instance.n_preallocs)
1306 G_WRITE_LOCK (&type_rw_lock);
1307 if (!node->data->instance.mem_chunk)
1308 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1309 node->data->instance.instance_size,
1310 (node->data->instance.instance_size *
1311 node->data->instance.n_preallocs),
1313 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1314 G_WRITE_UNLOCK (&type_rw_lock);
1317 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1318 for (i = node->n_supers; i > 0; i--)
1322 pnode = lookup_type_node_I (node->supers[i]);
1323 if (pnode->data->instance.instance_init)
1325 instance->g_class = pnode->data->instance.class;
1326 pnode->data->instance.instance_init (instance, class);
1329 instance->g_class = class;
1331 if (node->data->instance.instance_init)
1332 node->data->instance.instance_init (instance, class);
1338 g_type_free_instance (GTypeInstance *instance)
1343 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1345 class = instance->g_class;
1346 node = lookup_type_node_I (class->g_type);
1347 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1349 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1350 type_descriptive_name_I (class->g_type));
1353 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1354 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1356 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1361 instance->g_class = NULL;
1362 memset (instance, 0xaa, node->data->instance.instance_size); /* FIXME: debugging hack */
1363 if (node->data->instance.n_preallocs)
1365 G_WRITE_LOCK (&type_rw_lock);
1366 g_chunk_free (instance, node->data->instance.mem_chunk);
1367 G_WRITE_UNLOCK (&type_rw_lock);
1372 g_type_class_unref (class);
1376 type_iface_vtable_init_Wm (TypeNode *iface,
1379 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1380 IFaceHolder *iholder;
1381 GTypeInterface *vtable = NULL;
1384 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1385 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1387 return FALSE; /* we don't modify write lock upon FALSE */
1389 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1391 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1392 if (pnode) /* want to copy over parent iface contents */
1394 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1397 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1400 vtable = g_malloc0 (iface->data->iface.vtable_size);
1401 entry->vtable = vtable;
1402 vtable->g_type = NODE_TYPE (iface);
1403 vtable->g_instance_type = NODE_TYPE (node);
1405 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1407 G_WRITE_UNLOCK (&type_rw_lock);
1408 if (iface->data->iface.vtable_init_base)
1409 iface->data->iface.vtable_init_base (vtable);
1410 if (iholder->info->interface_init)
1411 iholder->info->interface_init (vtable, iholder->info->interface_data);
1412 G_WRITE_LOCK (&type_rw_lock);
1414 return TRUE; /* write lock modified */
1418 type_iface_vtable_finalize_Wm (TypeNode *iface,
1420 GTypeInterface *vtable)
1422 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1423 IFaceHolder *iholder;
1425 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1426 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1428 return FALSE; /* we don't modify write lock upon FALSE */
1430 g_assert (entry && entry->vtable == vtable && iholder->info);
1432 entry->vtable = NULL;
1433 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1435 G_WRITE_UNLOCK (&type_rw_lock);
1436 if (iholder->info->interface_finalize)
1437 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1438 if (iface->data->iface.vtable_finalize_base)
1439 iface->data->iface.vtable_finalize_base (vtable);
1440 G_WRITE_LOCK (&type_rw_lock);
1443 vtable->g_instance_type = 0;
1446 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1448 return TRUE; /* write lock modified */
1452 type_class_init_Wm (TypeNode *node,
1455 GSList *slist, *init_slist = NULL;
1458 TypeNode *bnode, *pnode;
1461 g_assert (node->is_classed && node->data &&
1462 node->data->class.class_size &&
1463 !node->data->class.class);
1465 class = g_malloc0 (node->data->class.class_size);
1466 node->data->class.class = class;
1470 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1472 memcpy (class, pclass, pnode->data->class.class_size);
1474 class->g_type = NODE_TYPE (node);
1476 G_WRITE_UNLOCK (&type_rw_lock);
1478 /* stack all base class initialization functions, so we
1479 * call them in ascending order.
1481 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1482 if (bnode->data->class.class_init_base)
1483 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1484 for (slist = init_slist; slist; slist = slist->next)
1486 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1488 class_init_base (class);
1490 g_slist_free (init_slist);
1492 if (node->data->class.class_init)
1493 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1495 G_WRITE_LOCK (&type_rw_lock);
1497 /* ok, we got the class done, now initialize all interfaces, either
1498 * from parent, or through our holder info
1500 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1501 entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1504 g_assert (entry->vtable == NULL);
1506 if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1510 /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1511 * need to get this interface from parent
1513 g_assert (pnode != NULL);
1515 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1517 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1519 if (pentry->iface_type == entry->iface_type)
1521 entry->vtable = pentry->vtable;
1525 g_assert (entry->vtable != NULL);
1528 /* refetch entry, IFACES_ENTRIES might be modified */
1529 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1530 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1531 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1536 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1541 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1543 g_message ("finalizing interfaces for %sClass `%s'",
1544 type_descriptive_name_I (NODE_FUNDAMENTAL_TYPE (node)),
1545 type_descriptive_name_I (NODE_TYPE (node)));
1547 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1548 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1549 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1550 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1553 if (!type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1555 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1556 * iface vtable came from parent
1558 entry->vtable = NULL;
1561 /* refetch entry, IFACES_ENTRIES might be modified */
1562 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1563 if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1564 CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1565 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1570 type_data_finalize_class_U (TypeNode *node,
1573 GTypeClass *class = cdata->class;
1576 g_assert (cdata->class && cdata->common.ref_count == 0);
1578 if (cdata->class_finalize)
1579 cdata->class_finalize (class, (gpointer) cdata->class_data);
1581 /* call all base class destruction functions in descending order
1583 if (cdata->class_finalize_base)
1584 cdata->class_finalize_base (class);
1585 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1586 if (bnode->data->class.class_finalize_base)
1587 bnode->data->class.class_finalize_base (class);
1590 g_free (cdata->class);
1594 type_data_last_unref_Wm (GType type,
1597 TypeNode *node = lookup_type_node_I (type);
1599 g_return_if_fail (node != NULL && node->plugin != NULL);
1601 if (!node->data || node->data->common.ref_count == 0)
1603 g_warning ("cannot drop last reference to unreferenced type `%s'",
1604 type_descriptive_name_I (type));
1608 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1612 G_WRITE_UNLOCK (&type_rw_lock);
1613 G_READ_LOCK (&type_rw_lock);
1614 for (i = 0; i < static_n_class_cache_funcs; i++)
1616 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1617 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1618 gboolean need_break;
1620 G_READ_UNLOCK (&type_rw_lock);
1621 need_break = cache_func (cache_data, node->data->class.class);
1622 G_READ_LOCK (&type_rw_lock);
1623 if (!node->data || node->data->common.ref_count == 0)
1624 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1628 G_READ_UNLOCK (&type_rw_lock);
1629 G_WRITE_LOCK (&type_rw_lock);
1632 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1633 node->data->common.ref_count -= 1;
1636 GType ptype = NODE_PARENT_TYPE (node);
1639 node->data->common.ref_count = 0;
1641 if (node->is_instantiatable && node->data->instance.mem_chunk)
1643 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1644 node->data->instance.mem_chunk = NULL;
1648 if (node->is_classed && tdata->class.class)
1650 if (CLASSED_NODE_N_IFACES (node))
1651 type_data_finalize_class_ifaces_Wm (node);
1652 node->mutatable_check_cache = FALSE;
1654 G_WRITE_UNLOCK (&type_rw_lock);
1655 type_data_finalize_class_U (node, &tdata->class);
1656 G_WRITE_LOCK (&type_rw_lock);
1660 node->mutatable_check_cache = FALSE;
1664 /* freeing tdata->common.value_table and its contents is taking care of
1665 * by allocating it in one chunk with tdata
1670 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1671 G_WRITE_UNLOCK (&type_rw_lock);
1672 g_type_plugin_unuse (node->plugin);
1673 G_WRITE_LOCK (&type_rw_lock);
1678 g_type_add_class_cache_func (gpointer cache_data,
1679 GTypeClassCacheFunc cache_func)
1683 g_return_if_fail (cache_func != NULL);
1685 G_WRITE_LOCK (&type_rw_lock);
1686 i = static_n_class_cache_funcs++;
1687 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1688 static_class_cache_funcs[i].cache_data = cache_data;
1689 static_class_cache_funcs[i].cache_func = cache_func;
1690 G_WRITE_UNLOCK (&type_rw_lock);
1694 g_type_remove_class_cache_func (gpointer cache_data,
1695 GTypeClassCacheFunc cache_func)
1697 gboolean found_it = FALSE;
1700 g_return_if_fail (cache_func != NULL);
1702 G_WRITE_LOCK (&type_rw_lock);
1703 for (i = 0; i < static_n_class_cache_funcs; i++)
1704 if (static_class_cache_funcs[i].cache_data == cache_data &&
1705 static_class_cache_funcs[i].cache_func == cache_func)
1707 static_n_class_cache_funcs--;
1708 g_memmove (static_class_cache_funcs + i,
1709 static_class_cache_funcs + i + 1,
1710 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1711 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1715 G_WRITE_UNLOCK (&type_rw_lock);
1718 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1719 cache_func, cache_data);
1723 /* --- type registration --- */
1725 g_type_register_fundamental (GType type_id,
1726 const gchar *type_name,
1727 const GTypeInfo *info,
1728 const GTypeFundamentalInfo *finfo,
1731 GTypeFundamentalInfo *node_finfo;
1734 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1735 g_return_val_if_fail (type_id > 0, 0);
1736 g_return_val_if_fail (type_name != NULL, 0);
1737 g_return_val_if_fail (info != NULL, 0);
1738 g_return_val_if_fail (finfo != NULL, 0);
1740 if (!check_type_name_I (type_name))
1742 if ((type_id & TYPE_ID_MASK) ||
1743 type_id > G_TYPE_FUNDAMENTAL_MAX)
1745 g_warning ("attempt to register fundamental type `%s' with invalid type id (%u)",
1750 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1751 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1753 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1757 if (lookup_type_node_I (type_id))
1759 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1760 type_descriptive_name_I (type_id),
1765 G_WRITE_LOCK (&type_rw_lock);
1766 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1767 node_finfo = type_node_fundamental_info_I (node);
1768 type_add_flags_W (node, flags);
1770 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1771 type_data_make_W (node, info,
1772 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1773 G_WRITE_UNLOCK (&type_rw_lock);
1775 return NODE_TYPE (node);
1779 g_type_register_static (GType parent_type,
1780 const gchar *type_name,
1781 const GTypeInfo *info,
1784 TypeNode *pnode, *node;
1787 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1788 g_return_val_if_fail (parent_type > 0, 0);
1789 g_return_val_if_fail (type_name != NULL, 0);
1790 g_return_val_if_fail (info != NULL, 0);
1792 if (!check_type_name_I (type_name) ||
1793 !check_derivation_I (parent_type, type_name))
1795 if (info->class_finalize)
1797 g_warning ("class finalizer specified for static type `%s'",
1802 pnode = lookup_type_node_I (parent_type);
1803 G_WRITE_LOCK (&type_rw_lock);
1804 type_data_ref_Wm (pnode);
1805 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1807 node = type_node_new_W (pnode, type_name, NULL);
1808 type_add_flags_W (node, flags);
1809 type = NODE_TYPE (node);
1810 type_data_make_W (node, info,
1811 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1813 G_WRITE_UNLOCK (&type_rw_lock);
1819 g_type_register_dynamic (GType parent_type,
1820 const gchar *type_name,
1821 GTypePlugin *plugin,
1824 TypeNode *pnode, *node;
1827 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1828 g_return_val_if_fail (parent_type > 0, 0);
1829 g_return_val_if_fail (type_name != NULL, 0);
1830 g_return_val_if_fail (plugin != NULL, 0);
1832 if (!check_type_name_I (type_name) ||
1833 !check_derivation_I (parent_type, type_name) ||
1834 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1837 G_WRITE_LOCK (&type_rw_lock);
1838 pnode = lookup_type_node_I (parent_type);
1839 node = type_node_new_W (pnode, type_name, plugin);
1840 type_add_flags_W (node, flags);
1841 type = NODE_TYPE (node);
1842 G_WRITE_UNLOCK (&type_rw_lock);
1848 g_type_add_interface_static (GType instance_type,
1849 GType interface_type,
1850 const GInterfaceInfo *info)
1852 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1853 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1854 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1856 G_WRITE_LOCK (&type_rw_lock);
1857 if (check_add_interface_L (instance_type, interface_type))
1859 TypeNode *node = lookup_type_node_I (instance_type);
1860 TypeNode *iface = lookup_type_node_I (interface_type);
1862 if (check_interface_info_I (iface, NODE_TYPE (node), info))
1864 type_add_interface_W (node, iface, info, NULL);
1865 /* if we have a class already, the interface vtable needs to
1866 * be initialized as well
1868 if (node->data && node->data->class.class)
1869 type_iface_vtable_init_Wm (iface, node);
1872 G_WRITE_UNLOCK (&type_rw_lock);
1876 g_type_add_interface_dynamic (GType instance_type,
1877 GType interface_type,
1878 GTypePlugin *plugin)
1882 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1883 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1884 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1886 node = lookup_type_node_I (instance_type);
1887 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1890 G_WRITE_LOCK (&type_rw_lock);
1891 if (check_add_interface_L (instance_type, interface_type))
1893 TypeNode *iface = lookup_type_node_I (interface_type);
1895 type_add_interface_W (node, iface, NULL, plugin);
1896 /* if we have a class already, the interface vtable needs to
1897 * be initialized as well
1899 if (node->data && node->data->class.class)
1900 type_iface_vtable_init_Wm (iface, node);
1902 G_WRITE_UNLOCK (&type_rw_lock);
1906 /* --- public API functions --- */
1908 g_type_class_ref (GType type)
1912 /* optimize for common code path
1914 G_WRITE_LOCK (&type_rw_lock);
1915 node = lookup_type_node_I (type);
1916 if (node && node->is_classed && node->data &&
1917 node->data->class.class && node->data->common.ref_count > 0)
1919 type_data_ref_Wm (node);
1920 G_WRITE_UNLOCK (&type_rw_lock);
1922 return node->data->class.class;
1925 if (!node || !node->is_classed ||
1926 (node->data && node->data->common.ref_count < 1))
1928 G_WRITE_UNLOCK (&type_rw_lock);
1929 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
1930 type_descriptive_name_I (type));
1934 type_data_ref_Wm (node);
1936 if (!node->data->class.class)
1938 GType ptype = NODE_PARENT_TYPE (node);
1939 GTypeClass *pclass = NULL;
1943 G_WRITE_UNLOCK (&type_rw_lock);
1944 pclass = g_type_class_ref (ptype);
1945 if (node->data->class.class)
1946 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1947 G_WRITE_LOCK (&type_rw_lock);
1950 type_class_init_Wm (node, pclass);
1952 G_WRITE_UNLOCK (&type_rw_lock);
1954 return node->data->class.class;
1958 g_type_class_unref (gpointer g_class)
1961 GTypeClass *class = g_class;
1963 g_return_if_fail (g_class != NULL);
1965 node = lookup_type_node_I (class->g_type);
1966 G_WRITE_LOCK (&type_rw_lock);
1967 if (node && node->is_classed && node->data &&
1968 node->data->class.class == class && node->data->common.ref_count > 0)
1969 type_data_unref_Wm (node, FALSE);
1971 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1972 type_descriptive_name_I (class->g_type));
1973 G_WRITE_UNLOCK (&type_rw_lock);
1977 g_type_class_unref_uncached (gpointer g_class)
1980 GTypeClass *class = g_class;
1982 g_return_if_fail (g_class != NULL);
1984 G_WRITE_LOCK (&type_rw_lock);
1985 node = lookup_type_node_I (class->g_type);
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, TRUE);
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_peek (GType type)
2001 node = lookup_type_node_I (type);
2002 G_READ_LOCK (&type_rw_lock);
2003 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2004 class = node->data->class.class;
2007 G_READ_UNLOCK (&type_rw_lock);
2013 g_type_class_peek_parent (gpointer g_class)
2016 gpointer class = NULL;
2018 g_return_val_if_fail (g_class != NULL, NULL);
2020 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2021 G_READ_LOCK (&type_rw_lock);
2022 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2024 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2025 class = node->data->class.class;
2027 else if (NODE_PARENT_TYPE (node))
2028 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2029 G_READ_UNLOCK (&type_rw_lock);
2035 g_type_interface_peek (gpointer instance_class,
2040 gpointer vtable = NULL;
2041 GTypeClass *class = instance_class;
2043 g_return_val_if_fail (instance_class != NULL, NULL);
2045 node = lookup_type_node_I (class->g_type);
2046 iface = lookup_type_node_I (iface_type);
2047 if (node && node->is_instantiatable && iface)
2051 G_READ_LOCK (&type_rw_lock);
2053 entry = type_lookup_iface_entry_L (node, iface);
2054 if (entry && entry->vtable) /* entry is relocatable */
2055 vtable = entry->vtable;
2057 G_READ_UNLOCK (&type_rw_lock);
2060 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2066 g_type_interface_peek_parent (gpointer g_iface)
2070 gpointer vtable = NULL;
2071 GTypeInterface *iface_class = g_iface;
2073 g_return_val_if_fail (g_iface != NULL, NULL);
2075 iface = lookup_type_node_I (iface_class->g_type);
2076 node = lookup_type_node_I (iface_class->g_instance_type);
2078 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2079 if (node && node->is_instantiatable && iface)
2083 G_READ_LOCK (&type_rw_lock);
2085 entry = type_lookup_iface_entry_L (node, iface);
2086 if (entry && entry->vtable) /* entry is relocatable */
2087 vtable = entry->vtable;
2089 G_READ_UNLOCK (&type_rw_lock);
2092 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2097 G_CONST_RETURN gchar*
2098 g_type_name (GType type)
2102 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2104 node = lookup_type_node_I (type);
2106 return node ? NODE_NAME (node) : NULL;
2110 g_type_qname (GType type)
2114 node = lookup_type_node_I (type);
2116 return node ? node->qname : 0;
2120 g_type_from_name (const gchar *name)
2125 g_return_val_if_fail (name != NULL, 0);
2127 quark = g_quark_try_string (name);
2130 G_READ_LOCK (&type_rw_lock);
2131 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
2132 G_READ_UNLOCK (&type_rw_lock);
2139 g_type_parent (GType type)
2143 node = lookup_type_node_I (type);
2145 return node ? NODE_PARENT_TYPE (node) : 0;
2149 g_type_depth (GType type)
2153 node = lookup_type_node_I (type);
2155 return node ? node->n_supers + 1 : 0;
2159 g_type_next_base (GType type,
2165 node = lookup_type_node_I (type);
2168 TypeNode *base_node = lookup_type_node_I (base_type);
2170 if (base_node && base_node->n_supers < node->n_supers)
2172 guint n = node->n_supers - base_node->n_supers;
2174 if (node->supers[n] == base_type)
2175 atype = node->supers[n - 1];
2182 static inline gboolean
2183 type_node_is_a_U (TypeNode *node,
2184 TypeNode *iface_node,
2185 /* support_inheritance */
2186 gboolean support_interfaces,
2187 gboolean support_prerequisites)
2191 if (/* support_inheritance && */
2192 iface_node->n_supers <= node->n_supers &&
2193 node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2196 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2197 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2199 if (support_interfaces || support_prerequisites)
2201 G_READ_LOCK (&type_rw_lock);
2202 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2204 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2206 G_READ_UNLOCK (&type_rw_lock);
2212 g_type_is_a (GType type,
2215 TypeNode *node, *iface_node;
2218 node = lookup_type_node_I (type);
2219 iface_node = lookup_type_node_I (iface_type);
2220 is_a = node && iface_node && type_node_is_a_U (node, iface_node, TRUE, TRUE);
2225 GType* /* free result */
2226 g_type_children (GType type,
2231 node = lookup_type_node_I (type);
2236 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2237 children = g_new (GType, node->n_children + 1);
2238 memcpy (children, node->children, sizeof (GType) * node->n_children);
2239 children[node->n_children] = 0;
2242 *n_children = node->n_children;
2243 G_READ_UNLOCK (&type_rw_lock);
2256 GType* /* free result */
2257 g_type_interfaces (GType type,
2258 guint *n_interfaces)
2262 node = lookup_type_node_I (type);
2263 if (node && node->is_instantiatable)
2268 G_READ_LOCK (&type_rw_lock);
2269 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2270 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2271 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2275 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2276 G_READ_UNLOCK (&type_rw_lock);
2289 typedef struct _QData QData;
2301 static inline gpointer
2302 type_get_qdata_L (TypeNode *node,
2305 GData *gdata = node->global_gdata;
2307 if (quark && gdata && gdata->n_qdatas)
2309 QData *qdatas = gdata->qdatas - 1;
2310 guint n_qdatas = gdata->n_qdatas;
2317 i = (n_qdatas + 1) / 2;
2319 if (quark == check->quark)
2321 else if (quark > check->quark)
2326 else /* if (quark < check->quark) */
2335 g_type_get_qdata (GType type,
2341 node = lookup_type_node_I (type);
2344 G_READ_LOCK (&type_rw_lock);
2345 data = type_get_qdata_L (node, quark);
2346 G_READ_UNLOCK (&type_rw_lock);
2350 g_return_val_if_fail (node != NULL, NULL);
2357 type_set_qdata_W (TypeNode *node,
2365 /* setup qdata list if necessary */
2366 if (!node->global_gdata)
2367 node->global_gdata = g_new0 (GData, 1);
2368 gdata = node->global_gdata;
2370 /* try resetting old data */
2371 qdata = gdata->qdatas;
2372 for (i = 0; i < gdata->n_qdatas; i++)
2373 if (qdata[i].quark == quark)
2375 qdata[i].data = data;
2381 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2382 qdata = gdata->qdatas;
2383 for (i = 0; i < gdata->n_qdatas - 1; i++)
2384 if (qdata[i].quark > quark)
2386 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2387 qdata[i].quark = quark;
2388 qdata[i].data = data;
2392 g_type_set_qdata (GType type,
2398 g_return_if_fail (quark != 0);
2400 node = lookup_type_node_I (type);
2403 G_WRITE_LOCK (&type_rw_lock);
2404 type_set_qdata_W (node, quark, data);
2405 G_WRITE_UNLOCK (&type_rw_lock);
2408 g_return_if_fail (node != NULL);
2412 type_add_flags_W (TypeNode *node,
2417 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2418 g_return_if_fail (node != NULL);
2420 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2421 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2422 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2424 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2428 g_type_query (GType type,
2433 g_return_if_fail (query != NULL);
2435 /* if node is not static and classed, we won't allow query */
2437 node = lookup_type_node_I (type);
2438 if (node && node->is_classed && !node->plugin)
2440 /* type is classed and probably even instantiatable */
2441 G_READ_LOCK (&type_rw_lock);
2442 if (node->data) /* type is static or referenced */
2444 query->type = NODE_TYPE (node);
2445 query->type_name = NODE_NAME (node);
2446 query->class_size = node->data->class.class_size;
2447 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2449 G_READ_UNLOCK (&type_rw_lock);
2454 /* --- implementation details --- */
2456 g_type_test_flags (GType type,
2460 gboolean result = FALSE;
2462 node = lookup_type_node_I (type);
2465 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2466 guint tflags = flags & TYPE_FLAG_MASK;
2470 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2472 fflags = (finfo->type_flags & fflags) == fflags;
2479 G_READ_LOCK (&type_rw_lock);
2480 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2481 G_READ_UNLOCK (&type_rw_lock);
2486 result = tflags && fflags;
2493 g_type_get_plugin (GType type)
2497 node = lookup_type_node_I (type);
2499 return node ? node->plugin : NULL;
2503 g_type_interface_get_plugin (GType instance_type,
2504 GType interface_type)
2509 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
2511 node = lookup_type_node_I (instance_type);
2512 iface = lookup_type_node_I (interface_type);
2515 IFaceHolder *iholder;
2516 GTypePlugin *plugin;
2518 G_READ_LOCK (&type_rw_lock);
2520 iholder = iface_node_get_holders_L (iface);
2521 while (iholder && iholder->instance_type != instance_type)
2522 iholder = iholder->next;
2523 plugin = iholder ? iholder->plugin : NULL;
2525 G_READ_UNLOCK (&type_rw_lock);
2530 g_return_val_if_fail (node == NULL, NULL);
2531 g_return_val_if_fail (iface == NULL, NULL);
2533 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2539 g_type_fundamental_next (void)
2543 G_READ_LOCK (&type_rw_lock);
2544 type = static_fundamental_next;
2545 G_READ_UNLOCK (&type_rw_lock);
2547 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2551 g_type_fundamental (GType type_id)
2553 TypeNode *node = lookup_type_node_I (type_id);
2555 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2559 g_type_check_instance_is_a (GTypeInstance *type_instance,
2562 TypeNode *node, *iface;
2565 if (!type_instance || !type_instance->g_class)
2568 node = lookup_type_node_I (type_instance->g_class->g_type);
2569 iface = lookup_type_node_I (iface_type);
2570 check = node && node->is_instantiatable && iface && type_node_is_a_U (node, iface, TRUE, FALSE);
2576 g_type_check_class_is_a (GTypeClass *type_class,
2579 TypeNode *node, *iface;
2585 node = lookup_type_node_I (type_class->g_type);
2586 iface = lookup_type_node_I (is_a_type);
2587 check = node && node->is_classed && iface && type_node_is_a_U (node, iface, FALSE, FALSE);
2593 g_type_check_instance_cast (GTypeInstance *type_instance,
2598 if (type_instance->g_class)
2600 TypeNode *node, *iface;
2601 gboolean is_instantiatable, check;
2603 node = lookup_type_node_I (type_instance->g_class->g_type);
2604 is_instantiatable = node && node->is_instantiatable;
2605 iface = lookup_type_node_I (iface_type);
2606 check = is_instantiatable && iface && type_node_is_a_U (node, iface, TRUE, FALSE);
2608 return type_instance;
2610 if (is_instantiatable)
2611 g_warning ("invalid cast from `%s' to `%s'",
2612 type_descriptive_name_I (type_instance->g_class->g_type),
2613 type_descriptive_name_I (iface_type));
2615 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2616 type_descriptive_name_I (type_instance->g_class->g_type),
2617 type_descriptive_name_I (iface_type));
2620 g_warning ("invalid unclassed pointer in cast to `%s'",
2621 type_descriptive_name_I (iface_type));
2624 g_warning ("invalid cast from (NULL) pointer to `%s'",
2625 type_descriptive_name_I (iface_type));
2627 return type_instance;
2631 g_type_check_class_cast (GTypeClass *type_class,
2636 TypeNode *node, *iface;
2637 gboolean is_classed, check;
2639 node = lookup_type_node_I (type_class->g_type);
2640 is_classed = node && node->is_classed;
2641 iface = lookup_type_node_I (is_a_type);
2642 check = is_classed && iface && type_node_is_a_U (node, iface, FALSE, FALSE);
2647 g_warning ("invalid class cast from `%s' to `%s'",
2648 type_descriptive_name_I (type_class->g_type),
2649 type_descriptive_name_I (is_a_type));
2651 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2652 type_descriptive_name_I (type_class->g_type),
2653 type_descriptive_name_I (is_a_type));
2656 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2657 type_descriptive_name_I (is_a_type));
2662 g_type_check_instance (GTypeInstance *type_instance)
2664 /* this function is just here to make the signal system
2665 * conveniently elaborated on instance checks
2669 if (type_instance->g_class)
2671 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2673 if (node && node->is_instantiatable)
2676 g_warning ("instance of invalid non-instantiatable type `%s'",
2677 type_descriptive_name_I (type_instance->g_class->g_type));
2680 g_warning ("instance with invalid (NULL) class pointer");
2683 g_warning ("invalid (NULL) pointer instance");
2688 static inline gboolean
2689 type_check_is_value_type_U (GType type)
2691 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2694 /* common path speed up */
2695 node = lookup_type_node_I (type);
2696 if (node && node->mutatable_check_cache)
2699 G_READ_LOCK (&type_rw_lock);
2703 if (node->data && node->data->common.ref_count > 0 &&
2704 node->data->common.value_table->value_init)
2705 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2706 else if (NODE_IS_IFACE (node))
2710 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2712 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2713 TypeNode *prnode = lookup_type_node_I (prtype);
2715 if (prnode->is_instantiatable)
2718 node = lookup_type_node_I (type);
2724 G_READ_UNLOCK (&type_rw_lock);
2726 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2730 g_type_check_is_value_type (GType type)
2732 return type_check_is_value_type_U (type);
2736 g_type_check_value (GValue *value)
2738 return value && type_check_is_value_type_U (value->g_type);
2742 g_type_check_value_holds (GValue *value,
2745 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2749 g_type_value_table_peek (GType type)
2751 GTypeValueTable *vtable = NULL;
2752 TypeNode *node = lookup_type_node_I (type);
2753 gboolean has_refed_data, has_table;
2756 /* speed up common code path, we're not 100% safe here,
2757 * but we should only get called with referenced types anyway
2759 data = node ? node->data : NULL;
2760 if (node->mutatable_check_cache)
2761 return data->common.value_table;
2763 G_READ_LOCK (&type_rw_lock);
2766 has_refed_data = node && node->data && node->data->common.ref_count;
2767 has_table = has_refed_data && node->data->common.value_table->value_init;
2771 vtable = node->data->common.value_table;
2772 else if (NODE_IS_IFACE (node))
2776 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2778 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2779 TypeNode *prnode = lookup_type_node_I (prtype);
2781 if (prnode->is_instantiatable)
2784 node = lookup_type_node_I (type);
2785 goto restart_table_peek;
2791 G_READ_UNLOCK (&type_rw_lock);
2797 g_warning (G_STRLOC ": type id `%u' is invalid", type);
2798 if (!has_refed_data)
2799 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2800 type_descriptive_name_I (type));
2805 G_CONST_RETURN gchar*
2806 g_type_name_from_instance (GTypeInstance *instance)
2809 return "<NULL-instance>";
2811 return g_type_name_from_class (instance->g_class);
2814 G_CONST_RETURN gchar*
2815 g_type_name_from_class (GTypeClass *g_class)
2818 return "<NULL-class>";
2820 return g_type_name (g_class->g_type);
2824 /* --- foreign prototypes --- */
2825 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2826 extern void g_enum_types_init (void); /* sync with genums.c */
2827 extern void g_param_type_init (void); /* sync with gparam.c */
2828 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2829 extern void g_object_type_init (void); /* sync with gobject.c */
2830 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2831 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2832 extern void g_signal_init (void); /* sync with gsignal.c */
2835 /* --- initialization --- */
2837 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2839 G_LOCK_DEFINE_STATIC (type_init_lock);
2840 const gchar *env_string;
2845 G_LOCK (type_init_lock);
2847 G_WRITE_LOCK (&type_rw_lock);
2849 if (static_quark_type_flags)
2851 G_WRITE_UNLOCK (&type_rw_lock);
2852 G_UNLOCK (type_init_lock);
2856 /* setup GRuntime wide debugging flags */
2857 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2858 env_string = g_getenv ("GRUNTIME_DEBUG");
2859 if (env_string != NULL)
2861 static GDebugKey debug_keys[] = {
2862 { "objects", G_TYPE_DEBUG_OBJECTS },
2863 { "signals", G_TYPE_DEBUG_SIGNALS },
2866 _g_type_debug_flags |= g_parse_debug_string (env_string,
2868 sizeof (debug_keys) / sizeof (debug_keys[0]));
2873 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
2874 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
2875 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
2877 /* type qname hash table */
2878 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2880 /* invalid type G_TYPE_INVALID (0)
2882 static_fundamental_type_nodes[0] = NULL;
2884 /* void type G_TYPE_NONE
2886 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2887 type = NODE_TYPE (node);
2888 g_assert (type == G_TYPE_NONE);
2890 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2892 memset (&info, 0, sizeof (info));
2893 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2894 type = NODE_TYPE (node);
2895 type_data_make_W (node, &info, NULL);
2896 g_assert (type == G_TYPE_INTERFACE);
2898 G_WRITE_UNLOCK (&type_rw_lock);
2900 /* G_TYPE_TYPE_PLUGIN
2902 g_type_plugin_get_type ();
2904 /* G_TYPE_* value types
2906 g_value_types_init ();
2908 /* G_TYPE_ENUM & G_TYPE_FLAGS
2910 g_enum_types_init ();
2914 g_boxed_type_init ();
2918 g_param_type_init ();
2922 g_object_type_init ();
2924 /* G_TYPE_PARAM_* pspec types
2926 g_param_spec_types_init ();
2928 /* Value Transformations
2930 g_value_transforms_init ();
2936 G_UNLOCK (type_init_lock);
2942 g_type_init_with_debug_flags (0);