1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998, 1999, 2000 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.
21 #include "gtypeplugin.h"
24 #undef FIXME_DISABLE_PREALLOCATIONS
26 /* NOTE: some functions (some internal variants and exported ones)
27 * invalidate data portions of the TypeNodes. if external functions/callbacks
28 * are called, pointers to memory maintained by TypeNodes have to be looked up
29 * again. this affects most of the struct TypeNode fields, e.g. ->children or
30 * ->iface_entries (not ->supers[] as of recently), as all those memory portions can
31 * get realloc()ed during callback invocation.
34 * - g_type_from_name() should do an ordered array lookup after fetching the
35 * the quark, instead of a second hashtable lookup.
36 * - speedup checks for virtual types, steal a bit somewhere
39 * - force interface initialization for already existing classes
40 * - make things threadsafe
43 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
44 G_TYPE_FLAG_INSTANTIATABLE | \
45 G_TYPE_FLAG_DERIVABLE | \
46 G_TYPE_FLAG_DEEP_DERIVABLE)
47 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT)
51 /* --- typedefs --- */
52 typedef struct _TypeNode TypeNode;
53 typedef struct _CommonData CommonData;
54 typedef struct _IFaceData IFaceData;
55 typedef struct _ClassData ClassData;
56 typedef struct _InstanceData InstanceData;
57 typedef union _TypeData TypeData;
58 typedef struct _IFaceEntry IFaceEntry;
59 typedef struct _IFaceHolder IFaceHolder;
62 /* --- prototypes --- */
63 static inline GTypeFundamentalInfo* type_node_fundamental_info (TypeNode *node);
64 static void type_add_flags (TypeNode *node,
66 static void type_data_make (TypeNode *node,
67 const GTypeInfo *info,
68 const GTypeValueTable *value_table);
69 static inline void type_data_ref (TypeNode *node);
70 static inline void type_data_unref (TypeNode *node,
72 static void type_data_last_unref (GType type,
76 /* --- structures --- */
77 struct _GValue /* kludge, keep in sync with gvalue.h */
84 guint n_children : 12;
88 guint is_instantiatable : 1;
95 IFaceEntry *iface_entries;
96 IFaceHolder *iface_conformants;
98 GType supers[1]; /* flexible array */
100 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
101 #define MAX_N_SUPERS (255)
102 #define MAX_N_CHILDREN (4095)
103 #define MAX_N_IFACES (511)
108 GInterfaceInfo *info;
115 GTypeInterface *vtable;
120 GTypeValueTable *value_table;
126 GBaseInitFunc vtable_init_base;
127 GBaseFinalizeFunc vtable_finalize_base;
133 GBaseInitFunc class_init_base;
134 GBaseFinalizeFunc class_finalize_base;
135 GClassInitFunc class_init;
136 GClassFinalizeFunc class_finalize;
137 gconstpointer class_data;
144 GBaseInitFunc class_init_base;
145 GBaseFinalizeFunc class_finalize_base;
146 GClassInitFunc class_init;
147 GClassFinalizeFunc class_finalize;
148 gconstpointer class_data;
150 guint16 instance_size;
152 GInstanceInitFunc instance_init;
153 GMemChunk *mem_chunk;
160 InstanceData instance;
164 GTypeClassCacheFunc cache_func;
168 /* --- variables --- */
169 static guint n_class_cache_funcs = 0;
170 static ClassCacheFunc *class_cache_funcs = NULL;
171 static GType last_fundamental_id = 0;
172 static GQuark quark_type_flags = 0;
175 /* --- externs --- */
176 const char *g_log_domain_gruntime = "GRuntime";
179 /* --- type nodes --- */
180 static GHashTable *g_type_nodes_ht = NULL;
181 static GType *g_branch_seqnos = NULL;
182 static TypeNode ***g_type_nodes = NULL;
184 static inline TypeNode*
185 LOOKUP_TYPE_NODE (register GType utype)
187 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
188 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
190 if (ftype < last_fundamental_id && b_seqno < g_branch_seqnos[ftype])
191 return g_type_nodes[ftype][b_seqno];
195 #define NODE_TYPE(node) (node->supers[0])
196 #define NODE_PARENT_TYPE(node) (node->supers[1])
197 #define NODE_NAME(node) (g_quark_to_string (node->qname))
200 type_node_any_new (TypeNode *pnode,
204 GTypeFundamentalFlags type_flags)
206 guint branch_last, n_supers = pnode ? pnode->n_supers + 1 : 0;
209 guint i, node_size = 0;
211 branch_last = g_branch_seqnos[ftype]++;
212 type = G_TYPE_DERIVE_ID (ftype, branch_last);
213 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (g_branch_seqnos[ftype] - 1))
214 g_type_nodes[ftype] = g_renew (TypeNode*, g_type_nodes[ftype], 1 << g_bit_storage (g_branch_seqnos[ftype] - 1));
217 node_size += sizeof (GTypeFundamentalInfo); /* fundamental type info */
218 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
219 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
220 node = g_malloc0 (node_size);
221 if (!pnode) /* offset fundamental types */
222 node = G_STRUCT_MEMBER_P (node, sizeof (GTypeFundamentalInfo));
223 g_type_nodes[ftype][branch_last] = node;
225 node->n_supers = n_supers;
228 node->supers[0] = type;
231 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
232 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
233 node->is_iface = G_TYPE_IS_INTERFACE (type);
237 node->private.iface_conformants = NULL;
239 node->private.iface_entries = NULL;
243 node->supers[0] = type;
244 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
246 node->is_classed = pnode->is_classed;
247 node->is_instantiatable = pnode->is_instantiatable;
248 node->is_iface = pnode->is_iface;
253 node->private.iface_conformants = NULL;
257 node->n_ifaces = pnode->n_ifaces;
258 node->private.iface_entries = g_memdup (pnode->private.iface_entries,
259 sizeof (pnode->private.iface_entries[0]) * node->n_ifaces);
262 i = pnode->n_children++;
263 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
264 pnode->children[i] = type;
267 node->plugin = plugin;
268 node->n_children = 0;
269 node->children = NULL;
271 node->qname = g_quark_from_string (name);
272 node->static_gdata = NULL;
274 g_hash_table_insert (g_type_nodes_ht,
275 GUINT_TO_POINTER (node->qname),
276 GUINT_TO_POINTER (type));
281 static inline GTypeFundamentalInfo*
282 type_node_fundamental_info (TypeNode *node)
284 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
286 if (ftype != NODE_TYPE (node))
287 node = LOOKUP_TYPE_NODE (ftype);
289 return node ? G_STRUCT_MEMBER_P (node, - (gssize) sizeof (GTypeFundamentalInfo)) : NULL;
293 type_node_fundamental_new (GType ftype,
295 GTypeFundamentalFlags type_flags)
297 GTypeFundamentalInfo *finfo;
299 guint i, flast = last_fundamental_id;
301 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
303 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
305 last_fundamental_id = MAX (last_fundamental_id, ftype + 1);
306 if (last_fundamental_id > flast)
308 g_type_nodes = g_renew (TypeNode**, g_type_nodes, last_fundamental_id);
309 g_branch_seqnos = g_renew (GType, g_branch_seqnos, last_fundamental_id);
310 for (i = flast; i < last_fundamental_id; i++)
312 g_type_nodes[i] = NULL;
313 g_branch_seqnos[i] = 0;
316 g_assert (g_branch_seqnos[ftype] == 0);
318 node = type_node_any_new (NULL, ftype, name, NULL, type_flags);
319 finfo = type_node_fundamental_info (node);
320 finfo->type_flags = type_flags;
326 type_node_new (TypeNode *pnode,
332 g_assert (pnode->n_supers < MAX_N_SUPERS);
333 g_assert (pnode->n_children < MAX_N_CHILDREN);
335 return type_node_any_new (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
338 static inline IFaceEntry*
339 type_lookup_iface_entry (TypeNode *node,
342 if (iface->is_iface && node->n_ifaces)
344 IFaceEntry *ifaces = node->private.iface_entries - 1;
345 guint n_ifaces = node->n_ifaces;
346 GType iface_type = NODE_TYPE (iface);
348 do /* FIXME: should optimize iface lookups for <= 4 */
353 i = (n_ifaces + 1) / 2;
355 if (iface_type == check->iface_type)
357 else if (iface_type > check->iface_type)
362 else /* if (iface_type < check->iface_type) */
372 type_descriptive_name (GType type)
376 gchar *name = g_type_name (type);
378 return name ? name : "<unknown>";
385 /* --- type consistency checks --- */
387 check_plugin (GTypePlugin *plugin,
388 gboolean need_complete_type_info,
389 gboolean need_complete_interface_info,
390 const gchar *type_name)
394 g_warning ("plugin handle for type `%s' is NULL",
398 if (!G_IS_TYPE_PLUGIN (plugin))
400 g_warning ("plugin pointer (%p) for type `%s' is invalid",
404 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
406 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
410 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
412 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
420 check_type_name (const gchar *type_name)
422 static const gchar *extra_chars = "-_+";
423 const gchar *p = type_name;
426 if (!type_name[0] || !type_name[1] || !type_name[2])
428 g_warning ("type name `%s' is too short", type_name);
431 /* check the first letter */
432 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
433 for (p = type_name + 1; *p; p++)
434 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
435 (p[0] >= 'a' && p[0] <= 'z') ||
436 (p[0] >= '0' && p[0] <= '9') ||
437 strchr (extra_chars, p[0]));
440 g_warning ("type name `%s' contains invalid characters", type_name);
443 if (g_type_from_name (type_name))
445 g_warning ("cannot register existing type `%s'", type_name);
453 check_derivation (GType parent_type,
454 const gchar *type_name)
456 TypeNode *pnode = LOOKUP_TYPE_NODE (parent_type);
457 GTypeFundamentalInfo* finfo = type_node_fundamental_info (pnode);
461 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
463 type_descriptive_name (parent_type));
466 /* ensure flat derivability */
467 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
469 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
474 /* ensure deep derivability */
475 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
476 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
478 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
488 check_value_table (const gchar *type_name,
489 const GTypeValueTable *value_table)
493 else if (value_table->value_init == NULL)
495 if (value_table->value_free || value_table->value_copy ||
496 value_table->value_peek_pointer ||
497 value_table->collect_type || value_table->collect_value ||
498 value_table->lcopy_type || value_table->lcopy_value)
499 g_warning ("cannot handle uninitializable values of type `%s'",
504 else /* value_table->value_init != NULL */
506 if (!value_table->value_free)
509 * g_warning ("missing `value_free()' for type `%s'", type_name);
513 if (!value_table->value_copy)
515 g_warning ("missing `value_copy()' for type `%s'", type_name);
518 if ((value_table->collect_type || value_table->collect_value) &&
519 (!value_table->collect_type || !value_table->collect_value))
521 g_warning ("one of `collect_type' and `collect_value()' is unspecified for type `%s'",
525 if ((value_table->lcopy_type || value_table->lcopy_value) &&
526 (!value_table->lcopy_type || !value_table->lcopy_value))
528 g_warning ("one of `lcopy_type' and `lcopy_value()' is unspecified for type `%s'",
538 check_type_info (TypeNode *pnode,
540 const gchar *type_name,
541 const GTypeInfo *info)
543 GTypeFundamentalInfo *finfo = type_node_fundamental_info (LOOKUP_TYPE_NODE (ftype));
544 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
546 /* check instance members */
547 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
548 (info->instance_size || info->n_preallocs || info->instance_init))
551 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
555 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
559 /* check class & interface members */
560 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
561 (info->class_init || info->class_finalize || info->class_data ||
562 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
565 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
569 g_warning ("cannot create class for `%s' as non-classed fundamental",
573 /* check interface size */
574 if (is_interface && info->class_size < sizeof (GTypeInterface))
576 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
580 /* check class size */
581 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
583 if (info->class_size < sizeof (GTypeClass))
585 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
589 if (pnode && info->class_size < pnode->data->class.class_size)
591 g_warning ("specified class size for type `%s' is smaller "
592 "than the parent type's `%s' class size",
598 /* check instance size */
599 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
601 if (info->instance_size < sizeof (GTypeInstance))
603 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
607 if (pnode && info->instance_size < pnode->data->instance.instance_size)
609 g_warning ("specified instance size for type `%s' is smaller "
610 "than the parent type's `%s' instance size",
621 find_conforming_type (TypeNode *pnode,
624 TypeNode *node = NULL;
627 if (type_lookup_iface_entry (pnode, iface))
630 for (i = 0; i < pnode->n_children && !node; i++)
631 node = find_conforming_type (LOOKUP_TYPE_NODE (pnode->children[i]), iface);
637 check_add_interface (GType instance_type,
640 TypeNode *node = LOOKUP_TYPE_NODE (instance_type);
641 TypeNode *iface = LOOKUP_TYPE_NODE (iface_type);
644 if (!node || !node->is_instantiatable)
646 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
647 type_descriptive_name (instance_type));
650 if (!iface || !iface->is_iface)
652 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
653 type_descriptive_name (iface_type),
657 tnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (iface));
658 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry (node, tnode))
660 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
666 tnode = find_conforming_type (node, iface);
669 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
680 check_interface_info (TypeNode *iface,
682 const GInterfaceInfo *info)
684 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
686 g_warning ("interface type `%s' for type `%s' comes without initializer",
688 type_descriptive_name (instance_type));
696 /* --- type info (type node data) --- */
698 type_add_flags (TypeNode *node,
703 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
704 g_return_if_fail (node != NULL);
706 if (!quark_type_flags)
707 quark_type_flags = g_quark_from_static_string ("GTypeFlags");
708 if ((flags & G_TYPE_FLAG_ABSTRACT) && node->is_classed &&
709 node->data && node->data->class.class)
710 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
711 dflags = GPOINTER_TO_UINT (g_type_get_qdata (NODE_TYPE (node), quark_type_flags));
713 g_type_set_qdata (NODE_TYPE (node), quark_type_flags, GUINT_TO_POINTER (dflags));
717 type_data_make (TypeNode *node,
718 const GTypeInfo *info,
719 const GTypeValueTable *value_table)
722 GTypeValueTable *vtable = NULL;
723 guint vtable_size = 0;
725 g_assert (node->data == NULL && info != NULL);
729 TypeNode *pnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
732 vtable = pnode->data->common.value_table;
735 static const GTypeValueTable zero_vtable = { NULL, };
737 value_table = &zero_vtable;
741 vtable_size = sizeof (GTypeValueTable);
743 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
745 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
747 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
748 data->instance.class_size = info->class_size;
749 data->instance.class_init_base = info->base_init;
750 data->instance.class_finalize_base = info->base_finalize;
751 data->instance.class_init = info->class_init;
752 data->instance.class_finalize = info->class_finalize;
753 data->instance.class_data = info->class_data;
754 data->instance.class = NULL;
755 data->instance.instance_size = info->instance_size;
756 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
757 #ifdef FIXME_DISABLE_PREALLOCATIONS
758 data->instance.n_preallocs = 0;
760 data->instance.instance_init = info->instance_init;
761 data->instance.mem_chunk = NULL;
763 else if (node->is_classed) /* only classed */
765 data = g_malloc0 (sizeof (ClassData) + vtable_size);
767 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
768 data->class.class_size = info->class_size;
769 data->class.class_init_base = info->base_init;
770 data->class.class_finalize_base = info->base_finalize;
771 data->class.class_init = info->class_init;
772 data->class.class_finalize = info->class_finalize;
773 data->class.class_data = info->class_data;
774 data->class.class = NULL;
776 else if (node->is_iface)
778 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
780 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
781 data->iface.vtable_size = info->class_size;
782 data->iface.vtable_init_base = info->base_init;
783 data->iface.vtable_finalize_base = info->base_finalize;
787 data = g_malloc0 (sizeof (CommonData) + vtable_size);
789 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
793 node->data->common.ref_count = 1;
796 *vtable = *value_table;
797 node->data->common.value_table = vtable;
799 g_assert (node->data->common.value_table != NULL); /* paranoid */
803 type_data_ref (TypeNode *node)
807 TypeNode *pnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
809 GTypeValueTable tmp_value_table;
811 g_assert (node->plugin != NULL);
814 type_data_ref (pnode);
816 memset (&tmp_info, 0, sizeof (tmp_info));
817 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
818 g_type_plugin_use (node->plugin);
819 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
820 check_type_info (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
821 type_data_make (node, &tmp_info,
822 check_value_table (NODE_NAME (node), &tmp_value_table) ? &tmp_value_table : NULL);
826 g_assert (node->data->common.ref_count > 0);
828 node->data->common.ref_count += 1;
833 type_data_unref (TypeNode *node,
836 g_assert (node->data && node->data->common.ref_count);
838 if (node->data->common.ref_count > 1)
839 node->data->common.ref_count -= 1;
844 g_warning ("static type `%s' unreferenced too often",
849 type_data_last_unref (NODE_TYPE (node), uncached);
854 type_node_add_iface_entry (TypeNode *node,
860 g_assert (node->is_instantiatable && node->n_ifaces < MAX_N_IFACES);
863 node->private.iface_entries = g_renew (IFaceEntry, node->private.iface_entries, node->n_ifaces);
864 entries = node->private.iface_entries;
865 for (i = 0; i < node->n_ifaces - 1; i++)
866 if (entries[i].iface_type > iface_type)
868 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (node->n_ifaces - i - 1));
869 entries[i].iface_type = iface_type;
870 entries[i].vtable = NULL;
872 for (i = 0; i < node->n_children; i++)
873 type_node_add_iface_entry (LOOKUP_TYPE_NODE (node->children[i]), iface_type);
877 type_add_interface (TypeNode *node,
879 const GInterfaceInfo *info,
882 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
884 /* we must not call any functions of GInterfaceInfo from within here, since
885 * we got most probably called from _within_ a type registration function
887 g_assert (node->is_instantiatable && iface->is_iface && ((info && !plugin) || (!info && plugin)));
889 iholder->next = iface->private.iface_conformants;
890 iface->private.iface_conformants = iholder;
891 iholder->instance_type = NODE_TYPE (node);
892 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
893 iholder->plugin = plugin;
895 type_node_add_iface_entry (node, NODE_TYPE (iface));
899 type_iface_retrive_holder_info (TypeNode *iface,
902 IFaceHolder *iholder = iface->private.iface_conformants;
904 g_assert (iface->is_iface);
906 while (iholder->instance_type != instance_type)
907 iholder = iholder->next;
911 GInterfaceInfo tmp_info;
913 g_assert (iholder->plugin != NULL);
915 type_data_ref (iface);
917 memset (&tmp_info, 0, sizeof (tmp_info));
918 g_type_plugin_use (iholder->plugin);
919 g_type_plugin_complete_interface_info (iholder->plugin, NODE_TYPE (iface), instance_type, &tmp_info);
920 check_interface_info (iface, instance_type, &tmp_info);
921 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
928 type_iface_blow_holder_info (TypeNode *iface,
931 IFaceHolder *iholder = iface->private.iface_conformants;
933 g_assert (iface->is_iface);
935 while (iholder->instance_type != instance_type)
936 iholder = iholder->next;
938 if (iholder->info && iholder->plugin)
940 g_free (iholder->info);
941 iholder->info = NULL;
942 g_type_plugin_unuse (iholder->plugin);
944 type_data_unref (iface, FALSE);
949 /* --- type structure creation/destruction --- */
951 g_type_create_instance (GType type)
953 TypeNode *node = LOOKUP_TYPE_NODE (type);
954 GTypeInstance *instance;
958 if (!node || !node->is_instantiatable)
960 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
961 type_descriptive_name (type));
964 if (G_TYPE_IS_ABSTRACT (type))
966 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
967 type_descriptive_name (type));
971 class = g_type_class_ref (type);
973 if (node->data->instance.n_preallocs)
975 if (!node->data->instance.mem_chunk)
976 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
977 node->data->instance.instance_size,
978 (node->data->instance.instance_size *
979 node->data->instance.n_preallocs),
981 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
984 instance = g_malloc0 (node->data->instance.instance_size);
986 for (i = node->n_supers; i > 0; i--)
988 TypeNode *pnode = LOOKUP_TYPE_NODE (node->supers[i]);
990 if (pnode->data->instance.instance_init)
992 instance->g_class = pnode->data->instance.class;
993 pnode->data->instance.instance_init (instance, class);
996 instance->g_class = class;
997 if (node->data->instance.instance_init)
998 node->data->instance.instance_init (instance, class);
1004 g_type_free_instance (GTypeInstance *instance)
1009 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1011 class = instance->g_class;
1012 node = LOOKUP_TYPE_NODE (class->g_type);
1013 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1015 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1016 type_descriptive_name (class->g_type));
1019 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1021 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1026 instance->g_class = NULL;
1027 memset (instance, 0xaa, node->data->instance.instance_size); // FIXME: debugging hack
1028 if (node->data->instance.n_preallocs)
1029 g_chunk_free (instance, node->data->instance.mem_chunk);
1033 g_type_class_unref (class);
1037 type_propagate_iface_vtable (TypeNode *pnode,
1039 GTypeInterface *vtable)
1041 IFaceEntry *entry = type_lookup_iface_entry (pnode, iface);
1044 entry->vtable = vtable;
1045 for (i = 0; i < pnode->n_children; i++)
1047 TypeNode *node = LOOKUP_TYPE_NODE (pnode->children[i]);
1049 type_propagate_iface_vtable (node, iface, vtable);
1054 type_iface_vtable_init (TypeNode *iface,
1057 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1058 IFaceHolder *iholder = type_iface_retrive_holder_info (iface, NODE_TYPE (node));
1059 GTypeInterface *vtable;
1061 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1063 vtable = g_malloc0 (iface->data->iface.vtable_size);
1064 type_propagate_iface_vtable (node, iface, vtable);
1065 vtable->g_type = NODE_TYPE (iface);
1066 vtable->g_instance_type = NODE_TYPE (node);
1068 if (iface->data->iface.vtable_init_base)
1069 iface->data->iface.vtable_init_base (vtable);
1070 if (iholder->info->interface_init)
1071 iholder->info->interface_init (vtable, iholder->info->interface_data);
1075 type_iface_vtable_finalize (TypeNode *iface,
1077 GTypeInterface *vtable)
1079 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1080 IFaceHolder *iholder = iface->private.iface_conformants;
1082 g_assert (entry && entry->vtable == vtable);
1084 while (iholder->instance_type != NODE_TYPE (node))
1085 iholder = iholder->next;
1086 g_assert (iholder && iholder->info);
1088 type_propagate_iface_vtable (node, iface, NULL);
1089 if (iholder->info->interface_finalize)
1090 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1091 if (iface->data->iface.vtable_finalize_base)
1092 iface->data->iface.vtable_finalize_base (vtable);
1095 vtable->g_instance_type = 0;
1098 type_iface_blow_holder_info (iface, NODE_TYPE (node));
1102 type_class_init (TypeNode *node,
1105 GSList *slist, *init_slist = NULL;
1111 g_assert (node->is_classed && node->data &&
1112 node->data->class.class_size &&
1113 !node->data->class.class);
1115 class = g_malloc0 (node->data->class.class_size);
1116 node->data->class.class = class;
1120 TypeNode *pnode = LOOKUP_TYPE_NODE (pclass->g_type);
1122 memcpy (class, pclass, pnode->data->class.class_size);
1125 class->g_type = NODE_TYPE (node);
1127 /* stack all base class initialization functions, so we
1128 * call them in ascending order.
1130 for (bnode = node; bnode; bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (bnode)))
1131 if (bnode->data->class.class_init_base)
1132 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1133 for (slist = init_slist; slist; slist = slist->next)
1135 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1137 class_init_base (class);
1139 g_slist_free (init_slist);
1141 if (node->data->class.class_init)
1142 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1144 /* ok, we got the class done, now initialize all interfaces */
1145 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1146 if (!node->private.iface_entries[i].vtable)
1147 entry = node->private.iface_entries + i;
1150 type_iface_vtable_init (LOOKUP_TYPE_NODE (entry->iface_type), node);
1152 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1153 if (!node->private.iface_entries[i].vtable)
1154 entry = node->private.iface_entries + i;
1159 type_data_finalize_class_ifaces (TypeNode *node)
1164 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1166 g_message ("finalizing interfaces for %sClass `%s'",
1167 type_descriptive_name (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1168 type_descriptive_name (NODE_TYPE (node)));
1170 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1171 if (node->private.iface_entries[i].vtable &&
1172 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1173 entry = node->private.iface_entries + i;
1176 type_iface_vtable_finalize (LOOKUP_TYPE_NODE (entry->iface_type), node, entry->vtable);
1178 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1179 if (node->private.iface_entries[i].vtable &&
1180 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1181 entry = node->private.iface_entries + i;
1186 type_data_finalize_class (TypeNode *node,
1189 GTypeClass *class = cdata->class;
1192 g_assert (cdata->class && cdata->common.ref_count == 0);
1194 g_message ("finalizing %sClass `%s'",
1195 type_descriptive_name (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1196 type_descriptive_name (NODE_TYPE (node)));
1198 if (cdata->class_finalize)
1199 cdata->class_finalize (class, (gpointer) cdata->class_data);
1201 /* call all base class destruction functions in descending order
1203 if (cdata->class_finalize_base)
1204 cdata->class_finalize_base (class);
1205 for (bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node)); bnode; bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (bnode)))
1206 if (bnode->data->class.class_finalize_base)
1207 bnode->data->class.class_finalize_base (class);
1210 g_free (cdata->class);
1214 type_data_last_unref (GType type,
1217 TypeNode *node = LOOKUP_TYPE_NODE (type);
1219 g_return_if_fail (node != NULL && node->plugin != NULL);
1221 if (!node->data || node->data->common.ref_count == 0)
1223 g_warning ("cannot drop last reference to unreferenced type `%s'",
1224 type_descriptive_name (type));
1228 if (node->is_classed && node->data && node->data->class.class)
1232 for (i = 0; i < n_class_cache_funcs; i++)
1233 if (class_cache_funcs[i].cache_func (class_cache_funcs[i].cache_data, node->data->class.class))
1237 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1238 node->data->common.ref_count -= 1;
1241 GType ptype = NODE_PARENT_TYPE (node);
1244 node->data->common.ref_count = 0;
1246 if (node->is_instantiatable && node->data->instance.mem_chunk)
1248 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1249 node->data->instance.mem_chunk = NULL;
1253 if (node->is_classed && tdata->class.class)
1256 type_data_finalize_class_ifaces (node);
1258 type_data_finalize_class (node, &tdata->class);
1266 type_data_unref (LOOKUP_TYPE_NODE (ptype), FALSE);
1267 g_type_plugin_unuse (node->plugin);
1272 g_type_add_class_cache_func (gpointer cache_data,
1273 GTypeClassCacheFunc cache_func)
1277 g_return_if_fail (cache_func != NULL);
1279 i = n_class_cache_funcs++;
1280 class_cache_funcs = g_renew (ClassCacheFunc, class_cache_funcs, n_class_cache_funcs);
1281 class_cache_funcs[i].cache_data = cache_data;
1282 class_cache_funcs[i].cache_func = cache_func;
1286 g_type_remove_class_cache_func (gpointer cache_data,
1287 GTypeClassCacheFunc cache_func)
1291 g_return_if_fail (cache_func != NULL);
1293 for (i = 0; i < n_class_cache_funcs; i++)
1294 if (class_cache_funcs[i].cache_data == cache_data &&
1295 class_cache_funcs[i].cache_func == cache_func)
1297 n_class_cache_funcs--;
1298 g_memmove (class_cache_funcs + i,
1299 class_cache_funcs + i + 1,
1300 sizeof (class_cache_funcs[0]) * (n_class_cache_funcs - i));
1301 class_cache_funcs = g_renew (ClassCacheFunc, class_cache_funcs, n_class_cache_funcs);
1306 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1307 cache_func, cache_data);
1311 /* --- type registration --- */
1313 g_type_register_fundamental (GType type_id,
1314 const gchar *type_name,
1315 const GTypeInfo *info,
1316 const GTypeFundamentalInfo *finfo,
1319 GTypeFundamentalInfo *node_finfo;
1322 g_return_val_if_fail (type_id > 0, 0);
1323 g_return_val_if_fail (type_name != NULL, 0);
1324 g_return_val_if_fail (info != NULL, 0);
1325 g_return_val_if_fail (finfo != NULL, 0);
1327 if (!check_type_name (type_name))
1329 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1331 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1336 if (LOOKUP_TYPE_NODE (type_id))
1338 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1339 type_descriptive_name (type_id),
1343 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1344 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1346 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1351 node = type_node_fundamental_new (type_id, type_name, finfo->type_flags);
1352 node_finfo = type_node_fundamental_info (node);
1353 type_add_flags (node, flags);
1355 if (!check_type_info (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1356 return NODE_TYPE (node);
1357 type_data_make (node, info,
1358 check_value_table (type_name, info->value_table) ? info->value_table : NULL);
1360 return NODE_TYPE (node);
1364 g_type_register_static (GType parent_type,
1365 const gchar *type_name,
1366 const GTypeInfo *info,
1369 TypeNode *pnode, *node;
1372 g_return_val_if_fail (parent_type > 0, 0);
1373 g_return_val_if_fail (type_name != NULL, 0);
1374 g_return_val_if_fail (info != NULL, 0);
1376 if (!check_type_name (type_name))
1378 if (!check_derivation (parent_type, type_name))
1381 pnode = LOOKUP_TYPE_NODE (parent_type);
1382 type_data_ref (pnode);
1384 if (!check_type_info (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1386 if (info->class_finalize)
1388 g_warning ("class finalizer specified for static type `%s'",
1393 node = type_node_new (pnode, type_name, NULL);
1394 type_add_flags (node, flags);
1395 type = NODE_TYPE (node);
1396 type_data_make (node, info,
1397 check_value_table (type_name, info->value_table) ? info->value_table : NULL);
1403 g_type_register_dynamic (GType parent_type,
1404 const gchar *type_name,
1405 GTypePlugin *plugin,
1408 TypeNode *pnode, *node;
1411 g_return_val_if_fail (parent_type > 0, 0);
1412 g_return_val_if_fail (type_name != NULL, 0);
1413 g_return_val_if_fail (plugin != NULL, 0);
1415 if (!check_type_name (type_name))
1417 if (!check_derivation (parent_type, type_name))
1419 if (!check_plugin (plugin, TRUE, FALSE, type_name))
1421 pnode = LOOKUP_TYPE_NODE (parent_type);
1423 node = type_node_new (pnode, type_name, plugin);
1424 type_add_flags (node, flags);
1425 type = NODE_TYPE (node);
1431 g_type_add_interface_static (GType instance_type,
1432 GType interface_type,
1433 const GInterfaceInfo *info)
1438 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1439 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1441 if (!check_add_interface (instance_type, interface_type))
1443 node = LOOKUP_TYPE_NODE (instance_type);
1444 iface = LOOKUP_TYPE_NODE (interface_type);
1445 if (!check_interface_info (iface, NODE_TYPE (node), info))
1447 type_add_interface (node, iface, info, NULL);
1451 g_type_add_interface_dynamic (GType instance_type,
1452 GType interface_type,
1453 GTypePlugin *plugin)
1458 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1459 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1461 if (!check_add_interface (instance_type, interface_type))
1463 node = LOOKUP_TYPE_NODE (instance_type);
1464 iface = LOOKUP_TYPE_NODE (interface_type);
1465 if (!check_plugin (plugin, FALSE, TRUE, NODE_NAME (node)))
1467 type_add_interface (node, iface, NULL, plugin);
1471 /* --- public API functions --- */
1473 g_type_class_ref (GType type)
1475 TypeNode *node = LOOKUP_TYPE_NODE (type);
1477 /* optimize for common code path
1479 if (node && node->is_classed && node->data &&
1480 node->data->class.class && node->data->common.ref_count > 0)
1482 type_data_ref (node);
1484 return node->data->class.class;
1487 if (!node || !node->is_classed ||
1488 (node->data && node->data->common.ref_count < 1))
1490 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1491 type_descriptive_name (type));
1495 type_data_ref (node);
1497 if (!node->data->class.class)
1499 GType ptype = NODE_PARENT_TYPE (node);
1500 GTypeClass *pclass = ptype ? g_type_class_ref (ptype) : NULL;
1502 type_class_init (node, pclass);
1505 return node->data->class.class;
1509 g_type_class_unref (gpointer g_class)
1512 GTypeClass *class = g_class;
1514 g_return_if_fail (g_class != NULL);
1516 node = LOOKUP_TYPE_NODE (class->g_type);
1517 if (node && node->is_classed && node->data &&
1518 node->data->class.class == class && node->data->common.ref_count > 0)
1519 type_data_unref (node, FALSE);
1521 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1522 type_descriptive_name (class->g_type));
1526 g_type_class_unref_uncached (gpointer g_class)
1529 GTypeClass *class = g_class;
1531 g_return_if_fail (g_class != NULL);
1533 node = LOOKUP_TYPE_NODE (class->g_type);
1534 if (node && node->is_classed && node->data &&
1535 node->data->class.class == class && node->data->common.ref_count > 0)
1536 type_data_unref (node, TRUE);
1538 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1539 type_descriptive_name (class->g_type));
1543 g_type_class_peek (GType type)
1545 TypeNode *node = LOOKUP_TYPE_NODE (type);
1547 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1548 return node->data->class.class;
1554 g_type_class_peek_parent (gpointer g_class)
1558 g_return_val_if_fail (g_class != NULL, NULL);
1560 node = LOOKUP_TYPE_NODE (G_TYPE_FROM_CLASS (g_class));
1561 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1563 node = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
1565 return node->data->class.class;
1572 g_type_interface_peek (gpointer instance_class,
1577 GTypeClass *class = instance_class;
1579 g_return_val_if_fail (instance_class != NULL, NULL);
1581 node = LOOKUP_TYPE_NODE (class->g_type);
1582 iface = LOOKUP_TYPE_NODE (iface_type);
1583 if (node && node->is_instantiatable && iface)
1585 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1587 if (entry && entry->vtable)
1588 return entry->vtable;
1595 g_type_value_table_peek (GType type)
1597 TypeNode *node = LOOKUP_TYPE_NODE (type);
1599 if (node && node->data && node->data->common.ref_count > 0)
1600 return node->data->common.value_table->value_init ? node->data->common.value_table : NULL;
1606 g_type_name (GType type)
1608 TypeNode *node = LOOKUP_TYPE_NODE (type);
1610 return node ? NODE_NAME (node) : NULL;
1614 g_type_qname (GType type)
1616 TypeNode *node = LOOKUP_TYPE_NODE (type);
1618 return node ? node->qname : 0;
1622 g_type_from_name (const gchar *name)
1626 g_return_val_if_fail (name != NULL, 0);
1628 quark = g_quark_try_string (name);
1631 GType type = GPOINTER_TO_UINT (g_hash_table_lookup (g_type_nodes_ht, GUINT_TO_POINTER (quark)));
1641 g_type_parent (GType type)
1643 TypeNode *node = LOOKUP_TYPE_NODE (type);
1645 return node ? NODE_PARENT_TYPE (node) : 0;
1649 g_type_next_base (GType type,
1652 TypeNode *node = LOOKUP_TYPE_NODE (type);
1656 TypeNode *base_node = LOOKUP_TYPE_NODE (base_type);
1658 if (base_node && base_node->n_supers < node->n_supers)
1660 guint n = node->n_supers - base_node->n_supers;
1662 if (node->supers[n] == base_type)
1663 return node->supers[n - 1];
1672 g_type_is_a (GType type,
1675 if (type != is_a_type)
1677 TypeNode *node = LOOKUP_TYPE_NODE (type);
1681 TypeNode *a_node = LOOKUP_TYPE_NODE (is_a_type);
1683 if (a_node && a_node->n_supers <= node->n_supers)
1684 return node->supers[node->n_supers - a_node->n_supers] == is_a_type;
1688 return LOOKUP_TYPE_NODE (type) != NULL;
1695 g_type_is_a (GType type,
1698 if (type != iface_type)
1700 TypeNode *node = LOOKUP_TYPE_NODE (type);
1704 TypeNode *iface_node = LOOKUP_TYPE_NODE (iface_type);
1708 if (iface_node->is_iface && node->is_instantiatable)
1709 return type_lookup_iface_entry (node, iface_node) != NULL;
1710 else if (iface_node->n_supers <= node->n_supers)
1711 return node->supers[node->n_supers - iface_node->n_supers] == iface_type;
1716 return LOOKUP_TYPE_NODE (type) != NULL;
1722 g_type_fundamental_branch_last (GType type)
1724 GType ftype = G_TYPE_FUNDAMENTAL (type);
1726 return ftype < last_fundamental_id ? g_branch_seqnos[ftype] : 0;
1729 GType* /* free result */
1730 g_type_children (GType type,
1733 TypeNode *node = LOOKUP_TYPE_NODE (type);
1737 GType *children = g_new (GType, node->n_children + 1);
1739 memcpy (children, node->children, sizeof (GType) * node->n_children);
1740 children[node->n_children] = 0;
1743 *n_children = node->n_children;
1756 GType* /* free result */
1757 g_type_interfaces (GType type,
1758 guint *n_interfaces)
1760 TypeNode *node = LOOKUP_TYPE_NODE (type);
1762 if (node && node->is_instantiatable)
1764 GType *ifaces = g_new (GType, node->n_ifaces + 1);
1767 for (i = 0; i < node->n_ifaces; i++)
1768 ifaces[i] = node->private.iface_entries[i].iface_type;
1772 *n_interfaces = node->n_ifaces;
1785 typedef struct _QData QData;
1798 g_type_get_qdata (GType type,
1801 TypeNode *node = LOOKUP_TYPE_NODE (type);
1804 g_return_val_if_fail (node != NULL, NULL);
1806 gdata = node->static_gdata;
1807 if (quark && gdata && gdata->n_qdatas)
1809 QData *qdatas = gdata->qdatas - 1;
1810 guint n_qdatas = gdata->n_qdatas;
1817 i = (n_qdatas + 1) / 2;
1819 if (quark == check->quark)
1821 else if (quark > check->quark)
1826 else /* if (quark < check->quark) */
1836 g_type_set_qdata (GType type,
1840 TypeNode *node = LOOKUP_TYPE_NODE (type);
1845 g_return_if_fail (node != NULL);
1846 g_return_if_fail (quark != 0);
1848 /* setup qdata list if necessary */
1849 if (!node->static_gdata)
1850 node->static_gdata = g_new0 (GData, 1);
1851 gdata = node->static_gdata;
1853 /* try resetting old data */
1854 qdata = gdata->qdatas;
1855 for (i = 0; i < gdata->n_qdatas; i++)
1856 if (qdata[i].quark == quark)
1858 qdata[i].data = data;
1864 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
1865 qdata = gdata->qdatas;
1866 for (i = 0; i < gdata->n_qdatas - 1; i++)
1867 if (qdata[i].quark > quark)
1869 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
1870 qdata[i].quark = quark;
1871 qdata[i].data = data;
1875 /* --- implementation details --- */
1877 g_type_check_flags (GType type,
1880 TypeNode *node = LOOKUP_TYPE_NODE (type);
1881 gboolean result = FALSE;
1885 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
1886 guint tflags = flags & TYPE_FLAG_MASK;
1890 GTypeFundamentalInfo *finfo = type_node_fundamental_info (node);
1892 fflags = (finfo->type_flags & fflags) == fflags;
1898 tflags = (tflags & GPOINTER_TO_UINT (g_type_get_qdata (type, quark_type_flags))) == tflags;
1902 result = tflags && fflags;
1908 g_type_get_plugin (GType type)
1910 TypeNode *node = LOOKUP_TYPE_NODE (type);
1912 return node ? node->plugin : NULL;
1916 g_type_interface_get_plugin (GType instance_type,
1917 GType interface_type)
1919 TypeNode *node = LOOKUP_TYPE_NODE (instance_type);
1920 TypeNode *iface = LOOKUP_TYPE_NODE (interface_type);
1921 IFaceHolder *iholder;
1923 g_return_val_if_fail (node == NULL, NULL);
1924 g_return_val_if_fail (iface == NULL, NULL);
1925 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1927 iholder = iface->private.iface_conformants;
1929 while (iholder && iholder->instance_type != instance_type)
1930 iholder = iholder->next;
1934 g_warning (G_STRLOC ": Attempt to look up plugin for invalid instance/interface type pair.");
1938 return iholder->plugin;
1942 g_type_fundamental_last (void)
1944 return last_fundamental_id;
1948 g_type_instance_is_a (GTypeInstance *type_instance,
1951 return (type_instance && type_instance->g_class &&
1952 G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type) &&
1953 g_type_is_a (type_instance->g_class->g_type, iface_type));
1957 g_type_class_is_a (GTypeClass *type_class,
1960 return (type_class && G_TYPE_IS_CLASSED (type_class->g_type) &&
1961 g_type_is_a (type_class->g_type, is_a_type));
1965 g_type_value_is_a (GValue *value,
1972 node = LOOKUP_TYPE_NODE (value->g_type);
1974 if (!G_TYPE_IS_FUNDAMENTAL (value->g_type) && !node || !node->data)
1975 node = LOOKUP_TYPE_NODE (G_TYPE_FUNDAMENTAL (value->g_type));
1977 if (!node || !node->data || node->data->common.ref_count < 1 ||
1978 !node->data->common.value_table->value_init ||
1979 !g_type_is_a (value->g_type, type))
1986 g_type_check_value (GValue *value)
1988 return value && g_type_value_is_a (value, value->g_type);
1992 g_type_check_instance_cast (GTypeInstance *type_instance,
1997 g_warning ("invalid cast from (NULL) pointer to `%s'",
1998 type_descriptive_name (iface_type));
1999 return type_instance;
2001 if (!type_instance->g_class)
2003 g_warning ("invalid unclassed pointer in cast to `%s'",
2004 type_descriptive_name (iface_type));
2005 return type_instance;
2007 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2009 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2010 type_descriptive_name (type_instance->g_class->g_type),
2011 type_descriptive_name (iface_type));
2012 return type_instance;
2014 if (!g_type_is_a (type_instance->g_class->g_type, iface_type))
2016 g_warning ("invalid cast from `%s' to `%s'",
2017 type_descriptive_name (type_instance->g_class->g_type),
2018 type_descriptive_name (iface_type));
2019 return type_instance;
2022 return type_instance;
2026 g_type_check_class_cast (GTypeClass *type_class,
2031 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2032 type_descriptive_name (is_a_type));
2035 if (!G_TYPE_IS_CLASSED (type_class->g_type))
2037 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2038 type_descriptive_name (type_class->g_type),
2039 type_descriptive_name (is_a_type));
2042 if (!g_type_is_a (type_class->g_type, is_a_type))
2044 g_warning ("invalid class cast from `%s' to `%s'",
2045 type_descriptive_name (type_class->g_type),
2046 type_descriptive_name (is_a_type));
2054 g_type_check_instance (GTypeInstance *type_instance)
2056 /* this function is just here to make the signal system
2057 * conveniently elaborated on instance checks
2061 g_warning ("instance is invalid (NULL) pointer");
2064 if (!type_instance->g_class)
2066 g_warning ("instance with invalid (NULL) class pointer");
2069 if (!G_TYPE_IS_CLASSED (type_instance->g_class->g_type))
2071 g_warning ("instance of invalid unclassed type `%s'",
2072 type_descriptive_name (type_instance->g_class->g_type));
2075 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2077 g_warning ("instance of invalid non-instantiatable type `%s'",
2078 type_descriptive_name (type_instance->g_class->g_type));
2086 /* --- foreign prototypes --- */
2087 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2088 extern void g_enum_types_init (void); /* sync with genums.c */
2089 extern void g_param_type_init (void); /* sync with gparam.c */
2090 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2091 extern void g_object_type_init (void); /* sync with gobject.c */
2092 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2093 extern void g_signal_init (void); /* sync with gsignal.c */
2096 /* --- initialization --- */
2100 static TypeNode *type0_node = NULL;
2105 if (last_fundamental_id)
2108 /* type qname hash table */
2109 g_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2111 /* invalid type G_TYPE_INVALID (0)
2113 last_fundamental_id = 1;
2114 g_type_nodes = g_renew (TypeNode**, g_type_nodes, last_fundamental_id);
2115 g_type_nodes[0] = &type0_node;
2116 g_branch_seqnos = g_renew (GType, g_branch_seqnos, last_fundamental_id);
2117 g_branch_seqnos[0] = 1;
2119 /* void type G_TYPE_NONE
2121 node = type_node_fundamental_new (G_TYPE_NONE, "void", 0);
2122 type = NODE_TYPE (node);
2123 g_assert (type == G_TYPE_NONE);
2125 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2127 memset (&info, 0, sizeof (info));
2128 node = type_node_fundamental_new (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2129 type = NODE_TYPE (node);
2130 type_data_make (node, &info, NULL);
2131 g_assert (type == G_TYPE_INTERFACE);
2133 /* G_TYPE_TYPE_PLUGIN
2135 g_type_plugin_get_type ();
2137 /* G_TYPE_* value types
2139 g_value_types_init ();
2141 /* G_TYPE_ENUM & G_TYPE_FLAGS
2143 g_enum_types_init ();
2147 g_param_type_init ();
2151 g_boxed_type_init ();
2155 g_object_type_init ();
2157 /* G_TYPE_PARAM_* pspec types
2159 g_param_spec_types_init ();