1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "../glib/valgrind.h"
28 #include "gtype-private.h"
29 #include "gtypeplugin.h"
30 #include "gvaluecollector.h"
31 #include "gbsearcharray.h"
32 #include "gatomicarray.h"
33 #include "gobject_trace.h"
35 #include "gconstructor.h"
40 * @short_description: The GLib Runtime type identification and
42 * @title:Type Information
44 * The GType API is the foundation of the GObject system. It provides the
45 * facilities for registering and managing all fundamental data types,
46 * user-defined object and interface types.
48 * For type creation and registration purposes, all types fall into one of
49 * two categories: static or dynamic. Static types are never loaded or
50 * unloaded at run-time as dynamic types may be. Static types are created
51 * with g_type_register_static() that gets type specific information passed
52 * in via a #GTypeInfo structure.
54 * Dynamic types are created with g_type_register_dynamic() which takes a
55 * #GTypePlugin structure instead. The remaining type information (the
56 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
57 * and the g_type_plugin_*() API.
59 * These registration functions are usually called only once from a
60 * function whose only purpose is to return the type identifier for a
61 * specific class. Once the type (or class or interface) is registered,
62 * it may be instantiated, inherited, or implemented depending on exactly
63 * what sort of type it is.
65 * There is also a third registration function for registering fundamental
66 * types called g_type_register_fundamental() which requires both a #GTypeInfo
67 * structure and a #GTypeFundamentalInfo structure but it is seldom used
68 * since most fundamental types are predefined rather than user-defined.
70 * Type instance and class structs are limited to a total of 64 KiB,
71 * including all parent types. Similarly, type instances' private data
72 * (as created by g_type_class_add_private()) are limited to a total of
73 * 64 KiB. If a type instance needs a large static buffer, allocate it
74 * separately (typically by using #GArray or #GPtrArray) and put a pointer
75 * to the buffer in the structure.
77 * A final word about type names: Such an identifier needs to be at least
78 * three characters long. There is no upper length limit. The first character
79 * needs to be a letter (a-z or A-Z) or an underscore '_'. Subsequent
80 * characters can be letters, numbers or any of '-_+'.
84 /* NOTE: some functions (some internal variants and exported ones)
85 * invalidate data portions of the TypeNodes. if external functions/callbacks
86 * are called, pointers to memory maintained by TypeNodes have to be looked up
87 * again. this affects most of the struct TypeNode fields, e.g. ->children or
88 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
89 * not ->supers[]), as all those memory portions can get realloc()ed during
90 * callback invocation.
93 * lock handling issues when calling static functions are indicated by
94 * uppercase letter postfixes, all static functions have to have
95 * one of the below postfixes:
96 * - _I: [Indifferent about locking]
97 * function doesn't care about locks at all
98 * - _U: [Unlocked invocation]
99 * no read or write lock has to be held across function invocation
100 * (locks may be acquired and released during invocation though)
101 * - _L: [Locked invocation]
102 * a write lock or more than 0 read locks have to be held across
103 * function invocation
104 * - _W: [Write-locked invocation]
105 * a write lock has to be held across function invocation
106 * - _Wm: [Write-locked invocation, mutatable]
107 * like _W, but the write lock might be released and reacquired
108 * during invocation, watch your pointers
109 * - _WmREC: [Write-locked invocation, mutatable, recursive]
110 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
114 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_rw_lock_reader_lock (rw_lock); } while (0)
115 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_rw_lock_reader_unlock (rw_lock); } while (0)
116 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_rw_lock_writer_lock (rw_lock); } while (0)
117 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_rw_lock_writer_unlock (rw_lock); } while (0)
119 #define G_READ_LOCK(rw_lock) g_rw_lock_reader_lock (rw_lock)
120 #define G_READ_UNLOCK(rw_lock) g_rw_lock_reader_unlock (rw_lock)
121 #define G_WRITE_LOCK(rw_lock) g_rw_lock_writer_lock (rw_lock)
122 #define G_WRITE_UNLOCK(rw_lock) g_rw_lock_writer_unlock (rw_lock)
124 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
125 static const gchar _action[] = " invalidly modified type "; \
126 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
128 g_error ("%s(%p)%s'%s'", _fname, _arg, _action, _tname); \
130 g_error ("%s()%s'%s'", _fname, _action, _tname); \
132 #define g_assert_type_system_initialized() \
133 g_assert (static_quark_type_flags)
135 #ifdef G_ENABLE_DEBUG
136 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
137 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
140 #else /* !G_ENABLE_DEBUG */
141 #define DEBUG_CODE(debug_type, code_block) /* code_block */
142 #endif /* G_ENABLE_DEBUG */
144 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
145 G_TYPE_FLAG_INSTANTIATABLE | \
146 G_TYPE_FLAG_DERIVABLE | \
147 G_TYPE_FLAG_DEEP_DERIVABLE)
148 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
149 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
150 sizeof (gpointer)), \
153 /* The 2*sizeof(size_t) alignment here is borrowed from
154 * GNU libc, so it should be good most everywhere.
155 * It is more conservative than is needed on some 64-bit
156 * platforms, but ia64 does require a 16-byte alignment.
157 * The SIMD extensions for x86 and ppc32 would want a
158 * larger alignment than this, but we don't need to
159 * do better than malloc.
161 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
162 #define ALIGN_STRUCT(offset) \
163 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
166 /* --- typedefs --- */
167 typedef struct _TypeNode TypeNode;
168 typedef struct _CommonData CommonData;
169 typedef struct _BoxedData BoxedData;
170 typedef struct _IFaceData IFaceData;
171 typedef struct _ClassData ClassData;
172 typedef struct _InstanceData InstanceData;
173 typedef union _TypeData TypeData;
174 typedef struct _IFaceEntries IFaceEntries;
175 typedef struct _IFaceEntry IFaceEntry;
176 typedef struct _IFaceHolder IFaceHolder;
179 /* --- prototypes --- */
180 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
181 static void type_add_flags_W (TypeNode *node,
183 static void type_data_make_W (TypeNode *node,
184 const GTypeInfo *info,
185 const GTypeValueTable *value_table);
186 static inline void type_data_ref_Wm (TypeNode *node);
187 static inline void type_data_unref_U (TypeNode *node,
189 static void type_data_last_unref_Wm (TypeNode * node,
191 static inline gpointer type_get_qdata_L (TypeNode *node,
193 static inline void type_set_qdata_W (TypeNode *node,
196 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
197 GType instance_type);
198 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
200 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
202 static gboolean type_node_is_a_L (TypeNode *node,
203 TypeNode *iface_node);
206 /* --- enumeration --- */
208 /* The InitState enumeration is used to track the progress of initializing
209 * both classes and interface vtables. Keeping the state of initialization
210 * is necessary to handle new interfaces being added while we are initializing
211 * the class or other interfaces.
223 /* --- structures --- */
226 guint volatile ref_count;
228 guint n_children; /* writable with lock */
230 guint n_prerequisites : 9;
231 guint is_classed : 1;
232 guint is_instantiatable : 1;
233 guint mutatable_check_cache : 1; /* combines some common path checks */
234 GType *children; /* writable with lock */
235 TypeData * volatile data;
239 GAtomicArray iface_entries; /* for !iface types */
240 GAtomicArray offsets;
242 GType *prerequisites;
243 GType supers[1]; /* flexible array */
246 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
247 #define MAX_N_SUPERS (255)
248 #define MAX_N_CHILDREN (4095)
249 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
250 #define MAX_N_PREREQUISITES (511)
251 #define NODE_TYPE(node) (node->supers[0])
252 #define NODE_PARENT_TYPE(node) (node->supers[1])
253 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
254 #define NODE_NAME(node) (g_quark_to_string (node->qname))
255 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
256 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
257 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
258 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
259 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
260 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
261 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
262 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
263 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
264 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
265 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
266 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
268 #define NODE_IS_ANCESTOR(ancestor, node) \
269 ((ancestor)->n_supers <= (node)->n_supers && \
270 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
275 GInterfaceInfo *info;
283 GTypeInterface *vtable;
284 InitState init_state;
287 struct _IFaceEntries {
292 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
293 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
297 GTypeValueTable *value_table;
303 GBoxedCopyFunc copy_func;
304 GBoxedFreeFunc free_func;
311 GBaseInitFunc vtable_init_base;
312 GBaseFinalizeFunc vtable_finalize_base;
313 GClassInitFunc dflt_init;
314 GClassFinalizeFunc dflt_finalize;
315 gconstpointer dflt_data;
316 gpointer dflt_vtable;
323 guint16 class_private_size;
324 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
325 GBaseInitFunc class_init_base;
326 GBaseFinalizeFunc class_finalize_base;
327 GClassInitFunc class_init;
328 GClassFinalizeFunc class_finalize;
329 gconstpointer class_data;
337 guint16 class_private_size;
338 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
339 GBaseInitFunc class_init_base;
340 GBaseFinalizeFunc class_finalize_base;
341 GClassInitFunc class_init;
342 GClassFinalizeFunc class_finalize;
343 gconstpointer class_data;
345 guint16 instance_size;
346 guint16 private_size;
348 GInstanceInitFunc instance_init;
357 InstanceData instance;
362 GTypeClassCacheFunc cache_func;
367 GTypeInterfaceCheckFunc check_func;
371 /* --- variables --- */
372 static GRWLock type_rw_lock;
373 static GRecMutex class_init_rec_mutex;
374 static guint static_n_class_cache_funcs = 0;
375 static ClassCacheFunc *static_class_cache_funcs = NULL;
376 static guint static_n_iface_check_funcs = 0;
377 static IFaceCheckFunc *static_iface_check_funcs = NULL;
378 static GQuark static_quark_type_flags = 0;
379 static GQuark static_quark_iface_holder = 0;
380 static GQuark static_quark_dependants_array = 0;
381 static guint type_registration_serial = 0;
382 GTypeDebugFlags _g_type_debug_flags = 0;
384 /* --- type nodes --- */
385 static GHashTable *static_type_nodes_ht = NULL;
386 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
387 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
389 static inline TypeNode*
390 lookup_type_node_I (register GType utype)
392 if (utype > G_TYPE_FUNDAMENTAL_MAX)
393 return (TypeNode*) (utype & ~TYPE_ID_MASK);
395 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
399 * g_type_get_type_registration_serial:
401 * Returns an opaque serial number that represents the state of the set
402 * of registered types. Any time a type is registered this serial changes,
403 * which means you can cache information based on type lookups (such as
404 * g_type_from_name()) and know if the cache is still valid at a later
405 * time by comparing the current serial with the one at the type lookup.
409 * Returns: An unsigned int, representing the state of type registrations
412 g_type_get_type_registration_serial (void)
414 return (guint)g_atomic_int_get ((gint *)&type_registration_serial);
418 type_node_any_new_W (TypeNode *pnode,
422 GTypeFundamentalFlags type_flags)
427 guint i, node_size = 0;
429 n_supers = pnode ? pnode->n_supers + 1 : 0;
432 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
433 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
434 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
435 node = g_malloc0 (node_size);
436 if (!pnode) /* offset fundamental types */
438 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
439 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
445 g_assert ((type & TYPE_ID_MASK) == 0);
447 node->n_supers = n_supers;
450 node->supers[0] = type;
453 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
454 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
456 if (NODE_IS_IFACE (node))
458 IFACE_NODE_N_PREREQUISITES (node) = 0;
459 IFACE_NODE_PREREQUISITES (node) = NULL;
462 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
466 node->supers[0] = type;
467 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
469 node->is_classed = pnode->is_classed;
470 node->is_instantiatable = pnode->is_instantiatable;
472 if (NODE_IS_IFACE (node))
474 IFACE_NODE_N_PREREQUISITES (node) = 0;
475 IFACE_NODE_PREREQUISITES (node) = NULL;
480 IFaceEntries *entries;
482 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
483 IFACE_ENTRIES_HEADER_SIZE,
487 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
489 entries->entry[j].vtable = NULL;
490 entries->entry[j].init_state = UNINITIALIZED;
492 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
497 i = pnode->n_children++;
498 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
499 pnode->children[i] = type;
502 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
504 node->plugin = plugin;
505 node->n_children = 0;
506 node->children = NULL;
508 node->qname = g_quark_from_string (name);
509 node->global_gdata = NULL;
511 g_hash_table_insert (static_type_nodes_ht,
512 (gpointer) g_quark_to_string (node->qname),
515 g_atomic_int_inc ((gint *)&type_registration_serial);
520 static inline GTypeFundamentalInfo*
521 type_node_fundamental_info_I (TypeNode *node)
523 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
525 if (ftype != NODE_TYPE (node))
526 node = lookup_type_node_I (ftype);
528 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
532 type_node_fundamental_new_W (GType ftype,
534 GTypeFundamentalFlags type_flags)
536 GTypeFundamentalInfo *finfo;
539 g_assert ((ftype & TYPE_ID_MASK) == 0);
540 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
542 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
543 static_fundamental_next++;
545 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
547 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
549 finfo = type_node_fundamental_info_I (node);
550 finfo->type_flags = type_flags;
556 type_node_new_W (TypeNode *pnode,
562 g_assert (pnode->n_supers < MAX_N_SUPERS);
563 g_assert (pnode->n_children < MAX_N_CHILDREN);
565 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
568 static inline IFaceEntry*
569 lookup_iface_entry_I (volatile IFaceEntries *entries,
570 TypeNode *iface_node)
581 G_ATOMIC_ARRAY_DO_TRANSACTION
582 (&iface_node->_prot.offsets, guint8,
585 offsets = transaction_data;
586 offset_index = entries->offset_index;
587 if (offsets != NULL &&
588 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
590 index = offsets[offset_index];
593 /* zero means unset, subtract one to get real index */
596 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
598 check = (IFaceEntry *)&entries->entry[index];
599 if (check->iface_type == NODE_TYPE (iface_node))
609 static inline IFaceEntry*
610 type_lookup_iface_entry_L (TypeNode *node,
611 TypeNode *iface_node)
613 if (!NODE_IS_IFACE (iface_node))
616 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
621 static inline gboolean
622 type_lookup_iface_vtable_I (TypeNode *node,
623 TypeNode *iface_node,
624 gpointer *vtable_ptr)
629 if (!NODE_IS_IFACE (iface_node))
636 G_ATOMIC_ARRAY_DO_TRANSACTION
637 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
639 entry = lookup_iface_entry_I (transaction_data, iface_node);
644 *vtable_ptr = entry->vtable;
653 static inline gboolean
654 type_lookup_prerequisite_L (TypeNode *iface,
655 GType prerequisite_type)
657 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
659 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
660 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
667 i = (n_prerequisites + 1) >> 1;
668 check = prerequisites + i;
669 if (prerequisite_type == *check)
671 else if (prerequisite_type > *check)
673 n_prerequisites -= i;
674 prerequisites = check;
676 else /* if (prerequisite_type < *check) */
677 n_prerequisites = i - 1;
679 while (n_prerequisites);
685 type_descriptive_name_I (GType type)
689 TypeNode *node = lookup_type_node_I (type);
691 return node ? NODE_NAME (node) : "<unknown>";
698 /* --- type consistency checks --- */
700 check_plugin_U (GTypePlugin *plugin,
701 gboolean need_complete_type_info,
702 gboolean need_complete_interface_info,
703 const gchar *type_name)
705 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
709 g_warning ("plugin handle for type '%s' is NULL",
713 if (!G_IS_TYPE_PLUGIN (plugin))
715 g_warning ("plugin pointer (%p) for type '%s' is invalid",
719 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
721 g_warning ("plugin for type '%s' has no complete_type_info() implementation",
725 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
727 g_warning ("plugin for type '%s' has no complete_interface_info() implementation",
735 check_type_name_I (const gchar *type_name)
737 static const gchar extra_chars[] = "-_+";
738 const gchar *p = type_name;
741 if (!type_name[0] || !type_name[1] || !type_name[2])
743 g_warning ("type name '%s' is too short", type_name);
746 /* check the first letter */
747 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
748 for (p = type_name + 1; *p; p++)
749 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
750 (p[0] >= 'a' && p[0] <= 'z') ||
751 (p[0] >= '0' && p[0] <= '9') ||
752 strchr (extra_chars, p[0]));
755 g_warning ("type name '%s' contains invalid characters", type_name);
758 if (g_type_from_name (type_name))
760 g_warning ("cannot register existing type '%s'", type_name);
768 check_derivation_I (GType parent_type,
769 const gchar *type_name)
772 GTypeFundamentalInfo* finfo;
774 pnode = lookup_type_node_I (parent_type);
777 g_warning ("cannot derive type '%s' from invalid parent type '%s'",
779 type_descriptive_name_I (parent_type));
782 finfo = type_node_fundamental_info_I (pnode);
783 /* ensure flat derivability */
784 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
786 g_warning ("cannot derive '%s' from non-derivable parent type '%s'",
791 /* ensure deep derivability */
792 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
793 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
795 g_warning ("cannot derive '%s' from non-fundamental parent type '%s'",
805 check_collect_format_I (const gchar *collect_format)
807 const gchar *p = collect_format;
808 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
809 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
810 G_VALUE_COLLECT_POINTER, 0 };
813 if (!strchr (valid_format, *p++))
815 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
819 check_value_table_I (const gchar *type_name,
820 const GTypeValueTable *value_table)
824 else if (value_table->value_init == NULL)
826 if (value_table->value_free || value_table->value_copy ||
827 value_table->value_peek_pointer ||
828 value_table->collect_format || value_table->collect_value ||
829 value_table->lcopy_format || value_table->lcopy_value)
830 g_warning ("cannot handle uninitializable values of type '%s'",
834 else /* value_table->value_init != NULL */
836 if (!value_table->value_free)
839 * g_warning ("missing 'value_free()' for type '%s'", type_name);
843 if (!value_table->value_copy)
845 g_warning ("missing 'value_copy()' for type '%s'", type_name);
848 if ((value_table->collect_format || value_table->collect_value) &&
849 (!value_table->collect_format || !value_table->collect_value))
851 g_warning ("one of 'collect_format' and 'collect_value()' is unspecified for type '%s'",
855 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
857 g_warning ("the '%s' specification for type '%s' is too long or invalid",
862 if ((value_table->lcopy_format || value_table->lcopy_value) &&
863 (!value_table->lcopy_format || !value_table->lcopy_value))
865 g_warning ("one of 'lcopy_format' and 'lcopy_value()' is unspecified for type '%s'",
869 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
871 g_warning ("the '%s' specification for type '%s' is too long or invalid",
881 check_type_info_I (TypeNode *pnode,
883 const gchar *type_name,
884 const GTypeInfo *info)
886 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
887 gboolean is_interface = ftype == G_TYPE_INTERFACE;
889 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
891 /* check instance members */
892 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
893 (info->instance_size || info->n_preallocs || info->instance_init))
896 g_warning ("cannot instantiate '%s', derived from non-instantiatable parent type '%s'",
900 g_warning ("cannot instantiate '%s' as non-instantiatable fundamental",
904 /* check class & interface members */
905 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
906 (info->class_init || info->class_finalize || info->class_data ||
907 info->class_size || info->base_init || info->base_finalize))
910 g_warning ("cannot create class for '%s', derived from non-classed parent type '%s'",
914 g_warning ("cannot create class for '%s' as non-classed fundamental",
918 /* check interface size */
919 if (is_interface && info->class_size < sizeof (GTypeInterface))
921 g_warning ("specified interface size for type '%s' is smaller than 'GTypeInterface' size",
925 /* check class size */
926 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
928 if (info->class_size < sizeof (GTypeClass))
930 g_warning ("specified class size for type '%s' is smaller than 'GTypeClass' size",
934 if (pnode && info->class_size < pnode->data->class.class_size)
936 g_warning ("specified class size for type '%s' is smaller "
937 "than the parent type's '%s' class size",
943 /* check instance size */
944 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
946 if (info->instance_size < sizeof (GTypeInstance))
948 g_warning ("specified instance size for type '%s' is smaller than 'GTypeInstance' size",
952 if (pnode && info->instance_size < pnode->data->instance.instance_size)
954 g_warning ("specified instance size for type '%s' is smaller "
955 "than the parent type's '%s' instance size",
966 find_conforming_child_type_L (TypeNode *pnode,
969 TypeNode *node = NULL;
972 if (type_lookup_iface_entry_L (pnode, iface))
975 for (i = 0; i < pnode->n_children && !node; i++)
976 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
982 check_add_interface_L (GType instance_type,
985 TypeNode *node = lookup_type_node_I (instance_type);
986 TypeNode *iface = lookup_type_node_I (iface_type);
989 GType *prerequisites;
993 if (!node || !node->is_instantiatable)
995 g_warning ("cannot add interfaces to invalid (non-instantiatable) type '%s'",
996 type_descriptive_name_I (instance_type));
999 if (!iface || !NODE_IS_IFACE (iface))
1001 g_warning ("cannot add invalid (non-interface) type '%s' to type '%s'",
1002 type_descriptive_name_I (iface_type),
1006 if (node->data && node->data->class.class)
1008 g_warning ("attempting to add an interface (%s) to class (%s) after class_init",
1009 NODE_NAME (iface), NODE_NAME (node));
1011 /* See https://bugzilla.gnome.org/show_bug.cgi?id=697229,
1012 * https://bugzilla.gnome.org/show_bug.cgi?id=687659
1014 if (!g_str_has_prefix (NODE_NAME (node), "gtkmm__CustomObject_") && !strstr (NODE_NAME (node), "_gtksharp_"))
1017 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
1018 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
1020 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
1021 g_warning ("cannot add sub-interface '%s' to type '%s' which does not conform to super-interface '%s'",
1027 /* allow overriding of interface type introduced for parent type */
1028 entry = type_lookup_iface_entry_L (node, iface);
1029 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1031 /* ok, we do conform to this interface already, but the interface vtable was not
1032 * yet intialized, and we just conform to the interface because it got added to
1033 * one of our parents. so we allow overriding of holder info here.
1037 /* check whether one of our children already conforms (or whether the interface
1038 * got added to this node already)
1040 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1043 g_warning ("cannot add interface type '%s' to type '%s', since type '%s' already conforms to interface",
1049 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1050 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1052 tnode = lookup_type_node_I (prerequisites[i]);
1053 if (!type_node_is_a_L (node, tnode))
1055 g_warning ("cannot add interface type '%s' to type '%s' which does not conform to prerequisite '%s'",
1066 check_interface_info_I (TypeNode *iface,
1067 GType instance_type,
1068 const GInterfaceInfo *info)
1070 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1072 g_warning ("interface type '%s' for type '%s' comes without initializer",
1074 type_descriptive_name_I (instance_type));
1081 /* --- type info (type node data) --- */
1083 type_data_make_W (TypeNode *node,
1084 const GTypeInfo *info,
1085 const GTypeValueTable *value_table)
1088 GTypeValueTable *vtable = NULL;
1089 guint vtable_size = 0;
1091 g_assert (node->data == NULL && info != NULL);
1095 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1098 vtable = pnode->data->common.value_table;
1101 static const GTypeValueTable zero_vtable = { NULL, };
1103 value_table = &zero_vtable;
1108 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1109 vtable_size = sizeof (GTypeValueTable);
1110 if (value_table->collect_format)
1111 vtable_size += strlen (value_table->collect_format);
1112 if (value_table->lcopy_format)
1113 vtable_size += strlen (value_table->lcopy_format);
1117 if (node->is_instantiatable) /* careful, is_instantiatable is also is_classed */
1119 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1121 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1123 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1124 data->instance.class_size = info->class_size;
1125 data->instance.class_init_base = info->base_init;
1126 data->instance.class_finalize_base = info->base_finalize;
1127 data->instance.class_init = info->class_init;
1128 data->instance.class_finalize = info->class_finalize;
1129 data->instance.class_data = info->class_data;
1130 data->instance.class = NULL;
1131 data->instance.init_state = UNINITIALIZED;
1132 data->instance.instance_size = info->instance_size;
1133 /* We'll set the final value for data->instance.private size
1134 * after the parent class has been initialized
1136 data->instance.private_size = 0;
1137 data->instance.class_private_size = 0;
1139 data->instance.class_private_size = pnode->data->instance.class_private_size;
1140 #ifdef DISABLE_MEM_POOLS
1141 data->instance.n_preallocs = 0;
1142 #else /* !DISABLE_MEM_POOLS */
1143 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1144 #endif /* !DISABLE_MEM_POOLS */
1145 data->instance.instance_init = info->instance_init;
1147 else if (node->is_classed) /* only classed */
1149 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1151 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1153 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1154 data->class.class_size = info->class_size;
1155 data->class.class_init_base = info->base_init;
1156 data->class.class_finalize_base = info->base_finalize;
1157 data->class.class_init = info->class_init;
1158 data->class.class_finalize = info->class_finalize;
1159 data->class.class_data = info->class_data;
1160 data->class.class = NULL;
1161 data->class.class_private_size = 0;
1163 data->class.class_private_size = pnode->data->class.class_private_size;
1164 data->class.init_state = UNINITIALIZED;
1166 else if (NODE_IS_IFACE (node))
1168 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1170 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1171 data->iface.vtable_size = info->class_size;
1172 data->iface.vtable_init_base = info->base_init;
1173 data->iface.vtable_finalize_base = info->base_finalize;
1174 data->iface.dflt_init = info->class_init;
1175 data->iface.dflt_finalize = info->class_finalize;
1176 data->iface.dflt_data = info->class_data;
1177 data->iface.dflt_vtable = NULL;
1179 else if (NODE_IS_BOXED (node))
1181 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1183 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1187 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1189 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1198 /* we allocate the vtable and its strings together with the type data, so
1199 * children can take over their parent's vtable pointer, and we don't
1200 * need to worry freeing it or not when the child data is destroyed
1202 *vtable = *value_table;
1203 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1205 vtable->collect_format = p;
1206 if (value_table->collect_format)
1208 strcat (p, value_table->collect_format);
1209 p += strlen (value_table->collect_format);
1213 vtable->lcopy_format = p;
1214 if (value_table->lcopy_format)
1215 strcat (p, value_table->lcopy_format);
1217 node->data->common.value_table = vtable;
1218 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1219 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1220 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1222 g_assert (node->data->common.value_table != NULL); /* paranoid */
1224 g_atomic_int_set ((int *) &node->ref_count, 1);
1228 type_data_ref_Wm (TypeNode *node)
1232 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1234 GTypeValueTable tmp_value_table;
1236 g_assert (node->plugin != NULL);
1240 type_data_ref_Wm (pnode);
1242 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1245 memset (&tmp_info, 0, sizeof (tmp_info));
1246 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1248 G_WRITE_UNLOCK (&type_rw_lock);
1249 g_type_plugin_use (node->plugin);
1250 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1251 G_WRITE_LOCK (&type_rw_lock);
1253 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1255 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1256 type_data_make_W (node, &tmp_info,
1257 check_value_table_I (NODE_NAME (node),
1258 &tmp_value_table) ? &tmp_value_table : NULL);
1262 g_assert (NODE_REFCOUNT (node) > 0);
1264 g_atomic_int_inc ((int *) &node->ref_count);
1268 static inline gboolean
1269 type_data_ref_U (TypeNode *node)
1274 current = NODE_REFCOUNT (node);
1278 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1284 iface_node_has_available_offset_L (TypeNode *iface_node,
1290 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1291 if (offsets == NULL)
1294 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1297 if (offsets[offset] == 0 ||
1298 offsets[offset] == for_index+1)
1305 find_free_iface_offset_L (IFaceEntries *entries)
1308 TypeNode *iface_node;
1313 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1318 for (i = 0; i < n_entries; i++)
1320 entry = &entries->entry[i];
1321 iface_node = lookup_type_node_I (entry->iface_type);
1323 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1327 while (i != n_entries);
1333 iface_node_set_offset_L (TypeNode *iface_node,
1337 guint8 *offsets, *old_offsets;
1338 int new_size, old_size;
1341 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1342 if (old_offsets == NULL)
1346 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1347 if (offset < old_size &&
1348 old_offsets[offset] == index + 1)
1349 return; /* Already set to this index, return */
1351 new_size = MAX (old_size, offset + 1);
1353 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1354 0, new_size - old_size);
1356 /* Mark new area as unused */
1357 for (i = old_size; i < new_size; i++)
1360 offsets[offset] = index + 1;
1362 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1366 type_node_add_iface_entry_W (TypeNode *node,
1368 IFaceEntry *parent_entry)
1370 IFaceEntries *entries;
1372 TypeNode *iface_node;
1376 g_assert (node->is_instantiatable);
1378 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1379 if (entries != NULL)
1381 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1383 g_assert (num_entries < MAX_N_INTERFACES);
1385 for (i = 0; i < num_entries; i++)
1387 entry = &entries->entry[i];
1388 if (entry->iface_type == iface_type)
1390 /* this can happen in two cases:
1391 * - our parent type already conformed to iface_type and node
1392 * got its own holder info. here, our children already have
1393 * entries and NULL vtables, since this will only work for
1394 * uninitialized classes.
1395 * - an interface type is added to an ancestor after it was
1396 * added to a child type.
1399 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1402 /* sick, interface is added to ancestor *after* child type;
1403 * nothing todo, the entry and our children were already setup correctly
1411 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1412 IFACE_ENTRIES_HEADER_SIZE,
1413 sizeof (IFaceEntry));
1414 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1415 i = num_entries - 1;
1417 entries->offset_index = 0;
1418 entries->entry[i].iface_type = iface_type;
1419 entries->entry[i].vtable = NULL;
1420 entries->entry[i].init_state = UNINITIALIZED;
1424 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1426 entries->entry[i].init_state = INITIALIZED;
1427 entries->entry[i].vtable = parent_entry->vtable;
1431 /* Update offsets in iface */
1432 iface_node = lookup_type_node_I (iface_type);
1434 if (iface_node_has_available_offset_L (iface_node,
1435 entries->offset_index,
1438 iface_node_set_offset_L (iface_node,
1439 entries->offset_index, i);
1443 entries->offset_index =
1444 find_free_iface_offset_L (entries);
1445 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1447 entry = &entries->entry[j];
1449 lookup_type_node_I (entry->iface_type);
1450 iface_node_set_offset_L (iface_node,
1451 entries->offset_index, j);
1455 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1459 for (i = 0; i < node->n_children; i++)
1460 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1465 type_add_interface_Wm (TypeNode *node,
1467 const GInterfaceInfo *info,
1468 GTypePlugin *plugin)
1470 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1474 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1476 iholder->next = iface_node_get_holders_L (iface);
1477 iface_node_set_holders_W (iface, iholder);
1478 iholder->instance_type = NODE_TYPE (node);
1479 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1480 iholder->plugin = plugin;
1482 /* create an iface entry for this type */
1483 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1485 /* if the class is already (partly) initialized, we may need to base
1486 * initalize and/or initialize the new interface.
1490 InitState class_state = node->data->class.init_state;
1492 if (class_state >= BASE_IFACE_INIT)
1493 type_iface_vtable_base_init_Wm (iface, node);
1495 if (class_state >= IFACE_INIT)
1496 type_iface_vtable_iface_init_Wm (iface, node);
1499 /* create iface entries for children of this type */
1500 entry = type_lookup_iface_entry_L (node, iface);
1501 for (i = 0; i < node->n_children; i++)
1502 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1506 type_iface_add_prerequisite_W (TypeNode *iface,
1507 TypeNode *prerequisite_node)
1509 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1510 GType *prerequisites, *dependants;
1511 guint n_dependants, i;
1513 g_assert (NODE_IS_IFACE (iface) &&
1514 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1515 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1517 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1518 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1519 if (prerequisites[i] == prerequisite_type)
1520 return; /* we already have that prerequisiste */
1521 else if (prerequisites[i] > prerequisite_type)
1523 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1524 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1525 IFACE_NODE_PREREQUISITES (iface),
1526 IFACE_NODE_N_PREREQUISITES (iface));
1527 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1528 memmove (prerequisites + i + 1, prerequisites + i,
1529 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1530 prerequisites[i] = prerequisite_type;
1532 /* we want to get notified when prerequisites get added to prerequisite_node */
1533 if (NODE_IS_IFACE (prerequisite_node))
1535 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1536 n_dependants = dependants ? dependants[0] : 0;
1538 dependants = g_renew (GType, dependants, n_dependants + 1);
1539 dependants[n_dependants] = NODE_TYPE (iface);
1540 dependants[0] = n_dependants;
1541 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1544 /* we need to notify all dependants */
1545 dependants = iface_node_get_dependants_array_L (iface);
1546 n_dependants = dependants ? dependants[0] : 0;
1547 for (i = 1; i <= n_dependants; i++)
1548 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1552 * g_type_interface_add_prerequisite:
1553 * @interface_type: #GType value of an interface type
1554 * @prerequisite_type: #GType value of an interface or instantiatable type
1556 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1557 * This means that any type implementing @interface_type must also implement
1558 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1559 * interface derivation (which GType doesn't support). An interface can have
1560 * at most one instantiatable prerequisite type.
1563 g_type_interface_add_prerequisite (GType interface_type,
1564 GType prerequisite_type)
1566 TypeNode *iface, *prerequisite_node;
1567 IFaceHolder *holders;
1569 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1570 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1571 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1573 iface = lookup_type_node_I (interface_type);
1574 prerequisite_node = lookup_type_node_I (prerequisite_type);
1575 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1577 g_warning ("interface type '%s' or prerequisite type '%s' invalid",
1578 type_descriptive_name_I (interface_type),
1579 type_descriptive_name_I (prerequisite_type));
1582 G_WRITE_LOCK (&type_rw_lock);
1583 holders = iface_node_get_holders_L (iface);
1586 G_WRITE_UNLOCK (&type_rw_lock);
1587 g_warning ("unable to add prerequisite '%s' to interface '%s' which is already in use for '%s'",
1588 type_descriptive_name_I (prerequisite_type),
1589 type_descriptive_name_I (interface_type),
1590 type_descriptive_name_I (holders->instance_type));
1593 if (prerequisite_node->is_instantiatable)
1597 /* can have at most one publicly installable instantiatable prerequisite */
1598 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1600 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1602 if (prnode->is_instantiatable)
1604 G_WRITE_UNLOCK (&type_rw_lock);
1605 g_warning ("adding prerequisite '%s' to interface '%s' conflicts with existing prerequisite '%s'",
1606 type_descriptive_name_I (prerequisite_type),
1607 type_descriptive_name_I (interface_type),
1608 type_descriptive_name_I (NODE_TYPE (prnode)));
1613 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1614 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1615 G_WRITE_UNLOCK (&type_rw_lock);
1617 else if (NODE_IS_IFACE (prerequisite_node))
1619 GType *prerequisites;
1622 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1623 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1624 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1625 type_iface_add_prerequisite_W (iface, prerequisite_node);
1626 G_WRITE_UNLOCK (&type_rw_lock);
1630 G_WRITE_UNLOCK (&type_rw_lock);
1631 g_warning ("prerequisite '%s' for interface '%s' is neither instantiatable nor interface",
1632 type_descriptive_name_I (prerequisite_type),
1633 type_descriptive_name_I (interface_type));
1638 * g_type_interface_prerequisites:
1639 * @interface_type: an interface type
1640 * @n_prerequisites: (out) (allow-none): location to return the number
1641 * of prerequisites, or %NULL
1643 * Returns the prerequisites of an interfaces type.
1647 * Returns: (array length=n_prerequisites) (transfer full): a
1648 * newly-allocated zero-terminated array of #GType containing
1649 * the prerequisites of @interface_type
1652 g_type_interface_prerequisites (GType interface_type,
1653 guint *n_prerequisites)
1657 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1659 iface = lookup_type_node_I (interface_type);
1663 TypeNode *inode = NULL;
1666 G_READ_LOCK (&type_rw_lock);
1667 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1668 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1670 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1671 TypeNode *node = lookup_type_node_I (prerequisite);
1672 if (node->is_instantiatable)
1674 if (!inode || type_node_is_a_L (node, inode))
1678 types[n++] = NODE_TYPE (node);
1681 types[n++] = NODE_TYPE (inode);
1683 if (n_prerequisites)
1684 *n_prerequisites = n;
1685 G_READ_UNLOCK (&type_rw_lock);
1691 if (n_prerequisites)
1692 *n_prerequisites = 0;
1700 type_iface_peek_holder_L (TypeNode *iface,
1701 GType instance_type)
1703 IFaceHolder *iholder;
1705 g_assert (NODE_IS_IFACE (iface));
1707 iholder = iface_node_get_holders_L (iface);
1708 while (iholder && iholder->instance_type != instance_type)
1709 iholder = iholder->next;
1714 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1715 GType instance_type,
1718 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1720 if (iholder && !iholder->info && need_info)
1722 GInterfaceInfo tmp_info;
1724 g_assert (iholder->plugin != NULL);
1726 type_data_ref_Wm (iface);
1728 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1730 memset (&tmp_info, 0, sizeof (tmp_info));
1732 G_WRITE_UNLOCK (&type_rw_lock);
1733 g_type_plugin_use (iholder->plugin);
1734 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1735 G_WRITE_LOCK (&type_rw_lock);
1737 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1739 check_interface_info_I (iface, instance_type, &tmp_info);
1740 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1743 return iholder; /* we don't modify write lock upon returning NULL */
1747 type_iface_blow_holder_info_Wm (TypeNode *iface,
1748 GType instance_type)
1750 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1752 g_assert (NODE_IS_IFACE (iface));
1754 while (iholder->instance_type != instance_type)
1755 iholder = iholder->next;
1757 if (iholder->info && iholder->plugin)
1759 g_free (iholder->info);
1760 iholder->info = NULL;
1762 G_WRITE_UNLOCK (&type_rw_lock);
1763 g_type_plugin_unuse (iholder->plugin);
1764 type_data_unref_U (iface, FALSE);
1765 G_WRITE_LOCK (&type_rw_lock);
1770 * g_type_create_instance: (skip)
1771 * @type: an instantiatable type to create an instance for
1773 * Creates and initializes an instance of @type if @type is valid and
1774 * can be instantiated. The type system only performs basic allocation
1775 * and structure setups for instances: actual instance creation should
1776 * happen through functions supplied by the type's fundamental type
1777 * implementation. So use of g_type_create_instance() is reserved for
1778 * implementators of fundamental types only. E.g. instances of the
1779 * #GObject hierarchy should be created via g_object_new() and never
1780 * directly through g_type_create_instance() which doesn't handle things
1781 * like singleton objects or object construction.
1783 * The extended members of the returned instance are guaranteed to be filled
1786 * Note: Do not use this function, unless you're implementing a
1787 * fundamental type. Also language bindings should not use this
1788 * function, but g_object_new() instead.
1790 * Returns: an allocated and initialized instance, subject to further
1791 * treatment by the fundamental type implementation
1794 g_type_create_instance (GType type)
1797 GTypeInstance *instance;
1804 node = lookup_type_node_I (type);
1805 if (!node || !node->is_instantiatable)
1807 g_error ("cannot create new instance of invalid (non-instantiatable) type '%s'",
1808 type_descriptive_name_I (type));
1810 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1811 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1813 g_error ("cannot create instance of abstract (non-instantiatable) type '%s'",
1814 type_descriptive_name_I (type));
1817 class = g_type_class_ref (type);
1819 /* We allocate the 'private' areas before the normal instance data, in
1820 * reverse order. This allows the private area of a particular class
1821 * to always be at a constant relative address to the instance data.
1822 * If we stored the private data after the instance data this would
1823 * not be the case (since a subclass that added more instance
1824 * variables would push the private data further along).
1826 * This presents problems for valgrindability, of course, so we do a
1827 * workaround for that case. We identify the start of the object to
1828 * valgrind as an allocated block (so that pointers to objects show up
1829 * as 'reachable' instead of 'possibly lost'). We then add an extra
1830 * pointer at the end of the object, after all instance data, back to
1831 * the start of the private area so that it is also recorded as
1832 * reachable. We also add extra private space at the start because
1833 * valgrind doesn't seem to like us claiming to have allocated an
1834 * address that it saw allocated by malloc().
1836 private_size = node->data->instance.private_size;
1837 ivar_size = node->data->instance.instance_size;
1839 if (private_size && RUNNING_ON_VALGRIND)
1841 private_size += ALIGN_STRUCT (1);
1843 /* Allocate one extra pointer size... */
1844 allocated = g_slice_alloc0 (private_size + ivar_size + sizeof (gpointer));
1845 /* ... and point it back to the start of the private data. */
1846 *(gpointer *) (allocated + private_size + ivar_size) = allocated + ALIGN_STRUCT (1);
1848 /* Tell valgrind that it should treat the object itself as such */
1849 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, ivar_size + sizeof (gpointer), 0, TRUE);
1850 VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
1853 allocated = g_slice_alloc0 (private_size + ivar_size);
1855 instance = (GTypeInstance *) (allocated + private_size);
1857 for (i = node->n_supers; i > 0; i--)
1861 pnode = lookup_type_node_I (node->supers[i]);
1862 if (pnode->data->instance.instance_init)
1864 instance->g_class = pnode->data->instance.class;
1865 pnode->data->instance.instance_init (instance, class);
1869 instance->g_class = class;
1870 if (node->data->instance.instance_init)
1871 node->data->instance.instance_init (instance, class);
1873 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1879 * g_type_free_instance:
1880 * @instance: an instance of a type
1882 * Frees an instance of a type, returning it to the instance pool for
1883 * the type, if there is one.
1885 * Like g_type_create_instance(), this function is reserved for
1886 * implementors of fundamental types.
1889 g_type_free_instance (GTypeInstance *instance)
1897 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1899 class = instance->g_class;
1900 node = lookup_type_node_I (class->g_type);
1901 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1903 g_warning ("cannot free instance of invalid (non-instantiatable) type '%s'",
1904 type_descriptive_name_I (class->g_type));
1907 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1908 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1910 g_warning ("cannot free instance of abstract (non-instantiatable) type '%s'",
1915 instance->g_class = NULL;
1916 private_size = node->data->instance.private_size;
1917 ivar_size = node->data->instance.instance_size;
1918 allocated = ((gchar *) instance) - private_size;
1920 #ifdef G_ENABLE_DEBUG
1921 memset (allocated, 0xaa, ivar_size + private_size);
1924 /* See comment in g_type_create_instance() about what's going on here.
1925 * We're basically unwinding what we put into motion there.
1927 if (private_size && RUNNING_ON_VALGRIND)
1929 private_size += ALIGN_STRUCT (1);
1930 allocated -= ALIGN_STRUCT (1);
1932 /* Clear out the extra pointer... */
1933 *(gpointer *) (allocated + private_size + ivar_size) = NULL;
1934 /* ... and ensure we include it in the size we free. */
1935 g_slice_free1 (private_size + ivar_size + sizeof (gpointer), allocated);
1937 VALGRIND_FREELIKE_BLOCK (allocated + ALIGN_STRUCT (1), 0);
1938 VALGRIND_FREELIKE_BLOCK (instance, 0);
1941 g_slice_free1 (private_size + ivar_size, allocated);
1943 g_type_class_unref (class);
1947 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1949 g_assert (iface->data);
1951 if (!iface->data->iface.dflt_vtable)
1953 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1954 iface->data->iface.dflt_vtable = vtable;
1955 vtable->g_type = NODE_TYPE (iface);
1956 vtable->g_instance_type = 0;
1957 if (iface->data->iface.vtable_init_base ||
1958 iface->data->iface.dflt_init)
1960 G_WRITE_UNLOCK (&type_rw_lock);
1961 if (iface->data->iface.vtable_init_base)
1962 iface->data->iface.vtable_init_base (vtable);
1963 if (iface->data->iface.dflt_init)
1964 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1965 G_WRITE_LOCK (&type_rw_lock);
1971 /* This is called to allocate and do the first part of initializing
1972 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1974 * A FALSE return indicates that we didn't find an init function for
1975 * this type/iface pair, so the vtable from the parent type should
1976 * be used. Note that the write lock is not modified upon a FALSE
1980 type_iface_vtable_base_init_Wm (TypeNode *iface,
1984 IFaceHolder *iholder;
1985 GTypeInterface *vtable = NULL;
1988 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1989 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1991 return FALSE; /* we don't modify write lock upon FALSE */
1993 type_iface_ensure_dflt_vtable_Wm (iface);
1995 entry = type_lookup_iface_entry_L (node, iface);
1997 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1999 entry->init_state = IFACE_INIT;
2001 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2002 if (pnode) /* want to copy over parent iface contents */
2004 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2007 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2010 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2011 entry->vtable = vtable;
2012 vtable->g_type = NODE_TYPE (iface);
2013 vtable->g_instance_type = NODE_TYPE (node);
2015 if (iface->data->iface.vtable_init_base)
2017 G_WRITE_UNLOCK (&type_rw_lock);
2018 iface->data->iface.vtable_init_base (vtable);
2019 G_WRITE_LOCK (&type_rw_lock);
2021 return TRUE; /* initialized the vtable */
2024 /* Finishes what type_iface_vtable_base_init_Wm started by
2025 * calling the interface init function.
2026 * this function may only be called for types with their
2027 * own interface holder info, i.e. types for which
2028 * g_type_add_interface*() was called and not children thereof.
2031 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2034 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2035 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2036 GTypeInterface *vtable = NULL;
2039 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2040 g_assert (iface->data && entry && iholder && iholder->info);
2041 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2043 entry->init_state = INITIALIZED;
2045 vtable = entry->vtable;
2047 if (iholder->info->interface_init)
2049 G_WRITE_UNLOCK (&type_rw_lock);
2050 if (iholder->info->interface_init)
2051 iholder->info->interface_init (vtable, iholder->info->interface_data);
2052 G_WRITE_LOCK (&type_rw_lock);
2055 for (i = 0; i < static_n_iface_check_funcs; i++)
2057 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2058 gpointer check_data = static_iface_check_funcs[i].check_data;
2060 G_WRITE_UNLOCK (&type_rw_lock);
2061 check_func (check_data, (gpointer)vtable);
2062 G_WRITE_LOCK (&type_rw_lock);
2067 type_iface_vtable_finalize_Wm (TypeNode *iface,
2069 GTypeInterface *vtable)
2071 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2072 IFaceHolder *iholder;
2074 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2075 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2077 return FALSE; /* we don't modify write lock upon FALSE */
2079 g_assert (entry && entry->vtable == vtable && iholder->info);
2081 entry->vtable = NULL;
2082 entry->init_state = UNINITIALIZED;
2083 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2085 G_WRITE_UNLOCK (&type_rw_lock);
2086 if (iholder->info->interface_finalize)
2087 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2088 if (iface->data->iface.vtable_finalize_base)
2089 iface->data->iface.vtable_finalize_base (vtable);
2090 G_WRITE_LOCK (&type_rw_lock);
2093 vtable->g_instance_type = 0;
2096 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2098 return TRUE; /* write lock modified */
2102 type_class_init_Wm (TypeNode *node,
2105 GSList *slist, *init_slist = NULL;
2107 IFaceEntries *entries;
2109 TypeNode *bnode, *pnode;
2112 /* Accessing data->class will work for instantiable types
2113 * too because ClassData is a subset of InstanceData
2115 g_assert (node->is_classed && node->data &&
2116 node->data->class.class_size &&
2117 !node->data->class.class &&
2118 node->data->class.init_state == UNINITIALIZED);
2119 if (node->data->class.class_private_size)
2120 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2122 class = g_malloc0 (node->data->class.class_size);
2123 node->data->class.class = class;
2124 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2128 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2130 memcpy (class, pclass, pnode->data->class.class_size);
2131 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);
2133 if (node->is_instantiatable)
2135 /* We need to initialize the private_size here rather than in
2136 * type_data_make_W() since the class init for the parent
2137 * class may have changed pnode->data->instance.private_size.
2139 node->data->instance.private_size = pnode->data->instance.private_size;
2142 class->g_type = NODE_TYPE (node);
2144 G_WRITE_UNLOCK (&type_rw_lock);
2146 /* stack all base class initialization functions, so we
2147 * call them in ascending order.
2149 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2150 if (bnode->data->class.class_init_base)
2151 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2152 for (slist = init_slist; slist; slist = slist->next)
2154 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2156 class_init_base (class);
2158 g_slist_free (init_slist);
2160 G_WRITE_LOCK (&type_rw_lock);
2162 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2164 /* Before we initialize the class, base initialize all interfaces, either
2165 * from parent, or through our holder info
2167 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2170 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2171 i < IFACE_ENTRIES_N_ENTRIES (entries))
2173 entry = &entries->entry[i];
2174 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2175 entry->init_state == IFACE_INIT)
2181 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2184 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2187 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2189 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2190 * doesn't modify write lock upon FALSE, so entry is still valid;
2192 g_assert (pnode != NULL);
2195 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2197 IFaceEntry *pentry = &pentries->entry[j];
2199 if (pentry->iface_type == entry->iface_type)
2201 entry->vtable = pentry->vtable;
2202 entry->init_state = INITIALIZED;
2206 g_assert (entry->vtable != NULL);
2209 /* If the write lock was released, additional interface entries might
2210 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2211 * be base-initialized when inserted, so we don't have to worry that
2212 * we might miss them. Uninitialized entries can only be moved higher
2213 * when new ones are inserted.
2218 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2220 G_WRITE_UNLOCK (&type_rw_lock);
2222 if (node->data->class.class_init)
2223 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2225 G_WRITE_LOCK (&type_rw_lock);
2227 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2229 /* finish initializing the interfaces through our holder info.
2230 * inherited interfaces are already init_state == INITIALIZED, because
2231 * they either got setup in the above base_init loop, or during
2232 * class_init from within type_add_interface_Wm() for this or
2233 * an anchestor type.
2236 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2238 entry = &entries->entry[i];
2239 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2240 entry->init_state == INITIALIZED)
2246 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2249 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2251 /* As in the loop above, additional initialized entries might be inserted
2252 * if the write lock is released, but that's harmless because the entries
2253 * we need to initialize only move higher in the list.
2258 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2262 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2265 IFaceEntries *entries;
2267 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2270 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2271 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2273 IFaceEntry *entry = &entries->entry[i];
2276 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2278 /* refetch entries, IFACES_ENTRIES might be modified */
2283 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2284 * iface vtable came from parent
2286 entry->vtable = NULL;
2287 entry->init_state = UNINITIALIZED;
2294 type_data_finalize_class_U (TypeNode *node,
2297 GTypeClass *class = cdata->class;
2300 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2302 if (cdata->class_finalize)
2303 cdata->class_finalize (class, (gpointer) cdata->class_data);
2305 /* call all base class destruction functions in descending order
2307 if (cdata->class_finalize_base)
2308 cdata->class_finalize_base (class);
2309 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2310 if (bnode->data->class.class_finalize_base)
2311 bnode->data->class.class_finalize_base (class);
2313 g_free (cdata->class);
2317 type_data_last_unref_Wm (TypeNode *node,
2320 g_return_if_fail (node != NULL && node->plugin != NULL);
2322 if (!node->data || NODE_REFCOUNT (node) == 0)
2324 g_warning ("cannot drop last reference to unreferenced type '%s'",
2329 /* call class cache hooks */
2330 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2334 G_WRITE_UNLOCK (&type_rw_lock);
2335 G_READ_LOCK (&type_rw_lock);
2336 for (i = 0; i < static_n_class_cache_funcs; i++)
2338 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2339 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2340 gboolean need_break;
2342 G_READ_UNLOCK (&type_rw_lock);
2343 need_break = cache_func (cache_data, node->data->class.class);
2344 G_READ_LOCK (&type_rw_lock);
2345 if (!node->data || NODE_REFCOUNT (node) == 0)
2346 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2350 G_READ_UNLOCK (&type_rw_lock);
2351 G_WRITE_LOCK (&type_rw_lock);
2354 /* may have been re-referenced meanwhile */
2355 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2357 GType ptype = NODE_PARENT_TYPE (node);
2360 if (node->is_instantiatable)
2362 /* destroy node->data->instance.mem_chunk */
2366 if (node->is_classed && tdata->class.class)
2368 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2369 type_data_finalize_class_ifaces_Wm (node);
2370 node->mutatable_check_cache = FALSE;
2372 G_WRITE_UNLOCK (&type_rw_lock);
2373 type_data_finalize_class_U (node, &tdata->class);
2374 G_WRITE_LOCK (&type_rw_lock);
2376 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2378 node->mutatable_check_cache = FALSE;
2380 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2382 G_WRITE_UNLOCK (&type_rw_lock);
2383 if (tdata->iface.dflt_finalize)
2384 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2385 if (tdata->iface.vtable_finalize_base)
2386 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2387 G_WRITE_LOCK (&type_rw_lock);
2389 g_free (tdata->iface.dflt_vtable);
2393 node->mutatable_check_cache = FALSE;
2397 /* freeing tdata->common.value_table and its contents is taken care of
2398 * by allocating it in one chunk with tdata
2402 G_WRITE_UNLOCK (&type_rw_lock);
2403 g_type_plugin_unuse (node->plugin);
2405 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2406 G_WRITE_LOCK (&type_rw_lock);
2411 type_data_unref_U (TypeNode *node,
2417 current = NODE_REFCOUNT (node);
2423 g_warning ("static type '%s' unreferenced too often",
2429 /* This is the last reference of a type from a plugin. We are
2430 * experimentally disabling support for unloading type
2431 * plugins, so don't allow the last ref to drop.
2436 g_assert (current > 0);
2438 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2439 G_WRITE_LOCK (&type_rw_lock);
2440 type_data_last_unref_Wm (node, uncached);
2441 G_WRITE_UNLOCK (&type_rw_lock);
2442 g_rec_mutex_unlock (&class_init_rec_mutex);
2445 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2449 * g_type_add_class_cache_func: (skip)
2450 * @cache_data: data to be passed to @cache_func
2451 * @cache_func: a #GTypeClassCacheFunc
2453 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2454 * class goes from one to zero. This can be used to prevent premature class
2455 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2456 * until one of them returns %TRUE. The functions have to check the class id
2457 * passed in to figure whether they actually want to cache the class of this
2458 * type, since all classes are routed through the same #GTypeClassCacheFunc
2462 g_type_add_class_cache_func (gpointer cache_data,
2463 GTypeClassCacheFunc cache_func)
2467 g_return_if_fail (cache_func != NULL);
2469 G_WRITE_LOCK (&type_rw_lock);
2470 i = static_n_class_cache_funcs++;
2471 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2472 static_class_cache_funcs[i].cache_data = cache_data;
2473 static_class_cache_funcs[i].cache_func = cache_func;
2474 G_WRITE_UNLOCK (&type_rw_lock);
2478 * g_type_remove_class_cache_func: (skip)
2479 * @cache_data: data that was given when adding @cache_func
2480 * @cache_func: a #GTypeClassCacheFunc
2482 * Removes a previously installed #GTypeClassCacheFunc. The cache
2483 * maintained by @cache_func has to be empty when calling
2484 * g_type_remove_class_cache_func() to avoid leaks.
2487 g_type_remove_class_cache_func (gpointer cache_data,
2488 GTypeClassCacheFunc cache_func)
2490 gboolean found_it = FALSE;
2493 g_return_if_fail (cache_func != NULL);
2495 G_WRITE_LOCK (&type_rw_lock);
2496 for (i = 0; i < static_n_class_cache_funcs; i++)
2497 if (static_class_cache_funcs[i].cache_data == cache_data &&
2498 static_class_cache_funcs[i].cache_func == cache_func)
2500 static_n_class_cache_funcs--;
2501 memmove (static_class_cache_funcs + i,
2502 static_class_cache_funcs + i + 1,
2503 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2504 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2508 G_WRITE_UNLOCK (&type_rw_lock);
2511 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2512 cache_func, cache_data);
2517 * g_type_add_interface_check: (skip)
2518 * @check_data: data to pass to @check_func
2519 * @check_func: function to be called after each interface
2522 * Adds a function to be called after an interface vtable is
2523 * initialized for any class (i.e. after the @interface_init
2524 * member of #GInterfaceInfo has been called).
2526 * This function is useful when you want to check an invariant
2527 * that depends on the interfaces of a class. For instance, the
2528 * implementation of #GObject uses this facility to check that an
2529 * object implements all of the properties that are defined on its
2535 g_type_add_interface_check (gpointer check_data,
2536 GTypeInterfaceCheckFunc check_func)
2540 g_return_if_fail (check_func != NULL);
2542 G_WRITE_LOCK (&type_rw_lock);
2543 i = static_n_iface_check_funcs++;
2544 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2545 static_iface_check_funcs[i].check_data = check_data;
2546 static_iface_check_funcs[i].check_func = check_func;
2547 G_WRITE_UNLOCK (&type_rw_lock);
2551 * g_type_remove_interface_check: (skip)
2552 * @check_data: callback data passed to g_type_add_interface_check()
2553 * @check_func: callback function passed to g_type_add_interface_check()
2555 * Removes an interface check function added with
2556 * g_type_add_interface_check().
2561 g_type_remove_interface_check (gpointer check_data,
2562 GTypeInterfaceCheckFunc check_func)
2564 gboolean found_it = FALSE;
2567 g_return_if_fail (check_func != NULL);
2569 G_WRITE_LOCK (&type_rw_lock);
2570 for (i = 0; i < static_n_iface_check_funcs; i++)
2571 if (static_iface_check_funcs[i].check_data == check_data &&
2572 static_iface_check_funcs[i].check_func == check_func)
2574 static_n_iface_check_funcs--;
2575 memmove (static_iface_check_funcs + i,
2576 static_iface_check_funcs + i + 1,
2577 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2578 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2582 G_WRITE_UNLOCK (&type_rw_lock);
2585 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2586 check_func, check_data);
2589 /* --- type registration --- */
2591 * g_type_register_fundamental:
2592 * @type_id: a predefined type identifier
2593 * @type_name: 0-terminated string used as the name of the new type
2594 * @info: #GTypeInfo structure for this type
2595 * @finfo: #GTypeFundamentalInfo structure for this type
2596 * @flags: bitwise combination of #GTypeFlags values
2598 * Registers @type_id as the predefined identifier and @type_name as the
2599 * name of a fundamental type. If @type_id is already registered, or a
2600 * type named @type_name is already registered, the behaviour is undefined.
2601 * The type system uses the information contained in the #GTypeInfo structure
2602 * pointed to by @info and the #GTypeFundamentalInfo structure pointed to by
2603 * @finfo to manage the type and its instances. The value of @flags determines
2604 * additional characteristics of the fundamental type.
2606 * Returns: the predefined type identifier
2609 g_type_register_fundamental (GType type_id,
2610 const gchar *type_name,
2611 const GTypeInfo *info,
2612 const GTypeFundamentalInfo *finfo,
2617 g_assert_type_system_initialized ();
2618 g_return_val_if_fail (type_id > 0, 0);
2619 g_return_val_if_fail (type_name != NULL, 0);
2620 g_return_val_if_fail (info != NULL, 0);
2621 g_return_val_if_fail (finfo != NULL, 0);
2623 if (!check_type_name_I (type_name))
2625 if ((type_id & TYPE_ID_MASK) ||
2626 type_id > G_TYPE_FUNDAMENTAL_MAX)
2628 g_warning ("attempt to register fundamental type '%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2633 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2634 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2636 g_warning ("cannot register instantiatable fundamental type '%s' as non-classed",
2640 if (lookup_type_node_I (type_id))
2642 g_warning ("cannot register existing fundamental type '%s' (as '%s')",
2643 type_descriptive_name_I (type_id),
2648 G_WRITE_LOCK (&type_rw_lock);
2649 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2650 type_add_flags_W (node, flags);
2652 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2653 type_data_make_W (node, info,
2654 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2655 G_WRITE_UNLOCK (&type_rw_lock);
2657 return NODE_TYPE (node);
2661 * g_type_register_static_simple: (skip)
2662 * @parent_type: type from which this type will be derived
2663 * @type_name: 0-terminated string used as the name of the new type
2664 * @class_size: size of the class structure (see #GTypeInfo)
2665 * @class_init: location of the class initialization function (see #GTypeInfo)
2666 * @instance_size: size of the instance structure (see #GTypeInfo)
2667 * @instance_init: location of the instance initialization function (see #GTypeInfo)
2668 * @flags: bitwise combination of #GTypeFlags values
2670 * Registers @type_name as the name of a new static type derived from
2671 * @parent_type. The value of @flags determines the nature (e.g.
2672 * abstract or not) of the type. It works by filling a #GTypeInfo
2673 * struct and calling g_type_register_static().
2677 * Returns: the new type identifier
2680 g_type_register_static_simple (GType parent_type,
2681 const gchar *type_name,
2683 GClassInitFunc class_init,
2684 guint instance_size,
2685 GInstanceInitFunc instance_init,
2690 /* Instances are not allowed to be larger than this. If you have a big
2691 * fixed-length array or something, point to it instead.
2693 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
2694 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
2696 info.class_size = class_size;
2697 info.base_init = NULL;
2698 info.base_finalize = NULL;
2699 info.class_init = class_init;
2700 info.class_finalize = NULL;
2701 info.class_data = NULL;
2702 info.instance_size = instance_size;
2703 info.n_preallocs = 0;
2704 info.instance_init = instance_init;
2705 info.value_table = NULL;
2707 return g_type_register_static (parent_type, type_name, &info, flags);
2711 * g_type_register_static:
2712 * @parent_type: type from which this type will be derived
2713 * @type_name: 0-terminated string used as the name of the new type
2714 * @info: #GTypeInfo structure for this type
2715 * @flags: bitwise combination of #GTypeFlags values
2717 * Registers @type_name as the name of a new static type derived from
2718 * @parent_type. The type system uses the information contained in the
2719 * #GTypeInfo structure pointed to by @info to manage the type and its
2720 * instances (if not abstract). The value of @flags determines the nature
2721 * (e.g. abstract or not) of the type.
2723 * Returns: the new type identifier
2726 g_type_register_static (GType parent_type,
2727 const gchar *type_name,
2728 const GTypeInfo *info,
2731 TypeNode *pnode, *node;
2734 g_assert_type_system_initialized ();
2735 g_return_val_if_fail (parent_type > 0, 0);
2736 g_return_val_if_fail (type_name != NULL, 0);
2737 g_return_val_if_fail (info != NULL, 0);
2739 if (!check_type_name_I (type_name) ||
2740 !check_derivation_I (parent_type, type_name))
2742 if (info->class_finalize)
2744 g_warning ("class finalizer specified for static type '%s'",
2749 pnode = lookup_type_node_I (parent_type);
2750 G_WRITE_LOCK (&type_rw_lock);
2751 type_data_ref_Wm (pnode);
2752 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2754 node = type_node_new_W (pnode, type_name, NULL);
2755 type_add_flags_W (node, flags);
2756 type = NODE_TYPE (node);
2757 type_data_make_W (node, info,
2758 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2760 G_WRITE_UNLOCK (&type_rw_lock);
2766 * g_type_register_dynamic:
2767 * @parent_type: type from which this type will be derived
2768 * @type_name: 0-terminated string used as the name of the new type
2769 * @plugin: #GTypePlugin structure to retrieve the #GTypeInfo from
2770 * @flags: bitwise combination of #GTypeFlags values
2772 * Registers @type_name as the name of a new dynamic type derived from
2773 * @parent_type. The type system uses the information contained in the
2774 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2775 * instances (if not abstract). The value of @flags determines the nature
2776 * (e.g. abstract or not) of the type.
2778 * Returns: the new type identifier or #G_TYPE_INVALID if registration failed
2781 g_type_register_dynamic (GType parent_type,
2782 const gchar *type_name,
2783 GTypePlugin *plugin,
2786 TypeNode *pnode, *node;
2789 g_assert_type_system_initialized ();
2790 g_return_val_if_fail (parent_type > 0, 0);
2791 g_return_val_if_fail (type_name != NULL, 0);
2792 g_return_val_if_fail (plugin != NULL, 0);
2794 if (!check_type_name_I (type_name) ||
2795 !check_derivation_I (parent_type, type_name) ||
2796 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2799 G_WRITE_LOCK (&type_rw_lock);
2800 pnode = lookup_type_node_I (parent_type);
2801 node = type_node_new_W (pnode, type_name, plugin);
2802 type_add_flags_W (node, flags);
2803 type = NODE_TYPE (node);
2804 G_WRITE_UNLOCK (&type_rw_lock);
2810 * g_type_add_interface_static:
2811 * @instance_type: #GType value of an instantiable type
2812 * @interface_type: #GType value of an interface type
2813 * @info: #GInterfaceInfo structure for this
2814 * (@instance_type, @interface_type) combination
2816 * Adds the static @interface_type to @instantiable_type.
2817 * The information contained in the #GInterfaceInfo structure
2818 * pointed to by @info is used to manage the relationship.
2821 g_type_add_interface_static (GType instance_type,
2822 GType interface_type,
2823 const GInterfaceInfo *info)
2825 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2826 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2827 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2829 /* we only need to lock class_init_rec_mutex if instance_type already has its
2830 * class initialized, however this function is rarely enough called to take
2831 * the simple route and always acquire class_init_rec_mutex.
2833 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2834 G_WRITE_LOCK (&type_rw_lock);
2835 if (check_add_interface_L (instance_type, interface_type))
2837 TypeNode *node = lookup_type_node_I (instance_type);
2838 TypeNode *iface = lookup_type_node_I (interface_type);
2839 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2840 type_add_interface_Wm (node, iface, info, NULL);
2842 G_WRITE_UNLOCK (&type_rw_lock);
2843 g_rec_mutex_unlock (&class_init_rec_mutex);
2847 * g_type_add_interface_dynamic:
2848 * @instance_type: #GType value of an instantiable type
2849 * @interface_type: #GType value of an interface type
2850 * @plugin: #GTypePlugin structure to retrieve the #GInterfaceInfo from
2852 * Adds the dynamic @interface_type to @instantiable_type. The information
2853 * contained in the #GTypePlugin structure pointed to by @plugin
2854 * is used to manage the relationship.
2857 g_type_add_interface_dynamic (GType instance_type,
2858 GType interface_type,
2859 GTypePlugin *plugin)
2862 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2863 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2864 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2866 node = lookup_type_node_I (instance_type);
2867 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2870 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2871 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2872 G_WRITE_LOCK (&type_rw_lock);
2873 if (check_add_interface_L (instance_type, interface_type))
2875 TypeNode *iface = lookup_type_node_I (interface_type);
2876 type_add_interface_Wm (node, iface, NULL, plugin);
2878 G_WRITE_UNLOCK (&type_rw_lock);
2879 g_rec_mutex_unlock (&class_init_rec_mutex);
2883 /* --- public API functions --- */
2886 * @type: type ID of a classed type
2888 * Increments the reference count of the class structure belonging to
2889 * @type. This function will demand-create the class if it doesn't
2892 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
2893 * structure for the given type ID
2896 g_type_class_ref (GType type)
2903 /* optimize for common code path */
2904 node = lookup_type_node_I (type);
2905 if (!node || !node->is_classed)
2907 g_warning ("cannot retrieve class for invalid (unclassed) type '%s'",
2908 type_descriptive_name_I (type));
2912 if (G_LIKELY (type_data_ref_U (node)))
2914 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2915 return node->data->class.class;
2921 /* here, we either have node->data->class.class == NULL, or a recursive
2922 * call to g_type_class_ref() with a partly initialized class, or
2923 * node->data->class.init_state == INITIALIZED, because any
2924 * concurrently running initialization was guarded by class_init_rec_mutex.
2926 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2928 /* we need an initialized parent class for initializing derived classes */
2929 ptype = NODE_PARENT_TYPE (node);
2930 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2932 G_WRITE_LOCK (&type_rw_lock);
2935 type_data_ref_Wm (node);
2937 if (!node->data->class.class) /* class uninitialized */
2938 type_class_init_Wm (node, pclass);
2940 G_WRITE_UNLOCK (&type_rw_lock);
2943 g_type_class_unref (pclass);
2945 g_rec_mutex_unlock (&class_init_rec_mutex);
2947 return node->data->class.class;
2951 * g_type_class_unref:
2952 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2954 * Decrements the reference count of the class structure being passed in.
2955 * Once the last reference count of a class has been released, classes
2956 * may be finalized by the type system, so further dereferencing of a
2957 * class pointer after g_type_class_unref() are invalid.
2960 g_type_class_unref (gpointer g_class)
2963 GTypeClass *class = g_class;
2965 g_return_if_fail (g_class != NULL);
2967 node = lookup_type_node_I (class->g_type);
2968 if (node && node->is_classed && NODE_REFCOUNT (node))
2969 type_data_unref_U (node, FALSE);
2971 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
2972 type_descriptive_name_I (class->g_type));
2976 * g_type_class_unref_uncached: (skip)
2977 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2979 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2980 * implementations. It unreferences a class without consulting the chain
2981 * of #GTypeClassCacheFuncs, avoiding the recursion which would occur
2985 g_type_class_unref_uncached (gpointer g_class)
2988 GTypeClass *class = g_class;
2990 g_return_if_fail (g_class != NULL);
2992 node = lookup_type_node_I (class->g_type);
2993 if (node && node->is_classed && NODE_REFCOUNT (node))
2994 type_data_unref_U (node, TRUE);
2996 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
2997 type_descriptive_name_I (class->g_type));
3001 * g_type_class_peek:
3002 * @type: type ID of a classed type
3004 * This function is essentially the same as g_type_class_ref(),
3005 * except that the classes reference count isn't incremented.
3006 * As a consequence, this function may return %NULL if the class
3007 * of the type passed in does not currently exist (hasn't been
3008 * referenced before).
3010 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3011 * structure for the given type ID or %NULL if the class does not
3015 g_type_class_peek (GType type)
3020 node = lookup_type_node_I (type);
3021 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3022 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3023 /* ref_count _may_ be 0 */
3024 class = node->data->class.class;
3032 * g_type_class_peek_static:
3033 * @type: type ID of a classed type
3035 * A more efficient version of g_type_class_peek() which works only for
3038 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3039 * structure for the given type ID or %NULL if the class does not
3040 * currently exist or is dynamically loaded
3045 g_type_class_peek_static (GType type)
3050 node = lookup_type_node_I (type);
3051 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3052 /* peek only static types: */ node->plugin == NULL &&
3053 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3054 /* ref_count _may_ be 0 */
3055 class = node->data->class.class;
3063 * g_type_class_peek_parent:
3064 * @g_class: (type GObject.TypeClass): the #GTypeClass structure to
3065 * retrieve the parent class for
3067 * This is a convenience function often needed in class initializers.
3068 * It returns the class structure of the immediate parent type of the
3069 * class passed in. Since derived classes hold a reference count on
3070 * their parent classes as long as they are instantiated, the returned
3071 * class will always exist.
3073 * This function is essentially equivalent to:
3074 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)))
3076 * Returns: (type GObject.TypeClass) (transfer none): the parent class
3080 g_type_class_peek_parent (gpointer g_class)
3083 gpointer class = NULL;
3085 g_return_val_if_fail (g_class != NULL, NULL);
3087 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3088 /* We used to acquire a read lock here. That is not necessary, since
3089 * parent->data->class.class is constant as long as the derived class
3092 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3094 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3095 class = node->data->class.class;
3097 else if (NODE_PARENT_TYPE (node))
3098 g_warning (G_STRLOC ": invalid class pointer '%p'", g_class);
3104 * g_type_interface_peek:
3105 * @instance_class: (type GObject.TypeClass): a #GTypeClass structure
3106 * @iface_type: an interface ID which this class conforms to
3108 * Returns the #GTypeInterface structure of an interface to which the
3109 * passed in class conforms.
3111 * Returns: (type GObject.TypeInterface) (transfer none): the #GTypeInterface
3112 * structure of @iface_type if implemented by @instance_class, %NULL
3116 g_type_interface_peek (gpointer instance_class,
3121 gpointer vtable = NULL;
3122 GTypeClass *class = instance_class;
3124 g_return_val_if_fail (instance_class != NULL, NULL);
3126 node = lookup_type_node_I (class->g_type);
3127 iface = lookup_type_node_I (iface_type);
3128 if (node && node->is_instantiatable && iface)
3129 type_lookup_iface_vtable_I (node, iface, &vtable);
3131 g_warning (G_STRLOC ": invalid class pointer '%p'", class);
3137 * g_type_interface_peek_parent:
3138 * @g_iface: (type GObject.TypeInterface): a #GTypeInterface structure
3140 * Returns the corresponding #GTypeInterface structure of the parent type
3141 * of the instance type to which @g_iface belongs. This is useful when
3142 * deriving the implementation of an interface from the parent type and
3143 * then possibly overriding some methods.
3145 * Returns: (transfer none) (type GObject.TypeInterface): the
3146 * corresponding #GTypeInterface structure of the parent type of the
3147 * instance type to which @g_iface belongs, or %NULL if the parent
3148 * type doesn't conform to the interface
3151 g_type_interface_peek_parent (gpointer g_iface)
3155 gpointer vtable = NULL;
3156 GTypeInterface *iface_class = g_iface;
3158 g_return_val_if_fail (g_iface != NULL, NULL);
3160 iface = lookup_type_node_I (iface_class->g_type);
3161 node = lookup_type_node_I (iface_class->g_instance_type);
3163 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3164 if (node && node->is_instantiatable && iface)
3165 type_lookup_iface_vtable_I (node, iface, &vtable);
3167 g_warning (G_STRLOC ": invalid interface pointer '%p'", g_iface);
3173 * g_type_default_interface_ref:
3174 * @g_type: an interface type
3176 * Increments the reference count for the interface type @g_type,
3177 * and returns the default interface vtable for the type.
3179 * If the type is not currently in use, then the default vtable
3180 * for the type will be created and initalized by calling
3181 * the base interface init and default vtable init functions for
3182 * the type (the @base_init and @class_init members of #GTypeInfo).
3183 * Calling g_type_default_interface_ref() is useful when you
3184 * want to make sure that signals and properties for an interface
3185 * have been installed.
3189 * Returns: (type GObject.TypeInterface) (transfer none): the default
3190 * vtable for the interface; call g_type_default_interface_unref()
3191 * when you are done using the interface.
3194 g_type_default_interface_ref (GType g_type)
3197 gpointer dflt_vtable;
3199 G_WRITE_LOCK (&type_rw_lock);
3201 node = lookup_type_node_I (g_type);
3202 if (!node || !NODE_IS_IFACE (node) ||
3203 (node->data && NODE_REFCOUNT (node) == 0))
3205 G_WRITE_UNLOCK (&type_rw_lock);
3206 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3207 type_descriptive_name_I (g_type));
3211 if (!node->data || !node->data->iface.dflt_vtable)
3213 G_WRITE_UNLOCK (&type_rw_lock);
3214 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3215 G_WRITE_LOCK (&type_rw_lock);
3216 node = lookup_type_node_I (g_type);
3217 type_data_ref_Wm (node);
3218 type_iface_ensure_dflt_vtable_Wm (node);
3219 g_rec_mutex_unlock (&class_init_rec_mutex);
3222 type_data_ref_Wm (node); /* ref_count >= 1 already */
3224 dflt_vtable = node->data->iface.dflt_vtable;
3225 G_WRITE_UNLOCK (&type_rw_lock);
3231 * g_type_default_interface_peek:
3232 * @g_type: an interface type
3234 * If the interface type @g_type is currently in use, returns its
3235 * default interface vtable.
3239 * Returns: (type GObject.TypeInterface) (transfer none): the default
3240 * vtable for the interface, or %NULL if the type is not currently
3244 g_type_default_interface_peek (GType g_type)
3249 node = lookup_type_node_I (g_type);
3250 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3251 vtable = node->data->iface.dflt_vtable;
3259 * g_type_default_interface_unref:
3260 * @g_iface: (type GObject.TypeInterface): the default vtable
3261 * structure for a interface, as returned by g_type_default_interface_ref()
3263 * Decrements the reference count for the type corresponding to the
3264 * interface default vtable @g_iface. If the type is dynamic, then
3265 * when no one is using the interface and all references have
3266 * been released, the finalize function for the interface's default
3267 * vtable (the @class_finalize member of #GTypeInfo) will be called.
3272 g_type_default_interface_unref (gpointer g_iface)
3275 GTypeInterface *vtable = g_iface;
3277 g_return_if_fail (g_iface != NULL);
3279 node = lookup_type_node_I (vtable->g_type);
3280 if (node && NODE_IS_IFACE (node))
3281 type_data_unref_U (node, FALSE);
3283 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3284 type_descriptive_name_I (vtable->g_type));
3289 * @type: type to return name for
3291 * Get the unique name that is assigned to a type ID. Note that this
3292 * function (like all other GType API) cannot cope with invalid type
3293 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3294 * other validly registered type ID, but randomized type IDs should
3295 * not be passed in and will most likely lead to a crash.
3297 * Returns: static type name or %NULL
3300 g_type_name (GType type)
3304 g_assert_type_system_initialized ();
3306 node = lookup_type_node_I (type);
3308 return node ? NODE_NAME (node) : NULL;
3313 * @type: type to return quark of type name for
3315 * Get the corresponding quark of the type IDs name.
3317 * Returns: the type names quark or 0
3320 g_type_qname (GType type)
3324 node = lookup_type_node_I (type);
3326 return node ? node->qname : 0;
3331 * @name: type name to lookup
3333 * Lookup the type ID from a given type name, returning 0 if no type
3334 * has been registered under this name (this is the preferred method
3335 * to find out by name whether a specific type has been registered
3338 * Returns: corresponding type ID or 0
3341 g_type_from_name (const gchar *name)
3345 g_return_val_if_fail (name != NULL, 0);
3347 G_READ_LOCK (&type_rw_lock);
3348 type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
3349 G_READ_UNLOCK (&type_rw_lock);
3356 * @type: the derived type
3358 * Return the direct parent type of the passed in type. If the passed
3359 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3361 * Returns: the parent type
3364 g_type_parent (GType type)
3368 node = lookup_type_node_I (type);
3370 return node ? NODE_PARENT_TYPE (node) : 0;
3377 * Returns the length of the ancestry of the passed in type. This
3378 * includes the type itself, so that e.g. a fundamental type has depth 1.
3380 * Returns: the depth of @type
3383 g_type_depth (GType type)
3387 node = lookup_type_node_I (type);
3389 return node ? node->n_supers + 1 : 0;
3394 * @leaf_type: descendant of @root_type and the type to be returned
3395 * @root_type: immediate parent of the returned type
3397 * Given a @leaf_type and a @root_type which is contained in its
3398 * anchestry, return the type that @root_type is the immediate parent
3399 * of. In other words, this function determines the type that is
3400 * derived directly from @root_type which is also a base class of
3401 * @leaf_type. Given a root type and a leaf type, this function can
3402 * be used to determine the types and order in which the leaf type is
3403 * descended from the root type.
3405 * Returns: immediate child of @root_type and anchestor of @leaf_type
3408 g_type_next_base (GType type,
3414 node = lookup_type_node_I (type);
3417 TypeNode *base_node = lookup_type_node_I (base_type);
3419 if (base_node && base_node->n_supers < node->n_supers)
3421 guint n = node->n_supers - base_node->n_supers;
3423 if (node->supers[n] == base_type)
3424 atype = node->supers[n - 1];
3431 static inline gboolean
3432 type_node_check_conformities_UorL (TypeNode *node,
3433 TypeNode *iface_node,
3434 /* support_inheritance */
3435 gboolean support_interfaces,
3436 gboolean support_prerequisites,
3441 if (/* support_inheritance && */
3442 NODE_IS_ANCESTOR (iface_node, node))
3445 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3446 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3448 if (support_interfaces)
3452 if (type_lookup_iface_entry_L (node, iface_node))
3457 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3462 support_prerequisites)
3465 G_READ_LOCK (&type_rw_lock);
3466 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3469 G_READ_UNLOCK (&type_rw_lock);
3475 type_node_is_a_L (TypeNode *node,
3476 TypeNode *iface_node)
3478 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3481 static inline gboolean
3482 type_node_conforms_to_U (TypeNode *node,
3483 TypeNode *iface_node,
3484 gboolean support_interfaces,
3485 gboolean support_prerequisites)
3487 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3492 * @type: type to check anchestry for
3493 * @is_a_type: possible anchestor of @type or interface that @type
3496 * If @is_a_type is a derivable type, check whether @type is a
3497 * descendant of @is_a_type. If @is_a_type is an interface, check
3498 * whether @type conforms to it.
3500 * Returns: %TRUE if @type is a @is_a_type
3503 g_type_is_a (GType type,
3506 TypeNode *node, *iface_node;
3509 node = lookup_type_node_I (type);
3510 iface_node = lookup_type_node_I (iface_type);
3511 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3518 * @type: the parent type
3519 * @n_children: (out) (allow-none): location to store the length of
3520 * the returned array, or %NULL
3522 * Return a newly allocated and 0-terminated array of type IDs, listing
3523 * the child types of @type.
3525 * Returns: (array length=n_children) (transfer full): Newly allocated
3526 * and 0-terminated array of child types, free with g_free()
3529 g_type_children (GType type,
3534 node = lookup_type_node_I (type);
3539 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3540 children = g_new (GType, node->n_children + 1);
3541 memcpy (children, node->children, sizeof (GType) * node->n_children);
3542 children[node->n_children] = 0;
3545 *n_children = node->n_children;
3546 G_READ_UNLOCK (&type_rw_lock);
3560 * g_type_interfaces:
3561 * @type: the type to list interface types for
3562 * @n_interfaces: (out) (allow-none): location to store the length of
3563 * the returned array, or %NULL
3565 * Return a newly allocated and 0-terminated array of type IDs, listing
3566 * the interface types that @type conforms to.
3568 * Returns: (array length=n_interfaces) (transfer full): Newly allocated
3569 * and 0-terminated array of interface types, free with g_free()
3572 g_type_interfaces (GType type,
3573 guint *n_interfaces)
3577 node = lookup_type_node_I (type);
3578 if (node && node->is_instantiatable)
3580 IFaceEntries *entries;
3584 G_READ_LOCK (&type_rw_lock);
3585 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3588 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3589 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3590 ifaces[i] = entries->entry[i].iface_type;
3594 ifaces = g_new (GType, 1);
3601 G_READ_UNLOCK (&type_rw_lock);
3614 typedef struct _QData QData;
3626 static inline gpointer
3627 type_get_qdata_L (TypeNode *node,
3630 GData *gdata = node->global_gdata;
3632 if (quark && gdata && gdata->n_qdatas)
3634 QData *qdatas = gdata->qdatas - 1;
3635 guint n_qdatas = gdata->n_qdatas;
3642 i = (n_qdatas + 1) / 2;
3644 if (quark == check->quark)
3646 else if (quark > check->quark)
3651 else /* if (quark < check->quark) */
3662 * @quark: a #GQuark id to identify the data
3664 * Obtains data which has previously been attached to @type
3665 * with g_type_set_qdata().
3667 * Note that this does not take subtyping into account; data
3668 * attached to one type with g_type_set_qdata() cannot
3669 * be retrieved from a subtype using g_type_get_qdata().
3671 * Returns: (transfer none): the data, or %NULL if no data was found
3674 g_type_get_qdata (GType type,
3680 node = lookup_type_node_I (type);
3683 G_READ_LOCK (&type_rw_lock);
3684 data = type_get_qdata_L (node, quark);
3685 G_READ_UNLOCK (&type_rw_lock);
3689 g_return_val_if_fail (node != NULL, NULL);
3696 type_set_qdata_W (TypeNode *node,
3704 /* setup qdata list if necessary */
3705 if (!node->global_gdata)
3706 node->global_gdata = g_new0 (GData, 1);
3707 gdata = node->global_gdata;
3709 /* try resetting old data */
3710 qdata = gdata->qdatas;
3711 for (i = 0; i < gdata->n_qdatas; i++)
3712 if (qdata[i].quark == quark)
3714 qdata[i].data = data;
3720 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3721 qdata = gdata->qdatas;
3722 for (i = 0; i < gdata->n_qdatas - 1; i++)
3723 if (qdata[i].quark > quark)
3725 memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3726 qdata[i].quark = quark;
3727 qdata[i].data = data;
3733 * @quark: a #GQuark id to identify the data
3736 * Attaches arbitrary data to a type.
3739 g_type_set_qdata (GType type,
3745 g_return_if_fail (quark != 0);
3747 node = lookup_type_node_I (type);
3750 G_WRITE_LOCK (&type_rw_lock);
3751 type_set_qdata_W (node, quark, data);
3752 G_WRITE_UNLOCK (&type_rw_lock);
3755 g_return_if_fail (node != NULL);
3759 type_add_flags_W (TypeNode *node,
3764 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3765 g_return_if_fail (node != NULL);
3767 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3768 g_warning ("tagging type '%s' as abstract after class initialization", NODE_NAME (node));
3769 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3771 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3776 * @type: #GType of a static, classed type
3777 * @query: (out caller-allocates): a user provided structure that is
3778 * filled in with constant values upon success
3780 * Queries the type system for information about a specific type.
3781 * This function will fill in a user-provided structure to hold
3782 * type-specific information. If an invalid #GType is passed in, the
3783 * @type member of the #GTypeQuery is 0. All members filled into the
3784 * #GTypeQuery structure should be considered constant and have to be
3788 g_type_query (GType type,
3793 g_return_if_fail (query != NULL);
3795 /* if node is not static and classed, we won't allow query */
3797 node = lookup_type_node_I (type);
3798 if (node && node->is_classed && !node->plugin)
3800 /* type is classed and probably even instantiatable */
3801 G_READ_LOCK (&type_rw_lock);
3802 if (node->data) /* type is static or referenced */
3804 query->type = NODE_TYPE (node);
3805 query->type_name = NODE_NAME (node);
3806 query->class_size = node->data->class.class_size;
3807 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3809 G_READ_UNLOCK (&type_rw_lock);
3814 /* --- implementation details --- */
3816 g_type_test_flags (GType type,
3820 gboolean result = FALSE;
3822 node = lookup_type_node_I (type);
3825 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3826 guint tflags = flags & TYPE_FLAG_MASK;
3830 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3832 fflags = (finfo->type_flags & fflags) == fflags;
3839 G_READ_LOCK (&type_rw_lock);
3840 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3841 G_READ_UNLOCK (&type_rw_lock);
3846 result = tflags && fflags;
3853 * g_type_get_plugin:
3854 * @type: #GType to retrieve the plugin for
3856 * Returns the #GTypePlugin structure for @type.
3858 * Returns: (transfer none): the corresponding plugin
3859 * if @type is a dynamic type, %NULL otherwise
3862 g_type_get_plugin (GType type)
3866 node = lookup_type_node_I (type);
3868 return node ? node->plugin : NULL;
3872 * g_type_interface_get_plugin:
3873 * @instance_type: #GType of an instantiatable type
3874 * @interface_type: #GType of an interface type
3876 * Returns the #GTypePlugin structure for the dynamic interface
3877 * @interface_type which has been added to @instance_type, or %NULL
3878 * if @interface_type has not been added to @instance_type or does
3879 * not have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3881 * Returns: (transfer none): the #GTypePlugin for the dynamic
3882 * interface @interface_type of @instance_type
3885 g_type_interface_get_plugin (GType instance_type,
3886 GType interface_type)
3891 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3893 node = lookup_type_node_I (instance_type);
3894 iface = lookup_type_node_I (interface_type);
3897 IFaceHolder *iholder;
3898 GTypePlugin *plugin;
3900 G_READ_LOCK (&type_rw_lock);
3902 iholder = iface_node_get_holders_L (iface);
3903 while (iholder && iholder->instance_type != instance_type)
3904 iholder = iholder->next;
3905 plugin = iholder ? iholder->plugin : NULL;
3907 G_READ_UNLOCK (&type_rw_lock);
3912 g_return_val_if_fail (node == NULL, NULL);
3913 g_return_val_if_fail (iface == NULL, NULL);
3915 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3921 * g_type_fundamental_next:
3923 * Returns the next free fundamental type id which can be used to
3924 * register a new fundamental type with g_type_register_fundamental().
3925 * The returned type ID represents the highest currently registered
3926 * fundamental type identifier.
3928 * Returns: the next available fundamental type ID to be registered,
3929 * or 0 if the type system ran out of fundamental type IDs
3932 g_type_fundamental_next (void)
3936 G_READ_LOCK (&type_rw_lock);
3937 type = static_fundamental_next;
3938 G_READ_UNLOCK (&type_rw_lock);
3939 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3940 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3944 * g_type_fundamental:
3945 * @type_id: valid type ID
3947 * Internal function, used to extract the fundamental type ID portion.
3948 * Use G_TYPE_FUNDAMENTAL() instead.
3950 * Returns: fundamental type ID
3953 g_type_fundamental (GType type_id)
3955 TypeNode *node = lookup_type_node_I (type_id);
3957 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3961 g_type_check_instance_is_a (GTypeInstance *type_instance,
3964 TypeNode *node, *iface;
3967 if (!type_instance || !type_instance->g_class)
3970 node = lookup_type_node_I (type_instance->g_class->g_type);
3971 iface = lookup_type_node_I (iface_type);
3972 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3978 g_type_check_class_is_a (GTypeClass *type_class,
3981 TypeNode *node, *iface;
3987 node = lookup_type_node_I (type_class->g_type);
3988 iface = lookup_type_node_I (is_a_type);
3989 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3995 g_type_check_instance_cast (GTypeInstance *type_instance,
4000 if (type_instance->g_class)
4002 TypeNode *node, *iface;
4003 gboolean is_instantiatable, check;
4005 node = lookup_type_node_I (type_instance->g_class->g_type);
4006 is_instantiatable = node && node->is_instantiatable;
4007 iface = lookup_type_node_I (iface_type);
4008 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4010 return type_instance;
4012 if (is_instantiatable)
4013 g_warning ("invalid cast from '%s' to '%s'",
4014 type_descriptive_name_I (type_instance->g_class->g_type),
4015 type_descriptive_name_I (iface_type));
4017 g_warning ("invalid uninstantiatable type '%s' in cast to '%s'",
4018 type_descriptive_name_I (type_instance->g_class->g_type),
4019 type_descriptive_name_I (iface_type));
4022 g_warning ("invalid unclassed pointer in cast to '%s'",
4023 type_descriptive_name_I (iface_type));
4026 return type_instance;
4030 g_type_check_class_cast (GTypeClass *type_class,
4035 TypeNode *node, *iface;
4036 gboolean is_classed, check;
4038 node = lookup_type_node_I (type_class->g_type);
4039 is_classed = node && node->is_classed;
4040 iface = lookup_type_node_I (is_a_type);
4041 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4046 g_warning ("invalid class cast from '%s' to '%s'",
4047 type_descriptive_name_I (type_class->g_type),
4048 type_descriptive_name_I (is_a_type));
4050 g_warning ("invalid unclassed type '%s' in class cast to '%s'",
4051 type_descriptive_name_I (type_class->g_type),
4052 type_descriptive_name_I (is_a_type));
4055 g_warning ("invalid class cast from (NULL) pointer to '%s'",
4056 type_descriptive_name_I (is_a_type));
4061 * g_type_check_instance:
4062 * @instance: a valid #GTypeInstance structure
4064 * Private helper function to aid implementation of the
4065 * G_TYPE_CHECK_INSTANCE() macro.
4067 * Returns: %TRUE if @instance is valid, %FALSE otherwise
4070 g_type_check_instance (GTypeInstance *type_instance)
4072 /* this function is just here to make the signal system
4073 * conveniently elaborated on instance checks
4077 if (type_instance->g_class)
4079 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4081 if (node && node->is_instantiatable)
4084 g_warning ("instance of invalid non-instantiatable type '%s'",
4085 type_descriptive_name_I (type_instance->g_class->g_type));
4088 g_warning ("instance with invalid (NULL) class pointer");
4091 g_warning ("invalid (NULL) pointer instance");
4096 static inline gboolean
4097 type_check_is_value_type_U (GType type)
4099 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4102 /* common path speed up */
4103 node = lookup_type_node_I (type);
4104 if (node && node->mutatable_check_cache)
4107 G_READ_LOCK (&type_rw_lock);
4111 if (node->data && NODE_REFCOUNT (node) > 0 &&
4112 node->data->common.value_table->value_init)
4113 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4114 else if (NODE_IS_IFACE (node))
4118 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4120 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4121 TypeNode *prnode = lookup_type_node_I (prtype);
4123 if (prnode->is_instantiatable)
4126 node = lookup_type_node_I (type);
4132 G_READ_UNLOCK (&type_rw_lock);
4134 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4138 g_type_check_is_value_type (GType type)
4140 return type_check_is_value_type_U (type);
4144 g_type_check_value (GValue *value)
4146 return value && type_check_is_value_type_U (value->g_type);
4150 g_type_check_value_holds (GValue *value,
4153 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4157 * g_type_value_table_peek: (skip)
4160 * Returns the location of the #GTypeValueTable associated with @type.
4162 * Note that this function should only be used from source code
4163 * that implements or has internal knowledge of the implementation of
4166 * Returns: location of the #GTypeValueTable associated with @type or
4167 * %NULL if there is no #GTypeValueTable associated with @type
4170 g_type_value_table_peek (GType type)
4172 GTypeValueTable *vtable = NULL;
4173 TypeNode *node = lookup_type_node_I (type);
4174 gboolean has_refed_data, has_table;
4176 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4177 return node->data->common.value_table;
4179 G_READ_LOCK (&type_rw_lock);
4182 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4183 has_table = has_refed_data && node->data->common.value_table->value_init;
4187 vtable = node->data->common.value_table;
4188 else if (NODE_IS_IFACE (node))
4192 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4194 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4195 TypeNode *prnode = lookup_type_node_I (prtype);
4197 if (prnode->is_instantiatable)
4200 node = lookup_type_node_I (type);
4201 goto restart_table_peek;
4207 G_READ_UNLOCK (&type_rw_lock);
4213 g_warning (G_STRLOC ": type id '%" G_GSIZE_FORMAT "' is invalid", type);
4214 if (!has_refed_data)
4215 g_warning ("can't peek value table for type '%s' which is not currently referenced",
4216 type_descriptive_name_I (type));
4222 g_type_name_from_instance (GTypeInstance *instance)
4225 return "<NULL-instance>";
4227 return g_type_name_from_class (instance->g_class);
4231 g_type_name_from_class (GTypeClass *g_class)
4234 return "<NULL-class>";
4236 return g_type_name (g_class->g_type);
4240 /* --- private api for gboxed.c --- */
4242 _g_type_boxed_copy (GType type, gpointer value)
4244 TypeNode *node = lookup_type_node_I (type);
4246 return node->data->boxed.copy_func (value);
4250 _g_type_boxed_free (GType type, gpointer value)
4252 TypeNode *node = lookup_type_node_I (type);
4254 node->data->boxed.free_func (value);
4258 _g_type_boxed_init (GType type,
4259 GBoxedCopyFunc copy_func,
4260 GBoxedFreeFunc free_func)
4262 TypeNode *node = lookup_type_node_I (type);
4264 node->data->boxed.copy_func = copy_func;
4265 node->data->boxed.free_func = free_func;
4268 /* --- initialization --- */
4270 * g_type_init_with_debug_flags:
4271 * @debug_flags: bitwise combination of #GTypeDebugFlags values for
4272 * debugging purposes
4274 * This function used to initialise the type system with debugging
4275 * flags. Since GLib 2.36, the type system is initialised automatically
4276 * and this function does nothing.
4278 * If you need to enable debugging features, use the GOBJECT_DEBUG
4279 * environment variable.
4281 * Deprecated: 2.36: the type system is now initialised automatically
4284 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4286 g_assert_type_system_initialized ();
4289 g_message ("g_type_init_with_debug_flags() is no longer supported. Use the GOBJECT_DEBUG environment variable.");
4295 * This function used to initialise the type system. Since GLib 2.36,
4296 * the type system is initialised automatically and this function does
4299 * Deprecated: 2.36: the type system is now initialised automatically
4304 g_assert_type_system_initialized ();
4307 #if defined (G_HAS_CONSTRUCTORS)
4308 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
4309 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
4311 G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
4313 # error Your platform/compiler is missing constructor support
4317 gobject_init_ctor (void)
4319 const gchar *env_string;
4324 G_WRITE_LOCK (&type_rw_lock);
4326 /* setup GObject library wide debugging flags */
4327 env_string = g_getenv ("GOBJECT_DEBUG");
4328 if (env_string != NULL)
4330 GDebugKey debug_keys[] = {
4331 { "objects", G_TYPE_DEBUG_OBJECTS },
4332 { "signals", G_TYPE_DEBUG_SIGNALS },
4335 _g_type_debug_flags = g_parse_debug_string (env_string, debug_keys, G_N_ELEMENTS (debug_keys));
4339 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4340 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4341 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4343 /* type qname hash table */
4344 static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
4346 /* invalid type G_TYPE_INVALID (0)
4348 static_fundamental_type_nodes[0] = NULL;
4350 /* void type G_TYPE_NONE
4352 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4353 type = NODE_TYPE (node);
4354 g_assert (type == G_TYPE_NONE);
4356 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4358 memset (&info, 0, sizeof (info));
4359 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4360 type = NODE_TYPE (node);
4361 type_data_make_W (node, &info, NULL);
4362 g_assert (type == G_TYPE_INTERFACE);
4364 G_WRITE_UNLOCK (&type_rw_lock);
4368 /* G_TYPE_TYPE_PLUGIN
4370 g_type_ensure (g_type_plugin_get_type ());
4372 /* G_TYPE_* value types
4374 _g_value_types_init ();
4376 /* G_TYPE_ENUM & G_TYPE_FLAGS
4378 _g_enum_types_init ();
4382 _g_boxed_type_init ();
4386 _g_param_type_init ();
4390 _g_object_type_init ();
4392 /* G_TYPE_PARAM_* pspec types
4394 _g_param_spec_types_init ();
4396 /* Value Transformations
4398 _g_value_transforms_init ();
4406 * g_type_class_add_private:
4407 * @g_class: class structure for an instantiatable type
4408 * @private_size: size of private structure
4410 * Registers a private structure for an instantiatable type.
4412 * When an object is allocated, the private structures for
4413 * the type and all of its parent types are allocated
4414 * sequentially in the same memory block as the public
4415 * structures, and are zero-filled.
4417 * Note that the accumulated size of the private structures of
4418 * a type and all its parent types cannot exceed 64 KiB.
4420 * This function should be called in the type's class_init() function.
4421 * The private structure can be retrieved using the
4422 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4424 * The following example shows attaching a private structure
4425 * MyObjectPrivate to an object MyObject defined in the standard
4426 * GObject fashion in the type's class_init() function.
4428 * Note the use of a structure member "priv" to avoid the overhead
4429 * of repeatedly calling MY_OBJECT_GET_PRIVATE().
4431 * |[<!-- language="C" -->
4432 * typedef struct _MyObject MyObject;
4433 * typedef struct _MyObjectPrivate MyObjectPrivate;
4435 * struct _MyObject {
4438 * MyObjectPrivate *priv;
4441 * struct _MyObjectPrivate {
4446 * my_object_class_init (MyObjectClass *klass)
4448 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4452 * my_object_init (MyObject *my_object)
4454 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4457 * /<!-- -->* my_object->priv->some_field will be
4458 * * automatically initialised to 0 *<!-- -->/
4462 * my_object_get_some_field (MyObject *my_object)
4464 * MyObjectPrivate *priv;
4466 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
4468 * priv = my_object->priv;
4470 * return priv->some_field;
4477 g_type_class_add_private (gpointer g_class,
4480 GType instance_type = ((GTypeClass *)g_class)->g_type;
4481 TypeNode *node = lookup_type_node_I (instance_type);
4483 g_return_if_fail (private_size > 0);
4484 g_return_if_fail (private_size <= 0xffff);
4486 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4488 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4489 type_descriptive_name_I (instance_type));
4493 if (NODE_PARENT_TYPE (node))
4495 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4496 if (node->data->instance.private_size != pnode->data->instance.private_size)
4498 g_warning ("g_type_class_add_private() called multiple times for the same type");
4503 G_WRITE_LOCK (&type_rw_lock);
4505 private_size = ALIGN_STRUCT (node->data->instance.private_size + private_size);
4506 g_assert (private_size <= 0xffff);
4507 node->data->instance.private_size = private_size;
4509 G_WRITE_UNLOCK (&type_rw_lock);
4512 /* semi-private, called only by the G_ADD_PRIVATE macro */
4514 g_type_add_instance_private (GType class_gtype,
4517 TypeNode *node = lookup_type_node_I (class_gtype);
4519 g_return_val_if_fail (private_size > 0, 0);
4520 g_return_val_if_fail (private_size <= 0xffff, 0);
4522 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4524 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4525 type_descriptive_name_I (class_gtype));
4529 if (node->plugin != NULL)
4531 g_warning ("cannot use g_type_add_instance_private() with dynamic type '%s'",
4532 type_descriptive_name_I (class_gtype));
4536 /* in the future, we want to register the private data size of a type
4537 * directly from the get_type() implementation so that we can take full
4538 * advantage of the type definition macros that we already have.
4540 * unfortunately, this does not behave correctly if a class in the middle
4541 * of the type hierarchy uses the "old style" of private data registration
4542 * from the class_init() implementation, as the private data offset is not
4543 * going to be known until the full class hierarchy is initialized.
4545 * in order to transition our code to the Glorious New Futureâ„¢, we proceed
4546 * with a two-step implementation: first, we provide this new function to
4547 * register the private data size in the get_type() implementation and we
4548 * hide it behind a macro. the function will return the private size, instead
4549 * of the offset, which will be stored inside a static variable defined by
4550 * the G_DEFINE_TYPE_EXTENDED macro. the G_DEFINE_TYPE_EXTENDED macro will
4551 * check the variable and call g_type_class_add_instance_private(), which
4552 * will use the data size and actually register the private data, then
4553 * return the computed offset of the private data, which will be stored
4554 * inside the static variable, so we can use it to retrieve the pointer
4555 * to the private data structure.
4557 * once all our code has been migrated to the new idiomatic form of private
4558 * data registration, we will change the g_type_add_instance_private()
4559 * function to actually perform the registration and return the offset
4560 * of the private data; g_type_class_add_instance_private() already checks
4561 * if the passed argument is negative (meaning that it's an offset in the
4562 * GTypeInstance allocation) and becomes a no-op if that's the case. this
4563 * should make the migration fully transparent even if we're effectively
4564 * copying this macro into everybody's code.
4566 return private_size;
4569 /* semi-private function, should only be used by G_DEFINE_TYPE_EXTENDED */
4571 g_type_class_adjust_private_offset (gpointer g_class,
4572 gint *private_size_or_offset)
4574 GType class_gtype = ((GTypeClass *) g_class)->g_type;
4575 TypeNode *node = lookup_type_node_I (class_gtype);
4576 gssize private_size;
4578 g_return_if_fail (private_size_or_offset != NULL);
4580 /* if we have been passed the offset instead of the private data size,
4581 * then we consider this as a no-op, and just return the value. see the
4582 * comment in g_type_add_instance_private() for the full explanation.
4584 if (*private_size_or_offset > 0)
4585 g_return_if_fail (*private_size_or_offset <= 0xffff);
4589 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4591 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4592 type_descriptive_name_I (class_gtype));
4593 *private_size_or_offset = 0;
4597 if (NODE_PARENT_TYPE (node))
4599 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4600 if (node->data->instance.private_size != pnode->data->instance.private_size)
4602 g_warning ("g_type_add_instance_private() called multiple times for the same type");
4603 *private_size_or_offset = 0;
4608 G_WRITE_LOCK (&type_rw_lock);
4610 private_size = ALIGN_STRUCT (node->data->instance.private_size + *private_size_or_offset);
4611 g_assert (private_size <= 0xffff);
4612 node->data->instance.private_size = private_size;
4614 *private_size_or_offset = -(gint) node->data->instance.private_size;
4616 G_WRITE_UNLOCK (&type_rw_lock);
4620 g_type_instance_get_private (GTypeInstance *instance,
4625 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4627 node = lookup_type_node_I (private_type);
4628 if (G_UNLIKELY (!node || !node->is_instantiatable))
4630 g_warning ("instance of invalid non-instantiatable type '%s'",
4631 type_descriptive_name_I (instance->g_class->g_type));
4635 return ((gchar *) instance) - node->data->instance.private_size;
4639 * g_type_class_get_instance_private_offset: (skip)
4640 * @g_class: a #GTypeClass
4642 * Gets the offset of the private data for instances of @g_class.
4644 * This is how many bytes you should add to the instance pointer of a
4645 * class in order to get the private data for the type represented by
4648 * You can only call this function after you have registered a private
4649 * data area for @g_class using g_type_class_add_private().
4651 * Returns: the offset, in bytes
4656 g_type_class_get_instance_private_offset (gpointer g_class)
4658 GType instance_type;
4659 guint16 parent_size;
4662 g_assert (g_class != NULL);
4664 instance_type = ((GTypeClass *) g_class)->g_type;
4665 node = lookup_type_node_I (instance_type);
4667 g_assert (node != NULL);
4668 g_assert (node->is_instantiatable);
4670 if (NODE_PARENT_TYPE (node))
4672 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4674 parent_size = pnode->data->instance.private_size;
4679 if (node->data->instance.private_size == parent_size)
4680 g_error ("g_type_class_get_instance_private_offset() called on class %s but it has no private data",
4681 g_type_name (instance_type));
4683 return -(gint) node->data->instance.private_size;
4687 * g_type_add_class_private:
4688 * @class_type: GType of an classed type
4689 * @private_size: size of private structure
4691 * Registers a private class structure for a classed type;
4692 * when the class is allocated, the private structures for
4693 * the class and all of its parent types are allocated
4694 * sequentially in the same memory block as the public
4695 * structures, and are zero-filled.
4697 * This function should be called in the
4698 * type's get_type() function after the type is registered.
4699 * The private structure can be retrieved using the
4700 * G_TYPE_CLASS_GET_PRIVATE() macro.
4705 g_type_add_class_private (GType class_type,
4708 TypeNode *node = lookup_type_node_I (class_type);
4711 g_return_if_fail (private_size > 0);
4713 if (!node || !node->is_classed || !node->data)
4715 g_warning ("cannot add class private field to invalid type '%s'",
4716 type_descriptive_name_I (class_type));
4720 if (NODE_PARENT_TYPE (node))
4722 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4723 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4725 g_warning ("g_type_add_class_private() called multiple times for the same type");
4730 G_WRITE_LOCK (&type_rw_lock);
4732 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4733 node->data->class.class_private_size = offset + private_size;
4735 G_WRITE_UNLOCK (&type_rw_lock);
4739 g_type_class_get_private (GTypeClass *klass,
4742 TypeNode *class_node;
4743 TypeNode *private_node;
4744 TypeNode *parent_node;
4747 g_return_val_if_fail (klass != NULL, NULL);
4749 class_node = lookup_type_node_I (klass->g_type);
4750 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4752 g_warning ("class of invalid type '%s'",
4753 type_descriptive_name_I (klass->g_type));
4757 private_node = lookup_type_node_I (private_type);
4758 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4760 g_warning ("attempt to retrieve private data for invalid type '%s'",
4761 type_descriptive_name_I (private_type));
4765 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4767 if (NODE_PARENT_TYPE (private_node))
4769 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4770 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4772 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4774 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_add_class_private()");
4778 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4781 return G_STRUCT_MEMBER_P (klass, offset);
4788 * Ensures that the indicated @type has been registered with the
4789 * type system, and its _class_init() method has been run.
4791 * In theory, simply calling the type's _get_type() method (or using
4792 * the corresponding macro) is supposed take care of this. However,
4793 * _get_type() methods are often marked %G_GNUC_CONST for performance
4794 * reasons, even though this is technically incorrect (since
4795 * %G_GNUC_CONST requires that the function not have side effects,
4796 * which _get_type() methods do on the first call). As a result, if
4797 * you write a bare call to a _get_type() macro, it may get optimized
4798 * out by the compiler. Using g_type_ensure() guarantees that the
4799 * type's _get_type() method is called.
4804 g_type_ensure (GType type)
4806 /* In theory, @type has already been resolved and so there's nothing
4807 * to do here. But this protects us in the case where the function
4808 * gets inlined (as it might in gobject_init_ctor() above).
4810 if (G_UNLIKELY (type == (GType)-1))
4811 g_error ("can't happen");
4815 g_type_is_in_init (GType type)
4819 node = lookup_type_node_I (type);
4821 return node->data->class.init_state != INITIALIZED;