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);
1589 if (node->is_instantiatable)
1591 /* We need to initialize the private_size here rather than in
1592 * type_data_make_W() since the class init for the parent
1593 * class may have changed pnode->data->instance.private_size.
1595 node->data->instance.private_size = pnode->data->instance.private_size;
1598 class->g_type = NODE_TYPE (node);
1600 G_WRITE_UNLOCK (&type_rw_lock);
1602 /* stack all base class initialization functions, so we
1603 * call them in ascending order.
1605 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1606 if (bnode->data->class.class_init_base)
1607 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1608 for (slist = init_slist; slist; slist = slist->next)
1610 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1612 class_init_base (class);
1614 g_slist_free (init_slist);
1616 if (node->data->class.class_init)
1617 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1619 G_WRITE_LOCK (&type_rw_lock);
1621 /* ok, we got the class done, now initialize all interfaces, either
1622 * from parent, or through our holder info
1624 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1625 entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1628 g_assert (entry->vtable == NULL);
1630 if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1634 /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1635 * need to get this interface from parent
1637 g_assert (pnode != NULL);
1639 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1641 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1643 if (pentry->iface_type == entry->iface_type)
1645 entry->vtable = pentry->vtable;
1649 g_assert (entry->vtable != NULL);
1652 /* refetch entry, IFACES_ENTRIES might be modified */
1653 for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1654 if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1655 entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1660 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1664 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1667 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1669 IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1672 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1674 /* refetch entries, IFACES_ENTRIES might be modified */
1679 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1680 * iface vtable came from parent
1682 entry->vtable = NULL;
1689 type_data_finalize_class_U (TypeNode *node,
1692 GTypeClass *class = cdata->class;
1695 g_assert (cdata->class && cdata->common.ref_count == 0);
1697 if (cdata->class_finalize)
1698 cdata->class_finalize (class, (gpointer) cdata->class_data);
1700 /* call all base class destruction functions in descending order
1702 if (cdata->class_finalize_base)
1703 cdata->class_finalize_base (class);
1704 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1705 if (bnode->data->class.class_finalize_base)
1706 bnode->data->class.class_finalize_base (class);
1709 g_free (cdata->class);
1713 type_data_last_unref_Wm (GType type,
1716 TypeNode *node = lookup_type_node_I (type);
1718 g_return_if_fail (node != NULL && node->plugin != NULL);
1720 if (!node->data || node->data->common.ref_count == 0)
1722 g_warning ("cannot drop last reference to unreferenced type `%s'",
1723 type_descriptive_name_I (type));
1727 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1731 G_WRITE_UNLOCK (&type_rw_lock);
1732 G_READ_LOCK (&type_rw_lock);
1733 for (i = 0; i < static_n_class_cache_funcs; i++)
1735 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1736 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1737 gboolean need_break;
1739 G_READ_UNLOCK (&type_rw_lock);
1740 need_break = cache_func (cache_data, node->data->class.class);
1741 G_READ_LOCK (&type_rw_lock);
1742 if (!node->data || node->data->common.ref_count == 0)
1743 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1747 G_READ_UNLOCK (&type_rw_lock);
1748 G_WRITE_LOCK (&type_rw_lock);
1751 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1752 node->data->common.ref_count -= 1;
1755 GType ptype = NODE_PARENT_TYPE (node);
1758 node->data->common.ref_count = 0;
1760 if (node->is_instantiatable && node->data->instance.mem_chunk)
1762 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1763 node->data->instance.mem_chunk = NULL;
1767 if (node->is_classed && tdata->class.class)
1769 if (CLASSED_NODE_N_IFACES (node))
1770 type_data_finalize_class_ifaces_Wm (node);
1771 node->mutatable_check_cache = FALSE;
1773 G_WRITE_UNLOCK (&type_rw_lock);
1774 type_data_finalize_class_U (node, &tdata->class);
1775 G_WRITE_LOCK (&type_rw_lock);
1779 node->mutatable_check_cache = FALSE;
1783 /* freeing tdata->common.value_table and its contents is taking care of
1784 * by allocating it in one chunk with tdata
1789 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1790 G_WRITE_UNLOCK (&type_rw_lock);
1791 g_type_plugin_unuse (node->plugin);
1792 G_WRITE_LOCK (&type_rw_lock);
1797 g_type_add_class_cache_func (gpointer cache_data,
1798 GTypeClassCacheFunc cache_func)
1802 g_return_if_fail (cache_func != NULL);
1804 G_WRITE_LOCK (&type_rw_lock);
1805 i = static_n_class_cache_funcs++;
1806 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1807 static_class_cache_funcs[i].cache_data = cache_data;
1808 static_class_cache_funcs[i].cache_func = cache_func;
1809 G_WRITE_UNLOCK (&type_rw_lock);
1813 g_type_remove_class_cache_func (gpointer cache_data,
1814 GTypeClassCacheFunc cache_func)
1816 gboolean found_it = FALSE;
1819 g_return_if_fail (cache_func != NULL);
1821 G_WRITE_LOCK (&type_rw_lock);
1822 for (i = 0; i < static_n_class_cache_funcs; i++)
1823 if (static_class_cache_funcs[i].cache_data == cache_data &&
1824 static_class_cache_funcs[i].cache_func == cache_func)
1826 static_n_class_cache_funcs--;
1827 g_memmove (static_class_cache_funcs + i,
1828 static_class_cache_funcs + i + 1,
1829 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1830 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1834 G_WRITE_UNLOCK (&type_rw_lock);
1837 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1838 cache_func, cache_data);
1842 /* --- type registration --- */
1844 g_type_register_fundamental (GType type_id,
1845 const gchar *type_name,
1846 const GTypeInfo *info,
1847 const GTypeFundamentalInfo *finfo,
1850 GTypeFundamentalInfo *node_finfo;
1853 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1854 g_return_val_if_fail (type_id > 0, 0);
1855 g_return_val_if_fail (type_name != NULL, 0);
1856 g_return_val_if_fail (info != NULL, 0);
1857 g_return_val_if_fail (finfo != NULL, 0);
1859 if (!check_type_name_I (type_name))
1861 if ((type_id & TYPE_ID_MASK) ||
1862 type_id > G_TYPE_FUNDAMENTAL_MAX)
1864 g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
1869 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1870 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1872 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1876 if (lookup_type_node_I (type_id))
1878 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1879 type_descriptive_name_I (type_id),
1884 G_WRITE_LOCK (&type_rw_lock);
1885 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1886 node_finfo = type_node_fundamental_info_I (node);
1887 type_add_flags_W (node, flags);
1889 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1890 type_data_make_W (node, info,
1891 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1892 G_WRITE_UNLOCK (&type_rw_lock);
1894 return NODE_TYPE (node);
1898 g_type_register_static (GType parent_type,
1899 const gchar *type_name,
1900 const GTypeInfo *info,
1903 TypeNode *pnode, *node;
1906 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1907 g_return_val_if_fail (parent_type > 0, 0);
1908 g_return_val_if_fail (type_name != NULL, 0);
1909 g_return_val_if_fail (info != NULL, 0);
1911 if (!check_type_name_I (type_name) ||
1912 !check_derivation_I (parent_type, type_name))
1914 if (info->class_finalize)
1916 g_warning ("class finalizer specified for static type `%s'",
1921 pnode = lookup_type_node_I (parent_type);
1922 G_WRITE_LOCK (&type_rw_lock);
1923 type_data_ref_Wm (pnode);
1924 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1926 node = type_node_new_W (pnode, type_name, NULL);
1927 type_add_flags_W (node, flags);
1928 type = NODE_TYPE (node);
1929 type_data_make_W (node, info,
1930 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1932 G_WRITE_UNLOCK (&type_rw_lock);
1938 g_type_register_dynamic (GType parent_type,
1939 const gchar *type_name,
1940 GTypePlugin *plugin,
1943 TypeNode *pnode, *node;
1946 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1947 g_return_val_if_fail (parent_type > 0, 0);
1948 g_return_val_if_fail (type_name != NULL, 0);
1949 g_return_val_if_fail (plugin != NULL, 0);
1951 if (!check_type_name_I (type_name) ||
1952 !check_derivation_I (parent_type, type_name) ||
1953 !check_plugin_U (plugin, TRUE, FALSE, type_name))
1956 G_WRITE_LOCK (&type_rw_lock);
1957 pnode = lookup_type_node_I (parent_type);
1958 node = type_node_new_W (pnode, type_name, plugin);
1959 type_add_flags_W (node, flags);
1960 type = NODE_TYPE (node);
1961 G_WRITE_UNLOCK (&type_rw_lock);
1967 g_type_add_interface_static (GType instance_type,
1968 GType interface_type,
1969 const GInterfaceInfo *info)
1971 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1972 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1973 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1975 G_WRITE_LOCK (&type_rw_lock);
1976 if (check_add_interface_L (instance_type, interface_type))
1978 TypeNode *node = lookup_type_node_I (instance_type);
1979 TypeNode *iface = lookup_type_node_I (interface_type);
1981 if (check_interface_info_I (iface, NODE_TYPE (node), info))
1983 type_add_interface_W (node, iface, info, NULL);
1984 /* if we have a class already, the interface vtable needs to
1985 * be initialized as well
1987 if (node->data && node->data->class.class)
1988 type_iface_vtable_init_Wm (iface, node);
1991 G_WRITE_UNLOCK (&type_rw_lock);
1995 g_type_add_interface_dynamic (GType instance_type,
1996 GType interface_type,
1997 GTypePlugin *plugin)
2001 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2002 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2003 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2005 node = lookup_type_node_I (instance_type);
2006 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2009 G_WRITE_LOCK (&type_rw_lock);
2010 if (check_add_interface_L (instance_type, interface_type))
2012 TypeNode *iface = lookup_type_node_I (interface_type);
2014 type_add_interface_W (node, iface, NULL, plugin);
2015 /* if we have a class already, the interface vtable needs to
2016 * be initialized as well
2018 if (node->data && node->data->class.class)
2019 type_iface_vtable_init_Wm (iface, node);
2021 G_WRITE_UNLOCK (&type_rw_lock);
2025 /* --- public API functions --- */
2027 g_type_class_ref (GType type)
2031 /* optimize for common code path
2033 G_WRITE_LOCK (&type_rw_lock);
2034 node = lookup_type_node_I (type);
2035 if (node && node->is_classed && node->data &&
2036 node->data->class.class && node->data->common.ref_count > 0)
2038 type_data_ref_Wm (node);
2039 G_WRITE_UNLOCK (&type_rw_lock);
2041 return node->data->class.class;
2044 if (!node || !node->is_classed ||
2045 (node->data && node->data->common.ref_count < 1))
2047 G_WRITE_UNLOCK (&type_rw_lock);
2048 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2049 type_descriptive_name_I (type));
2053 type_data_ref_Wm (node);
2055 if (!node->data->class.class)
2057 GType ptype = NODE_PARENT_TYPE (node);
2058 GTypeClass *pclass = NULL;
2062 G_WRITE_UNLOCK (&type_rw_lock);
2063 pclass = g_type_class_ref (ptype);
2064 if (node->data->class.class)
2065 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2066 G_WRITE_LOCK (&type_rw_lock);
2069 type_class_init_Wm (node, pclass);
2071 G_WRITE_UNLOCK (&type_rw_lock);
2073 return node->data->class.class;
2077 g_type_class_unref (gpointer g_class)
2080 GTypeClass *class = g_class;
2082 g_return_if_fail (g_class != NULL);
2084 node = lookup_type_node_I (class->g_type);
2085 G_WRITE_LOCK (&type_rw_lock);
2086 if (node && node->is_classed && node->data &&
2087 node->data->class.class == class && node->data->common.ref_count > 0)
2088 type_data_unref_Wm (node, FALSE);
2090 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2091 type_descriptive_name_I (class->g_type));
2092 G_WRITE_UNLOCK (&type_rw_lock);
2096 g_type_class_unref_uncached (gpointer g_class)
2099 GTypeClass *class = g_class;
2101 g_return_if_fail (g_class != NULL);
2103 G_WRITE_LOCK (&type_rw_lock);
2104 node = lookup_type_node_I (class->g_type);
2105 if (node && node->is_classed && node->data &&
2106 node->data->class.class == class && node->data->common.ref_count > 0)
2107 type_data_unref_Wm (node, TRUE);
2109 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2110 type_descriptive_name_I (class->g_type));
2111 G_WRITE_UNLOCK (&type_rw_lock);
2115 g_type_class_peek (GType type)
2120 node = lookup_type_node_I (type);
2121 G_READ_LOCK (&type_rw_lock);
2122 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2123 class = node->data->class.class;
2126 G_READ_UNLOCK (&type_rw_lock);
2132 g_type_class_peek_parent (gpointer g_class)
2135 gpointer class = NULL;
2137 g_return_val_if_fail (g_class != NULL, NULL);
2139 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2140 G_READ_LOCK (&type_rw_lock);
2141 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2143 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2144 class = node->data->class.class;
2146 else if (NODE_PARENT_TYPE (node))
2147 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2148 G_READ_UNLOCK (&type_rw_lock);
2154 g_type_interface_peek (gpointer instance_class,
2159 gpointer vtable = NULL;
2160 GTypeClass *class = instance_class;
2162 g_return_val_if_fail (instance_class != NULL, NULL);
2164 node = lookup_type_node_I (class->g_type);
2165 iface = lookup_type_node_I (iface_type);
2166 if (node && node->is_instantiatable && iface)
2170 G_READ_LOCK (&type_rw_lock);
2172 entry = type_lookup_iface_entry_L (node, iface);
2173 if (entry && entry->vtable) /* entry is relocatable */
2174 vtable = entry->vtable;
2176 G_READ_UNLOCK (&type_rw_lock);
2179 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2185 g_type_interface_peek_parent (gpointer g_iface)
2189 gpointer vtable = NULL;
2190 GTypeInterface *iface_class = g_iface;
2192 g_return_val_if_fail (g_iface != NULL, NULL);
2194 iface = lookup_type_node_I (iface_class->g_type);
2195 node = lookup_type_node_I (iface_class->g_instance_type);
2197 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2198 if (node && node->is_instantiatable && iface)
2202 G_READ_LOCK (&type_rw_lock);
2204 entry = type_lookup_iface_entry_L (node, iface);
2205 if (entry && entry->vtable) /* entry is relocatable */
2206 vtable = entry->vtable;
2208 G_READ_UNLOCK (&type_rw_lock);
2211 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2216 G_CONST_RETURN gchar*
2217 g_type_name (GType type)
2221 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2223 node = lookup_type_node_I (type);
2225 return node ? NODE_NAME (node) : NULL;
2229 g_type_qname (GType type)
2233 node = lookup_type_node_I (type);
2235 return node ? node->qname : 0;
2239 g_type_from_name (const gchar *name)
2244 g_return_val_if_fail (name != NULL, 0);
2246 quark = g_quark_try_string (name);
2249 G_READ_LOCK (&type_rw_lock);
2250 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2251 G_READ_UNLOCK (&type_rw_lock);
2258 g_type_parent (GType type)
2262 node = lookup_type_node_I (type);
2264 return node ? NODE_PARENT_TYPE (node) : 0;
2268 g_type_depth (GType type)
2272 node = lookup_type_node_I (type);
2274 return node ? node->n_supers + 1 : 0;
2278 g_type_next_base (GType type,
2284 node = lookup_type_node_I (type);
2287 TypeNode *base_node = lookup_type_node_I (base_type);
2289 if (base_node && base_node->n_supers < node->n_supers)
2291 guint n = node->n_supers - base_node->n_supers;
2293 if (node->supers[n] == base_type)
2294 atype = node->supers[n - 1];
2301 static inline gboolean
2302 type_node_check_conformities_UorL (TypeNode *node,
2303 TypeNode *iface_node,
2304 /* support_inheritance */
2305 gboolean support_interfaces,
2306 gboolean support_prerequisites,
2311 if (/* support_inheritance && */
2312 NODE_IS_ANCESTOR (iface_node, node))
2315 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2316 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2318 if (support_interfaces || support_prerequisites)
2321 G_READ_LOCK (&type_rw_lock);
2322 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2324 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2327 G_READ_UNLOCK (&type_rw_lock);
2333 type_node_is_a_L (TypeNode *node,
2334 TypeNode *iface_node)
2336 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2339 static inline gboolean
2340 type_node_conforms_to_U (TypeNode *node,
2341 TypeNode *iface_node,
2342 gboolean support_interfaces,
2343 gboolean support_prerequisites)
2345 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2349 g_type_is_a (GType type,
2352 TypeNode *node, *iface_node;
2355 node = lookup_type_node_I (type);
2356 iface_node = lookup_type_node_I (iface_type);
2357 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2362 GType* /* free result */
2363 g_type_children (GType type,
2368 node = lookup_type_node_I (type);
2373 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2374 children = g_new (GType, node->n_children + 1);
2375 memcpy (children, node->children, sizeof (GType) * node->n_children);
2376 children[node->n_children] = 0;
2379 *n_children = node->n_children;
2380 G_READ_UNLOCK (&type_rw_lock);
2393 GType* /* free result */
2394 g_type_interfaces (GType type,
2395 guint *n_interfaces)
2399 node = lookup_type_node_I (type);
2400 if (node && node->is_instantiatable)
2405 G_READ_LOCK (&type_rw_lock);
2406 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2407 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2408 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2412 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2413 G_READ_UNLOCK (&type_rw_lock);
2426 typedef struct _QData QData;
2438 static inline gpointer
2439 type_get_qdata_L (TypeNode *node,
2442 GData *gdata = node->global_gdata;
2444 if (quark && gdata && gdata->n_qdatas)
2446 QData *qdatas = gdata->qdatas - 1;
2447 guint n_qdatas = gdata->n_qdatas;
2454 i = (n_qdatas + 1) / 2;
2456 if (quark == check->quark)
2458 else if (quark > check->quark)
2463 else /* if (quark < check->quark) */
2472 g_type_get_qdata (GType type,
2478 node = lookup_type_node_I (type);
2481 G_READ_LOCK (&type_rw_lock);
2482 data = type_get_qdata_L (node, quark);
2483 G_READ_UNLOCK (&type_rw_lock);
2487 g_return_val_if_fail (node != NULL, NULL);
2494 type_set_qdata_W (TypeNode *node,
2502 /* setup qdata list if necessary */
2503 if (!node->global_gdata)
2504 node->global_gdata = g_new0 (GData, 1);
2505 gdata = node->global_gdata;
2507 /* try resetting old data */
2508 qdata = gdata->qdatas;
2509 for (i = 0; i < gdata->n_qdatas; i++)
2510 if (qdata[i].quark == quark)
2512 qdata[i].data = data;
2518 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2519 qdata = gdata->qdatas;
2520 for (i = 0; i < gdata->n_qdatas - 1; i++)
2521 if (qdata[i].quark > quark)
2523 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2524 qdata[i].quark = quark;
2525 qdata[i].data = data;
2529 g_type_set_qdata (GType type,
2535 g_return_if_fail (quark != 0);
2537 node = lookup_type_node_I (type);
2540 G_WRITE_LOCK (&type_rw_lock);
2541 type_set_qdata_W (node, quark, data);
2542 G_WRITE_UNLOCK (&type_rw_lock);
2545 g_return_if_fail (node != NULL);
2549 type_add_flags_W (TypeNode *node,
2554 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2555 g_return_if_fail (node != NULL);
2557 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2558 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2559 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2561 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2565 g_type_query (GType type,
2570 g_return_if_fail (query != NULL);
2572 /* if node is not static and classed, we won't allow query */
2574 node = lookup_type_node_I (type);
2575 if (node && node->is_classed && !node->plugin)
2577 /* type is classed and probably even instantiatable */
2578 G_READ_LOCK (&type_rw_lock);
2579 if (node->data) /* type is static or referenced */
2581 query->type = NODE_TYPE (node);
2582 query->type_name = NODE_NAME (node);
2583 query->class_size = node->data->class.class_size;
2584 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2586 G_READ_UNLOCK (&type_rw_lock);
2591 /* --- implementation details --- */
2593 g_type_test_flags (GType type,
2597 gboolean result = FALSE;
2599 node = lookup_type_node_I (type);
2602 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2603 guint tflags = flags & TYPE_FLAG_MASK;
2607 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2609 fflags = (finfo->type_flags & fflags) == fflags;
2616 G_READ_LOCK (&type_rw_lock);
2617 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2618 G_READ_UNLOCK (&type_rw_lock);
2623 result = tflags && fflags;
2630 g_type_get_plugin (GType type)
2634 node = lookup_type_node_I (type);
2636 return node ? node->plugin : NULL;
2640 g_type_interface_get_plugin (GType instance_type,
2641 GType interface_type)
2646 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
2648 node = lookup_type_node_I (instance_type);
2649 iface = lookup_type_node_I (interface_type);
2652 IFaceHolder *iholder;
2653 GTypePlugin *plugin;
2655 G_READ_LOCK (&type_rw_lock);
2657 iholder = iface_node_get_holders_L (iface);
2658 while (iholder && iholder->instance_type != instance_type)
2659 iholder = iholder->next;
2660 plugin = iholder ? iholder->plugin : NULL;
2662 G_READ_UNLOCK (&type_rw_lock);
2667 g_return_val_if_fail (node == NULL, NULL);
2668 g_return_val_if_fail (iface == NULL, NULL);
2670 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2676 g_type_fundamental_next (void)
2680 G_READ_LOCK (&type_rw_lock);
2681 type = static_fundamental_next;
2682 G_READ_UNLOCK (&type_rw_lock);
2683 type = G_TYPE_MAKE_FUNDAMENTAL (type);
2684 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2688 g_type_fundamental (GType type_id)
2690 TypeNode *node = lookup_type_node_I (type_id);
2692 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2696 g_type_check_instance_is_a (GTypeInstance *type_instance,
2699 TypeNode *node, *iface;
2702 if (!type_instance || !type_instance->g_class)
2705 node = lookup_type_node_I (type_instance->g_class->g_type);
2706 iface = lookup_type_node_I (iface_type);
2707 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2713 g_type_check_class_is_a (GTypeClass *type_class,
2716 TypeNode *node, *iface;
2722 node = lookup_type_node_I (type_class->g_type);
2723 iface = lookup_type_node_I (is_a_type);
2724 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2730 g_type_check_instance_cast (GTypeInstance *type_instance,
2735 if (type_instance->g_class)
2737 TypeNode *node, *iface;
2738 gboolean is_instantiatable, check;
2740 node = lookup_type_node_I (type_instance->g_class->g_type);
2741 is_instantiatable = node && node->is_instantiatable;
2742 iface = lookup_type_node_I (iface_type);
2743 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2745 return type_instance;
2747 if (is_instantiatable)
2748 g_warning ("invalid cast from `%s' to `%s'",
2749 type_descriptive_name_I (type_instance->g_class->g_type),
2750 type_descriptive_name_I (iface_type));
2752 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2753 type_descriptive_name_I (type_instance->g_class->g_type),
2754 type_descriptive_name_I (iface_type));
2757 g_warning ("invalid unclassed pointer in cast to `%s'",
2758 type_descriptive_name_I (iface_type));
2761 return type_instance;
2765 g_type_check_class_cast (GTypeClass *type_class,
2770 TypeNode *node, *iface;
2771 gboolean is_classed, check;
2773 node = lookup_type_node_I (type_class->g_type);
2774 is_classed = node && node->is_classed;
2775 iface = lookup_type_node_I (is_a_type);
2776 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2781 g_warning ("invalid class cast from `%s' to `%s'",
2782 type_descriptive_name_I (type_class->g_type),
2783 type_descriptive_name_I (is_a_type));
2785 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2786 type_descriptive_name_I (type_class->g_type),
2787 type_descriptive_name_I (is_a_type));
2790 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2791 type_descriptive_name_I (is_a_type));
2796 g_type_check_instance (GTypeInstance *type_instance)
2798 /* this function is just here to make the signal system
2799 * conveniently elaborated on instance checks
2803 if (type_instance->g_class)
2805 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2807 if (node && node->is_instantiatable)
2810 g_warning ("instance of invalid non-instantiatable type `%s'",
2811 type_descriptive_name_I (type_instance->g_class->g_type));
2814 g_warning ("instance with invalid (NULL) class pointer");
2817 g_warning ("invalid (NULL) pointer instance");
2822 static inline gboolean
2823 type_check_is_value_type_U (GType type)
2825 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2828 /* common path speed up */
2829 node = lookup_type_node_I (type);
2830 if (node && node->mutatable_check_cache)
2833 G_READ_LOCK (&type_rw_lock);
2837 if (node->data && node->data->common.ref_count > 0 &&
2838 node->data->common.value_table->value_init)
2839 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2840 else if (NODE_IS_IFACE (node))
2844 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2846 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2847 TypeNode *prnode = lookup_type_node_I (prtype);
2849 if (prnode->is_instantiatable)
2852 node = lookup_type_node_I (type);
2858 G_READ_UNLOCK (&type_rw_lock);
2860 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2864 g_type_check_is_value_type (GType type)
2866 return type_check_is_value_type_U (type);
2870 g_type_check_value (GValue *value)
2872 return value && type_check_is_value_type_U (value->g_type);
2876 g_type_check_value_holds (GValue *value,
2879 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2883 g_type_value_table_peek (GType type)
2885 GTypeValueTable *vtable = NULL;
2886 TypeNode *node = lookup_type_node_I (type);
2887 gboolean has_refed_data, has_table;
2890 /* speed up common code path, we're not 100% safe here,
2891 * but we should only get called with referenced types anyway
2893 data = node ? node->data : NULL;
2894 if (node && node->mutatable_check_cache)
2895 return data->common.value_table;
2897 G_READ_LOCK (&type_rw_lock);
2900 has_refed_data = node && node->data && node->data->common.ref_count;
2901 has_table = has_refed_data && node->data->common.value_table->value_init;
2905 vtable = node->data->common.value_table;
2906 else if (NODE_IS_IFACE (node))
2910 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2912 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2913 TypeNode *prnode = lookup_type_node_I (prtype);
2915 if (prnode->is_instantiatable)
2918 node = lookup_type_node_I (type);
2919 goto restart_table_peek;
2925 G_READ_UNLOCK (&type_rw_lock);
2931 g_warning (G_STRLOC ": type id `%lu' is invalid", type);
2932 if (!has_refed_data)
2933 g_warning ("can't peek value table for type `%s' which is not currently referenced",
2934 type_descriptive_name_I (type));
2939 G_CONST_RETURN gchar*
2940 g_type_name_from_instance (GTypeInstance *instance)
2943 return "<NULL-instance>";
2945 return g_type_name_from_class (instance->g_class);
2948 G_CONST_RETURN gchar*
2949 g_type_name_from_class (GTypeClass *g_class)
2952 return "<NULL-class>";
2954 return g_type_name (g_class->g_type);
2958 /* --- foreign prototypes --- */
2959 extern void g_value_c_init (void); /* sync with gvalue.c */
2960 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2961 extern void g_enum_types_init (void); /* sync with genums.c */
2962 extern void g_param_type_init (void); /* sync with gparam.c */
2963 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2964 extern void g_object_type_init (void); /* sync with gobject.c */
2965 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2966 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
2967 extern void g_signal_init (void); /* sync with gsignal.c */
2970 /* --- initialization --- */
2972 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2974 G_LOCK_DEFINE_STATIC (type_init_lock);
2975 const gchar *env_string;
2980 G_LOCK (type_init_lock);
2982 G_WRITE_LOCK (&type_rw_lock);
2984 if (static_quark_type_flags)
2986 G_WRITE_UNLOCK (&type_rw_lock);
2987 G_UNLOCK (type_init_lock);
2991 /* setup GObject library wide debugging flags */
2992 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2993 env_string = g_getenv ("GOBJECT_DEBUG");
2994 if (env_string != NULL)
2996 static GDebugKey debug_keys[] = {
2997 { "objects", G_TYPE_DEBUG_OBJECTS },
2998 { "signals", G_TYPE_DEBUG_SIGNALS },
3001 _g_type_debug_flags |= g_parse_debug_string (env_string,
3003 sizeof (debug_keys) / sizeof (debug_keys[0]));
3008 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3009 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3010 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3012 /* type qname hash table */
3013 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3015 /* invalid type G_TYPE_INVALID (0)
3017 static_fundamental_type_nodes[0] = NULL;
3019 /* void type G_TYPE_NONE
3021 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
3022 type = NODE_TYPE (node);
3023 g_assert (type == G_TYPE_NONE);
3025 /* interface fundamental type G_TYPE_INTERFACE (!classed)
3027 memset (&info, 0, sizeof (info));
3028 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
3029 type = NODE_TYPE (node);
3030 type_data_make_W (node, &info, NULL);
3031 g_assert (type == G_TYPE_INTERFACE);
3033 G_WRITE_UNLOCK (&type_rw_lock);
3037 /* G_TYPE_TYPE_PLUGIN
3039 g_type_plugin_get_type ();
3041 /* G_TYPE_* value types
3043 g_value_types_init ();
3045 /* G_TYPE_ENUM & G_TYPE_FLAGS
3047 g_enum_types_init ();
3051 g_boxed_type_init ();
3055 g_param_type_init ();
3059 g_object_type_init ();
3061 /* G_TYPE_PARAM_* pspec types
3063 g_param_spec_types_init ();
3065 /* Value Transformations
3067 g_value_transforms_init ();
3073 G_UNLOCK (type_init_lock);
3079 g_type_init_with_debug_flags (0);
3083 g_type_class_add_private (gpointer g_class,
3086 GType instance_type = ((GTypeClass *)g_class)->g_type;
3087 TypeNode *node = lookup_type_node_I (instance_type);
3090 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3092 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3093 type_descriptive_name_I (instance_type));
3097 if (NODE_PARENT_TYPE (node))
3099 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3100 if (node->data->instance.private_size != pnode->data->instance.private_size)
3102 g_warning ("g_type_add_private() called multiple times for the same type");
3107 G_WRITE_LOCK (&type_rw_lock);
3109 offset = ALIGN_STRUCT (node->data->instance.private_size);
3110 node->data->instance.private_size = offset + private_size;
3112 G_WRITE_UNLOCK (&type_rw_lock);
3116 g_type_instance_get_private (GTypeInstance *instance,
3119 TypeNode *instance_node;
3120 TypeNode *private_node;
3121 TypeNode *parent_node;
3124 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3126 instance_node = lookup_type_node_I (instance->g_class->g_type);
3127 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3129 g_warning ("instance of invalid non-instantiatable type `%s'",
3130 type_descriptive_name_I (instance->g_class->g_type));
3134 private_node = lookup_type_node_I (private_type);
3135 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3137 g_warning ("attempt to retrieve private data for invalid type '%s'",
3138 type_descriptive_name_I (private_type));
3142 /* Note that we don't need a read lock, since instance existing
3143 * means that the instance class and all parent classes
3144 * exist, so the node->data, node->data->instance.instance_size,
3145 * and node->data->instance.private_size are not going to be changed.
3146 * for any of the relevant types.
3149 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3151 if (NODE_PARENT_TYPE (private_node))
3153 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3154 g_assert (parent_node->data && parent_node->data->common.ref_count);
3156 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3159 return G_STRUCT_MEMBER_P (instance, offset);