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.
26 #include "gtypeplugin.h"
27 #include "gvaluecollector.h"
31 /* NOTE: some functions (some internal variants and exported ones)
32 * invalidate data portions of the TypeNodes. if external functions/callbacks
33 * are called, pointers to memory maintained by TypeNodes have to be looked up
34 * again. this affects most of the struct TypeNode fields, e.g. ->children or
35 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
36 * not ->supers[]), as all those memory portions can get realloc()ed during
37 * callback invocation.
40 * - g_type_from_name() should do an ordered array lookup after fetching the
41 * the quark, instead of a second hashtable lookup.
44 * lock handling issues when calling static functions are indicated by
45 * uppercase letter postfixes, all static functions have to have
46 * one of the below postfixes:
47 * - _I: [Indifferent about locking]
48 * function doesn't care about locks at all
49 * - _U: [Unlocked invocation]
50 * no read or write lock has to be held across function invocation
51 * (locks may be acquired and released during invocation though)
52 * - _L: [Locked invocation]
53 * a write lock or more than 0 read locks have to be held across
55 * - _W: [Write-locked invocation]
56 * a write lock has to be held across function invokation
57 * - _Wm: [Write-locked invocation, mutatable]
58 * like _W, but the write lock might be released and reacquired
59 * during invocation, watch your pointers
62 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
64 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
65 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
66 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
67 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
69 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
70 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
71 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
72 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
74 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
75 static const gchar *_action = " invalidly modified type "; \
76 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
78 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
80 g_error ("%s()%s`%s'", _fname, _action, _tname); \
82 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
85 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
86 "%s: initialization assertion failed, use %s() prior to this function", \
87 G_STRLOC, G_STRINGIFY (init_function)); \
88 return (return_value); \
93 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
94 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
97 #else /* !G_ENABLE_DEBUG */
98 #define DEBUG_CODE(debug_type, code_block) /* code_block */
99 #endif /* G_ENABLE_DEBUG */
101 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
102 G_TYPE_FLAG_INSTANTIATABLE | \
103 G_TYPE_FLAG_DERIVABLE | \
104 G_TYPE_FLAG_DEEP_DERIVABLE)
105 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
106 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
107 sizeof (gpointer)), \
110 /* The 2*sizeof(size_t) alignment here is borrowed from
111 * GNU libc, so it should be good most everywhere.
112 * It is more conservative than is needed on some 64-bit
113 * platforms, but ia64 does require a 16-byte alignment.
114 * The SIMD extensions for x86 and ppc32 would want a
115 * larger alignment than this, but we don't need to
116 * do better than malloc.
118 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
119 #define ALIGN_STRUCT(offset) \
120 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
123 /* --- typedefs --- */
124 typedef struct _TypeNode TypeNode;
125 typedef struct _CommonData CommonData;
126 typedef struct _IFaceData IFaceData;
127 typedef struct _ClassData ClassData;
128 typedef struct _InstanceData InstanceData;
129 typedef union _TypeData TypeData;
130 typedef struct _IFaceEntry IFaceEntry;
131 typedef struct _IFaceHolder IFaceHolder;
134 /* --- prototypes --- */
135 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
136 static void type_add_flags_W (TypeNode *node,
138 static void type_data_make_W (TypeNode *node,
139 const GTypeInfo *info,
140 const GTypeValueTable *value_table);
141 static inline void type_data_ref_Wm (TypeNode *node);
142 static inline void type_data_unref_Wm (TypeNode *node,
144 static void type_data_last_unref_Wm (GType type,
146 static inline gpointer type_get_qdata_L (TypeNode *node,
148 static inline void type_set_qdata_W (TypeNode *node,
151 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
152 GType instance_type);
153 static gboolean type_node_is_a_L (TypeNode *node,
154 TypeNode *iface_node);
157 /* --- structures --- */
161 guint n_children : 12;
163 guint _prot_n_ifaces_prerequisites : 9;
164 guint is_classed : 1;
165 guint is_instantiatable : 1;
166 guint mutatable_check_cache : 1; /* combines some common path checks */
168 TypeData * volatile data;
172 IFaceEntry *iface_entries; /* for !iface types */
173 GType *prerequisistes;
175 GType supers[1]; /* flexible array */
177 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
178 #define MAX_N_SUPERS (255)
179 #define MAX_N_CHILDREN (4095)
180 #define MAX_N_IFACES (511)
181 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
182 #define NODE_TYPE(node) (node->supers[0])
183 #define NODE_PARENT_TYPE(node) (node->supers[1])
184 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
185 #define NODE_NAME(node) (g_quark_to_string (node->qname))
186 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
187 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
188 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
189 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
190 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
191 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
192 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
193 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
194 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
195 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
197 #define NODE_IS_ANCESTOR(ancestor, node) \
198 ((ancestor)->n_supers <= (node)->n_supers && \
199 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
205 GInterfaceInfo *info;
212 GTypeInterface *vtable;
217 GTypeValueTable *value_table;
223 GBaseInitFunc vtable_init_base;
224 GBaseFinalizeFunc vtable_finalize_base;
230 GBaseInitFunc class_init_base;
231 GBaseFinalizeFunc class_finalize_base;
232 GClassInitFunc class_init;
233 GClassFinalizeFunc class_finalize;
234 gconstpointer class_data;
241 GBaseInitFunc class_init_base;
242 GBaseFinalizeFunc class_finalize_base;
243 GClassInitFunc class_init;
244 GClassFinalizeFunc class_finalize;
245 gconstpointer class_data;
247 guint16 instance_size;
248 guint16 private_size;
250 GInstanceInitFunc instance_init;
251 GMemChunk *mem_chunk;
258 InstanceData instance;
262 GTypeClassCacheFunc cache_func;
266 /* --- variables --- */
267 static guint static_n_class_cache_funcs = 0;
268 static ClassCacheFunc *static_class_cache_funcs = NULL;
269 static GQuark static_quark_type_flags = 0;
270 static GQuark static_quark_iface_holder = 0;
271 static GQuark static_quark_dependants_array = 0;
272 GTypeDebugFlags _g_type_debug_flags = 0;
275 /* --- type nodes --- */
276 static GHashTable *static_type_nodes_ht = NULL;
277 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { 0, };
278 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
280 static inline TypeNode*
281 lookup_type_node_I (register GType utype)
283 if (utype > G_TYPE_FUNDAMENTAL_MAX)
284 return (TypeNode*) (utype & ~TYPE_ID_MASK);
286 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
290 type_node_any_new_W (TypeNode *pnode,
294 GTypeFundamentalFlags type_flags)
299 guint i, node_size = 0;
301 n_supers = pnode ? pnode->n_supers + 1 : 0;
304 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
305 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
306 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
307 node = g_malloc0 (node_size);
308 if (!pnode) /* offset fundamental types */
310 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
311 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
317 g_assert ((type & TYPE_ID_MASK) == 0);
319 node->n_supers = n_supers;
322 node->supers[0] = type;
325 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
326 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
328 if (NODE_IS_IFACE (node))
330 IFACE_NODE_N_PREREQUISITES (node) = 0;
331 IFACE_NODE_PREREQUISITES (node) = NULL;
335 CLASSED_NODE_N_IFACES (node) = 0;
336 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
341 node->supers[0] = type;
342 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
344 node->is_classed = pnode->is_classed;
345 node->is_instantiatable = pnode->is_instantiatable;
347 if (NODE_IS_IFACE (node))
349 IFACE_NODE_N_PREREQUISITES (node) = 0;
350 IFACE_NODE_PREREQUISITES (node) = NULL;
356 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
357 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
358 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
359 CLASSED_NODE_N_IFACES (node));
360 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
361 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
364 i = pnode->n_children++;
365 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
366 pnode->children[i] = type;
369 node->plugin = plugin;
370 node->n_children = 0;
371 node->children = NULL;
373 node->qname = g_quark_from_string (name);
374 node->global_gdata = NULL;
376 g_hash_table_insert (static_type_nodes_ht,
377 GUINT_TO_POINTER (node->qname),
382 static inline GTypeFundamentalInfo*
383 type_node_fundamental_info_I (TypeNode *node)
385 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
387 if (ftype != NODE_TYPE (node))
388 node = lookup_type_node_I (ftype);
390 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
394 type_node_fundamental_new_W (GType ftype,
396 GTypeFundamentalFlags type_flags)
398 GTypeFundamentalInfo *finfo;
401 g_assert ((ftype & TYPE_ID_MASK) == 0);
402 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
404 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
405 static_fundamental_next++;
407 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
409 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
411 finfo = type_node_fundamental_info_I (node);
412 finfo->type_flags = type_flags;
418 type_node_new_W (TypeNode *pnode,
424 g_assert (pnode->n_supers < MAX_N_SUPERS);
425 g_assert (pnode->n_children < MAX_N_CHILDREN);
427 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
430 static inline IFaceEntry*
431 type_lookup_iface_entry_L (TypeNode *node,
432 TypeNode *iface_node)
434 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
436 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
437 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
438 GType iface_type = NODE_TYPE (iface_node);
445 i = (n_ifaces + 1) >> 1;
447 if (iface_type == check->iface_type)
449 else if (iface_type > check->iface_type)
454 else /* if (iface_type < check->iface_type) */
463 static inline gboolean
464 type_lookup_prerequisite_L (TypeNode *iface,
465 GType prerequisite_type)
467 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
469 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
470 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
477 i = (n_prerequisites + 1) >> 1;
478 check = prerequisites + i;
479 if (prerequisite_type == *check)
481 else if (prerequisite_type > *check)
483 n_prerequisites -= i;
484 prerequisites = check;
486 else /* if (prerequisite_type < *check) */
487 n_prerequisites = i - 1;
489 while (n_prerequisites);
495 type_descriptive_name_I (GType type)
499 TypeNode *node = lookup_type_node_I (type);
501 return node ? NODE_NAME (node) : "<unknown>";
508 /* --- type consistency checks --- */
510 check_plugin_U (GTypePlugin *plugin,
511 gboolean need_complete_type_info,
512 gboolean need_complete_interface_info,
513 const gchar *type_name)
515 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
519 g_warning ("plugin handle for type `%s' is NULL",
523 if (!G_IS_TYPE_PLUGIN (plugin))
525 g_warning ("plugin pointer (%p) for type `%s' is invalid",
529 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
531 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
535 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
537 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
545 check_type_name_I (const gchar *type_name)
547 static const gchar *extra_chars = "-_+";
548 const gchar *p = type_name;
551 if (!type_name[0] || !type_name[1] || !type_name[2])
553 g_warning ("type name `%s' is too short", type_name);
556 /* check the first letter */
557 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
558 for (p = type_name + 1; *p; p++)
559 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
560 (p[0] >= 'a' && p[0] <= 'z') ||
561 (p[0] >= '0' && p[0] <= '9') ||
562 strchr (extra_chars, p[0]));
565 g_warning ("type name `%s' contains invalid characters", type_name);
568 if (g_type_from_name (type_name))
570 g_warning ("cannot register existing type `%s'", type_name);
578 check_derivation_I (GType parent_type,
579 const gchar *type_name)
582 GTypeFundamentalInfo* finfo;
584 pnode = lookup_type_node_I (parent_type);
587 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
589 type_descriptive_name_I (parent_type));
592 finfo = type_node_fundamental_info_I (pnode);
593 /* ensure flat derivability */
594 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
596 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
601 /* ensure deep derivability */
602 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
603 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
605 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
615 check_collect_format_I (const gchar *collect_format)
617 const gchar *p = collect_format;
618 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
619 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
620 G_VALUE_COLLECT_POINTER, 0 };
623 if (!strchr (valid_format, *p++))
625 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
629 check_value_table_I (const gchar *type_name,
630 const GTypeValueTable *value_table)
634 else if (value_table->value_init == NULL)
636 if (value_table->value_free || value_table->value_copy ||
637 value_table->value_peek_pointer ||
638 value_table->collect_format || value_table->collect_value ||
639 value_table->lcopy_format || value_table->lcopy_value)
640 g_warning ("cannot handle uninitializable values of type `%s'",
644 else /* value_table->value_init != NULL */
646 if (!value_table->value_free)
649 * g_warning ("missing `value_free()' for type `%s'", type_name);
653 if (!value_table->value_copy)
655 g_warning ("missing `value_copy()' for type `%s'", type_name);
658 if ((value_table->collect_format || value_table->collect_value) &&
659 (!value_table->collect_format || !value_table->collect_value))
661 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
665 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
667 g_warning ("the `%s' specification for type `%s' is too long or invalid",
672 if ((value_table->lcopy_format || value_table->lcopy_value) &&
673 (!value_table->lcopy_format || !value_table->lcopy_value))
675 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
679 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
681 g_warning ("the `%s' specification for type `%s' is too long or invalid",
691 check_type_info_I (TypeNode *pnode,
693 const gchar *type_name,
694 const GTypeInfo *info)
696 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
697 gboolean is_interface = ftype == G_TYPE_INTERFACE;
699 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
701 /* check instance members */
702 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
703 (info->instance_size || info->n_preallocs || info->instance_init))
706 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
710 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
714 /* check class & interface members */
715 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
716 (info->class_init || info->class_finalize || info->class_data ||
717 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
720 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
724 g_warning ("cannot create class for `%s' as non-classed fundamental",
728 /* check interface size */
729 if (is_interface && info->class_size < sizeof (GTypeInterface))
731 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
735 /* check class size */
736 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
738 if (info->class_size < sizeof (GTypeClass))
740 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
744 if (pnode && info->class_size < pnode->data->class.class_size)
746 g_warning ("specified class size for type `%s' is smaller "
747 "than the parent type's `%s' class size",
753 /* check instance size */
754 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
756 if (info->instance_size < sizeof (GTypeInstance))
758 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
762 if (pnode && info->instance_size < pnode->data->instance.instance_size)
764 g_warning ("specified instance size for type `%s' is smaller "
765 "than the parent type's `%s' instance size",
776 find_conforming_child_type_L (TypeNode *pnode,
779 TypeNode *node = NULL;
782 if (type_lookup_iface_entry_L (pnode, iface))
785 for (i = 0; i < pnode->n_children && !node; i++)
786 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
792 check_add_interface_L (GType instance_type,
795 TypeNode *node = lookup_type_node_I (instance_type);
796 TypeNode *iface = lookup_type_node_I (iface_type);
799 GType *prerequisites;
803 if (!node || !node->is_instantiatable)
805 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
806 type_descriptive_name_I (instance_type));
809 if (!iface || !NODE_IS_IFACE (iface))
811 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
812 type_descriptive_name_I (iface_type),
816 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
817 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
819 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
820 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
826 /* allow overriding of interface type introduced for parent type */
827 entry = type_lookup_iface_entry_L (node, iface);
828 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
830 /* ok, we do conform to this interface already, but the interface vtable was not
831 * yet intialized, and we just conform to the interface because it got added to
832 * one of our parents. so we allow overriding of holder info here.
836 /* check whether one of our children already conforms (or whether the interface
837 * got added to this node already)
839 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
842 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
848 prerequisites = IFACE_NODE_PREREQUISITES (iface);
849 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
851 tnode = lookup_type_node_I (prerequisites[i]);
852 if (!type_node_is_a_L (node, tnode))
854 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
865 check_interface_info_I (TypeNode *iface,
867 const GInterfaceInfo *info)
869 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
871 g_warning ("interface type `%s' for type `%s' comes without initializer",
873 type_descriptive_name_I (instance_type));
880 /* --- type info (type node data) --- */
882 type_data_make_W (TypeNode *node,
883 const GTypeInfo *info,
884 const GTypeValueTable *value_table)
887 GTypeValueTable *vtable = NULL;
888 guint vtable_size = 0;
890 g_assert (node->data == NULL && info != NULL);
894 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
897 vtable = pnode->data->common.value_table;
900 static const GTypeValueTable zero_vtable = { NULL, };
902 value_table = &zero_vtable;
907 /* need to setup vtable_size since we have to allocate it with data in one chunk */
908 vtable_size = sizeof (GTypeValueTable);
909 if (value_table->collect_format)
910 vtable_size += strlen (value_table->collect_format);
911 if (value_table->lcopy_format)
912 vtable_size += strlen (value_table->lcopy_format);
916 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
918 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
920 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
921 data->instance.class_size = info->class_size;
922 data->instance.class_init_base = info->base_init;
923 data->instance.class_finalize_base = info->base_finalize;
924 data->instance.class_init = info->class_init;
925 data->instance.class_finalize = info->class_finalize;
926 data->instance.class_data = info->class_data;
927 data->instance.class = NULL;
928 data->instance.instance_size = info->instance_size;
929 /* We'll set the final value for data->instance.private size
930 * after the parent class has been initialized
932 data->instance.private_size = 0;
933 #ifdef DISABLE_MEM_POOLS
934 data->instance.n_preallocs = 0;
935 #else /* !DISABLE_MEM_POOLS */
936 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
937 #endif /* !DISABLE_MEM_POOLS */
938 data->instance.instance_init = info->instance_init;
939 data->instance.mem_chunk = NULL;
941 else if (node->is_classed) /* only classed */
943 data = g_malloc0 (sizeof (ClassData) + vtable_size);
945 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
946 data->class.class_size = info->class_size;
947 data->class.class_init_base = info->base_init;
948 data->class.class_finalize_base = info->base_finalize;
949 data->class.class_init = info->class_init;
950 data->class.class_finalize = info->class_finalize;
951 data->class.class_data = info->class_data;
952 data->class.class = NULL;
954 else if (NODE_IS_IFACE (node))
956 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
958 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
959 data->iface.vtable_size = info->class_size;
960 data->iface.vtable_init_base = info->base_init;
961 data->iface.vtable_finalize_base = info->base_finalize;
965 data = g_malloc0 (sizeof (CommonData) + vtable_size);
967 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
971 node->data->common.ref_count = 1;
977 /* we allocate the vtable and its strings together with the type data, so
978 * children can take over their parent's vtable pointer, and we don't
979 * need to worry freeing it or not when the child data is destroyed
981 *vtable = *value_table;
982 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
984 vtable->collect_format = p;
985 if (value_table->collect_format)
987 strcat (p, value_table->collect_format);
988 p += strlen (value_table->collect_format);
992 vtable->lcopy_format = p;
993 if (value_table->lcopy_format)
994 strcat (p, value_table->lcopy_format);
996 node->data->common.value_table = vtable;
997 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
998 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
999 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1001 g_assert (node->data->common.value_table != NULL); /* paranoid */
1005 type_data_ref_Wm (TypeNode *node)
1009 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1011 GTypeValueTable tmp_value_table;
1013 g_assert (node->plugin != NULL);
1017 type_data_ref_Wm (pnode);
1019 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1022 memset (&tmp_info, 0, sizeof (tmp_info));
1023 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1025 G_WRITE_UNLOCK (&type_rw_lock);
1026 g_type_plugin_use (node->plugin);
1027 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1028 G_WRITE_LOCK (&type_rw_lock);
1030 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1032 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1033 type_data_make_W (node, &tmp_info,
1034 check_value_table_I (NODE_NAME (node),
1035 &tmp_value_table) ? &tmp_value_table : NULL);
1039 g_assert (node->data->common.ref_count > 0);
1041 node->data->common.ref_count += 1;
1046 type_data_unref_Wm (TypeNode *node,
1049 g_assert (node->data && node->data->common.ref_count);
1051 if (node->data->common.ref_count > 1)
1052 node->data->common.ref_count -= 1;
1057 g_warning ("static type `%s' unreferenced too often",
1062 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1067 type_node_add_iface_entry_W (TypeNode *node,
1070 IFaceEntry *entries;
1073 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1075 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1076 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1077 if (entries[i].iface_type == iface_type)
1079 /* this can (should) only happen if our parent type already conformed
1080 * to iface_type and node got it's own holder info. here, our
1081 * children should already have entries with NULL vtables, so
1082 * we're actually done.
1084 g_assert (entries[i].vtable == NULL);
1087 else if (entries[i].iface_type > iface_type)
1089 CLASSED_NODE_N_IFACES (node) += 1;
1090 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1091 CLASSED_NODE_IFACES_ENTRIES (node),
1092 CLASSED_NODE_N_IFACES (node));
1093 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1094 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1095 entries[i].iface_type = iface_type;
1096 entries[i].vtable = NULL;
1098 for (i = 0; i < node->n_children; i++)
1099 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type);
1103 type_add_interface_W (TypeNode *node,
1105 const GInterfaceInfo *info,
1106 GTypePlugin *plugin)
1108 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1110 /* we must not call any functions of GInterfaceInfo from within here, since
1111 * we got most probably called from _within_ a type registration function
1113 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1115 iholder->next = iface_node_get_holders_L (iface);
1116 iface_node_set_holders_W (iface, iholder);
1117 iholder->instance_type = NODE_TYPE (node);
1118 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1119 iholder->plugin = plugin;
1121 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1125 type_iface_add_prerequisite_W (TypeNode *iface,
1126 TypeNode *prerequisite_node)
1128 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1129 GType *prerequisites, *dependants;
1130 guint n_dependants, i;
1132 g_assert (NODE_IS_IFACE (iface) &&
1133 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1134 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1136 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1137 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1138 if (prerequisites[i] == prerequisite_type)
1139 return; /* we already have that prerequisiste */
1140 else if (prerequisites[i] > prerequisite_type)
1142 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1143 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1144 IFACE_NODE_PREREQUISITES (iface),
1145 IFACE_NODE_N_PREREQUISITES (iface));
1146 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1147 g_memmove (prerequisites + i + 1, prerequisites + i,
1148 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1149 prerequisites[i] = prerequisite_type;
1151 /* we want to get notified when prerequisites get added to prerequisite_node */
1152 if (NODE_IS_IFACE (prerequisite_node))
1154 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1155 n_dependants = dependants ? dependants[0] : 0;
1157 dependants = g_renew (GType, dependants, n_dependants + 1);
1158 dependants[n_dependants] = NODE_TYPE (iface);
1159 dependants[0] = n_dependants;
1160 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1163 /* we need to notify all dependants */
1164 dependants = iface_node_get_dependants_array_L (iface);
1165 n_dependants = dependants ? dependants[0] : 0;
1166 for (i = 1; i <= n_dependants; i++)
1167 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1171 g_type_interface_add_prerequisite (GType interface_type,
1172 GType prerequisite_type)
1174 TypeNode *iface, *prerequisite_node;
1175 IFaceHolder *holders;
1177 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1178 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1179 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1181 iface = lookup_type_node_I (interface_type);
1182 prerequisite_node = lookup_type_node_I (prerequisite_type);
1183 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1185 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1186 type_descriptive_name_I (interface_type),
1187 type_descriptive_name_I (prerequisite_type));
1190 G_WRITE_LOCK (&type_rw_lock);
1191 holders = iface_node_get_holders_L (iface);
1194 G_WRITE_UNLOCK (&type_rw_lock);
1195 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1196 type_descriptive_name_I (prerequisite_type),
1197 type_descriptive_name_I (interface_type),
1198 type_descriptive_name_I (holders->instance_type));
1201 if (prerequisite_node->is_instantiatable)
1205 /* can have at most one publically installable instantiatable prerequisite */
1206 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1208 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1210 if (prnode->is_instantiatable)
1212 G_WRITE_UNLOCK (&type_rw_lock);
1213 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1214 type_descriptive_name_I (prerequisite_type),
1215 type_descriptive_name_I (interface_type),
1216 type_descriptive_name_I (NODE_TYPE (prnode)));
1221 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1222 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1223 G_WRITE_UNLOCK (&type_rw_lock);
1225 else if (NODE_IS_IFACE (prerequisite_node))
1227 GType *prerequisites;
1230 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1231 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1232 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1233 type_iface_add_prerequisite_W (iface, prerequisite_node);
1234 G_WRITE_UNLOCK (&type_rw_lock);
1238 G_WRITE_UNLOCK (&type_rw_lock);
1239 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1240 type_descriptive_name_I (prerequisite_type),
1241 type_descriptive_name_I (interface_type));
1245 GType* /* free result */
1246 g_type_interface_prerequisites (GType interface_type,
1247 guint *n_prerequisites)
1251 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1253 iface = lookup_type_node_I (interface_type);
1257 TypeNode *inode = NULL;
1260 G_READ_LOCK (&type_rw_lock);
1261 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1262 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1264 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1265 TypeNode *node = lookup_type_node_I (prerequisite);
1266 if (node->is_instantiatable &&
1267 (!inode || type_node_is_a_L (node, inode)))
1270 types[n++] = NODE_TYPE (node);
1273 types[n++] = NODE_TYPE (inode);
1275 if (n_prerequisites)
1276 *n_prerequisites = n;
1277 G_READ_UNLOCK (&type_rw_lock);
1283 if (n_prerequisites)
1284 *n_prerequisites = 0;
1292 type_iface_peek_holder_L (TypeNode *iface,
1293 GType instance_type)
1295 IFaceHolder *iholder;
1297 g_assert (NODE_IS_IFACE (iface));
1299 iholder = iface_node_get_holders_L (iface);
1300 while (iholder && iholder->instance_type != instance_type)
1301 iholder = iholder->next;
1306 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1307 GType instance_type,
1310 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1312 if (iholder && !iholder->info && need_info)
1314 GInterfaceInfo tmp_info;
1316 g_assert (iholder->plugin != NULL);
1318 type_data_ref_Wm (iface);
1320 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1322 memset (&tmp_info, 0, sizeof (tmp_info));
1324 G_WRITE_UNLOCK (&type_rw_lock);
1325 g_type_plugin_use (iholder->plugin);
1326 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1327 G_WRITE_LOCK (&type_rw_lock);
1329 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1331 check_interface_info_I (iface, instance_type, &tmp_info);
1332 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1335 return iholder; /* we don't modify write lock upon returning NULL */
1339 type_iface_blow_holder_info_Wm (TypeNode *iface,
1340 GType instance_type)
1342 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1344 g_assert (NODE_IS_IFACE (iface));
1346 while (iholder->instance_type != instance_type)
1347 iholder = iholder->next;
1349 if (iholder->info && iholder->plugin)
1351 g_free (iholder->info);
1352 iholder->info = NULL;
1354 G_WRITE_UNLOCK (&type_rw_lock);
1355 g_type_plugin_unuse (iholder->plugin);
1356 G_WRITE_LOCK (&type_rw_lock);
1358 type_data_unref_Wm (iface, FALSE);
1362 /* Assumes type's class already exists
1364 static inline size_t
1365 type_total_instance_size_I (TypeNode *node)
1367 gsize total_instance_size;
1369 total_instance_size = node->data->instance.instance_size;
1370 if (node->data->instance.private_size != 0)
1371 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1373 return total_instance_size;
1376 /* --- type structure creation/destruction --- */
1378 g_type_create_instance (GType type)
1381 GTypeInstance *instance;
1384 gsize total_instance_size;
1386 node = lookup_type_node_I (type);
1387 if (!node || !node->is_instantiatable)
1389 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1390 type_descriptive_name_I (type));
1393 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1394 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1396 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1397 type_descriptive_name_I (type));
1401 class = g_type_class_ref (type);
1403 total_instance_size = type_total_instance_size_I (node);
1405 if (node->data->instance.n_preallocs)
1407 G_WRITE_LOCK (&type_rw_lock);
1408 if (!node->data->instance.mem_chunk)
1410 /* If there isn't private data, the compiler will have already
1411 * added the necessary padding, but in the private data case, we
1412 * have to pad ourselves to ensure proper alignment of all the
1413 * atoms in the slab.
1415 gsize atom_size = total_instance_size;
1416 if (node->data->instance.private_size)
1417 atom_size = ALIGN_STRUCT (atom_size);
1419 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1422 node->data->instance.n_preallocs),
1426 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1427 G_WRITE_UNLOCK (&type_rw_lock);
1430 instance = g_malloc0 (total_instance_size); /* fine without read lock */
1431 for (i = node->n_supers; i > 0; i--)
1435 pnode = lookup_type_node_I (node->supers[i]);
1436 if (pnode->data->instance.instance_init)
1438 instance->g_class = pnode->data->instance.class;
1439 pnode->data->instance.instance_init (instance, class);
1442 instance->g_class = class;
1444 if (node->data->instance.instance_init)
1445 node->data->instance.instance_init (instance, class);
1451 g_type_free_instance (GTypeInstance *instance)
1456 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1458 class = instance->g_class;
1459 node = lookup_type_node_I (class->g_type);
1460 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1462 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1463 type_descriptive_name_I (class->g_type));
1466 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1467 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1469 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1474 instance->g_class = NULL;
1475 #ifdef G_ENABLE_DEBUG
1476 memset (instance, 0xaa, type_total_instance_size_I (node)); /* debugging hack */
1478 if (node->data->instance.n_preallocs)
1480 G_WRITE_LOCK (&type_rw_lock);
1481 g_chunk_free (instance, node->data->instance.mem_chunk);
1482 G_WRITE_UNLOCK (&type_rw_lock);
1487 g_type_class_unref (class);
1491 type_iface_vtable_init_Wm (TypeNode *iface,
1494 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1495 IFaceHolder *iholder;
1496 GTypeInterface *vtable = NULL;
1499 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1500 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1502 return FALSE; /* we don't modify write lock upon FALSE */
1504 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1506 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1507 if (pnode) /* want to copy over parent iface contents */
1509 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1512 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1515 vtable = g_malloc0 (iface->data->iface.vtable_size);
1516 entry->vtable = vtable;
1517 vtable->g_type = NODE_TYPE (iface);
1518 vtable->g_instance_type = NODE_TYPE (node);
1520 if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1522 G_WRITE_UNLOCK (&type_rw_lock);
1523 if (iface->data->iface.vtable_init_base)
1524 iface->data->iface.vtable_init_base (vtable);
1525 if (iholder->info->interface_init)
1526 iholder->info->interface_init (vtable, iholder->info->interface_data);
1527 G_WRITE_LOCK (&type_rw_lock);
1529 return TRUE; /* write lock modified */
1533 type_iface_vtable_finalize_Wm (TypeNode *iface,
1535 GTypeInterface *vtable)
1537 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1538 IFaceHolder *iholder;
1540 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1541 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1543 return FALSE; /* we don't modify write lock upon FALSE */
1545 g_assert (entry && entry->vtable == vtable && iholder->info);
1547 entry->vtable = NULL;
1548 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1550 G_WRITE_UNLOCK (&type_rw_lock);
1551 if (iholder->info->interface_finalize)
1552 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1553 if (iface->data->iface.vtable_finalize_base)
1554 iface->data->iface.vtable_finalize_base (vtable);
1555 G_WRITE_LOCK (&type_rw_lock);
1558 vtable->g_instance_type = 0;
1561 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1563 return TRUE; /* write lock modified */
1567 type_class_init_Wm (TypeNode *node,
1570 GSList *slist, *init_slist = NULL;
1573 TypeNode *bnode, *pnode;
1576 g_assert (node->is_classed && node->data &&
1577 node->data->class.class_size &&
1578 !node->data->class.class);
1580 class = g_malloc0 (node->data->class.class_size);
1581 node->data->class.class = class;
1585 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1587 memcpy (class, pclass, pnode->data->class.class_size);
1588 /* We need to initialize the private_size here rather than in
1589 * type_data_make_W() since the class init for the parent
1590 * class may have changed pnode->data->instance.private_size.
1592 node->data->instance.private_size = pnode->data->instance.private_size;
1594 class->g_type = NODE_TYPE (node);
1596 G_WRITE_UNLOCK (&type_rw_lock);
1598 /* stack all base class initialization functions, so we
1599 * call them in ascending order.
1601 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1602 if (bnode->data->class.class_init_base)
1603 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1604 for (slist = init_slist; slist; slist = slist->next)
1606 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1608 class_init_base (class);
1610 g_slist_free (init_slist);
1612 if (node->data->class.class_init)
1613 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1615 G_WRITE_LOCK (&type_rw_lock);
1617 /* ok, we got the class done, now initialize all interfaces, either
1618 * from parent, or through our holder info
1620 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1621 entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1624 g_assert (entry->vtable == NULL);
1626 if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1630 /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1631 * need to get this interface from parent
1633 g_assert (pnode != NULL);
1635 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1637 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1639 if (pentry->iface_type == entry->iface_type)
1641 entry->vtable = pentry->vtable;
1645 g_assert (entry->vtable != NULL);
1648 /* refetch entry, IFACES_ENTRIES might be modified */
1649 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1650 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1651 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1656 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1660 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1663 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1665 IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1668 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1670 /* refetch entries, IFACES_ENTRIES might be modified */
1675 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1676 * iface vtable came from parent
1678 entry->vtable = NULL;
1685 type_data_finalize_class_U (TypeNode *node,
1688 GTypeClass *class = cdata->class;
1691 g_assert (cdata->class && cdata->common.ref_count == 0);
1693 if (cdata->class_finalize)
1694 cdata->class_finalize (class, (gpointer) cdata->class_data);
1696 /* call all base class destruction functions in descending order
1698 if (cdata->class_finalize_base)
1699 cdata->class_finalize_base (class);
1700 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1701 if (bnode->data->class.class_finalize_base)
1702 bnode->data->class.class_finalize_base (class);
1705 g_free (cdata->class);
1709 type_data_last_unref_Wm (GType type,
1712 TypeNode *node = lookup_type_node_I (type);
1714 g_return_if_fail (node != NULL && node->plugin != NULL);
1716 if (!node->data || node->data->common.ref_count == 0)
1718 g_warning ("cannot drop last reference to unreferenced type `%s'",
1719 type_descriptive_name_I (type));
1723 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1727 G_WRITE_UNLOCK (&type_rw_lock);
1728 G_READ_LOCK (&type_rw_lock);
1729 for (i = 0; i < static_n_class_cache_funcs; i++)
1731 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1732 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1733 gboolean need_break;
1735 G_READ_UNLOCK (&type_rw_lock);
1736 need_break = cache_func (cache_data, node->data->class.class);
1737 G_READ_LOCK (&type_rw_lock);
1738 if (!node->data || node->data->common.ref_count == 0)
1739 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1743 G_READ_UNLOCK (&type_rw_lock);
1744 G_WRITE_LOCK (&type_rw_lock);
1747 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1748 node->data->common.ref_count -= 1;
1751 GType ptype = NODE_PARENT_TYPE (node);
1754 node->data->common.ref_count = 0;
1756 if (node->is_instantiatable && node->data->instance.mem_chunk)
1758 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1759 node->data->instance.mem_chunk = NULL;
1763 if (node->is_classed && tdata->class.class)
1765 if (CLASSED_NODE_N_IFACES (node))
1766 type_data_finalize_class_ifaces_Wm (node);
1767 node->mutatable_check_cache = FALSE;
1769 G_WRITE_UNLOCK (&type_rw_lock);
1770 type_data_finalize_class_U (node, &tdata->class);
1771 G_WRITE_LOCK (&type_rw_lock);
1775 node->mutatable_check_cache = FALSE;
1779 /* freeing tdata->common.value_table and its contents is taking care of
1780 * by allocating it in one chunk with tdata
1785 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1786 G_WRITE_UNLOCK (&type_rw_lock);
1787 g_type_plugin_unuse (node->plugin);
1788 G_WRITE_LOCK (&type_rw_lock);
1793 g_type_add_class_cache_func (gpointer cache_data,
1794 GTypeClassCacheFunc cache_func)
1798 g_return_if_fail (cache_func != NULL);
1800 G_WRITE_LOCK (&type_rw_lock);
1801 i = static_n_class_cache_funcs++;
1802 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1803 static_class_cache_funcs[i].cache_data = cache_data;
1804 static_class_cache_funcs[i].cache_func = cache_func;
1805 G_WRITE_UNLOCK (&type_rw_lock);
1809 g_type_remove_class_cache_func (gpointer cache_data,
1810 GTypeClassCacheFunc cache_func)
1812 gboolean found_it = FALSE;
1815 g_return_if_fail (cache_func != NULL);
1817 G_WRITE_LOCK (&type_rw_lock);
1818 for (i = 0; i < static_n_class_cache_funcs; i++)
1819 if (static_class_cache_funcs[i].cache_data == cache_data &&
1820 static_class_cache_funcs[i].cache_func == cache_func)
1822 static_n_class_cache_funcs--;
1823 g_memmove (static_class_cache_funcs + i,
1824 static_class_cache_funcs + i + 1,
1825 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1826 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1830 G_WRITE_UNLOCK (&type_rw_lock);
1833 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1834 cache_func, cache_data);
1838 /* --- type registration --- */
1840 g_type_register_fundamental (GType type_id,
1841 const gchar *type_name,
1842 const GTypeInfo *info,
1843 const GTypeFundamentalInfo *finfo,
1846 GTypeFundamentalInfo *node_finfo;
1849 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1850 g_return_val_if_fail (type_id > 0, 0);
1851 g_return_val_if_fail (type_name != NULL, 0);
1852 g_return_val_if_fail (info != NULL, 0);
1853 g_return_val_if_fail (finfo != NULL, 0);
1855 if (!check_type_name_I (type_name))
1857 if ((type_id & TYPE_ID_MASK) ||
1858 type_id > G_TYPE_FUNDAMENTAL_MAX)
1860 g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
1865 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1866 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1868 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1872 if (lookup_type_node_I (type_id))
1874 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1875 type_descriptive_name_I (type_id),
1880 G_WRITE_LOCK (&type_rw_lock);
1881 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1882 node_finfo = type_node_fundamental_info_I (node);
1883 type_add_flags_W (node, flags);
1885 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1886 type_data_make_W (node, info,
1887 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1888 G_WRITE_UNLOCK (&type_rw_lock);
1890 return NODE_TYPE (node);
1894 g_type_register_static (GType parent_type,
1895 const gchar *type_name,
1896 const GTypeInfo *info,
1899 TypeNode *pnode, *node;
1902 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1903 g_return_val_if_fail (parent_type > 0, 0);
1904 g_return_val_if_fail (type_name != NULL, 0);
1905 g_return_val_if_fail (info != NULL, 0);
1907 if (!check_type_name_I (type_name) ||
1908 !check_derivation_I (parent_type, type_name))
1910 if (info->class_finalize)
1912 g_warning ("class finalizer specified for static type `%s'",
1917 pnode = lookup_type_node_I (parent_type);
1918 G_WRITE_LOCK (&type_rw_lock);
1919 type_data_ref_Wm (pnode);
1920 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1922 node = type_node_new_W (pnode, type_name, NULL);
1923 type_add_flags_W (node, flags);
1924 type = NODE_TYPE (node);
1925 type_data_make_W (node, info,
1926 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1928 G_WRITE_UNLOCK (&type_rw_lock);
1934 g_type_register_dynamic (GType parent_type,
1935 const gchar *type_name,
1936 GTypePlugin *plugin,
1939 TypeNode *pnode, *node;
1942 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1943 g_return_val_if_fail (parent_type > 0, 0);
1944 g_return_val_if_fail (type_name != NULL, 0);
1945 g_return_val_if_fail (plugin != NULL, 0);
1947 if (!check_type_name_I (type_name) ||
1948 !check_derivation_I (parent_type, type_name) ||
1949 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1952 G_WRITE_LOCK (&type_rw_lock);
1953 pnode = lookup_type_node_I (parent_type);
1954 node = type_node_new_W (pnode, type_name, plugin);
1955 type_add_flags_W (node, flags);
1956 type = NODE_TYPE (node);
1957 G_WRITE_UNLOCK (&type_rw_lock);
1963 g_type_add_interface_static (GType instance_type,
1964 GType interface_type,
1965 const GInterfaceInfo *info)
1967 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1968 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1969 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1971 G_WRITE_LOCK (&type_rw_lock);
1972 if (check_add_interface_L (instance_type, interface_type))
1974 TypeNode *node = lookup_type_node_I (instance_type);
1975 TypeNode *iface = lookup_type_node_I (interface_type);
1977 if (check_interface_info_I (iface, NODE_TYPE (node), info))
1979 type_add_interface_W (node, iface, info, NULL);
1980 /* if we have a class already, the interface vtable needs to
1981 * be initialized as well
1983 if (node->data && node->data->class.class)
1984 type_iface_vtable_init_Wm (iface, node);
1987 G_WRITE_UNLOCK (&type_rw_lock);
1991 g_type_add_interface_dynamic (GType instance_type,
1992 GType interface_type,
1993 GTypePlugin *plugin)
1997 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1998 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1999 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2001 node = lookup_type_node_I (instance_type);
2002 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2005 G_WRITE_LOCK (&type_rw_lock);
2006 if (check_add_interface_L (instance_type, interface_type))
2008 TypeNode *iface = lookup_type_node_I (interface_type);
2010 type_add_interface_W (node, iface, NULL, plugin);
2011 /* if we have a class already, the interface vtable needs to
2012 * be initialized as well
2014 if (node->data && node->data->class.class)
2015 type_iface_vtable_init_Wm (iface, node);
2017 G_WRITE_UNLOCK (&type_rw_lock);
2021 /* --- public API functions --- */
2023 g_type_class_ref (GType type)
2027 /* optimize for common code path
2029 G_WRITE_LOCK (&type_rw_lock);
2030 node = lookup_type_node_I (type);
2031 if (node && node->is_classed && node->data &&
2032 node->data->class.class && node->data->common.ref_count > 0)
2034 type_data_ref_Wm (node);
2035 G_WRITE_UNLOCK (&type_rw_lock);
2037 return node->data->class.class;
2040 if (!node || !node->is_classed ||
2041 (node->data && node->data->common.ref_count < 1))
2043 G_WRITE_UNLOCK (&type_rw_lock);
2044 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2045 type_descriptive_name_I (type));
2049 type_data_ref_Wm (node);
2051 if (!node->data->class.class)
2053 GType ptype = NODE_PARENT_TYPE (node);
2054 GTypeClass *pclass = NULL;
2058 G_WRITE_UNLOCK (&type_rw_lock);
2059 pclass = g_type_class_ref (ptype);
2060 if (node->data->class.class)
2061 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2062 G_WRITE_LOCK (&type_rw_lock);
2065 type_class_init_Wm (node, pclass);
2067 G_WRITE_UNLOCK (&type_rw_lock);
2069 return node->data->class.class;
2073 g_type_class_unref (gpointer g_class)
2076 GTypeClass *class = g_class;
2078 g_return_if_fail (g_class != NULL);
2080 node = lookup_type_node_I (class->g_type);
2081 G_WRITE_LOCK (&type_rw_lock);
2082 if (node && node->is_classed && node->data &&
2083 node->data->class.class == class && node->data->common.ref_count > 0)
2084 type_data_unref_Wm (node, FALSE);
2086 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2087 type_descriptive_name_I (class->g_type));
2088 G_WRITE_UNLOCK (&type_rw_lock);
2092 g_type_class_unref_uncached (gpointer g_class)
2095 GTypeClass *class = g_class;
2097 g_return_if_fail (g_class != NULL);
2099 G_WRITE_LOCK (&type_rw_lock);
2100 node = lookup_type_node_I (class->g_type);
2101 if (node && node->is_classed && node->data &&
2102 node->data->class.class == class && node->data->common.ref_count > 0)
2103 type_data_unref_Wm (node, TRUE);
2105 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2106 type_descriptive_name_I (class->g_type));
2107 G_WRITE_UNLOCK (&type_rw_lock);
2111 g_type_class_peek (GType type)
2116 node = lookup_type_node_I (type);
2117 G_READ_LOCK (&type_rw_lock);
2118 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2119 class = node->data->class.class;
2122 G_READ_UNLOCK (&type_rw_lock);
2128 g_type_class_peek_parent (gpointer g_class)
2131 gpointer class = NULL;
2133 g_return_val_if_fail (g_class != NULL, NULL);
2135 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2136 G_READ_LOCK (&type_rw_lock);
2137 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2139 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2140 class = node->data->class.class;
2142 else if (NODE_PARENT_TYPE (node))
2143 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2144 G_READ_UNLOCK (&type_rw_lock);
2150 g_type_interface_peek (gpointer instance_class,
2155 gpointer vtable = NULL;
2156 GTypeClass *class = instance_class;
2158 g_return_val_if_fail (instance_class != NULL, NULL);
2160 node = lookup_type_node_I (class->g_type);
2161 iface = lookup_type_node_I (iface_type);
2162 if (node && node->is_instantiatable && iface)
2166 G_READ_LOCK (&type_rw_lock);
2168 entry = type_lookup_iface_entry_L (node, iface);
2169 if (entry && entry->vtable) /* entry is relocatable */
2170 vtable = entry->vtable;
2172 G_READ_UNLOCK (&type_rw_lock);
2175 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2181 g_type_interface_peek_parent (gpointer g_iface)
2185 gpointer vtable = NULL;
2186 GTypeInterface *iface_class = g_iface;
2188 g_return_val_if_fail (g_iface != NULL, NULL);
2190 iface = lookup_type_node_I (iface_class->g_type);
2191 node = lookup_type_node_I (iface_class->g_instance_type);
2193 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2194 if (node && node->is_instantiatable && iface)
2198 G_READ_LOCK (&type_rw_lock);
2200 entry = type_lookup_iface_entry_L (node, iface);
2201 if (entry && entry->vtable) /* entry is relocatable */
2202 vtable = entry->vtable;
2204 G_READ_UNLOCK (&type_rw_lock);
2207 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2212 G_CONST_RETURN gchar*
2213 g_type_name (GType type)
2217 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2219 node = lookup_type_node_I (type);
2221 return node ? NODE_NAME (node) : NULL;
2225 g_type_qname (GType type)
2229 node = lookup_type_node_I (type);
2231 return node ? node->qname : 0;
2235 g_type_from_name (const gchar *name)
2240 g_return_val_if_fail (name != NULL, 0);
2242 quark = g_quark_try_string (name);
2245 G_READ_LOCK (&type_rw_lock);
2246 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2247 G_READ_UNLOCK (&type_rw_lock);
2254 g_type_parent (GType type)
2258 node = lookup_type_node_I (type);
2260 return node ? NODE_PARENT_TYPE (node) : 0;
2264 g_type_depth (GType type)
2268 node = lookup_type_node_I (type);
2270 return node ? node->n_supers + 1 : 0;
2274 g_type_next_base (GType type,
2280 node = lookup_type_node_I (type);
2283 TypeNode *base_node = lookup_type_node_I (base_type);
2285 if (base_node && base_node->n_supers < node->n_supers)
2287 guint n = node->n_supers - base_node->n_supers;
2289 if (node->supers[n] == base_type)
2290 atype = node->supers[n - 1];
2297 static inline gboolean
2298 type_node_check_conformities_UorL (TypeNode *node,
2299 TypeNode *iface_node,
2300 /* support_inheritance */
2301 gboolean support_interfaces,
2302 gboolean support_prerequisites,
2307 if (/* support_inheritance && */
2308 NODE_IS_ANCESTOR (iface_node, node))
2311 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2312 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2314 if (support_interfaces || support_prerequisites)
2317 G_READ_LOCK (&type_rw_lock);
2318 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2320 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2323 G_READ_UNLOCK (&type_rw_lock);
2329 type_node_is_a_L (TypeNode *node,
2330 TypeNode *iface_node)
2332 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2335 static inline gboolean
2336 type_node_conforms_to_U (TypeNode *node,
2337 TypeNode *iface_node,
2338 gboolean support_interfaces,
2339 gboolean support_prerequisites)
2341 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2345 g_type_is_a (GType type,
2348 TypeNode *node, *iface_node;
2351 node = lookup_type_node_I (type);
2352 iface_node = lookup_type_node_I (iface_type);
2353 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2358 GType* /* free result */
2359 g_type_children (GType type,
2364 node = lookup_type_node_I (type);
2369 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2370 children = g_new (GType, node->n_children + 1);
2371 memcpy (children, node->children, sizeof (GType) * node->n_children);
2372 children[node->n_children] = 0;
2375 *n_children = node->n_children;
2376 G_READ_UNLOCK (&type_rw_lock);
2389 GType* /* free result */
2390 g_type_interfaces (GType type,
2391 guint *n_interfaces)
2395 node = lookup_type_node_I (type);
2396 if (node && node->is_instantiatable)
2401 G_READ_LOCK (&type_rw_lock);
2402 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2403 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2404 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2408 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2409 G_READ_UNLOCK (&type_rw_lock);
2422 typedef struct _QData QData;
2434 static inline gpointer
2435 type_get_qdata_L (TypeNode *node,
2438 GData *gdata = node->global_gdata;
2440 if (quark && gdata && gdata->n_qdatas)
2442 QData *qdatas = gdata->qdatas - 1;
2443 guint n_qdatas = gdata->n_qdatas;
2450 i = (n_qdatas + 1) / 2;
2452 if (quark == check->quark)
2454 else if (quark > check->quark)
2459 else /* if (quark < check->quark) */
2468 g_type_get_qdata (GType type,
2474 node = lookup_type_node_I (type);
2477 G_READ_LOCK (&type_rw_lock);
2478 data = type_get_qdata_L (node, quark);
2479 G_READ_UNLOCK (&type_rw_lock);
2483 g_return_val_if_fail (node != NULL, NULL);
2490 type_set_qdata_W (TypeNode *node,
2498 /* setup qdata list if necessary */
2499 if (!node->global_gdata)
2500 node->global_gdata = g_new0 (GData, 1);
2501 gdata = node->global_gdata;
2503 /* try resetting old data */
2504 qdata = gdata->qdatas;
2505 for (i = 0; i < gdata->n_qdatas; i++)
2506 if (qdata[i].quark == quark)
2508 qdata[i].data = data;
2514 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2515 qdata = gdata->qdatas;
2516 for (i = 0; i < gdata->n_qdatas - 1; i++)
2517 if (qdata[i].quark > quark)
2519 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2520 qdata[i].quark = quark;
2521 qdata[i].data = data;
2525 g_type_set_qdata (GType type,
2531 g_return_if_fail (quark != 0);
2533 node = lookup_type_node_I (type);
2536 G_WRITE_LOCK (&type_rw_lock);
2537 type_set_qdata_W (node, quark, data);
2538 G_WRITE_UNLOCK (&type_rw_lock);
2541 g_return_if_fail (node != NULL);
2545 type_add_flags_W (TypeNode *node,
2550 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2551 g_return_if_fail (node != NULL);
2553 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2554 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2555 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2557 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2561 g_type_query (GType type,
2566 g_return_if_fail (query != NULL);
2568 /* if node is not static and classed, we won't allow query */
2570 node = lookup_type_node_I (type);
2571 if (node && node->is_classed && !node->plugin)
2573 /* type is classed and probably even instantiatable */
2574 G_READ_LOCK (&type_rw_lock);
2575 if (node->data) /* type is static or referenced */
2577 query->type = NODE_TYPE (node);
2578 query->type_name = NODE_NAME (node);
2579 query->class_size = node->data->class.class_size;
2580 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2582 G_READ_UNLOCK (&type_rw_lock);
2587 /* --- implementation details --- */
2589 g_type_test_flags (GType type,
2593 gboolean result = FALSE;
2595 node = lookup_type_node_I (type);
2598 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2599 guint tflags = flags & TYPE_FLAG_MASK;
2603 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2605 fflags = (finfo->type_flags & fflags) == fflags;
2612 G_READ_LOCK (&type_rw_lock);
2613 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2614 G_READ_UNLOCK (&type_rw_lock);
2619 result = tflags && fflags;
2626 g_type_get_plugin (GType type)
2630 node = lookup_type_node_I (type);
2632 return node ? node->plugin : NULL;
2636 g_type_interface_get_plugin (GType instance_type,
2637 GType interface_type)
2642 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
2644 node = lookup_type_node_I (instance_type);
2645 iface = lookup_type_node_I (interface_type);
2648 IFaceHolder *iholder;
2649 GTypePlugin *plugin;
2651 G_READ_LOCK (&type_rw_lock);
2653 iholder = iface_node_get_holders_L (iface);
2654 while (iholder && iholder->instance_type != instance_type)
2655 iholder = iholder->next;
2656 plugin = iholder ? iholder->plugin : NULL;
2658 G_READ_UNLOCK (&type_rw_lock);
2663 g_return_val_if_fail (node == NULL, NULL);
2664 g_return_val_if_fail (iface == NULL, NULL);
2666 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2672 g_type_fundamental_next (void)
2676 G_READ_LOCK (&type_rw_lock);
2677 type = static_fundamental_next;
2678 G_READ_UNLOCK (&type_rw_lock);
2679 type = G_TYPE_MAKE_FUNDAMENTAL (type);
2680 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2684 g_type_fundamental (GType type_id)
2686 TypeNode *node = lookup_type_node_I (type_id);
2688 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2692 g_type_check_instance_is_a (GTypeInstance *type_instance,
2695 TypeNode *node, *iface;
2698 if (!type_instance || !type_instance->g_class)
2701 node = lookup_type_node_I (type_instance->g_class->g_type);
2702 iface = lookup_type_node_I (iface_type);
2703 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2709 g_type_check_class_is_a (GTypeClass *type_class,
2712 TypeNode *node, *iface;
2718 node = lookup_type_node_I (type_class->g_type);
2719 iface = lookup_type_node_I (is_a_type);
2720 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2726 g_type_check_instance_cast (GTypeInstance *type_instance,
2731 if (type_instance->g_class)
2733 TypeNode *node, *iface;
2734 gboolean is_instantiatable, check;
2736 node = lookup_type_node_I (type_instance->g_class->g_type);
2737 is_instantiatable = node && node->is_instantiatable;
2738 iface = lookup_type_node_I (iface_type);
2739 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2741 return type_instance;
2743 if (is_instantiatable)
2744 g_warning ("invalid cast from `%s' to `%s'",
2745 type_descriptive_name_I (type_instance->g_class->g_type),
2746 type_descriptive_name_I (iface_type));
2748 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2749 type_descriptive_name_I (type_instance->g_class->g_type),
2750 type_descriptive_name_I (iface_type));
2753 g_warning ("invalid unclassed pointer in cast to `%s'",
2754 type_descriptive_name_I (iface_type));
2757 return type_instance;
2761 g_type_check_class_cast (GTypeClass *type_class,
2766 TypeNode *node, *iface;
2767 gboolean is_classed, check;
2769 node = lookup_type_node_I (type_class->g_type);
2770 is_classed = node && node->is_classed;
2771 iface = lookup_type_node_I (is_a_type);
2772 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2777 g_warning ("invalid class cast from `%s' to `%s'",
2778 type_descriptive_name_I (type_class->g_type),
2779 type_descriptive_name_I (is_a_type));
2781 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2782 type_descriptive_name_I (type_class->g_type),
2783 type_descriptive_name_I (is_a_type));
2786 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2787 type_descriptive_name_I (is_a_type));
2792 g_type_check_instance (GTypeInstance *type_instance)
2794 /* this function is just here to make the signal system
2795 * conveniently elaborated on instance checks
2799 if (type_instance->g_class)
2801 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2803 if (node && node->is_instantiatable)
2806 g_warning ("instance of invalid non-instantiatable type `%s'",
2807 type_descriptive_name_I (type_instance->g_class->g_type));
2810 g_warning ("instance with invalid (NULL) class pointer");
2813 g_warning ("invalid (NULL) pointer instance");
2818 static inline gboolean
2819 type_check_is_value_type_U (GType type)
2821 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2824 /* common path speed up */
2825 node = lookup_type_node_I (type);
2826 if (node && node->mutatable_check_cache)
2829 G_READ_LOCK (&type_rw_lock);
2833 if (node->data && node->data->common.ref_count > 0 &&
2834 node->data->common.value_table->value_init)
2835 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2836 else if (NODE_IS_IFACE (node))
2840 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2842 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2843 TypeNode *prnode = lookup_type_node_I (prtype);
2845 if (prnode->is_instantiatable)
2848 node = lookup_type_node_I (type);
2854 G_READ_UNLOCK (&type_rw_lock);
2856 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2860 g_type_check_is_value_type (GType type)
2862 return type_check_is_value_type_U (type);
2866 g_type_check_value (GValue *value)
2868 return value && type_check_is_value_type_U (value->g_type);
2872 g_type_check_value_holds (GValue *value,
2875 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2879 g_type_value_table_peek (GType type)
2881 GTypeValueTable *vtable = NULL;
2882 TypeNode *node = lookup_type_node_I (type);
2883 gboolean has_refed_data, has_table;
2886 /* speed up common code path, we're not 100% safe here,
2887 * but we should only get called with referenced types anyway
2889 data = node ? node->data : NULL;
2890 if (node && node->mutatable_check_cache)
2891 return data->common.value_table;
2893 G_READ_LOCK (&type_rw_lock);
2896 has_refed_data = node && node->data && node->data->common.ref_count;
2897 has_table = has_refed_data && node->data->common.value_table->value_init;
2901 vtable = node->data->common.value_table;
2902 else if (NODE_IS_IFACE (node))
2906 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2908 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2909 TypeNode *prnode = lookup_type_node_I (prtype);
2911 if (prnode->is_instantiatable)
2914 node = lookup_type_node_I (type);
2915 goto restart_table_peek;
2921 G_READ_UNLOCK (&type_rw_lock);
2927 g_warning (G_STRLOC ": type id `%lu' is invalid", type);
2928 if (!has_refed_data)
2929 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2930 type_descriptive_name_I (type));
2935 G_CONST_RETURN gchar*
2936 g_type_name_from_instance (GTypeInstance *instance)
2939 return "<NULL-instance>";
2941 return g_type_name_from_class (instance->g_class);
2944 G_CONST_RETURN gchar*
2945 g_type_name_from_class (GTypeClass *g_class)
2948 return "<NULL-class>";
2950 return g_type_name (g_class->g_type);
2954 /* --- foreign prototypes --- */
2955 extern void g_value_c_init (void); /* sync with gvalue.c */
2956 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2957 extern void g_enum_types_init (void); /* sync with genums.c */
2958 extern void g_param_type_init (void); /* sync with gparam.c */
2959 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2960 extern void g_object_type_init (void); /* sync with gobject.c */
2961 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2962 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2963 extern void g_signal_init (void); /* sync with gsignal.c */
2966 /* --- initialization --- */
2968 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2970 G_LOCK_DEFINE_STATIC (type_init_lock);
2971 const gchar *env_string;
2976 G_LOCK (type_init_lock);
2978 G_WRITE_LOCK (&type_rw_lock);
2980 if (static_quark_type_flags)
2982 G_WRITE_UNLOCK (&type_rw_lock);
2983 G_UNLOCK (type_init_lock);
2987 /* setup GObject library wide debugging flags */
2988 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2989 env_string = g_getenv ("GOBJECT_DEBUG");
2990 if (env_string != NULL)
2992 static GDebugKey debug_keys[] = {
2993 { "objects", G_TYPE_DEBUG_OBJECTS },
2994 { "signals", G_TYPE_DEBUG_SIGNALS },
2997 _g_type_debug_flags |= g_parse_debug_string (env_string,
2999 sizeof (debug_keys) / sizeof (debug_keys[0]));
3004 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3005 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3006 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3008 /* type qname hash table */
3009 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3011 /* invalid type G_TYPE_INVALID (0)
3013 static_fundamental_type_nodes[0] = NULL;
3015 /* void type G_TYPE_NONE
3017 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
3018 type = NODE_TYPE (node);
3019 g_assert (type == G_TYPE_NONE);
3021 /* interface fundamental type G_TYPE_INTERFACE (!classed)
3023 memset (&info, 0, sizeof (info));
3024 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
3025 type = NODE_TYPE (node);
3026 type_data_make_W (node, &info, NULL);
3027 g_assert (type == G_TYPE_INTERFACE);
3029 G_WRITE_UNLOCK (&type_rw_lock);
3033 /* G_TYPE_TYPE_PLUGIN
3035 g_type_plugin_get_type ();
3037 /* G_TYPE_* value types
3039 g_value_types_init ();
3041 /* G_TYPE_ENUM & G_TYPE_FLAGS
3043 g_enum_types_init ();
3047 g_boxed_type_init ();
3051 g_param_type_init ();
3055 g_object_type_init ();
3057 /* G_TYPE_PARAM_* pspec types
3059 g_param_spec_types_init ();
3061 /* Value Transformations
3063 g_value_transforms_init ();
3069 G_UNLOCK (type_init_lock);
3075 g_type_init_with_debug_flags (0);
3079 g_type_class_add_private (gpointer g_class,
3082 GType instance_type = ((GTypeClass *)g_class)->g_type;
3083 TypeNode *node = lookup_type_node_I (instance_type);
3086 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3088 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3089 type_descriptive_name_I (instance_type));
3093 if (NODE_PARENT_TYPE (node))
3095 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3096 if (node->data->instance.private_size != pnode->data->instance.private_size)
3098 g_warning ("g_type_add_private() called multiple times for the same type");
3103 G_WRITE_LOCK (&type_rw_lock);
3105 offset = ALIGN_STRUCT (node->data->instance.private_size);
3106 node->data->instance.private_size = offset + private_size;
3108 G_WRITE_UNLOCK (&type_rw_lock);
3112 g_type_instance_get_private (GTypeInstance *instance,
3115 TypeNode *instance_node;
3116 TypeNode *private_node;
3117 TypeNode *parent_node;
3120 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3122 instance_node = lookup_type_node_I (instance->g_class->g_type);
3123 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3125 g_warning ("instance of invalid non-instantiatable type `%s'",
3126 type_descriptive_name_I (instance->g_class->g_type));
3130 private_node = lookup_type_node_I (private_type);
3131 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3133 g_warning ("attempt to retrieve private data for invalid type '%s'",
3134 type_descriptive_name_I (private_type));
3138 /* Note that we don't need a read lock, since instance existing
3139 * means that the instance class and all parent classes
3140 * exist, so the node->data, node->data->instance.instance_size,
3141 * and node->data->instance.private_size are not going to be changed.
3142 * for any of the relevant types.
3145 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3147 if (NODE_PARENT_TYPE (private_node))
3149 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3150 g_assert (parent_node->data && parent_node->data->common.ref_count);
3152 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3155 return G_STRUCT_MEMBER_P (instance, offset);