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"
28 #include "gbsearcharray.h"
31 #include "gobjectalias.h"
33 /* NOTE: some functions (some internal variants and exported ones)
34 * invalidate data portions of the TypeNodes. if external functions/callbacks
35 * are called, pointers to memory maintained by TypeNodes have to be looked up
36 * again. this affects most of the struct TypeNode fields, e.g. ->children or
37 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
38 * not ->supers[]), as all those memory portions can get realloc()ed during
39 * callback invocation.
42 * - g_type_from_name() should do an ordered array lookup after fetching the
43 * the quark, instead of a second hashtable lookup.
46 * lock handling issues when calling static functions are indicated by
47 * uppercase letter postfixes, all static functions have to have
48 * one of the below postfixes:
49 * - _I: [Indifferent about locking]
50 * function doesn't care about locks at all
51 * - _U: [Unlocked invocation]
52 * no read or write lock has to be held across function invocation
53 * (locks may be acquired and released during invocation though)
54 * - _L: [Locked invocation]
55 * a write lock or more than 0 read locks have to be held across
57 * - _W: [Write-locked invocation]
58 * a write lock has to be held across function invocation
59 * - _Wm: [Write-locked invocation, mutatable]
60 * like _W, but the write lock might be released and reacquired
61 * during invocation, watch your pointers
64 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
66 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
67 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
68 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
69 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
71 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
72 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
73 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
74 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
76 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
77 static const gchar _action[] = " invalidly modified type "; \
78 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
80 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
82 g_error ("%s()%s`%s'", _fname, _action, _tname); \
84 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
87 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
88 "%s: initialization assertion failed, use %s() prior to this function", \
89 G_STRLOC, G_STRINGIFY (init_function)); \
90 return (return_value); \
95 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
96 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
99 #else /* !G_ENABLE_DEBUG */
100 #define DEBUG_CODE(debug_type, code_block) /* code_block */
101 #endif /* G_ENABLE_DEBUG */
103 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
104 G_TYPE_FLAG_INSTANTIATABLE | \
105 G_TYPE_FLAG_DERIVABLE | \
106 G_TYPE_FLAG_DEEP_DERIVABLE)
107 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
108 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
109 sizeof (gpointer)), \
112 /* The 2*sizeof(size_t) alignment here is borrowed from
113 * GNU libc, so it should be good most everywhere.
114 * It is more conservative than is needed on some 64-bit
115 * platforms, but ia64 does require a 16-byte alignment.
116 * The SIMD extensions for x86 and ppc32 would want a
117 * larger alignment than this, but we don't need to
118 * do better than malloc.
120 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
121 #define ALIGN_STRUCT(offset) \
122 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
125 /* --- typedefs --- */
126 typedef struct _TypeNode TypeNode;
127 typedef struct _CommonData CommonData;
128 typedef struct _IFaceData IFaceData;
129 typedef struct _ClassData ClassData;
130 typedef struct _InstanceData InstanceData;
131 typedef union _TypeData TypeData;
132 typedef struct _IFaceEntry IFaceEntry;
133 typedef struct _IFaceHolder IFaceHolder;
136 /* --- prototypes --- */
137 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
138 static void type_add_flags_W (TypeNode *node,
140 static void type_data_make_W (TypeNode *node,
141 const GTypeInfo *info,
142 const GTypeValueTable *value_table);
143 static inline void type_data_ref_Wm (TypeNode *node);
144 static inline void type_data_unref_Wm (TypeNode *node,
146 static void type_data_last_unref_Wm (GType type,
148 static inline gpointer type_get_qdata_L (TypeNode *node,
150 static inline void type_set_qdata_W (TypeNode *node,
153 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
154 GType instance_type);
155 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
157 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
159 static gboolean type_node_is_a_L (TypeNode *node,
160 TypeNode *iface_node);
163 /* --- enumeration --- */
165 /* The InitState enumeration is used to track the progress of initializing
166 * both classes and interface vtables. Keeping the state of initialization
167 * is necessary to handle new interfaces being added while we are initializing
168 * the class or other interfaces.
180 /* --- structures --- */
184 guint n_children : 12;
186 guint _prot_n_ifaces_prerequisites : 9;
187 guint is_classed : 1;
188 guint is_instantiatable : 1;
189 guint mutatable_check_cache : 1; /* combines some common path checks */
191 TypeData * volatile data;
195 IFaceEntry *iface_entries; /* for !iface types */
196 GType *prerequisistes;
198 GType supers[1]; /* flexible array */
200 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
201 #define MAX_N_SUPERS (255)
202 #define MAX_N_CHILDREN (4095)
203 #define MAX_N_IFACES (511)
204 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
205 #define NODE_TYPE(node) (node->supers[0])
206 #define NODE_PARENT_TYPE(node) (node->supers[1])
207 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
208 #define NODE_NAME(node) (g_quark_to_string (node->qname))
209 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
210 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
211 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
212 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
213 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
214 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
215 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
216 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
217 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
218 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
220 #define NODE_IS_ANCESTOR(ancestor, node) \
221 ((ancestor)->n_supers <= (node)->n_supers && \
222 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
228 GInterfaceInfo *info;
235 GTypeInterface *vtable;
236 InitState init_state;
241 GTypeValueTable *value_table;
247 GBaseInitFunc vtable_init_base;
248 GBaseFinalizeFunc vtable_finalize_base;
249 GClassInitFunc dflt_init;
250 GClassFinalizeFunc dflt_finalize;
251 gconstpointer dflt_data;
252 gpointer dflt_vtable;
258 guint init_state : 4;
259 GBaseInitFunc class_init_base;
260 GBaseFinalizeFunc class_finalize_base;
261 GClassInitFunc class_init;
262 GClassFinalizeFunc class_finalize;
263 gconstpointer class_data;
270 guint init_state : 4;
271 GBaseInitFunc class_init_base;
272 GBaseFinalizeFunc class_finalize_base;
273 GClassInitFunc class_init;
274 GClassFinalizeFunc class_finalize;
275 gconstpointer class_data;
277 guint16 instance_size;
278 guint16 private_size;
280 GInstanceInitFunc instance_init;
287 InstanceData instance;
291 GTypeClassCacheFunc cache_func;
295 GTypeInterfaceCheckFunc check_func;
299 /* --- variables --- */
300 static guint static_n_class_cache_funcs = 0;
301 static ClassCacheFunc *static_class_cache_funcs = NULL;
302 static guint static_n_iface_check_funcs = 0;
303 static IFaceCheckFunc *static_iface_check_funcs = NULL;
304 static GQuark static_quark_type_flags = 0;
305 static GQuark static_quark_iface_holder = 0;
306 static GQuark static_quark_dependants_array = 0;
307 GTypeDebugFlags _g_type_debug_flags = 0;
310 /* --- type nodes --- */
311 static GHashTable *static_type_nodes_ht = NULL;
312 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
313 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
315 static inline TypeNode*
316 lookup_type_node_I (register GType utype)
318 if (utype > G_TYPE_FUNDAMENTAL_MAX)
319 return (TypeNode*) (utype & ~TYPE_ID_MASK);
321 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
325 type_node_any_new_W (TypeNode *pnode,
329 GTypeFundamentalFlags type_flags)
334 guint i, node_size = 0;
336 n_supers = pnode ? pnode->n_supers + 1 : 0;
339 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
340 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
341 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
342 node = g_malloc0 (node_size);
343 if (!pnode) /* offset fundamental types */
345 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
346 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
352 g_assert ((type & TYPE_ID_MASK) == 0);
354 node->n_supers = n_supers;
357 node->supers[0] = type;
360 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
361 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
363 if (NODE_IS_IFACE (node))
365 IFACE_NODE_N_PREREQUISITES (node) = 0;
366 IFACE_NODE_PREREQUISITES (node) = NULL;
370 CLASSED_NODE_N_IFACES (node) = 0;
371 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
376 node->supers[0] = type;
377 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
379 node->is_classed = pnode->is_classed;
380 node->is_instantiatable = pnode->is_instantiatable;
382 if (NODE_IS_IFACE (node))
384 IFACE_NODE_N_PREREQUISITES (node) = 0;
385 IFACE_NODE_PREREQUISITES (node) = NULL;
391 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
392 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
393 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
394 CLASSED_NODE_N_IFACES (node));
395 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
397 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
398 CLASSED_NODE_IFACES_ENTRIES (node)[j].init_state = UNINITIALIZED;
402 i = pnode->n_children++;
403 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
404 pnode->children[i] = type;
407 node->plugin = plugin;
408 node->n_children = 0;
409 node->children = NULL;
411 node->qname = g_quark_from_string (name);
412 node->global_gdata = NULL;
414 g_hash_table_insert (static_type_nodes_ht,
415 GUINT_TO_POINTER (node->qname),
420 static inline GTypeFundamentalInfo*
421 type_node_fundamental_info_I (TypeNode *node)
423 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
425 if (ftype != NODE_TYPE (node))
426 node = lookup_type_node_I (ftype);
428 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
432 type_node_fundamental_new_W (GType ftype,
434 GTypeFundamentalFlags type_flags)
436 GTypeFundamentalInfo *finfo;
439 g_assert ((ftype & TYPE_ID_MASK) == 0);
440 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
442 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
443 static_fundamental_next++;
445 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
447 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
449 finfo = type_node_fundamental_info_I (node);
450 finfo->type_flags = type_flags;
456 type_node_new_W (TypeNode *pnode,
462 g_assert (pnode->n_supers < MAX_N_SUPERS);
463 g_assert (pnode->n_children < MAX_N_CHILDREN);
465 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
468 static inline IFaceEntry*
469 type_lookup_iface_entry_L (TypeNode *node,
470 TypeNode *iface_node)
472 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
474 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
475 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
476 GType iface_type = NODE_TYPE (iface_node);
483 i = (n_ifaces + 1) >> 1;
485 if (iface_type == check->iface_type)
487 else if (iface_type > check->iface_type)
492 else /* if (iface_type < check->iface_type) */
501 static inline gboolean
502 type_lookup_prerequisite_L (TypeNode *iface,
503 GType prerequisite_type)
505 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
507 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
508 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
515 i = (n_prerequisites + 1) >> 1;
516 check = prerequisites + i;
517 if (prerequisite_type == *check)
519 else if (prerequisite_type > *check)
521 n_prerequisites -= i;
522 prerequisites = check;
524 else /* if (prerequisite_type < *check) */
525 n_prerequisites = i - 1;
527 while (n_prerequisites);
533 type_descriptive_name_I (GType type)
537 TypeNode *node = lookup_type_node_I (type);
539 return node ? NODE_NAME (node) : "<unknown>";
546 /* --- type consistency checks --- */
548 check_plugin_U (GTypePlugin *plugin,
549 gboolean need_complete_type_info,
550 gboolean need_complete_interface_info,
551 const gchar *type_name)
553 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
557 g_warning ("plugin handle for type `%s' is NULL",
561 if (!G_IS_TYPE_PLUGIN (plugin))
563 g_warning ("plugin pointer (%p) for type `%s' is invalid",
567 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
569 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
573 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
575 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
583 check_type_name_I (const gchar *type_name)
585 static const gchar extra_chars[] = "-_+";
586 const gchar *p = type_name;
589 if (!type_name[0] || !type_name[1] || !type_name[2])
591 g_warning ("type name `%s' is too short", type_name);
594 /* check the first letter */
595 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
596 for (p = type_name + 1; *p; p++)
597 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
598 (p[0] >= 'a' && p[0] <= 'z') ||
599 (p[0] >= '0' && p[0] <= '9') ||
600 strchr (extra_chars, p[0]));
603 g_warning ("type name `%s' contains invalid characters", type_name);
606 if (g_type_from_name (type_name))
608 g_warning ("cannot register existing type `%s'", type_name);
616 check_derivation_I (GType parent_type,
617 const gchar *type_name)
620 GTypeFundamentalInfo* finfo;
622 pnode = lookup_type_node_I (parent_type);
625 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
627 type_descriptive_name_I (parent_type));
630 finfo = type_node_fundamental_info_I (pnode);
631 /* ensure flat derivability */
632 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
634 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
639 /* ensure deep derivability */
640 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
641 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
643 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
653 check_collect_format_I (const gchar *collect_format)
655 const gchar *p = collect_format;
656 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
657 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
658 G_VALUE_COLLECT_POINTER, 0 };
661 if (!strchr (valid_format, *p++))
663 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
667 check_value_table_I (const gchar *type_name,
668 const GTypeValueTable *value_table)
672 else if (value_table->value_init == NULL)
674 if (value_table->value_free || value_table->value_copy ||
675 value_table->value_peek_pointer ||
676 value_table->collect_format || value_table->collect_value ||
677 value_table->lcopy_format || value_table->lcopy_value)
678 g_warning ("cannot handle uninitializable values of type `%s'",
682 else /* value_table->value_init != NULL */
684 if (!value_table->value_free)
687 * g_warning ("missing `value_free()' for type `%s'", type_name);
691 if (!value_table->value_copy)
693 g_warning ("missing `value_copy()' for type `%s'", type_name);
696 if ((value_table->collect_format || value_table->collect_value) &&
697 (!value_table->collect_format || !value_table->collect_value))
699 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
703 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
705 g_warning ("the `%s' specification for type `%s' is too long or invalid",
710 if ((value_table->lcopy_format || value_table->lcopy_value) &&
711 (!value_table->lcopy_format || !value_table->lcopy_value))
713 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
717 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
719 g_warning ("the `%s' specification for type `%s' is too long or invalid",
729 check_type_info_I (TypeNode *pnode,
731 const gchar *type_name,
732 const GTypeInfo *info)
734 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
735 gboolean is_interface = ftype == G_TYPE_INTERFACE;
737 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
739 /* check instance members */
740 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
741 (info->instance_size || info->n_preallocs || info->instance_init))
744 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
748 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
752 /* check class & interface members */
753 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
754 (info->class_init || info->class_finalize || info->class_data ||
755 info->class_size || info->base_init || info->base_finalize))
758 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
762 g_warning ("cannot create class for `%s' as non-classed fundamental",
766 /* check interface size */
767 if (is_interface && info->class_size < sizeof (GTypeInterface))
769 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
773 /* check class size */
774 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
776 if (info->class_size < sizeof (GTypeClass))
778 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
782 if (pnode && info->class_size < pnode->data->class.class_size)
784 g_warning ("specified class size for type `%s' is smaller "
785 "than the parent type's `%s' class size",
791 /* check instance size */
792 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
794 if (info->instance_size < sizeof (GTypeInstance))
796 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
800 if (pnode && info->instance_size < pnode->data->instance.instance_size)
802 g_warning ("specified instance size for type `%s' is smaller "
803 "than the parent type's `%s' instance size",
814 find_conforming_child_type_L (TypeNode *pnode,
817 TypeNode *node = NULL;
820 if (type_lookup_iface_entry_L (pnode, iface))
823 for (i = 0; i < pnode->n_children && !node; i++)
824 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
830 check_add_interface_L (GType instance_type,
833 TypeNode *node = lookup_type_node_I (instance_type);
834 TypeNode *iface = lookup_type_node_I (iface_type);
837 GType *prerequisites;
841 if (!node || !node->is_instantiatable)
843 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
844 type_descriptive_name_I (instance_type));
847 if (!iface || !NODE_IS_IFACE (iface))
849 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
850 type_descriptive_name_I (iface_type),
854 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
855 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
857 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
858 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
864 /* allow overriding of interface type introduced for parent type */
865 entry = type_lookup_iface_entry_L (node, iface);
866 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
868 /* ok, we do conform to this interface already, but the interface vtable was not
869 * yet intialized, and we just conform to the interface because it got added to
870 * one of our parents. so we allow overriding of holder info here.
874 /* check whether one of our children already conforms (or whether the interface
875 * got added to this node already)
877 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
880 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
886 prerequisites = IFACE_NODE_PREREQUISITES (iface);
887 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
889 tnode = lookup_type_node_I (prerequisites[i]);
890 if (!type_node_is_a_L (node, tnode))
892 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
903 check_interface_info_I (TypeNode *iface,
905 const GInterfaceInfo *info)
907 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
909 g_warning ("interface type `%s' for type `%s' comes without initializer",
911 type_descriptive_name_I (instance_type));
918 /* --- type info (type node data) --- */
920 type_data_make_W (TypeNode *node,
921 const GTypeInfo *info,
922 const GTypeValueTable *value_table)
925 GTypeValueTable *vtable = NULL;
926 guint vtable_size = 0;
928 g_assert (node->data == NULL && info != NULL);
932 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
935 vtable = pnode->data->common.value_table;
938 static const GTypeValueTable zero_vtable = { NULL, };
940 value_table = &zero_vtable;
945 /* need to setup vtable_size since we have to allocate it with data in one chunk */
946 vtable_size = sizeof (GTypeValueTable);
947 if (value_table->collect_format)
948 vtable_size += strlen (value_table->collect_format);
949 if (value_table->lcopy_format)
950 vtable_size += strlen (value_table->lcopy_format);
954 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
956 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
958 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
959 data->instance.class_size = info->class_size;
960 data->instance.class_init_base = info->base_init;
961 data->instance.class_finalize_base = info->base_finalize;
962 data->instance.class_init = info->class_init;
963 data->instance.class_finalize = info->class_finalize;
964 data->instance.class_data = info->class_data;
965 data->instance.class = NULL;
966 data->instance.init_state = UNINITIALIZED;
967 data->instance.instance_size = info->instance_size;
968 /* We'll set the final value for data->instance.private size
969 * after the parent class has been initialized
971 data->instance.private_size = 0;
972 #ifdef DISABLE_MEM_POOLS
973 data->instance.n_preallocs = 0;
974 #else /* !DISABLE_MEM_POOLS */
975 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
976 #endif /* !DISABLE_MEM_POOLS */
977 data->instance.instance_init = info->instance_init;
979 else if (node->is_classed) /* only classed */
981 data = g_malloc0 (sizeof (ClassData) + vtable_size);
983 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
984 data->class.class_size = info->class_size;
985 data->class.class_init_base = info->base_init;
986 data->class.class_finalize_base = info->base_finalize;
987 data->class.class_init = info->class_init;
988 data->class.class_finalize = info->class_finalize;
989 data->class.class_data = info->class_data;
990 data->class.class = NULL;
991 data->class.init_state = UNINITIALIZED;
993 else if (NODE_IS_IFACE (node))
995 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
997 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
998 data->iface.vtable_size = info->class_size;
999 data->iface.vtable_init_base = info->base_init;
1000 data->iface.vtable_finalize_base = info->base_finalize;
1001 data->iface.dflt_init = info->class_init;
1002 data->iface.dflt_finalize = info->class_finalize;
1003 data->iface.dflt_data = info->class_data;
1004 data->iface.dflt_vtable = NULL;
1008 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1010 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1014 node->data->common.ref_count = 1;
1020 /* we allocate the vtable and its strings together with the type data, so
1021 * children can take over their parent's vtable pointer, and we don't
1022 * need to worry freeing it or not when the child data is destroyed
1024 *vtable = *value_table;
1025 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1027 vtable->collect_format = p;
1028 if (value_table->collect_format)
1030 strcat (p, value_table->collect_format);
1031 p += strlen (value_table->collect_format);
1035 vtable->lcopy_format = p;
1036 if (value_table->lcopy_format)
1037 strcat (p, value_table->lcopy_format);
1039 node->data->common.value_table = vtable;
1040 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1041 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1042 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1044 g_assert (node->data->common.value_table != NULL); /* paranoid */
1048 type_data_ref_Wm (TypeNode *node)
1052 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1054 GTypeValueTable tmp_value_table;
1056 g_assert (node->plugin != NULL);
1060 type_data_ref_Wm (pnode);
1062 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1065 memset (&tmp_info, 0, sizeof (tmp_info));
1066 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1068 G_WRITE_UNLOCK (&type_rw_lock);
1069 g_type_plugin_use (node->plugin);
1070 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1071 G_WRITE_LOCK (&type_rw_lock);
1073 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1075 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1076 type_data_make_W (node, &tmp_info,
1077 check_value_table_I (NODE_NAME (node),
1078 &tmp_value_table) ? &tmp_value_table : NULL);
1082 g_assert (node->data->common.ref_count > 0);
1084 node->data->common.ref_count += 1;
1089 type_data_unref_Wm (TypeNode *node,
1092 g_assert (node->data && node->data->common.ref_count);
1094 if (node->data->common.ref_count > 1)
1095 node->data->common.ref_count -= 1;
1100 g_warning ("static type `%s' unreferenced too often",
1105 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1110 type_node_add_iface_entry_W (TypeNode *node,
1112 IFaceEntry *parent_entry)
1114 IFaceEntry *entries;
1117 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1119 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1120 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1121 if (entries[i].iface_type == iface_type)
1123 /* this can happen in two cases:
1124 * - our parent type already conformed to iface_type and node
1125 * got it's own holder info. here, our children already have
1126 * entries and NULL vtables, since this will only work for
1127 * uninitialized classes.
1128 * - an interface type is added to an ancestor after it was
1129 * added to a child type.
1132 g_assert (entries[i].vtable == NULL && entries[i].init_state == UNINITIALIZED);
1135 /* sick, interface is added to ancestor *after* child type;
1136 * nothing todo, the entry and our children were already setup correctly
1141 else if (entries[i].iface_type > iface_type)
1143 CLASSED_NODE_N_IFACES (node) += 1;
1144 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1145 CLASSED_NODE_IFACES_ENTRIES (node),
1146 CLASSED_NODE_N_IFACES (node));
1147 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1148 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1149 entries[i].iface_type = iface_type;
1150 entries[i].vtable = NULL;
1151 entries[i].init_state = UNINITIALIZED;
1155 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1157 entries[i].init_state = INITIALIZED;
1158 entries[i].vtable = parent_entry->vtable;
1160 for (i = 0; i < node->n_children; i++)
1161 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries[i]);
1166 type_add_interface_Wm (TypeNode *node,
1168 const GInterfaceInfo *info,
1169 GTypePlugin *plugin)
1171 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1175 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1177 iholder->next = iface_node_get_holders_L (iface);
1178 iface_node_set_holders_W (iface, iholder);
1179 iholder->instance_type = NODE_TYPE (node);
1180 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1181 iholder->plugin = plugin;
1183 /* create an iface entry for this type */
1184 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1186 /* if the class is already (partly) initialized, we may need to base
1187 * initalize and/or initialize the new interface.
1191 InitState class_state = node->data->class.init_state;
1193 if (class_state >= BASE_IFACE_INIT)
1194 type_iface_vtable_base_init_Wm (iface, node);
1196 if (class_state >= IFACE_INIT)
1197 type_iface_vtable_iface_init_Wm (iface, node);
1200 /* create iface entries for children of this type */
1201 entry = type_lookup_iface_entry_L (node, iface);
1202 for (i = 0; i < node->n_children; i++)
1203 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1207 type_iface_add_prerequisite_W (TypeNode *iface,
1208 TypeNode *prerequisite_node)
1210 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1211 GType *prerequisites, *dependants;
1212 guint n_dependants, i;
1214 g_assert (NODE_IS_IFACE (iface) &&
1215 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1216 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1218 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1219 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1220 if (prerequisites[i] == prerequisite_type)
1221 return; /* we already have that prerequisiste */
1222 else if (prerequisites[i] > prerequisite_type)
1224 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1225 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1226 IFACE_NODE_PREREQUISITES (iface),
1227 IFACE_NODE_N_PREREQUISITES (iface));
1228 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1229 g_memmove (prerequisites + i + 1, prerequisites + i,
1230 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1231 prerequisites[i] = prerequisite_type;
1233 /* we want to get notified when prerequisites get added to prerequisite_node */
1234 if (NODE_IS_IFACE (prerequisite_node))
1236 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1237 n_dependants = dependants ? dependants[0] : 0;
1239 dependants = g_renew (GType, dependants, n_dependants + 1);
1240 dependants[n_dependants] = NODE_TYPE (iface);
1241 dependants[0] = n_dependants;
1242 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1245 /* we need to notify all dependants */
1246 dependants = iface_node_get_dependants_array_L (iface);
1247 n_dependants = dependants ? dependants[0] : 0;
1248 for (i = 1; i <= n_dependants; i++)
1249 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1253 g_type_interface_add_prerequisite (GType interface_type,
1254 GType prerequisite_type)
1256 TypeNode *iface, *prerequisite_node;
1257 IFaceHolder *holders;
1259 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1260 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1261 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1263 iface = lookup_type_node_I (interface_type);
1264 prerequisite_node = lookup_type_node_I (prerequisite_type);
1265 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1267 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1268 type_descriptive_name_I (interface_type),
1269 type_descriptive_name_I (prerequisite_type));
1272 G_WRITE_LOCK (&type_rw_lock);
1273 holders = iface_node_get_holders_L (iface);
1276 G_WRITE_UNLOCK (&type_rw_lock);
1277 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1278 type_descriptive_name_I (prerequisite_type),
1279 type_descriptive_name_I (interface_type),
1280 type_descriptive_name_I (holders->instance_type));
1283 if (prerequisite_node->is_instantiatable)
1287 /* can have at most one publically installable instantiatable prerequisite */
1288 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1290 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1292 if (prnode->is_instantiatable)
1294 G_WRITE_UNLOCK (&type_rw_lock);
1295 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1296 type_descriptive_name_I (prerequisite_type),
1297 type_descriptive_name_I (interface_type),
1298 type_descriptive_name_I (NODE_TYPE (prnode)));
1303 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1304 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1305 G_WRITE_UNLOCK (&type_rw_lock);
1307 else if (NODE_IS_IFACE (prerequisite_node))
1309 GType *prerequisites;
1312 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1313 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1314 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1315 type_iface_add_prerequisite_W (iface, prerequisite_node);
1316 G_WRITE_UNLOCK (&type_rw_lock);
1320 G_WRITE_UNLOCK (&type_rw_lock);
1321 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1322 type_descriptive_name_I (prerequisite_type),
1323 type_descriptive_name_I (interface_type));
1327 GType* /* free result */
1328 g_type_interface_prerequisites (GType interface_type,
1329 guint *n_prerequisites)
1333 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1335 iface = lookup_type_node_I (interface_type);
1339 TypeNode *inode = NULL;
1342 G_READ_LOCK (&type_rw_lock);
1343 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1344 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1346 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1347 TypeNode *node = lookup_type_node_I (prerequisite);
1348 if (node->is_instantiatable &&
1349 (!inode || type_node_is_a_L (node, inode)))
1352 types[n++] = NODE_TYPE (node);
1355 types[n++] = NODE_TYPE (inode);
1357 if (n_prerequisites)
1358 *n_prerequisites = n;
1359 G_READ_UNLOCK (&type_rw_lock);
1365 if (n_prerequisites)
1366 *n_prerequisites = 0;
1374 type_iface_peek_holder_L (TypeNode *iface,
1375 GType instance_type)
1377 IFaceHolder *iholder;
1379 g_assert (NODE_IS_IFACE (iface));
1381 iholder = iface_node_get_holders_L (iface);
1382 while (iholder && iholder->instance_type != instance_type)
1383 iholder = iholder->next;
1388 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1389 GType instance_type,
1392 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1394 if (iholder && !iholder->info && need_info)
1396 GInterfaceInfo tmp_info;
1398 g_assert (iholder->plugin != NULL);
1400 type_data_ref_Wm (iface);
1402 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1404 memset (&tmp_info, 0, sizeof (tmp_info));
1406 G_WRITE_UNLOCK (&type_rw_lock);
1407 g_type_plugin_use (iholder->plugin);
1408 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1409 G_WRITE_LOCK (&type_rw_lock);
1411 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1413 check_interface_info_I (iface, instance_type, &tmp_info);
1414 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1417 return iholder; /* we don't modify write lock upon returning NULL */
1421 type_iface_blow_holder_info_Wm (TypeNode *iface,
1422 GType instance_type)
1424 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1426 g_assert (NODE_IS_IFACE (iface));
1428 while (iholder->instance_type != instance_type)
1429 iholder = iholder->next;
1431 if (iholder->info && iholder->plugin)
1433 g_free (iholder->info);
1434 iholder->info = NULL;
1436 G_WRITE_UNLOCK (&type_rw_lock);
1437 g_type_plugin_unuse (iholder->plugin);
1438 G_WRITE_LOCK (&type_rw_lock);
1440 type_data_unref_Wm (iface, FALSE);
1444 /* Assumes type's class already exists
1446 static inline size_t
1447 type_total_instance_size_I (TypeNode *node)
1449 gsize total_instance_size;
1451 total_instance_size = node->data->instance.instance_size;
1452 if (node->data->instance.private_size != 0)
1453 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1455 return total_instance_size;
1458 /* --- type structure creation/destruction --- */
1462 } InstanceRealClass;
1464 instance_real_class_cmp (gconstpointer p1,
1467 const InstanceRealClass *irc1 = p1;
1468 const InstanceRealClass *irc2 = p2;
1469 guint8 *i1 = irc1->instance;
1470 guint8 *i2 = irc2->instance;
1471 return G_BSEARCH_ARRAY_CMP (i1, i2);
1473 G_LOCK_DEFINE_STATIC (instance_real_class);
1474 static GBSearchArray *instance_real_class_bsa = NULL;
1475 static GBSearchConfig instance_real_class_bconfig = {
1476 sizeof (InstanceRealClass),
1477 instance_real_class_cmp,
1481 instance_real_class_set (gpointer instance,
1484 InstanceRealClass key;
1485 key.instance = instance;
1487 G_LOCK (instance_real_class);
1488 if (!instance_real_class_bsa)
1489 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1490 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1491 G_UNLOCK (instance_real_class);
1494 instance_real_class_remove (gpointer instance)
1496 InstanceRealClass key, *node;
1498 key.instance = instance;
1499 G_LOCK (instance_real_class);
1500 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1501 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1502 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1503 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1505 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1506 instance_real_class_bsa = NULL;
1508 G_UNLOCK (instance_real_class);
1510 static inline GTypeClass*
1511 instance_real_class_get (gpointer instance)
1513 InstanceRealClass key, *node;
1514 key.instance = instance;
1515 G_LOCK (instance_real_class);
1516 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1517 G_UNLOCK (instance_real_class);
1518 return node ? node->class : NULL;
1522 g_type_create_instance (GType type)
1525 GTypeInstance *instance;
1527 guint i, total_size;
1529 node = lookup_type_node_I (type);
1530 if (!node || !node->is_instantiatable)
1532 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1533 type_descriptive_name_I (type));
1536 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1537 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1539 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1540 type_descriptive_name_I (type));
1544 class = g_type_class_ref (type);
1545 total_size = type_total_instance_size_I (node);
1547 instance = g_slice_alloc0 (total_size);
1549 if (node->data->instance.private_size)
1550 instance_real_class_set (instance, class);
1551 for (i = node->n_supers; i > 0; i--)
1555 pnode = lookup_type_node_I (node->supers[i]);
1556 if (pnode->data->instance.instance_init)
1558 instance->g_class = pnode->data->instance.class;
1559 pnode->data->instance.instance_init (instance, class);
1562 if (node->data->instance.private_size)
1563 instance_real_class_remove (instance);
1565 instance->g_class = class;
1566 if (node->data->instance.instance_init)
1567 node->data->instance.instance_init (instance, class);
1573 g_type_free_instance (GTypeInstance *instance)
1578 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1580 class = instance->g_class;
1581 node = lookup_type_node_I (class->g_type);
1582 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1584 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1585 type_descriptive_name_I (class->g_type));
1588 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1589 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1591 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1596 instance->g_class = NULL;
1597 #ifdef G_ENABLE_DEBUG
1598 memset (instance, 0xaa, type_total_instance_size_I (node));
1600 g_slice_free1 (type_total_instance_size_I (node), instance);
1602 g_type_class_unref (class);
1606 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1608 g_assert (iface->data);
1610 if (!iface->data->iface.dflt_vtable)
1612 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1613 iface->data->iface.dflt_vtable = vtable;
1614 vtable->g_type = NODE_TYPE (iface);
1615 vtable->g_instance_type = 0;
1616 if (iface->data->iface.vtable_init_base ||
1617 iface->data->iface.dflt_init)
1619 G_WRITE_UNLOCK (&type_rw_lock);
1620 if (iface->data->iface.vtable_init_base)
1621 iface->data->iface.vtable_init_base (vtable);
1622 if (iface->data->iface.dflt_init)
1623 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1624 G_WRITE_LOCK (&type_rw_lock);
1630 /* This is called to allocate and do the first part of initializing
1631 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1633 * A FALSE return indicates that we didn't find an init function for
1634 * this type/iface pair, so the vtable from the parent type should
1635 * be used. Note that the write lock is not modified upon a FALSE
1639 type_iface_vtable_base_init_Wm (TypeNode *iface,
1643 IFaceHolder *iholder;
1644 GTypeInterface *vtable = NULL;
1647 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1648 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1650 return FALSE; /* we don't modify write lock upon FALSE */
1652 type_iface_ensure_dflt_vtable_Wm (iface);
1654 entry = type_lookup_iface_entry_L (node, iface);
1656 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1658 entry->init_state = IFACE_INIT;
1660 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1661 if (pnode) /* want to copy over parent iface contents */
1663 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1666 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1669 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1670 entry->vtable = vtable;
1671 vtable->g_type = NODE_TYPE (iface);
1672 vtable->g_instance_type = NODE_TYPE (node);
1674 if (iface->data->iface.vtable_init_base)
1676 G_WRITE_UNLOCK (&type_rw_lock);
1677 iface->data->iface.vtable_init_base (vtable);
1678 G_WRITE_LOCK (&type_rw_lock);
1680 return TRUE; /* initialized the vtable */
1683 /* Finishes what type_iface_vtable_base_init_Wm started by
1684 * calling the interface init function.
1685 * this function may only be called for types with their
1686 * own interface holder info, i.e. types for which
1687 * g_type_add_interface*() was called and not children thereof.
1690 type_iface_vtable_iface_init_Wm (TypeNode *iface,
1693 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1694 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
1695 GTypeInterface *vtable = NULL;
1698 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
1699 g_assert (iface->data && entry && iholder && iholder->info);
1700 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
1702 entry->init_state = INITIALIZED;
1704 vtable = entry->vtable;
1706 if (iholder->info->interface_init)
1708 G_WRITE_UNLOCK (&type_rw_lock);
1709 if (iholder->info->interface_init)
1710 iholder->info->interface_init (vtable, iholder->info->interface_data);
1711 G_WRITE_LOCK (&type_rw_lock);
1714 for (i = 0; i < static_n_iface_check_funcs; i++)
1716 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
1717 gpointer check_data = static_iface_check_funcs[i].check_data;
1719 G_WRITE_UNLOCK (&type_rw_lock);
1720 check_func (check_data, (gpointer)vtable);
1721 G_WRITE_LOCK (&type_rw_lock);
1726 type_iface_vtable_finalize_Wm (TypeNode *iface,
1728 GTypeInterface *vtable)
1730 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1731 IFaceHolder *iholder;
1733 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1734 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1736 return FALSE; /* we don't modify write lock upon FALSE */
1738 g_assert (entry && entry->vtable == vtable && iholder->info);
1740 entry->vtable = NULL;
1741 entry->init_state = UNINITIALIZED;
1742 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1744 G_WRITE_UNLOCK (&type_rw_lock);
1745 if (iholder->info->interface_finalize)
1746 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1747 if (iface->data->iface.vtable_finalize_base)
1748 iface->data->iface.vtable_finalize_base (vtable);
1749 G_WRITE_LOCK (&type_rw_lock);
1752 vtable->g_instance_type = 0;
1755 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1757 return TRUE; /* write lock modified */
1761 type_class_init_Wm (TypeNode *node,
1764 GSList *slist, *init_slist = NULL;
1767 TypeNode *bnode, *pnode;
1770 g_assert (node->is_classed && node->data &&
1771 node->data->class.class_size &&
1772 !node->data->class.class &&
1773 node->data->class.init_state == UNINITIALIZED);
1775 class = g_malloc0 (node->data->class.class_size);
1776 node->data->class.class = class;
1777 node->data->class.init_state = BASE_CLASS_INIT;
1781 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1783 memcpy (class, pclass, pnode->data->class.class_size);
1785 if (node->is_instantiatable)
1787 /* We need to initialize the private_size here rather than in
1788 * type_data_make_W() since the class init for the parent
1789 * class may have changed pnode->data->instance.private_size.
1791 node->data->instance.private_size = pnode->data->instance.private_size;
1794 class->g_type = NODE_TYPE (node);
1796 G_WRITE_UNLOCK (&type_rw_lock);
1798 /* stack all base class initialization functions, so we
1799 * call them in ascending order.
1801 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1802 if (bnode->data->class.class_init_base)
1803 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1804 for (slist = init_slist; slist; slist = slist->next)
1806 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1808 class_init_base (class);
1810 g_slist_free (init_slist);
1812 G_WRITE_LOCK (&type_rw_lock);
1814 node->data->class.init_state = BASE_IFACE_INIT;
1816 /* Before we initialize the class, base initialize all interfaces, either
1817 * from parent, or through our holder info
1819 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1822 while (i < CLASSED_NODE_N_IFACES (node))
1824 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1825 while (i < CLASSED_NODE_N_IFACES (node) &&
1826 entry->init_state == IFACE_INIT)
1832 if (i == CLASSED_NODE_N_IFACES (node))
1835 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
1839 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
1840 * doesn't modify write lock upon FALSE, so entry is still valid;
1842 g_assert (pnode != NULL);
1844 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1846 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1848 if (pentry->iface_type == entry->iface_type)
1850 entry->vtable = pentry->vtable;
1851 entry->init_state = INITIALIZED;
1855 g_assert (entry->vtable != NULL);
1858 /* If the write lock was released, additional interface entries might
1859 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
1860 * be base-initialized when inserted, so we don't have to worry that
1861 * we might miss them. Uninitialized entries can only be moved higher
1862 * when new ones are inserted.
1867 node->data->class.init_state = CLASS_INIT;
1869 G_WRITE_UNLOCK (&type_rw_lock);
1871 if (node->data->class.class_init)
1872 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1874 G_WRITE_LOCK (&type_rw_lock);
1876 node->data->class.init_state = IFACE_INIT;
1878 /* finish initializing the interfaces through our holder info.
1879 * inherited interfaces are already init_state == INITIALIZED, because
1880 * they either got setup in the above base_init loop, or during
1881 * class_init from within type_add_interface_Wm() for this or
1882 * an anchestor type.
1887 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1888 while (i < CLASSED_NODE_N_IFACES (node) &&
1889 entry->init_state == INITIALIZED)
1895 if (i == CLASSED_NODE_N_IFACES (node))
1898 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
1900 /* As in the loop above, additional initialized entries might be inserted
1901 * if the write lock is released, but that's harmless because the entries
1902 * we need to initialize only move higher in the list.
1907 node->data->class.init_state = INITIALIZED;
1911 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1915 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1918 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1920 IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1923 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1925 /* refetch entries, IFACES_ENTRIES might be modified */
1930 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1931 * iface vtable came from parent
1933 entry->vtable = NULL;
1934 entry->init_state = UNINITIALIZED;
1941 type_data_finalize_class_U (TypeNode *node,
1944 GTypeClass *class = cdata->class;
1947 g_assert (cdata->class && cdata->common.ref_count == 0);
1949 if (cdata->class_finalize)
1950 cdata->class_finalize (class, (gpointer) cdata->class_data);
1952 /* call all base class destruction functions in descending order
1954 if (cdata->class_finalize_base)
1955 cdata->class_finalize_base (class);
1956 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1957 if (bnode->data->class.class_finalize_base)
1958 bnode->data->class.class_finalize_base (class);
1960 g_free (cdata->class);
1964 type_data_last_unref_Wm (GType type,
1967 TypeNode *node = lookup_type_node_I (type);
1969 g_return_if_fail (node != NULL && node->plugin != NULL);
1971 if (!node->data || node->data->common.ref_count == 0)
1973 g_warning ("cannot drop last reference to unreferenced type `%s'",
1974 type_descriptive_name_I (type));
1978 /* call class cache hooks */
1979 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
1983 G_WRITE_UNLOCK (&type_rw_lock);
1984 G_READ_LOCK (&type_rw_lock);
1985 for (i = 0; i < static_n_class_cache_funcs; i++)
1987 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1988 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1989 gboolean need_break;
1991 G_READ_UNLOCK (&type_rw_lock);
1992 need_break = cache_func (cache_data, node->data->class.class);
1993 G_READ_LOCK (&type_rw_lock);
1994 if (!node->data || node->data->common.ref_count == 0)
1995 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1999 G_READ_UNLOCK (&type_rw_lock);
2000 G_WRITE_LOCK (&type_rw_lock);
2003 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
2004 node->data->common.ref_count -= 1;
2007 GType ptype = NODE_PARENT_TYPE (node);
2010 node->data->common.ref_count = 0;
2012 if (node->is_instantiatable)
2014 /* destroy node->data->instance.mem_chunk */
2018 if (node->is_classed && tdata->class.class)
2020 if (CLASSED_NODE_N_IFACES (node))
2021 type_data_finalize_class_ifaces_Wm (node);
2022 node->mutatable_check_cache = FALSE;
2024 G_WRITE_UNLOCK (&type_rw_lock);
2025 type_data_finalize_class_U (node, &tdata->class);
2026 G_WRITE_LOCK (&type_rw_lock);
2028 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2030 node->mutatable_check_cache = FALSE;
2032 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2034 G_WRITE_UNLOCK (&type_rw_lock);
2035 if (tdata->iface.dflt_finalize)
2036 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2037 if (tdata->iface.vtable_finalize_base)
2038 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2039 G_WRITE_LOCK (&type_rw_lock);
2041 g_free (tdata->iface.dflt_vtable);
2045 node->mutatable_check_cache = FALSE;
2049 /* freeing tdata->common.value_table and its contents is taken care of
2050 * by allocating it in one chunk with tdata
2054 G_WRITE_UNLOCK (&type_rw_lock);
2055 g_type_plugin_unuse (node->plugin);
2056 G_WRITE_LOCK (&type_rw_lock);
2058 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
2063 g_type_add_class_cache_func (gpointer cache_data,
2064 GTypeClassCacheFunc cache_func)
2068 g_return_if_fail (cache_func != NULL);
2070 G_WRITE_LOCK (&type_rw_lock);
2071 i = static_n_class_cache_funcs++;
2072 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2073 static_class_cache_funcs[i].cache_data = cache_data;
2074 static_class_cache_funcs[i].cache_func = cache_func;
2075 G_WRITE_UNLOCK (&type_rw_lock);
2079 g_type_remove_class_cache_func (gpointer cache_data,
2080 GTypeClassCacheFunc cache_func)
2082 gboolean found_it = FALSE;
2085 g_return_if_fail (cache_func != NULL);
2087 G_WRITE_LOCK (&type_rw_lock);
2088 for (i = 0; i < static_n_class_cache_funcs; i++)
2089 if (static_class_cache_funcs[i].cache_data == cache_data &&
2090 static_class_cache_funcs[i].cache_func == cache_func)
2092 static_n_class_cache_funcs--;
2093 g_memmove (static_class_cache_funcs + i,
2094 static_class_cache_funcs + i + 1,
2095 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2096 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2100 G_WRITE_UNLOCK (&type_rw_lock);
2103 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2104 cache_func, cache_data);
2109 g_type_add_interface_check (gpointer check_data,
2110 GTypeInterfaceCheckFunc check_func)
2114 g_return_if_fail (check_func != NULL);
2116 G_WRITE_LOCK (&type_rw_lock);
2117 i = static_n_iface_check_funcs++;
2118 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2119 static_iface_check_funcs[i].check_data = check_data;
2120 static_iface_check_funcs[i].check_func = check_func;
2121 G_WRITE_UNLOCK (&type_rw_lock);
2125 g_type_remove_interface_check (gpointer check_data,
2126 GTypeInterfaceCheckFunc check_func)
2128 gboolean found_it = FALSE;
2131 g_return_if_fail (check_func != NULL);
2133 G_WRITE_LOCK (&type_rw_lock);
2134 for (i = 0; i < static_n_iface_check_funcs; i++)
2135 if (static_iface_check_funcs[i].check_data == check_data &&
2136 static_iface_check_funcs[i].check_func == check_func)
2138 static_n_iface_check_funcs--;
2139 g_memmove (static_iface_check_funcs + i,
2140 static_iface_check_funcs + i + 1,
2141 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2142 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2146 G_WRITE_UNLOCK (&type_rw_lock);
2149 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2150 check_func, check_data);
2153 /* --- type registration --- */
2155 g_type_register_fundamental (GType type_id,
2156 const gchar *type_name,
2157 const GTypeInfo *info,
2158 const GTypeFundamentalInfo *finfo,
2163 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2164 g_return_val_if_fail (type_id > 0, 0);
2165 g_return_val_if_fail (type_name != NULL, 0);
2166 g_return_val_if_fail (info != NULL, 0);
2167 g_return_val_if_fail (finfo != NULL, 0);
2169 if (!check_type_name_I (type_name))
2171 if ((type_id & TYPE_ID_MASK) ||
2172 type_id > G_TYPE_FUNDAMENTAL_MAX)
2174 g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
2179 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2180 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2182 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2186 if (lookup_type_node_I (type_id))
2188 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2189 type_descriptive_name_I (type_id),
2194 G_WRITE_LOCK (&type_rw_lock);
2195 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2196 type_add_flags_W (node, flags);
2198 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2199 type_data_make_W (node, info,
2200 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2201 G_WRITE_UNLOCK (&type_rw_lock);
2203 return NODE_TYPE (node);
2207 g_type_register_static_simple (GType parent_type,
2208 const gchar *type_name,
2210 GClassInitFunc class_init,
2211 guint instance_size,
2212 GInstanceInitFunc instance_init,
2217 info.class_size = class_size;
2218 info.base_init = NULL;
2219 info.base_finalize = NULL;
2220 info.class_init = class_init;
2221 info.class_finalize = NULL;
2222 info.class_data = NULL;
2223 info.instance_size = instance_size;
2224 info.n_preallocs = 0;
2225 info.instance_init = instance_init;
2226 info.value_table = NULL;
2228 return g_type_register_static (parent_type, type_name, &info, flags);
2232 g_type_register_static (GType parent_type,
2233 const gchar *type_name,
2234 const GTypeInfo *info,
2237 TypeNode *pnode, *node;
2240 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2241 g_return_val_if_fail (parent_type > 0, 0);
2242 g_return_val_if_fail (type_name != NULL, 0);
2243 g_return_val_if_fail (info != NULL, 0);
2245 if (!check_type_name_I (type_name) ||
2246 !check_derivation_I (parent_type, type_name))
2248 if (info->class_finalize)
2250 g_warning ("class finalizer specified for static type `%s'",
2255 pnode = lookup_type_node_I (parent_type);
2256 G_WRITE_LOCK (&type_rw_lock);
2257 type_data_ref_Wm (pnode);
2258 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2260 node = type_node_new_W (pnode, type_name, NULL);
2261 type_add_flags_W (node, flags);
2262 type = NODE_TYPE (node);
2263 type_data_make_W (node, info,
2264 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2266 G_WRITE_UNLOCK (&type_rw_lock);
2272 g_type_register_dynamic (GType parent_type,
2273 const gchar *type_name,
2274 GTypePlugin *plugin,
2277 TypeNode *pnode, *node;
2280 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2281 g_return_val_if_fail (parent_type > 0, 0);
2282 g_return_val_if_fail (type_name != NULL, 0);
2283 g_return_val_if_fail (plugin != NULL, 0);
2285 if (!check_type_name_I (type_name) ||
2286 !check_derivation_I (parent_type, type_name) ||
2287 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2290 G_WRITE_LOCK (&type_rw_lock);
2291 pnode = lookup_type_node_I (parent_type);
2292 node = type_node_new_W (pnode, type_name, plugin);
2293 type_add_flags_W (node, flags);
2294 type = NODE_TYPE (node);
2295 G_WRITE_UNLOCK (&type_rw_lock);
2301 g_type_add_interface_static (GType instance_type,
2302 GType interface_type,
2303 const GInterfaceInfo *info)
2305 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2306 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2307 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2309 G_WRITE_LOCK (&type_rw_lock);
2310 if (check_add_interface_L (instance_type, interface_type))
2312 TypeNode *node = lookup_type_node_I (instance_type);
2313 TypeNode *iface = lookup_type_node_I (interface_type);
2315 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2316 type_add_interface_Wm (node, iface, info, NULL);
2318 G_WRITE_UNLOCK (&type_rw_lock);
2322 g_type_add_interface_dynamic (GType instance_type,
2323 GType interface_type,
2324 GTypePlugin *plugin)
2328 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2329 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2330 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2332 node = lookup_type_node_I (instance_type);
2333 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2336 G_WRITE_LOCK (&type_rw_lock);
2337 if (check_add_interface_L (instance_type, interface_type))
2339 TypeNode *iface = lookup_type_node_I (interface_type);
2341 type_add_interface_Wm (node, iface, NULL, plugin);
2343 G_WRITE_UNLOCK (&type_rw_lock);
2347 /* --- public API functions --- */
2349 g_type_class_ref (GType type)
2353 /* optimize for common code path
2355 G_WRITE_LOCK (&type_rw_lock);
2356 node = lookup_type_node_I (type);
2357 if (node && node->is_classed && node->data &&
2358 node->data->class.class && node->data->common.ref_count > 0)
2360 type_data_ref_Wm (node);
2361 G_WRITE_UNLOCK (&type_rw_lock);
2363 return node->data->class.class;
2366 if (!node || !node->is_classed ||
2367 (node->data && node->data->common.ref_count < 1))
2369 G_WRITE_UNLOCK (&type_rw_lock);
2370 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2371 type_descriptive_name_I (type));
2375 type_data_ref_Wm (node);
2377 if (!node->data->class.class)
2379 GType ptype = NODE_PARENT_TYPE (node);
2380 GTypeClass *pclass = NULL;
2384 G_WRITE_UNLOCK (&type_rw_lock);
2385 pclass = g_type_class_ref (ptype);
2386 if (node->data->class.class)
2387 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2388 G_WRITE_LOCK (&type_rw_lock);
2391 type_class_init_Wm (node, pclass);
2393 G_WRITE_UNLOCK (&type_rw_lock);
2395 return node->data->class.class;
2399 g_type_class_unref (gpointer g_class)
2402 GTypeClass *class = g_class;
2404 g_return_if_fail (g_class != NULL);
2406 node = lookup_type_node_I (class->g_type);
2407 G_WRITE_LOCK (&type_rw_lock);
2408 if (node && node->is_classed && node->data &&
2409 node->data->class.class == class && node->data->common.ref_count > 0)
2410 type_data_unref_Wm (node, FALSE);
2412 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2413 type_descriptive_name_I (class->g_type));
2414 G_WRITE_UNLOCK (&type_rw_lock);
2418 g_type_class_unref_uncached (gpointer g_class)
2421 GTypeClass *class = g_class;
2423 g_return_if_fail (g_class != NULL);
2425 G_WRITE_LOCK (&type_rw_lock);
2426 node = lookup_type_node_I (class->g_type);
2427 if (node && node->is_classed && node->data &&
2428 node->data->class.class == class && node->data->common.ref_count > 0)
2429 type_data_unref_Wm (node, TRUE);
2431 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2432 type_descriptive_name_I (class->g_type));
2433 G_WRITE_UNLOCK (&type_rw_lock);
2437 g_type_class_peek (GType type)
2442 node = lookup_type_node_I (type);
2443 G_READ_LOCK (&type_rw_lock);
2444 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2445 class = node->data->class.class;
2448 G_READ_UNLOCK (&type_rw_lock);
2454 g_type_class_peek_static (GType type)
2459 node = lookup_type_node_I (type);
2460 G_READ_LOCK (&type_rw_lock);
2461 if (node && node->is_classed && node->data &&
2462 /* peek only static types: */ node->plugin == NULL &&
2463 node->data->class.class) /* common.ref_count _may_ be 0 */
2464 class = node->data->class.class;
2467 G_READ_UNLOCK (&type_rw_lock);
2473 g_type_class_peek_parent (gpointer g_class)
2476 gpointer class = NULL;
2478 g_return_val_if_fail (g_class != NULL, NULL);
2480 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2481 /* We used to acquire a read lock here. That is not necessary, since
2482 * parent->data->class.class is constant as long as the derived class
2485 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2487 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2488 class = node->data->class.class;
2490 else if (NODE_PARENT_TYPE (node))
2491 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2497 g_type_interface_peek (gpointer instance_class,
2502 gpointer vtable = NULL;
2503 GTypeClass *class = instance_class;
2505 g_return_val_if_fail (instance_class != NULL, NULL);
2507 node = lookup_type_node_I (class->g_type);
2508 iface = lookup_type_node_I (iface_type);
2509 if (node && node->is_instantiatable && iface)
2513 G_READ_LOCK (&type_rw_lock);
2515 entry = type_lookup_iface_entry_L (node, iface);
2516 if (entry && entry->vtable) /* entry is relocatable */
2517 vtable = entry->vtable;
2519 G_READ_UNLOCK (&type_rw_lock);
2522 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2528 g_type_interface_peek_parent (gpointer g_iface)
2532 gpointer vtable = NULL;
2533 GTypeInterface *iface_class = g_iface;
2535 g_return_val_if_fail (g_iface != NULL, NULL);
2537 iface = lookup_type_node_I (iface_class->g_type);
2538 node = lookup_type_node_I (iface_class->g_instance_type);
2540 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2541 if (node && node->is_instantiatable && iface)
2545 G_READ_LOCK (&type_rw_lock);
2547 entry = type_lookup_iface_entry_L (node, iface);
2548 if (entry && entry->vtable) /* entry is relocatable */
2549 vtable = entry->vtable;
2551 G_READ_UNLOCK (&type_rw_lock);
2554 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2560 g_type_default_interface_ref (GType g_type)
2564 G_WRITE_LOCK (&type_rw_lock);
2566 node = lookup_type_node_I (g_type);
2567 if (!node || !NODE_IS_IFACE (node) ||
2568 (node->data && node->data->common.ref_count < 1))
2570 G_WRITE_UNLOCK (&type_rw_lock);
2571 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
2572 type_descriptive_name_I (g_type));
2576 type_data_ref_Wm (node);
2578 type_iface_ensure_dflt_vtable_Wm (node);
2580 G_WRITE_UNLOCK (&type_rw_lock);
2582 return node->data->iface.dflt_vtable;
2586 g_type_default_interface_peek (GType g_type)
2591 node = lookup_type_node_I (g_type);
2592 G_READ_LOCK (&type_rw_lock);
2593 if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
2594 vtable = node->data->iface.dflt_vtable;
2597 G_READ_UNLOCK (&type_rw_lock);
2603 g_type_default_interface_unref (gpointer g_iface)
2606 GTypeInterface *vtable = g_iface;
2608 g_return_if_fail (g_iface != NULL);
2610 node = lookup_type_node_I (vtable->g_type);
2611 G_WRITE_LOCK (&type_rw_lock);
2612 if (node && NODE_IS_IFACE (node) &&
2613 node->data->iface.dflt_vtable == g_iface &&
2614 node->data->common.ref_count > 0)
2615 type_data_unref_Wm (node, FALSE);
2617 g_warning ("cannot unreference invalid interface default vtable for '%s'",
2618 type_descriptive_name_I (vtable->g_type));
2619 G_WRITE_UNLOCK (&type_rw_lock);
2622 G_CONST_RETURN gchar*
2623 g_type_name (GType type)
2627 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2629 node = lookup_type_node_I (type);
2631 return node ? NODE_NAME (node) : NULL;
2635 g_type_qname (GType type)
2639 node = lookup_type_node_I (type);
2641 return node ? node->qname : 0;
2645 g_type_from_name (const gchar *name)
2650 g_return_val_if_fail (name != NULL, 0);
2652 quark = g_quark_try_string (name);
2655 G_READ_LOCK (&type_rw_lock);
2656 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2657 G_READ_UNLOCK (&type_rw_lock);
2664 g_type_parent (GType type)
2668 node = lookup_type_node_I (type);
2670 return node ? NODE_PARENT_TYPE (node) : 0;
2674 g_type_depth (GType type)
2678 node = lookup_type_node_I (type);
2680 return node ? node->n_supers + 1 : 0;
2684 g_type_next_base (GType type,
2690 node = lookup_type_node_I (type);
2693 TypeNode *base_node = lookup_type_node_I (base_type);
2695 if (base_node && base_node->n_supers < node->n_supers)
2697 guint n = node->n_supers - base_node->n_supers;
2699 if (node->supers[n] == base_type)
2700 atype = node->supers[n - 1];
2707 static inline gboolean
2708 type_node_check_conformities_UorL (TypeNode *node,
2709 TypeNode *iface_node,
2710 /* support_inheritance */
2711 gboolean support_interfaces,
2712 gboolean support_prerequisites,
2717 if (/* support_inheritance && */
2718 NODE_IS_ANCESTOR (iface_node, node))
2721 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2722 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2724 if (support_interfaces || support_prerequisites)
2727 G_READ_LOCK (&type_rw_lock);
2728 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2730 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2733 G_READ_UNLOCK (&type_rw_lock);
2739 type_node_is_a_L (TypeNode *node,
2740 TypeNode *iface_node)
2742 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2745 static inline gboolean
2746 type_node_conforms_to_U (TypeNode *node,
2747 TypeNode *iface_node,
2748 gboolean support_interfaces,
2749 gboolean support_prerequisites)
2751 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2755 g_type_is_a (GType type,
2758 TypeNode *node, *iface_node;
2761 node = lookup_type_node_I (type);
2762 iface_node = lookup_type_node_I (iface_type);
2763 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2768 GType* /* free result */
2769 g_type_children (GType type,
2774 node = lookup_type_node_I (type);
2779 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2780 children = g_new (GType, node->n_children + 1);
2781 memcpy (children, node->children, sizeof (GType) * node->n_children);
2782 children[node->n_children] = 0;
2785 *n_children = node->n_children;
2786 G_READ_UNLOCK (&type_rw_lock);
2799 GType* /* free result */
2800 g_type_interfaces (GType type,
2801 guint *n_interfaces)
2805 node = lookup_type_node_I (type);
2806 if (node && node->is_instantiatable)
2811 G_READ_LOCK (&type_rw_lock);
2812 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2813 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2814 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2818 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2819 G_READ_UNLOCK (&type_rw_lock);
2832 typedef struct _QData QData;
2844 static inline gpointer
2845 type_get_qdata_L (TypeNode *node,
2848 GData *gdata = node->global_gdata;
2850 if (quark && gdata && gdata->n_qdatas)
2852 QData *qdatas = gdata->qdatas - 1;
2853 guint n_qdatas = gdata->n_qdatas;
2860 i = (n_qdatas + 1) / 2;
2862 if (quark == check->quark)
2864 else if (quark > check->quark)
2869 else /* if (quark < check->quark) */
2878 g_type_get_qdata (GType type,
2884 node = lookup_type_node_I (type);
2887 G_READ_LOCK (&type_rw_lock);
2888 data = type_get_qdata_L (node, quark);
2889 G_READ_UNLOCK (&type_rw_lock);
2893 g_return_val_if_fail (node != NULL, NULL);
2900 type_set_qdata_W (TypeNode *node,
2908 /* setup qdata list if necessary */
2909 if (!node->global_gdata)
2910 node->global_gdata = g_new0 (GData, 1);
2911 gdata = node->global_gdata;
2913 /* try resetting old data */
2914 qdata = gdata->qdatas;
2915 for (i = 0; i < gdata->n_qdatas; i++)
2916 if (qdata[i].quark == quark)
2918 qdata[i].data = data;
2924 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2925 qdata = gdata->qdatas;
2926 for (i = 0; i < gdata->n_qdatas - 1; i++)
2927 if (qdata[i].quark > quark)
2929 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2930 qdata[i].quark = quark;
2931 qdata[i].data = data;
2935 g_type_set_qdata (GType type,
2941 g_return_if_fail (quark != 0);
2943 node = lookup_type_node_I (type);
2946 G_WRITE_LOCK (&type_rw_lock);
2947 type_set_qdata_W (node, quark, data);
2948 G_WRITE_UNLOCK (&type_rw_lock);
2951 g_return_if_fail (node != NULL);
2955 type_add_flags_W (TypeNode *node,
2960 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2961 g_return_if_fail (node != NULL);
2963 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2964 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2965 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2967 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2971 g_type_query (GType type,
2976 g_return_if_fail (query != NULL);
2978 /* if node is not static and classed, we won't allow query */
2980 node = lookup_type_node_I (type);
2981 if (node && node->is_classed && !node->plugin)
2983 /* type is classed and probably even instantiatable */
2984 G_READ_LOCK (&type_rw_lock);
2985 if (node->data) /* type is static or referenced */
2987 query->type = NODE_TYPE (node);
2988 query->type_name = NODE_NAME (node);
2989 query->class_size = node->data->class.class_size;
2990 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2992 G_READ_UNLOCK (&type_rw_lock);
2997 /* --- implementation details --- */
2999 g_type_test_flags (GType type,
3003 gboolean result = FALSE;
3005 node = lookup_type_node_I (type);
3008 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3009 guint tflags = flags & TYPE_FLAG_MASK;
3013 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3015 fflags = (finfo->type_flags & fflags) == fflags;
3022 G_READ_LOCK (&type_rw_lock);
3023 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3024 G_READ_UNLOCK (&type_rw_lock);
3029 result = tflags && fflags;
3036 g_type_get_plugin (GType type)
3040 node = lookup_type_node_I (type);
3042 return node ? node->plugin : NULL;
3046 g_type_interface_get_plugin (GType instance_type,
3047 GType interface_type)
3052 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3054 node = lookup_type_node_I (instance_type);
3055 iface = lookup_type_node_I (interface_type);
3058 IFaceHolder *iholder;
3059 GTypePlugin *plugin;
3061 G_READ_LOCK (&type_rw_lock);
3063 iholder = iface_node_get_holders_L (iface);
3064 while (iholder && iholder->instance_type != instance_type)
3065 iholder = iholder->next;
3066 plugin = iholder ? iholder->plugin : NULL;
3068 G_READ_UNLOCK (&type_rw_lock);
3073 g_return_val_if_fail (node == NULL, NULL);
3074 g_return_val_if_fail (iface == NULL, NULL);
3076 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3082 g_type_fundamental_next (void)
3086 G_READ_LOCK (&type_rw_lock);
3087 type = static_fundamental_next;
3088 G_READ_UNLOCK (&type_rw_lock);
3089 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3090 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3094 g_type_fundamental (GType type_id)
3096 TypeNode *node = lookup_type_node_I (type_id);
3098 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3102 g_type_check_instance_is_a (GTypeInstance *type_instance,
3105 TypeNode *node, *iface;
3108 if (!type_instance || !type_instance->g_class)
3111 node = lookup_type_node_I (type_instance->g_class->g_type);
3112 iface = lookup_type_node_I (iface_type);
3113 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3119 g_type_check_class_is_a (GTypeClass *type_class,
3122 TypeNode *node, *iface;
3128 node = lookup_type_node_I (type_class->g_type);
3129 iface = lookup_type_node_I (is_a_type);
3130 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3136 g_type_check_instance_cast (GTypeInstance *type_instance,
3141 if (type_instance->g_class)
3143 TypeNode *node, *iface;
3144 gboolean is_instantiatable, check;
3146 node = lookup_type_node_I (type_instance->g_class->g_type);
3147 is_instantiatable = node && node->is_instantiatable;
3148 iface = lookup_type_node_I (iface_type);
3149 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3151 return type_instance;
3153 if (is_instantiatable)
3154 g_warning ("invalid cast from `%s' to `%s'",
3155 type_descriptive_name_I (type_instance->g_class->g_type),
3156 type_descriptive_name_I (iface_type));
3158 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3159 type_descriptive_name_I (type_instance->g_class->g_type),
3160 type_descriptive_name_I (iface_type));
3163 g_warning ("invalid unclassed pointer in cast to `%s'",
3164 type_descriptive_name_I (iface_type));
3167 return type_instance;
3171 g_type_check_class_cast (GTypeClass *type_class,
3176 TypeNode *node, *iface;
3177 gboolean is_classed, check;
3179 node = lookup_type_node_I (type_class->g_type);
3180 is_classed = node && node->is_classed;
3181 iface = lookup_type_node_I (is_a_type);
3182 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3187 g_warning ("invalid class cast from `%s' to `%s'",
3188 type_descriptive_name_I (type_class->g_type),
3189 type_descriptive_name_I (is_a_type));
3191 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3192 type_descriptive_name_I (type_class->g_type),
3193 type_descriptive_name_I (is_a_type));
3196 g_warning ("invalid class cast from (NULL) pointer to `%s'",
3197 type_descriptive_name_I (is_a_type));
3202 g_type_check_instance (GTypeInstance *type_instance)
3204 /* this function is just here to make the signal system
3205 * conveniently elaborated on instance checks
3209 if (type_instance->g_class)
3211 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
3213 if (node && node->is_instantiatable)
3216 g_warning ("instance of invalid non-instantiatable type `%s'",
3217 type_descriptive_name_I (type_instance->g_class->g_type));
3220 g_warning ("instance with invalid (NULL) class pointer");
3223 g_warning ("invalid (NULL) pointer instance");
3228 static inline gboolean
3229 type_check_is_value_type_U (GType type)
3231 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
3234 /* common path speed up */
3235 node = lookup_type_node_I (type);
3236 if (node && node->mutatable_check_cache)
3239 G_READ_LOCK (&type_rw_lock);
3243 if (node->data && node->data->common.ref_count > 0 &&
3244 node->data->common.value_table->value_init)
3245 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3246 else if (NODE_IS_IFACE (node))
3250 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3252 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3253 TypeNode *prnode = lookup_type_node_I (prtype);
3255 if (prnode->is_instantiatable)
3258 node = lookup_type_node_I (type);
3264 G_READ_UNLOCK (&type_rw_lock);
3266 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
3270 g_type_check_is_value_type (GType type)
3272 return type_check_is_value_type_U (type);
3276 g_type_check_value (GValue *value)
3278 return value && type_check_is_value_type_U (value->g_type);
3282 g_type_check_value_holds (GValue *value,
3285 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
3289 g_type_value_table_peek (GType type)
3291 GTypeValueTable *vtable = NULL;
3292 TypeNode *node = lookup_type_node_I (type);
3293 gboolean has_refed_data, has_table;
3296 /* speed up common code path, we're not 100% safe here,
3297 * but we should only get called with referenced types anyway
3299 data = node ? node->data : NULL;
3300 if (node && node->mutatable_check_cache)
3301 return data->common.value_table;
3303 G_READ_LOCK (&type_rw_lock);
3306 has_refed_data = node && node->data && node->data->common.ref_count;
3307 has_table = has_refed_data && node->data->common.value_table->value_init;
3311 vtable = node->data->common.value_table;
3312 else if (NODE_IS_IFACE (node))
3316 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3318 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3319 TypeNode *prnode = lookup_type_node_I (prtype);
3321 if (prnode->is_instantiatable)
3324 node = lookup_type_node_I (type);
3325 goto restart_table_peek;
3331 G_READ_UNLOCK (&type_rw_lock);
3337 g_warning (G_STRLOC ": type id `%lu' is invalid", type);
3338 if (!has_refed_data)
3339 g_warning ("can't peek value table for type `%s' which is not currently referenced",
3340 type_descriptive_name_I (type));
3345 G_CONST_RETURN gchar*
3346 g_type_name_from_instance (GTypeInstance *instance)
3349 return "<NULL-instance>";
3351 return g_type_name_from_class (instance->g_class);
3354 G_CONST_RETURN gchar*
3355 g_type_name_from_class (GTypeClass *g_class)
3358 return "<NULL-class>";
3360 return g_type_name (g_class->g_type);
3364 /* --- initialization --- */
3366 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
3368 G_LOCK_DEFINE_STATIC (type_init_lock);
3369 const gchar *env_string;
3374 G_LOCK (type_init_lock);
3376 G_WRITE_LOCK (&type_rw_lock);
3378 if (static_quark_type_flags)
3380 G_WRITE_UNLOCK (&type_rw_lock);
3381 G_UNLOCK (type_init_lock);
3385 /* setup GObject library wide debugging flags */
3386 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
3387 env_string = g_getenv ("GOBJECT_DEBUG");
3388 if (env_string != NULL)
3390 static GDebugKey debug_keys[] = {
3391 { "objects", G_TYPE_DEBUG_OBJECTS },
3392 { "signals", G_TYPE_DEBUG_SIGNALS },
3395 _g_type_debug_flags |= g_parse_debug_string (env_string,
3397 sizeof (debug_keys) / sizeof (debug_keys[0]));
3402 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3403 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3404 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3406 /* type qname hash table */
3407 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3409 /* invalid type G_TYPE_INVALID (0)
3411 static_fundamental_type_nodes[0] = NULL;
3413 /* void type G_TYPE_NONE
3415 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
3416 type = NODE_TYPE (node);
3417 g_assert (type == G_TYPE_NONE);
3419 /* interface fundamental type G_TYPE_INTERFACE (!classed)
3421 memset (&info, 0, sizeof (info));
3422 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
3423 type = NODE_TYPE (node);
3424 type_data_make_W (node, &info, NULL);
3425 g_assert (type == G_TYPE_INTERFACE);
3427 G_WRITE_UNLOCK (&type_rw_lock);
3431 /* G_TYPE_TYPE_PLUGIN
3433 g_type_plugin_get_type ();
3435 /* G_TYPE_* value types
3437 g_value_types_init ();
3439 /* G_TYPE_ENUM & G_TYPE_FLAGS
3441 g_enum_types_init ();
3445 g_boxed_type_init ();
3449 g_param_type_init ();
3453 g_object_type_init ();
3455 /* G_TYPE_PARAM_* pspec types
3457 g_param_spec_types_init ();
3459 /* Value Transformations
3461 g_value_transforms_init ();
3467 G_UNLOCK (type_init_lock);
3473 g_type_init_with_debug_flags (0);
3477 g_type_class_add_private (gpointer g_class,
3480 GType instance_type = ((GTypeClass *)g_class)->g_type;
3481 TypeNode *node = lookup_type_node_I (instance_type);
3484 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3486 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3487 type_descriptive_name_I (instance_type));
3491 if (NODE_PARENT_TYPE (node))
3493 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3494 if (node->data->instance.private_size != pnode->data->instance.private_size)
3496 g_warning ("g_type_add_private() called multiple times for the same type");
3501 G_WRITE_LOCK (&type_rw_lock);
3503 offset = ALIGN_STRUCT (node->data->instance.private_size);
3504 node->data->instance.private_size = offset + private_size;
3506 G_WRITE_UNLOCK (&type_rw_lock);
3510 g_type_instance_get_private (GTypeInstance *instance,
3513 TypeNode *instance_node;
3514 TypeNode *private_node;
3515 TypeNode *parent_node;
3519 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3521 /* while instances are initialized, their class pointers change,
3522 * so figure the instances real class first
3524 class = instance_real_class_get (instance);
3526 class = instance->g_class;
3528 instance_node = lookup_type_node_I (class->g_type);
3529 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3531 g_warning ("instance of invalid non-instantiatable type `%s'",
3532 type_descriptive_name_I (instance->g_class->g_type));
3536 private_node = lookup_type_node_I (private_type);
3537 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3539 g_warning ("attempt to retrieve private data for invalid type '%s'",
3540 type_descriptive_name_I (private_type));
3544 /* Note that we don't need a read lock, since instance existing
3545 * means that the instance class and all parent classes
3546 * exist, so the node->data, node->data->instance.instance_size,
3547 * and node->data->instance.private_size are not going to be changed.
3548 * for any of the relevant types.
3551 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3553 if (NODE_PARENT_TYPE (private_node))
3555 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3556 g_assert (parent_node->data && parent_node->data->common.ref_count);
3558 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
3560 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
3564 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3567 return G_STRUCT_MEMBER_P (instance, offset);
3570 #define __G_TYPE_C__
3571 #include "gobjectaliasdef.c"