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.1 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, see <http://www.gnu.org/licenses/>.
24 #include "../glib/gvalgrind.h"
28 #include "gtype-private.h"
29 #include "gtypeplugin.h"
30 #include "gvaluecollector.h"
31 #include "gatomicarray.h"
32 #include "gobject_trace.h"
34 #include "glib-private.h"
35 #include "gconstructor.h"
42 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
47 * @short_description: The GLib Runtime type identification and
49 * @title:Type Information
51 * The GType API is the foundation of the GObject system. It provides the
52 * facilities for registering and managing all fundamental data types,
53 * user-defined object and interface types.
55 * For type creation and registration purposes, all types fall into one of
56 * two categories: static or dynamic. Static types are never loaded or
57 * unloaded at run-time as dynamic types may be. Static types are created
58 * with g_type_register_static() that gets type specific information passed
59 * in via a #GTypeInfo structure.
61 * Dynamic types are created with g_type_register_dynamic() which takes a
62 * #GTypePlugin structure instead. The remaining type information (the
63 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
64 * and the g_type_plugin_*() API.
66 * These registration functions are usually called only once from a
67 * function whose only purpose is to return the type identifier for a
68 * specific class. Once the type (or class or interface) is registered,
69 * it may be instantiated, inherited, or implemented depending on exactly
70 * what sort of type it is.
72 * There is also a third registration function for registering fundamental
73 * types called g_type_register_fundamental() which requires both a #GTypeInfo
74 * structure and a #GTypeFundamentalInfo structure but it is seldom used
75 * since most fundamental types are predefined rather than user-defined.
77 * Type instance and class structs are limited to a total of 64 KiB,
78 * including all parent types. Similarly, type instances' private data
79 * (as created by G_ADD_PRIVATE()) are limited to a total of
80 * 64 KiB. If a type instance needs a large static buffer, allocate it
81 * separately (typically by using #GArray or #GPtrArray) and put a pointer
82 * to the buffer in the structure.
84 * As mentioned in the [GType conventions][gtype-conventions], type names must
85 * be at least three characters long. There is no upper length limit. The first
86 * character must be a letter (a–z or A–Z) or an underscore (‘_’). Subsequent
87 * characters can be letters, numbers or any of ‘-_+’.
91 /* NOTE: some functions (some internal variants and exported ones)
92 * invalidate data portions of the TypeNodes. if external functions/callbacks
93 * are called, pointers to memory maintained by TypeNodes have to be looked up
94 * again. this affects most of the struct TypeNode fields, e.g. ->children or
95 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
96 * not ->supers[]), as all those memory portions can get realloc()ed during
97 * callback invocation.
100 * lock handling issues when calling static functions are indicated by
101 * uppercase letter postfixes, all static functions have to have
102 * one of the below postfixes:
103 * - _I: [Indifferent about locking]
104 * function doesn't care about locks at all
105 * - _U: [Unlocked invocation]
106 * no read or write lock has to be held across function invocation
107 * (locks may be acquired and released during invocation though)
108 * - _L: [Locked invocation]
109 * a write lock or more than 0 read locks have to be held across
110 * function invocation
111 * - _W: [Write-locked invocation]
112 * a write lock has to be held across function invocation
113 * - _Wm: [Write-locked invocation, mutatable]
114 * like _W, but the write lock might be released and reacquired
115 * during invocation, watch your pointers
116 * - _WmREC: [Write-locked invocation, mutatable, recursive]
117 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
121 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_rw_lock_reader_lock (rw_lock); } while (0)
122 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_rw_lock_reader_unlock (rw_lock); } while (0)
123 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_rw_lock_writer_lock (rw_lock); } while (0)
124 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_rw_lock_writer_unlock (rw_lock); } while (0)
126 #define G_READ_LOCK(rw_lock) g_rw_lock_reader_lock (rw_lock)
127 #define G_READ_UNLOCK(rw_lock) g_rw_lock_reader_unlock (rw_lock)
128 #define G_WRITE_LOCK(rw_lock) g_rw_lock_writer_lock (rw_lock)
129 #define G_WRITE_UNLOCK(rw_lock) g_rw_lock_writer_unlock (rw_lock)
131 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
132 static const gchar _action[] = " invalidly modified type "; \
133 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
135 g_error ("%s(%p)%s'%s'", _fname, _arg, _action, _tname); \
137 g_error ("%s()%s'%s'", _fname, _action, _tname); \
139 #define g_assert_type_system_initialized() \
140 g_assert (static_quark_type_flags)
142 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
143 G_TYPE_FLAG_INSTANTIATABLE | \
144 G_TYPE_FLAG_DERIVABLE | \
145 G_TYPE_FLAG_DEEP_DERIVABLE)
146 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
147 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
148 sizeof (gpointer)), \
151 /* The 2*sizeof(size_t) alignment here is borrowed from
152 * GNU libc, so it should be good most everywhere.
153 * It is more conservative than is needed on some 64-bit
154 * platforms, but ia64 does require a 16-byte alignment.
155 * The SIMD extensions for x86 and ppc32 would want a
156 * larger alignment than this, but we don't need to
157 * do better than malloc.
159 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
160 #define ALIGN_STRUCT(offset) \
161 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
164 /* --- typedefs --- */
165 typedef struct _TypeNode TypeNode;
166 typedef struct _CommonData CommonData;
167 typedef struct _BoxedData BoxedData;
168 typedef struct _IFaceData IFaceData;
169 typedef struct _ClassData ClassData;
170 typedef struct _InstanceData InstanceData;
171 typedef union _TypeData TypeData;
172 typedef struct _IFaceEntries IFaceEntries;
173 typedef struct _IFaceEntry IFaceEntry;
174 typedef struct _IFaceHolder IFaceHolder;
177 /* --- prototypes --- */
178 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
179 static void type_add_flags_W (TypeNode *node,
181 static void type_data_make_W (TypeNode *node,
182 const GTypeInfo *info,
183 const GTypeValueTable *value_table);
184 static inline void type_data_ref_Wm (TypeNode *node);
185 static inline void type_data_unref_U (TypeNode *node,
187 static void type_data_last_unref_Wm (TypeNode * node,
189 static inline gpointer type_get_qdata_L (TypeNode *node,
191 static inline void type_set_qdata_W (TypeNode *node,
194 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
195 GType instance_type);
196 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
198 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
200 static gboolean type_node_is_a_L (TypeNode *node,
201 TypeNode *iface_node);
204 /* --- enumeration --- */
206 /* The InitState enumeration is used to track the progress of initializing
207 * both classes and interface vtables. Keeping the state of initialization
208 * is necessary to handle new interfaces being added while we are initializing
209 * the class or other interfaces.
221 /* --- structures --- */
224 guint volatile ref_count;
225 #ifdef G_ENABLE_DEBUG
226 guint volatile instance_count;
229 guint n_children; /* writable with lock */
231 guint n_prerequisites : 9;
232 guint is_classed : 1;
233 guint is_instantiatable : 1;
234 guint mutatable_check_cache : 1; /* combines some common path checks */
235 GType *children; /* writable with lock */
236 TypeData * volatile data;
240 GAtomicArray iface_entries; /* for !iface types */
241 GAtomicArray offsets;
243 GType *prerequisites;
244 GType supers[1]; /* flexible array */
247 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
248 #define MAX_N_SUPERS (255)
249 #define MAX_N_CHILDREN (G_MAXUINT)
250 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
251 #define MAX_N_PREREQUISITES (511)
252 #define NODE_TYPE(node) (node->supers[0])
253 #define NODE_PARENT_TYPE(node) (node->supers[1])
254 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
255 #define NODE_NAME(node) (g_quark_to_string (node->qname))
256 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
257 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
258 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
259 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
260 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
261 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
262 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
263 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
264 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
265 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
266 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
267 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
269 #define NODE_IS_ANCESTOR(ancestor, node) \
270 ((ancestor)->n_supers <= (node)->n_supers && \
271 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
276 GInterfaceInfo *info;
284 GTypeInterface *vtable;
285 InitState init_state;
288 struct _IFaceEntries {
293 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
294 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
298 GTypeValueTable *value_table;
304 GBoxedCopyFunc copy_func;
305 GBoxedFreeFunc free_func;
312 GBaseInitFunc vtable_init_base;
313 GBaseFinalizeFunc vtable_finalize_base;
314 GClassInitFunc dflt_init;
315 GClassFinalizeFunc dflt_finalize;
316 gconstpointer dflt_data;
317 gpointer dflt_vtable;
324 guint16 class_private_size;
325 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
326 GBaseInitFunc class_init_base;
327 GBaseFinalizeFunc class_finalize_base;
328 GClassInitFunc class_init;
329 GClassFinalizeFunc class_finalize;
330 gconstpointer class_data;
338 guint16 class_private_size;
339 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
340 GBaseInitFunc class_init_base;
341 GBaseFinalizeFunc class_finalize_base;
342 GClassInitFunc class_init;
343 GClassFinalizeFunc class_finalize;
344 gconstpointer class_data;
346 guint16 instance_size;
347 guint16 private_size;
349 GInstanceInitFunc instance_init;
358 InstanceData instance;
363 GTypeClassCacheFunc cache_func;
368 GTypeInterfaceCheckFunc check_func;
372 /* --- variables --- */
373 static GRWLock type_rw_lock;
374 static GRecMutex class_init_rec_mutex;
375 static guint static_n_class_cache_funcs = 0;
376 static ClassCacheFunc *static_class_cache_funcs = NULL;
377 static guint static_n_iface_check_funcs = 0;
378 static IFaceCheckFunc *static_iface_check_funcs = NULL;
379 static GQuark static_quark_type_flags = 0;
380 static GQuark static_quark_iface_holder = 0;
381 static GQuark static_quark_dependants_array = 0;
382 static guint type_registration_serial = 0;
384 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
385 GTypeDebugFlags _g_type_debug_flags = 0;
386 G_GNUC_END_IGNORE_DEPRECATIONS
388 /* --- type nodes --- */
389 static GHashTable *static_type_nodes_ht = NULL;
390 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
391 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
393 static inline TypeNode*
394 lookup_type_node_I (GType utype)
396 if (utype > G_TYPE_FUNDAMENTAL_MAX)
397 return (TypeNode*) (utype & ~TYPE_ID_MASK);
399 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
403 * g_type_get_type_registration_serial:
405 * Returns an opaque serial number that represents the state of the set
406 * of registered types. Any time a type is registered this serial changes,
407 * which means you can cache information based on type lookups (such as
408 * g_type_from_name()) and know if the cache is still valid at a later
409 * time by comparing the current serial with the one at the type lookup.
413 * Returns: An unsigned int, representing the state of type registrations
416 g_type_get_type_registration_serial (void)
418 return (guint)g_atomic_int_get ((gint *)&type_registration_serial);
422 type_node_any_new_W (TypeNode *pnode,
426 GTypeFundamentalFlags type_flags)
431 guint i, node_size = 0;
433 n_supers = pnode ? pnode->n_supers + 1 : 0;
436 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
437 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
438 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
439 node = g_malloc0 (node_size);
440 if (!pnode) /* offset fundamental types */
442 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
443 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
449 g_assert ((type & TYPE_ID_MASK) == 0);
451 node->n_supers = n_supers;
454 node->supers[0] = type;
457 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
458 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
460 if (NODE_IS_IFACE (node))
462 IFACE_NODE_N_PREREQUISITES (node) = 0;
463 IFACE_NODE_PREREQUISITES (node) = NULL;
466 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
470 node->supers[0] = type;
471 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
473 node->is_classed = pnode->is_classed;
474 node->is_instantiatable = pnode->is_instantiatable;
476 if (NODE_IS_IFACE (node))
478 IFACE_NODE_N_PREREQUISITES (node) = 0;
479 IFACE_NODE_PREREQUISITES (node) = NULL;
484 IFaceEntries *entries;
486 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
487 IFACE_ENTRIES_HEADER_SIZE,
491 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
493 entries->entry[j].vtable = NULL;
494 entries->entry[j].init_state = UNINITIALIZED;
496 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
501 i = pnode->n_children++;
502 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
503 pnode->children[i] = type;
506 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
508 node->plugin = plugin;
509 node->n_children = 0;
510 node->children = NULL;
512 node->qname = g_quark_from_string (name);
513 node->global_gdata = NULL;
514 g_hash_table_insert (static_type_nodes_ht,
515 (gpointer) g_quark_to_string (node->qname),
518 g_atomic_int_inc ((gint *)&type_registration_serial);
523 static inline GTypeFundamentalInfo*
524 type_node_fundamental_info_I (TypeNode *node)
526 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
528 if (ftype != NODE_TYPE (node))
529 node = lookup_type_node_I (ftype);
531 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
535 type_node_fundamental_new_W (GType ftype,
537 GTypeFundamentalFlags type_flags)
539 GTypeFundamentalInfo *finfo;
542 g_assert ((ftype & TYPE_ID_MASK) == 0);
543 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
545 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
546 static_fundamental_next++;
548 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
550 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
552 finfo = type_node_fundamental_info_I (node);
553 finfo->type_flags = type_flags;
559 type_node_new_W (TypeNode *pnode,
565 g_assert (pnode->n_supers < MAX_N_SUPERS);
566 g_assert (pnode->n_children < MAX_N_CHILDREN);
568 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
571 static inline IFaceEntry*
572 lookup_iface_entry_I (volatile IFaceEntries *entries,
573 TypeNode *iface_node)
584 G_ATOMIC_ARRAY_DO_TRANSACTION
585 (&iface_node->_prot.offsets, guint8,
588 offsets = transaction_data;
589 offset_index = entries->offset_index;
590 if (offsets != NULL &&
591 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
593 index = offsets[offset_index];
596 /* zero means unset, subtract one to get real index */
599 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
601 check = (IFaceEntry *)&entries->entry[index];
602 if (check->iface_type == NODE_TYPE (iface_node))
612 static inline IFaceEntry*
613 type_lookup_iface_entry_L (TypeNode *node,
614 TypeNode *iface_node)
616 if (!NODE_IS_IFACE (iface_node))
619 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
624 static inline gboolean
625 type_lookup_iface_vtable_I (TypeNode *node,
626 TypeNode *iface_node,
627 gpointer *vtable_ptr)
632 if (!NODE_IS_IFACE (iface_node))
639 G_ATOMIC_ARRAY_DO_TRANSACTION
640 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
642 entry = lookup_iface_entry_I (transaction_data, iface_node);
647 *vtable_ptr = entry->vtable;
656 static inline gboolean
657 type_lookup_prerequisite_L (TypeNode *iface,
658 GType prerequisite_type)
660 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
662 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
663 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
670 i = (n_prerequisites + 1) >> 1;
671 check = prerequisites + i;
672 if (prerequisite_type == *check)
674 else if (prerequisite_type > *check)
676 n_prerequisites -= i;
677 prerequisites = check;
679 else /* if (prerequisite_type < *check) */
680 n_prerequisites = i - 1;
682 while (n_prerequisites);
688 type_descriptive_name_I (GType type)
692 TypeNode *node = lookup_type_node_I (type);
694 return node ? NODE_NAME (node) : "<unknown>";
701 /* --- type consistency checks --- */
703 check_plugin_U (GTypePlugin *plugin,
704 gboolean need_complete_type_info,
705 gboolean need_complete_interface_info,
706 const gchar *type_name)
708 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
712 g_warning ("plugin handle for type '%s' is NULL",
716 if (!G_IS_TYPE_PLUGIN (plugin))
718 g_warning ("plugin pointer (%p) for type '%s' is invalid",
722 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
724 g_warning ("plugin for type '%s' has no complete_type_info() implementation",
728 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
730 g_warning ("plugin for type '%s' has no complete_interface_info() implementation",
738 check_type_name_I (const gchar *type_name)
740 static const gchar extra_chars[] = "-_+";
741 const gchar *p = type_name;
744 if (!type_name[0] || !type_name[1] || !type_name[2])
746 g_warning ("type name '%s' is too short", type_name);
749 /* check the first letter */
750 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
751 for (p = type_name + 1; *p; p++)
752 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
753 (p[0] >= 'a' && p[0] <= 'z') ||
754 (p[0] >= '0' && p[0] <= '9') ||
755 strchr (extra_chars, p[0]));
758 g_warning ("type name '%s' contains invalid characters", type_name);
761 if (g_type_from_name (type_name))
763 g_warning ("cannot register existing type '%s'", type_name);
771 check_derivation_I (GType parent_type,
772 const gchar *type_name)
775 GTypeFundamentalInfo* finfo;
777 pnode = lookup_type_node_I (parent_type);
780 g_warning ("cannot derive type '%s' from invalid parent type '%s'",
782 type_descriptive_name_I (parent_type));
785 finfo = type_node_fundamental_info_I (pnode);
786 /* ensure flat derivability */
787 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
789 g_warning ("cannot derive '%s' from non-derivable parent type '%s'",
794 /* ensure deep derivability */
795 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
796 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
798 g_warning ("cannot derive '%s' from non-fundamental parent type '%s'",
808 check_collect_format_I (const gchar *collect_format)
810 const gchar *p = collect_format;
811 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
812 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
813 G_VALUE_COLLECT_POINTER, 0 };
816 if (!strchr (valid_format, *p++))
818 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
822 check_value_table_I (const gchar *type_name,
823 const GTypeValueTable *value_table)
827 else if (value_table->value_init == NULL)
829 if (value_table->value_free || value_table->value_copy ||
830 value_table->value_peek_pointer ||
831 value_table->collect_format || value_table->collect_value ||
832 value_table->lcopy_format || value_table->lcopy_value)
833 g_warning ("cannot handle uninitializable values of type '%s'",
837 else /* value_table->value_init != NULL */
839 if (!value_table->value_free)
842 * g_warning ("missing 'value_free()' for type '%s'", type_name);
846 if (!value_table->value_copy)
848 g_warning ("missing 'value_copy()' for type '%s'", type_name);
851 if ((value_table->collect_format || value_table->collect_value) &&
852 (!value_table->collect_format || !value_table->collect_value))
854 g_warning ("one of 'collect_format' and 'collect_value()' is unspecified for type '%s'",
858 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
860 g_warning ("the '%s' specification for type '%s' is too long or invalid",
865 if ((value_table->lcopy_format || value_table->lcopy_value) &&
866 (!value_table->lcopy_format || !value_table->lcopy_value))
868 g_warning ("one of 'lcopy_format' and 'lcopy_value()' is unspecified for type '%s'",
872 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
874 g_warning ("the '%s' specification for type '%s' is too long or invalid",
884 check_type_info_I (TypeNode *pnode,
886 const gchar *type_name,
887 const GTypeInfo *info)
889 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
890 gboolean is_interface = ftype == G_TYPE_INTERFACE;
892 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
894 /* check instance members */
895 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
896 (info->instance_size || info->n_preallocs || info->instance_init))
899 g_warning ("cannot instantiate '%s', derived from non-instantiatable parent type '%s'",
903 g_warning ("cannot instantiate '%s' as non-instantiatable fundamental",
907 /* check class & interface members */
908 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
909 (info->class_init || info->class_finalize || info->class_data ||
910 info->class_size || info->base_init || info->base_finalize))
913 g_warning ("cannot create class for '%s', derived from non-classed parent type '%s'",
917 g_warning ("cannot create class for '%s' as non-classed fundamental",
921 /* check interface size */
922 if (is_interface && info->class_size < sizeof (GTypeInterface))
924 g_warning ("specified interface size for type '%s' is smaller than 'GTypeInterface' size",
928 /* check class size */
929 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
931 if (info->class_size < sizeof (GTypeClass))
933 g_warning ("specified class size for type '%s' is smaller than 'GTypeClass' size",
937 if (pnode && info->class_size < pnode->data->class.class_size)
939 g_warning ("specified class size for type '%s' is smaller "
940 "than the parent type's '%s' class size",
946 /* check instance size */
947 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
949 if (info->instance_size < sizeof (GTypeInstance))
951 g_warning ("specified instance size for type '%s' is smaller than 'GTypeInstance' size",
955 if (pnode && info->instance_size < pnode->data->instance.instance_size)
957 g_warning ("specified instance size for type '%s' is smaller "
958 "than the parent type's '%s' instance size",
969 find_conforming_child_type_L (TypeNode *pnode,
972 TypeNode *node = NULL;
975 if (type_lookup_iface_entry_L (pnode, iface))
978 for (i = 0; i < pnode->n_children && !node; i++)
979 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
985 check_add_interface_L (GType instance_type,
988 TypeNode *node = lookup_type_node_I (instance_type);
989 TypeNode *iface = lookup_type_node_I (iface_type);
992 GType *prerequisites;
996 if (!node || !node->is_instantiatable)
998 g_warning ("cannot add interfaces to invalid (non-instantiatable) type '%s'",
999 type_descriptive_name_I (instance_type));
1002 if (!iface || !NODE_IS_IFACE (iface))
1004 g_warning ("cannot add invalid (non-interface) type '%s' to type '%s'",
1005 type_descriptive_name_I (iface_type),
1009 if (node->data && node->data->class.class)
1011 g_warning ("attempting to add an interface (%s) to class (%s) after class_init",
1012 NODE_NAME (iface), NODE_NAME (node));
1015 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
1016 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
1018 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
1019 g_warning ("cannot add sub-interface '%s' to type '%s' which does not conform to super-interface '%s'",
1025 /* allow overriding of interface type introduced for parent type */
1026 entry = type_lookup_iface_entry_L (node, iface);
1027 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1029 /* ok, we do conform to this interface already, but the interface vtable was not
1030 * yet initialized, and we just conform to the interface because it got added to
1031 * one of our parents. so we allow overriding of holder info here.
1035 /* check whether one of our children already conforms (or whether the interface
1036 * got added to this node already)
1038 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1041 g_warning ("cannot add interface type '%s' to type '%s', since type '%s' already conforms to interface",
1047 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1048 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1050 tnode = lookup_type_node_I (prerequisites[i]);
1051 if (!type_node_is_a_L (node, tnode))
1053 g_warning ("cannot add interface type '%s' to type '%s' which does not conform to prerequisite '%s'",
1064 check_interface_info_I (TypeNode *iface,
1065 GType instance_type,
1066 const GInterfaceInfo *info)
1068 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1070 g_warning ("interface type '%s' for type '%s' comes without initializer",
1072 type_descriptive_name_I (instance_type));
1079 /* --- type info (type node data) --- */
1081 type_data_make_W (TypeNode *node,
1082 const GTypeInfo *info,
1083 const GTypeValueTable *value_table)
1086 GTypeValueTable *vtable = NULL;
1087 guint vtable_size = 0;
1089 g_assert (node->data == NULL && info != NULL);
1093 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1096 vtable = pnode->data->common.value_table;
1099 static const GTypeValueTable zero_vtable = { NULL, };
1101 value_table = &zero_vtable;
1106 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1107 vtable_size = sizeof (GTypeValueTable);
1108 if (value_table->collect_format)
1109 vtable_size += strlen (value_table->collect_format);
1110 if (value_table->lcopy_format)
1111 vtable_size += strlen (value_table->lcopy_format);
1115 if (node->is_instantiatable) /* careful, is_instantiatable is also is_classed */
1117 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1119 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1121 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1122 data->instance.class_size = info->class_size;
1123 data->instance.class_init_base = info->base_init;
1124 data->instance.class_finalize_base = info->base_finalize;
1125 data->instance.class_init = info->class_init;
1126 data->instance.class_finalize = info->class_finalize;
1127 data->instance.class_data = info->class_data;
1128 data->instance.class = NULL;
1129 data->instance.init_state = UNINITIALIZED;
1130 data->instance.instance_size = info->instance_size;
1131 /* We'll set the final value for data->instance.private size
1132 * after the parent class has been initialized
1134 data->instance.private_size = 0;
1135 data->instance.class_private_size = 0;
1137 data->instance.class_private_size = pnode->data->instance.class_private_size;
1138 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1139 data->instance.instance_init = info->instance_init;
1141 else if (node->is_classed) /* only classed */
1143 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1145 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1147 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1148 data->class.class_size = info->class_size;
1149 data->class.class_init_base = info->base_init;
1150 data->class.class_finalize_base = info->base_finalize;
1151 data->class.class_init = info->class_init;
1152 data->class.class_finalize = info->class_finalize;
1153 data->class.class_data = info->class_data;
1154 data->class.class = NULL;
1155 data->class.class_private_size = 0;
1157 data->class.class_private_size = pnode->data->class.class_private_size;
1158 data->class.init_state = UNINITIALIZED;
1160 else if (NODE_IS_IFACE (node))
1162 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1164 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1165 data->iface.vtable_size = info->class_size;
1166 data->iface.vtable_init_base = info->base_init;
1167 data->iface.vtable_finalize_base = info->base_finalize;
1168 data->iface.dflt_init = info->class_init;
1169 data->iface.dflt_finalize = info->class_finalize;
1170 data->iface.dflt_data = info->class_data;
1171 data->iface.dflt_vtable = NULL;
1173 else if (NODE_IS_BOXED (node))
1175 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1177 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1181 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1183 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1192 /* we allocate the vtable and its strings together with the type data, so
1193 * children can take over their parent's vtable pointer, and we don't
1194 * need to worry freeing it or not when the child data is destroyed
1196 *vtable = *value_table;
1197 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1199 vtable->collect_format = p;
1200 if (value_table->collect_format)
1202 strcat (p, value_table->collect_format);
1203 p += strlen (value_table->collect_format);
1207 vtable->lcopy_format = p;
1208 if (value_table->lcopy_format)
1209 strcat (p, value_table->lcopy_format);
1211 node->data->common.value_table = vtable;
1212 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1213 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1214 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1216 g_assert (node->data->common.value_table != NULL); /* paranoid */
1218 g_atomic_int_set ((int *) &node->ref_count, 1);
1222 type_data_ref_Wm (TypeNode *node)
1226 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1228 GTypeValueTable tmp_value_table;
1230 g_assert (node->plugin != NULL);
1234 type_data_ref_Wm (pnode);
1236 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1239 memset (&tmp_info, 0, sizeof (tmp_info));
1240 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1242 G_WRITE_UNLOCK (&type_rw_lock);
1243 g_type_plugin_use (node->plugin);
1244 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1245 G_WRITE_LOCK (&type_rw_lock);
1247 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1249 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1250 type_data_make_W (node, &tmp_info,
1251 check_value_table_I (NODE_NAME (node),
1252 &tmp_value_table) ? &tmp_value_table : NULL);
1256 g_assert (NODE_REFCOUNT (node) > 0);
1258 g_atomic_int_inc ((int *) &node->ref_count);
1262 static inline gboolean
1263 type_data_ref_U (TypeNode *node)
1268 current = NODE_REFCOUNT (node);
1272 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1278 iface_node_has_available_offset_L (TypeNode *iface_node,
1284 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1285 if (offsets == NULL)
1288 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1291 if (offsets[offset] == 0 ||
1292 offsets[offset] == for_index+1)
1299 find_free_iface_offset_L (IFaceEntries *entries)
1302 TypeNode *iface_node;
1307 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1312 for (i = 0; i < n_entries; i++)
1314 entry = &entries->entry[i];
1315 iface_node = lookup_type_node_I (entry->iface_type);
1317 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1321 while (i != n_entries);
1327 iface_node_set_offset_L (TypeNode *iface_node,
1331 guint8 *offsets, *old_offsets;
1332 int new_size, old_size;
1335 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1336 if (old_offsets == NULL)
1340 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1341 if (offset < old_size &&
1342 old_offsets[offset] == index + 1)
1343 return; /* Already set to this index, return */
1345 new_size = MAX (old_size, offset + 1);
1347 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1348 0, new_size - old_size);
1350 /* Mark new area as unused */
1351 for (i = old_size; i < new_size; i++)
1354 offsets[offset] = index + 1;
1356 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1360 type_node_add_iface_entry_W (TypeNode *node,
1362 IFaceEntry *parent_entry)
1364 IFaceEntries *entries;
1366 TypeNode *iface_node;
1370 g_assert (node->is_instantiatable);
1372 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1373 if (entries != NULL)
1375 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1377 g_assert (num_entries < MAX_N_INTERFACES);
1379 for (i = 0; i < num_entries; i++)
1381 entry = &entries->entry[i];
1382 if (entry->iface_type == iface_type)
1384 /* this can happen in two cases:
1385 * - our parent type already conformed to iface_type and node
1386 * got its own holder info. here, our children already have
1387 * entries and NULL vtables, since this will only work for
1388 * uninitialized classes.
1389 * - an interface type is added to an ancestor after it was
1390 * added to a child type.
1393 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1396 /* sick, interface is added to ancestor *after* child type;
1397 * nothing todo, the entry and our children were already setup correctly
1405 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1406 IFACE_ENTRIES_HEADER_SIZE,
1407 sizeof (IFaceEntry));
1408 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1409 i = num_entries - 1;
1411 entries->offset_index = 0;
1412 entries->entry[i].iface_type = iface_type;
1413 entries->entry[i].vtable = NULL;
1414 entries->entry[i].init_state = UNINITIALIZED;
1418 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1420 entries->entry[i].init_state = INITIALIZED;
1421 entries->entry[i].vtable = parent_entry->vtable;
1425 /* Update offsets in iface */
1426 iface_node = lookup_type_node_I (iface_type);
1428 if (iface_node_has_available_offset_L (iface_node,
1429 entries->offset_index,
1432 iface_node_set_offset_L (iface_node,
1433 entries->offset_index, i);
1437 entries->offset_index =
1438 find_free_iface_offset_L (entries);
1439 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1441 entry = &entries->entry[j];
1443 lookup_type_node_I (entry->iface_type);
1444 iface_node_set_offset_L (iface_node,
1445 entries->offset_index, j);
1449 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1453 for (i = 0; i < node->n_children; i++)
1454 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1459 type_add_interface_Wm (TypeNode *node,
1461 const GInterfaceInfo *info,
1462 GTypePlugin *plugin)
1464 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1468 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1470 iholder->next = iface_node_get_holders_L (iface);
1471 iface_node_set_holders_W (iface, iholder);
1472 iholder->instance_type = NODE_TYPE (node);
1473 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1474 iholder->plugin = plugin;
1476 /* create an iface entry for this type */
1477 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1479 /* if the class is already (partly) initialized, we may need to base
1480 * initialize and/or initialize the new interface.
1484 InitState class_state = node->data->class.init_state;
1486 if (class_state >= BASE_IFACE_INIT)
1487 type_iface_vtable_base_init_Wm (iface, node);
1489 if (class_state >= IFACE_INIT)
1490 type_iface_vtable_iface_init_Wm (iface, node);
1493 /* create iface entries for children of this type */
1494 entry = type_lookup_iface_entry_L (node, iface);
1495 for (i = 0; i < node->n_children; i++)
1496 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1500 type_iface_add_prerequisite_W (TypeNode *iface,
1501 TypeNode *prerequisite_node)
1503 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1504 GType *prerequisites, *dependants;
1505 guint n_dependants, i;
1507 g_assert (NODE_IS_IFACE (iface) &&
1508 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1509 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1511 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1512 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1513 if (prerequisites[i] == prerequisite_type)
1514 return; /* we already have that prerequisiste */
1515 else if (prerequisites[i] > prerequisite_type)
1517 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1518 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1519 IFACE_NODE_PREREQUISITES (iface),
1520 IFACE_NODE_N_PREREQUISITES (iface));
1521 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1522 memmove (prerequisites + i + 1, prerequisites + i,
1523 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1524 prerequisites[i] = prerequisite_type;
1526 /* we want to get notified when prerequisites get added to prerequisite_node */
1527 if (NODE_IS_IFACE (prerequisite_node))
1529 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1530 n_dependants = dependants ? dependants[0] : 0;
1532 dependants = g_renew (GType, dependants, n_dependants + 1);
1533 dependants[n_dependants] = NODE_TYPE (iface);
1534 dependants[0] = n_dependants;
1535 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1538 /* we need to notify all dependants */
1539 dependants = iface_node_get_dependants_array_L (iface);
1540 n_dependants = dependants ? dependants[0] : 0;
1541 for (i = 1; i <= n_dependants; i++)
1542 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1546 * g_type_interface_add_prerequisite:
1547 * @interface_type: #GType value of an interface type
1548 * @prerequisite_type: #GType value of an interface or instantiatable type
1550 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1551 * This means that any type implementing @interface_type must also implement
1552 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1553 * interface derivation (which GType doesn't support). An interface can have
1554 * at most one instantiatable prerequisite type.
1557 g_type_interface_add_prerequisite (GType interface_type,
1558 GType prerequisite_type)
1560 TypeNode *iface, *prerequisite_node;
1561 IFaceHolder *holders;
1563 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1564 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1565 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1567 iface = lookup_type_node_I (interface_type);
1568 prerequisite_node = lookup_type_node_I (prerequisite_type);
1569 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1571 g_warning ("interface type '%s' or prerequisite type '%s' invalid",
1572 type_descriptive_name_I (interface_type),
1573 type_descriptive_name_I (prerequisite_type));
1576 G_WRITE_LOCK (&type_rw_lock);
1577 holders = iface_node_get_holders_L (iface);
1580 G_WRITE_UNLOCK (&type_rw_lock);
1581 g_warning ("unable to add prerequisite '%s' to interface '%s' which is already in use for '%s'",
1582 type_descriptive_name_I (prerequisite_type),
1583 type_descriptive_name_I (interface_type),
1584 type_descriptive_name_I (holders->instance_type));
1587 if (prerequisite_node->is_instantiatable)
1591 /* can have at most one publicly installable instantiatable prerequisite */
1592 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1594 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1596 if (prnode->is_instantiatable)
1598 G_WRITE_UNLOCK (&type_rw_lock);
1599 g_warning ("adding prerequisite '%s' to interface '%s' conflicts with existing prerequisite '%s'",
1600 type_descriptive_name_I (prerequisite_type),
1601 type_descriptive_name_I (interface_type),
1602 type_descriptive_name_I (NODE_TYPE (prnode)));
1607 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1608 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1609 G_WRITE_UNLOCK (&type_rw_lock);
1611 else if (NODE_IS_IFACE (prerequisite_node))
1613 GType *prerequisites;
1616 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1617 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1618 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1619 type_iface_add_prerequisite_W (iface, prerequisite_node);
1620 G_WRITE_UNLOCK (&type_rw_lock);
1624 G_WRITE_UNLOCK (&type_rw_lock);
1625 g_warning ("prerequisite '%s' for interface '%s' is neither instantiatable nor interface",
1626 type_descriptive_name_I (prerequisite_type),
1627 type_descriptive_name_I (interface_type));
1632 * g_type_interface_prerequisites:
1633 * @interface_type: an interface type
1634 * @n_prerequisites: (out) (optional): location to return the number
1635 * of prerequisites, or %NULL
1637 * Returns the prerequisites of an interfaces type.
1641 * Returns: (array length=n_prerequisites) (transfer full): a
1642 * newly-allocated zero-terminated array of #GType containing
1643 * the prerequisites of @interface_type
1646 g_type_interface_prerequisites (GType interface_type,
1647 guint *n_prerequisites)
1651 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1653 iface = lookup_type_node_I (interface_type);
1657 TypeNode *inode = NULL;
1660 G_READ_LOCK (&type_rw_lock);
1661 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1662 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1664 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1665 TypeNode *node = lookup_type_node_I (prerequisite);
1666 if (node->is_instantiatable)
1668 if (!inode || type_node_is_a_L (node, inode))
1672 types[n++] = NODE_TYPE (node);
1675 types[n++] = NODE_TYPE (inode);
1677 if (n_prerequisites)
1678 *n_prerequisites = n;
1679 G_READ_UNLOCK (&type_rw_lock);
1685 if (n_prerequisites)
1686 *n_prerequisites = 0;
1694 type_iface_peek_holder_L (TypeNode *iface,
1695 GType instance_type)
1697 IFaceHolder *iholder;
1699 g_assert (NODE_IS_IFACE (iface));
1701 iholder = iface_node_get_holders_L (iface);
1702 while (iholder && iholder->instance_type != instance_type)
1703 iholder = iholder->next;
1708 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1709 GType instance_type,
1712 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1714 if (iholder && !iholder->info && need_info)
1716 GInterfaceInfo tmp_info;
1718 g_assert (iholder->plugin != NULL);
1720 type_data_ref_Wm (iface);
1722 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1724 memset (&tmp_info, 0, sizeof (tmp_info));
1726 G_WRITE_UNLOCK (&type_rw_lock);
1727 g_type_plugin_use (iholder->plugin);
1728 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1729 G_WRITE_LOCK (&type_rw_lock);
1731 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1733 check_interface_info_I (iface, instance_type, &tmp_info);
1734 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1737 return iholder; /* we don't modify write lock upon returning NULL */
1741 type_iface_blow_holder_info_Wm (TypeNode *iface,
1742 GType instance_type)
1744 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1746 g_assert (NODE_IS_IFACE (iface));
1748 while (iholder->instance_type != instance_type)
1749 iholder = iholder->next;
1751 if (iholder->info && iholder->plugin)
1753 g_free (iholder->info);
1754 iholder->info = NULL;
1756 G_WRITE_UNLOCK (&type_rw_lock);
1757 g_type_plugin_unuse (iholder->plugin);
1758 type_data_unref_U (iface, FALSE);
1759 G_WRITE_LOCK (&type_rw_lock);
1764 * g_type_create_instance: (skip)
1765 * @type: an instantiatable type to create an instance for
1767 * Creates and initializes an instance of @type if @type is valid and
1768 * can be instantiated. The type system only performs basic allocation
1769 * and structure setups for instances: actual instance creation should
1770 * happen through functions supplied by the type's fundamental type
1771 * implementation. So use of g_type_create_instance() is reserved for
1772 * implementers of fundamental types only. E.g. instances of the
1773 * #GObject hierarchy should be created via g_object_new() and never
1774 * directly through g_type_create_instance() which doesn't handle things
1775 * like singleton objects or object construction.
1777 * The extended members of the returned instance are guaranteed to be filled
1780 * Note: Do not use this function, unless you're implementing a
1781 * fundamental type. Also language bindings should not use this
1782 * function, but g_object_new() instead.
1784 * Returns: an allocated and initialized instance, subject to further
1785 * treatment by the fundamental type implementation
1788 g_type_create_instance (GType type)
1791 GTypeInstance *instance;
1798 node = lookup_type_node_I (type);
1799 if (!node || !node->is_instantiatable)
1801 g_error ("cannot create new instance of invalid (non-instantiatable) type '%s'",
1802 type_descriptive_name_I (type));
1804 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1805 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1807 g_error ("cannot create instance of abstract (non-instantiatable) type '%s'",
1808 type_descriptive_name_I (type));
1811 class = g_type_class_ref (type);
1813 /* We allocate the 'private' areas before the normal instance data, in
1814 * reverse order. This allows the private area of a particular class
1815 * to always be at a constant relative address to the instance data.
1816 * If we stored the private data after the instance data this would
1817 * not be the case (since a subclass that added more instance
1818 * variables would push the private data further along).
1820 * This presents problems for valgrindability, of course, so we do a
1821 * workaround for that case. We identify the start of the object to
1822 * valgrind as an allocated block (so that pointers to objects show up
1823 * as 'reachable' instead of 'possibly lost'). We then add an extra
1824 * pointer at the end of the object, after all instance data, back to
1825 * the start of the private area so that it is also recorded as
1826 * reachable. We also add extra private space at the start because
1827 * valgrind doesn't seem to like us claiming to have allocated an
1828 * address that it saw allocated by malloc().
1830 private_size = node->data->instance.private_size;
1831 ivar_size = node->data->instance.instance_size;
1833 #ifdef ENABLE_VALGRIND
1834 if (private_size && RUNNING_ON_VALGRIND)
1836 private_size += ALIGN_STRUCT (1);
1838 /* Allocate one extra pointer size... */
1839 allocated = g_slice_alloc0 (private_size + ivar_size + sizeof (gpointer));
1840 /* ... and point it back to the start of the private data. */
1841 *(gpointer *) (allocated + private_size + ivar_size) = allocated + ALIGN_STRUCT (1);
1843 /* Tell valgrind that it should treat the object itself as such */
1844 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, ivar_size + sizeof (gpointer), 0, TRUE);
1845 VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
1849 allocated = g_slice_alloc0 (private_size + ivar_size);
1851 instance = (GTypeInstance *) (allocated + private_size);
1853 for (i = node->n_supers; i > 0; i--)
1857 pnode = lookup_type_node_I (node->supers[i]);
1858 if (pnode->data->instance.instance_init)
1860 instance->g_class = pnode->data->instance.class;
1861 pnode->data->instance.instance_init (instance, class);
1865 instance->g_class = class;
1866 if (node->data->instance.instance_init)
1867 node->data->instance.instance_init (instance, class);
1869 #ifdef G_ENABLE_DEBUG
1870 IF_DEBUG (INSTANCE_COUNT)
1872 g_atomic_int_inc ((int *) &node->instance_count);
1876 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1882 * g_type_free_instance:
1883 * @instance: an instance of a type
1885 * Frees an instance of a type, returning it to the instance pool for
1886 * the type, if there is one.
1888 * Like g_type_create_instance(), this function is reserved for
1889 * implementors of fundamental types.
1892 g_type_free_instance (GTypeInstance *instance)
1900 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1902 class = instance->g_class;
1903 node = lookup_type_node_I (class->g_type);
1904 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1906 g_warning ("cannot free instance of invalid (non-instantiatable) type '%s'",
1907 type_descriptive_name_I (class->g_type));
1910 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1911 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1913 g_warning ("cannot free instance of abstract (non-instantiatable) type '%s'",
1918 instance->g_class = NULL;
1919 private_size = node->data->instance.private_size;
1920 ivar_size = node->data->instance.instance_size;
1921 allocated = ((gchar *) instance) - private_size;
1923 #ifdef G_ENABLE_DEBUG
1924 memset (allocated, 0xaa, ivar_size + private_size);
1927 #ifdef ENABLE_VALGRIND
1928 /* See comment in g_type_create_instance() about what's going on here.
1929 * We're basically unwinding what we put into motion there.
1931 if (private_size && RUNNING_ON_VALGRIND)
1933 private_size += ALIGN_STRUCT (1);
1934 allocated -= ALIGN_STRUCT (1);
1936 /* Clear out the extra pointer... */
1937 *(gpointer *) (allocated + private_size + ivar_size) = NULL;
1938 /* ... and ensure we include it in the size we free. */
1939 g_slice_free1 (private_size + ivar_size + sizeof (gpointer), allocated);
1941 VALGRIND_FREELIKE_BLOCK (allocated + ALIGN_STRUCT (1), 0);
1942 VALGRIND_FREELIKE_BLOCK (instance, 0);
1946 g_slice_free1 (private_size + ivar_size, allocated);
1948 #ifdef G_ENABLE_DEBUG
1949 IF_DEBUG (INSTANCE_COUNT)
1951 g_atomic_int_add ((int *) &node->instance_count, -1);
1955 g_type_class_unref (class);
1959 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1961 g_assert (iface->data);
1963 if (!iface->data->iface.dflt_vtable)
1965 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1966 iface->data->iface.dflt_vtable = vtable;
1967 vtable->g_type = NODE_TYPE (iface);
1968 vtable->g_instance_type = 0;
1969 if (iface->data->iface.vtable_init_base ||
1970 iface->data->iface.dflt_init)
1972 G_WRITE_UNLOCK (&type_rw_lock);
1973 if (iface->data->iface.vtable_init_base)
1974 iface->data->iface.vtable_init_base (vtable);
1975 if (iface->data->iface.dflt_init)
1976 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1977 G_WRITE_LOCK (&type_rw_lock);
1983 /* This is called to allocate and do the first part of initializing
1984 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1986 * A FALSE return indicates that we didn't find an init function for
1987 * this type/iface pair, so the vtable from the parent type should
1988 * be used. Note that the write lock is not modified upon a FALSE
1992 type_iface_vtable_base_init_Wm (TypeNode *iface,
1996 IFaceHolder *iholder;
1997 GTypeInterface *vtable = NULL;
2000 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2001 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
2003 return FALSE; /* we don't modify write lock upon FALSE */
2005 type_iface_ensure_dflt_vtable_Wm (iface);
2007 entry = type_lookup_iface_entry_L (node, iface);
2009 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
2011 entry->init_state = IFACE_INIT;
2013 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2014 if (pnode) /* want to copy over parent iface contents */
2016 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2019 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2022 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2023 entry->vtable = vtable;
2024 vtable->g_type = NODE_TYPE (iface);
2025 vtable->g_instance_type = NODE_TYPE (node);
2027 if (iface->data->iface.vtable_init_base)
2029 G_WRITE_UNLOCK (&type_rw_lock);
2030 iface->data->iface.vtable_init_base (vtable);
2031 G_WRITE_LOCK (&type_rw_lock);
2033 return TRUE; /* initialized the vtable */
2036 /* Finishes what type_iface_vtable_base_init_Wm started by
2037 * calling the interface init function.
2038 * this function may only be called for types with their
2039 * own interface holder info, i.e. types for which
2040 * g_type_add_interface*() was called and not children thereof.
2043 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2046 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2047 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2048 GTypeInterface *vtable = NULL;
2051 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2052 g_assert (iface->data && entry && iholder && iholder->info);
2053 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2055 entry->init_state = INITIALIZED;
2057 vtable = entry->vtable;
2059 if (iholder->info->interface_init)
2061 G_WRITE_UNLOCK (&type_rw_lock);
2062 if (iholder->info->interface_init)
2063 iholder->info->interface_init (vtable, iholder->info->interface_data);
2064 G_WRITE_LOCK (&type_rw_lock);
2067 for (i = 0; i < static_n_iface_check_funcs; i++)
2069 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2070 gpointer check_data = static_iface_check_funcs[i].check_data;
2072 G_WRITE_UNLOCK (&type_rw_lock);
2073 check_func (check_data, (gpointer)vtable);
2074 G_WRITE_LOCK (&type_rw_lock);
2079 type_iface_vtable_finalize_Wm (TypeNode *iface,
2081 GTypeInterface *vtable)
2083 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2084 IFaceHolder *iholder;
2086 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2087 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2089 return FALSE; /* we don't modify write lock upon FALSE */
2091 g_assert (entry && entry->vtable == vtable && iholder->info);
2093 entry->vtable = NULL;
2094 entry->init_state = UNINITIALIZED;
2095 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2097 G_WRITE_UNLOCK (&type_rw_lock);
2098 if (iholder->info->interface_finalize)
2099 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2100 if (iface->data->iface.vtable_finalize_base)
2101 iface->data->iface.vtable_finalize_base (vtable);
2102 G_WRITE_LOCK (&type_rw_lock);
2105 vtable->g_instance_type = 0;
2108 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2110 return TRUE; /* write lock modified */
2114 type_class_init_Wm (TypeNode *node,
2117 GSList *slist, *init_slist = NULL;
2119 IFaceEntries *entries;
2121 TypeNode *bnode, *pnode;
2124 /* Accessing data->class will work for instantiable types
2125 * too because ClassData is a subset of InstanceData
2127 g_assert (node->is_classed && node->data &&
2128 node->data->class.class_size &&
2129 !node->data->class.class &&
2130 node->data->class.init_state == UNINITIALIZED);
2131 if (node->data->class.class_private_size)
2132 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2134 class = g_malloc0 (node->data->class.class_size);
2135 node->data->class.class = class;
2136 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2140 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2142 memcpy (class, pclass, pnode->data->class.class_size);
2143 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);
2145 if (node->is_instantiatable)
2147 /* We need to initialize the private_size here rather than in
2148 * type_data_make_W() since the class init for the parent
2149 * class may have changed pnode->data->instance.private_size.
2151 node->data->instance.private_size = pnode->data->instance.private_size;
2154 class->g_type = NODE_TYPE (node);
2156 G_WRITE_UNLOCK (&type_rw_lock);
2158 /* stack all base class initialization functions, so we
2159 * call them in ascending order.
2161 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2162 if (bnode->data->class.class_init_base)
2163 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2164 for (slist = init_slist; slist; slist = slist->next)
2166 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2168 class_init_base (class);
2170 g_slist_free (init_slist);
2172 G_WRITE_LOCK (&type_rw_lock);
2174 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2176 /* Before we initialize the class, base initialize all interfaces, either
2177 * from parent, or through our holder info
2179 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2182 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2183 i < IFACE_ENTRIES_N_ENTRIES (entries))
2185 entry = &entries->entry[i];
2186 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2187 entry->init_state == IFACE_INIT)
2193 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2196 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2199 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2201 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2202 * doesn't modify write lock upon FALSE, so entry is still valid;
2204 g_assert (pnode != NULL);
2207 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2209 IFaceEntry *pentry = &pentries->entry[j];
2211 if (pentry->iface_type == entry->iface_type)
2213 entry->vtable = pentry->vtable;
2214 entry->init_state = INITIALIZED;
2218 g_assert (entry->vtable != NULL);
2221 /* If the write lock was released, additional interface entries might
2222 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2223 * be base-initialized when inserted, so we don't have to worry that
2224 * we might miss them. Uninitialized entries can only be moved higher
2225 * when new ones are inserted.
2230 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2232 G_WRITE_UNLOCK (&type_rw_lock);
2234 if (node->data->class.class_init)
2235 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2237 G_WRITE_LOCK (&type_rw_lock);
2239 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2241 /* finish initializing the interfaces through our holder info.
2242 * inherited interfaces are already init_state == INITIALIZED, because
2243 * they either got setup in the above base_init loop, or during
2244 * class_init from within type_add_interface_Wm() for this or
2245 * an anchestor type.
2248 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2250 entry = &entries->entry[i];
2251 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2252 entry->init_state == INITIALIZED)
2258 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2261 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2263 /* As in the loop above, additional initialized entries might be inserted
2264 * if the write lock is released, but that's harmless because the entries
2265 * we need to initialize only move higher in the list.
2270 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2274 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2277 IFaceEntries *entries;
2279 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2282 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2283 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2285 IFaceEntry *entry = &entries->entry[i];
2288 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2290 /* refetch entries, IFACES_ENTRIES might be modified */
2295 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2296 * iface vtable came from parent
2298 entry->vtable = NULL;
2299 entry->init_state = UNINITIALIZED;
2306 type_data_finalize_class_U (TypeNode *node,
2309 GTypeClass *class = cdata->class;
2312 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2314 if (cdata->class_finalize)
2315 cdata->class_finalize (class, (gpointer) cdata->class_data);
2317 /* call all base class destruction functions in descending order
2319 if (cdata->class_finalize_base)
2320 cdata->class_finalize_base (class);
2321 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2322 if (bnode->data->class.class_finalize_base)
2323 bnode->data->class.class_finalize_base (class);
2325 g_free (cdata->class);
2329 type_data_last_unref_Wm (TypeNode *node,
2332 g_return_if_fail (node != NULL && node->plugin != NULL);
2334 if (!node->data || NODE_REFCOUNT (node) == 0)
2336 g_warning ("cannot drop last reference to unreferenced type '%s'",
2341 /* call class cache hooks */
2342 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2346 G_WRITE_UNLOCK (&type_rw_lock);
2347 G_READ_LOCK (&type_rw_lock);
2348 for (i = 0; i < static_n_class_cache_funcs; i++)
2350 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2351 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2352 gboolean need_break;
2354 G_READ_UNLOCK (&type_rw_lock);
2355 need_break = cache_func (cache_data, node->data->class.class);
2356 G_READ_LOCK (&type_rw_lock);
2357 if (!node->data || NODE_REFCOUNT (node) == 0)
2358 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2362 G_READ_UNLOCK (&type_rw_lock);
2363 G_WRITE_LOCK (&type_rw_lock);
2366 /* may have been re-referenced meanwhile */
2367 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2369 GType ptype = NODE_PARENT_TYPE (node);
2372 if (node->is_instantiatable)
2374 /* destroy node->data->instance.mem_chunk */
2378 if (node->is_classed && tdata->class.class)
2380 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2381 type_data_finalize_class_ifaces_Wm (node);
2382 node->mutatable_check_cache = FALSE;
2384 G_WRITE_UNLOCK (&type_rw_lock);
2385 type_data_finalize_class_U (node, &tdata->class);
2386 G_WRITE_LOCK (&type_rw_lock);
2388 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2390 node->mutatable_check_cache = FALSE;
2392 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2394 G_WRITE_UNLOCK (&type_rw_lock);
2395 if (tdata->iface.dflt_finalize)
2396 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2397 if (tdata->iface.vtable_finalize_base)
2398 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2399 G_WRITE_LOCK (&type_rw_lock);
2401 g_free (tdata->iface.dflt_vtable);
2405 node->mutatable_check_cache = FALSE;
2409 /* freeing tdata->common.value_table and its contents is taken care of
2410 * by allocating it in one chunk with tdata
2414 G_WRITE_UNLOCK (&type_rw_lock);
2415 g_type_plugin_unuse (node->plugin);
2417 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2418 G_WRITE_LOCK (&type_rw_lock);
2423 type_data_unref_U (TypeNode *node,
2429 current = NODE_REFCOUNT (node);
2435 g_warning ("static type '%s' unreferenced too often",
2441 /* This is the last reference of a type from a plugin. We are
2442 * experimentally disabling support for unloading type
2443 * plugins, so don't allow the last ref to drop.
2448 g_assert (current > 0);
2450 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2451 G_WRITE_LOCK (&type_rw_lock);
2452 type_data_last_unref_Wm (node, uncached);
2453 G_WRITE_UNLOCK (&type_rw_lock);
2454 g_rec_mutex_unlock (&class_init_rec_mutex);
2457 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2461 * g_type_add_class_cache_func: (skip)
2462 * @cache_data: data to be passed to @cache_func
2463 * @cache_func: a #GTypeClassCacheFunc
2465 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2466 * class goes from one to zero. This can be used to prevent premature class
2467 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2468 * until one of them returns %TRUE. The functions have to check the class id
2469 * passed in to figure whether they actually want to cache the class of this
2470 * type, since all classes are routed through the same #GTypeClassCacheFunc
2474 g_type_add_class_cache_func (gpointer cache_data,
2475 GTypeClassCacheFunc cache_func)
2479 g_return_if_fail (cache_func != NULL);
2481 G_WRITE_LOCK (&type_rw_lock);
2482 i = static_n_class_cache_funcs++;
2483 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2484 static_class_cache_funcs[i].cache_data = cache_data;
2485 static_class_cache_funcs[i].cache_func = cache_func;
2486 G_WRITE_UNLOCK (&type_rw_lock);
2490 * g_type_remove_class_cache_func: (skip)
2491 * @cache_data: data that was given when adding @cache_func
2492 * @cache_func: a #GTypeClassCacheFunc
2494 * Removes a previously installed #GTypeClassCacheFunc. The cache
2495 * maintained by @cache_func has to be empty when calling
2496 * g_type_remove_class_cache_func() to avoid leaks.
2499 g_type_remove_class_cache_func (gpointer cache_data,
2500 GTypeClassCacheFunc cache_func)
2502 gboolean found_it = FALSE;
2505 g_return_if_fail (cache_func != NULL);
2507 G_WRITE_LOCK (&type_rw_lock);
2508 for (i = 0; i < static_n_class_cache_funcs; i++)
2509 if (static_class_cache_funcs[i].cache_data == cache_data &&
2510 static_class_cache_funcs[i].cache_func == cache_func)
2512 static_n_class_cache_funcs--;
2513 memmove (static_class_cache_funcs + i,
2514 static_class_cache_funcs + i + 1,
2515 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2516 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2520 G_WRITE_UNLOCK (&type_rw_lock);
2523 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2524 cache_func, cache_data);
2529 * g_type_add_interface_check: (skip)
2530 * @check_data: data to pass to @check_func
2531 * @check_func: function to be called after each interface
2534 * Adds a function to be called after an interface vtable is
2535 * initialized for any class (i.e. after the @interface_init
2536 * member of #GInterfaceInfo has been called).
2538 * This function is useful when you want to check an invariant
2539 * that depends on the interfaces of a class. For instance, the
2540 * implementation of #GObject uses this facility to check that an
2541 * object implements all of the properties that are defined on its
2547 g_type_add_interface_check (gpointer check_data,
2548 GTypeInterfaceCheckFunc check_func)
2552 g_return_if_fail (check_func != NULL);
2554 G_WRITE_LOCK (&type_rw_lock);
2555 i = static_n_iface_check_funcs++;
2556 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2557 static_iface_check_funcs[i].check_data = check_data;
2558 static_iface_check_funcs[i].check_func = check_func;
2559 G_WRITE_UNLOCK (&type_rw_lock);
2563 * g_type_remove_interface_check: (skip)
2564 * @check_data: callback data passed to g_type_add_interface_check()
2565 * @check_func: callback function passed to g_type_add_interface_check()
2567 * Removes an interface check function added with
2568 * g_type_add_interface_check().
2573 g_type_remove_interface_check (gpointer check_data,
2574 GTypeInterfaceCheckFunc check_func)
2576 gboolean found_it = FALSE;
2579 g_return_if_fail (check_func != NULL);
2581 G_WRITE_LOCK (&type_rw_lock);
2582 for (i = 0; i < static_n_iface_check_funcs; i++)
2583 if (static_iface_check_funcs[i].check_data == check_data &&
2584 static_iface_check_funcs[i].check_func == check_func)
2586 static_n_iface_check_funcs--;
2587 memmove (static_iface_check_funcs + i,
2588 static_iface_check_funcs + i + 1,
2589 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2590 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2594 G_WRITE_UNLOCK (&type_rw_lock);
2597 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2598 check_func, check_data);
2601 /* --- type registration --- */
2603 * g_type_register_fundamental:
2604 * @type_id: a predefined type identifier
2605 * @type_name: 0-terminated string used as the name of the new type
2606 * @info: #GTypeInfo structure for this type
2607 * @finfo: #GTypeFundamentalInfo structure for this type
2608 * @flags: bitwise combination of #GTypeFlags values
2610 * Registers @type_id as the predefined identifier and @type_name as the
2611 * name of a fundamental type. If @type_id is already registered, or a
2612 * type named @type_name is already registered, the behaviour is undefined.
2613 * The type system uses the information contained in the #GTypeInfo structure
2614 * pointed to by @info and the #GTypeFundamentalInfo structure pointed to by
2615 * @finfo to manage the type and its instances. The value of @flags determines
2616 * additional characteristics of the fundamental type.
2618 * Returns: the predefined type identifier
2621 g_type_register_fundamental (GType type_id,
2622 const gchar *type_name,
2623 const GTypeInfo *info,
2624 const GTypeFundamentalInfo *finfo,
2629 g_assert_type_system_initialized ();
2630 g_return_val_if_fail (type_id > 0, 0);
2631 g_return_val_if_fail (type_name != NULL, 0);
2632 g_return_val_if_fail (info != NULL, 0);
2633 g_return_val_if_fail (finfo != NULL, 0);
2635 if (!check_type_name_I (type_name))
2637 if ((type_id & TYPE_ID_MASK) ||
2638 type_id > G_TYPE_FUNDAMENTAL_MAX)
2640 g_warning ("attempt to register fundamental type '%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2645 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2646 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2648 g_warning ("cannot register instantiatable fundamental type '%s' as non-classed",
2652 if (lookup_type_node_I (type_id))
2654 g_warning ("cannot register existing fundamental type '%s' (as '%s')",
2655 type_descriptive_name_I (type_id),
2660 G_WRITE_LOCK (&type_rw_lock);
2661 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2662 type_add_flags_W (node, flags);
2664 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2665 type_data_make_W (node, info,
2666 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2667 G_WRITE_UNLOCK (&type_rw_lock);
2669 return NODE_TYPE (node);
2673 * g_type_register_static_simple: (skip)
2674 * @parent_type: type from which this type will be derived
2675 * @type_name: 0-terminated string used as the name of the new type
2676 * @class_size: size of the class structure (see #GTypeInfo)
2677 * @class_init: location of the class initialization function (see #GTypeInfo)
2678 * @instance_size: size of the instance structure (see #GTypeInfo)
2679 * @instance_init: location of the instance initialization function (see #GTypeInfo)
2680 * @flags: bitwise combination of #GTypeFlags values
2682 * Registers @type_name as the name of a new static type derived from
2683 * @parent_type. The value of @flags determines the nature (e.g.
2684 * abstract or not) of the type. It works by filling a #GTypeInfo
2685 * struct and calling g_type_register_static().
2689 * Returns: the new type identifier
2692 g_type_register_static_simple (GType parent_type,
2693 const gchar *type_name,
2695 GClassInitFunc class_init,
2696 guint instance_size,
2697 GInstanceInitFunc instance_init,
2702 /* Instances are not allowed to be larger than this. If you have a big
2703 * fixed-length array or something, point to it instead.
2705 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
2706 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
2708 info.class_size = class_size;
2709 info.base_init = NULL;
2710 info.base_finalize = NULL;
2711 info.class_init = class_init;
2712 info.class_finalize = NULL;
2713 info.class_data = NULL;
2714 info.instance_size = instance_size;
2715 info.n_preallocs = 0;
2716 info.instance_init = instance_init;
2717 info.value_table = NULL;
2719 return g_type_register_static (parent_type, type_name, &info, flags);
2723 * g_type_register_static:
2724 * @parent_type: type from which this type will be derived
2725 * @type_name: 0-terminated string used as the name of the new type
2726 * @info: #GTypeInfo structure for this type
2727 * @flags: bitwise combination of #GTypeFlags values
2729 * Registers @type_name as the name of a new static type derived from
2730 * @parent_type. The type system uses the information contained in the
2731 * #GTypeInfo structure pointed to by @info to manage the type and its
2732 * instances (if not abstract). The value of @flags determines the nature
2733 * (e.g. abstract or not) of the type.
2735 * Returns: the new type identifier
2738 g_type_register_static (GType parent_type,
2739 const gchar *type_name,
2740 const GTypeInfo *info,
2743 TypeNode *pnode, *node;
2746 g_assert_type_system_initialized ();
2747 g_return_val_if_fail (parent_type > 0, 0);
2748 g_return_val_if_fail (type_name != NULL, 0);
2749 g_return_val_if_fail (info != NULL, 0);
2751 if (!check_type_name_I (type_name) ||
2752 !check_derivation_I (parent_type, type_name))
2754 if (info->class_finalize)
2756 g_warning ("class finalizer specified for static type '%s'",
2761 pnode = lookup_type_node_I (parent_type);
2762 G_WRITE_LOCK (&type_rw_lock);
2763 type_data_ref_Wm (pnode);
2764 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2766 node = type_node_new_W (pnode, type_name, NULL);
2767 type_add_flags_W (node, flags);
2768 type = NODE_TYPE (node);
2769 type_data_make_W (node, info,
2770 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2772 G_WRITE_UNLOCK (&type_rw_lock);
2778 * g_type_register_dynamic:
2779 * @parent_type: type from which this type will be derived
2780 * @type_name: 0-terminated string used as the name of the new type
2781 * @plugin: #GTypePlugin structure to retrieve the #GTypeInfo from
2782 * @flags: bitwise combination of #GTypeFlags values
2784 * Registers @type_name as the name of a new dynamic type derived from
2785 * @parent_type. The type system uses the information contained in the
2786 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2787 * instances (if not abstract). The value of @flags determines the nature
2788 * (e.g. abstract or not) of the type.
2790 * Returns: the new type identifier or #G_TYPE_INVALID if registration failed
2793 g_type_register_dynamic (GType parent_type,
2794 const gchar *type_name,
2795 GTypePlugin *plugin,
2798 TypeNode *pnode, *node;
2801 g_assert_type_system_initialized ();
2802 g_return_val_if_fail (parent_type > 0, 0);
2803 g_return_val_if_fail (type_name != NULL, 0);
2804 g_return_val_if_fail (plugin != NULL, 0);
2806 if (!check_type_name_I (type_name) ||
2807 !check_derivation_I (parent_type, type_name) ||
2808 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2811 G_WRITE_LOCK (&type_rw_lock);
2812 pnode = lookup_type_node_I (parent_type);
2813 node = type_node_new_W (pnode, type_name, plugin);
2814 type_add_flags_W (node, flags);
2815 type = NODE_TYPE (node);
2816 G_WRITE_UNLOCK (&type_rw_lock);
2822 * g_type_add_interface_static:
2823 * @instance_type: #GType value of an instantiable type
2824 * @interface_type: #GType value of an interface type
2825 * @info: #GInterfaceInfo structure for this
2826 * (@instance_type, @interface_type) combination
2828 * Adds @interface_type to the static @instantiable_type.
2829 * The information contained in the #GInterfaceInfo structure
2830 * pointed to by @info is used to manage the relationship.
2833 g_type_add_interface_static (GType instance_type,
2834 GType interface_type,
2835 const GInterfaceInfo *info)
2837 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2838 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2839 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2841 /* we only need to lock class_init_rec_mutex if instance_type already has its
2842 * class initialized, however this function is rarely enough called to take
2843 * the simple route and always acquire class_init_rec_mutex.
2845 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2846 G_WRITE_LOCK (&type_rw_lock);
2847 if (check_add_interface_L (instance_type, interface_type))
2849 TypeNode *node = lookup_type_node_I (instance_type);
2850 TypeNode *iface = lookup_type_node_I (interface_type);
2851 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2852 type_add_interface_Wm (node, iface, info, NULL);
2854 G_WRITE_UNLOCK (&type_rw_lock);
2855 g_rec_mutex_unlock (&class_init_rec_mutex);
2859 * g_type_add_interface_dynamic:
2860 * @instance_type: #GType value of an instantiable type
2861 * @interface_type: #GType value of an interface type
2862 * @plugin: #GTypePlugin structure to retrieve the #GInterfaceInfo from
2864 * Adds @interface_type to the dynamic @instantiable_type. The information
2865 * contained in the #GTypePlugin structure pointed to by @plugin
2866 * is used to manage the relationship.
2869 g_type_add_interface_dynamic (GType instance_type,
2870 GType interface_type,
2871 GTypePlugin *plugin)
2874 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2875 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2876 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2878 node = lookup_type_node_I (instance_type);
2879 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2882 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2883 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2884 G_WRITE_LOCK (&type_rw_lock);
2885 if (check_add_interface_L (instance_type, interface_type))
2887 TypeNode *iface = lookup_type_node_I (interface_type);
2888 type_add_interface_Wm (node, iface, NULL, plugin);
2890 G_WRITE_UNLOCK (&type_rw_lock);
2891 g_rec_mutex_unlock (&class_init_rec_mutex);
2895 /* --- public API functions --- */
2898 * @type: type ID of a classed type
2900 * Increments the reference count of the class structure belonging to
2901 * @type. This function will demand-create the class if it doesn't
2904 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
2905 * structure for the given type ID
2908 g_type_class_ref (GType type)
2915 /* optimize for common code path */
2916 node = lookup_type_node_I (type);
2917 if (!node || !node->is_classed)
2919 g_warning ("cannot retrieve class for invalid (unclassed) type '%s'",
2920 type_descriptive_name_I (type));
2924 if (G_LIKELY (type_data_ref_U (node)))
2926 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2927 return node->data->class.class;
2933 /* here, we either have node->data->class.class == NULL, or a recursive
2934 * call to g_type_class_ref() with a partly initialized class, or
2935 * node->data->class.init_state == INITIALIZED, because any
2936 * concurrently running initialization was guarded by class_init_rec_mutex.
2938 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2940 /* we need an initialized parent class for initializing derived classes */
2941 ptype = NODE_PARENT_TYPE (node);
2942 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2944 G_WRITE_LOCK (&type_rw_lock);
2947 type_data_ref_Wm (node);
2949 if (!node->data->class.class) /* class uninitialized */
2950 type_class_init_Wm (node, pclass);
2952 G_WRITE_UNLOCK (&type_rw_lock);
2955 g_type_class_unref (pclass);
2957 g_rec_mutex_unlock (&class_init_rec_mutex);
2959 return node->data->class.class;
2963 * g_type_class_unref:
2964 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2966 * Decrements the reference count of the class structure being passed in.
2967 * Once the last reference count of a class has been released, classes
2968 * may be finalized by the type system, so further dereferencing of a
2969 * class pointer after g_type_class_unref() are invalid.
2972 g_type_class_unref (gpointer g_class)
2975 GTypeClass *class = g_class;
2977 g_return_if_fail (g_class != NULL);
2979 node = lookup_type_node_I (class->g_type);
2980 if (node && node->is_classed && NODE_REFCOUNT (node))
2981 type_data_unref_U (node, FALSE);
2983 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
2984 type_descriptive_name_I (class->g_type));
2988 * g_type_class_unref_uncached: (skip)
2989 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2991 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2992 * implementations. It unreferences a class without consulting the chain
2993 * of #GTypeClassCacheFuncs, avoiding the recursion which would occur
2997 g_type_class_unref_uncached (gpointer g_class)
3000 GTypeClass *class = g_class;
3002 g_return_if_fail (g_class != NULL);
3004 node = lookup_type_node_I (class->g_type);
3005 if (node && node->is_classed && NODE_REFCOUNT (node))
3006 type_data_unref_U (node, TRUE);
3008 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
3009 type_descriptive_name_I (class->g_type));
3013 * g_type_class_peek:
3014 * @type: type ID of a classed type
3016 * This function is essentially the same as g_type_class_ref(),
3017 * except that the classes reference count isn't incremented.
3018 * As a consequence, this function may return %NULL if the class
3019 * of the type passed in does not currently exist (hasn't been
3020 * referenced before).
3022 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3023 * structure for the given type ID or %NULL if the class does not
3027 g_type_class_peek (GType type)
3032 node = lookup_type_node_I (type);
3033 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3034 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3035 /* ref_count _may_ be 0 */
3036 class = node->data->class.class;
3044 * g_type_class_peek_static:
3045 * @type: type ID of a classed type
3047 * A more efficient version of g_type_class_peek() which works only for
3050 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3051 * structure for the given type ID or %NULL if the class does not
3052 * currently exist or is dynamically loaded
3057 g_type_class_peek_static (GType type)
3062 node = lookup_type_node_I (type);
3063 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3064 /* peek only static types: */ node->plugin == NULL &&
3065 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3066 /* ref_count _may_ be 0 */
3067 class = node->data->class.class;
3075 * g_type_class_peek_parent:
3076 * @g_class: (type GObject.TypeClass): the #GTypeClass structure to
3077 * retrieve the parent class for
3079 * This is a convenience function often needed in class initializers.
3080 * It returns the class structure of the immediate parent type of the
3081 * class passed in. Since derived classes hold a reference count on
3082 * their parent classes as long as they are instantiated, the returned
3083 * class will always exist.
3085 * This function is essentially equivalent to:
3086 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)))
3088 * Returns: (type GObject.TypeClass) (transfer none): the parent class
3092 g_type_class_peek_parent (gpointer g_class)
3095 gpointer class = NULL;
3097 g_return_val_if_fail (g_class != NULL, NULL);
3099 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3100 /* We used to acquire a read lock here. That is not necessary, since
3101 * parent->data->class.class is constant as long as the derived class
3104 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3106 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3107 class = node->data->class.class;
3109 else if (NODE_PARENT_TYPE (node))
3110 g_warning (G_STRLOC ": invalid class pointer '%p'", g_class);
3116 * g_type_interface_peek:
3117 * @instance_class: (type GObject.TypeClass): a #GTypeClass structure
3118 * @iface_type: an interface ID which this class conforms to
3120 * Returns the #GTypeInterface structure of an interface to which the
3121 * passed in class conforms.
3123 * Returns: (type GObject.TypeInterface) (transfer none): the #GTypeInterface
3124 * structure of @iface_type if implemented by @instance_class, %NULL
3128 g_type_interface_peek (gpointer instance_class,
3133 gpointer vtable = NULL;
3134 GTypeClass *class = instance_class;
3136 g_return_val_if_fail (instance_class != NULL, NULL);
3138 node = lookup_type_node_I (class->g_type);
3139 iface = lookup_type_node_I (iface_type);
3140 if (node && node->is_instantiatable && iface)
3141 type_lookup_iface_vtable_I (node, iface, &vtable);
3143 g_warning (G_STRLOC ": invalid class pointer '%p'", class);
3149 * g_type_interface_peek_parent:
3150 * @g_iface: (type GObject.TypeInterface): a #GTypeInterface structure
3152 * Returns the corresponding #GTypeInterface structure of the parent type
3153 * of the instance type to which @g_iface belongs. This is useful when
3154 * deriving the implementation of an interface from the parent type and
3155 * then possibly overriding some methods.
3157 * Returns: (transfer none) (type GObject.TypeInterface): the
3158 * corresponding #GTypeInterface structure of the parent type of the
3159 * instance type to which @g_iface belongs, or %NULL if the parent
3160 * type doesn't conform to the interface
3163 g_type_interface_peek_parent (gpointer g_iface)
3167 gpointer vtable = NULL;
3168 GTypeInterface *iface_class = g_iface;
3170 g_return_val_if_fail (g_iface != NULL, NULL);
3172 iface = lookup_type_node_I (iface_class->g_type);
3173 node = lookup_type_node_I (iface_class->g_instance_type);
3175 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3176 if (node && node->is_instantiatable && iface)
3177 type_lookup_iface_vtable_I (node, iface, &vtable);
3179 g_warning (G_STRLOC ": invalid interface pointer '%p'", g_iface);
3185 * g_type_default_interface_ref:
3186 * @g_type: an interface type
3188 * Increments the reference count for the interface type @g_type,
3189 * and returns the default interface vtable for the type.
3191 * If the type is not currently in use, then the default vtable
3192 * for the type will be created and initialized by calling
3193 * the base interface init and default vtable init functions for
3194 * the type (the @base_init and @class_init members of #GTypeInfo).
3195 * Calling g_type_default_interface_ref() is useful when you
3196 * want to make sure that signals and properties for an interface
3197 * have been installed.
3201 * Returns: (type GObject.TypeInterface) (transfer none): the default
3202 * vtable for the interface; call g_type_default_interface_unref()
3203 * when you are done using the interface.
3206 g_type_default_interface_ref (GType g_type)
3209 gpointer dflt_vtable;
3211 G_WRITE_LOCK (&type_rw_lock);
3213 node = lookup_type_node_I (g_type);
3214 if (!node || !NODE_IS_IFACE (node) ||
3215 (node->data && NODE_REFCOUNT (node) == 0))
3217 G_WRITE_UNLOCK (&type_rw_lock);
3218 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3219 type_descriptive_name_I (g_type));
3223 if (!node->data || !node->data->iface.dflt_vtable)
3225 G_WRITE_UNLOCK (&type_rw_lock);
3226 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3227 G_WRITE_LOCK (&type_rw_lock);
3228 node = lookup_type_node_I (g_type);
3229 type_data_ref_Wm (node);
3230 type_iface_ensure_dflt_vtable_Wm (node);
3231 g_rec_mutex_unlock (&class_init_rec_mutex);
3234 type_data_ref_Wm (node); /* ref_count >= 1 already */
3236 dflt_vtable = node->data->iface.dflt_vtable;
3237 G_WRITE_UNLOCK (&type_rw_lock);
3243 * g_type_default_interface_peek:
3244 * @g_type: an interface type
3246 * If the interface type @g_type is currently in use, returns its
3247 * default interface vtable.
3251 * Returns: (type GObject.TypeInterface) (transfer none): the default
3252 * vtable for the interface, or %NULL if the type is not currently
3256 g_type_default_interface_peek (GType g_type)
3261 node = lookup_type_node_I (g_type);
3262 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3263 vtable = node->data->iface.dflt_vtable;
3271 * g_type_default_interface_unref:
3272 * @g_iface: (type GObject.TypeInterface): the default vtable
3273 * structure for an interface, as returned by g_type_default_interface_ref()
3275 * Decrements the reference count for the type corresponding to the
3276 * interface default vtable @g_iface. If the type is dynamic, then
3277 * when no one is using the interface and all references have
3278 * been released, the finalize function for the interface's default
3279 * vtable (the @class_finalize member of #GTypeInfo) will be called.
3284 g_type_default_interface_unref (gpointer g_iface)
3287 GTypeInterface *vtable = g_iface;
3289 g_return_if_fail (g_iface != NULL);
3291 node = lookup_type_node_I (vtable->g_type);
3292 if (node && NODE_IS_IFACE (node))
3293 type_data_unref_U (node, FALSE);
3295 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3296 type_descriptive_name_I (vtable->g_type));
3301 * @type: type to return name for
3303 * Get the unique name that is assigned to a type ID. Note that this
3304 * function (like all other GType API) cannot cope with invalid type
3305 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3306 * other validly registered type ID, but randomized type IDs should
3307 * not be passed in and will most likely lead to a crash.
3309 * Returns: static type name or %NULL
3312 g_type_name (GType type)
3316 g_assert_type_system_initialized ();
3318 node = lookup_type_node_I (type);
3320 return node ? NODE_NAME (node) : NULL;
3325 * @type: type to return quark of type name for
3327 * Get the corresponding quark of the type IDs name.
3329 * Returns: the type names quark or 0
3332 g_type_qname (GType type)
3336 node = lookup_type_node_I (type);
3338 return node ? node->qname : 0;
3343 * @name: type name to look up
3345 * Look up the type ID from a given type name, returning 0 if no type
3346 * has been registered under this name (this is the preferred method
3347 * to find out by name whether a specific type has been registered
3350 * Returns: corresponding type ID or 0
3353 g_type_from_name (const gchar *name)
3357 g_return_val_if_fail (name != NULL, 0);
3359 G_READ_LOCK (&type_rw_lock);
3360 type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
3361 G_READ_UNLOCK (&type_rw_lock);
3368 * @type: the derived type
3370 * Return the direct parent type of the passed in type. If the passed
3371 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3373 * Returns: the parent type
3376 g_type_parent (GType type)
3380 node = lookup_type_node_I (type);
3382 return node ? NODE_PARENT_TYPE (node) : 0;
3389 * Returns the length of the ancestry of the passed in type. This
3390 * includes the type itself, so that e.g. a fundamental type has depth 1.
3392 * Returns: the depth of @type
3395 g_type_depth (GType type)
3399 node = lookup_type_node_I (type);
3401 return node ? node->n_supers + 1 : 0;
3406 * @leaf_type: descendant of @root_type and the type to be returned
3407 * @root_type: immediate parent of the returned type
3409 * Given a @leaf_type and a @root_type which is contained in its
3410 * anchestry, return the type that @root_type is the immediate parent
3411 * of. In other words, this function determines the type that is
3412 * derived directly from @root_type which is also a base class of
3413 * @leaf_type. Given a root type and a leaf type, this function can
3414 * be used to determine the types and order in which the leaf type is
3415 * descended from the root type.
3417 * Returns: immediate child of @root_type and anchestor of @leaf_type
3420 g_type_next_base (GType type,
3426 node = lookup_type_node_I (type);
3429 TypeNode *base_node = lookup_type_node_I (base_type);
3431 if (base_node && base_node->n_supers < node->n_supers)
3433 guint n = node->n_supers - base_node->n_supers;
3435 if (node->supers[n] == base_type)
3436 atype = node->supers[n - 1];
3443 static inline gboolean
3444 type_node_check_conformities_UorL (TypeNode *node,
3445 TypeNode *iface_node,
3446 /* support_inheritance */
3447 gboolean support_interfaces,
3448 gboolean support_prerequisites,
3453 if (/* support_inheritance && */
3454 NODE_IS_ANCESTOR (iface_node, node))
3457 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3458 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3460 if (support_interfaces)
3464 if (type_lookup_iface_entry_L (node, iface_node))
3469 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3474 support_prerequisites)
3477 G_READ_LOCK (&type_rw_lock);
3478 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3481 G_READ_UNLOCK (&type_rw_lock);
3487 type_node_is_a_L (TypeNode *node,
3488 TypeNode *iface_node)
3490 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3493 static inline gboolean
3494 type_node_conforms_to_U (TypeNode *node,
3495 TypeNode *iface_node,
3496 gboolean support_interfaces,
3497 gboolean support_prerequisites)
3499 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3504 * @type: type to check anchestry for
3505 * @is_a_type: possible anchestor of @type or interface that @type
3508 * If @is_a_type is a derivable type, check whether @type is a
3509 * descendant of @is_a_type. If @is_a_type is an interface, check
3510 * whether @type conforms to it.
3512 * Returns: %TRUE if @type is a @is_a_type
3515 g_type_is_a (GType type,
3518 TypeNode *node, *iface_node;
3521 if (type == iface_type)
3524 node = lookup_type_node_I (type);
3525 iface_node = lookup_type_node_I (iface_type);
3526 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3533 * @type: the parent type
3534 * @n_children: (out) (optional): location to store the length of
3535 * the returned array, or %NULL
3537 * Return a newly allocated and 0-terminated array of type IDs, listing
3538 * the child types of @type.
3540 * Returns: (array length=n_children) (transfer full): Newly allocated
3541 * and 0-terminated array of child types, free with g_free()
3544 g_type_children (GType type,
3549 node = lookup_type_node_I (type);
3554 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3555 children = g_new (GType, node->n_children + 1);
3556 if (node->n_children != 0)
3557 memcpy (children, node->children, sizeof (GType) * node->n_children);
3558 children[node->n_children] = 0;
3561 *n_children = node->n_children;
3562 G_READ_UNLOCK (&type_rw_lock);
3576 * g_type_interfaces:
3577 * @type: the type to list interface types for
3578 * @n_interfaces: (out) (optional): location to store the length of
3579 * the returned array, or %NULL
3581 * Return a newly allocated and 0-terminated array of type IDs, listing
3582 * the interface types that @type conforms to.
3584 * Returns: (array length=n_interfaces) (transfer full): Newly allocated
3585 * and 0-terminated array of interface types, free with g_free()
3588 g_type_interfaces (GType type,
3589 guint *n_interfaces)
3593 node = lookup_type_node_I (type);
3594 if (node && node->is_instantiatable)
3596 IFaceEntries *entries;
3600 G_READ_LOCK (&type_rw_lock);
3601 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3604 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3605 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3606 ifaces[i] = entries->entry[i].iface_type;
3610 ifaces = g_new (GType, 1);
3617 G_READ_UNLOCK (&type_rw_lock);
3630 typedef struct _QData QData;
3642 static inline gpointer
3643 type_get_qdata_L (TypeNode *node,
3646 GData *gdata = node->global_gdata;
3648 if (quark && gdata && gdata->n_qdatas)
3650 QData *qdatas = gdata->qdatas - 1;
3651 guint n_qdatas = gdata->n_qdatas;
3658 i = (n_qdatas + 1) / 2;
3660 if (quark == check->quark)
3662 else if (quark > check->quark)
3667 else /* if (quark < check->quark) */
3678 * @quark: a #GQuark id to identify the data
3680 * Obtains data which has previously been attached to @type
3681 * with g_type_set_qdata().
3683 * Note that this does not take subtyping into account; data
3684 * attached to one type with g_type_set_qdata() cannot
3685 * be retrieved from a subtype using g_type_get_qdata().
3687 * Returns: (transfer none): the data, or %NULL if no data was found
3690 g_type_get_qdata (GType type,
3696 node = lookup_type_node_I (type);
3699 G_READ_LOCK (&type_rw_lock);
3700 data = type_get_qdata_L (node, quark);
3701 G_READ_UNLOCK (&type_rw_lock);
3705 g_return_val_if_fail (node != NULL, NULL);
3712 type_set_qdata_W (TypeNode *node,
3720 /* setup qdata list if necessary */
3721 if (!node->global_gdata)
3722 node->global_gdata = g_new0 (GData, 1);
3723 gdata = node->global_gdata;
3725 /* try resetting old data */
3726 qdata = gdata->qdatas;
3727 for (i = 0; i < gdata->n_qdatas; i++)
3728 if (qdata[i].quark == quark)
3730 qdata[i].data = data;
3736 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3737 qdata = gdata->qdatas;
3738 for (i = 0; i < gdata->n_qdatas - 1; i++)
3739 if (qdata[i].quark > quark)
3741 memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3742 qdata[i].quark = quark;
3743 qdata[i].data = data;
3749 * @quark: a #GQuark id to identify the data
3752 * Attaches arbitrary data to a type.
3755 g_type_set_qdata (GType type,
3761 g_return_if_fail (quark != 0);
3763 node = lookup_type_node_I (type);
3766 G_WRITE_LOCK (&type_rw_lock);
3767 type_set_qdata_W (node, quark, data);
3768 G_WRITE_UNLOCK (&type_rw_lock);
3771 g_return_if_fail (node != NULL);
3775 type_add_flags_W (TypeNode *node,
3780 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3781 g_return_if_fail (node != NULL);
3783 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3784 g_warning ("tagging type '%s' as abstract after class initialization", NODE_NAME (node));
3785 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3787 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3792 * @type: #GType of a static, classed type
3793 * @query: (out caller-allocates): a user provided structure that is
3794 * filled in with constant values upon success
3796 * Queries the type system for information about a specific type.
3797 * This function will fill in a user-provided structure to hold
3798 * type-specific information. If an invalid #GType is passed in, the
3799 * @type member of the #GTypeQuery is 0. All members filled into the
3800 * #GTypeQuery structure should be considered constant and have to be
3804 g_type_query (GType type,
3809 g_return_if_fail (query != NULL);
3811 /* if node is not static and classed, we won't allow query */
3813 node = lookup_type_node_I (type);
3814 if (node && node->is_classed && !node->plugin)
3816 /* type is classed and probably even instantiatable */
3817 G_READ_LOCK (&type_rw_lock);
3818 if (node->data) /* type is static or referenced */
3820 query->type = NODE_TYPE (node);
3821 query->type_name = NODE_NAME (node);
3822 query->class_size = node->data->class.class_size;
3823 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3825 G_READ_UNLOCK (&type_rw_lock);
3830 * g_type_get_instance_count:
3833 * Returns the number of instances allocated of the particular type;
3834 * this is only available if GLib is built with debugging support and
3835 * the instance_count debug flag is set (by setting the GOBJECT_DEBUG
3836 * variable to include instance-count).
3838 * Returns: the number of instances allocated of the given type;
3839 * if instance counts are not available, returns 0.
3844 g_type_get_instance_count (GType type)
3846 #ifdef G_ENABLE_DEBUG
3849 node = lookup_type_node_I (type);
3850 g_return_val_if_fail (node != NULL, 0);
3852 return g_atomic_int_get (&node->instance_count);
3858 /* --- implementation details --- */
3860 g_type_test_flags (GType type,
3864 gboolean result = FALSE;
3866 node = lookup_type_node_I (type);
3869 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3870 guint tflags = flags & TYPE_FLAG_MASK;
3874 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3876 fflags = (finfo->type_flags & fflags) == fflags;
3883 G_READ_LOCK (&type_rw_lock);
3884 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3885 G_READ_UNLOCK (&type_rw_lock);
3890 result = tflags && fflags;
3897 * g_type_get_plugin:
3898 * @type: #GType to retrieve the plugin for
3900 * Returns the #GTypePlugin structure for @type.
3902 * Returns: (transfer none): the corresponding plugin
3903 * if @type is a dynamic type, %NULL otherwise
3906 g_type_get_plugin (GType type)
3910 node = lookup_type_node_I (type);
3912 return node ? node->plugin : NULL;
3916 * g_type_interface_get_plugin:
3917 * @instance_type: #GType of an instantiatable type
3918 * @interface_type: #GType of an interface type
3920 * Returns the #GTypePlugin structure for the dynamic interface
3921 * @interface_type which has been added to @instance_type, or %NULL
3922 * if @interface_type has not been added to @instance_type or does
3923 * not have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3925 * Returns: (transfer none): the #GTypePlugin for the dynamic
3926 * interface @interface_type of @instance_type
3929 g_type_interface_get_plugin (GType instance_type,
3930 GType interface_type)
3935 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3937 node = lookup_type_node_I (instance_type);
3938 iface = lookup_type_node_I (interface_type);
3941 IFaceHolder *iholder;
3942 GTypePlugin *plugin;
3944 G_READ_LOCK (&type_rw_lock);
3946 iholder = iface_node_get_holders_L (iface);
3947 while (iholder && iholder->instance_type != instance_type)
3948 iholder = iholder->next;
3949 plugin = iholder ? iholder->plugin : NULL;
3951 G_READ_UNLOCK (&type_rw_lock);
3956 g_return_val_if_fail (node == NULL, NULL);
3957 g_return_val_if_fail (iface == NULL, NULL);
3959 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3965 * g_type_fundamental_next:
3967 * Returns the next free fundamental type id which can be used to
3968 * register a new fundamental type with g_type_register_fundamental().
3969 * The returned type ID represents the highest currently registered
3970 * fundamental type identifier.
3972 * Returns: the next available fundamental type ID to be registered,
3973 * or 0 if the type system ran out of fundamental type IDs
3976 g_type_fundamental_next (void)
3980 G_READ_LOCK (&type_rw_lock);
3981 type = static_fundamental_next;
3982 G_READ_UNLOCK (&type_rw_lock);
3983 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3984 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3988 * g_type_fundamental:
3989 * @type_id: valid type ID
3991 * Internal function, used to extract the fundamental type ID portion.
3992 * Use G_TYPE_FUNDAMENTAL() instead.
3994 * Returns: fundamental type ID
3997 g_type_fundamental (GType type_id)
3999 TypeNode *node = lookup_type_node_I (type_id);
4001 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
4005 g_type_check_instance_is_a (GTypeInstance *type_instance,
4008 TypeNode *node, *iface;
4011 if (!type_instance || !type_instance->g_class)
4014 node = lookup_type_node_I (type_instance->g_class->g_type);
4015 iface = lookup_type_node_I (iface_type);
4016 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4022 g_type_check_instance_is_fundamentally_a (GTypeInstance *type_instance,
4023 GType fundamental_type)
4026 if (!type_instance || !type_instance->g_class)
4028 node = lookup_type_node_I (type_instance->g_class->g_type);
4029 return node && (NODE_FUNDAMENTAL_TYPE(node) == fundamental_type);
4033 g_type_check_class_is_a (GTypeClass *type_class,
4036 TypeNode *node, *iface;
4042 node = lookup_type_node_I (type_class->g_type);
4043 iface = lookup_type_node_I (is_a_type);
4044 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4050 g_type_check_instance_cast (GTypeInstance *type_instance,
4055 if (type_instance->g_class)
4057 TypeNode *node, *iface;
4058 gboolean is_instantiatable, check;
4060 node = lookup_type_node_I (type_instance->g_class->g_type);
4061 is_instantiatable = node && node->is_instantiatable;
4062 iface = lookup_type_node_I (iface_type);
4063 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4065 return type_instance;
4067 if (is_instantiatable)
4068 g_warning ("invalid cast from '%s' to '%s'",
4069 type_descriptive_name_I (type_instance->g_class->g_type),
4070 type_descriptive_name_I (iface_type));
4072 g_warning ("invalid uninstantiatable type '%s' in cast to '%s'",
4073 type_descriptive_name_I (type_instance->g_class->g_type),
4074 type_descriptive_name_I (iface_type));
4077 g_warning ("invalid unclassed pointer in cast to '%s'",
4078 type_descriptive_name_I (iface_type));
4081 return type_instance;
4085 g_type_check_class_cast (GTypeClass *type_class,
4090 TypeNode *node, *iface;
4091 gboolean is_classed, check;
4093 node = lookup_type_node_I (type_class->g_type);
4094 is_classed = node && node->is_classed;
4095 iface = lookup_type_node_I (is_a_type);
4096 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4101 g_warning ("invalid class cast from '%s' to '%s'",
4102 type_descriptive_name_I (type_class->g_type),
4103 type_descriptive_name_I (is_a_type));
4105 g_warning ("invalid unclassed type '%s' in class cast to '%s'",
4106 type_descriptive_name_I (type_class->g_type),
4107 type_descriptive_name_I (is_a_type));
4110 g_warning ("invalid class cast from (NULL) pointer to '%s'",
4111 type_descriptive_name_I (is_a_type));
4116 * g_type_check_instance:
4117 * @instance: a valid #GTypeInstance structure
4119 * Private helper function to aid implementation of the
4120 * G_TYPE_CHECK_INSTANCE() macro.
4122 * Returns: %TRUE if @instance is valid, %FALSE otherwise
4125 g_type_check_instance (GTypeInstance *type_instance)
4127 /* this function is just here to make the signal system
4128 * conveniently elaborated on instance checks
4132 if (type_instance->g_class)
4134 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4136 if (node && node->is_instantiatable)
4139 g_warning ("instance of invalid non-instantiatable type '%s'",
4140 type_descriptive_name_I (type_instance->g_class->g_type));
4143 g_warning ("instance with invalid (NULL) class pointer");
4146 g_warning ("invalid (NULL) pointer instance");
4151 static inline gboolean
4152 type_check_is_value_type_U (GType type)
4154 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4157 /* common path speed up */
4158 node = lookup_type_node_I (type);
4159 if (node && node->mutatable_check_cache)
4162 G_READ_LOCK (&type_rw_lock);
4166 if (node->data && NODE_REFCOUNT (node) > 0 &&
4167 node->data->common.value_table->value_init)
4168 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4169 else if (NODE_IS_IFACE (node))
4173 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4175 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4176 TypeNode *prnode = lookup_type_node_I (prtype);
4178 if (prnode->is_instantiatable)
4181 node = lookup_type_node_I (type);
4187 G_READ_UNLOCK (&type_rw_lock);
4189 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4193 g_type_check_is_value_type (GType type)
4195 return type_check_is_value_type_U (type);
4199 g_type_check_value (const GValue *value)
4201 return value && type_check_is_value_type_U (value->g_type);
4205 g_type_check_value_holds (const GValue *value,
4208 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4212 * g_type_value_table_peek: (skip)
4215 * Returns the location of the #GTypeValueTable associated with @type.
4217 * Note that this function should only be used from source code
4218 * that implements or has internal knowledge of the implementation of
4221 * Returns: location of the #GTypeValueTable associated with @type or
4222 * %NULL if there is no #GTypeValueTable associated with @type
4225 g_type_value_table_peek (GType type)
4227 GTypeValueTable *vtable = NULL;
4228 TypeNode *node = lookup_type_node_I (type);
4229 gboolean has_refed_data, has_table;
4231 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4232 return node->data->common.value_table;
4234 G_READ_LOCK (&type_rw_lock);
4237 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4238 has_table = has_refed_data && node->data->common.value_table->value_init;
4242 vtable = node->data->common.value_table;
4243 else if (NODE_IS_IFACE (node))
4247 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4249 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4250 TypeNode *prnode = lookup_type_node_I (prtype);
4252 if (prnode->is_instantiatable)
4255 node = lookup_type_node_I (type);
4256 goto restart_table_peek;
4262 G_READ_UNLOCK (&type_rw_lock);
4268 g_warning (G_STRLOC ": type id '%" G_GSIZE_FORMAT "' is invalid", type);
4269 if (!has_refed_data)
4270 g_warning ("can't peek value table for type '%s' which is not currently referenced",
4271 type_descriptive_name_I (type));
4277 g_type_name_from_instance (GTypeInstance *instance)
4280 return "<NULL-instance>";
4282 return g_type_name_from_class (instance->g_class);
4286 g_type_name_from_class (GTypeClass *g_class)
4289 return "<NULL-class>";
4291 return g_type_name (g_class->g_type);
4295 /* --- private api for gboxed.c --- */
4297 _g_type_boxed_copy (GType type, gpointer value)
4299 TypeNode *node = lookup_type_node_I (type);
4301 return node->data->boxed.copy_func (value);
4305 _g_type_boxed_free (GType type, gpointer value)
4307 TypeNode *node = lookup_type_node_I (type);
4309 node->data->boxed.free_func (value);
4313 _g_type_boxed_init (GType type,
4314 GBoxedCopyFunc copy_func,
4315 GBoxedFreeFunc free_func)
4317 TypeNode *node = lookup_type_node_I (type);
4319 node->data->boxed.copy_func = copy_func;
4320 node->data->boxed.free_func = free_func;
4323 /* --- initialization --- */
4325 * g_type_init_with_debug_flags:
4326 * @debug_flags: bitwise combination of #GTypeDebugFlags values for
4327 * debugging purposes
4329 * This function used to initialise the type system with debugging
4330 * flags. Since GLib 2.36, the type system is initialised automatically
4331 * and this function does nothing.
4333 * If you need to enable debugging features, use the GOBJECT_DEBUG
4334 * environment variable.
4336 * Deprecated: 2.36: the type system is now initialised automatically
4338 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4340 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4342 g_assert_type_system_initialized ();
4345 g_message ("g_type_init_with_debug_flags() is no longer supported. Use the GOBJECT_DEBUG environment variable.");
4347 G_GNUC_END_IGNORE_DEPRECATIONS
4352 * This function used to initialise the type system. Since GLib 2.36,
4353 * the type system is initialised automatically and this function does
4356 * Deprecated: 2.36: the type system is now initialised automatically
4361 g_assert_type_system_initialized ();
4367 const gchar *env_string;
4370 GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
4372 /* Ensure GLib is initialized first, see
4373 * https://bugzilla.gnome.org/show_bug.cgi?id=756139
4375 GLIB_PRIVATE_CALL (glib_init) ();
4377 G_WRITE_LOCK (&type_rw_lock);
4379 /* setup GObject library wide debugging flags */
4380 env_string = g_getenv ("GOBJECT_DEBUG");
4381 if (env_string != NULL)
4383 GDebugKey debug_keys[] = {
4384 { "objects", G_TYPE_DEBUG_OBJECTS },
4385 { "instance-count", G_TYPE_DEBUG_INSTANCE_COUNT },
4386 { "signals", G_TYPE_DEBUG_SIGNALS },
4389 _g_type_debug_flags = g_parse_debug_string (env_string, debug_keys, G_N_ELEMENTS (debug_keys));
4393 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4394 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4395 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4397 /* type qname hash table */
4398 static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
4400 /* invalid type G_TYPE_INVALID (0)
4402 static_fundamental_type_nodes[0] = NULL;
4404 /* void type G_TYPE_NONE
4406 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4407 type = NODE_TYPE (node);
4408 g_assert (type == G_TYPE_NONE);
4410 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4412 memset (&info, 0, sizeof (info));
4413 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4414 type = NODE_TYPE (node);
4415 type_data_make_W (node, &info, NULL);
4416 g_assert (type == G_TYPE_INTERFACE);
4418 G_WRITE_UNLOCK (&type_rw_lock);
4422 /* G_TYPE_TYPE_PLUGIN
4424 g_type_ensure (g_type_plugin_get_type ());
4426 /* G_TYPE_* value types
4428 _g_value_types_init ();
4430 /* G_TYPE_ENUM & G_TYPE_FLAGS
4432 _g_enum_types_init ();
4436 _g_boxed_type_init ();
4440 _g_param_type_init ();
4444 _g_object_type_init ();
4446 /* G_TYPE_PARAM_* pspec types
4448 _g_param_spec_types_init ();
4450 /* Value Transformations
4452 _g_value_transforms_init ();
4459 #if defined (G_OS_WIN32)
4461 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
4463 LPVOID lpvReserved);
4466 DllMain (HINSTANCE hinstDLL,
4472 case DLL_PROCESS_ATTACH:
4484 #elif defined (G_HAS_CONSTRUCTORS)
4485 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
4486 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
4488 G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
4491 gobject_init_ctor (void)
4497 # error Your platform/compiler is missing constructor support
4501 * g_type_class_add_private:
4502 * @g_class: (type GObject.TypeClass): class structure for an instantiatable
4504 * @private_size: size of private structure
4506 * Registers a private structure for an instantiatable type.
4508 * When an object is allocated, the private structures for
4509 * the type and all of its parent types are allocated
4510 * sequentially in the same memory block as the public
4511 * structures, and are zero-filled.
4513 * Note that the accumulated size of the private structures of
4514 * a type and all its parent types cannot exceed 64 KiB.
4516 * This function should be called in the type's class_init() function.
4517 * The private structure can be retrieved using the
4518 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4520 * The following example shows attaching a private structure
4521 * MyObjectPrivate to an object MyObject defined in the standard
4522 * GObject fashion in the type's class_init() function.
4524 * Note the use of a structure member "priv" to avoid the overhead
4525 * of repeatedly calling MY_OBJECT_GET_PRIVATE().
4527 * |[<!-- language="C" -->
4528 * typedef struct _MyObject MyObject;
4529 * typedef struct _MyObjectPrivate MyObjectPrivate;
4531 * struct _MyObject {
4534 * MyObjectPrivate *priv;
4537 * struct _MyObjectPrivate {
4542 * my_object_class_init (MyObjectClass *klass)
4544 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4548 * my_object_init (MyObject *my_object)
4550 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4553 * // my_object->priv->some_field will be automatically initialised to 0
4557 * my_object_get_some_field (MyObject *my_object)
4559 * MyObjectPrivate *priv;
4561 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
4563 * priv = my_object->priv;
4565 * return priv->some_field;
4570 * Deprecated: 2.58: Use the G_ADD_PRIVATE() macro with the `G_DEFINE_*`
4571 * family of macros to add instance private data to a type
4574 g_type_class_add_private (gpointer g_class,
4577 GType instance_type = ((GTypeClass *)g_class)->g_type;
4578 TypeNode *node = lookup_type_node_I (instance_type);
4580 g_return_if_fail (private_size > 0);
4581 g_return_if_fail (private_size <= 0xffff);
4583 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4585 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4586 type_descriptive_name_I (instance_type));
4590 if (NODE_PARENT_TYPE (node))
4592 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4593 if (node->data->instance.private_size != pnode->data->instance.private_size)
4595 g_warning ("g_type_class_add_private() called multiple times for the same type");
4600 G_WRITE_LOCK (&type_rw_lock);
4602 private_size = ALIGN_STRUCT (node->data->instance.private_size + private_size);
4603 g_assert (private_size <= 0xffff);
4604 node->data->instance.private_size = private_size;
4606 G_WRITE_UNLOCK (&type_rw_lock);
4609 /* semi-private, called only by the G_ADD_PRIVATE macro */
4611 g_type_add_instance_private (GType class_gtype,
4614 TypeNode *node = lookup_type_node_I (class_gtype);
4616 g_return_val_if_fail (private_size > 0, 0);
4617 g_return_val_if_fail (private_size <= 0xffff, 0);
4619 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4621 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4622 type_descriptive_name_I (class_gtype));
4626 if (node->plugin != NULL)
4628 g_warning ("cannot use g_type_add_instance_private() with dynamic type '%s'",
4629 type_descriptive_name_I (class_gtype));
4633 /* in the future, we want to register the private data size of a type
4634 * directly from the get_type() implementation so that we can take full
4635 * advantage of the type definition macros that we already have.
4637 * unfortunately, this does not behave correctly if a class in the middle
4638 * of the type hierarchy uses the "old style" of private data registration
4639 * from the class_init() implementation, as the private data offset is not
4640 * going to be known until the full class hierarchy is initialized.
4642 * in order to transition our code to the Glorious New Future™, we proceed
4643 * with a two-step implementation: first, we provide this new function to
4644 * register the private data size in the get_type() implementation and we
4645 * hide it behind a macro. the function will return the private size, instead
4646 * of the offset, which will be stored inside a static variable defined by
4647 * the G_DEFINE_TYPE_EXTENDED macro. the G_DEFINE_TYPE_EXTENDED macro will
4648 * check the variable and call g_type_class_add_instance_private(), which
4649 * will use the data size and actually register the private data, then
4650 * return the computed offset of the private data, which will be stored
4651 * inside the static variable, so we can use it to retrieve the pointer
4652 * to the private data structure.
4654 * once all our code has been migrated to the new idiomatic form of private
4655 * data registration, we will change the g_type_add_instance_private()
4656 * function to actually perform the registration and return the offset
4657 * of the private data; g_type_class_add_instance_private() already checks
4658 * if the passed argument is negative (meaning that it's an offset in the
4659 * GTypeInstance allocation) and becomes a no-op if that's the case. this
4660 * should make the migration fully transparent even if we're effectively
4661 * copying this macro into everybody's code.
4663 return private_size;
4666 /* semi-private function, should only be used by G_DEFINE_TYPE_EXTENDED */
4668 g_type_class_adjust_private_offset (gpointer g_class,
4669 gint *private_size_or_offset)
4671 GType class_gtype = ((GTypeClass *) g_class)->g_type;
4672 TypeNode *node = lookup_type_node_I (class_gtype);
4673 gssize private_size;
4675 g_return_if_fail (private_size_or_offset != NULL);
4677 /* if we have been passed the offset instead of the private data size,
4678 * then we consider this as a no-op, and just return the value. see the
4679 * comment in g_type_add_instance_private() for the full explanation.
4681 if (*private_size_or_offset > 0)
4682 g_return_if_fail (*private_size_or_offset <= 0xffff);
4686 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4688 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4689 type_descriptive_name_I (class_gtype));
4690 *private_size_or_offset = 0;
4694 if (NODE_PARENT_TYPE (node))
4696 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4697 if (node->data->instance.private_size != pnode->data->instance.private_size)
4699 g_warning ("g_type_add_instance_private() called multiple times for the same type");
4700 *private_size_or_offset = 0;
4705 G_WRITE_LOCK (&type_rw_lock);
4707 private_size = ALIGN_STRUCT (node->data->instance.private_size + *private_size_or_offset);
4708 g_assert (private_size <= 0xffff);
4709 node->data->instance.private_size = private_size;
4711 *private_size_or_offset = -(gint) node->data->instance.private_size;
4713 G_WRITE_UNLOCK (&type_rw_lock);
4717 g_type_instance_get_private (GTypeInstance *instance,
4722 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4724 node = lookup_type_node_I (private_type);
4725 if (G_UNLIKELY (!node || !node->is_instantiatable))
4727 g_warning ("instance of invalid non-instantiatable type '%s'",
4728 type_descriptive_name_I (instance->g_class->g_type));
4732 return ((gchar *) instance) - node->data->instance.private_size;
4736 * g_type_class_get_instance_private_offset: (skip)
4737 * @g_class: (type GObject.TypeClass): a #GTypeClass
4739 * Gets the offset of the private data for instances of @g_class.
4741 * This is how many bytes you should add to the instance pointer of a
4742 * class in order to get the private data for the type represented by
4745 * You can only call this function after you have registered a private
4746 * data area for @g_class using g_type_class_add_private().
4748 * Returns: the offset, in bytes
4753 g_type_class_get_instance_private_offset (gpointer g_class)
4755 GType instance_type;
4756 guint16 parent_size;
4759 g_assert (g_class != NULL);
4761 instance_type = ((GTypeClass *) g_class)->g_type;
4762 node = lookup_type_node_I (instance_type);
4764 g_assert (node != NULL);
4765 g_assert (node->is_instantiatable);
4767 if (NODE_PARENT_TYPE (node))
4769 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4771 parent_size = pnode->data->instance.private_size;
4776 if (node->data->instance.private_size == parent_size)
4777 g_error ("g_type_class_get_instance_private_offset() called on class %s but it has no private data",
4778 g_type_name (instance_type));
4780 return -(gint) node->data->instance.private_size;
4784 * g_type_add_class_private:
4785 * @class_type: GType of a classed type
4786 * @private_size: size of private structure
4788 * Registers a private class structure for a classed type;
4789 * when the class is allocated, the private structures for
4790 * the class and all of its parent types are allocated
4791 * sequentially in the same memory block as the public
4792 * structures, and are zero-filled.
4794 * This function should be called in the
4795 * type's get_type() function after the type is registered.
4796 * The private structure can be retrieved using the
4797 * G_TYPE_CLASS_GET_PRIVATE() macro.
4802 g_type_add_class_private (GType class_type,
4805 TypeNode *node = lookup_type_node_I (class_type);
4808 g_return_if_fail (private_size > 0);
4810 if (!node || !node->is_classed || !node->data)
4812 g_warning ("cannot add class private field to invalid type '%s'",
4813 type_descriptive_name_I (class_type));
4817 if (NODE_PARENT_TYPE (node))
4819 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4820 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4822 g_warning ("g_type_add_class_private() called multiple times for the same type");
4827 G_WRITE_LOCK (&type_rw_lock);
4829 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4830 node->data->class.class_private_size = offset + private_size;
4832 G_WRITE_UNLOCK (&type_rw_lock);
4836 g_type_class_get_private (GTypeClass *klass,
4839 TypeNode *class_node;
4840 TypeNode *private_node;
4841 TypeNode *parent_node;
4844 g_return_val_if_fail (klass != NULL, NULL);
4846 class_node = lookup_type_node_I (klass->g_type);
4847 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4849 g_warning ("class of invalid type '%s'",
4850 type_descriptive_name_I (klass->g_type));
4854 private_node = lookup_type_node_I (private_type);
4855 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4857 g_warning ("attempt to retrieve private data for invalid type '%s'",
4858 type_descriptive_name_I (private_type));
4862 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4864 if (NODE_PARENT_TYPE (private_node))
4866 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4867 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4869 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4871 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_add_class_private()");
4875 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4878 return G_STRUCT_MEMBER_P (klass, offset);
4885 * Ensures that the indicated @type has been registered with the
4886 * type system, and its _class_init() method has been run.
4888 * In theory, simply calling the type's _get_type() method (or using
4889 * the corresponding macro) is supposed take care of this. However,
4890 * _get_type() methods are often marked %G_GNUC_CONST for performance
4891 * reasons, even though this is technically incorrect (since
4892 * %G_GNUC_CONST requires that the function not have side effects,
4893 * which _get_type() methods do on the first call). As a result, if
4894 * you write a bare call to a _get_type() macro, it may get optimized
4895 * out by the compiler. Using g_type_ensure() guarantees that the
4896 * type's _get_type() method is called.
4901 g_type_ensure (GType type)
4903 /* In theory, @type has already been resolved and so there's nothing
4904 * to do here. But this protects us in the case where the function
4905 * gets inlined (as it might in gobject_init_ctor() above).
4907 if (G_UNLIKELY (type == (GType)-1))
4908 g_error ("can't happen");