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 * ->iface_entries (not ->supers[] as of recently), as all those memory portions can
35 * get realloc()ed during callback invocation.
38 * - g_type_from_name() should do an ordered array lookup after fetching the
39 * the quark, instead of a second hashtable lookup.
40 * - speedup checks for virtual types, steal a bit somewhere
43 * - force interface initialization for already existing classes
46 * lock handling issues when calling static functions are indicated by
47 * uppercase letter postfixes, all static functions have to have
48 * one of the below postfixes:
49 * - _I: [Indifferent about locking]
50 * function doesn't care about locks at all
51 * - _U: [Unlocked invocation]
52 * no read or write lock has to be held across function invocation
53 * (locks may be acquired and released during invocation though)
54 * - _L: [Locked invocation]
55 * a write lock or more than 0 read locks have to be held across
57 * - _W: [Write-locked invocation]
58 * a write lock has to be held across function invokation
59 * - _Wm: [Write-locked invocation, mutatable]
60 * like _W, but the write lock might be released and reacquired
61 * during invocation, watch your pointers
64 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
65 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
66 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
67 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
68 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
69 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
70 static const gchar *_action = " invalidly modified type "; \
71 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
73 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
75 g_error ("%s()%s`%s'", _fname, _action, _tname); \
77 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
80 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
81 "%s: initialization assertion failed, use %s() prior to this function", \
82 G_STRLOC, G_STRINGIFY (init_function)); \
83 return (return_value); \
88 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
89 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
92 #else /* !G_ENABLE_DEBUG */
93 #define DEBUG_CODE(debug_type, code_block) /* code_block */
94 #endif /* G_ENABLE_DEBUG */
96 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
97 G_TYPE_FLAG_INSTANTIATABLE | \
98 G_TYPE_FLAG_DERIVABLE | \
99 G_TYPE_FLAG_DEEP_DERIVABLE)
100 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
103 /* --- typedefs --- */
104 typedef struct _TypeNode TypeNode;
105 typedef struct _CommonData CommonData;
106 typedef struct _IFaceData IFaceData;
107 typedef struct _ClassData ClassData;
108 typedef struct _InstanceData InstanceData;
109 typedef union _TypeData TypeData;
110 typedef struct _IFaceEntry IFaceEntry;
111 typedef struct _IFaceHolder IFaceHolder;
114 /* --- prototypes --- */
115 static inline GTypeFundamentalInfo* type_node_fundamental_info_L (TypeNode *node);
116 static void type_add_flags_W (TypeNode *node,
118 static void type_data_make_W (TypeNode *node,
119 const GTypeInfo *info,
120 const GTypeValueTable *value_table);
121 static inline void type_data_ref_Wm (TypeNode *node);
122 static inline void type_data_unref_Wm (TypeNode *node,
124 static void type_data_last_unref_Wm (GType type,
128 /* --- structures --- */
129 struct _GValue /* kludge, keep in sync with gvalue.h */
136 guint n_children : 12;
139 guint is_classed : 1;
140 guint is_instantiatable : 1;
147 IFaceEntry *iface_entries;
148 IFaceHolder *iface_conformants;
150 GType supers[1]; /* flexible array */
152 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
153 #define MAX_N_SUPERS (255)
154 #define MAX_N_CHILDREN (4095)
155 #define MAX_N_IFACES (511)
156 #define NODE_TYPE(node) (node->supers[0])
157 #define NODE_PARENT_TYPE(node) (node->supers[1])
158 #define NODE_NAME(node) ((gchar*)g_quark_to_string (node->qname))
163 GInterfaceInfo *info;
170 GTypeInterface *vtable;
175 GTypeValueTable *value_table;
181 GBaseInitFunc vtable_init_base;
182 GBaseFinalizeFunc vtable_finalize_base;
188 GBaseInitFunc class_init_base;
189 GBaseFinalizeFunc class_finalize_base;
190 GClassInitFunc class_init;
191 GClassFinalizeFunc class_finalize;
192 gconstpointer class_data;
199 GBaseInitFunc class_init_base;
200 GBaseFinalizeFunc class_finalize_base;
201 GClassInitFunc class_init;
202 GClassFinalizeFunc class_finalize;
203 gconstpointer class_data;
205 guint16 instance_size;
207 GInstanceInitFunc instance_init;
208 GMemChunk *mem_chunk;
215 InstanceData instance;
219 GTypeClassCacheFunc cache_func;
223 /* --- variables --- */
224 static guint static_n_class_cache_funcs = 0;
225 static ClassCacheFunc *static_class_cache_funcs = NULL;
226 static GType static_last_fundamental_id = 0;
227 static GQuark static_quark_type_flags = 0;
228 GTypeDebugFlags _g_type_debug_flags = 0;
231 /* --- externs --- */
232 const char *g_log_domain_gruntime = "GRuntime";
235 /* --- type nodes --- */
236 static GHashTable *static_type_nodes_ht = NULL;
237 static GType *static_branch_seqnos = NULL;
238 static TypeNode ***static_type_nodes = NULL;
240 static inline TypeNode*
241 lookup_type_node_L (register GType utype)
243 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
244 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
246 if (ftype < static_last_fundamental_id && b_seqno < static_branch_seqnos[ftype])
247 return static_type_nodes[ftype][b_seqno];
253 type_node_any_new_W (TypeNode *pnode,
257 GTypeFundamentalFlags type_flags)
259 guint branch_last, n_supers;
262 guint i, node_size = 0;
264 n_supers = pnode ? pnode->n_supers + 1 : 0;
265 branch_last = static_branch_seqnos[ftype]++;
266 type = G_TYPE_DERIVE_ID (ftype, branch_last);
267 g_assert ((type & G_TYPE_FLAG_RESERVED_ID_BIT) == 0);
268 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (static_branch_seqnos[ftype] - 1))
269 static_type_nodes[ftype] = g_renew (TypeNode*, static_type_nodes[ftype], 1 << g_bit_storage (static_branch_seqnos[ftype] - 1));
272 node_size += sizeof (GTypeFundamentalInfo); /* fundamental type info */
273 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
274 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
275 node = g_malloc0 (node_size);
276 if (!pnode) /* offset fundamental types */
277 node = G_STRUCT_MEMBER_P (node, sizeof (GTypeFundamentalInfo));
278 static_type_nodes[ftype][branch_last] = node;
280 node->n_supers = n_supers;
283 node->supers[0] = type;
286 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
287 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
288 node->is_iface = G_TYPE_IS_INTERFACE (type);
292 node->private.iface_conformants = NULL;
294 node->private.iface_entries = NULL;
298 node->supers[0] = type;
299 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
301 node->is_classed = pnode->is_classed;
302 node->is_instantiatable = pnode->is_instantiatable;
303 node->is_iface = pnode->is_iface;
308 node->private.iface_conformants = NULL;
312 node->n_ifaces = pnode->n_ifaces;
313 node->private.iface_entries = g_memdup (pnode->private.iface_entries,
314 sizeof (pnode->private.iface_entries[0]) * node->n_ifaces);
317 i = pnode->n_children++;
318 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
319 pnode->children[i] = type;
322 node->plugin = plugin;
323 node->n_children = 0;
324 node->children = NULL;
326 node->qname = g_quark_from_string (name);
327 node->global_gdata = NULL;
329 g_hash_table_insert (static_type_nodes_ht,
330 GUINT_TO_POINTER (node->qname),
331 GUINT_TO_POINTER (type));
335 static inline GTypeFundamentalInfo*
336 type_node_fundamental_info_L (TypeNode *node)
338 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
340 if (ftype != NODE_TYPE (node))
341 node = lookup_type_node_L (ftype);
343 return node ? G_STRUCT_MEMBER_P (node, - (gssize) sizeof (GTypeFundamentalInfo)) : NULL;
347 type_node_fundamental_new_W (GType ftype,
349 GTypeFundamentalFlags type_flags)
351 GTypeFundamentalInfo *finfo;
355 flast = static_last_fundamental_id;
357 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
359 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
361 static_last_fundamental_id = MAX (static_last_fundamental_id, ftype + 1);
362 if (static_last_fundamental_id > flast)
364 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
365 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
366 for (i = flast; i < static_last_fundamental_id; i++)
368 static_type_nodes[i] = NULL;
369 static_branch_seqnos[i] = 0;
372 g_assert (static_branch_seqnos[ftype] == 0);
374 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
376 finfo = type_node_fundamental_info_L (node);
377 finfo->type_flags = type_flags;
383 type_node_new_W (TypeNode *pnode,
389 g_assert (pnode->n_supers < MAX_N_SUPERS);
390 g_assert (pnode->n_children < MAX_N_CHILDREN);
392 return type_node_any_new_W (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
395 static inline IFaceEntry*
396 type_lookup_iface_entry_L (TypeNode *node,
399 if (iface->is_iface && node->n_ifaces)
401 IFaceEntry *ifaces = node->private.iface_entries - 1;
402 guint n_ifaces = node->n_ifaces;
403 GType iface_type = NODE_TYPE (iface);
410 i = (n_ifaces + 1) / 2;
412 if (iface_type == check->iface_type)
414 else if (iface_type > check->iface_type)
419 else /* if (iface_type < check->iface_type) */
429 type_descriptive_name_L (GType type)
433 TypeNode *node = lookup_type_node_L (type);
435 return node ? NODE_NAME (node) : "<unknown>";
442 type_descriptive_name_U (GType type)
446 G_READ_LOCK (&type_rw_lock);
447 name = type_descriptive_name_L (type);
448 G_READ_UNLOCK (&type_rw_lock);
450 return (gchar *)name;
454 /* --- type consistency checks --- */
456 check_plugin_U (GTypePlugin *plugin,
457 gboolean need_complete_type_info,
458 gboolean need_complete_interface_info,
459 const gchar *type_name)
461 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
465 g_warning ("plugin handle for type `%s' is NULL",
469 if (!G_IS_TYPE_PLUGIN (plugin))
471 g_warning ("plugin pointer (%p) for type `%s' is invalid",
475 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
477 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
481 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
483 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
491 check_type_name_U (const gchar *type_name)
493 static const gchar *extra_chars = "-_+";
494 const gchar *p = type_name;
497 if (!type_name[0] || !type_name[1] || !type_name[2])
499 g_warning ("type name `%s' is too short", type_name);
502 /* check the first letter */
503 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
504 for (p = type_name + 1; *p; p++)
505 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
506 (p[0] >= 'a' && p[0] <= 'z') ||
507 (p[0] >= '0' && p[0] <= '9') ||
508 strchr (extra_chars, p[0]));
511 g_warning ("type name `%s' contains invalid characters", type_name);
514 if (g_type_from_name (type_name))
516 g_warning ("cannot register existing type `%s'", type_name);
524 check_derivation_U (GType parent_type,
525 const gchar *type_name)
528 GTypeFundamentalInfo* finfo;
530 G_READ_LOCK (&type_rw_lock);
531 pnode = lookup_type_node_L (parent_type);
534 G_READ_UNLOCK (&type_rw_lock);
535 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
537 type_descriptive_name_U (parent_type));
540 finfo = type_node_fundamental_info_L (pnode);
541 /* ensure flat derivability */
542 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
544 G_READ_UNLOCK (&type_rw_lock);
545 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
550 /* ensure deep derivability */
551 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
552 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
554 G_READ_UNLOCK (&type_rw_lock);
555 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
560 G_READ_UNLOCK (&type_rw_lock);
566 check_collect_format_I (const gchar *collect_format)
568 const gchar *p = collect_format;
569 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
570 G_VALUE_COLLECT_DOUBLE, G_VALUE_COLLECT_POINTER,
574 if (!strchr (valid_format, *p++))
576 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
580 check_value_table_I (const gchar *type_name,
581 const GTypeValueTable *value_table)
585 else if (value_table->value_init == NULL)
587 if (value_table->value_free || value_table->value_copy ||
588 value_table->value_peek_pointer ||
589 value_table->collect_format || value_table->collect_value ||
590 value_table->lcopy_format || value_table->lcopy_value)
591 g_warning ("cannot handle uninitializable values of type `%s'",
595 else /* value_table->value_init != NULL */
597 if (!value_table->value_free)
600 * g_warning ("missing `value_free()' for type `%s'", type_name);
604 if (!value_table->value_copy)
606 g_warning ("missing `value_copy()' for type `%s'", type_name);
609 if ((value_table->collect_format || value_table->collect_value) &&
610 (!value_table->collect_format || !value_table->collect_value))
612 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
616 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
618 g_warning ("the `%s' specification for type `%s' is too long or invalid",
623 if ((value_table->lcopy_format || value_table->lcopy_value) &&
624 (!value_table->lcopy_format || !value_table->lcopy_value))
626 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
630 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
632 g_warning ("the `%s' specification for type `%s' is too long or invalid",
642 check_type_info_L (TypeNode *pnode,
644 const gchar *type_name,
645 const GTypeInfo *info)
647 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (lookup_type_node_L (ftype));
648 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
650 /* check instance members */
651 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
652 (info->instance_size || info->n_preallocs || info->instance_init))
655 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
659 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
663 /* check class & interface members */
664 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
665 (info->class_init || info->class_finalize || info->class_data ||
666 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
669 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
673 g_warning ("cannot create class for `%s' as non-classed fundamental",
677 /* check interface size */
678 if (is_interface && info->class_size < sizeof (GTypeInterface))
680 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
684 /* check class size */
685 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
687 if (info->class_size < sizeof (GTypeClass))
689 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
693 if (pnode && info->class_size < pnode->data->class.class_size)
695 g_warning ("specified class size for type `%s' is smaller "
696 "than the parent type's `%s' class size",
702 /* check instance size */
703 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
705 if (info->instance_size < sizeof (GTypeInstance))
707 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
711 if (pnode && info->instance_size < pnode->data->instance.instance_size)
713 g_warning ("specified instance size for type `%s' is smaller "
714 "than the parent type's `%s' instance size",
725 find_conforming_type_L (TypeNode *pnode,
728 TypeNode *node = NULL;
731 if (type_lookup_iface_entry_L (pnode, iface))
734 for (i = 0; i < pnode->n_children && !node; i++)
735 node = find_conforming_type_L (lookup_type_node_L (pnode->children[i]), iface);
741 check_add_interface_L (GType instance_type,
744 TypeNode *node = lookup_type_node_L (instance_type);
745 TypeNode *iface = lookup_type_node_L (iface_type);
748 if (!node || !node->is_instantiatable)
750 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
751 type_descriptive_name_L (instance_type));
754 if (!iface || !iface->is_iface)
756 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
757 type_descriptive_name_L (iface_type),
761 tnode = lookup_type_node_L (NODE_PARENT_TYPE (iface));
762 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
764 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
770 tnode = find_conforming_type_L (node, iface);
773 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
783 check_interface_info_L (TypeNode *iface,
785 const GInterfaceInfo *info)
787 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
789 g_warning ("interface type `%s' for type `%s' comes without initializer",
791 type_descriptive_name_L (instance_type));
798 /* --- type info (type node data) --- */
800 type_data_make_W (TypeNode *node,
801 const GTypeInfo *info,
802 const GTypeValueTable *value_table)
805 GTypeValueTable *vtable = NULL;
806 guint vtable_size = 0;
808 g_assert (node->data == NULL && info != NULL);
812 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
815 vtable = pnode->data->common.value_table;
818 static const GTypeValueTable zero_vtable = { NULL, };
820 value_table = &zero_vtable;
825 /* need to setup vtable_size since we have to allocate it with data in one chunk */
826 vtable_size = sizeof (GTypeValueTable);
827 if (value_table->collect_format)
828 vtable_size += strlen (value_table->collect_format);
829 if (value_table->lcopy_format)
830 vtable_size += strlen (value_table->lcopy_format);
834 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
836 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
838 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
839 data->instance.class_size = info->class_size;
840 data->instance.class_init_base = info->base_init;
841 data->instance.class_finalize_base = info->base_finalize;
842 data->instance.class_init = info->class_init;
843 data->instance.class_finalize = info->class_finalize;
844 data->instance.class_data = info->class_data;
845 data->instance.class = NULL;
846 data->instance.instance_size = info->instance_size;
847 #ifdef DISABLE_MEM_POOLS
848 data->instance.n_preallocs = 0;
849 #else /* !DISABLE_MEM_POOLS */
850 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
851 #endif /* !DISABLE_MEM_POOLS */
852 data->instance.instance_init = info->instance_init;
853 data->instance.mem_chunk = NULL;
855 else if (node->is_classed) /* only classed */
857 data = g_malloc0 (sizeof (ClassData) + vtable_size);
859 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
860 data->class.class_size = info->class_size;
861 data->class.class_init_base = info->base_init;
862 data->class.class_finalize_base = info->base_finalize;
863 data->class.class_init = info->class_init;
864 data->class.class_finalize = info->class_finalize;
865 data->class.class_data = info->class_data;
866 data->class.class = NULL;
868 else if (node->is_iface)
870 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
872 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
873 data->iface.vtable_size = info->class_size;
874 data->iface.vtable_init_base = info->base_init;
875 data->iface.vtable_finalize_base = info->base_finalize;
879 data = g_malloc0 (sizeof (CommonData) + vtable_size);
881 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
885 node->data->common.ref_count = 1;
891 /* we allocate the vtable and its strings together with the type data, so
892 * children can take over their parent's vtable pointer, and we don't
893 * need to worry freeing it or not when the child data is destroyed
895 *vtable = *value_table;
896 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
898 vtable->collect_format = p;
899 if (value_table->collect_format)
901 strcat (p, value_table->collect_format);
902 p += strlen (value_table->collect_format);
906 vtable->lcopy_format = p;
907 if (value_table->lcopy_format)
908 strcat (p, value_table->lcopy_format);
910 node->data->common.value_table = vtable;
912 g_assert (node->data->common.value_table != NULL); /* paranoid */
916 type_data_ref_Wm (TypeNode *node)
920 TypeNode *pnode = lookup_type_node_L (NODE_PARENT_TYPE (node));
922 GTypeValueTable tmp_value_table;
924 g_assert (node->plugin != NULL);
928 type_data_ref_Wm (pnode);
930 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
933 memset (&tmp_info, 0, sizeof (tmp_info));
934 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
936 G_WRITE_UNLOCK (&type_rw_lock);
937 g_type_plugin_use (node->plugin);
938 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
939 G_WRITE_LOCK (&type_rw_lock);
941 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
943 check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
944 type_data_make_W (node, &tmp_info,
945 check_value_table_I (NODE_NAME (node),
946 &tmp_value_table) ? &tmp_value_table : NULL);
950 g_assert (node->data->common.ref_count > 0);
952 node->data->common.ref_count += 1;
957 type_data_unref_Wm (TypeNode *node,
960 g_assert (node->data && node->data->common.ref_count);
962 if (node->data->common.ref_count > 1)
963 node->data->common.ref_count -= 1;
968 g_warning ("static type `%s' unreferenced too often",
973 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
978 type_node_add_iface_entry_W (TypeNode *node,
984 g_assert (node->is_instantiatable && node->n_ifaces < MAX_N_IFACES);
987 node->private.iface_entries = g_renew (IFaceEntry, node->private.iface_entries, node->n_ifaces);
988 entries = node->private.iface_entries;
989 for (i = 0; i < node->n_ifaces - 1; i++)
990 if (entries[i].iface_type > iface_type)
992 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (node->n_ifaces - i - 1));
993 entries[i].iface_type = iface_type;
994 entries[i].vtable = NULL;
996 for (i = 0; i < node->n_children; i++)
997 type_node_add_iface_entry_W (lookup_type_node_L (node->children[i]), iface_type);
1001 type_add_interface_W (TypeNode *node,
1003 const GInterfaceInfo *info,
1004 GTypePlugin *plugin)
1006 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1008 /* we must not call any functions of GInterfaceInfo from within here, since
1009 * we got most probably called from _within_ a type registration function
1011 g_assert (node->is_instantiatable && iface->is_iface && ((info && !plugin) || (!info && plugin)));
1013 iholder->next = iface->private.iface_conformants;
1014 iface->private.iface_conformants = iholder;
1015 iholder->instance_type = NODE_TYPE (node);
1016 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1017 iholder->plugin = plugin;
1019 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1023 type_iface_retrive_holder_info_Wm (TypeNode *iface,
1024 GType instance_type)
1026 IFaceHolder *iholder = iface->private.iface_conformants;
1028 g_assert (iface->is_iface);
1030 while (iholder->instance_type != instance_type)
1031 iholder = iholder->next;
1035 GInterfaceInfo tmp_info;
1037 g_assert (iholder->plugin != NULL);
1039 type_data_ref_Wm (iface);
1041 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1043 memset (&tmp_info, 0, sizeof (tmp_info));
1045 G_WRITE_UNLOCK (&type_rw_lock);
1046 g_type_plugin_use (iholder->plugin);
1047 g_type_plugin_complete_interface_info (iholder->plugin, NODE_TYPE (iface), instance_type, &tmp_info);
1048 G_WRITE_LOCK (&type_rw_lock);
1050 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1052 check_interface_info_L (iface, instance_type, &tmp_info);
1053 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1060 type_iface_blow_holder_info_Wm (TypeNode *iface,
1061 GType instance_type)
1063 IFaceHolder *iholder = iface->private.iface_conformants;
1065 g_assert (iface->is_iface);
1067 while (iholder->instance_type != instance_type)
1068 iholder = iholder->next;
1070 if (iholder->info && iholder->plugin)
1072 g_free (iholder->info);
1073 iholder->info = NULL;
1075 G_WRITE_UNLOCK (&type_rw_lock);
1076 g_type_plugin_unuse (iholder->plugin);
1077 G_WRITE_LOCK (&type_rw_lock);
1079 type_data_unref_Wm (iface, FALSE);
1084 /* --- type structure creation/destruction --- */
1086 g_type_create_instance (GType type)
1089 GTypeInstance *instance;
1093 G_READ_LOCK (&type_rw_lock);
1094 node = lookup_type_node_L (type);
1095 G_READ_UNLOCK (&type_rw_lock);
1096 if (!node || !node->is_instantiatable)
1098 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1099 type_descriptive_name_U (type));
1102 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1103 if (G_TYPE_IS_ABSTRACT (type))
1105 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1106 type_descriptive_name_U (type));
1110 class = g_type_class_ref (type);
1112 if (node->data->instance.n_preallocs)
1114 G_WRITE_LOCK (&type_rw_lock);
1115 if (!node->data->instance.mem_chunk)
1116 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1117 node->data->instance.instance_size,
1118 (node->data->instance.instance_size *
1119 node->data->instance.n_preallocs),
1121 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1122 G_WRITE_UNLOCK (&type_rw_lock);
1125 instance = g_malloc0 (node->data->instance.instance_size); /* fine without read lock */
1126 for (i = node->n_supers; i > 0; i--)
1130 G_READ_LOCK (&type_rw_lock);
1131 pnode = lookup_type_node_L (node->supers[i]);
1132 G_READ_UNLOCK (&type_rw_lock);
1134 if (pnode->data->instance.instance_init)
1136 instance->g_class = pnode->data->instance.class;
1137 pnode->data->instance.instance_init (instance, class);
1140 instance->g_class = class;
1142 if (node->data->instance.instance_init)
1143 node->data->instance.instance_init (instance, class);
1149 g_type_free_instance (GTypeInstance *instance)
1154 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1156 G_READ_LOCK (&type_rw_lock);
1157 class = instance->g_class;
1158 node = lookup_type_node_L (class->g_type);
1159 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1161 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1162 type_descriptive_name_L (class->g_type));
1163 G_READ_UNLOCK (&type_rw_lock);
1166 G_READ_UNLOCK (&type_rw_lock);
1167 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1168 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1170 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1175 instance->g_class = NULL;
1176 memset (instance, 0xaa, node->data->instance.instance_size); // FIXME: debugging hack
1177 if (node->data->instance.n_preallocs)
1179 G_WRITE_LOCK (&type_rw_lock);
1180 g_chunk_free (instance, node->data->instance.mem_chunk);
1181 G_WRITE_UNLOCK (&type_rw_lock);
1186 g_type_class_unref (class);
1190 type_propagate_iface_vtable_W (TypeNode *pnode,
1192 GTypeInterface *vtable)
1194 IFaceEntry *entry = type_lookup_iface_entry_L (pnode, iface);
1197 entry->vtable = vtable;
1198 for (i = 0; i < pnode->n_children; i++)
1200 TypeNode *node = lookup_type_node_L (pnode->children[i]);
1202 type_propagate_iface_vtable_W (node, iface, vtable);
1207 type_iface_vtable_init_Wm (TypeNode *iface,
1210 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1211 IFaceHolder *iholder = type_iface_retrive_holder_info_Wm (iface, NODE_TYPE (node));
1212 GTypeInterface *vtable;
1214 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1216 vtable = g_malloc0 (iface->data->iface.vtable_size);
1217 type_propagate_iface_vtable_W (node, iface, vtable);
1218 vtable->g_type = NODE_TYPE (iface);
1219 vtable->g_instance_type = NODE_TYPE (node);
1221 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1223 G_WRITE_UNLOCK (&type_rw_lock);
1224 if (iface->data->iface.vtable_init_base)
1225 iface->data->iface.vtable_init_base (vtable);
1226 if (iholder->info->interface_init)
1227 iholder->info->interface_init (vtable, iholder->info->interface_data);
1228 G_WRITE_LOCK (&type_rw_lock);
1233 type_iface_vtable_finalize_Wm (TypeNode *iface,
1235 GTypeInterface *vtable)
1237 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1238 IFaceHolder *iholder = iface->private.iface_conformants;
1240 g_assert (entry && entry->vtable == vtable);
1242 while (iholder->instance_type != NODE_TYPE (node))
1243 iholder = iholder->next;
1244 g_assert (iholder && iholder->info);
1246 type_propagate_iface_vtable_W (node, iface, NULL);
1247 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1249 G_WRITE_UNLOCK (&type_rw_lock);
1250 if (iholder->info->interface_finalize)
1251 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1252 if (iface->data->iface.vtable_finalize_base)
1253 iface->data->iface.vtable_finalize_base (vtable);
1254 G_WRITE_LOCK (&type_rw_lock);
1257 vtable->g_instance_type = 0;
1260 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1264 type_class_init_Wm (TypeNode *node,
1267 GSList *slist, *init_slist = NULL;
1273 g_assert (node->is_classed && node->data &&
1274 node->data->class.class_size &&
1275 !node->data->class.class);
1277 class = g_malloc0 (node->data->class.class_size);
1278 node->data->class.class = class;
1282 TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1284 memcpy (class, pclass, pnode->data->class.class_size);
1286 class->g_type = NODE_TYPE (node);
1288 G_WRITE_UNLOCK (&type_rw_lock);
1290 /* stack all base class initialization functions, so we
1291 * call them in ascending order.
1293 G_READ_LOCK (&type_rw_lock);
1294 for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1295 if (bnode->data->class.class_init_base)
1296 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1297 G_READ_UNLOCK (&type_rw_lock);
1298 for (slist = init_slist; slist; slist = slist->next)
1300 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1302 class_init_base (class);
1304 g_slist_free (init_slist);
1306 if (node->data->class.class_init)
1307 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1309 G_WRITE_LOCK (&type_rw_lock);
1311 /* ok, we got the class done, now initialize all interfaces */
1312 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1313 if (!node->private.iface_entries[i].vtable)
1314 entry = node->private.iface_entries + i;
1317 type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node);
1319 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1320 if (!node->private.iface_entries[i].vtable)
1321 entry = node->private.iface_entries + i;
1326 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1331 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1333 g_message ("finalizing interfaces for %sClass `%s'",
1334 type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1335 type_descriptive_name_L (NODE_TYPE (node)));
1337 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1338 if (node->private.iface_entries[i].vtable &&
1339 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1340 entry = node->private.iface_entries + i;
1343 type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable);
1345 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1346 if (node->private.iface_entries[i].vtable &&
1347 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1348 entry = node->private.iface_entries + i;
1353 type_data_finalize_class_U (TypeNode *node,
1356 GTypeClass *class = cdata->class;
1359 g_assert (cdata->class && cdata->common.ref_count == 0);
1361 if (cdata->class_finalize)
1362 cdata->class_finalize (class, (gpointer) cdata->class_data);
1364 /* call all base class destruction functions in descending order
1366 if (cdata->class_finalize_base)
1367 cdata->class_finalize_base (class);
1368 G_READ_LOCK (&type_rw_lock);
1369 for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1370 if (bnode->data->class.class_finalize_base)
1372 G_READ_UNLOCK (&type_rw_lock);
1373 bnode->data->class.class_finalize_base (class);
1374 G_READ_LOCK (&type_rw_lock);
1376 G_READ_UNLOCK (&type_rw_lock);
1379 g_free (cdata->class);
1383 type_data_last_unref_Wm (GType type,
1386 TypeNode *node = lookup_type_node_L (type);
1388 g_return_if_fail (node != NULL && node->plugin != NULL);
1390 if (!node->data || node->data->common.ref_count == 0)
1392 g_warning ("cannot drop last reference to unreferenced type `%s'",
1393 type_descriptive_name_U (type));
1397 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1401 G_WRITE_UNLOCK (&type_rw_lock);
1402 G_READ_LOCK (&type_rw_lock);
1403 for (i = 0; i < static_n_class_cache_funcs; i++)
1405 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1406 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1407 gboolean need_break;
1409 G_READ_UNLOCK (&type_rw_lock);
1410 need_break = cache_func (cache_data, node->data->class.class);
1411 G_READ_LOCK (&type_rw_lock);
1412 if (!node->data || node->data->common.ref_count == 0)
1413 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1417 G_READ_UNLOCK (&type_rw_lock);
1418 G_WRITE_LOCK (&type_rw_lock);
1421 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1422 node->data->common.ref_count -= 1;
1425 GType ptype = NODE_PARENT_TYPE (node);
1428 node->data->common.ref_count = 0;
1430 if (node->is_instantiatable && node->data->instance.mem_chunk)
1432 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1433 node->data->instance.mem_chunk = NULL;
1437 if (node->is_classed && tdata->class.class)
1440 type_data_finalize_class_ifaces_Wm (node);
1442 G_WRITE_UNLOCK (&type_rw_lock);
1443 type_data_finalize_class_U (node, &tdata->class);
1444 G_WRITE_LOCK (&type_rw_lock);
1449 /* freeing tdata->common.value_table and its contents is taking care of
1450 * by allocating it in one chunk with tdata
1455 type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1456 G_WRITE_UNLOCK (&type_rw_lock);
1457 g_type_plugin_unuse (node->plugin);
1458 G_WRITE_LOCK (&type_rw_lock);
1463 g_type_add_class_cache_func (gpointer cache_data,
1464 GTypeClassCacheFunc cache_func)
1468 g_return_if_fail (cache_func != NULL);
1470 G_WRITE_LOCK (&type_rw_lock);
1471 i = static_n_class_cache_funcs++;
1472 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1473 static_class_cache_funcs[i].cache_data = cache_data;
1474 static_class_cache_funcs[i].cache_func = cache_func;
1475 G_WRITE_UNLOCK (&type_rw_lock);
1479 g_type_remove_class_cache_func (gpointer cache_data,
1480 GTypeClassCacheFunc cache_func)
1482 gboolean found_it = FALSE;
1485 g_return_if_fail (cache_func != NULL);
1487 G_WRITE_LOCK (&type_rw_lock);
1488 for (i = 0; i < static_n_class_cache_funcs; i++)
1489 if (static_class_cache_funcs[i].cache_data == cache_data &&
1490 static_class_cache_funcs[i].cache_func == cache_func)
1492 static_n_class_cache_funcs--;
1493 g_memmove (static_class_cache_funcs + i,
1494 static_class_cache_funcs + i + 1,
1495 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1496 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1500 G_WRITE_UNLOCK (&type_rw_lock);
1503 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1504 cache_func, cache_data);
1508 /* --- type registration --- */
1510 g_type_register_fundamental (GType type_id,
1511 const gchar *type_name,
1512 const GTypeInfo *info,
1513 const GTypeFundamentalInfo *finfo,
1516 GTypeFundamentalInfo *node_finfo;
1519 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1520 g_return_val_if_fail (type_id > 0, 0);
1521 g_return_val_if_fail (type_name != NULL, 0);
1522 g_return_val_if_fail (info != NULL, 0);
1523 g_return_val_if_fail (finfo != NULL, 0);
1525 if (!check_type_name_U (type_name))
1527 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1529 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1534 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1535 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1537 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1541 G_WRITE_LOCK (&type_rw_lock);
1542 if (lookup_type_node_L (type_id))
1544 G_WRITE_UNLOCK (&type_rw_lock);
1545 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1546 type_descriptive_name_U (type_id),
1551 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1552 node_finfo = type_node_fundamental_info_L (node);
1553 type_add_flags_W (node, flags);
1555 if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1556 type_data_make_W (node, info,
1557 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1558 G_WRITE_UNLOCK (&type_rw_lock);
1560 return NODE_TYPE (node);
1564 g_type_register_static (GType parent_type,
1565 const gchar *type_name,
1566 const GTypeInfo *info,
1569 TypeNode *pnode, *node;
1572 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1573 g_return_val_if_fail (parent_type > 0, 0);
1574 g_return_val_if_fail (type_name != NULL, 0);
1575 g_return_val_if_fail (info != NULL, 0);
1577 if (!check_type_name_U (type_name) ||
1578 !check_derivation_U (parent_type, type_name))
1580 if (info->class_finalize)
1582 g_warning ("class finalizer specified for static type `%s'",
1587 G_WRITE_LOCK (&type_rw_lock);
1588 pnode = lookup_type_node_L (parent_type);
1589 type_data_ref_Wm (pnode);
1590 if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1592 node = type_node_new_W (pnode, type_name, NULL);
1593 type_add_flags_W (node, flags);
1594 type = NODE_TYPE (node);
1595 type_data_make_W (node, info,
1596 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1598 G_WRITE_UNLOCK (&type_rw_lock);
1604 g_type_register_dynamic (GType parent_type,
1605 const gchar *type_name,
1606 GTypePlugin *plugin,
1609 TypeNode *pnode, *node;
1612 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1613 g_return_val_if_fail (parent_type > 0, 0);
1614 g_return_val_if_fail (type_name != NULL, 0);
1615 g_return_val_if_fail (plugin != NULL, 0);
1617 if (!check_type_name_U (type_name) ||
1618 !check_derivation_U (parent_type, type_name) ||
1619 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1622 G_WRITE_LOCK (&type_rw_lock);
1623 pnode = lookup_type_node_L (parent_type);
1624 node = type_node_new_W (pnode, type_name, plugin);
1625 type_add_flags_W (node, flags);
1626 type = NODE_TYPE (node);
1627 G_WRITE_UNLOCK (&type_rw_lock);
1633 g_type_add_interface_static (GType instance_type,
1634 GType interface_type,
1635 const GInterfaceInfo *info)
1637 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1638 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1639 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1641 G_WRITE_LOCK (&type_rw_lock);
1642 if (check_add_interface_L (instance_type, interface_type))
1644 TypeNode *node = lookup_type_node_L (instance_type);
1645 TypeNode *iface = lookup_type_node_L (interface_type);
1647 if (check_interface_info_L (iface, NODE_TYPE (node), info))
1648 type_add_interface_W (node, iface, info, NULL);
1650 G_WRITE_UNLOCK (&type_rw_lock);
1654 g_type_add_interface_dynamic (GType instance_type,
1655 GType interface_type,
1656 GTypePlugin *plugin)
1660 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1661 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1662 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1664 G_READ_LOCK (&type_rw_lock);
1665 node = lookup_type_node_L (instance_type);
1666 G_READ_UNLOCK (&type_rw_lock);
1667 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1670 G_WRITE_LOCK (&type_rw_lock);
1671 if (check_add_interface_L (instance_type, interface_type))
1673 TypeNode *iface = lookup_type_node_L (interface_type);
1675 type_add_interface_W (node, iface, NULL, plugin);
1677 G_WRITE_UNLOCK (&type_rw_lock);
1681 /* --- public API functions --- */
1683 g_type_class_ref (GType type)
1687 /* optimize for common code path
1689 G_WRITE_LOCK (&type_rw_lock);
1690 node = lookup_type_node_L (type);
1691 if (node && node->is_classed && node->data &&
1692 node->data->class.class && node->data->common.ref_count > 0)
1694 type_data_ref_Wm (node);
1695 G_WRITE_UNLOCK (&type_rw_lock);
1697 return node->data->class.class;
1700 if (!node || !node->is_classed ||
1701 (node->data && node->data->common.ref_count < 1))
1703 G_WRITE_UNLOCK (&type_rw_lock);
1704 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1705 type_descriptive_name_U (type));
1709 type_data_ref_Wm (node);
1711 if (!node->data->class.class)
1713 GType ptype = NODE_PARENT_TYPE (node);
1714 GTypeClass *pclass = NULL;
1718 G_WRITE_UNLOCK (&type_rw_lock);
1719 pclass = g_type_class_ref (ptype);
1720 if (node->data->class.class)
1721 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1722 G_WRITE_LOCK (&type_rw_lock);
1725 type_class_init_Wm (node, pclass);
1727 G_WRITE_UNLOCK (&type_rw_lock);
1729 return node->data->class.class;
1733 g_type_class_unref (gpointer g_class)
1736 GTypeClass *class = g_class;
1738 g_return_if_fail (g_class != NULL);
1740 G_WRITE_LOCK (&type_rw_lock);
1741 node = lookup_type_node_L (class->g_type);
1742 if (node && node->is_classed && node->data &&
1743 node->data->class.class == class && node->data->common.ref_count > 0)
1744 type_data_unref_Wm (node, FALSE);
1746 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1747 type_descriptive_name_L (class->g_type));
1748 G_WRITE_UNLOCK (&type_rw_lock);
1752 g_type_class_unref_uncached (gpointer g_class)
1755 GTypeClass *class = g_class;
1757 g_return_if_fail (g_class != NULL);
1759 G_WRITE_LOCK (&type_rw_lock);
1760 node = lookup_type_node_L (class->g_type);
1761 if (node && node->is_classed && node->data &&
1762 node->data->class.class == class && node->data->common.ref_count > 0)
1763 type_data_unref_Wm (node, TRUE);
1765 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1766 type_descriptive_name_L (class->g_type));
1767 G_WRITE_UNLOCK (&type_rw_lock);
1771 g_type_class_peek (GType type)
1776 G_READ_LOCK (&type_rw_lock);
1777 node = lookup_type_node_L (type);
1778 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1779 class = node->data->class.class;
1782 G_READ_UNLOCK (&type_rw_lock);
1788 g_type_class_peek_parent (gpointer g_class)
1793 g_return_val_if_fail (g_class != NULL, NULL);
1795 G_READ_LOCK (&type_rw_lock);
1796 node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
1797 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1799 node = lookup_type_node_L (NODE_PARENT_TYPE (node));
1800 class = node->data->class.class;
1804 G_READ_UNLOCK (&type_rw_lock);
1810 g_type_interface_peek (gpointer instance_class,
1815 gpointer vtable = NULL;
1816 GTypeClass *class = instance_class;
1818 g_return_val_if_fail (instance_class != NULL, NULL);
1820 G_READ_LOCK (&type_rw_lock);
1821 node = lookup_type_node_L (class->g_type);
1822 iface = lookup_type_node_L (iface_type);
1823 if (node && node->is_instantiatable && iface)
1825 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1827 if (entry && entry->vtable)
1828 vtable = entry->vtable;
1830 G_READ_UNLOCK (&type_rw_lock);
1835 G_CONST_RETURN gchar*
1836 g_type_name (GType type)
1840 g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, NULL);
1842 G_READ_LOCK (&type_rw_lock);
1843 node = lookup_type_node_L (type);
1844 G_READ_UNLOCK (&type_rw_lock);
1846 return node ? NODE_NAME (node) : NULL;
1850 g_type_qname (GType type)
1854 G_READ_LOCK (&type_rw_lock);
1855 node = lookup_type_node_L (type);
1856 G_READ_UNLOCK (&type_rw_lock);
1858 return node ? node->qname : 0;
1862 g_type_from_name (const gchar *name)
1867 g_return_val_if_fail (name != NULL, 0);
1869 quark = g_quark_try_string (name);
1872 G_READ_LOCK (&type_rw_lock);
1873 type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
1874 G_READ_UNLOCK (&type_rw_lock);
1881 g_type_parent (GType type)
1885 G_READ_LOCK (&type_rw_lock);
1886 node = lookup_type_node_L (type);
1887 G_READ_UNLOCK (&type_rw_lock);
1889 return node ? NODE_PARENT_TYPE (node) : 0;
1893 g_type_next_base (GType type,
1899 G_READ_LOCK (&type_rw_lock);
1900 node = lookup_type_node_L (type);
1903 TypeNode *base_node = lookup_type_node_L (base_type);
1905 if (base_node && base_node->n_supers < node->n_supers)
1907 guint n = node->n_supers - base_node->n_supers;
1909 if (node->supers[n] == base_type)
1910 atype = node->supers[n - 1];
1913 G_READ_UNLOCK (&type_rw_lock);
1919 g_type_is_a (GType type,
1922 gboolean is_a = FALSE;
1924 G_READ_LOCK (&type_rw_lock);
1925 if (type != iface_type)
1927 TypeNode *node = lookup_type_node_L (type);
1931 TypeNode *iface_node = lookup_type_node_L (iface_type);
1935 if (iface_node->is_iface && node->is_instantiatable)
1936 is_a = type_lookup_iface_entry_L (node, iface_node) != NULL;
1937 else if (iface_node->n_supers <= node->n_supers)
1938 is_a = node->supers[node->n_supers - iface_node->n_supers] == iface_type;
1943 is_a = lookup_type_node_L (type) != NULL;
1944 G_READ_UNLOCK (&type_rw_lock);
1950 g_type_fundamental_branch_last (GType type)
1952 GType ftype = G_TYPE_FUNDAMENTAL (type);
1955 G_READ_LOCK (&type_rw_lock);
1956 last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
1957 G_READ_UNLOCK (&type_rw_lock);
1962 GType* /* free result */
1963 g_type_children (GType type,
1968 G_READ_LOCK (&type_rw_lock);
1969 node = lookup_type_node_L (type);
1972 GType *children = g_new (GType, node->n_children + 1);
1974 memcpy (children, node->children, sizeof (GType) * node->n_children);
1975 children[node->n_children] = 0;
1978 *n_children = node->n_children;
1979 G_READ_UNLOCK (&type_rw_lock);
1985 G_READ_UNLOCK (&type_rw_lock);
1993 GType* /* free result */
1994 g_type_interfaces (GType type,
1995 guint *n_interfaces)
1999 G_READ_LOCK (&type_rw_lock);
2000 node = lookup_type_node_L (type);
2001 if (node && node->is_instantiatable)
2003 GType *ifaces = g_new (GType, node->n_ifaces + 1);
2006 for (i = 0; i < node->n_ifaces; i++)
2007 ifaces[i] = node->private.iface_entries[i].iface_type;
2011 *n_interfaces = node->n_ifaces;
2012 G_READ_UNLOCK (&type_rw_lock);
2018 G_READ_UNLOCK (&type_rw_lock);
2026 typedef struct _QData QData;
2038 static inline gpointer
2039 type_get_qdata_L (TypeNode *node,
2042 GData *gdata = node->global_gdata;
2044 if (quark && gdata && gdata->n_qdatas)
2046 QData *qdatas = gdata->qdatas - 1;
2047 guint n_qdatas = gdata->n_qdatas;
2054 i = (n_qdatas + 1) / 2;
2056 if (quark == check->quark)
2058 else if (quark > check->quark)
2063 else /* if (quark < check->quark) */
2072 g_type_get_qdata (GType type,
2078 G_READ_LOCK (&type_rw_lock);
2079 node = lookup_type_node_L (type);
2082 data = type_get_qdata_L (node, quark);
2083 G_READ_UNLOCK (&type_rw_lock);
2087 G_READ_UNLOCK (&type_rw_lock);
2088 g_return_val_if_fail (node != NULL, NULL);
2095 type_set_qdata_W (TypeNode *node,
2103 /* setup qdata list if necessary */
2104 if (!node->global_gdata)
2105 node->global_gdata = g_new0 (GData, 1);
2106 gdata = node->global_gdata;
2108 /* try resetting old data */
2109 qdata = gdata->qdatas;
2110 for (i = 0; i < gdata->n_qdatas; i++)
2111 if (qdata[i].quark == quark)
2113 qdata[i].data = data;
2119 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2120 qdata = gdata->qdatas;
2121 for (i = 0; i < gdata->n_qdatas - 1; i++)
2122 if (qdata[i].quark > quark)
2124 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2125 qdata[i].quark = quark;
2126 qdata[i].data = data;
2130 g_type_set_qdata (GType type,
2136 g_return_if_fail (quark != 0);
2138 G_WRITE_LOCK (&type_rw_lock);
2139 node = lookup_type_node_L (type);
2142 type_set_qdata_W (node, quark, data);
2143 G_WRITE_UNLOCK (&type_rw_lock);
2147 G_WRITE_UNLOCK (&type_rw_lock);
2148 g_return_if_fail (node != NULL);
2153 type_add_flags_W (TypeNode *node,
2158 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2159 g_return_if_fail (node != NULL);
2161 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2162 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2163 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2165 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2169 /* --- implementation details --- */
2171 g_type_check_flags (GType type,
2175 gboolean result = FALSE;
2177 G_READ_LOCK (&type_rw_lock);
2178 node = lookup_type_node_L (type);
2181 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2182 guint tflags = flags & TYPE_FLAG_MASK;
2186 GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2188 fflags = (finfo->type_flags & fflags) == fflags;
2194 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2198 result = tflags && fflags;
2200 G_READ_UNLOCK (&type_rw_lock);
2206 g_type_get_plugin (GType type)
2210 G_READ_LOCK (&type_rw_lock);
2211 node = lookup_type_node_L (type);
2212 G_READ_UNLOCK (&type_rw_lock);
2214 return node ? node->plugin : NULL;
2218 g_type_interface_get_plugin (GType instance_type,
2219 GType interface_type)
2224 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2226 G_READ_LOCK (&type_rw_lock);
2227 node = lookup_type_node_L (instance_type);
2228 iface = lookup_type_node_L (interface_type);
2231 IFaceHolder *iholder = iface->private.iface_conformants;
2233 while (iholder && iholder->instance_type != instance_type)
2234 iholder = iholder->next;
2235 G_READ_UNLOCK (&type_rw_lock);
2238 return iholder->plugin;
2241 G_READ_UNLOCK (&type_rw_lock);
2243 g_return_val_if_fail (node == NULL, NULL);
2244 g_return_val_if_fail (iface == NULL, NULL);
2246 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2252 g_type_fundamental_last (void)
2256 G_READ_LOCK (&type_rw_lock);
2257 type = static_last_fundamental_id;
2258 G_READ_UNLOCK (&type_rw_lock);
2264 g_type_instance_is_a (GTypeInstance *type_instance,
2267 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2268 return (type_instance && type_instance->g_class &&
2269 G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type) &&
2270 g_type_is_a (type_instance->g_class->g_type, iface_type));
2274 g_type_class_is_a (GTypeClass *type_class,
2277 /* G_TYPE_IS_CLASSED() is an external call: _U */
2278 return (type_class && G_TYPE_IS_CLASSED (type_class->g_type) &&
2279 g_type_is_a (type_class->g_type, is_a_type));
2283 g_type_check_instance_cast (GTypeInstance *type_instance,
2288 g_warning ("invalid cast from (NULL) pointer to `%s'",
2289 type_descriptive_name_U (iface_type));
2290 return type_instance;
2292 if (!type_instance->g_class)
2294 g_warning ("invalid unclassed pointer in cast to `%s'",
2295 type_descriptive_name_U (iface_type));
2296 return type_instance;
2298 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2299 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2301 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2302 type_descriptive_name_U (type_instance->g_class->g_type),
2303 type_descriptive_name_U (iface_type));
2304 return type_instance;
2306 if (!g_type_is_a (type_instance->g_class->g_type, iface_type))
2308 g_warning ("invalid cast from `%s' to `%s'",
2309 type_descriptive_name_U (type_instance->g_class->g_type),
2310 type_descriptive_name_U (iface_type));
2311 return type_instance;
2314 return type_instance;
2318 g_type_check_class_cast (GTypeClass *type_class,
2323 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2324 type_descriptive_name_U (is_a_type));
2327 /* G_TYPE_IS_CLASSED() is an external call: _U */
2328 if (!G_TYPE_IS_CLASSED (type_class->g_type))
2330 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2331 type_descriptive_name_U (type_class->g_type),
2332 type_descriptive_name_U (is_a_type));
2335 if (!g_type_is_a (type_class->g_type, is_a_type))
2337 g_warning ("invalid class cast from `%s' to `%s'",
2338 type_descriptive_name_U (type_class->g_type),
2339 type_descriptive_name_U (is_a_type));
2347 g_type_check_instance (GTypeInstance *type_instance)
2349 /* this function is just here to make the signal system
2350 * conveniently elaborated on instance checks
2354 g_warning ("instance is invalid (NULL) pointer");
2357 if (!type_instance->g_class)
2359 g_warning ("instance with invalid (NULL) class pointer");
2362 /* G_TYPE_IS_CLASSED() is an external call: _U */
2363 if (!G_TYPE_IS_CLASSED (type_instance->g_class->g_type))
2365 g_warning ("instance of invalid unclassed type `%s'",
2366 type_descriptive_name_U (type_instance->g_class->g_type));
2369 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2370 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2372 g_warning ("instance of invalid non-instantiatable type `%s'",
2373 type_descriptive_name_U (type_instance->g_class->g_type));
2380 static inline gboolean
2381 type_check_is_value_type_U (GType type)
2383 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2386 G_READ_LOCK (&type_rw_lock);
2387 node = lookup_type_node_L (type);
2388 if (node && node->data && node->data->common.ref_count > 0 &&
2389 node->data->common.value_table->value_init)
2390 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2391 G_READ_UNLOCK (&type_rw_lock);
2393 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2397 g_type_check_is_value_type (GType type)
2399 return type_check_is_value_type_U (type);
2403 g_type_check_value (GValue *value)
2405 return value && type_check_is_value_type_U (value->g_type);
2409 g_type_check_value_holds (GValue *value,
2412 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2416 g_type_value_table_peek (GType type)
2419 GTypeValueTable *vtable = NULL;
2421 G_READ_LOCK (&type_rw_lock);
2422 node = lookup_type_node_L (type);
2423 if (node && node->data && node->data->common.ref_count > 0 &&
2424 node->data->common.value_table->value_init)
2425 vtable = node->data->common.value_table;
2426 G_READ_UNLOCK (&type_rw_lock);
2432 /* --- foreign prototypes --- */
2433 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2434 extern void g_enum_types_init (void); /* sync with genums.c */
2435 extern void g_param_type_init (void); /* sync with gparam.c */
2436 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2437 extern void g_object_type_init (void); /* sync with gobject.c */
2438 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2439 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2440 extern void g_signal_init (void); /* sync with gsignal.c */
2443 /* --- initialization --- */
2445 g_type_init (GTypeDebugFlags debug_flags)
2447 G_LOCK_DEFINE_STATIC (type_init_lock);
2448 static TypeNode *type0_node = NULL;
2449 const gchar *env_string;
2454 G_LOCK (type_init_lock);
2456 G_WRITE_LOCK (&type_rw_lock);
2458 if (static_last_fundamental_id)
2460 G_WRITE_UNLOCK (&type_rw_lock);
2461 G_UNLOCK (type_init_lock);
2465 /* setup GRuntime wide debugging flags */
2466 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2467 env_string = g_getenv ("GRUNTIME_DEBUG");
2468 if (env_string != NULL)
2470 static GDebugKey debug_keys[] = {
2471 { "objects", G_TYPE_DEBUG_OBJECTS },
2472 { "signals", G_TYPE_DEBUG_SIGNALS },
2475 _g_type_debug_flags |= g_parse_debug_string (env_string,
2477 sizeof (debug_keys) / sizeof (debug_keys[0]));
2482 static_quark_type_flags = g_quark_from_static_string ("GTypeFlags");
2484 /* type qname hash table */
2485 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2487 /* invalid type G_TYPE_INVALID (0)
2489 static_last_fundamental_id = 1;
2490 static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2491 static_type_nodes[0] = &type0_node;
2492 static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2493 static_branch_seqnos[0] = 1;
2495 /* void type G_TYPE_NONE
2497 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2498 type = NODE_TYPE (node);
2499 g_assert (type == G_TYPE_NONE);
2501 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2503 memset (&info, 0, sizeof (info));
2504 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2505 type = NODE_TYPE (node);
2506 type_data_make_W (node, &info, NULL);
2507 g_assert (type == G_TYPE_INTERFACE);
2509 G_WRITE_UNLOCK (&type_rw_lock);
2511 /* G_TYPE_TYPE_PLUGIN
2513 g_type_plugin_get_type ();
2515 /* G_TYPE_* value types
2517 g_value_types_init ();
2519 /* G_TYPE_ENUM & G_TYPE_FLAGS
2521 g_enum_types_init ();
2525 g_boxed_type_init ();
2529 g_param_type_init ();
2533 g_object_type_init ();
2535 /* G_TYPE_PARAM_* pspec types
2537 g_param_spec_types_init ();
2539 /* Value Transformations
2541 g_value_transforms_init ();
2547 G_UNLOCK (type_init_lock);