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.
29 #include "gtype-private.h"
30 #include "gtypeplugin.h"
31 #include "gvaluecollector.h"
32 #include "gbsearcharray.h"
33 #include "gatomicarray.h"
34 #include "gobject_trace.h"
39 * @short_description: The GLib Runtime type identification and
41 * @title:Type Information
43 * The GType API is the foundation of the GObject system. It provides the
44 * facilities for registering and managing all fundamental data types,
45 * user-defined object and interface types. Before using any GType
46 * or GObject functions, g_type_init() must be called to initialize the
49 * For type creation and registration purposes, all types fall into one of
50 * two categories: static or dynamic. Static types are never loaded or
51 * unloaded at run-time as dynamic types may be. Static types are created
52 * with g_type_register_static() that gets type specific information passed
53 * in via a #GTypeInfo structure.
54 * Dynamic types are created with g_type_register_dynamic() which takes a
55 * #GTypePlugin structure instead. The remaining type information (the
56 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
57 * and the g_type_plugin_*() API.
58 * These registration functions are usually called only once from a
59 * function whose only purpose is to return the type identifier for a
60 * specific class. Once the type (or class or interface) is registered,
61 * it may be instantiated, inherited, or implemented depending on exactly
62 * what sort of type it is.
63 * There is also a third registration function for registering fundamental
64 * types called g_type_register_fundamental() which requires both a #GTypeInfo
65 * structure and a #GTypeFundamentalInfo structure but it is seldom used
66 * since most fundamental types are predefined rather than user-defined.
68 * A final word about type names.
69 * Such an identifier needs to be at least three characters long. There is no
70 * upper length limit. The first character needs to be a letter (a-z or A-Z)
71 * or an underscore '_'. Subsequent characters can be letters, numbers or
76 /* NOTE: some functions (some internal variants and exported ones)
77 * invalidate data portions of the TypeNodes. if external functions/callbacks
78 * are called, pointers to memory maintained by TypeNodes have to be looked up
79 * again. this affects most of the struct TypeNode fields, e.g. ->children or
80 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
81 * not ->supers[]), as all those memory portions can get realloc()ed during
82 * callback invocation.
85 * - g_type_from_name() should do an ordered array lookup after fetching the
86 * the quark, instead of a second hashtable lookup.
89 * lock handling issues when calling static functions are indicated by
90 * uppercase letter postfixes, all static functions have to have
91 * one of the below postfixes:
92 * - _I: [Indifferent about locking]
93 * function doesn't care about locks at all
94 * - _U: [Unlocked invocation]
95 * no read or write lock has to be held across function invocation
96 * (locks may be acquired and released during invocation though)
97 * - _L: [Locked invocation]
98 * a write lock or more than 0 read locks have to be held across
100 * - _W: [Write-locked invocation]
101 * a write lock has to be held across function invocation
102 * - _Wm: [Write-locked invocation, mutatable]
103 * like _W, but the write lock might be released and reacquired
104 * during invocation, watch your pointers
105 * - _WmREC: [Write-locked invocation, mutatable, recursive]
106 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
110 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
111 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
112 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
113 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
115 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
116 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
117 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
118 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
120 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
121 static const gchar _action[] = " invalidly modified type "; \
122 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
124 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
126 g_error ("%s()%s`%s'", _fname, _action, _tname); \
128 #define g_return_val_if_type_system_uninitialized(return_value) G_STMT_START{ \
129 if (G_UNLIKELY (!static_quark_type_flags)) \
131 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
132 "%s: You forgot to call g_type_init()", \
134 return (return_value); \
138 #ifdef G_ENABLE_DEBUG
139 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
140 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
143 #else /* !G_ENABLE_DEBUG */
144 #define DEBUG_CODE(debug_type, code_block) /* code_block */
145 #endif /* G_ENABLE_DEBUG */
147 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
148 G_TYPE_FLAG_INSTANTIATABLE | \
149 G_TYPE_FLAG_DERIVABLE | \
150 G_TYPE_FLAG_DEEP_DERIVABLE)
151 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
152 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
153 sizeof (gpointer)), \
156 /* The 2*sizeof(size_t) alignment here is borrowed from
157 * GNU libc, so it should be good most everywhere.
158 * It is more conservative than is needed on some 64-bit
159 * platforms, but ia64 does require a 16-byte alignment.
160 * The SIMD extensions for x86 and ppc32 would want a
161 * larger alignment than this, but we don't need to
162 * do better than malloc.
164 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
165 #define ALIGN_STRUCT(offset) \
166 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
169 /* --- typedefs --- */
170 typedef struct _TypeNode TypeNode;
171 typedef struct _CommonData CommonData;
172 typedef struct _BoxedData BoxedData;
173 typedef struct _IFaceData IFaceData;
174 typedef struct _ClassData ClassData;
175 typedef struct _InstanceData InstanceData;
176 typedef union _TypeData TypeData;
177 typedef struct _IFaceEntries IFaceEntries;
178 typedef struct _IFaceEntry IFaceEntry;
179 typedef struct _IFaceHolder IFaceHolder;
182 /* --- prototypes --- */
183 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
184 static void type_add_flags_W (TypeNode *node,
186 static void type_data_make_W (TypeNode *node,
187 const GTypeInfo *info,
188 const GTypeValueTable *value_table);
189 static inline void type_data_ref_Wm (TypeNode *node);
190 static inline void type_data_unref_U (TypeNode *node,
192 static void type_data_last_unref_Wm (TypeNode * node,
194 static inline gpointer type_get_qdata_L (TypeNode *node,
196 static inline void type_set_qdata_W (TypeNode *node,
199 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
200 GType instance_type);
201 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
203 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
205 static gboolean type_node_is_a_L (TypeNode *node,
206 TypeNode *iface_node);
209 /* --- enumeration --- */
211 /* The InitState enumeration is used to track the progress of initializing
212 * both classes and interface vtables. Keeping the state of initialization
213 * is necessary to handle new interfaces being added while we are initializing
214 * the class or other interfaces.
226 /* --- structures --- */
229 guint volatile ref_count;
231 guint n_children; /* writable with lock */
233 guint n_prerequisites : 9;
234 guint is_classed : 1;
235 guint is_instantiatable : 1;
236 guint mutatable_check_cache : 1; /* combines some common path checks */
237 GType *children; /* writable with lock */
238 TypeData * volatile data;
242 GAtomicArray iface_entries; /* for !iface types */
243 GAtomicArray offsets;
245 GType *prerequisites;
246 GType supers[1]; /* flexible array */
249 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
250 #define MAX_N_SUPERS (255)
251 #define MAX_N_CHILDREN (4095)
252 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
253 #define MAX_N_PREREQUISITES (511)
254 #define NODE_TYPE(node) (node->supers[0])
255 #define NODE_PARENT_TYPE(node) (node->supers[1])
256 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
257 #define NODE_NAME(node) (g_quark_to_string (node->qname))
258 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
259 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
260 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
261 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
262 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
263 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
264 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
265 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
266 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
267 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
268 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
269 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
271 #define NODE_IS_ANCESTOR(ancestor, node) \
272 ((ancestor)->n_supers <= (node)->n_supers && \
273 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
278 GInterfaceInfo *info;
286 GTypeInterface *vtable;
287 InitState init_state;
290 struct _IFaceEntries {
295 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
296 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
300 GTypeValueTable *value_table;
306 GBoxedCopyFunc copy_func;
307 GBoxedFreeFunc free_func;
314 GBaseInitFunc vtable_init_base;
315 GBaseFinalizeFunc vtable_finalize_base;
316 GClassInitFunc dflt_init;
317 GClassFinalizeFunc dflt_finalize;
318 gconstpointer dflt_data;
319 gpointer dflt_vtable;
326 guint16 class_private_size;
327 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
328 GBaseInitFunc class_init_base;
329 GBaseFinalizeFunc class_finalize_base;
330 GClassInitFunc class_init;
331 GClassFinalizeFunc class_finalize;
332 gconstpointer class_data;
340 guint16 class_private_size;
341 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
342 GBaseInitFunc class_init_base;
343 GBaseFinalizeFunc class_finalize_base;
344 GClassInitFunc class_init;
345 GClassFinalizeFunc class_finalize;
346 gconstpointer class_data;
348 guint16 instance_size;
349 guint16 private_size;
351 GInstanceInitFunc instance_init;
360 InstanceData instance;
365 GTypeClassCacheFunc cache_func;
370 GTypeInterfaceCheckFunc check_func;
374 /* --- variables --- */
375 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
376 static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
377 static guint static_n_class_cache_funcs = 0;
378 static ClassCacheFunc *static_class_cache_funcs = NULL;
379 static guint static_n_iface_check_funcs = 0;
380 static IFaceCheckFunc *static_iface_check_funcs = NULL;
381 static GQuark static_quark_type_flags = 0;
382 static GQuark static_quark_iface_holder = 0;
383 static GQuark static_quark_dependants_array = 0;
384 GTypeDebugFlags _g_type_debug_flags = 0;
386 /* --- type nodes --- */
387 static GHashTable *static_type_nodes_ht = NULL;
388 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
389 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
391 static inline TypeNode*
392 lookup_type_node_I (register GType utype)
394 if (utype > G_TYPE_FUNDAMENTAL_MAX)
395 return (TypeNode*) (utype & ~TYPE_ID_MASK);
397 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
401 type_node_any_new_W (TypeNode *pnode,
405 GTypeFundamentalFlags type_flags)
410 guint i, node_size = 0;
412 n_supers = pnode ? pnode->n_supers + 1 : 0;
415 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
416 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
417 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
418 node = g_malloc0 (node_size);
419 if (!pnode) /* offset fundamental types */
421 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
422 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
428 g_assert ((type & TYPE_ID_MASK) == 0);
430 node->n_supers = n_supers;
433 node->supers[0] = type;
436 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
437 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
439 if (NODE_IS_IFACE (node))
441 IFACE_NODE_N_PREREQUISITES (node) = 0;
442 IFACE_NODE_PREREQUISITES (node) = NULL;
445 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
449 node->supers[0] = type;
450 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
452 node->is_classed = pnode->is_classed;
453 node->is_instantiatable = pnode->is_instantiatable;
455 if (NODE_IS_IFACE (node))
457 IFACE_NODE_N_PREREQUISITES (node) = 0;
458 IFACE_NODE_PREREQUISITES (node) = NULL;
463 IFaceEntries *entries;
465 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
466 IFACE_ENTRIES_HEADER_SIZE,
470 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
472 entries->entry[j].vtable = NULL;
473 entries->entry[j].init_state = UNINITIALIZED;
475 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
480 i = pnode->n_children++;
481 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
482 pnode->children[i] = type;
485 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
487 node->plugin = plugin;
488 node->n_children = 0;
489 node->children = NULL;
491 node->qname = g_quark_from_string (name);
492 node->global_gdata = NULL;
494 g_hash_table_insert (static_type_nodes_ht,
495 GUINT_TO_POINTER (node->qname),
500 static inline GTypeFundamentalInfo*
501 type_node_fundamental_info_I (TypeNode *node)
503 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
505 if (ftype != NODE_TYPE (node))
506 node = lookup_type_node_I (ftype);
508 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
512 type_node_fundamental_new_W (GType ftype,
514 GTypeFundamentalFlags type_flags)
516 GTypeFundamentalInfo *finfo;
519 g_assert ((ftype & TYPE_ID_MASK) == 0);
520 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
522 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
523 static_fundamental_next++;
525 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
527 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
529 finfo = type_node_fundamental_info_I (node);
530 finfo->type_flags = type_flags;
536 type_node_new_W (TypeNode *pnode,
542 g_assert (pnode->n_supers < MAX_N_SUPERS);
543 g_assert (pnode->n_children < MAX_N_CHILDREN);
545 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
548 static inline IFaceEntry*
549 lookup_iface_entry_I (volatile IFaceEntries *entries,
550 TypeNode *iface_node)
561 G_ATOMIC_ARRAY_DO_TRANSACTION
562 (&iface_node->_prot.offsets, guint8,
565 offsets = transaction_data;
566 offset_index = entries->offset_index;
567 if (offsets != NULL &&
568 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
570 index = offsets[offset_index];
573 /* zero means unset, subtract one to get real index */
576 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
578 check = (IFaceEntry *)&entries->entry[index];
579 if (check->iface_type == NODE_TYPE (iface_node))
589 static inline IFaceEntry*
590 type_lookup_iface_entry_L (TypeNode *node,
591 TypeNode *iface_node)
593 if (!NODE_IS_IFACE (iface_node))
596 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
601 static inline gboolean
602 type_lookup_iface_vtable_I (TypeNode *node,
603 TypeNode *iface_node,
604 gpointer *vtable_ptr)
609 if (!NODE_IS_IFACE (iface_node))
616 G_ATOMIC_ARRAY_DO_TRANSACTION
617 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
619 entry = lookup_iface_entry_I (transaction_data, iface_node);
624 *vtable_ptr = entry->vtable;
633 static inline gboolean
634 type_lookup_prerequisite_L (TypeNode *iface,
635 GType prerequisite_type)
637 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
639 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
640 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
647 i = (n_prerequisites + 1) >> 1;
648 check = prerequisites + i;
649 if (prerequisite_type == *check)
651 else if (prerequisite_type > *check)
653 n_prerequisites -= i;
654 prerequisites = check;
656 else /* if (prerequisite_type < *check) */
657 n_prerequisites = i - 1;
659 while (n_prerequisites);
665 type_descriptive_name_I (GType type)
669 TypeNode *node = lookup_type_node_I (type);
671 return node ? NODE_NAME (node) : "<unknown>";
678 /* --- type consistency checks --- */
680 check_plugin_U (GTypePlugin *plugin,
681 gboolean need_complete_type_info,
682 gboolean need_complete_interface_info,
683 const gchar *type_name)
685 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
689 g_warning ("plugin handle for type `%s' is NULL",
693 if (!G_IS_TYPE_PLUGIN (plugin))
695 g_warning ("plugin pointer (%p) for type `%s' is invalid",
699 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
701 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
705 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
707 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
715 check_type_name_I (const gchar *type_name)
717 static const gchar extra_chars[] = "-_+";
718 const gchar *p = type_name;
721 if (!type_name[0] || !type_name[1] || !type_name[2])
723 g_warning ("type name `%s' is too short", type_name);
726 /* check the first letter */
727 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
728 for (p = type_name + 1; *p; p++)
729 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
730 (p[0] >= 'a' && p[0] <= 'z') ||
731 (p[0] >= '0' && p[0] <= '9') ||
732 strchr (extra_chars, p[0]));
735 g_warning ("type name `%s' contains invalid characters", type_name);
738 if (g_type_from_name (type_name))
740 g_warning ("cannot register existing type `%s'", type_name);
748 check_derivation_I (GType parent_type,
749 const gchar *type_name)
752 GTypeFundamentalInfo* finfo;
754 pnode = lookup_type_node_I (parent_type);
757 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
759 type_descriptive_name_I (parent_type));
762 finfo = type_node_fundamental_info_I (pnode);
763 /* ensure flat derivability */
764 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
766 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
771 /* ensure deep derivability */
772 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
773 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
775 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
785 check_collect_format_I (const gchar *collect_format)
787 const gchar *p = collect_format;
788 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
789 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
790 G_VALUE_COLLECT_POINTER, 0 };
793 if (!strchr (valid_format, *p++))
795 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
799 check_value_table_I (const gchar *type_name,
800 const GTypeValueTable *value_table)
804 else if (value_table->value_init == NULL)
806 if (value_table->value_free || value_table->value_copy ||
807 value_table->value_peek_pointer ||
808 value_table->collect_format || value_table->collect_value ||
809 value_table->lcopy_format || value_table->lcopy_value)
810 g_warning ("cannot handle uninitializable values of type `%s'",
814 else /* value_table->value_init != NULL */
816 if (!value_table->value_free)
819 * g_warning ("missing `value_free()' for type `%s'", type_name);
823 if (!value_table->value_copy)
825 g_warning ("missing `value_copy()' for type `%s'", type_name);
828 if ((value_table->collect_format || value_table->collect_value) &&
829 (!value_table->collect_format || !value_table->collect_value))
831 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
835 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
837 g_warning ("the `%s' specification for type `%s' is too long or invalid",
842 if ((value_table->lcopy_format || value_table->lcopy_value) &&
843 (!value_table->lcopy_format || !value_table->lcopy_value))
845 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
849 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
851 g_warning ("the `%s' specification for type `%s' is too long or invalid",
861 check_type_info_I (TypeNode *pnode,
863 const gchar *type_name,
864 const GTypeInfo *info)
866 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
867 gboolean is_interface = ftype == G_TYPE_INTERFACE;
869 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
871 /* check instance members */
872 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
873 (info->instance_size || info->n_preallocs || info->instance_init))
876 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
880 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
884 /* check class & interface members */
885 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
886 (info->class_init || info->class_finalize || info->class_data ||
887 info->class_size || info->base_init || info->base_finalize))
890 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
894 g_warning ("cannot create class for `%s' as non-classed fundamental",
898 /* check interface size */
899 if (is_interface && info->class_size < sizeof (GTypeInterface))
901 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
905 /* check class size */
906 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
908 if (info->class_size < sizeof (GTypeClass))
910 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
914 if (pnode && info->class_size < pnode->data->class.class_size)
916 g_warning ("specified class size for type `%s' is smaller "
917 "than the parent type's `%s' class size",
923 /* check instance size */
924 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
926 if (info->instance_size < sizeof (GTypeInstance))
928 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
932 if (pnode && info->instance_size < pnode->data->instance.instance_size)
934 g_warning ("specified instance size for type `%s' is smaller "
935 "than the parent type's `%s' instance size",
946 find_conforming_child_type_L (TypeNode *pnode,
949 TypeNode *node = NULL;
952 if (type_lookup_iface_entry_L (pnode, iface))
955 for (i = 0; i < pnode->n_children && !node; i++)
956 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
962 check_add_interface_L (GType instance_type,
965 TypeNode *node = lookup_type_node_I (instance_type);
966 TypeNode *iface = lookup_type_node_I (iface_type);
969 GType *prerequisites;
973 if (!node || !node->is_instantiatable)
975 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
976 type_descriptive_name_I (instance_type));
979 if (!iface || !NODE_IS_IFACE (iface))
981 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
982 type_descriptive_name_I (iface_type),
986 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
987 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
989 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
990 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
996 /* allow overriding of interface type introduced for parent type */
997 entry = type_lookup_iface_entry_L (node, iface);
998 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1000 /* ok, we do conform to this interface already, but the interface vtable was not
1001 * yet intialized, and we just conform to the interface because it got added to
1002 * one of our parents. so we allow overriding of holder info here.
1006 /* check whether one of our children already conforms (or whether the interface
1007 * got added to this node already)
1009 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1012 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
1018 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1019 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1021 tnode = lookup_type_node_I (prerequisites[i]);
1022 if (!type_node_is_a_L (node, tnode))
1024 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
1035 check_interface_info_I (TypeNode *iface,
1036 GType instance_type,
1037 const GInterfaceInfo *info)
1039 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1041 g_warning ("interface type `%s' for type `%s' comes without initializer",
1043 type_descriptive_name_I (instance_type));
1050 /* --- type info (type node data) --- */
1052 type_data_make_W (TypeNode *node,
1053 const GTypeInfo *info,
1054 const GTypeValueTable *value_table)
1057 GTypeValueTable *vtable = NULL;
1058 guint vtable_size = 0;
1060 g_assert (node->data == NULL && info != NULL);
1064 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1067 vtable = pnode->data->common.value_table;
1070 static const GTypeValueTable zero_vtable = { NULL, };
1072 value_table = &zero_vtable;
1077 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1078 vtable_size = sizeof (GTypeValueTable);
1079 if (value_table->collect_format)
1080 vtable_size += strlen (value_table->collect_format);
1081 if (value_table->lcopy_format)
1082 vtable_size += strlen (value_table->lcopy_format);
1086 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
1088 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1090 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1092 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1093 data->instance.class_size = info->class_size;
1094 data->instance.class_init_base = info->base_init;
1095 data->instance.class_finalize_base = info->base_finalize;
1096 data->instance.class_init = info->class_init;
1097 data->instance.class_finalize = info->class_finalize;
1098 data->instance.class_data = info->class_data;
1099 data->instance.class = NULL;
1100 data->instance.init_state = UNINITIALIZED;
1101 data->instance.instance_size = info->instance_size;
1102 /* We'll set the final value for data->instance.private size
1103 * after the parent class has been initialized
1105 data->instance.private_size = 0;
1106 data->instance.class_private_size = 0;
1108 data->instance.class_private_size = pnode->data->instance.class_private_size;
1109 #ifdef DISABLE_MEM_POOLS
1110 data->instance.n_preallocs = 0;
1111 #else /* !DISABLE_MEM_POOLS */
1112 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1113 #endif /* !DISABLE_MEM_POOLS */
1114 data->instance.instance_init = info->instance_init;
1116 else if (node->is_classed) /* only classed */
1118 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1120 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1122 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1123 data->class.class_size = info->class_size;
1124 data->class.class_init_base = info->base_init;
1125 data->class.class_finalize_base = info->base_finalize;
1126 data->class.class_init = info->class_init;
1127 data->class.class_finalize = info->class_finalize;
1128 data->class.class_data = info->class_data;
1129 data->class.class = NULL;
1130 data->class.class_private_size = 0;
1132 data->class.class_private_size = pnode->data->class.class_private_size;
1133 data->class.init_state = UNINITIALIZED;
1135 else if (NODE_IS_IFACE (node))
1137 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1139 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1140 data->iface.vtable_size = info->class_size;
1141 data->iface.vtable_init_base = info->base_init;
1142 data->iface.vtable_finalize_base = info->base_finalize;
1143 data->iface.dflt_init = info->class_init;
1144 data->iface.dflt_finalize = info->class_finalize;
1145 data->iface.dflt_data = info->class_data;
1146 data->iface.dflt_vtable = NULL;
1148 else if (NODE_IS_BOXED (node))
1150 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1152 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1156 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1158 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1167 /* we allocate the vtable and its strings together with the type data, so
1168 * children can take over their parent's vtable pointer, and we don't
1169 * need to worry freeing it or not when the child data is destroyed
1171 *vtable = *value_table;
1172 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1174 vtable->collect_format = p;
1175 if (value_table->collect_format)
1177 strcat (p, value_table->collect_format);
1178 p += strlen (value_table->collect_format);
1182 vtable->lcopy_format = p;
1183 if (value_table->lcopy_format)
1184 strcat (p, value_table->lcopy_format);
1186 node->data->common.value_table = vtable;
1187 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1188 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1189 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1191 g_assert (node->data->common.value_table != NULL); /* paranoid */
1193 g_atomic_int_set ((int *) &node->ref_count, 1);
1197 type_data_ref_Wm (TypeNode *node)
1201 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1203 GTypeValueTable tmp_value_table;
1205 g_assert (node->plugin != NULL);
1209 type_data_ref_Wm (pnode);
1211 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1214 memset (&tmp_info, 0, sizeof (tmp_info));
1215 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1217 G_WRITE_UNLOCK (&type_rw_lock);
1218 g_type_plugin_use (node->plugin);
1219 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1220 G_WRITE_LOCK (&type_rw_lock);
1222 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1224 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1225 type_data_make_W (node, &tmp_info,
1226 check_value_table_I (NODE_NAME (node),
1227 &tmp_value_table) ? &tmp_value_table : NULL);
1231 g_assert (NODE_REFCOUNT (node) > 0);
1233 g_atomic_int_inc ((int *) &node->ref_count);
1237 static inline gboolean
1238 type_data_ref_U (TypeNode *node)
1243 current = NODE_REFCOUNT (node);
1247 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1253 iface_node_has_available_offset_L (TypeNode *iface_node,
1259 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1260 if (offsets == NULL)
1263 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1266 if (offsets[offset] == 0 ||
1267 offsets[offset] == for_index+1)
1274 find_free_iface_offset_L (IFaceEntries *entries)
1277 TypeNode *iface_node;
1282 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1287 for (i = 0; i < n_entries; i++)
1289 entry = &entries->entry[i];
1290 iface_node = lookup_type_node_I (entry->iface_type);
1292 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1296 while (i != n_entries);
1302 iface_node_set_offset_L (TypeNode *iface_node,
1306 guint8 *offsets, *old_offsets;
1307 int new_size, old_size;
1310 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1311 if (old_offsets == NULL)
1315 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1316 if (offset < old_size &&
1317 old_offsets[offset] == index + 1)
1318 return; /* Already set to this index, return */
1320 new_size = MAX (old_size, offset + 1);
1322 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1323 0, new_size - old_size);
1325 /* Mark new area as unused */
1326 for (i = old_size; i < new_size; i++)
1329 offsets[offset] = index + 1;
1331 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1335 type_node_add_iface_entry_W (TypeNode *node,
1337 IFaceEntry *parent_entry)
1339 IFaceEntries *entries;
1341 TypeNode *iface_node;
1345 g_assert (node->is_instantiatable);
1347 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1348 if (entries != NULL)
1350 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1352 g_assert (num_entries < MAX_N_INTERFACES);
1354 for (i = 0; i < num_entries; i++)
1356 entry = &entries->entry[i];
1357 if (entry->iface_type == iface_type)
1359 /* this can happen in two cases:
1360 * - our parent type already conformed to iface_type and node
1361 * got its own holder info. here, our children already have
1362 * entries and NULL vtables, since this will only work for
1363 * uninitialized classes.
1364 * - an interface type is added to an ancestor after it was
1365 * added to a child type.
1368 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1371 /* sick, interface is added to ancestor *after* child type;
1372 * nothing todo, the entry and our children were already setup correctly
1380 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1381 IFACE_ENTRIES_HEADER_SIZE,
1382 sizeof (IFaceEntry));
1383 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1384 i = num_entries - 1;
1386 entries->offset_index = 0;
1387 entries->entry[i].iface_type = iface_type;
1388 entries->entry[i].vtable = NULL;
1389 entries->entry[i].init_state = UNINITIALIZED;
1393 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1395 entries->entry[i].init_state = INITIALIZED;
1396 entries->entry[i].vtable = parent_entry->vtable;
1400 /* Update offsets in iface */
1401 iface_node = lookup_type_node_I (iface_type);
1403 if (iface_node_has_available_offset_L (iface_node,
1404 entries->offset_index,
1407 iface_node_set_offset_L (iface_node,
1408 entries->offset_index, i);
1412 entries->offset_index =
1413 find_free_iface_offset_L (entries);
1414 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1416 entry = &entries->entry[j];
1418 lookup_type_node_I (entry->iface_type);
1419 iface_node_set_offset_L (iface_node,
1420 entries->offset_index, j);
1424 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1428 for (i = 0; i < node->n_children; i++)
1429 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1434 type_add_interface_Wm (TypeNode *node,
1436 const GInterfaceInfo *info,
1437 GTypePlugin *plugin)
1439 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1443 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1445 iholder->next = iface_node_get_holders_L (iface);
1446 iface_node_set_holders_W (iface, iholder);
1447 iholder->instance_type = NODE_TYPE (node);
1448 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1449 iholder->plugin = plugin;
1451 /* create an iface entry for this type */
1452 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1454 /* if the class is already (partly) initialized, we may need to base
1455 * initalize and/or initialize the new interface.
1459 InitState class_state = node->data->class.init_state;
1461 if (class_state >= BASE_IFACE_INIT)
1462 type_iface_vtable_base_init_Wm (iface, node);
1464 if (class_state >= IFACE_INIT)
1465 type_iface_vtable_iface_init_Wm (iface, node);
1468 /* create iface entries for children of this type */
1469 entry = type_lookup_iface_entry_L (node, iface);
1470 for (i = 0; i < node->n_children; i++)
1471 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1475 type_iface_add_prerequisite_W (TypeNode *iface,
1476 TypeNode *prerequisite_node)
1478 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1479 GType *prerequisites, *dependants;
1480 guint n_dependants, i;
1482 g_assert (NODE_IS_IFACE (iface) &&
1483 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1484 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1486 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1487 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1488 if (prerequisites[i] == prerequisite_type)
1489 return; /* we already have that prerequisiste */
1490 else if (prerequisites[i] > prerequisite_type)
1492 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1493 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1494 IFACE_NODE_PREREQUISITES (iface),
1495 IFACE_NODE_N_PREREQUISITES (iface));
1496 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1497 g_memmove (prerequisites + i + 1, prerequisites + i,
1498 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1499 prerequisites[i] = prerequisite_type;
1501 /* we want to get notified when prerequisites get added to prerequisite_node */
1502 if (NODE_IS_IFACE (prerequisite_node))
1504 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1505 n_dependants = dependants ? dependants[0] : 0;
1507 dependants = g_renew (GType, dependants, n_dependants + 1);
1508 dependants[n_dependants] = NODE_TYPE (iface);
1509 dependants[0] = n_dependants;
1510 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1513 /* we need to notify all dependants */
1514 dependants = iface_node_get_dependants_array_L (iface);
1515 n_dependants = dependants ? dependants[0] : 0;
1516 for (i = 1; i <= n_dependants; i++)
1517 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1521 * g_type_interface_add_prerequisite:
1522 * @interface_type: #GType value of an interface type.
1523 * @prerequisite_type: #GType value of an interface or instantiatable type.
1525 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1526 * This means that any type implementing @interface_type must also implement
1527 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1528 * interface derivation (which GType doesn't support). An interface can have
1529 * at most one instantiatable prerequisite type.
1532 g_type_interface_add_prerequisite (GType interface_type,
1533 GType prerequisite_type)
1535 TypeNode *iface, *prerequisite_node;
1536 IFaceHolder *holders;
1538 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1539 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1540 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1542 iface = lookup_type_node_I (interface_type);
1543 prerequisite_node = lookup_type_node_I (prerequisite_type);
1544 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1546 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1547 type_descriptive_name_I (interface_type),
1548 type_descriptive_name_I (prerequisite_type));
1551 G_WRITE_LOCK (&type_rw_lock);
1552 holders = iface_node_get_holders_L (iface);
1555 G_WRITE_UNLOCK (&type_rw_lock);
1556 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1557 type_descriptive_name_I (prerequisite_type),
1558 type_descriptive_name_I (interface_type),
1559 type_descriptive_name_I (holders->instance_type));
1562 if (prerequisite_node->is_instantiatable)
1566 /* can have at most one publically installable instantiatable prerequisite */
1567 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1569 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1571 if (prnode->is_instantiatable)
1573 G_WRITE_UNLOCK (&type_rw_lock);
1574 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1575 type_descriptive_name_I (prerequisite_type),
1576 type_descriptive_name_I (interface_type),
1577 type_descriptive_name_I (NODE_TYPE (prnode)));
1582 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1583 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1584 G_WRITE_UNLOCK (&type_rw_lock);
1586 else if (NODE_IS_IFACE (prerequisite_node))
1588 GType *prerequisites;
1591 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1592 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1593 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1594 type_iface_add_prerequisite_W (iface, prerequisite_node);
1595 G_WRITE_UNLOCK (&type_rw_lock);
1599 G_WRITE_UNLOCK (&type_rw_lock);
1600 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1601 type_descriptive_name_I (prerequisite_type),
1602 type_descriptive_name_I (interface_type));
1607 * g_type_interface_prerequisites:
1608 * @interface_type: an interface type
1609 * @n_prerequisites: (out) (allow-none): location to return the number
1610 * of prerequisites, or %NULL
1612 * Returns the prerequisites of an interfaces type.
1616 * Returns: (array length=n_prerequisites) (transfer full): a
1617 * newly-allocated zero-terminated array of #GType containing
1618 * the prerequisites of @interface_type
1621 g_type_interface_prerequisites (GType interface_type,
1622 guint *n_prerequisites)
1626 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1628 iface = lookup_type_node_I (interface_type);
1632 TypeNode *inode = NULL;
1635 G_READ_LOCK (&type_rw_lock);
1636 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1637 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1639 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1640 TypeNode *node = lookup_type_node_I (prerequisite);
1641 if (node->is_instantiatable)
1643 if (!inode || type_node_is_a_L (node, inode))
1647 types[n++] = NODE_TYPE (node);
1650 types[n++] = NODE_TYPE (inode);
1652 if (n_prerequisites)
1653 *n_prerequisites = n;
1654 G_READ_UNLOCK (&type_rw_lock);
1660 if (n_prerequisites)
1661 *n_prerequisites = 0;
1669 type_iface_peek_holder_L (TypeNode *iface,
1670 GType instance_type)
1672 IFaceHolder *iholder;
1674 g_assert (NODE_IS_IFACE (iface));
1676 iholder = iface_node_get_holders_L (iface);
1677 while (iholder && iholder->instance_type != instance_type)
1678 iholder = iholder->next;
1683 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1684 GType instance_type,
1687 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1689 if (iholder && !iholder->info && need_info)
1691 GInterfaceInfo tmp_info;
1693 g_assert (iholder->plugin != NULL);
1695 type_data_ref_Wm (iface);
1697 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1699 memset (&tmp_info, 0, sizeof (tmp_info));
1701 G_WRITE_UNLOCK (&type_rw_lock);
1702 g_type_plugin_use (iholder->plugin);
1703 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1704 G_WRITE_LOCK (&type_rw_lock);
1706 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1708 check_interface_info_I (iface, instance_type, &tmp_info);
1709 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1712 return iholder; /* we don't modify write lock upon returning NULL */
1716 type_iface_blow_holder_info_Wm (TypeNode *iface,
1717 GType instance_type)
1719 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1721 g_assert (NODE_IS_IFACE (iface));
1723 while (iholder->instance_type != instance_type)
1724 iholder = iholder->next;
1726 if (iholder->info && iholder->plugin)
1728 g_free (iholder->info);
1729 iholder->info = NULL;
1731 G_WRITE_UNLOCK (&type_rw_lock);
1732 g_type_plugin_unuse (iholder->plugin);
1733 type_data_unref_U (iface, FALSE);
1734 G_WRITE_LOCK (&type_rw_lock);
1738 /* Assumes type's class already exists
1740 static inline size_t
1741 type_total_instance_size_I (TypeNode *node)
1743 gsize total_instance_size;
1745 total_instance_size = node->data->instance.instance_size;
1746 if (node->data->instance.private_size != 0)
1747 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1749 return total_instance_size;
1752 /* --- type structure creation/destruction --- */
1756 } InstanceRealClass;
1759 instance_real_class_cmp (gconstpointer p1,
1762 const InstanceRealClass *irc1 = p1;
1763 const InstanceRealClass *irc2 = p2;
1764 guint8 *i1 = irc1->instance;
1765 guint8 *i2 = irc2->instance;
1766 return G_BSEARCH_ARRAY_CMP (i1, i2);
1769 G_LOCK_DEFINE_STATIC (instance_real_class);
1770 static GBSearchArray *instance_real_class_bsa = NULL;
1771 static GBSearchConfig instance_real_class_bconfig = {
1772 sizeof (InstanceRealClass),
1773 instance_real_class_cmp,
1778 instance_real_class_set (gpointer instance,
1781 InstanceRealClass key;
1782 key.instance = instance;
1784 G_LOCK (instance_real_class);
1785 if (!instance_real_class_bsa)
1786 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1787 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1788 G_UNLOCK (instance_real_class);
1792 instance_real_class_remove (gpointer instance)
1794 InstanceRealClass key, *node;
1796 key.instance = instance;
1797 G_LOCK (instance_real_class);
1798 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1799 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1800 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1801 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1803 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1804 instance_real_class_bsa = NULL;
1806 G_UNLOCK (instance_real_class);
1809 static inline GTypeClass*
1810 instance_real_class_get (gpointer instance)
1812 InstanceRealClass key, *node;
1814 key.instance = instance;
1815 G_LOCK (instance_real_class);
1816 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1817 class = node ? node->class : NULL;
1818 G_UNLOCK (instance_real_class);
1823 * g_type_create_instance: (skip)
1824 * @type: An instantiatable type to create an instance for.
1826 * Creates and initializes an instance of @type if @type is valid and
1827 * can be instantiated. The type system only performs basic allocation
1828 * and structure setups for instances: actual instance creation should
1829 * happen through functions supplied by the type's fundamental type
1830 * implementation. So use of g_type_create_instance() is reserved for
1831 * implementators of fundamental types only. E.g. instances of the
1832 * #GObject hierarchy should be created via g_object_new() and
1833 * <emphasis>never</emphasis> directly through
1834 * g_type_create_instance() which doesn't handle things like singleton
1835 * objects or object construction. Note: Do <emphasis>not</emphasis>
1836 * use this function, unless you're implementing a fundamental
1837 * type. Also language bindings should <emphasis>not</emphasis> use
1838 * this function but g_object_new() instead.
1840 * Returns: An allocated and initialized instance, subject to further
1841 * treatment by the fundamental type implementation.
1844 g_type_create_instance (GType type)
1847 GTypeInstance *instance;
1849 guint i, total_size;
1851 node = lookup_type_node_I (type);
1852 if (!node || !node->is_instantiatable)
1854 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1855 type_descriptive_name_I (type));
1858 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1859 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1861 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1862 type_descriptive_name_I (type));
1866 class = g_type_class_ref (type);
1867 total_size = type_total_instance_size_I (node);
1869 instance = g_slice_alloc0 (total_size);
1871 if (node->data->instance.private_size)
1872 instance_real_class_set (instance, class);
1873 for (i = node->n_supers; i > 0; i--)
1877 pnode = lookup_type_node_I (node->supers[i]);
1878 if (pnode->data->instance.instance_init)
1880 instance->g_class = pnode->data->instance.class;
1881 pnode->data->instance.instance_init (instance, class);
1884 if (node->data->instance.private_size)
1885 instance_real_class_remove (instance);
1887 instance->g_class = class;
1888 if (node->data->instance.instance_init)
1889 node->data->instance.instance_init (instance, class);
1891 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1897 * g_type_free_instance:
1898 * @instance: an instance of a type.
1900 * Frees an instance of a type, returning it to the instance pool for
1901 * the type, if there is one.
1903 * Like g_type_create_instance(), this function is reserved for
1904 * implementors of fundamental types.
1907 g_type_free_instance (GTypeInstance *instance)
1912 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1914 class = instance->g_class;
1915 node = lookup_type_node_I (class->g_type);
1916 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1918 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1919 type_descriptive_name_I (class->g_type));
1922 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1923 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1925 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1930 instance->g_class = NULL;
1931 #ifdef G_ENABLE_DEBUG
1932 memset (instance, 0xaa, type_total_instance_size_I (node));
1934 g_slice_free1 (type_total_instance_size_I (node), instance);
1936 g_type_class_unref (class);
1940 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1942 g_assert (iface->data);
1944 if (!iface->data->iface.dflt_vtable)
1946 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1947 iface->data->iface.dflt_vtable = vtable;
1948 vtable->g_type = NODE_TYPE (iface);
1949 vtable->g_instance_type = 0;
1950 if (iface->data->iface.vtable_init_base ||
1951 iface->data->iface.dflt_init)
1953 G_WRITE_UNLOCK (&type_rw_lock);
1954 if (iface->data->iface.vtable_init_base)
1955 iface->data->iface.vtable_init_base (vtable);
1956 if (iface->data->iface.dflt_init)
1957 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1958 G_WRITE_LOCK (&type_rw_lock);
1964 /* This is called to allocate and do the first part of initializing
1965 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1967 * A FALSE return indicates that we didn't find an init function for
1968 * this type/iface pair, so the vtable from the parent type should
1969 * be used. Note that the write lock is not modified upon a FALSE
1973 type_iface_vtable_base_init_Wm (TypeNode *iface,
1977 IFaceHolder *iholder;
1978 GTypeInterface *vtable = NULL;
1981 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1982 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1984 return FALSE; /* we don't modify write lock upon FALSE */
1986 type_iface_ensure_dflt_vtable_Wm (iface);
1988 entry = type_lookup_iface_entry_L (node, iface);
1990 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1992 entry->init_state = IFACE_INIT;
1994 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1995 if (pnode) /* want to copy over parent iface contents */
1997 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2000 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2003 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2004 entry->vtable = vtable;
2005 vtable->g_type = NODE_TYPE (iface);
2006 vtable->g_instance_type = NODE_TYPE (node);
2008 if (iface->data->iface.vtable_init_base)
2010 G_WRITE_UNLOCK (&type_rw_lock);
2011 iface->data->iface.vtable_init_base (vtable);
2012 G_WRITE_LOCK (&type_rw_lock);
2014 return TRUE; /* initialized the vtable */
2017 /* Finishes what type_iface_vtable_base_init_Wm started by
2018 * calling the interface init function.
2019 * this function may only be called for types with their
2020 * own interface holder info, i.e. types for which
2021 * g_type_add_interface*() was called and not children thereof.
2024 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2027 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2028 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2029 GTypeInterface *vtable = NULL;
2032 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2033 g_assert (iface->data && entry && iholder && iholder->info);
2034 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2036 entry->init_state = INITIALIZED;
2038 vtable = entry->vtable;
2040 if (iholder->info->interface_init)
2042 G_WRITE_UNLOCK (&type_rw_lock);
2043 if (iholder->info->interface_init)
2044 iholder->info->interface_init (vtable, iholder->info->interface_data);
2045 G_WRITE_LOCK (&type_rw_lock);
2048 for (i = 0; i < static_n_iface_check_funcs; i++)
2050 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2051 gpointer check_data = static_iface_check_funcs[i].check_data;
2053 G_WRITE_UNLOCK (&type_rw_lock);
2054 check_func (check_data, (gpointer)vtable);
2055 G_WRITE_LOCK (&type_rw_lock);
2060 type_iface_vtable_finalize_Wm (TypeNode *iface,
2062 GTypeInterface *vtable)
2064 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2065 IFaceHolder *iholder;
2067 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2068 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2070 return FALSE; /* we don't modify write lock upon FALSE */
2072 g_assert (entry && entry->vtable == vtable && iholder->info);
2074 entry->vtable = NULL;
2075 entry->init_state = UNINITIALIZED;
2076 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2078 G_WRITE_UNLOCK (&type_rw_lock);
2079 if (iholder->info->interface_finalize)
2080 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2081 if (iface->data->iface.vtable_finalize_base)
2082 iface->data->iface.vtable_finalize_base (vtable);
2083 G_WRITE_LOCK (&type_rw_lock);
2086 vtable->g_instance_type = 0;
2089 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2091 return TRUE; /* write lock modified */
2095 type_class_init_Wm (TypeNode *node,
2098 GSList *slist, *init_slist = NULL;
2100 IFaceEntries *entries;
2102 TypeNode *bnode, *pnode;
2105 /* Accessing data->class will work for instantiable types
2106 * too because ClassData is a subset of InstanceData
2108 g_assert (node->is_classed && node->data &&
2109 node->data->class.class_size &&
2110 !node->data->class.class &&
2111 node->data->class.init_state == UNINITIALIZED);
2112 if (node->data->class.class_private_size)
2113 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2115 class = g_malloc0 (node->data->class.class_size);
2116 node->data->class.class = class;
2117 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2121 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2123 memcpy (class, pclass, pnode->data->class.class_size);
2124 memcpy (G_STRUCT_MEMBER_P (class, ALIGN_STRUCT (node->data->class.class_size)), G_STRUCT_MEMBER_P (pclass, ALIGN_STRUCT (pnode->data->class.class_size)), pnode->data->class.class_private_size);
2126 if (node->is_instantiatable)
2128 /* We need to initialize the private_size here rather than in
2129 * type_data_make_W() since the class init for the parent
2130 * class may have changed pnode->data->instance.private_size.
2132 node->data->instance.private_size = pnode->data->instance.private_size;
2135 class->g_type = NODE_TYPE (node);
2137 G_WRITE_UNLOCK (&type_rw_lock);
2139 /* stack all base class initialization functions, so we
2140 * call them in ascending order.
2142 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2143 if (bnode->data->class.class_init_base)
2144 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2145 for (slist = init_slist; slist; slist = slist->next)
2147 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2149 class_init_base (class);
2151 g_slist_free (init_slist);
2153 G_WRITE_LOCK (&type_rw_lock);
2155 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2157 /* Before we initialize the class, base initialize all interfaces, either
2158 * from parent, or through our holder info
2160 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2163 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2164 i < IFACE_ENTRIES_N_ENTRIES (entries))
2166 entry = &entries->entry[i];
2167 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2168 entry->init_state == IFACE_INIT)
2174 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2177 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2180 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2182 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2183 * doesn't modify write lock upon FALSE, so entry is still valid;
2185 g_assert (pnode != NULL);
2188 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2190 IFaceEntry *pentry = &pentries->entry[j];
2192 if (pentry->iface_type == entry->iface_type)
2194 entry->vtable = pentry->vtable;
2195 entry->init_state = INITIALIZED;
2199 g_assert (entry->vtable != NULL);
2202 /* If the write lock was released, additional interface entries might
2203 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2204 * be base-initialized when inserted, so we don't have to worry that
2205 * we might miss them. Uninitialized entries can only be moved higher
2206 * when new ones are inserted.
2211 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2213 G_WRITE_UNLOCK (&type_rw_lock);
2215 if (node->data->class.class_init)
2216 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2218 G_WRITE_LOCK (&type_rw_lock);
2220 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2222 /* finish initializing the interfaces through our holder info.
2223 * inherited interfaces are already init_state == INITIALIZED, because
2224 * they either got setup in the above base_init loop, or during
2225 * class_init from within type_add_interface_Wm() for this or
2226 * an anchestor type.
2229 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2231 entry = &entries->entry[i];
2232 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2233 entry->init_state == INITIALIZED)
2239 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2242 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2244 /* As in the loop above, additional initialized entries might be inserted
2245 * if the write lock is released, but that's harmless because the entries
2246 * we need to initialize only move higher in the list.
2251 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2255 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2258 IFaceEntries *entries;
2260 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2263 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2264 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2266 IFaceEntry *entry = &entries->entry[i];
2269 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2271 /* refetch entries, IFACES_ENTRIES might be modified */
2276 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2277 * iface vtable came from parent
2279 entry->vtable = NULL;
2280 entry->init_state = UNINITIALIZED;
2287 type_data_finalize_class_U (TypeNode *node,
2290 GTypeClass *class = cdata->class;
2293 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2295 if (cdata->class_finalize)
2296 cdata->class_finalize (class, (gpointer) cdata->class_data);
2298 /* call all base class destruction functions in descending order
2300 if (cdata->class_finalize_base)
2301 cdata->class_finalize_base (class);
2302 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2303 if (bnode->data->class.class_finalize_base)
2304 bnode->data->class.class_finalize_base (class);
2306 g_free (cdata->class);
2310 type_data_last_unref_Wm (TypeNode *node,
2313 g_return_if_fail (node != NULL && node->plugin != NULL);
2315 if (!node->data || NODE_REFCOUNT (node) == 0)
2317 g_warning ("cannot drop last reference to unreferenced type `%s'",
2322 /* call class cache hooks */
2323 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2327 G_WRITE_UNLOCK (&type_rw_lock);
2328 G_READ_LOCK (&type_rw_lock);
2329 for (i = 0; i < static_n_class_cache_funcs; i++)
2331 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2332 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2333 gboolean need_break;
2335 G_READ_UNLOCK (&type_rw_lock);
2336 need_break = cache_func (cache_data, node->data->class.class);
2337 G_READ_LOCK (&type_rw_lock);
2338 if (!node->data || NODE_REFCOUNT (node) == 0)
2339 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2343 G_READ_UNLOCK (&type_rw_lock);
2344 G_WRITE_LOCK (&type_rw_lock);
2347 /* may have been re-referenced meanwhile */
2348 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2350 GType ptype = NODE_PARENT_TYPE (node);
2353 if (node->is_instantiatable)
2355 /* destroy node->data->instance.mem_chunk */
2359 if (node->is_classed && tdata->class.class)
2361 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2362 type_data_finalize_class_ifaces_Wm (node);
2363 node->mutatable_check_cache = FALSE;
2365 G_WRITE_UNLOCK (&type_rw_lock);
2366 type_data_finalize_class_U (node, &tdata->class);
2367 G_WRITE_LOCK (&type_rw_lock);
2369 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2371 node->mutatable_check_cache = FALSE;
2373 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2375 G_WRITE_UNLOCK (&type_rw_lock);
2376 if (tdata->iface.dflt_finalize)
2377 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2378 if (tdata->iface.vtable_finalize_base)
2379 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2380 G_WRITE_LOCK (&type_rw_lock);
2382 g_free (tdata->iface.dflt_vtable);
2386 node->mutatable_check_cache = FALSE;
2390 /* freeing tdata->common.value_table and its contents is taken care of
2391 * by allocating it in one chunk with tdata
2395 G_WRITE_UNLOCK (&type_rw_lock);
2396 g_type_plugin_unuse (node->plugin);
2398 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2399 G_WRITE_LOCK (&type_rw_lock);
2404 type_data_unref_U (TypeNode *node,
2410 current = NODE_REFCOUNT (node);
2416 g_warning ("static type `%s' unreferenced too often",
2421 g_assert (current > 0);
2423 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2424 G_WRITE_LOCK (&type_rw_lock);
2425 type_data_last_unref_Wm (node, uncached);
2426 G_WRITE_UNLOCK (&type_rw_lock);
2427 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2430 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2434 * g_type_add_class_cache_func: (skip)
2435 * @cache_data: data to be passed to @cache_func
2436 * @cache_func: a #GTypeClassCacheFunc
2438 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2439 * class goes from one to zero. This can be used to prevent premature class
2440 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2441 * until one of them returns %TRUE. The functions have to check the class id
2442 * passed in to figure whether they actually want to cache the class of this
2443 * type, since all classes are routed through the same #GTypeClassCacheFunc
2447 g_type_add_class_cache_func (gpointer cache_data,
2448 GTypeClassCacheFunc cache_func)
2452 g_return_if_fail (cache_func != NULL);
2454 G_WRITE_LOCK (&type_rw_lock);
2455 i = static_n_class_cache_funcs++;
2456 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2457 static_class_cache_funcs[i].cache_data = cache_data;
2458 static_class_cache_funcs[i].cache_func = cache_func;
2459 G_WRITE_UNLOCK (&type_rw_lock);
2463 * g_type_remove_class_cache_func: (skip)
2464 * @cache_data: data that was given when adding @cache_func
2465 * @cache_func: a #GTypeClassCacheFunc
2467 * Removes a previously installed #GTypeClassCacheFunc. The cache
2468 * maintained by @cache_func has to be empty when calling
2469 * g_type_remove_class_cache_func() to avoid leaks.
2472 g_type_remove_class_cache_func (gpointer cache_data,
2473 GTypeClassCacheFunc cache_func)
2475 gboolean found_it = FALSE;
2478 g_return_if_fail (cache_func != NULL);
2480 G_WRITE_LOCK (&type_rw_lock);
2481 for (i = 0; i < static_n_class_cache_funcs; i++)
2482 if (static_class_cache_funcs[i].cache_data == cache_data &&
2483 static_class_cache_funcs[i].cache_func == cache_func)
2485 static_n_class_cache_funcs--;
2486 g_memmove (static_class_cache_funcs + i,
2487 static_class_cache_funcs + i + 1,
2488 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2489 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2493 G_WRITE_UNLOCK (&type_rw_lock);
2496 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2497 cache_func, cache_data);
2502 * g_type_add_interface_check: (skip)
2503 * @check_data: data to pass to @check_func
2504 * @check_func: function to be called after each interface
2507 * Adds a function to be called after an interface vtable is
2508 * initialized for any class (i.e. after the @interface_init member of
2509 * #GInterfaceInfo has been called).
2511 * This function is useful when you want to check an invariant that
2512 * depends on the interfaces of a class. For instance, the
2513 * implementation of #GObject uses this facility to check that an
2514 * object implements all of the properties that are defined on its
2520 g_type_add_interface_check (gpointer check_data,
2521 GTypeInterfaceCheckFunc check_func)
2525 g_return_if_fail (check_func != NULL);
2527 G_WRITE_LOCK (&type_rw_lock);
2528 i = static_n_iface_check_funcs++;
2529 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2530 static_iface_check_funcs[i].check_data = check_data;
2531 static_iface_check_funcs[i].check_func = check_func;
2532 G_WRITE_UNLOCK (&type_rw_lock);
2536 * g_type_remove_interface_check: (skip)
2537 * @check_data: callback data passed to g_type_add_interface_check()
2538 * @check_func: callback function passed to g_type_add_interface_check()
2540 * Removes an interface check function added with
2541 * g_type_add_interface_check().
2546 g_type_remove_interface_check (gpointer check_data,
2547 GTypeInterfaceCheckFunc check_func)
2549 gboolean found_it = FALSE;
2552 g_return_if_fail (check_func != NULL);
2554 G_WRITE_LOCK (&type_rw_lock);
2555 for (i = 0; i < static_n_iface_check_funcs; i++)
2556 if (static_iface_check_funcs[i].check_data == check_data &&
2557 static_iface_check_funcs[i].check_func == check_func)
2559 static_n_iface_check_funcs--;
2560 g_memmove (static_iface_check_funcs + i,
2561 static_iface_check_funcs + i + 1,
2562 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2563 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2567 G_WRITE_UNLOCK (&type_rw_lock);
2570 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2571 check_func, check_data);
2574 /* --- type registration --- */
2576 * g_type_register_fundamental:
2577 * @type_id: A predefined type identifier.
2578 * @type_name: 0-terminated string used as the name of the new type.
2579 * @info: The #GTypeInfo structure for this type.
2580 * @finfo: The #GTypeFundamentalInfo structure for this type.
2581 * @flags: Bitwise combination of #GTypeFlags values.
2583 * Registers @type_id as the predefined identifier and @type_name as the
2584 * name of a fundamental type. The type system uses the information
2585 * contained in the #GTypeInfo structure pointed to by @info and the
2586 * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
2587 * type and its instances. The value of @flags determines additional
2588 * characteristics of the fundamental type.
2590 * Returns: The predefined type identifier.
2593 g_type_register_fundamental (GType type_id,
2594 const gchar *type_name,
2595 const GTypeInfo *info,
2596 const GTypeFundamentalInfo *finfo,
2601 g_return_val_if_type_system_uninitialized (0);
2602 g_return_val_if_fail (type_id > 0, 0);
2603 g_return_val_if_fail (type_name != NULL, 0);
2604 g_return_val_if_fail (info != NULL, 0);
2605 g_return_val_if_fail (finfo != NULL, 0);
2607 if (!check_type_name_I (type_name))
2609 if ((type_id & TYPE_ID_MASK) ||
2610 type_id > G_TYPE_FUNDAMENTAL_MAX)
2612 g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2617 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2618 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2620 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2624 if (lookup_type_node_I (type_id))
2626 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2627 type_descriptive_name_I (type_id),
2632 G_WRITE_LOCK (&type_rw_lock);
2633 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2634 type_add_flags_W (node, flags);
2636 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2637 type_data_make_W (node, info,
2638 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2639 G_WRITE_UNLOCK (&type_rw_lock);
2641 return NODE_TYPE (node);
2645 * g_type_register_static_simple: (skip)
2646 * @parent_type: Type from which this type will be derived.
2647 * @type_name: 0-terminated string used as the name of the new type.
2648 * @class_size: Size of the class structure (see #GTypeInfo)
2649 * @class_init: Location of the class initialization function (see #GTypeInfo)
2650 * @instance_size: Size of the instance structure (see #GTypeInfo)
2651 * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2652 * @flags: Bitwise combination of #GTypeFlags values.
2654 * Registers @type_name as the name of a new static type derived from
2655 * @parent_type. The value of @flags determines the nature (e.g.
2656 * abstract or not) of the type. It works by filling a #GTypeInfo
2657 * struct and calling g_type_register_static().
2661 * Returns: The new type identifier.
2664 g_type_register_static_simple (GType parent_type,
2665 const gchar *type_name,
2667 GClassInitFunc class_init,
2668 guint instance_size,
2669 GInstanceInitFunc instance_init,
2674 info.class_size = class_size;
2675 info.base_init = NULL;
2676 info.base_finalize = NULL;
2677 info.class_init = class_init;
2678 info.class_finalize = NULL;
2679 info.class_data = NULL;
2680 info.instance_size = instance_size;
2681 info.n_preallocs = 0;
2682 info.instance_init = instance_init;
2683 info.value_table = NULL;
2685 return g_type_register_static (parent_type, type_name, &info, flags);
2689 * g_type_register_static:
2690 * @parent_type: Type from which this type will be derived.
2691 * @type_name: 0-terminated string used as the name of the new type.
2692 * @info: The #GTypeInfo structure for this type.
2693 * @flags: Bitwise combination of #GTypeFlags values.
2695 * Registers @type_name as the name of a new static type derived from
2696 * @parent_type. The type system uses the information contained in the
2697 * #GTypeInfo structure pointed to by @info to manage the type and its
2698 * instances (if not abstract). The value of @flags determines the nature
2699 * (e.g. abstract or not) of the type.
2701 * Returns: The new type identifier.
2704 g_type_register_static (GType parent_type,
2705 const gchar *type_name,
2706 const GTypeInfo *info,
2709 TypeNode *pnode, *node;
2712 g_return_val_if_type_system_uninitialized (0);
2713 g_return_val_if_fail (parent_type > 0, 0);
2714 g_return_val_if_fail (type_name != NULL, 0);
2715 g_return_val_if_fail (info != NULL, 0);
2717 if (!check_type_name_I (type_name) ||
2718 !check_derivation_I (parent_type, type_name))
2720 if (info->class_finalize)
2722 g_warning ("class finalizer specified for static type `%s'",
2727 pnode = lookup_type_node_I (parent_type);
2728 G_WRITE_LOCK (&type_rw_lock);
2729 type_data_ref_Wm (pnode);
2730 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2732 node = type_node_new_W (pnode, type_name, NULL);
2733 type_add_flags_W (node, flags);
2734 type = NODE_TYPE (node);
2735 type_data_make_W (node, info,
2736 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2738 G_WRITE_UNLOCK (&type_rw_lock);
2744 * g_type_register_dynamic:
2745 * @parent_type: Type from which this type will be derived.
2746 * @type_name: 0-terminated string used as the name of the new type.
2747 * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2748 * @flags: Bitwise combination of #GTypeFlags values.
2750 * Registers @type_name as the name of a new dynamic type derived from
2751 * @parent_type. The type system uses the information contained in the
2752 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2753 * instances (if not abstract). The value of @flags determines the nature
2754 * (e.g. abstract or not) of the type.
2756 * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2759 g_type_register_dynamic (GType parent_type,
2760 const gchar *type_name,
2761 GTypePlugin *plugin,
2764 TypeNode *pnode, *node;
2767 g_return_val_if_type_system_uninitialized (0);
2768 g_return_val_if_fail (parent_type > 0, 0);
2769 g_return_val_if_fail (type_name != NULL, 0);
2770 g_return_val_if_fail (plugin != NULL, 0);
2772 if (!check_type_name_I (type_name) ||
2773 !check_derivation_I (parent_type, type_name) ||
2774 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2777 G_WRITE_LOCK (&type_rw_lock);
2778 pnode = lookup_type_node_I (parent_type);
2779 node = type_node_new_W (pnode, type_name, plugin);
2780 type_add_flags_W (node, flags);
2781 type = NODE_TYPE (node);
2782 G_WRITE_UNLOCK (&type_rw_lock);
2788 * g_type_add_interface_static:
2789 * @instance_type: #GType value of an instantiable type.
2790 * @interface_type: #GType value of an interface type.
2791 * @info: The #GInterfaceInfo structure for this
2792 * (@instance_type, @interface_type) combination.
2794 * Adds the static @interface_type to @instantiable_type. The information
2795 * contained in the #GTypeInterfaceInfo structure pointed to by @info
2796 * is used to manage the relationship.
2799 g_type_add_interface_static (GType instance_type,
2800 GType interface_type,
2801 const GInterfaceInfo *info)
2803 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2804 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2805 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2807 /* we only need to lock class_init_rec_mutex if instance_type already has its
2808 * class initialized, however this function is rarely enough called to take
2809 * the simple route and always acquire class_init_rec_mutex.
2811 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2812 G_WRITE_LOCK (&type_rw_lock);
2813 if (check_add_interface_L (instance_type, interface_type))
2815 TypeNode *node = lookup_type_node_I (instance_type);
2816 TypeNode *iface = lookup_type_node_I (interface_type);
2817 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2818 type_add_interface_Wm (node, iface, info, NULL);
2820 G_WRITE_UNLOCK (&type_rw_lock);
2821 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2825 * g_type_add_interface_dynamic:
2826 * @instance_type: the #GType value of an instantiable type.
2827 * @interface_type: the #GType value of an interface type.
2828 * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2830 * Adds the dynamic @interface_type to @instantiable_type. The information
2831 * contained in the #GTypePlugin structure pointed to by @plugin
2832 * is used to manage the relationship.
2835 g_type_add_interface_dynamic (GType instance_type,
2836 GType interface_type,
2837 GTypePlugin *plugin)
2840 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2841 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2842 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2844 node = lookup_type_node_I (instance_type);
2845 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2848 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2849 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2850 G_WRITE_LOCK (&type_rw_lock);
2851 if (check_add_interface_L (instance_type, interface_type))
2853 TypeNode *iface = lookup_type_node_I (interface_type);
2854 type_add_interface_Wm (node, iface, NULL, plugin);
2856 G_WRITE_UNLOCK (&type_rw_lock);
2857 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2861 /* --- public API functions --- */
2864 * @type: Type ID of a classed type.
2866 * Increments the reference count of the class structure belonging to
2867 * @type. This function will demand-create the class if it doesn't
2870 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
2871 * structure for the given type ID.
2874 g_type_class_ref (GType type)
2881 /* optimize for common code path */
2882 node = lookup_type_node_I (type);
2883 if (!node || !node->is_classed)
2885 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2886 type_descriptive_name_I (type));
2890 if (G_LIKELY (type_data_ref_U (node)))
2892 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2893 return node->data->class.class;
2899 /* here, we either have node->data->class.class == NULL, or a recursive
2900 * call to g_type_class_ref() with a partly initialized class, or
2901 * node->data->class.init_state == INITIALIZED, because any
2902 * concurrently running initialization was guarded by class_init_rec_mutex.
2904 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2906 /* we need an initialized parent class for initializing derived classes */
2907 ptype = NODE_PARENT_TYPE (node);
2908 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2910 G_WRITE_LOCK (&type_rw_lock);
2913 type_data_ref_Wm (node);
2915 if (!node->data->class.class) /* class uninitialized */
2916 type_class_init_Wm (node, pclass);
2918 G_WRITE_UNLOCK (&type_rw_lock);
2921 g_type_class_unref (pclass);
2923 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2925 return node->data->class.class;
2929 * g_type_class_unref:
2930 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
2933 * Decrements the reference count of the class structure being passed in.
2934 * Once the last reference count of a class has been released, classes
2935 * may be finalized by the type system, so further dereferencing of a
2936 * class pointer after g_type_class_unref() are invalid.
2939 g_type_class_unref (gpointer g_class)
2942 GTypeClass *class = g_class;
2944 g_return_if_fail (g_class != NULL);
2946 node = lookup_type_node_I (class->g_type);
2947 if (node && node->is_classed && NODE_REFCOUNT (node))
2948 type_data_unref_U (node, FALSE);
2950 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2951 type_descriptive_name_I (class->g_type));
2955 * g_type_class_unref_uncached: (skip)
2956 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
2959 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2960 * implementations. It unreferences a class without consulting the chain
2961 * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2965 g_type_class_unref_uncached (gpointer g_class)
2968 GTypeClass *class = g_class;
2970 g_return_if_fail (g_class != NULL);
2972 node = lookup_type_node_I (class->g_type);
2973 if (node && node->is_classed && NODE_REFCOUNT (node))
2974 type_data_unref_U (node, TRUE);
2976 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2977 type_descriptive_name_I (class->g_type));
2981 * g_type_class_peek:
2982 * @type: Type ID of a classed type.
2984 * This function is essentially the same as g_type_class_ref(), except that
2985 * the classes reference count isn't incremented. As a consequence, this function
2986 * may return %NULL if the class of the type passed in does not currently
2987 * exist (hasn't been referenced before).
2989 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
2990 * structure for the given type ID or %NULL if the class does not
2994 g_type_class_peek (GType type)
2999 node = lookup_type_node_I (type);
3000 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3001 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3002 /* ref_count _may_ be 0 */
3003 class = node->data->class.class;
3011 * g_type_class_peek_static:
3012 * @type: Type ID of a classed type.
3014 * A more efficient version of g_type_class_peek() which works only for
3018 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
3019 * structure for the given type ID or %NULL if the class does not
3020 * currently exist or is dynamically loaded.
3023 g_type_class_peek_static (GType type)
3028 node = lookup_type_node_I (type);
3029 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3030 /* peek only static types: */ node->plugin == NULL &&
3031 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3032 /* ref_count _may_ be 0 */
3033 class = node->data->class.class;
3041 * g_type_class_peek_parent:
3042 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
3043 * retrieve the parent class for.
3045 * This is a convenience function often needed in class initializers.
3046 * It returns the class structure of the immediate parent type of the
3047 * class passed in. Since derived classes hold a reference count on
3048 * their parent classes as long as they are instantiated, the returned
3049 * class will always exist. This function is essentially equivalent
3053 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
3056 * Returns: (type GObject.TypeClass) (transfer none): The parent class
3060 g_type_class_peek_parent (gpointer g_class)
3063 gpointer class = NULL;
3065 g_return_val_if_fail (g_class != NULL, NULL);
3067 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3068 /* We used to acquire a read lock here. That is not necessary, since
3069 * parent->data->class.class is constant as long as the derived class
3072 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3074 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3075 class = node->data->class.class;
3077 else if (NODE_PARENT_TYPE (node))
3078 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
3084 * g_type_interface_peek:
3085 * @instance_class: (type GObject.TypeClass): A #GTypeClass structure.
3086 * @iface_type: An interface ID which this class conforms to.
3088 * Returns the #GTypeInterface structure of an interface to which the
3089 * passed in class conforms.
3091 * Returns: (type GObject.TypeInterface) (transfer none): The GTypeInterface
3092 * structure of iface_type if implemented by @instance_class, %NULL
3096 g_type_interface_peek (gpointer instance_class,
3101 gpointer vtable = NULL;
3102 GTypeClass *class = instance_class;
3104 g_return_val_if_fail (instance_class != NULL, NULL);
3106 node = lookup_type_node_I (class->g_type);
3107 iface = lookup_type_node_I (iface_type);
3108 if (node && node->is_instantiatable && iface)
3109 type_lookup_iface_vtable_I (node, iface, &vtable);
3111 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
3117 * g_type_interface_peek_parent:
3118 * @g_iface: (type GObject.TypeInterface): A #GTypeInterface structure.
3120 * Returns the corresponding #GTypeInterface structure of the parent type
3121 * of the instance type to which @g_iface belongs. This is useful when
3122 * deriving the implementation of an interface from the parent type and
3123 * then possibly overriding some methods.
3125 * Returns: (transfer none) (type GObject.TypeInterface): The
3126 * corresponding #GTypeInterface structure of the parent type of the
3127 * instance type to which @g_iface belongs, or %NULL if the parent
3128 * type doesn't conform to the interface.
3131 g_type_interface_peek_parent (gpointer g_iface)
3135 gpointer vtable = NULL;
3136 GTypeInterface *iface_class = g_iface;
3138 g_return_val_if_fail (g_iface != NULL, NULL);
3140 iface = lookup_type_node_I (iface_class->g_type);
3141 node = lookup_type_node_I (iface_class->g_instance_type);
3143 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3144 if (node && node->is_instantiatable && iface)
3145 type_lookup_iface_vtable_I (node, iface, &vtable);
3147 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
3153 * g_type_default_interface_ref:
3154 * @g_type: an interface type
3156 * Increments the reference count for the interface type @g_type,
3157 * and returns the default interface vtable for the type.
3159 * If the type is not currently in use, then the default vtable
3160 * for the type will be created and initalized by calling
3161 * the base interface init and default vtable init functions for
3162 * the type (the @<structfield>base_init</structfield>
3163 * and <structfield>class_init</structfield> members of #GTypeInfo).
3164 * Calling g_type_default_interface_ref() is useful when you
3165 * want to make sure that signals and properties for an interface
3166 * have been installed.
3170 * Returns: (type GObject.TypeInterface) (transfer none): the default
3171 * vtable for the interface; call g_type_default_interface_unref()
3172 * when you are done using the interface.
3175 g_type_default_interface_ref (GType g_type)
3178 gpointer dflt_vtable;
3180 G_WRITE_LOCK (&type_rw_lock);
3182 node = lookup_type_node_I (g_type);
3183 if (!node || !NODE_IS_IFACE (node) ||
3184 (node->data && NODE_REFCOUNT (node) == 0))
3186 G_WRITE_UNLOCK (&type_rw_lock);
3187 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3188 type_descriptive_name_I (g_type));
3192 if (!node->data || !node->data->iface.dflt_vtable)
3194 G_WRITE_UNLOCK (&type_rw_lock);
3195 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3196 G_WRITE_LOCK (&type_rw_lock);
3197 node = lookup_type_node_I (g_type);
3198 type_data_ref_Wm (node);
3199 type_iface_ensure_dflt_vtable_Wm (node);
3200 g_static_rec_mutex_unlock (&class_init_rec_mutex);
3203 type_data_ref_Wm (node); /* ref_count >= 1 already */
3205 dflt_vtable = node->data->iface.dflt_vtable;
3206 G_WRITE_UNLOCK (&type_rw_lock);
3212 * g_type_default_interface_peek:
3213 * @g_type: an interface type
3215 * If the interface type @g_type is currently in use, returns its
3216 * default interface vtable.
3220 * Returns: (type GObject.TypeInterface) (transfer none): the default
3221 * vtable for the interface, or %NULL if the type is not currently in
3225 g_type_default_interface_peek (GType g_type)
3230 node = lookup_type_node_I (g_type);
3231 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3232 vtable = node->data->iface.dflt_vtable;
3240 * g_type_default_interface_unref:
3241 * @g_iface: (type GObject.TypeInterface): the default vtable
3242 * structure for a interface, as returned by
3243 * g_type_default_interface_ref()
3245 * Decrements the reference count for the type corresponding to the
3246 * interface default vtable @g_iface. If the type is dynamic, then
3247 * when no one is using the interface and all references have
3248 * been released, the finalize function for the interface's default
3249 * vtable (the <structfield>class_finalize</structfield> member of
3250 * #GTypeInfo) will be called.
3255 g_type_default_interface_unref (gpointer g_iface)
3258 GTypeInterface *vtable = g_iface;
3260 g_return_if_fail (g_iface != NULL);
3262 node = lookup_type_node_I (vtable->g_type);
3263 if (node && NODE_IS_IFACE (node))
3264 type_data_unref_U (node, FALSE);
3266 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3267 type_descriptive_name_I (vtable->g_type));
3272 * @type: Type to return name for.
3274 * Get the unique name that is assigned to a type ID. Note that this
3275 * function (like all other GType API) cannot cope with invalid type
3276 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3277 * other validly registered type ID, but randomized type IDs should
3278 * not be passed in and will most likely lead to a crash.
3280 * Returns: Static type name or %NULL.
3282 G_CONST_RETURN gchar*
3283 g_type_name (GType type)
3287 g_return_val_if_type_system_uninitialized (NULL);
3289 node = lookup_type_node_I (type);
3291 return node ? NODE_NAME (node) : NULL;
3296 * @type: Type to return quark of type name for.
3298 * Get the corresponding quark of the type IDs name.
3300 * Returns: The type names quark or 0.
3303 g_type_qname (GType type)
3307 node = lookup_type_node_I (type);
3309 return node ? node->qname : 0;
3314 * @name: Type name to lookup.
3316 * Lookup the type ID from a given type name, returning 0 if no type
3317 * has been registered under this name (this is the preferred method
3318 * to find out by name whether a specific type has been registered
3321 * Returns: Corresponding type ID or 0.
3324 g_type_from_name (const gchar *name)
3329 g_return_val_if_fail (name != NULL, 0);
3331 quark = g_quark_try_string (name);
3334 G_READ_LOCK (&type_rw_lock);
3335 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
3336 G_READ_UNLOCK (&type_rw_lock);
3344 * @type: The derived type.
3346 * Return the direct parent type of the passed in type. If the passed
3347 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3349 * Returns: The parent type.
3352 g_type_parent (GType type)
3356 node = lookup_type_node_I (type);
3358 return node ? NODE_PARENT_TYPE (node) : 0;
3363 * @type: A #GType value.
3365 * Returns the length of the ancestry of the passed in type. This
3366 * includes the type itself, so that e.g. a fundamental type has depth 1.
3368 * Returns: The depth of @type.
3371 g_type_depth (GType type)
3375 node = lookup_type_node_I (type);
3377 return node ? node->n_supers + 1 : 0;
3382 * @leaf_type: Descendant of @root_type and the type to be returned.
3383 * @root_type: Immediate parent of the returned type.
3385 * Given a @leaf_type and a @root_type which is contained in its
3386 * anchestry, return the type that @root_type is the immediate parent
3387 * of. In other words, this function determines the type that is
3388 * derived directly from @root_type which is also a base class of
3389 * @leaf_type. Given a root type and a leaf type, this function can
3390 * be used to determine the types and order in which the leaf type is
3391 * descended from the root type.
3393 * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3396 g_type_next_base (GType type,
3402 node = lookup_type_node_I (type);
3405 TypeNode *base_node = lookup_type_node_I (base_type);
3407 if (base_node && base_node->n_supers < node->n_supers)
3409 guint n = node->n_supers - base_node->n_supers;
3411 if (node->supers[n] == base_type)
3412 atype = node->supers[n - 1];
3419 static inline gboolean
3420 type_node_check_conformities_UorL (TypeNode *node,
3421 TypeNode *iface_node,
3422 /* support_inheritance */
3423 gboolean support_interfaces,
3424 gboolean support_prerequisites,
3429 if (/* support_inheritance && */
3430 NODE_IS_ANCESTOR (iface_node, node))
3433 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3434 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3436 if (support_interfaces)
3440 if (type_lookup_iface_entry_L (node, iface_node))
3445 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3450 support_prerequisites)
3453 G_READ_LOCK (&type_rw_lock);
3454 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3457 G_READ_UNLOCK (&type_rw_lock);
3463 type_node_is_a_L (TypeNode *node,
3464 TypeNode *iface_node)
3466 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3469 static inline gboolean
3470 type_node_conforms_to_U (TypeNode *node,
3471 TypeNode *iface_node,
3472 gboolean support_interfaces,
3473 gboolean support_prerequisites)
3475 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3480 * @type: Type to check anchestry for.
3481 * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3483 * If @is_a_type is a derivable type, check whether @type is a
3484 * descendant of @is_a_type. If @is_a_type is an interface, check
3485 * whether @type conforms to it.
3487 * Returns: %TRUE if @type is_a @is_a_type holds true.
3490 g_type_is_a (GType type,
3493 TypeNode *node, *iface_node;
3496 node = lookup_type_node_I (type);
3497 iface_node = lookup_type_node_I (iface_type);
3498 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3505 * @type: The parent type.
3506 * @n_children: (out) (allow-none): Optional #guint pointer to contain
3507 * the number of child types.
3509 * Return a newly allocated and 0-terminated array of type IDs, listing the
3510 * child types of @type. The return value has to be g_free()ed after use.
3512 * Returns: (array length=n_children) (transfer full): Newly allocated
3513 * and 0-terminated array of child types.
3516 g_type_children (GType type,
3521 node = lookup_type_node_I (type);
3526 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3527 children = g_new (GType, node->n_children + 1);
3528 memcpy (children, node->children, sizeof (GType) * node->n_children);
3529 children[node->n_children] = 0;
3532 *n_children = node->n_children;
3533 G_READ_UNLOCK (&type_rw_lock);
3547 * g_type_interfaces:
3548 * @type: The type to list interface types for.
3549 * @n_interfaces: (out) (allow-none): Optional #guint pointer to
3550 * contain the number of interface types.
3552 * Return a newly allocated and 0-terminated array of type IDs, listing the
3553 * interface types that @type conforms to. The return value has to be
3554 * g_free()ed after use.
3556 * Returns: (array length=n_interfaces) (transfer full): Newly
3557 * allocated and 0-terminated array of interface types.
3560 g_type_interfaces (GType type,
3561 guint *n_interfaces)
3565 node = lookup_type_node_I (type);
3566 if (node && node->is_instantiatable)
3568 IFaceEntries *entries;
3572 G_READ_LOCK (&type_rw_lock);
3573 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3576 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3577 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3578 ifaces[i] = entries->entry[i].iface_type;
3582 ifaces = g_new (GType, 1);
3589 G_READ_UNLOCK (&type_rw_lock);
3602 typedef struct _QData QData;
3614 static inline gpointer
3615 type_get_qdata_L (TypeNode *node,
3618 GData *gdata = node->global_gdata;
3620 if (quark && gdata && gdata->n_qdatas)
3622 QData *qdatas = gdata->qdatas - 1;
3623 guint n_qdatas = gdata->n_qdatas;
3630 i = (n_qdatas + 1) / 2;
3632 if (quark == check->quark)
3634 else if (quark > check->quark)
3639 else /* if (quark < check->quark) */
3650 * @quark: a #GQuark id to identify the data
3652 * Obtains data which has previously been attached to @type
3653 * with g_type_set_qdata().
3655 * Returns: (transfer none): the data, or %NULL if no data was found
3658 g_type_get_qdata (GType type,
3664 node = lookup_type_node_I (type);
3667 G_READ_LOCK (&type_rw_lock);
3668 data = type_get_qdata_L (node, quark);
3669 G_READ_UNLOCK (&type_rw_lock);
3673 g_return_val_if_fail (node != NULL, NULL);
3680 type_set_qdata_W (TypeNode *node,
3688 /* setup qdata list if necessary */
3689 if (!node->global_gdata)
3690 node->global_gdata = g_new0 (GData, 1);
3691 gdata = node->global_gdata;
3693 /* try resetting old data */
3694 qdata = gdata->qdatas;
3695 for (i = 0; i < gdata->n_qdatas; i++)
3696 if (qdata[i].quark == quark)
3698 qdata[i].data = data;
3704 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3705 qdata = gdata->qdatas;
3706 for (i = 0; i < gdata->n_qdatas - 1; i++)
3707 if (qdata[i].quark > quark)
3709 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3710 qdata[i].quark = quark;
3711 qdata[i].data = data;
3717 * @quark: a #GQuark id to identify the data
3720 * Attaches arbitrary data to a type.
3723 g_type_set_qdata (GType type,
3729 g_return_if_fail (quark != 0);
3731 node = lookup_type_node_I (type);
3734 G_WRITE_LOCK (&type_rw_lock);
3735 type_set_qdata_W (node, quark, data);
3736 G_WRITE_UNLOCK (&type_rw_lock);
3739 g_return_if_fail (node != NULL);
3743 type_add_flags_W (TypeNode *node,
3748 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3749 g_return_if_fail (node != NULL);
3751 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3752 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3753 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3755 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3760 * @type: the #GType value of a static, classed type.
3761 * @query: (out caller-allocates): A user provided structure that is
3762 * filled in with constant values upon success.
3764 * Queries the type system for information about a specific type.
3765 * This function will fill in a user-provided structure to hold
3766 * type-specific information. If an invalid #GType is passed in, the
3767 * @type member of the #GTypeQuery is 0. All members filled into the
3768 * #GTypeQuery structure should be considered constant and have to be
3772 g_type_query (GType type,
3777 g_return_if_fail (query != NULL);
3779 /* if node is not static and classed, we won't allow query */
3781 node = lookup_type_node_I (type);
3782 if (node && node->is_classed && !node->plugin)
3784 /* type is classed and probably even instantiatable */
3785 G_READ_LOCK (&type_rw_lock);
3786 if (node->data) /* type is static or referenced */
3788 query->type = NODE_TYPE (node);
3789 query->type_name = NODE_NAME (node);
3790 query->class_size = node->data->class.class_size;
3791 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3793 G_READ_UNLOCK (&type_rw_lock);
3798 /* --- implementation details --- */
3800 g_type_test_flags (GType type,
3804 gboolean result = FALSE;
3806 node = lookup_type_node_I (type);
3809 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3810 guint tflags = flags & TYPE_FLAG_MASK;
3814 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3816 fflags = (finfo->type_flags & fflags) == fflags;
3823 G_READ_LOCK (&type_rw_lock);
3824 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3825 G_READ_UNLOCK (&type_rw_lock);
3830 result = tflags && fflags;
3837 * g_type_get_plugin:
3838 * @type: The #GType to retrieve the plugin for.
3840 * Returns the #GTypePlugin structure for @type or
3841 * %NULL if @type does not have a #GTypePlugin structure.
3843 * Returns: (transfer none): The corresponding plugin if @type is a
3844 * dynamic type, %NULL otherwise.
3847 g_type_get_plugin (GType type)
3851 node = lookup_type_node_I (type);
3853 return node ? node->plugin : NULL;
3857 * g_type_interface_get_plugin:
3858 * @instance_type: the #GType value of an instantiatable type.
3859 * @interface_type: the #GType value of an interface type.
3861 * Returns the #GTypePlugin structure for the dynamic interface
3862 * @interface_type which has been added to @instance_type, or %NULL if
3863 * @interface_type has not been added to @instance_type or does not
3864 * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3866 * Returns: (transfer none): the #GTypePlugin for the dynamic
3867 * interface @interface_type of @instance_type.
3870 g_type_interface_get_plugin (GType instance_type,
3871 GType interface_type)
3876 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3878 node = lookup_type_node_I (instance_type);
3879 iface = lookup_type_node_I (interface_type);
3882 IFaceHolder *iholder;
3883 GTypePlugin *plugin;
3885 G_READ_LOCK (&type_rw_lock);
3887 iholder = iface_node_get_holders_L (iface);
3888 while (iholder && iholder->instance_type != instance_type)
3889 iholder = iholder->next;
3890 plugin = iholder ? iholder->plugin : NULL;
3892 G_READ_UNLOCK (&type_rw_lock);
3897 g_return_val_if_fail (node == NULL, NULL);
3898 g_return_val_if_fail (iface == NULL, NULL);
3900 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3906 * g_type_fundamental_next:
3908 * Returns the next free fundamental type id which can be used to
3909 * register a new fundamental type with g_type_register_fundamental().
3910 * The returned type ID represents the highest currently registered
3911 * fundamental type identifier.
3913 * Returns: The nextmost fundamental type ID to be registered,
3914 * or 0 if the type system ran out of fundamental type IDs.
3917 g_type_fundamental_next (void)
3921 G_READ_LOCK (&type_rw_lock);
3922 type = static_fundamental_next;
3923 G_READ_UNLOCK (&type_rw_lock);
3924 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3925 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3929 * g_type_fundamental:
3930 * @type_id: valid type ID
3932 * Internal function, used to extract the fundamental type ID portion.
3933 * use G_TYPE_FUNDAMENTAL() instead.
3935 * Returns: fundamental type ID
3938 g_type_fundamental (GType type_id)
3940 TypeNode *node = lookup_type_node_I (type_id);
3942 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3946 g_type_check_instance_is_a (GTypeInstance *type_instance,
3949 TypeNode *node, *iface;
3952 if (!type_instance || !type_instance->g_class)
3955 node = lookup_type_node_I (type_instance->g_class->g_type);
3956 iface = lookup_type_node_I (iface_type);
3957 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3963 g_type_check_class_is_a (GTypeClass *type_class,
3966 TypeNode *node, *iface;
3972 node = lookup_type_node_I (type_class->g_type);
3973 iface = lookup_type_node_I (is_a_type);
3974 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3980 g_type_check_instance_cast (GTypeInstance *type_instance,
3985 if (type_instance->g_class)
3987 TypeNode *node, *iface;
3988 gboolean is_instantiatable, check;
3990 node = lookup_type_node_I (type_instance->g_class->g_type);
3991 is_instantiatable = node && node->is_instantiatable;
3992 iface = lookup_type_node_I (iface_type);
3993 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3995 return type_instance;
3997 if (is_instantiatable)
3998 g_warning ("invalid cast from `%s' to `%s'",
3999 type_descriptive_name_I (type_instance->g_class->g_type),
4000 type_descriptive_name_I (iface_type));
4002 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
4003 type_descriptive_name_I (type_instance->g_class->g_type),
4004 type_descriptive_name_I (iface_type));
4007 g_warning ("invalid unclassed pointer in cast to `%s'",
4008 type_descriptive_name_I (iface_type));
4011 return type_instance;
4015 g_type_check_class_cast (GTypeClass *type_class,
4020 TypeNode *node, *iface;
4021 gboolean is_classed, check;
4023 node = lookup_type_node_I (type_class->g_type);
4024 is_classed = node && node->is_classed;
4025 iface = lookup_type_node_I (is_a_type);
4026 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4031 g_warning ("invalid class cast from `%s' to `%s'",
4032 type_descriptive_name_I (type_class->g_type),
4033 type_descriptive_name_I (is_a_type));
4035 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
4036 type_descriptive_name_I (type_class->g_type),
4037 type_descriptive_name_I (is_a_type));
4040 g_warning ("invalid class cast from (NULL) pointer to `%s'",
4041 type_descriptive_name_I (is_a_type));
4046 * g_type_check_instance:
4047 * @instance: A valid #GTypeInstance structure.
4049 * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
4052 * @Returns: #TRUE if @instance is valid, #FALSE otherwise.
4055 g_type_check_instance (GTypeInstance *type_instance)
4057 /* this function is just here to make the signal system
4058 * conveniently elaborated on instance checks
4062 if (type_instance->g_class)
4064 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4066 if (node && node->is_instantiatable)
4069 g_warning ("instance of invalid non-instantiatable type `%s'",
4070 type_descriptive_name_I (type_instance->g_class->g_type));
4073 g_warning ("instance with invalid (NULL) class pointer");
4076 g_warning ("invalid (NULL) pointer instance");
4081 static inline gboolean
4082 type_check_is_value_type_U (GType type)
4084 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4087 /* common path speed up */
4088 node = lookup_type_node_I (type);
4089 if (node && node->mutatable_check_cache)
4092 G_READ_LOCK (&type_rw_lock);
4096 if (node->data && NODE_REFCOUNT (node) > 0 &&
4097 node->data->common.value_table->value_init)
4098 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4099 else if (NODE_IS_IFACE (node))
4103 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4105 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4106 TypeNode *prnode = lookup_type_node_I (prtype);
4108 if (prnode->is_instantiatable)
4111 node = lookup_type_node_I (type);
4117 G_READ_UNLOCK (&type_rw_lock);
4119 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4123 g_type_check_is_value_type (GType type)
4125 return type_check_is_value_type_U (type);
4129 g_type_check_value (GValue *value)
4131 return value && type_check_is_value_type_U (value->g_type);
4135 g_type_check_value_holds (GValue *value,
4138 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4142 * g_type_value_table_peek: (skip)
4143 * @type: A #GType value.
4145 * Returns the location of the #GTypeValueTable associated with @type.
4146 * <emphasis>Note that this function should only be used from source code
4147 * that implements or has internal knowledge of the implementation of
4150 * Returns: Location of the #GTypeValueTable associated with @type or
4151 * %NULL if there is no #GTypeValueTable associated with @type.
4154 g_type_value_table_peek (GType type)
4156 GTypeValueTable *vtable = NULL;
4157 TypeNode *node = lookup_type_node_I (type);
4158 gboolean has_refed_data, has_table;
4160 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4161 return node->data->common.value_table;
4163 G_READ_LOCK (&type_rw_lock);
4166 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4167 has_table = has_refed_data && node->data->common.value_table->value_init;
4171 vtable = node->data->common.value_table;
4172 else if (NODE_IS_IFACE (node))
4176 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4178 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4179 TypeNode *prnode = lookup_type_node_I (prtype);
4181 if (prnode->is_instantiatable)
4184 node = lookup_type_node_I (type);
4185 goto restart_table_peek;
4191 G_READ_UNLOCK (&type_rw_lock);
4197 g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
4198 if (!has_refed_data)
4199 g_warning ("can't peek value table for type `%s' which is not currently referenced",
4200 type_descriptive_name_I (type));
4205 G_CONST_RETURN gchar*
4206 g_type_name_from_instance (GTypeInstance *instance)
4209 return "<NULL-instance>";
4211 return g_type_name_from_class (instance->g_class);
4214 G_CONST_RETURN gchar*
4215 g_type_name_from_class (GTypeClass *g_class)
4218 return "<NULL-class>";
4220 return g_type_name (g_class->g_type);
4224 /* --- private api for gboxed.c --- */
4226 _g_type_boxed_copy (GType type, gpointer value)
4228 TypeNode *node = lookup_type_node_I (type);
4230 return node->data->boxed.copy_func (value);
4234 _g_type_boxed_free (GType type, gpointer value)
4236 TypeNode *node = lookup_type_node_I (type);
4238 node->data->boxed.free_func (value);
4242 _g_type_boxed_init (GType type,
4243 GBoxedCopyFunc copy_func,
4244 GBoxedFreeFunc free_func)
4246 TypeNode *node = lookup_type_node_I (type);
4248 node->data->boxed.copy_func = copy_func;
4249 node->data->boxed.free_func = free_func;
4252 /* --- initialization --- */
4254 * g_type_init_with_debug_flags:
4255 * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
4256 * debugging purposes.
4258 * Similar to g_type_init(), but additionally sets debug flags.
4261 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4263 G_LOCK_DEFINE_STATIC (type_init_lock);
4264 const gchar *env_string;
4267 volatile GType votype;
4269 #ifdef G_THREADS_ENABLED
4270 if (!g_thread_get_initialized())
4271 g_thread_init (NULL);
4274 G_LOCK (type_init_lock);
4276 G_WRITE_LOCK (&type_rw_lock);
4278 if (static_quark_type_flags)
4280 G_WRITE_UNLOCK (&type_rw_lock);
4281 G_UNLOCK (type_init_lock);
4285 /* setup GObject library wide debugging flags */
4286 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
4287 env_string = g_getenv ("GOBJECT_DEBUG");
4288 if (env_string != NULL)
4290 static GDebugKey debug_keys[] = {
4291 { "objects", G_TYPE_DEBUG_OBJECTS },
4292 { "signals", G_TYPE_DEBUG_SIGNALS },
4295 _g_type_debug_flags |= g_parse_debug_string (env_string,
4297 sizeof (debug_keys) / sizeof (debug_keys[0]));
4302 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4303 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4304 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4306 /* type qname hash table */
4307 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
4309 /* invalid type G_TYPE_INVALID (0)
4311 static_fundamental_type_nodes[0] = NULL;
4313 /* void type G_TYPE_NONE
4315 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4316 votype = NODE_TYPE (node);
4317 g_assert (votype == G_TYPE_NONE);
4319 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4321 memset (&info, 0, sizeof (info));
4322 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4323 votype = NODE_TYPE (node);
4324 type_data_make_W (node, &info, NULL);
4325 g_assert (votype == G_TYPE_INTERFACE);
4327 G_WRITE_UNLOCK (&type_rw_lock);
4331 /* G_TYPE_TYPE_PLUGIN
4333 votype = g_type_plugin_get_type ();
4335 /* G_TYPE_* value types
4337 g_value_types_init ();
4339 /* G_TYPE_ENUM & G_TYPE_FLAGS
4341 g_enum_types_init ();
4345 g_boxed_type_init ();
4349 g_param_type_init ();
4353 g_object_type_init ();
4355 /* G_TYPE_PARAM_* pspec types
4357 g_param_spec_types_init ();
4359 /* Value Transformations
4361 g_value_transforms_init ();
4367 G_UNLOCK (type_init_lock);
4373 * Prior to any use of the type system, g_type_init() has to be called
4374 * to initialize the type system and assorted other code portions
4375 * (such as the various fundamental type implementations or the signal
4378 * Since version 2.24 this also initializes the thread system
4383 g_type_init_with_debug_flags (0);
4387 * g_type_class_add_private:
4388 * @g_class: class structure for an instantiatable type
4389 * @private_size: size of private structure.
4391 * Registers a private structure for an instantiatable type.
4393 * When an object is allocated, the private structures for
4394 * the type and all of its parent types are allocated
4395 * sequentially in the same memory block as the public
4398 * Note that the accumulated size of the private structures of
4399 * a type and all its parent types cannot excced 64kB.
4401 * This function should be called in the type's class_init() function.
4402 * The private structure can be retrieved using the
4403 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4405 * The following example shows attaching a private structure
4406 * <structname>MyObjectPrivate</structname> to an object
4407 * <structname>MyObject</structname> defined in the standard GObject
4409 * type's class_init() function.
4412 * typedef struct _MyObject MyObject;
4413 * typedef struct _MyObjectPrivate MyObjectPrivate;
4415 * struct _MyObject {
4418 * MyObjectPrivate *priv;
4421 * struct _MyObjectPrivate {
4426 * my_object_class_init (MyObjectClass *klass)
4428 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4432 * my_object_init (MyObject *my_object)
4434 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4440 * my_object_get_some_field (MyObject *my_object)
4442 * MyObjectPrivate *priv = my_object->priv;
4444 * return priv->some_field;
4451 g_type_class_add_private (gpointer g_class,
4454 GType instance_type = ((GTypeClass *)g_class)->g_type;
4455 TypeNode *node = lookup_type_node_I (instance_type);
4458 g_return_if_fail (private_size > 0);
4459 g_return_if_fail (private_size <= 0xffff);
4461 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4463 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4464 type_descriptive_name_I (instance_type));
4468 if (NODE_PARENT_TYPE (node))
4470 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4471 if (node->data->instance.private_size != pnode->data->instance.private_size)
4473 g_warning ("g_type_add_private() called multiple times for the same type");
4478 G_WRITE_LOCK (&type_rw_lock);
4480 offset = ALIGN_STRUCT (node->data->instance.private_size);
4482 g_assert (offset + private_size <= 0xffff);
4484 node->data->instance.private_size = offset + private_size;
4486 G_WRITE_UNLOCK (&type_rw_lock);
4490 g_type_instance_get_private (GTypeInstance *instance,
4493 TypeNode *instance_node;
4494 TypeNode *private_node;
4495 TypeNode *parent_node;
4499 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4501 /* while instances are initialized, their class pointers change,
4502 * so figure the instances real class first
4504 class = instance_real_class_get (instance);
4506 class = instance->g_class;
4508 instance_node = lookup_type_node_I (class->g_type);
4509 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4511 g_warning ("instance of invalid non-instantiatable type `%s'",
4512 type_descriptive_name_I (instance->g_class->g_type));
4516 private_node = lookup_type_node_I (private_type);
4517 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4519 g_warning ("attempt to retrieve private data for invalid type '%s'",
4520 type_descriptive_name_I (private_type));
4524 /* Note that we don't need a read lock, since instance existing
4525 * means that the instance class and all parent classes
4526 * exist, so the node->data, node->data->instance.instance_size,
4527 * and node->data->instance.private_size are not going to be changed.
4528 * for any of the relevant types.
4531 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4533 if (NODE_PARENT_TYPE (private_node))
4535 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4536 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4538 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4540 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4544 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4547 return G_STRUCT_MEMBER_P (instance, offset);
4551 * g_type_add_class_private:
4552 * @class_type: GType of an classed type.
4553 * @private_size: size of private structure.
4555 * Registers a private class structure for a classed type;
4556 * when the class is allocated, the private structures for
4557 * the class and all of its parent types are allocated
4558 * sequentially in the same memory block as the public
4559 * structures. This function should be called in the
4560 * type's get_type() function after the type is registered.
4561 * The private structure can be retrieved using the
4562 * G_TYPE_CLASS_GET_PRIVATE() macro.
4567 g_type_add_class_private (GType class_type,
4570 TypeNode *node = lookup_type_node_I (class_type);
4573 g_return_if_fail (private_size > 0);
4575 if (!node || !node->is_classed || !node->data)
4577 g_warning ("cannot add class private field to invalid type '%s'",
4578 type_descriptive_name_I (class_type));
4582 if (NODE_PARENT_TYPE (node))
4584 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4585 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4587 g_warning ("g_type_add_class_private() called multiple times for the same type");
4592 G_WRITE_LOCK (&type_rw_lock);
4594 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4595 node->data->class.class_private_size = offset + private_size;
4597 G_WRITE_UNLOCK (&type_rw_lock);
4601 g_type_class_get_private (GTypeClass *klass,
4604 TypeNode *class_node;
4605 TypeNode *private_node;
4606 TypeNode *parent_node;
4609 g_return_val_if_fail (klass != NULL, NULL);
4611 class_node = lookup_type_node_I (klass->g_type);
4612 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4614 g_warning ("class of invalid type `%s'",
4615 type_descriptive_name_I (klass->g_type));
4619 private_node = lookup_type_node_I (private_type);
4620 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4622 g_warning ("attempt to retrieve private data for invalid type '%s'",
4623 type_descriptive_name_I (private_type));
4627 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4629 if (NODE_PARENT_TYPE (private_node))
4631 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4632 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4634 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4636 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_class_add_class_private()");
4640 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4643 return G_STRUCT_MEMBER_P (klass, offset);