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"
32 /* NOTE: some functions (some internal variants and exported ones)
33 * invalidate data portions of the TypeNodes. if external functions/callbacks
34 * are called, pointers to memory maintained by TypeNodes have to be looked up
35 * again. this affects most of the struct TypeNode fields, e.g. ->children or
36 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
37 * not ->supers[]), as all those memory portions can get realloc()ed during
38 * callback invocation.
41 * - g_type_from_name() should do an ordered array lookup after fetching the
42 * the quark, instead of a second hashtable lookup.
45 * lock handling issues when calling static functions are indicated by
46 * uppercase letter postfixes, all static functions have to have
47 * one of the below postfixes:
48 * - _I: [Indifferent about locking]
49 * function doesn't care about locks at all
50 * - _U: [Unlocked invocation]
51 * no read or write lock has to be held across function invocation
52 * (locks may be acquired and released during invocation though)
53 * - _L: [Locked invocation]
54 * a write lock or more than 0 read locks have to be held across
56 * - _W: [Write-locked invocation]
57 * a write lock has to be held across function invokation
58 * - _Wm: [Write-locked invocation, mutatable]
59 * like _W, but the write lock might be released and reacquired
60 * during invocation, watch your pointers
63 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
65 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
66 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
67 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
68 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
70 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
71 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
72 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
73 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
75 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
76 static const gchar *_action = " invalidly modified type "; \
77 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
79 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
81 g_error ("%s()%s`%s'", _fname, _action, _tname); \
83 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
86 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
87 "%s: initialization assertion failed, use %s() prior to this function", \
88 G_STRLOC, G_STRINGIFY (init_function)); \
89 return (return_value); \
94 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
95 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
98 #else /* !G_ENABLE_DEBUG */
99 #define DEBUG_CODE(debug_type, code_block) /* code_block */
100 #endif /* G_ENABLE_DEBUG */
102 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
103 G_TYPE_FLAG_INSTANTIATABLE | \
104 G_TYPE_FLAG_DERIVABLE | \
105 G_TYPE_FLAG_DEEP_DERIVABLE)
106 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
107 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
108 sizeof (gpointer)), \
111 /* The 2*sizeof(size_t) alignment here is borrowed from
112 * GNU libc, so it should be good most everywhere.
113 * It is more conservative than is needed on some 64-bit
114 * platforms, but ia64 does require a 16-byte alignment.
115 * The SIMD extensions for x86 and ppc32 would want a
116 * larger alignment than this, but we don't need to
117 * do better than malloc.
119 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
120 #define ALIGN_STRUCT(offset) \
121 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
124 /* --- typedefs --- */
125 typedef struct _TypeNode TypeNode;
126 typedef struct _CommonData CommonData;
127 typedef struct _IFaceData IFaceData;
128 typedef struct _ClassData ClassData;
129 typedef struct _InstanceData InstanceData;
130 typedef union _TypeData TypeData;
131 typedef struct _IFaceEntry IFaceEntry;
132 typedef struct _IFaceHolder IFaceHolder;
135 /* --- prototypes --- */
136 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
137 static void type_add_flags_W (TypeNode *node,
139 static void type_data_make_W (TypeNode *node,
140 const GTypeInfo *info,
141 const GTypeValueTable *value_table);
142 static inline void type_data_ref_Wm (TypeNode *node);
143 static inline void type_data_unref_Wm (TypeNode *node,
145 static void type_data_last_unref_Wm (GType type,
147 static inline gpointer type_get_qdata_L (TypeNode *node,
149 static inline void type_set_qdata_W (TypeNode *node,
152 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
153 GType instance_type);
154 static gboolean type_node_is_a_L (TypeNode *node,
155 TypeNode *iface_node);
158 /* --- enumeration --- */
160 /* The InitState enumeration is used to track the progress of initializing
161 * both classes and interface vtables. Keeping the state of initialization
162 * is necessary to handle new interfaces being added while we are initializing
163 * the class or other interfaces.
175 /* --- structures --- */
179 guint n_children : 12;
181 guint _prot_n_ifaces_prerequisites : 9;
182 guint is_classed : 1;
183 guint is_instantiatable : 1;
184 guint mutatable_check_cache : 1; /* combines some common path checks */
186 TypeData * volatile data;
190 IFaceEntry *iface_entries; /* for !iface types */
191 GType *prerequisistes;
193 GType supers[1]; /* flexible array */
195 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
196 #define MAX_N_SUPERS (255)
197 #define MAX_N_CHILDREN (4095)
198 #define MAX_N_IFACES (511)
199 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
200 #define NODE_TYPE(node) (node->supers[0])
201 #define NODE_PARENT_TYPE(node) (node->supers[1])
202 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
203 #define NODE_NAME(node) (g_quark_to_string (node->qname))
204 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
205 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
206 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
207 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
208 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
209 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
210 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
211 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
212 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
213 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
215 #define NODE_IS_ANCESTOR(ancestor, node) \
216 ((ancestor)->n_supers <= (node)->n_supers && \
217 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
223 GInterfaceInfo *info;
230 GTypeInterface *vtable;
231 InitState init_state;
236 GTypeValueTable *value_table;
242 GBaseInitFunc vtable_init_base;
243 GBaseFinalizeFunc vtable_finalize_base;
244 GClassInitFunc dflt_init;
245 GClassFinalizeFunc dflt_finalize;
246 gconstpointer dflt_data;
247 gpointer dflt_vtable;
253 guint init_state : 4;
254 GBaseInitFunc class_init_base;
255 GBaseFinalizeFunc class_finalize_base;
256 GClassInitFunc class_init;
257 GClassFinalizeFunc class_finalize;
258 gconstpointer class_data;
265 guint init_state : 4;
266 GBaseInitFunc class_init_base;
267 GBaseFinalizeFunc class_finalize_base;
268 GClassInitFunc class_init;
269 GClassFinalizeFunc class_finalize;
270 gconstpointer class_data;
272 guint16 instance_size;
273 guint16 private_size;
275 GInstanceInitFunc instance_init;
276 GMemChunk *mem_chunk;
283 InstanceData instance;
287 GTypeClassCacheFunc cache_func;
291 GTypeInterfaceCheckFunc check_func;
295 /* --- variables --- */
296 static guint static_n_class_cache_funcs = 0;
297 static ClassCacheFunc *static_class_cache_funcs = NULL;
298 static guint static_n_iface_check_funcs = 0;
299 static IFaceCheckFunc *static_iface_check_funcs = NULL;
300 static GQuark static_quark_type_flags = 0;
301 static GQuark static_quark_iface_holder = 0;
302 static GQuark static_quark_dependants_array = 0;
303 GTypeDebugFlags _g_type_debug_flags = 0;
306 /* --- type nodes --- */
307 static GHashTable *static_type_nodes_ht = NULL;
308 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { 0, };
309 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
311 static inline TypeNode*
312 lookup_type_node_I (register GType utype)
314 if (utype > G_TYPE_FUNDAMENTAL_MAX)
315 return (TypeNode*) (utype & ~TYPE_ID_MASK);
317 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
321 type_node_any_new_W (TypeNode *pnode,
325 GTypeFundamentalFlags type_flags)
330 guint i, node_size = 0;
332 n_supers = pnode ? pnode->n_supers + 1 : 0;
335 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
336 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
337 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
338 node = g_malloc0 (node_size);
339 if (!pnode) /* offset fundamental types */
341 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
342 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
348 g_assert ((type & TYPE_ID_MASK) == 0);
350 node->n_supers = n_supers;
353 node->supers[0] = type;
356 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
357 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
359 if (NODE_IS_IFACE (node))
361 IFACE_NODE_N_PREREQUISITES (node) = 0;
362 IFACE_NODE_PREREQUISITES (node) = NULL;
366 CLASSED_NODE_N_IFACES (node) = 0;
367 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
372 node->supers[0] = type;
373 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
375 node->is_classed = pnode->is_classed;
376 node->is_instantiatable = pnode->is_instantiatable;
378 if (NODE_IS_IFACE (node))
380 IFACE_NODE_N_PREREQUISITES (node) = 0;
381 IFACE_NODE_PREREQUISITES (node) = NULL;
387 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
388 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
389 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
390 CLASSED_NODE_N_IFACES (node));
391 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
393 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
394 CLASSED_NODE_IFACES_ENTRIES (node)[j].init_state = UNINITIALIZED;
398 i = pnode->n_children++;
399 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
400 pnode->children[i] = type;
403 node->plugin = plugin;
404 node->n_children = 0;
405 node->children = NULL;
407 node->qname = g_quark_from_string (name);
408 node->global_gdata = NULL;
410 g_hash_table_insert (static_type_nodes_ht,
411 GUINT_TO_POINTER (node->qname),
416 static inline GTypeFundamentalInfo*
417 type_node_fundamental_info_I (TypeNode *node)
419 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
421 if (ftype != NODE_TYPE (node))
422 node = lookup_type_node_I (ftype);
424 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
428 type_node_fundamental_new_W (GType ftype,
430 GTypeFundamentalFlags type_flags)
432 GTypeFundamentalInfo *finfo;
435 g_assert ((ftype & TYPE_ID_MASK) == 0);
436 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
438 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
439 static_fundamental_next++;
441 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
443 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
445 finfo = type_node_fundamental_info_I (node);
446 finfo->type_flags = type_flags;
452 type_node_new_W (TypeNode *pnode,
458 g_assert (pnode->n_supers < MAX_N_SUPERS);
459 g_assert (pnode->n_children < MAX_N_CHILDREN);
461 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
464 static inline IFaceEntry*
465 type_lookup_iface_entry_L (TypeNode *node,
466 TypeNode *iface_node)
468 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
470 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
471 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
472 GType iface_type = NODE_TYPE (iface_node);
479 i = (n_ifaces + 1) >> 1;
481 if (iface_type == check->iface_type)
483 else if (iface_type > check->iface_type)
488 else /* if (iface_type < check->iface_type) */
497 static inline gboolean
498 type_lookup_prerequisite_L (TypeNode *iface,
499 GType prerequisite_type)
501 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
503 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
504 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
511 i = (n_prerequisites + 1) >> 1;
512 check = prerequisites + i;
513 if (prerequisite_type == *check)
515 else if (prerequisite_type > *check)
517 n_prerequisites -= i;
518 prerequisites = check;
520 else /* if (prerequisite_type < *check) */
521 n_prerequisites = i - 1;
523 while (n_prerequisites);
529 type_descriptive_name_I (GType type)
533 TypeNode *node = lookup_type_node_I (type);
535 return node ? NODE_NAME (node) : "<unknown>";
542 /* --- type consistency checks --- */
544 check_plugin_U (GTypePlugin *plugin,
545 gboolean need_complete_type_info,
546 gboolean need_complete_interface_info,
547 const gchar *type_name)
549 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
553 g_warning ("plugin handle for type `%s' is NULL",
557 if (!G_IS_TYPE_PLUGIN (plugin))
559 g_warning ("plugin pointer (%p) for type `%s' is invalid",
563 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
565 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
569 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
571 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
579 check_type_name_I (const gchar *type_name)
581 static const gchar *extra_chars = "-_+";
582 const gchar *p = type_name;
585 if (!type_name[0] || !type_name[1] || !type_name[2])
587 g_warning ("type name `%s' is too short", type_name);
590 /* check the first letter */
591 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
592 for (p = type_name + 1; *p; p++)
593 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
594 (p[0] >= 'a' && p[0] <= 'z') ||
595 (p[0] >= '0' && p[0] <= '9') ||
596 strchr (extra_chars, p[0]));
599 g_warning ("type name `%s' contains invalid characters", type_name);
602 if (g_type_from_name (type_name))
604 g_warning ("cannot register existing type `%s'", type_name);
612 check_derivation_I (GType parent_type,
613 const gchar *type_name)
616 GTypeFundamentalInfo* finfo;
618 pnode = lookup_type_node_I (parent_type);
621 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
623 type_descriptive_name_I (parent_type));
626 finfo = type_node_fundamental_info_I (pnode);
627 /* ensure flat derivability */
628 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
630 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
635 /* ensure deep derivability */
636 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
637 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
639 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
649 check_collect_format_I (const gchar *collect_format)
651 const gchar *p = collect_format;
652 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
653 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
654 G_VALUE_COLLECT_POINTER, 0 };
657 if (!strchr (valid_format, *p++))
659 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
663 check_value_table_I (const gchar *type_name,
664 const GTypeValueTable *value_table)
668 else if (value_table->value_init == NULL)
670 if (value_table->value_free || value_table->value_copy ||
671 value_table->value_peek_pointer ||
672 value_table->collect_format || value_table->collect_value ||
673 value_table->lcopy_format || value_table->lcopy_value)
674 g_warning ("cannot handle uninitializable values of type `%s'",
678 else /* value_table->value_init != NULL */
680 if (!value_table->value_free)
683 * g_warning ("missing `value_free()' for type `%s'", type_name);
687 if (!value_table->value_copy)
689 g_warning ("missing `value_copy()' for type `%s'", type_name);
692 if ((value_table->collect_format || value_table->collect_value) &&
693 (!value_table->collect_format || !value_table->collect_value))
695 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
699 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
701 g_warning ("the `%s' specification for type `%s' is too long or invalid",
706 if ((value_table->lcopy_format || value_table->lcopy_value) &&
707 (!value_table->lcopy_format || !value_table->lcopy_value))
709 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
713 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
715 g_warning ("the `%s' specification for type `%s' is too long or invalid",
725 check_type_info_I (TypeNode *pnode,
727 const gchar *type_name,
728 const GTypeInfo *info)
730 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
731 gboolean is_interface = ftype == G_TYPE_INTERFACE;
733 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
735 /* check instance members */
736 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
737 (info->instance_size || info->n_preallocs || info->instance_init))
740 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
744 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
748 /* check class & interface members */
749 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
750 (info->class_init || info->class_finalize || info->class_data ||
751 info->class_size || info->base_init || info->base_finalize))
754 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
758 g_warning ("cannot create class for `%s' as non-classed fundamental",
762 /* check interface size */
763 if (is_interface && info->class_size < sizeof (GTypeInterface))
765 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
769 /* check class size */
770 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
772 if (info->class_size < sizeof (GTypeClass))
774 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
778 if (pnode && info->class_size < pnode->data->class.class_size)
780 g_warning ("specified class size for type `%s' is smaller "
781 "than the parent type's `%s' class size",
787 /* check instance size */
788 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
790 if (info->instance_size < sizeof (GTypeInstance))
792 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
796 if (pnode && info->instance_size < pnode->data->instance.instance_size)
798 g_warning ("specified instance size for type `%s' is smaller "
799 "than the parent type's `%s' instance size",
810 find_conforming_child_type_L (TypeNode *pnode,
813 TypeNode *node = NULL;
816 if (type_lookup_iface_entry_L (pnode, iface))
819 for (i = 0; i < pnode->n_children && !node; i++)
820 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
826 check_add_interface_L (GType instance_type,
829 TypeNode *node = lookup_type_node_I (instance_type);
830 TypeNode *iface = lookup_type_node_I (iface_type);
833 GType *prerequisites;
837 if (!node || !node->is_instantiatable)
839 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
840 type_descriptive_name_I (instance_type));
843 if (!iface || !NODE_IS_IFACE (iface))
845 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
846 type_descriptive_name_I (iface_type),
850 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
851 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
853 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
854 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
860 /* allow overriding of interface type introduced for parent type */
861 entry = type_lookup_iface_entry_L (node, iface);
862 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
864 /* ok, we do conform to this interface already, but the interface vtable was not
865 * yet intialized, and we just conform to the interface because it got added to
866 * one of our parents. so we allow overriding of holder info here.
870 /* check whether one of our children already conforms (or whether the interface
871 * got added to this node already)
873 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
876 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
882 prerequisites = IFACE_NODE_PREREQUISITES (iface);
883 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
885 tnode = lookup_type_node_I (prerequisites[i]);
886 if (!type_node_is_a_L (node, tnode))
888 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
899 check_interface_info_I (TypeNode *iface,
901 const GInterfaceInfo *info)
903 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
905 g_warning ("interface type `%s' for type `%s' comes without initializer",
907 type_descriptive_name_I (instance_type));
914 /* --- type info (type node data) --- */
916 type_data_make_W (TypeNode *node,
917 const GTypeInfo *info,
918 const GTypeValueTable *value_table)
921 GTypeValueTable *vtable = NULL;
922 guint vtable_size = 0;
924 g_assert (node->data == NULL && info != NULL);
928 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
931 vtable = pnode->data->common.value_table;
934 static const GTypeValueTable zero_vtable = { NULL, };
936 value_table = &zero_vtable;
941 /* need to setup vtable_size since we have to allocate it with data in one chunk */
942 vtable_size = sizeof (GTypeValueTable);
943 if (value_table->collect_format)
944 vtable_size += strlen (value_table->collect_format);
945 if (value_table->lcopy_format)
946 vtable_size += strlen (value_table->lcopy_format);
950 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
952 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
954 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
955 data->instance.class_size = info->class_size;
956 data->instance.class_init_base = info->base_init;
957 data->instance.class_finalize_base = info->base_finalize;
958 data->instance.class_init = info->class_init;
959 data->instance.class_finalize = info->class_finalize;
960 data->instance.class_data = info->class_data;
961 data->instance.class = NULL;
962 data->instance.init_state = UNINITIALIZED;
963 data->instance.instance_size = info->instance_size;
964 /* We'll set the final value for data->instance.private size
965 * after the parent class has been initialized
967 data->instance.private_size = 0;
968 #ifdef DISABLE_MEM_POOLS
969 data->instance.n_preallocs = 0;
970 #else /* !DISABLE_MEM_POOLS */
971 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
972 #endif /* !DISABLE_MEM_POOLS */
973 data->instance.instance_init = info->instance_init;
974 data->instance.mem_chunk = NULL;
976 else if (node->is_classed) /* only classed */
978 data = g_malloc0 (sizeof (ClassData) + vtable_size);
980 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
981 data->class.class_size = info->class_size;
982 data->class.class_init_base = info->base_init;
983 data->class.class_finalize_base = info->base_finalize;
984 data->class.class_init = info->class_init;
985 data->class.class_finalize = info->class_finalize;
986 data->class.class_data = info->class_data;
987 data->class.class = NULL;
988 data->class.init_state = UNINITIALIZED;
990 else if (NODE_IS_IFACE (node))
992 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
994 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
995 data->iface.vtable_size = info->class_size;
996 data->iface.vtable_init_base = info->base_init;
997 data->iface.vtable_finalize_base = info->base_finalize;
998 data->iface.dflt_init = info->class_init;
999 data->iface.dflt_finalize = info->class_finalize;
1000 data->iface.dflt_data = info->class_data;
1001 data->iface.dflt_vtable = NULL;
1005 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1007 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1011 node->data->common.ref_count = 1;
1017 /* we allocate the vtable and its strings together with the type data, so
1018 * children can take over their parent's vtable pointer, and we don't
1019 * need to worry freeing it or not when the child data is destroyed
1021 *vtable = *value_table;
1022 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1024 vtable->collect_format = p;
1025 if (value_table->collect_format)
1027 strcat (p, value_table->collect_format);
1028 p += strlen (value_table->collect_format);
1032 vtable->lcopy_format = p;
1033 if (value_table->lcopy_format)
1034 strcat (p, value_table->lcopy_format);
1036 node->data->common.value_table = vtable;
1037 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1038 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1039 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1041 g_assert (node->data->common.value_table != NULL); /* paranoid */
1045 type_data_ref_Wm (TypeNode *node)
1049 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1051 GTypeValueTable tmp_value_table;
1053 g_assert (node->plugin != NULL);
1057 type_data_ref_Wm (pnode);
1059 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1062 memset (&tmp_info, 0, sizeof (tmp_info));
1063 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1065 G_WRITE_UNLOCK (&type_rw_lock);
1066 g_type_plugin_use (node->plugin);
1067 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1068 G_WRITE_LOCK (&type_rw_lock);
1070 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1072 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1073 type_data_make_W (node, &tmp_info,
1074 check_value_table_I (NODE_NAME (node),
1075 &tmp_value_table) ? &tmp_value_table : NULL);
1079 g_assert (node->data->common.ref_count > 0);
1081 node->data->common.ref_count += 1;
1086 type_data_unref_Wm (TypeNode *node,
1089 g_assert (node->data && node->data->common.ref_count);
1091 if (node->data->common.ref_count > 1)
1092 node->data->common.ref_count -= 1;
1097 g_warning ("static type `%s' unreferenced too often",
1102 type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1107 type_node_add_iface_entry_W (TypeNode *node,
1110 IFaceEntry *entries;
1113 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1115 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1116 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1117 if (entries[i].iface_type == iface_type)
1119 /* this can (should) only happen if our parent type already conformed
1120 * to iface_type and node got it's own holder info. here, our
1121 * children should already have entries with NULL vtables, so
1122 * we're actually done.
1124 g_assert (entries[i].vtable == NULL);
1127 else if (entries[i].iface_type > iface_type)
1129 CLASSED_NODE_N_IFACES (node) += 1;
1130 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1131 CLASSED_NODE_IFACES_ENTRIES (node),
1132 CLASSED_NODE_N_IFACES (node));
1133 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1134 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1135 entries[i].iface_type = iface_type;
1136 entries[i].vtable = NULL;
1137 entries[i].init_state = UNINITIALIZED;
1139 for (i = 0; i < node->n_children; i++)
1140 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type);
1144 type_add_interface_W (TypeNode *node,
1146 const GInterfaceInfo *info,
1147 GTypePlugin *plugin)
1149 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1151 /* we must not call any functions of GInterfaceInfo from within here, since
1152 * we got most probably called from _within_ a type registration function
1154 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1156 iholder->next = iface_node_get_holders_L (iface);
1157 iface_node_set_holders_W (iface, iholder);
1158 iholder->instance_type = NODE_TYPE (node);
1159 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1160 iholder->plugin = plugin;
1162 type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1166 type_iface_add_prerequisite_W (TypeNode *iface,
1167 TypeNode *prerequisite_node)
1169 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1170 GType *prerequisites, *dependants;
1171 guint n_dependants, i;
1173 g_assert (NODE_IS_IFACE (iface) &&
1174 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1175 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1177 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1178 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1179 if (prerequisites[i] == prerequisite_type)
1180 return; /* we already have that prerequisiste */
1181 else if (prerequisites[i] > prerequisite_type)
1183 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1184 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1185 IFACE_NODE_PREREQUISITES (iface),
1186 IFACE_NODE_N_PREREQUISITES (iface));
1187 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1188 g_memmove (prerequisites + i + 1, prerequisites + i,
1189 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1190 prerequisites[i] = prerequisite_type;
1192 /* we want to get notified when prerequisites get added to prerequisite_node */
1193 if (NODE_IS_IFACE (prerequisite_node))
1195 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1196 n_dependants = dependants ? dependants[0] : 0;
1198 dependants = g_renew (GType, dependants, n_dependants + 1);
1199 dependants[n_dependants] = NODE_TYPE (iface);
1200 dependants[0] = n_dependants;
1201 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1204 /* we need to notify all dependants */
1205 dependants = iface_node_get_dependants_array_L (iface);
1206 n_dependants = dependants ? dependants[0] : 0;
1207 for (i = 1; i <= n_dependants; i++)
1208 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1212 g_type_interface_add_prerequisite (GType interface_type,
1213 GType prerequisite_type)
1215 TypeNode *iface, *prerequisite_node;
1216 IFaceHolder *holders;
1218 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1219 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1220 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1222 iface = lookup_type_node_I (interface_type);
1223 prerequisite_node = lookup_type_node_I (prerequisite_type);
1224 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1226 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1227 type_descriptive_name_I (interface_type),
1228 type_descriptive_name_I (prerequisite_type));
1231 G_WRITE_LOCK (&type_rw_lock);
1232 holders = iface_node_get_holders_L (iface);
1235 G_WRITE_UNLOCK (&type_rw_lock);
1236 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1237 type_descriptive_name_I (prerequisite_type),
1238 type_descriptive_name_I (interface_type),
1239 type_descriptive_name_I (holders->instance_type));
1242 if (prerequisite_node->is_instantiatable)
1246 /* can have at most one publically installable instantiatable prerequisite */
1247 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1249 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1251 if (prnode->is_instantiatable)
1253 G_WRITE_UNLOCK (&type_rw_lock);
1254 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1255 type_descriptive_name_I (prerequisite_type),
1256 type_descriptive_name_I (interface_type),
1257 type_descriptive_name_I (NODE_TYPE (prnode)));
1262 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1263 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1264 G_WRITE_UNLOCK (&type_rw_lock);
1266 else if (NODE_IS_IFACE (prerequisite_node))
1268 GType *prerequisites;
1271 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1272 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1273 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1274 type_iface_add_prerequisite_W (iface, prerequisite_node);
1275 G_WRITE_UNLOCK (&type_rw_lock);
1279 G_WRITE_UNLOCK (&type_rw_lock);
1280 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1281 type_descriptive_name_I (prerequisite_type),
1282 type_descriptive_name_I (interface_type));
1286 GType* /* free result */
1287 g_type_interface_prerequisites (GType interface_type,
1288 guint *n_prerequisites)
1292 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1294 iface = lookup_type_node_I (interface_type);
1298 TypeNode *inode = NULL;
1301 G_READ_LOCK (&type_rw_lock);
1302 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1303 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1305 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1306 TypeNode *node = lookup_type_node_I (prerequisite);
1307 if (node->is_instantiatable &&
1308 (!inode || type_node_is_a_L (node, inode)))
1311 types[n++] = NODE_TYPE (node);
1314 types[n++] = NODE_TYPE (inode);
1316 if (n_prerequisites)
1317 *n_prerequisites = n;
1318 G_READ_UNLOCK (&type_rw_lock);
1324 if (n_prerequisites)
1325 *n_prerequisites = 0;
1333 type_iface_peek_holder_L (TypeNode *iface,
1334 GType instance_type)
1336 IFaceHolder *iholder;
1338 g_assert (NODE_IS_IFACE (iface));
1340 iholder = iface_node_get_holders_L (iface);
1341 while (iholder && iholder->instance_type != instance_type)
1342 iholder = iholder->next;
1347 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1348 GType instance_type,
1351 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1353 if (iholder && !iholder->info && need_info)
1355 GInterfaceInfo tmp_info;
1357 g_assert (iholder->plugin != NULL);
1359 type_data_ref_Wm (iface);
1361 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1363 memset (&tmp_info, 0, sizeof (tmp_info));
1365 G_WRITE_UNLOCK (&type_rw_lock);
1366 g_type_plugin_use (iholder->plugin);
1367 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1368 G_WRITE_LOCK (&type_rw_lock);
1370 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1372 check_interface_info_I (iface, instance_type, &tmp_info);
1373 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1376 return iholder; /* we don't modify write lock upon returning NULL */
1380 type_iface_blow_holder_info_Wm (TypeNode *iface,
1381 GType instance_type)
1383 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1385 g_assert (NODE_IS_IFACE (iface));
1387 while (iholder->instance_type != instance_type)
1388 iholder = iholder->next;
1390 if (iholder->info && iholder->plugin)
1392 g_free (iholder->info);
1393 iholder->info = NULL;
1395 G_WRITE_UNLOCK (&type_rw_lock);
1396 g_type_plugin_unuse (iholder->plugin);
1397 G_WRITE_LOCK (&type_rw_lock);
1399 type_data_unref_Wm (iface, FALSE);
1403 /* Assumes type's class already exists
1405 static inline size_t
1406 type_total_instance_size_I (TypeNode *node)
1408 gsize total_instance_size;
1410 total_instance_size = node->data->instance.instance_size;
1411 if (node->data->instance.private_size != 0)
1412 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1414 return total_instance_size;
1417 /* --- type structure creation/destruction --- */
1421 } InstanceRealClass;
1423 instance_real_class_cmp (gconstpointer p1,
1426 const InstanceRealClass *irc1 = p1;
1427 const InstanceRealClass *irc2 = p2;
1428 guint8 *i1 = irc1->instance;
1429 guint8 *i2 = irc2->instance;
1430 return G_BSEARCH_ARRAY_CMP (i1, i2);
1432 G_LOCK_DEFINE_STATIC (instance_real_class);
1433 static GBSearchArray *instance_real_class_bsa = NULL;
1434 static GBSearchConfig instance_real_class_bconfig = {
1435 sizeof (InstanceRealClass),
1436 instance_real_class_cmp,
1440 instance_real_class_set (gpointer instance,
1443 InstanceRealClass key;
1444 key.instance = instance;
1446 G_LOCK (instance_real_class);
1447 if (!instance_real_class_bsa)
1448 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1449 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1450 G_UNLOCK (instance_real_class);
1453 instance_real_class_remove (gpointer instance)
1455 InstanceRealClass key, *node;
1457 key.instance = instance;
1458 G_LOCK (instance_real_class);
1459 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1460 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1461 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1462 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1464 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1465 instance_real_class_bsa = NULL;
1467 G_UNLOCK (instance_real_class);
1469 static inline GTypeClass*
1470 instance_real_class_get (gpointer instance)
1472 InstanceRealClass key, *node;
1473 key.instance = instance;
1474 G_LOCK (instance_real_class);
1475 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1476 G_UNLOCK (instance_real_class);
1477 return node ? node->class : NULL;
1481 g_type_create_instance (GType type)
1484 GTypeInstance *instance;
1487 gsize total_instance_size;
1489 node = lookup_type_node_I (type);
1490 if (!node || !node->is_instantiatable)
1492 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1493 type_descriptive_name_I (type));
1496 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1497 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1499 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1500 type_descriptive_name_I (type));
1504 class = g_type_class_ref (type);
1506 total_instance_size = type_total_instance_size_I (node);
1508 if (node->data->instance.n_preallocs)
1510 G_WRITE_LOCK (&type_rw_lock);
1511 if (!node->data->instance.mem_chunk)
1513 /* If there isn't private data, the compiler will have already
1514 * added the necessary padding, but in the private data case, we
1515 * have to pad ourselves to ensure proper alignment of all the
1516 * atoms in the slab.
1518 gsize atom_size = total_instance_size;
1519 if (node->data->instance.private_size)
1520 atom_size = ALIGN_STRUCT (atom_size);
1522 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1525 node->data->instance.n_preallocs),
1529 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1530 G_WRITE_UNLOCK (&type_rw_lock);
1533 instance = g_malloc0 (total_instance_size); /* fine without read lock */
1535 if (node->data->instance.private_size)
1536 instance_real_class_set (instance, class);
1537 for (i = node->n_supers; i > 0; i--)
1541 pnode = lookup_type_node_I (node->supers[i]);
1542 if (pnode->data->instance.instance_init)
1544 instance->g_class = pnode->data->instance.class;
1545 pnode->data->instance.instance_init (instance, class);
1548 if (node->data->instance.private_size)
1549 instance_real_class_remove (instance);
1551 instance->g_class = class;
1552 if (node->data->instance.instance_init)
1553 node->data->instance.instance_init (instance, class);
1559 g_type_free_instance (GTypeInstance *instance)
1564 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1566 class = instance->g_class;
1567 node = lookup_type_node_I (class->g_type);
1568 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1570 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1571 type_descriptive_name_I (class->g_type));
1574 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1575 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1577 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1582 instance->g_class = NULL;
1583 #ifdef G_ENABLE_DEBUG
1584 memset (instance, 0xaa, type_total_instance_size_I (node)); /* debugging hack */
1586 if (node->data->instance.n_preallocs)
1588 G_WRITE_LOCK (&type_rw_lock);
1589 g_chunk_free (instance, node->data->instance.mem_chunk);
1590 G_WRITE_UNLOCK (&type_rw_lock);
1595 g_type_class_unref (class);
1599 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1601 g_assert (iface->data);
1603 if (!iface->data->iface.dflt_vtable)
1605 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1606 iface->data->iface.dflt_vtable = vtable;
1607 vtable->g_type = NODE_TYPE (iface);
1608 vtable->g_instance_type = 0;
1609 if (iface->data->iface.vtable_init_base ||
1610 iface->data->iface.dflt_init)
1612 G_WRITE_UNLOCK (&type_rw_lock);
1613 if (iface->data->iface.vtable_init_base)
1614 iface->data->iface.vtable_init_base (vtable);
1615 if (iface->data->iface.dflt_init)
1616 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1617 G_WRITE_LOCK (&type_rw_lock);
1623 /* This is called to allocate and do the first part of initializing
1624 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1626 * A FALSE return indicates that we didn't find an init function for
1627 * this type/iface pair, so the vtable from the parent type should
1628 * be used. Note that the write lock is not modified upon a FALSE
1632 type_iface_vtable_base_init_Wm (TypeNode *iface,
1636 IFaceHolder *iholder;
1637 GTypeInterface *vtable = NULL;
1640 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1641 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1643 return FALSE; /* we don't modify write lock upon FALSE */
1645 type_iface_ensure_dflt_vtable_Wm (iface);
1647 entry = type_lookup_iface_entry_L (node, iface);
1649 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1651 entry->init_state = IFACE_INIT;
1653 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1654 if (pnode) /* want to copy over parent iface contents */
1656 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1659 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1662 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1663 entry->vtable = vtable;
1664 vtable->g_type = NODE_TYPE (iface);
1665 vtable->g_instance_type = NODE_TYPE (node);
1667 if (iface->data->iface.vtable_init_base)
1669 G_WRITE_UNLOCK (&type_rw_lock);
1670 iface->data->iface.vtable_init_base (vtable);
1671 G_WRITE_LOCK (&type_rw_lock);
1673 return TRUE; /* initialized the vtable */
1676 /* Finishes what type_iface_vtable_base_init_Wm started by
1677 * calling the interface init function.
1680 type_iface_vtable_iface_init_Wm (TypeNode *iface,
1683 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1684 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
1685 GTypeInterface *vtable = NULL;
1688 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
1689 g_assert (iface->data && entry && iholder && iholder->info);
1691 entry->init_state = INITIALIZED;
1693 vtable = entry->vtable;
1695 if (iholder->info->interface_init)
1697 G_WRITE_UNLOCK (&type_rw_lock);
1698 if (iholder->info->interface_init)
1699 iholder->info->interface_init (vtable, iholder->info->interface_data);
1700 G_WRITE_LOCK (&type_rw_lock);
1703 for (i = 0; i < static_n_iface_check_funcs; i++)
1705 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
1706 gpointer check_data = static_iface_check_funcs[i].check_data;
1708 G_WRITE_UNLOCK (&type_rw_lock);
1709 check_func (check_data, (gpointer)vtable);
1710 G_WRITE_LOCK (&type_rw_lock);
1715 type_iface_vtable_finalize_Wm (TypeNode *iface,
1717 GTypeInterface *vtable)
1719 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1720 IFaceHolder *iholder;
1722 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1723 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1725 return FALSE; /* we don't modify write lock upon FALSE */
1727 g_assert (entry && entry->vtable == vtable && iholder->info);
1729 entry->vtable = NULL;
1730 entry->init_state = UNINITIALIZED;
1731 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1733 G_WRITE_UNLOCK (&type_rw_lock);
1734 if (iholder->info->interface_finalize)
1735 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1736 if (iface->data->iface.vtable_finalize_base)
1737 iface->data->iface.vtable_finalize_base (vtable);
1738 G_WRITE_LOCK (&type_rw_lock);
1741 vtable->g_instance_type = 0;
1744 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1746 return TRUE; /* write lock modified */
1750 type_class_init_Wm (TypeNode *node,
1753 GSList *slist, *init_slist = NULL;
1756 TypeNode *bnode, *pnode;
1759 g_assert (node->is_classed && node->data &&
1760 node->data->class.class_size &&
1761 !node->data->class.class &&
1762 node->data->class.init_state == UNINITIALIZED);
1764 class = g_malloc0 (node->data->class.class_size);
1765 node->data->class.class = class;
1766 node->data->class.init_state = BASE_CLASS_INIT;
1770 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1772 memcpy (class, pclass, pnode->data->class.class_size);
1774 if (node->is_instantiatable)
1776 /* We need to initialize the private_size here rather than in
1777 * type_data_make_W() since the class init for the parent
1778 * class may have changed pnode->data->instance.private_size.
1780 node->data->instance.private_size = pnode->data->instance.private_size;
1783 class->g_type = NODE_TYPE (node);
1785 G_WRITE_UNLOCK (&type_rw_lock);
1787 /* stack all base class initialization functions, so we
1788 * call them in ascending order.
1790 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1791 if (bnode->data->class.class_init_base)
1792 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1793 for (slist = init_slist; slist; slist = slist->next)
1795 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1797 class_init_base (class);
1799 g_slist_free (init_slist);
1801 G_WRITE_LOCK (&type_rw_lock);
1803 node->data->class.init_state = BASE_IFACE_INIT;
1805 /* Before we initialize the class, base initialize all interfaces, either
1806 * from parent, or through our holder info
1808 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1811 while (i < CLASSED_NODE_N_IFACES (node))
1813 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1814 while (i < CLASSED_NODE_N_IFACES (node) &&
1815 entry->init_state == IFACE_INIT)
1821 if (i == CLASSED_NODE_N_IFACES (node))
1824 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
1828 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
1829 * doesn't modify write lock upon FALSE, so entry is still valid;
1831 g_assert (pnode != NULL);
1833 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1835 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1837 if (pentry->iface_type == entry->iface_type)
1839 entry->vtable = pentry->vtable;
1840 entry->init_state = INITIALIZED;
1844 g_assert (entry->vtable != NULL);
1847 /* If the write lock was released, additional interface entries might
1848 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
1849 * be base-initialized when inserted, so we don't have to worry that
1850 * we might miss them. Uninitialized entries can only be moved higher
1851 * when new ones are inserted.
1856 node->data->class.init_state = CLASS_INIT;
1858 G_WRITE_UNLOCK (&type_rw_lock);
1860 if (node->data->class.class_init)
1861 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1863 G_WRITE_LOCK (&type_rw_lock);
1865 node->data->class.init_state = IFACE_INIT;
1867 /* finish initialize the interfaces through our holder info
1872 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1873 while (i < CLASSED_NODE_N_IFACES (node) &&
1874 entry->init_state == INITIALIZED)
1880 if (i == CLASSED_NODE_N_IFACES (node))
1883 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
1885 /* As in the loop above, additional initialized entries might be inserted
1886 * if the write lock is released, but that's harmless because the entries
1887 * we need to initialize only move higher in the list.
1892 node->data->class.init_state = INITIALIZED;
1895 /* This function is called when we add a new interface to an existing type;
1896 * depending on whether the type already has a class or not, we call
1897 * base-init and interface-init functions. If we are called *while* the
1898 * class is being initialized, then we need to update an array saying which
1899 * interfaces for the class have been initialized.
1902 type_iface_vtable_init_Wm (TypeNode *node,
1905 InitState class_state =
1906 node->data ? node->data->class.init_state : UNINITIALIZED;
1908 if (class_state >= BASE_IFACE_INIT)
1909 type_iface_vtable_base_init_Wm (iface, node);
1911 if (class_state >= IFACE_INIT)
1912 type_iface_vtable_iface_init_Wm (iface, node);
1916 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1920 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1923 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1925 IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1928 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1930 /* refetch entries, IFACES_ENTRIES might be modified */
1935 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1936 * iface vtable came from parent
1938 entry->vtable = NULL;
1939 entry->init_state = UNINITIALIZED;
1946 type_data_finalize_class_U (TypeNode *node,
1949 GTypeClass *class = cdata->class;
1952 g_assert (cdata->class && cdata->common.ref_count == 0);
1954 if (cdata->class_finalize)
1955 cdata->class_finalize (class, (gpointer) cdata->class_data);
1957 /* call all base class destruction functions in descending order
1959 if (cdata->class_finalize_base)
1960 cdata->class_finalize_base (class);
1961 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1962 if (bnode->data->class.class_finalize_base)
1963 bnode->data->class.class_finalize_base (class);
1965 g_free (cdata->class);
1969 type_data_last_unref_Wm (GType type,
1972 TypeNode *node = lookup_type_node_I (type);
1974 g_return_if_fail (node != NULL && node->plugin != NULL);
1976 if (!node->data || node->data->common.ref_count == 0)
1978 g_warning ("cannot drop last reference to unreferenced type `%s'",
1979 type_descriptive_name_I (type));
1983 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1987 G_WRITE_UNLOCK (&type_rw_lock);
1988 G_READ_LOCK (&type_rw_lock);
1989 for (i = 0; i < static_n_class_cache_funcs; i++)
1991 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1992 gpointer cache_data = static_class_cache_funcs[i].cache_data;
1993 gboolean need_break;
1995 G_READ_UNLOCK (&type_rw_lock);
1996 need_break = cache_func (cache_data, node->data->class.class);
1997 G_READ_LOCK (&type_rw_lock);
1998 if (!node->data || node->data->common.ref_count == 0)
1999 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2003 G_READ_UNLOCK (&type_rw_lock);
2004 G_WRITE_LOCK (&type_rw_lock);
2007 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
2008 node->data->common.ref_count -= 1;
2011 GType ptype = NODE_PARENT_TYPE (node);
2014 node->data->common.ref_count = 0;
2016 if (node->is_instantiatable && node->data->instance.mem_chunk)
2018 g_mem_chunk_destroy (node->data->instance.mem_chunk);
2019 node->data->instance.mem_chunk = NULL;
2023 if (node->is_classed && tdata->class.class)
2025 if (CLASSED_NODE_N_IFACES (node))
2026 type_data_finalize_class_ifaces_Wm (node);
2027 node->mutatable_check_cache = FALSE;
2029 G_WRITE_UNLOCK (&type_rw_lock);
2030 type_data_finalize_class_U (node, &tdata->class);
2031 G_WRITE_LOCK (&type_rw_lock);
2033 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2035 node->mutatable_check_cache = FALSE;
2037 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2039 G_WRITE_UNLOCK (&type_rw_lock);
2040 if (tdata->iface.dflt_finalize)
2041 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2042 if (tdata->iface.vtable_finalize_base)
2043 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2044 G_WRITE_LOCK (&type_rw_lock);
2046 g_free (tdata->iface.dflt_vtable);
2050 node->mutatable_check_cache = FALSE;
2054 /* freeing tdata->common.value_table and its contents is taken care of
2055 * by allocating it in one chunk with tdata
2059 G_WRITE_UNLOCK (&type_rw_lock);
2060 g_type_plugin_unuse (node->plugin);
2061 G_WRITE_LOCK (&type_rw_lock);
2063 type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
2068 g_type_add_class_cache_func (gpointer cache_data,
2069 GTypeClassCacheFunc cache_func)
2073 g_return_if_fail (cache_func != NULL);
2075 G_WRITE_LOCK (&type_rw_lock);
2076 i = static_n_class_cache_funcs++;
2077 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2078 static_class_cache_funcs[i].cache_data = cache_data;
2079 static_class_cache_funcs[i].cache_func = cache_func;
2080 G_WRITE_UNLOCK (&type_rw_lock);
2084 g_type_remove_class_cache_func (gpointer cache_data,
2085 GTypeClassCacheFunc cache_func)
2087 gboolean found_it = FALSE;
2090 g_return_if_fail (cache_func != NULL);
2092 G_WRITE_LOCK (&type_rw_lock);
2093 for (i = 0; i < static_n_class_cache_funcs; i++)
2094 if (static_class_cache_funcs[i].cache_data == cache_data &&
2095 static_class_cache_funcs[i].cache_func == cache_func)
2097 static_n_class_cache_funcs--;
2098 g_memmove (static_class_cache_funcs + i,
2099 static_class_cache_funcs + i + 1,
2100 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2101 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2105 G_WRITE_UNLOCK (&type_rw_lock);
2108 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2109 cache_func, cache_data);
2114 g_type_add_interface_check (gpointer check_data,
2115 GTypeInterfaceCheckFunc check_func)
2119 g_return_if_fail (check_func != NULL);
2121 G_WRITE_LOCK (&type_rw_lock);
2122 i = static_n_iface_check_funcs++;
2123 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2124 static_iface_check_funcs[i].check_data = check_data;
2125 static_iface_check_funcs[i].check_func = check_func;
2126 G_WRITE_UNLOCK (&type_rw_lock);
2130 g_type_remove_interface_check (gpointer check_data,
2131 GTypeInterfaceCheckFunc check_func)
2133 gboolean found_it = FALSE;
2136 g_return_if_fail (check_func != NULL);
2138 G_WRITE_LOCK (&type_rw_lock);
2139 for (i = 0; i < static_n_iface_check_funcs; i++)
2140 if (static_iface_check_funcs[i].check_data == check_data &&
2141 static_iface_check_funcs[i].check_func == check_func)
2143 static_n_iface_check_funcs--;
2144 g_memmove (static_iface_check_funcs + i,
2145 static_iface_check_funcs + i + 1,
2146 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2147 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2151 G_WRITE_UNLOCK (&type_rw_lock);
2154 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2155 check_func, check_data);
2158 /* --- type registration --- */
2160 g_type_register_fundamental (GType type_id,
2161 const gchar *type_name,
2162 const GTypeInfo *info,
2163 const GTypeFundamentalInfo *finfo,
2166 GTypeFundamentalInfo *node_finfo;
2169 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2170 g_return_val_if_fail (type_id > 0, 0);
2171 g_return_val_if_fail (type_name != NULL, 0);
2172 g_return_val_if_fail (info != NULL, 0);
2173 g_return_val_if_fail (finfo != NULL, 0);
2175 if (!check_type_name_I (type_name))
2177 if ((type_id & TYPE_ID_MASK) ||
2178 type_id > G_TYPE_FUNDAMENTAL_MAX)
2180 g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
2185 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2186 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2188 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2192 if (lookup_type_node_I (type_id))
2194 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2195 type_descriptive_name_I (type_id),
2200 G_WRITE_LOCK (&type_rw_lock);
2201 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2202 node_finfo = type_node_fundamental_info_I (node);
2203 type_add_flags_W (node, flags);
2205 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2206 type_data_make_W (node, info,
2207 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2208 G_WRITE_UNLOCK (&type_rw_lock);
2210 return NODE_TYPE (node);
2214 g_type_register_static (GType parent_type,
2215 const gchar *type_name,
2216 const GTypeInfo *info,
2219 TypeNode *pnode, *node;
2222 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2223 g_return_val_if_fail (parent_type > 0, 0);
2224 g_return_val_if_fail (type_name != NULL, 0);
2225 g_return_val_if_fail (info != NULL, 0);
2227 if (!check_type_name_I (type_name) ||
2228 !check_derivation_I (parent_type, type_name))
2230 if (info->class_finalize)
2232 g_warning ("class finalizer specified for static type `%s'",
2237 pnode = lookup_type_node_I (parent_type);
2238 G_WRITE_LOCK (&type_rw_lock);
2239 type_data_ref_Wm (pnode);
2240 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2242 node = type_node_new_W (pnode, type_name, NULL);
2243 type_add_flags_W (node, flags);
2244 type = NODE_TYPE (node);
2245 type_data_make_W (node, info,
2246 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2248 G_WRITE_UNLOCK (&type_rw_lock);
2254 g_type_register_dynamic (GType parent_type,
2255 const gchar *type_name,
2256 GTypePlugin *plugin,
2259 TypeNode *pnode, *node;
2262 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2263 g_return_val_if_fail (parent_type > 0, 0);
2264 g_return_val_if_fail (type_name != NULL, 0);
2265 g_return_val_if_fail (plugin != NULL, 0);
2267 if (!check_type_name_I (type_name) ||
2268 !check_derivation_I (parent_type, type_name) ||
2269 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2272 G_WRITE_LOCK (&type_rw_lock);
2273 pnode = lookup_type_node_I (parent_type);
2274 node = type_node_new_W (pnode, type_name, plugin);
2275 type_add_flags_W (node, flags);
2276 type = NODE_TYPE (node);
2277 G_WRITE_UNLOCK (&type_rw_lock);
2283 g_type_add_interface_static (GType instance_type,
2284 GType interface_type,
2285 const GInterfaceInfo *info)
2287 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2288 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2289 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2291 G_WRITE_LOCK (&type_rw_lock);
2292 if (check_add_interface_L (instance_type, interface_type))
2294 TypeNode *node = lookup_type_node_I (instance_type);
2295 TypeNode *iface = lookup_type_node_I (interface_type);
2297 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2299 type_add_interface_W (node, iface, info, NULL);
2300 /* If we have a class already, we may need to base initalize
2301 * and/or initialize the new interface.
2303 type_iface_vtable_init_Wm (node, iface);
2306 G_WRITE_UNLOCK (&type_rw_lock);
2310 g_type_add_interface_dynamic (GType instance_type,
2311 GType interface_type,
2312 GTypePlugin *plugin)
2316 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2317 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2318 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2320 node = lookup_type_node_I (instance_type);
2321 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2324 G_WRITE_LOCK (&type_rw_lock);
2325 if (check_add_interface_L (instance_type, interface_type))
2327 TypeNode *iface = lookup_type_node_I (interface_type);
2329 type_add_interface_W (node, iface, NULL, plugin);
2330 /* If we have a class already, we may need to base initalize
2331 * and/or initialize the new interface.
2333 type_iface_vtable_init_Wm (node, iface);
2335 G_WRITE_UNLOCK (&type_rw_lock);
2339 /* --- public API functions --- */
2341 g_type_class_ref (GType type)
2345 /* optimize for common code path
2347 G_WRITE_LOCK (&type_rw_lock);
2348 node = lookup_type_node_I (type);
2349 if (node && node->is_classed && node->data &&
2350 node->data->class.class && node->data->common.ref_count > 0)
2352 type_data_ref_Wm (node);
2353 G_WRITE_UNLOCK (&type_rw_lock);
2355 return node->data->class.class;
2358 if (!node || !node->is_classed ||
2359 (node->data && node->data->common.ref_count < 1))
2361 G_WRITE_UNLOCK (&type_rw_lock);
2362 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2363 type_descriptive_name_I (type));
2367 type_data_ref_Wm (node);
2369 if (!node->data->class.class)
2371 GType ptype = NODE_PARENT_TYPE (node);
2372 GTypeClass *pclass = NULL;
2376 G_WRITE_UNLOCK (&type_rw_lock);
2377 pclass = g_type_class_ref (ptype);
2378 if (node->data->class.class)
2379 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2380 G_WRITE_LOCK (&type_rw_lock);
2383 type_class_init_Wm (node, pclass);
2385 G_WRITE_UNLOCK (&type_rw_lock);
2387 return node->data->class.class;
2391 g_type_class_unref (gpointer g_class)
2394 GTypeClass *class = g_class;
2396 g_return_if_fail (g_class != NULL);
2398 node = lookup_type_node_I (class->g_type);
2399 G_WRITE_LOCK (&type_rw_lock);
2400 if (node && node->is_classed && node->data &&
2401 node->data->class.class == class && node->data->common.ref_count > 0)
2402 type_data_unref_Wm (node, FALSE);
2404 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2405 type_descriptive_name_I (class->g_type));
2406 G_WRITE_UNLOCK (&type_rw_lock);
2410 g_type_class_unref_uncached (gpointer g_class)
2413 GTypeClass *class = g_class;
2415 g_return_if_fail (g_class != NULL);
2417 G_WRITE_LOCK (&type_rw_lock);
2418 node = lookup_type_node_I (class->g_type);
2419 if (node && node->is_classed && node->data &&
2420 node->data->class.class == class && node->data->common.ref_count > 0)
2421 type_data_unref_Wm (node, TRUE);
2423 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2424 type_descriptive_name_I (class->g_type));
2425 G_WRITE_UNLOCK (&type_rw_lock);
2429 g_type_class_peek (GType type)
2434 node = lookup_type_node_I (type);
2435 G_READ_LOCK (&type_rw_lock);
2436 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2437 class = node->data->class.class;
2440 G_READ_UNLOCK (&type_rw_lock);
2446 g_type_class_peek_parent (gpointer g_class)
2449 gpointer class = NULL;
2451 g_return_val_if_fail (g_class != NULL, NULL);
2453 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2454 G_READ_LOCK (&type_rw_lock);
2455 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2457 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2458 class = node->data->class.class;
2460 else if (NODE_PARENT_TYPE (node))
2461 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2462 G_READ_UNLOCK (&type_rw_lock);
2468 g_type_interface_peek (gpointer instance_class,
2473 gpointer vtable = NULL;
2474 GTypeClass *class = instance_class;
2476 g_return_val_if_fail (instance_class != NULL, NULL);
2478 node = lookup_type_node_I (class->g_type);
2479 iface = lookup_type_node_I (iface_type);
2480 if (node && node->is_instantiatable && iface)
2484 G_READ_LOCK (&type_rw_lock);
2486 entry = type_lookup_iface_entry_L (node, iface);
2487 if (entry && entry->vtable) /* entry is relocatable */
2488 vtable = entry->vtable;
2490 G_READ_UNLOCK (&type_rw_lock);
2493 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2499 g_type_interface_peek_parent (gpointer g_iface)
2503 gpointer vtable = NULL;
2504 GTypeInterface *iface_class = g_iface;
2506 g_return_val_if_fail (g_iface != NULL, NULL);
2508 iface = lookup_type_node_I (iface_class->g_type);
2509 node = lookup_type_node_I (iface_class->g_instance_type);
2511 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2512 if (node && node->is_instantiatable && iface)
2516 G_READ_LOCK (&type_rw_lock);
2518 entry = type_lookup_iface_entry_L (node, iface);
2519 if (entry && entry->vtable) /* entry is relocatable */
2520 vtable = entry->vtable;
2522 G_READ_UNLOCK (&type_rw_lock);
2525 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2531 g_type_default_interface_ref (GType g_type)
2535 G_WRITE_LOCK (&type_rw_lock);
2537 node = lookup_type_node_I (g_type);
2538 if (!node || !NODE_IS_IFACE (node) ||
2539 (node->data && node->data->common.ref_count < 1))
2541 G_WRITE_UNLOCK (&type_rw_lock);
2542 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
2543 type_descriptive_name_I (g_type));
2547 type_data_ref_Wm (node);
2549 type_iface_ensure_dflt_vtable_Wm (node);
2551 G_WRITE_UNLOCK (&type_rw_lock);
2553 return node->data->iface.dflt_vtable;
2557 g_type_default_interface_peek (GType g_type)
2562 node = lookup_type_node_I (g_type);
2563 G_READ_LOCK (&type_rw_lock);
2564 if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
2565 vtable = node->data->iface.dflt_vtable;
2568 G_READ_UNLOCK (&type_rw_lock);
2574 g_type_default_interface_unref (gpointer g_iface)
2577 GTypeInterface *vtable = g_iface;
2579 g_return_if_fail (g_iface != NULL);
2581 node = lookup_type_node_I (vtable->g_type);
2582 G_WRITE_LOCK (&type_rw_lock);
2583 if (node && NODE_IS_IFACE (node) &&
2584 node->data->iface.dflt_vtable == g_iface &&
2585 node->data->common.ref_count > 0)
2586 type_data_unref_Wm (node, FALSE);
2588 g_warning ("cannot unreference invalid interface default vtable for '%s'",
2589 type_descriptive_name_I (vtable->g_type));
2590 G_WRITE_UNLOCK (&type_rw_lock);
2593 G_CONST_RETURN gchar*
2594 g_type_name (GType type)
2598 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2600 node = lookup_type_node_I (type);
2602 return node ? NODE_NAME (node) : NULL;
2606 g_type_qname (GType type)
2610 node = lookup_type_node_I (type);
2612 return node ? node->qname : 0;
2616 g_type_from_name (const gchar *name)
2621 g_return_val_if_fail (name != NULL, 0);
2623 quark = g_quark_try_string (name);
2626 G_READ_LOCK (&type_rw_lock);
2627 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2628 G_READ_UNLOCK (&type_rw_lock);
2635 g_type_parent (GType type)
2639 node = lookup_type_node_I (type);
2641 return node ? NODE_PARENT_TYPE (node) : 0;
2645 g_type_depth (GType type)
2649 node = lookup_type_node_I (type);
2651 return node ? node->n_supers + 1 : 0;
2655 g_type_next_base (GType type,
2661 node = lookup_type_node_I (type);
2664 TypeNode *base_node = lookup_type_node_I (base_type);
2666 if (base_node && base_node->n_supers < node->n_supers)
2668 guint n = node->n_supers - base_node->n_supers;
2670 if (node->supers[n] == base_type)
2671 atype = node->supers[n - 1];
2678 static inline gboolean
2679 type_node_check_conformities_UorL (TypeNode *node,
2680 TypeNode *iface_node,
2681 /* support_inheritance */
2682 gboolean support_interfaces,
2683 gboolean support_prerequisites,
2688 if (/* support_inheritance && */
2689 NODE_IS_ANCESTOR (iface_node, node))
2692 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2693 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2695 if (support_interfaces || support_prerequisites)
2698 G_READ_LOCK (&type_rw_lock);
2699 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2701 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2704 G_READ_UNLOCK (&type_rw_lock);
2710 type_node_is_a_L (TypeNode *node,
2711 TypeNode *iface_node)
2713 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2716 static inline gboolean
2717 type_node_conforms_to_U (TypeNode *node,
2718 TypeNode *iface_node,
2719 gboolean support_interfaces,
2720 gboolean support_prerequisites)
2722 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2726 g_type_is_a (GType type,
2729 TypeNode *node, *iface_node;
2732 node = lookup_type_node_I (type);
2733 iface_node = lookup_type_node_I (iface_type);
2734 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2739 GType* /* free result */
2740 g_type_children (GType type,
2745 node = lookup_type_node_I (type);
2750 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
2751 children = g_new (GType, node->n_children + 1);
2752 memcpy (children, node->children, sizeof (GType) * node->n_children);
2753 children[node->n_children] = 0;
2756 *n_children = node->n_children;
2757 G_READ_UNLOCK (&type_rw_lock);
2770 GType* /* free result */
2771 g_type_interfaces (GType type,
2772 guint *n_interfaces)
2776 node = lookup_type_node_I (type);
2777 if (node && node->is_instantiatable)
2782 G_READ_LOCK (&type_rw_lock);
2783 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2784 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2785 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2789 *n_interfaces = CLASSED_NODE_N_IFACES (node);
2790 G_READ_UNLOCK (&type_rw_lock);
2803 typedef struct _QData QData;
2815 static inline gpointer
2816 type_get_qdata_L (TypeNode *node,
2819 GData *gdata = node->global_gdata;
2821 if (quark && gdata && gdata->n_qdatas)
2823 QData *qdatas = gdata->qdatas - 1;
2824 guint n_qdatas = gdata->n_qdatas;
2831 i = (n_qdatas + 1) / 2;
2833 if (quark == check->quark)
2835 else if (quark > check->quark)
2840 else /* if (quark < check->quark) */
2849 g_type_get_qdata (GType type,
2855 node = lookup_type_node_I (type);
2858 G_READ_LOCK (&type_rw_lock);
2859 data = type_get_qdata_L (node, quark);
2860 G_READ_UNLOCK (&type_rw_lock);
2864 g_return_val_if_fail (node != NULL, NULL);
2871 type_set_qdata_W (TypeNode *node,
2879 /* setup qdata list if necessary */
2880 if (!node->global_gdata)
2881 node->global_gdata = g_new0 (GData, 1);
2882 gdata = node->global_gdata;
2884 /* try resetting old data */
2885 qdata = gdata->qdatas;
2886 for (i = 0; i < gdata->n_qdatas; i++)
2887 if (qdata[i].quark == quark)
2889 qdata[i].data = data;
2895 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2896 qdata = gdata->qdatas;
2897 for (i = 0; i < gdata->n_qdatas - 1; i++)
2898 if (qdata[i].quark > quark)
2900 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2901 qdata[i].quark = quark;
2902 qdata[i].data = data;
2906 g_type_set_qdata (GType type,
2912 g_return_if_fail (quark != 0);
2914 node = lookup_type_node_I (type);
2917 G_WRITE_LOCK (&type_rw_lock);
2918 type_set_qdata_W (node, quark, data);
2919 G_WRITE_UNLOCK (&type_rw_lock);
2922 g_return_if_fail (node != NULL);
2926 type_add_flags_W (TypeNode *node,
2931 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2932 g_return_if_fail (node != NULL);
2934 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2935 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2936 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2938 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2942 g_type_query (GType type,
2947 g_return_if_fail (query != NULL);
2949 /* if node is not static and classed, we won't allow query */
2951 node = lookup_type_node_I (type);
2952 if (node && node->is_classed && !node->plugin)
2954 /* type is classed and probably even instantiatable */
2955 G_READ_LOCK (&type_rw_lock);
2956 if (node->data) /* type is static or referenced */
2958 query->type = NODE_TYPE (node);
2959 query->type_name = NODE_NAME (node);
2960 query->class_size = node->data->class.class_size;
2961 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2963 G_READ_UNLOCK (&type_rw_lock);
2968 /* --- implementation details --- */
2970 g_type_test_flags (GType type,
2974 gboolean result = FALSE;
2976 node = lookup_type_node_I (type);
2979 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2980 guint tflags = flags & TYPE_FLAG_MASK;
2984 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2986 fflags = (finfo->type_flags & fflags) == fflags;
2993 G_READ_LOCK (&type_rw_lock);
2994 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2995 G_READ_UNLOCK (&type_rw_lock);
3000 result = tflags && fflags;
3007 g_type_get_plugin (GType type)
3011 node = lookup_type_node_I (type);
3013 return node ? node->plugin : NULL;
3017 g_type_interface_get_plugin (GType instance_type,
3018 GType interface_type)
3023 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3025 node = lookup_type_node_I (instance_type);
3026 iface = lookup_type_node_I (interface_type);
3029 IFaceHolder *iholder;
3030 GTypePlugin *plugin;
3032 G_READ_LOCK (&type_rw_lock);
3034 iholder = iface_node_get_holders_L (iface);
3035 while (iholder && iholder->instance_type != instance_type)
3036 iholder = iholder->next;
3037 plugin = iholder ? iholder->plugin : NULL;
3039 G_READ_UNLOCK (&type_rw_lock);
3044 g_return_val_if_fail (node == NULL, NULL);
3045 g_return_val_if_fail (iface == NULL, NULL);
3047 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3053 g_type_fundamental_next (void)
3057 G_READ_LOCK (&type_rw_lock);
3058 type = static_fundamental_next;
3059 G_READ_UNLOCK (&type_rw_lock);
3060 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3061 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3065 g_type_fundamental (GType type_id)
3067 TypeNode *node = lookup_type_node_I (type_id);
3069 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3073 g_type_check_instance_is_a (GTypeInstance *type_instance,
3076 TypeNode *node, *iface;
3079 if (!type_instance || !type_instance->g_class)
3082 node = lookup_type_node_I (type_instance->g_class->g_type);
3083 iface = lookup_type_node_I (iface_type);
3084 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3090 g_type_check_class_is_a (GTypeClass *type_class,
3093 TypeNode *node, *iface;
3099 node = lookup_type_node_I (type_class->g_type);
3100 iface = lookup_type_node_I (is_a_type);
3101 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3107 g_type_check_instance_cast (GTypeInstance *type_instance,
3112 if (type_instance->g_class)
3114 TypeNode *node, *iface;
3115 gboolean is_instantiatable, check;
3117 node = lookup_type_node_I (type_instance->g_class->g_type);
3118 is_instantiatable = node && node->is_instantiatable;
3119 iface = lookup_type_node_I (iface_type);
3120 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3122 return type_instance;
3124 if (is_instantiatable)
3125 g_warning ("invalid cast from `%s' to `%s'",
3126 type_descriptive_name_I (type_instance->g_class->g_type),
3127 type_descriptive_name_I (iface_type));
3129 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3130 type_descriptive_name_I (type_instance->g_class->g_type),
3131 type_descriptive_name_I (iface_type));
3134 g_warning ("invalid unclassed pointer in cast to `%s'",
3135 type_descriptive_name_I (iface_type));
3138 return type_instance;
3142 g_type_check_class_cast (GTypeClass *type_class,
3147 TypeNode *node, *iface;
3148 gboolean is_classed, check;
3150 node = lookup_type_node_I (type_class->g_type);
3151 is_classed = node && node->is_classed;
3152 iface = lookup_type_node_I (is_a_type);
3153 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3158 g_warning ("invalid class cast from `%s' to `%s'",
3159 type_descriptive_name_I (type_class->g_type),
3160 type_descriptive_name_I (is_a_type));
3162 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3163 type_descriptive_name_I (type_class->g_type),
3164 type_descriptive_name_I (is_a_type));
3167 g_warning ("invalid class cast from (NULL) pointer to `%s'",
3168 type_descriptive_name_I (is_a_type));
3173 g_type_check_instance (GTypeInstance *type_instance)
3175 /* this function is just here to make the signal system
3176 * conveniently elaborated on instance checks
3180 if (type_instance->g_class)
3182 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
3184 if (node && node->is_instantiatable)
3187 g_warning ("instance of invalid non-instantiatable type `%s'",
3188 type_descriptive_name_I (type_instance->g_class->g_type));
3191 g_warning ("instance with invalid (NULL) class pointer");
3194 g_warning ("invalid (NULL) pointer instance");
3199 static inline gboolean
3200 type_check_is_value_type_U (GType type)
3202 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
3205 /* common path speed up */
3206 node = lookup_type_node_I (type);
3207 if (node && node->mutatable_check_cache)
3210 G_READ_LOCK (&type_rw_lock);
3214 if (node->data && node->data->common.ref_count > 0 &&
3215 node->data->common.value_table->value_init)
3216 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3217 else if (NODE_IS_IFACE (node))
3221 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3223 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3224 TypeNode *prnode = lookup_type_node_I (prtype);
3226 if (prnode->is_instantiatable)
3229 node = lookup_type_node_I (type);
3235 G_READ_UNLOCK (&type_rw_lock);
3237 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
3241 g_type_check_is_value_type (GType type)
3243 return type_check_is_value_type_U (type);
3247 g_type_check_value (GValue *value)
3249 return value && type_check_is_value_type_U (value->g_type);
3253 g_type_check_value_holds (GValue *value,
3256 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
3260 g_type_value_table_peek (GType type)
3262 GTypeValueTable *vtable = NULL;
3263 TypeNode *node = lookup_type_node_I (type);
3264 gboolean has_refed_data, has_table;
3267 /* speed up common code path, we're not 100% safe here,
3268 * but we should only get called with referenced types anyway
3270 data = node ? node->data : NULL;
3271 if (node && node->mutatable_check_cache)
3272 return data->common.value_table;
3274 G_READ_LOCK (&type_rw_lock);
3277 has_refed_data = node && node->data && node->data->common.ref_count;
3278 has_table = has_refed_data && node->data->common.value_table->value_init;
3282 vtable = node->data->common.value_table;
3283 else if (NODE_IS_IFACE (node))
3287 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3289 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3290 TypeNode *prnode = lookup_type_node_I (prtype);
3292 if (prnode->is_instantiatable)
3295 node = lookup_type_node_I (type);
3296 goto restart_table_peek;
3302 G_READ_UNLOCK (&type_rw_lock);
3308 g_warning (G_STRLOC ": type id `%lu' is invalid", type);
3309 if (!has_refed_data)
3310 g_warning ("can't peek value table for type `%s' which is not currently referenced",
3311 type_descriptive_name_I (type));
3316 G_CONST_RETURN gchar*
3317 g_type_name_from_instance (GTypeInstance *instance)
3320 return "<NULL-instance>";
3322 return g_type_name_from_class (instance->g_class);
3325 G_CONST_RETURN gchar*
3326 g_type_name_from_class (GTypeClass *g_class)
3329 return "<NULL-class>";
3331 return g_type_name (g_class->g_type);
3335 /* --- foreign prototypes --- */
3336 extern void g_value_c_init (void); /* sync with gvalue.c */
3337 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
3338 extern void g_enum_types_init (void); /* sync with genums.c */
3339 extern void g_param_type_init (void); /* sync with gparam.c */
3340 extern void g_boxed_type_init (void); /* sync with gboxed.c */
3341 extern void g_object_type_init (void); /* sync with gobject.c */
3342 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
3343 extern void g_value_transforms_init (void); /* sync with gvaluetransform.c */
3344 extern void g_signal_init (void); /* sync with gsignal.c */
3347 /* --- initialization --- */
3349 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
3351 G_LOCK_DEFINE_STATIC (type_init_lock);
3352 const gchar *env_string;
3357 G_LOCK (type_init_lock);
3359 G_WRITE_LOCK (&type_rw_lock);
3361 if (static_quark_type_flags)
3363 G_WRITE_UNLOCK (&type_rw_lock);
3364 G_UNLOCK (type_init_lock);
3368 /* setup GObject library wide debugging flags */
3369 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
3370 env_string = g_getenv ("GOBJECT_DEBUG");
3371 if (env_string != NULL)
3373 static GDebugKey debug_keys[] = {
3374 { "objects", G_TYPE_DEBUG_OBJECTS },
3375 { "signals", G_TYPE_DEBUG_SIGNALS },
3378 _g_type_debug_flags |= g_parse_debug_string (env_string,
3380 sizeof (debug_keys) / sizeof (debug_keys[0]));
3385 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3386 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3387 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3389 /* type qname hash table */
3390 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3392 /* invalid type G_TYPE_INVALID (0)
3394 static_fundamental_type_nodes[0] = NULL;
3396 /* void type G_TYPE_NONE
3398 node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
3399 type = NODE_TYPE (node);
3400 g_assert (type == G_TYPE_NONE);
3402 /* interface fundamental type G_TYPE_INTERFACE (!classed)
3404 memset (&info, 0, sizeof (info));
3405 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
3406 type = NODE_TYPE (node);
3407 type_data_make_W (node, &info, NULL);
3408 g_assert (type == G_TYPE_INTERFACE);
3410 G_WRITE_UNLOCK (&type_rw_lock);
3414 /* G_TYPE_TYPE_PLUGIN
3416 g_type_plugin_get_type ();
3418 /* G_TYPE_* value types
3420 g_value_types_init ();
3422 /* G_TYPE_ENUM & G_TYPE_FLAGS
3424 g_enum_types_init ();
3428 g_boxed_type_init ();
3432 g_param_type_init ();
3436 g_object_type_init ();
3438 /* G_TYPE_PARAM_* pspec types
3440 g_param_spec_types_init ();
3442 /* Value Transformations
3444 g_value_transforms_init ();
3450 G_UNLOCK (type_init_lock);
3456 g_type_init_with_debug_flags (0);
3460 g_type_class_add_private (gpointer g_class,
3463 GType instance_type = ((GTypeClass *)g_class)->g_type;
3464 TypeNode *node = lookup_type_node_I (instance_type);
3467 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3469 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3470 type_descriptive_name_I (instance_type));
3474 if (NODE_PARENT_TYPE (node))
3476 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3477 if (node->data->instance.private_size != pnode->data->instance.private_size)
3479 g_warning ("g_type_add_private() called multiple times for the same type");
3484 G_WRITE_LOCK (&type_rw_lock);
3486 offset = ALIGN_STRUCT (node->data->instance.private_size);
3487 node->data->instance.private_size = offset + private_size;
3489 G_WRITE_UNLOCK (&type_rw_lock);
3493 g_type_instance_get_private (GTypeInstance *instance,
3496 TypeNode *instance_node;
3497 TypeNode *private_node;
3498 TypeNode *parent_node;
3502 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3504 /* while instances are initialized, their class pointers change,
3505 * so figure the instances real class first
3507 if (instance_real_class_bsa)
3509 class = instance_real_class_get (instance);
3511 class = instance->g_class;
3514 class = instance->g_class;
3516 instance_node = lookup_type_node_I (class->g_type);
3517 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3519 g_warning ("instance of invalid non-instantiatable type `%s'",
3520 type_descriptive_name_I (instance->g_class->g_type));
3524 private_node = lookup_type_node_I (private_type);
3525 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3527 g_warning ("attempt to retrieve private data for invalid type '%s'",
3528 type_descriptive_name_I (private_type));
3532 /* Note that we don't need a read lock, since instance existing
3533 * means that the instance class and all parent classes
3534 * exist, so the node->data, node->data->instance.instance_size,
3535 * and node->data->instance.private_size are not going to be changed.
3536 * for any of the relevant types.
3539 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3541 if (NODE_PARENT_TYPE (private_node))
3543 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3544 g_assert (parent_node->data && parent_node->data->common.ref_count);
3546 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3549 return G_STRUCT_MEMBER_P (instance, offset);