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 "gatomicarray.h"
32 #include "gobject_trace.h"
34 #include "glib-private.h"
35 #include "gconstructor.h"
42 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
47 * @short_description: The GLib Runtime type identification and
49 * @title:Type Information
51 * The GType API is the foundation of the GObject system. It provides the
52 * facilities for registering and managing all fundamental data types,
53 * user-defined object and interface types.
55 * For type creation and registration purposes, all types fall into one of
56 * two categories: static or dynamic. Static types are never loaded or
57 * unloaded at run-time as dynamic types may be. Static types are created
58 * with g_type_register_static() that gets type specific information passed
59 * in via a #GTypeInfo structure.
61 * Dynamic types are created with g_type_register_dynamic() which takes a
62 * #GTypePlugin structure instead. The remaining type information (the
63 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
64 * and the g_type_plugin_*() API.
66 * These registration functions are usually called only once from a
67 * function whose only purpose is to return the type identifier for a
68 * specific class. Once the type (or class or interface) is registered,
69 * it may be instantiated, inherited, or implemented depending on exactly
70 * what sort of type it is.
72 * There is also a third registration function for registering fundamental
73 * types called g_type_register_fundamental() which requires both a #GTypeInfo
74 * structure and a #GTypeFundamentalInfo structure but it is seldom used
75 * since most fundamental types are predefined rather than user-defined.
77 * Type instance and class structs are limited to a total of 64 KiB,
78 * including all parent types. Similarly, type instances' private data
79 * (as created by g_type_class_add_private()) are limited to a total of
80 * 64 KiB. If a type instance needs a large static buffer, allocate it
81 * separately (typically by using #GArray or #GPtrArray) and put a pointer
82 * to the buffer in the structure.
84 * As mentioned in the [GType conventions][gtype-conventions], type names must
85 * be at least three characters long. There is no upper length limit. The first
86 * character must be a letter (a–z or A–Z) or an underscore (‘_’). Subsequent
87 * characters can be letters, numbers or any of ‘-_+’.
91 /* NOTE: some functions (some internal variants and exported ones)
92 * invalidate data portions of the TypeNodes. if external functions/callbacks
93 * are called, pointers to memory maintained by TypeNodes have to be looked up
94 * again. this affects most of the struct TypeNode fields, e.g. ->children or
95 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
96 * not ->supers[]), as all those memory portions can get realloc()ed during
97 * callback invocation.
100 * lock handling issues when calling static functions are indicated by
101 * uppercase letter postfixes, all static functions have to have
102 * one of the below postfixes:
103 * - _I: [Indifferent about locking]
104 * function doesn't care about locks at all
105 * - _U: [Unlocked invocation]
106 * no read or write lock has to be held across function invocation
107 * (locks may be acquired and released during invocation though)
108 * - _L: [Locked invocation]
109 * a write lock or more than 0 read locks have to be held across
110 * function invocation
111 * - _W: [Write-locked invocation]
112 * a write lock has to be held across function invocation
113 * - _Wm: [Write-locked invocation, mutatable]
114 * like _W, but the write lock might be released and reacquired
115 * during invocation, watch your pointers
116 * - _WmREC: [Write-locked invocation, mutatable, recursive]
117 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
121 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_rw_lock_reader_lock (rw_lock); } while (0)
122 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_rw_lock_reader_unlock (rw_lock); } while (0)
123 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_rw_lock_writer_lock (rw_lock); } while (0)
124 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_rw_lock_writer_unlock (rw_lock); } while (0)
126 #define G_READ_LOCK(rw_lock) g_rw_lock_reader_lock (rw_lock)
127 #define G_READ_UNLOCK(rw_lock) g_rw_lock_reader_unlock (rw_lock)
128 #define G_WRITE_LOCK(rw_lock) g_rw_lock_writer_lock (rw_lock)
129 #define G_WRITE_UNLOCK(rw_lock) g_rw_lock_writer_unlock (rw_lock)
131 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
132 static const gchar _action[] = " invalidly modified type "; \
133 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
135 g_error ("%s(%p)%s'%s'", _fname, _arg, _action, _tname); \
137 g_error ("%s()%s'%s'", _fname, _action, _tname); \
139 #define g_assert_type_system_initialized() \
140 g_assert (static_quark_type_flags)
142 #ifdef G_ENABLE_DEBUG
143 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
144 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
147 #else /* !G_ENABLE_DEBUG */
148 #define DEBUG_CODE(debug_type, code_block) /* code_block */
149 #endif /* G_ENABLE_DEBUG */
151 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
152 G_TYPE_FLAG_INSTANTIATABLE | \
153 G_TYPE_FLAG_DERIVABLE | \
154 G_TYPE_FLAG_DEEP_DERIVABLE)
155 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
156 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
157 sizeof (gpointer)), \
160 /* The 2*sizeof(size_t) alignment here is borrowed from
161 * GNU libc, so it should be good most everywhere.
162 * It is more conservative than is needed on some 64-bit
163 * platforms, but ia64 does require a 16-byte alignment.
164 * The SIMD extensions for x86 and ppc32 would want a
165 * larger alignment than this, but we don't need to
166 * do better than malloc.
168 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
169 #define ALIGN_STRUCT(offset) \
170 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
173 /* --- typedefs --- */
174 typedef struct _TypeNode TypeNode;
175 typedef struct _CommonData CommonData;
176 typedef struct _BoxedData BoxedData;
177 typedef struct _IFaceData IFaceData;
178 typedef struct _ClassData ClassData;
179 typedef struct _InstanceData InstanceData;
180 typedef union _TypeData TypeData;
181 typedef struct _IFaceEntries IFaceEntries;
182 typedef struct _IFaceEntry IFaceEntry;
183 typedef struct _IFaceHolder IFaceHolder;
186 /* --- prototypes --- */
187 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
188 static void type_add_flags_W (TypeNode *node,
190 static void type_data_make_W (TypeNode *node,
191 const GTypeInfo *info,
192 const GTypeValueTable *value_table);
193 static inline void type_data_ref_Wm (TypeNode *node);
194 static inline void type_data_unref_U (TypeNode *node,
196 static void type_data_last_unref_Wm (TypeNode * node,
198 static inline gpointer type_get_qdata_L (TypeNode *node,
200 static inline void type_set_qdata_W (TypeNode *node,
203 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
204 GType instance_type);
205 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
207 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
209 static gboolean type_node_is_a_L (TypeNode *node,
210 TypeNode *iface_node);
213 /* --- enumeration --- */
215 /* The InitState enumeration is used to track the progress of initializing
216 * both classes and interface vtables. Keeping the state of initialization
217 * is necessary to handle new interfaces being added while we are initializing
218 * the class or other interfaces.
230 /* --- structures --- */
233 guint volatile ref_count;
234 #ifdef G_ENABLE_DEBUG
235 guint volatile instance_count;
238 guint n_children; /* writable with lock */
240 guint n_prerequisites : 9;
241 guint is_classed : 1;
242 guint is_instantiatable : 1;
243 guint mutatable_check_cache : 1; /* combines some common path checks */
244 GType *children; /* writable with lock */
245 TypeData * volatile data;
249 GAtomicArray iface_entries; /* for !iface types */
250 GAtomicArray offsets;
252 GType *prerequisites;
253 GType supers[1]; /* flexible array */
256 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
257 #define MAX_N_SUPERS (255)
258 #define MAX_N_CHILDREN (G_MAXUINT)
259 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
260 #define MAX_N_PREREQUISITES (511)
261 #define NODE_TYPE(node) (node->supers[0])
262 #define NODE_PARENT_TYPE(node) (node->supers[1])
263 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
264 #define NODE_NAME(node) (g_quark_to_string (node->qname))
265 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
266 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
267 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
268 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
269 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
270 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
271 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
272 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
273 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
274 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
275 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
276 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
278 #define NODE_IS_ANCESTOR(ancestor, node) \
279 ((ancestor)->n_supers <= (node)->n_supers && \
280 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
285 GInterfaceInfo *info;
293 GTypeInterface *vtable;
294 InitState init_state;
297 struct _IFaceEntries {
302 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
303 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
307 GTypeValueTable *value_table;
313 GBoxedCopyFunc copy_func;
314 GBoxedFreeFunc free_func;
321 GBaseInitFunc vtable_init_base;
322 GBaseFinalizeFunc vtable_finalize_base;
323 GClassInitFunc dflt_init;
324 GClassFinalizeFunc dflt_finalize;
325 gconstpointer dflt_data;
326 gpointer dflt_vtable;
333 guint16 class_private_size;
334 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
335 GBaseInitFunc class_init_base;
336 GBaseFinalizeFunc class_finalize_base;
337 GClassInitFunc class_init;
338 GClassFinalizeFunc class_finalize;
339 gconstpointer class_data;
347 guint16 class_private_size;
348 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
349 GBaseInitFunc class_init_base;
350 GBaseFinalizeFunc class_finalize_base;
351 GClassInitFunc class_init;
352 GClassFinalizeFunc class_finalize;
353 gconstpointer class_data;
355 guint16 instance_size;
356 guint16 private_size;
358 GInstanceInitFunc instance_init;
367 InstanceData instance;
372 GTypeClassCacheFunc cache_func;
377 GTypeInterfaceCheckFunc check_func;
381 /* --- variables --- */
382 static GRWLock type_rw_lock;
383 static GRecMutex class_init_rec_mutex;
384 static guint static_n_class_cache_funcs = 0;
385 static ClassCacheFunc *static_class_cache_funcs = NULL;
386 static guint static_n_iface_check_funcs = 0;
387 static IFaceCheckFunc *static_iface_check_funcs = NULL;
388 static GQuark static_quark_type_flags = 0;
389 static GQuark static_quark_iface_holder = 0;
390 static GQuark static_quark_dependants_array = 0;
391 static guint type_registration_serial = 0;
392 GTypeDebugFlags _g_type_debug_flags = 0;
394 /* --- type nodes --- */
395 static GHashTable *static_type_nodes_ht = NULL;
396 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
397 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
399 static inline TypeNode*
400 lookup_type_node_I (GType utype)
402 if (utype > G_TYPE_FUNDAMENTAL_MAX)
403 return (TypeNode*) (utype & ~TYPE_ID_MASK);
405 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
409 * g_type_get_type_registration_serial:
411 * Returns an opaque serial number that represents the state of the set
412 * of registered types. Any time a type is registered this serial changes,
413 * which means you can cache information based on type lookups (such as
414 * g_type_from_name()) and know if the cache is still valid at a later
415 * time by comparing the current serial with the one at the type lookup.
419 * Returns: An unsigned int, representing the state of type registrations
422 g_type_get_type_registration_serial (void)
424 return (guint)g_atomic_int_get ((gint *)&type_registration_serial);
428 type_node_any_new_W (TypeNode *pnode,
432 GTypeFundamentalFlags type_flags)
437 guint i, node_size = 0;
439 n_supers = pnode ? pnode->n_supers + 1 : 0;
442 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
443 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
444 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
445 node = g_malloc0 (node_size);
446 if (!pnode) /* offset fundamental types */
448 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
449 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
455 g_assert ((type & TYPE_ID_MASK) == 0);
457 node->n_supers = n_supers;
460 node->supers[0] = type;
463 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
464 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
466 if (NODE_IS_IFACE (node))
468 IFACE_NODE_N_PREREQUISITES (node) = 0;
469 IFACE_NODE_PREREQUISITES (node) = NULL;
472 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
476 node->supers[0] = type;
477 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
479 node->is_classed = pnode->is_classed;
480 node->is_instantiatable = pnode->is_instantiatable;
482 if (NODE_IS_IFACE (node))
484 IFACE_NODE_N_PREREQUISITES (node) = 0;
485 IFACE_NODE_PREREQUISITES (node) = NULL;
490 IFaceEntries *entries;
492 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
493 IFACE_ENTRIES_HEADER_SIZE,
497 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
499 entries->entry[j].vtable = NULL;
500 entries->entry[j].init_state = UNINITIALIZED;
502 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
507 i = pnode->n_children++;
508 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
509 pnode->children[i] = type;
512 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
514 node->plugin = plugin;
515 node->n_children = 0;
516 node->children = NULL;
518 node->qname = g_quark_from_string (name);
519 node->global_gdata = NULL;
520 g_hash_table_insert (static_type_nodes_ht,
521 (gpointer) g_quark_to_string (node->qname),
524 g_atomic_int_inc ((gint *)&type_registration_serial);
529 static inline GTypeFundamentalInfo*
530 type_node_fundamental_info_I (TypeNode *node)
532 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
534 if (ftype != NODE_TYPE (node))
535 node = lookup_type_node_I (ftype);
537 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
541 type_node_fundamental_new_W (GType ftype,
543 GTypeFundamentalFlags type_flags)
545 GTypeFundamentalInfo *finfo;
548 g_assert ((ftype & TYPE_ID_MASK) == 0);
549 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
551 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
552 static_fundamental_next++;
554 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
556 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
558 finfo = type_node_fundamental_info_I (node);
559 finfo->type_flags = type_flags;
565 type_node_new_W (TypeNode *pnode,
571 g_assert (pnode->n_supers < MAX_N_SUPERS);
572 g_assert (pnode->n_children < MAX_N_CHILDREN);
574 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
577 static inline IFaceEntry*
578 lookup_iface_entry_I (volatile IFaceEntries *entries,
579 TypeNode *iface_node)
590 G_ATOMIC_ARRAY_DO_TRANSACTION
591 (&iface_node->_prot.offsets, guint8,
594 offsets = transaction_data;
595 offset_index = entries->offset_index;
596 if (offsets != NULL &&
597 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
599 index = offsets[offset_index];
602 /* zero means unset, subtract one to get real index */
605 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
607 check = (IFaceEntry *)&entries->entry[index];
608 if (check->iface_type == NODE_TYPE (iface_node))
618 static inline IFaceEntry*
619 type_lookup_iface_entry_L (TypeNode *node,
620 TypeNode *iface_node)
622 if (!NODE_IS_IFACE (iface_node))
625 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
630 static inline gboolean
631 type_lookup_iface_vtable_I (TypeNode *node,
632 TypeNode *iface_node,
633 gpointer *vtable_ptr)
638 if (!NODE_IS_IFACE (iface_node))
645 G_ATOMIC_ARRAY_DO_TRANSACTION
646 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
648 entry = lookup_iface_entry_I (transaction_data, iface_node);
653 *vtable_ptr = entry->vtable;
662 static inline gboolean
663 type_lookup_prerequisite_L (TypeNode *iface,
664 GType prerequisite_type)
666 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
668 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
669 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
676 i = (n_prerequisites + 1) >> 1;
677 check = prerequisites + i;
678 if (prerequisite_type == *check)
680 else if (prerequisite_type > *check)
682 n_prerequisites -= i;
683 prerequisites = check;
685 else /* if (prerequisite_type < *check) */
686 n_prerequisites = i - 1;
688 while (n_prerequisites);
694 type_descriptive_name_I (GType type)
698 TypeNode *node = lookup_type_node_I (type);
700 return node ? NODE_NAME (node) : "<unknown>";
707 /* --- type consistency checks --- */
709 check_plugin_U (GTypePlugin *plugin,
710 gboolean need_complete_type_info,
711 gboolean need_complete_interface_info,
712 const gchar *type_name)
714 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
718 g_warning ("plugin handle for type '%s' is NULL",
722 if (!G_IS_TYPE_PLUGIN (plugin))
724 g_warning ("plugin pointer (%p) for type '%s' is invalid",
728 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
730 g_warning ("plugin for type '%s' has no complete_type_info() implementation",
734 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
736 g_warning ("plugin for type '%s' has no complete_interface_info() implementation",
744 check_type_name_I (const gchar *type_name)
746 static const gchar extra_chars[] = "-_+";
747 const gchar *p = type_name;
750 if (!type_name[0] || !type_name[1] || !type_name[2])
752 g_warning ("type name '%s' is too short", type_name);
755 /* check the first letter */
756 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
757 for (p = type_name + 1; *p; p++)
758 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
759 (p[0] >= 'a' && p[0] <= 'z') ||
760 (p[0] >= '0' && p[0] <= '9') ||
761 strchr (extra_chars, p[0]));
764 g_warning ("type name '%s' contains invalid characters", type_name);
767 if (g_type_from_name (type_name))
769 g_warning ("cannot register existing type '%s'", type_name);
777 check_derivation_I (GType parent_type,
778 const gchar *type_name)
781 GTypeFundamentalInfo* finfo;
783 pnode = lookup_type_node_I (parent_type);
786 g_warning ("cannot derive type '%s' from invalid parent type '%s'",
788 type_descriptive_name_I (parent_type));
791 finfo = type_node_fundamental_info_I (pnode);
792 /* ensure flat derivability */
793 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
795 g_warning ("cannot derive '%s' from non-derivable parent type '%s'",
800 /* ensure deep derivability */
801 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
802 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
804 g_warning ("cannot derive '%s' from non-fundamental parent type '%s'",
814 check_collect_format_I (const gchar *collect_format)
816 const gchar *p = collect_format;
817 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
818 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
819 G_VALUE_COLLECT_POINTER, 0 };
822 if (!strchr (valid_format, *p++))
824 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
828 check_value_table_I (const gchar *type_name,
829 const GTypeValueTable *value_table)
833 else if (value_table->value_init == NULL)
835 if (value_table->value_free || value_table->value_copy ||
836 value_table->value_peek_pointer ||
837 value_table->collect_format || value_table->collect_value ||
838 value_table->lcopy_format || value_table->lcopy_value)
839 g_warning ("cannot handle uninitializable values of type '%s'",
843 else /* value_table->value_init != NULL */
845 if (!value_table->value_free)
848 * g_warning ("missing 'value_free()' for type '%s'", type_name);
852 if (!value_table->value_copy)
854 g_warning ("missing 'value_copy()' for type '%s'", type_name);
857 if ((value_table->collect_format || value_table->collect_value) &&
858 (!value_table->collect_format || !value_table->collect_value))
860 g_warning ("one of 'collect_format' and 'collect_value()' is unspecified for type '%s'",
864 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
866 g_warning ("the '%s' specification for type '%s' is too long or invalid",
871 if ((value_table->lcopy_format || value_table->lcopy_value) &&
872 (!value_table->lcopy_format || !value_table->lcopy_value))
874 g_warning ("one of 'lcopy_format' and 'lcopy_value()' is unspecified for type '%s'",
878 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
880 g_warning ("the '%s' specification for type '%s' is too long or invalid",
890 check_type_info_I (TypeNode *pnode,
892 const gchar *type_name,
893 const GTypeInfo *info)
895 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
896 gboolean is_interface = ftype == G_TYPE_INTERFACE;
898 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
900 /* check instance members */
901 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
902 (info->instance_size || info->n_preallocs || info->instance_init))
905 g_warning ("cannot instantiate '%s', derived from non-instantiatable parent type '%s'",
909 g_warning ("cannot instantiate '%s' as non-instantiatable fundamental",
913 /* check class & interface members */
914 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
915 (info->class_init || info->class_finalize || info->class_data ||
916 info->class_size || info->base_init || info->base_finalize))
919 g_warning ("cannot create class for '%s', derived from non-classed parent type '%s'",
923 g_warning ("cannot create class for '%s' as non-classed fundamental",
927 /* check interface size */
928 if (is_interface && info->class_size < sizeof (GTypeInterface))
930 g_warning ("specified interface size for type '%s' is smaller than 'GTypeInterface' size",
934 /* check class size */
935 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
937 if (info->class_size < sizeof (GTypeClass))
939 g_warning ("specified class size for type '%s' is smaller than 'GTypeClass' size",
943 if (pnode && info->class_size < pnode->data->class.class_size)
945 g_warning ("specified class size for type '%s' is smaller "
946 "than the parent type's '%s' class size",
952 /* check instance size */
953 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
955 if (info->instance_size < sizeof (GTypeInstance))
957 g_warning ("specified instance size for type '%s' is smaller than 'GTypeInstance' size",
961 if (pnode && info->instance_size < pnode->data->instance.instance_size)
963 g_warning ("specified instance size for type '%s' is smaller "
964 "than the parent type's '%s' instance size",
975 find_conforming_child_type_L (TypeNode *pnode,
978 TypeNode *node = NULL;
981 if (type_lookup_iface_entry_L (pnode, iface))
984 for (i = 0; i < pnode->n_children && !node; i++)
985 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
991 check_add_interface_L (GType instance_type,
994 TypeNode *node = lookup_type_node_I (instance_type);
995 TypeNode *iface = lookup_type_node_I (iface_type);
998 GType *prerequisites;
1002 if (!node || !node->is_instantiatable)
1004 g_warning ("cannot add interfaces to invalid (non-instantiatable) type '%s'",
1005 type_descriptive_name_I (instance_type));
1008 if (!iface || !NODE_IS_IFACE (iface))
1010 g_warning ("cannot add invalid (non-interface) type '%s' to type '%s'",
1011 type_descriptive_name_I (iface_type),
1015 if (node->data && node->data->class.class)
1017 g_warning ("attempting to add an interface (%s) to class (%s) after class_init",
1018 NODE_NAME (iface), NODE_NAME (node));
1021 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
1022 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
1024 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
1025 g_warning ("cannot add sub-interface '%s' to type '%s' which does not conform to super-interface '%s'",
1031 /* allow overriding of interface type introduced for parent type */
1032 entry = type_lookup_iface_entry_L (node, iface);
1033 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1035 /* ok, we do conform to this interface already, but the interface vtable was not
1036 * yet intialized, and we just conform to the interface because it got added to
1037 * one of our parents. so we allow overriding of holder info here.
1041 /* check whether one of our children already conforms (or whether the interface
1042 * got added to this node already)
1044 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1047 g_warning ("cannot add interface type '%s' to type '%s', since type '%s' already conforms to interface",
1053 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1054 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1056 tnode = lookup_type_node_I (prerequisites[i]);
1057 if (!type_node_is_a_L (node, tnode))
1059 g_warning ("cannot add interface type '%s' to type '%s' which does not conform to prerequisite '%s'",
1070 check_interface_info_I (TypeNode *iface,
1071 GType instance_type,
1072 const GInterfaceInfo *info)
1074 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1076 g_warning ("interface type '%s' for type '%s' comes without initializer",
1078 type_descriptive_name_I (instance_type));
1085 /* --- type info (type node data) --- */
1087 type_data_make_W (TypeNode *node,
1088 const GTypeInfo *info,
1089 const GTypeValueTable *value_table)
1092 GTypeValueTable *vtable = NULL;
1093 guint vtable_size = 0;
1095 g_assert (node->data == NULL && info != NULL);
1099 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1102 vtable = pnode->data->common.value_table;
1105 static const GTypeValueTable zero_vtable = { NULL, };
1107 value_table = &zero_vtable;
1112 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1113 vtable_size = sizeof (GTypeValueTable);
1114 if (value_table->collect_format)
1115 vtable_size += strlen (value_table->collect_format);
1116 if (value_table->lcopy_format)
1117 vtable_size += strlen (value_table->lcopy_format);
1121 if (node->is_instantiatable) /* careful, is_instantiatable is also is_classed */
1123 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1125 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1127 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1128 data->instance.class_size = info->class_size;
1129 data->instance.class_init_base = info->base_init;
1130 data->instance.class_finalize_base = info->base_finalize;
1131 data->instance.class_init = info->class_init;
1132 data->instance.class_finalize = info->class_finalize;
1133 data->instance.class_data = info->class_data;
1134 data->instance.class = NULL;
1135 data->instance.init_state = UNINITIALIZED;
1136 data->instance.instance_size = info->instance_size;
1137 /* We'll set the final value for data->instance.private size
1138 * after the parent class has been initialized
1140 data->instance.private_size = 0;
1141 data->instance.class_private_size = 0;
1143 data->instance.class_private_size = pnode->data->instance.class_private_size;
1144 #ifdef DISABLE_MEM_POOLS
1145 data->instance.n_preallocs = 0;
1146 #else /* !DISABLE_MEM_POOLS */
1147 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1148 #endif /* !DISABLE_MEM_POOLS */
1149 data->instance.instance_init = info->instance_init;
1151 else if (node->is_classed) /* only classed */
1153 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1155 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1157 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1158 data->class.class_size = info->class_size;
1159 data->class.class_init_base = info->base_init;
1160 data->class.class_finalize_base = info->base_finalize;
1161 data->class.class_init = info->class_init;
1162 data->class.class_finalize = info->class_finalize;
1163 data->class.class_data = info->class_data;
1164 data->class.class = NULL;
1165 data->class.class_private_size = 0;
1167 data->class.class_private_size = pnode->data->class.class_private_size;
1168 data->class.init_state = UNINITIALIZED;
1170 else if (NODE_IS_IFACE (node))
1172 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1174 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1175 data->iface.vtable_size = info->class_size;
1176 data->iface.vtable_init_base = info->base_init;
1177 data->iface.vtable_finalize_base = info->base_finalize;
1178 data->iface.dflt_init = info->class_init;
1179 data->iface.dflt_finalize = info->class_finalize;
1180 data->iface.dflt_data = info->class_data;
1181 data->iface.dflt_vtable = NULL;
1183 else if (NODE_IS_BOXED (node))
1185 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1187 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1191 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1193 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1202 /* we allocate the vtable and its strings together with the type data, so
1203 * children can take over their parent's vtable pointer, and we don't
1204 * need to worry freeing it or not when the child data is destroyed
1206 *vtable = *value_table;
1207 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1209 vtable->collect_format = p;
1210 if (value_table->collect_format)
1212 strcat (p, value_table->collect_format);
1213 p += strlen (value_table->collect_format);
1217 vtable->lcopy_format = p;
1218 if (value_table->lcopy_format)
1219 strcat (p, value_table->lcopy_format);
1221 node->data->common.value_table = vtable;
1222 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1223 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1224 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1226 g_assert (node->data->common.value_table != NULL); /* paranoid */
1228 g_atomic_int_set ((int *) &node->ref_count, 1);
1232 type_data_ref_Wm (TypeNode *node)
1236 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1238 GTypeValueTable tmp_value_table;
1240 g_assert (node->plugin != NULL);
1244 type_data_ref_Wm (pnode);
1246 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1249 memset (&tmp_info, 0, sizeof (tmp_info));
1250 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1252 G_WRITE_UNLOCK (&type_rw_lock);
1253 g_type_plugin_use (node->plugin);
1254 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1255 G_WRITE_LOCK (&type_rw_lock);
1257 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1259 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1260 type_data_make_W (node, &tmp_info,
1261 check_value_table_I (NODE_NAME (node),
1262 &tmp_value_table) ? &tmp_value_table : NULL);
1266 g_assert (NODE_REFCOUNT (node) > 0);
1268 g_atomic_int_inc ((int *) &node->ref_count);
1272 static inline gboolean
1273 type_data_ref_U (TypeNode *node)
1278 current = NODE_REFCOUNT (node);
1282 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1288 iface_node_has_available_offset_L (TypeNode *iface_node,
1294 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1295 if (offsets == NULL)
1298 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1301 if (offsets[offset] == 0 ||
1302 offsets[offset] == for_index+1)
1309 find_free_iface_offset_L (IFaceEntries *entries)
1312 TypeNode *iface_node;
1317 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1322 for (i = 0; i < n_entries; i++)
1324 entry = &entries->entry[i];
1325 iface_node = lookup_type_node_I (entry->iface_type);
1327 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1331 while (i != n_entries);
1337 iface_node_set_offset_L (TypeNode *iface_node,
1341 guint8 *offsets, *old_offsets;
1342 int new_size, old_size;
1345 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1346 if (old_offsets == NULL)
1350 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1351 if (offset < old_size &&
1352 old_offsets[offset] == index + 1)
1353 return; /* Already set to this index, return */
1355 new_size = MAX (old_size, offset + 1);
1357 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1358 0, new_size - old_size);
1360 /* Mark new area as unused */
1361 for (i = old_size; i < new_size; i++)
1364 offsets[offset] = index + 1;
1366 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1370 type_node_add_iface_entry_W (TypeNode *node,
1372 IFaceEntry *parent_entry)
1374 IFaceEntries *entries;
1376 TypeNode *iface_node;
1380 g_assert (node->is_instantiatable);
1382 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1383 if (entries != NULL)
1385 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1387 g_assert (num_entries < MAX_N_INTERFACES);
1389 for (i = 0; i < num_entries; i++)
1391 entry = &entries->entry[i];
1392 if (entry->iface_type == iface_type)
1394 /* this can happen in two cases:
1395 * - our parent type already conformed to iface_type and node
1396 * got its own holder info. here, our children already have
1397 * entries and NULL vtables, since this will only work for
1398 * uninitialized classes.
1399 * - an interface type is added to an ancestor after it was
1400 * added to a child type.
1403 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1406 /* sick, interface is added to ancestor *after* child type;
1407 * nothing todo, the entry and our children were already setup correctly
1415 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1416 IFACE_ENTRIES_HEADER_SIZE,
1417 sizeof (IFaceEntry));
1418 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1419 i = num_entries - 1;
1421 entries->offset_index = 0;
1422 entries->entry[i].iface_type = iface_type;
1423 entries->entry[i].vtable = NULL;
1424 entries->entry[i].init_state = UNINITIALIZED;
1428 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1430 entries->entry[i].init_state = INITIALIZED;
1431 entries->entry[i].vtable = parent_entry->vtable;
1435 /* Update offsets in iface */
1436 iface_node = lookup_type_node_I (iface_type);
1438 if (iface_node_has_available_offset_L (iface_node,
1439 entries->offset_index,
1442 iface_node_set_offset_L (iface_node,
1443 entries->offset_index, i);
1447 entries->offset_index =
1448 find_free_iface_offset_L (entries);
1449 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1451 entry = &entries->entry[j];
1453 lookup_type_node_I (entry->iface_type);
1454 iface_node_set_offset_L (iface_node,
1455 entries->offset_index, j);
1459 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1463 for (i = 0; i < node->n_children; i++)
1464 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1469 type_add_interface_Wm (TypeNode *node,
1471 const GInterfaceInfo *info,
1472 GTypePlugin *plugin)
1474 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1478 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1480 iholder->next = iface_node_get_holders_L (iface);
1481 iface_node_set_holders_W (iface, iholder);
1482 iholder->instance_type = NODE_TYPE (node);
1483 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1484 iholder->plugin = plugin;
1486 /* create an iface entry for this type */
1487 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1489 /* if the class is already (partly) initialized, we may need to base
1490 * initalize and/or initialize the new interface.
1494 InitState class_state = node->data->class.init_state;
1496 if (class_state >= BASE_IFACE_INIT)
1497 type_iface_vtable_base_init_Wm (iface, node);
1499 if (class_state >= IFACE_INIT)
1500 type_iface_vtable_iface_init_Wm (iface, node);
1503 /* create iface entries for children of this type */
1504 entry = type_lookup_iface_entry_L (node, iface);
1505 for (i = 0; i < node->n_children; i++)
1506 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1510 type_iface_add_prerequisite_W (TypeNode *iface,
1511 TypeNode *prerequisite_node)
1513 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1514 GType *prerequisites, *dependants;
1515 guint n_dependants, i;
1517 g_assert (NODE_IS_IFACE (iface) &&
1518 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1519 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1521 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1522 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1523 if (prerequisites[i] == prerequisite_type)
1524 return; /* we already have that prerequisiste */
1525 else if (prerequisites[i] > prerequisite_type)
1527 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1528 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1529 IFACE_NODE_PREREQUISITES (iface),
1530 IFACE_NODE_N_PREREQUISITES (iface));
1531 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1532 memmove (prerequisites + i + 1, prerequisites + i,
1533 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1534 prerequisites[i] = prerequisite_type;
1536 /* we want to get notified when prerequisites get added to prerequisite_node */
1537 if (NODE_IS_IFACE (prerequisite_node))
1539 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1540 n_dependants = dependants ? dependants[0] : 0;
1542 dependants = g_renew (GType, dependants, n_dependants + 1);
1543 dependants[n_dependants] = NODE_TYPE (iface);
1544 dependants[0] = n_dependants;
1545 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1548 /* we need to notify all dependants */
1549 dependants = iface_node_get_dependants_array_L (iface);
1550 n_dependants = dependants ? dependants[0] : 0;
1551 for (i = 1; i <= n_dependants; i++)
1552 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1556 * g_type_interface_add_prerequisite:
1557 * @interface_type: #GType value of an interface type
1558 * @prerequisite_type: #GType value of an interface or instantiatable type
1560 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1561 * This means that any type implementing @interface_type must also implement
1562 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1563 * interface derivation (which GType doesn't support). An interface can have
1564 * at most one instantiatable prerequisite type.
1567 g_type_interface_add_prerequisite (GType interface_type,
1568 GType prerequisite_type)
1570 TypeNode *iface, *prerequisite_node;
1571 IFaceHolder *holders;
1573 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1574 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1575 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1577 iface = lookup_type_node_I (interface_type);
1578 prerequisite_node = lookup_type_node_I (prerequisite_type);
1579 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1581 g_warning ("interface type '%s' or prerequisite type '%s' invalid",
1582 type_descriptive_name_I (interface_type),
1583 type_descriptive_name_I (prerequisite_type));
1586 G_WRITE_LOCK (&type_rw_lock);
1587 holders = iface_node_get_holders_L (iface);
1590 G_WRITE_UNLOCK (&type_rw_lock);
1591 g_warning ("unable to add prerequisite '%s' to interface '%s' which is already in use for '%s'",
1592 type_descriptive_name_I (prerequisite_type),
1593 type_descriptive_name_I (interface_type),
1594 type_descriptive_name_I (holders->instance_type));
1597 if (prerequisite_node->is_instantiatable)
1601 /* can have at most one publicly installable instantiatable prerequisite */
1602 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1604 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1606 if (prnode->is_instantiatable)
1608 G_WRITE_UNLOCK (&type_rw_lock);
1609 g_warning ("adding prerequisite '%s' to interface '%s' conflicts with existing prerequisite '%s'",
1610 type_descriptive_name_I (prerequisite_type),
1611 type_descriptive_name_I (interface_type),
1612 type_descriptive_name_I (NODE_TYPE (prnode)));
1617 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1618 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1619 G_WRITE_UNLOCK (&type_rw_lock);
1621 else if (NODE_IS_IFACE (prerequisite_node))
1623 GType *prerequisites;
1626 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1627 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1628 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1629 type_iface_add_prerequisite_W (iface, prerequisite_node);
1630 G_WRITE_UNLOCK (&type_rw_lock);
1634 G_WRITE_UNLOCK (&type_rw_lock);
1635 g_warning ("prerequisite '%s' for interface '%s' is neither instantiatable nor interface",
1636 type_descriptive_name_I (prerequisite_type),
1637 type_descriptive_name_I (interface_type));
1642 * g_type_interface_prerequisites:
1643 * @interface_type: an interface type
1644 * @n_prerequisites: (out) (allow-none): location to return the number
1645 * of prerequisites, or %NULL
1647 * Returns the prerequisites of an interfaces type.
1651 * Returns: (array length=n_prerequisites) (transfer full): a
1652 * newly-allocated zero-terminated array of #GType containing
1653 * the prerequisites of @interface_type
1656 g_type_interface_prerequisites (GType interface_type,
1657 guint *n_prerequisites)
1661 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1663 iface = lookup_type_node_I (interface_type);
1667 TypeNode *inode = NULL;
1670 G_READ_LOCK (&type_rw_lock);
1671 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1672 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1674 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1675 TypeNode *node = lookup_type_node_I (prerequisite);
1676 if (node->is_instantiatable)
1678 if (!inode || type_node_is_a_L (node, inode))
1682 types[n++] = NODE_TYPE (node);
1685 types[n++] = NODE_TYPE (inode);
1687 if (n_prerequisites)
1688 *n_prerequisites = n;
1689 G_READ_UNLOCK (&type_rw_lock);
1695 if (n_prerequisites)
1696 *n_prerequisites = 0;
1704 type_iface_peek_holder_L (TypeNode *iface,
1705 GType instance_type)
1707 IFaceHolder *iholder;
1709 g_assert (NODE_IS_IFACE (iface));
1711 iholder = iface_node_get_holders_L (iface);
1712 while (iholder && iholder->instance_type != instance_type)
1713 iholder = iholder->next;
1718 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1719 GType instance_type,
1722 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1724 if (iholder && !iholder->info && need_info)
1726 GInterfaceInfo tmp_info;
1728 g_assert (iholder->plugin != NULL);
1730 type_data_ref_Wm (iface);
1732 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1734 memset (&tmp_info, 0, sizeof (tmp_info));
1736 G_WRITE_UNLOCK (&type_rw_lock);
1737 g_type_plugin_use (iholder->plugin);
1738 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1739 G_WRITE_LOCK (&type_rw_lock);
1741 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1743 check_interface_info_I (iface, instance_type, &tmp_info);
1744 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1747 return iholder; /* we don't modify write lock upon returning NULL */
1751 type_iface_blow_holder_info_Wm (TypeNode *iface,
1752 GType instance_type)
1754 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1756 g_assert (NODE_IS_IFACE (iface));
1758 while (iholder->instance_type != instance_type)
1759 iholder = iholder->next;
1761 if (iholder->info && iholder->plugin)
1763 g_free (iholder->info);
1764 iholder->info = NULL;
1766 G_WRITE_UNLOCK (&type_rw_lock);
1767 g_type_plugin_unuse (iholder->plugin);
1768 type_data_unref_U (iface, FALSE);
1769 G_WRITE_LOCK (&type_rw_lock);
1774 * g_type_create_instance: (skip)
1775 * @type: an instantiatable type to create an instance for
1777 * Creates and initializes an instance of @type if @type is valid and
1778 * can be instantiated. The type system only performs basic allocation
1779 * and structure setups for instances: actual instance creation should
1780 * happen through functions supplied by the type's fundamental type
1781 * implementation. So use of g_type_create_instance() is reserved for
1782 * implementators of fundamental types only. E.g. instances of the
1783 * #GObject hierarchy should be created via g_object_new() and never
1784 * directly through g_type_create_instance() which doesn't handle things
1785 * like singleton objects or object construction.
1787 * The extended members of the returned instance are guaranteed to be filled
1790 * Note: Do not use this function, unless you're implementing a
1791 * fundamental type. Also language bindings should not use this
1792 * function, but g_object_new() instead.
1794 * Returns: an allocated and initialized instance, subject to further
1795 * treatment by the fundamental type implementation
1798 g_type_create_instance (GType type)
1801 GTypeInstance *instance;
1808 node = lookup_type_node_I (type);
1809 if (!node || !node->is_instantiatable)
1811 g_error ("cannot create new instance of invalid (non-instantiatable) type '%s'",
1812 type_descriptive_name_I (type));
1814 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1815 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1817 g_error ("cannot create instance of abstract (non-instantiatable) type '%s'",
1818 type_descriptive_name_I (type));
1821 class = g_type_class_ref (type);
1823 /* We allocate the 'private' areas before the normal instance data, in
1824 * reverse order. This allows the private area of a particular class
1825 * to always be at a constant relative address to the instance data.
1826 * If we stored the private data after the instance data this would
1827 * not be the case (since a subclass that added more instance
1828 * variables would push the private data further along).
1830 * This presents problems for valgrindability, of course, so we do a
1831 * workaround for that case. We identify the start of the object to
1832 * valgrind as an allocated block (so that pointers to objects show up
1833 * as 'reachable' instead of 'possibly lost'). We then add an extra
1834 * pointer at the end of the object, after all instance data, back to
1835 * the start of the private area so that it is also recorded as
1836 * reachable. We also add extra private space at the start because
1837 * valgrind doesn't seem to like us claiming to have allocated an
1838 * address that it saw allocated by malloc().
1840 private_size = node->data->instance.private_size;
1841 ivar_size = node->data->instance.instance_size;
1843 if (private_size && RUNNING_ON_VALGRIND)
1845 private_size += ALIGN_STRUCT (1);
1847 /* Allocate one extra pointer size... */
1848 allocated = g_slice_alloc0 (private_size + ivar_size + sizeof (gpointer));
1849 /* ... and point it back to the start of the private data. */
1850 *(gpointer *) (allocated + private_size + ivar_size) = allocated + ALIGN_STRUCT (1);
1852 /* Tell valgrind that it should treat the object itself as such */
1853 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, ivar_size + sizeof (gpointer), 0, TRUE);
1854 VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
1857 allocated = g_slice_alloc0 (private_size + ivar_size);
1859 instance = (GTypeInstance *) (allocated + private_size);
1861 for (i = node->n_supers; i > 0; i--)
1865 pnode = lookup_type_node_I (node->supers[i]);
1866 if (pnode->data->instance.instance_init)
1868 instance->g_class = pnode->data->instance.class;
1869 pnode->data->instance.instance_init (instance, class);
1873 instance->g_class = class;
1874 if (node->data->instance.instance_init)
1875 node->data->instance.instance_init (instance, class);
1877 #ifdef G_ENABLE_DEBUG
1878 IF_DEBUG (INSTANCE_COUNT)
1880 g_atomic_int_inc ((int *) &node->instance_count);
1884 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1890 * g_type_free_instance:
1891 * @instance: an instance of a type
1893 * Frees an instance of a type, returning it to the instance pool for
1894 * the type, if there is one.
1896 * Like g_type_create_instance(), this function is reserved for
1897 * implementors of fundamental types.
1900 g_type_free_instance (GTypeInstance *instance)
1908 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1910 class = instance->g_class;
1911 node = lookup_type_node_I (class->g_type);
1912 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1914 g_warning ("cannot free instance of invalid (non-instantiatable) type '%s'",
1915 type_descriptive_name_I (class->g_type));
1918 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1919 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1921 g_warning ("cannot free instance of abstract (non-instantiatable) type '%s'",
1926 instance->g_class = NULL;
1927 private_size = node->data->instance.private_size;
1928 ivar_size = node->data->instance.instance_size;
1929 allocated = ((gchar *) instance) - private_size;
1931 #ifdef G_ENABLE_DEBUG
1932 memset (allocated, 0xaa, ivar_size + private_size);
1935 /* See comment in g_type_create_instance() about what's going on here.
1936 * We're basically unwinding what we put into motion there.
1938 if (private_size && RUNNING_ON_VALGRIND)
1940 private_size += ALIGN_STRUCT (1);
1941 allocated -= ALIGN_STRUCT (1);
1943 /* Clear out the extra pointer... */
1944 *(gpointer *) (allocated + private_size + ivar_size) = NULL;
1945 /* ... and ensure we include it in the size we free. */
1946 g_slice_free1 (private_size + ivar_size + sizeof (gpointer), allocated);
1948 VALGRIND_FREELIKE_BLOCK (allocated + ALIGN_STRUCT (1), 0);
1949 VALGRIND_FREELIKE_BLOCK (instance, 0);
1952 g_slice_free1 (private_size + ivar_size, allocated);
1954 #ifdef G_ENABLE_DEBUG
1955 IF_DEBUG (INSTANCE_COUNT)
1957 g_atomic_int_add ((int *) &node->instance_count, -1);
1961 g_type_class_unref (class);
1965 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1967 g_assert (iface->data);
1969 if (!iface->data->iface.dflt_vtable)
1971 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1972 iface->data->iface.dflt_vtable = vtable;
1973 vtable->g_type = NODE_TYPE (iface);
1974 vtable->g_instance_type = 0;
1975 if (iface->data->iface.vtable_init_base ||
1976 iface->data->iface.dflt_init)
1978 G_WRITE_UNLOCK (&type_rw_lock);
1979 if (iface->data->iface.vtable_init_base)
1980 iface->data->iface.vtable_init_base (vtable);
1981 if (iface->data->iface.dflt_init)
1982 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1983 G_WRITE_LOCK (&type_rw_lock);
1989 /* This is called to allocate and do the first part of initializing
1990 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1992 * A FALSE return indicates that we didn't find an init function for
1993 * this type/iface pair, so the vtable from the parent type should
1994 * be used. Note that the write lock is not modified upon a FALSE
1998 type_iface_vtable_base_init_Wm (TypeNode *iface,
2002 IFaceHolder *iholder;
2003 GTypeInterface *vtable = NULL;
2006 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2007 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
2009 return FALSE; /* we don't modify write lock upon FALSE */
2011 type_iface_ensure_dflt_vtable_Wm (iface);
2013 entry = type_lookup_iface_entry_L (node, iface);
2015 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
2017 entry->init_state = IFACE_INIT;
2019 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2020 if (pnode) /* want to copy over parent iface contents */
2022 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2025 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2028 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2029 entry->vtable = vtable;
2030 vtable->g_type = NODE_TYPE (iface);
2031 vtable->g_instance_type = NODE_TYPE (node);
2033 if (iface->data->iface.vtable_init_base)
2035 G_WRITE_UNLOCK (&type_rw_lock);
2036 iface->data->iface.vtable_init_base (vtable);
2037 G_WRITE_LOCK (&type_rw_lock);
2039 return TRUE; /* initialized the vtable */
2042 /* Finishes what type_iface_vtable_base_init_Wm started by
2043 * calling the interface init function.
2044 * this function may only be called for types with their
2045 * own interface holder info, i.e. types for which
2046 * g_type_add_interface*() was called and not children thereof.
2049 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2052 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2053 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2054 GTypeInterface *vtable = NULL;
2057 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2058 g_assert (iface->data && entry && iholder && iholder->info);
2059 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2061 entry->init_state = INITIALIZED;
2063 vtable = entry->vtable;
2065 if (iholder->info->interface_init)
2067 G_WRITE_UNLOCK (&type_rw_lock);
2068 if (iholder->info->interface_init)
2069 iholder->info->interface_init (vtable, iholder->info->interface_data);
2070 G_WRITE_LOCK (&type_rw_lock);
2073 for (i = 0; i < static_n_iface_check_funcs; i++)
2075 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2076 gpointer check_data = static_iface_check_funcs[i].check_data;
2078 G_WRITE_UNLOCK (&type_rw_lock);
2079 check_func (check_data, (gpointer)vtable);
2080 G_WRITE_LOCK (&type_rw_lock);
2085 type_iface_vtable_finalize_Wm (TypeNode *iface,
2087 GTypeInterface *vtable)
2089 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2090 IFaceHolder *iholder;
2092 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2093 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2095 return FALSE; /* we don't modify write lock upon FALSE */
2097 g_assert (entry && entry->vtable == vtable && iholder->info);
2099 entry->vtable = NULL;
2100 entry->init_state = UNINITIALIZED;
2101 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2103 G_WRITE_UNLOCK (&type_rw_lock);
2104 if (iholder->info->interface_finalize)
2105 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2106 if (iface->data->iface.vtable_finalize_base)
2107 iface->data->iface.vtable_finalize_base (vtable);
2108 G_WRITE_LOCK (&type_rw_lock);
2111 vtable->g_instance_type = 0;
2114 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2116 return TRUE; /* write lock modified */
2120 type_class_init_Wm (TypeNode *node,
2123 GSList *slist, *init_slist = NULL;
2125 IFaceEntries *entries;
2127 TypeNode *bnode, *pnode;
2130 /* Accessing data->class will work for instantiable types
2131 * too because ClassData is a subset of InstanceData
2133 g_assert (node->is_classed && node->data &&
2134 node->data->class.class_size &&
2135 !node->data->class.class &&
2136 node->data->class.init_state == UNINITIALIZED);
2137 if (node->data->class.class_private_size)
2138 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2140 class = g_malloc0 (node->data->class.class_size);
2141 node->data->class.class = class;
2142 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2146 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2148 memcpy (class, pclass, pnode->data->class.class_size);
2149 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);
2151 if (node->is_instantiatable)
2153 /* We need to initialize the private_size here rather than in
2154 * type_data_make_W() since the class init for the parent
2155 * class may have changed pnode->data->instance.private_size.
2157 node->data->instance.private_size = pnode->data->instance.private_size;
2160 class->g_type = NODE_TYPE (node);
2162 G_WRITE_UNLOCK (&type_rw_lock);
2164 /* stack all base class initialization functions, so we
2165 * call them in ascending order.
2167 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2168 if (bnode->data->class.class_init_base)
2169 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2170 for (slist = init_slist; slist; slist = slist->next)
2172 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2174 class_init_base (class);
2176 g_slist_free (init_slist);
2178 G_WRITE_LOCK (&type_rw_lock);
2180 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2182 /* Before we initialize the class, base initialize all interfaces, either
2183 * from parent, or through our holder info
2185 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2188 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2189 i < IFACE_ENTRIES_N_ENTRIES (entries))
2191 entry = &entries->entry[i];
2192 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2193 entry->init_state == IFACE_INIT)
2199 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2202 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2205 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2207 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2208 * doesn't modify write lock upon FALSE, so entry is still valid;
2210 g_assert (pnode != NULL);
2213 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2215 IFaceEntry *pentry = &pentries->entry[j];
2217 if (pentry->iface_type == entry->iface_type)
2219 entry->vtable = pentry->vtable;
2220 entry->init_state = INITIALIZED;
2224 g_assert (entry->vtable != NULL);
2227 /* If the write lock was released, additional interface entries might
2228 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2229 * be base-initialized when inserted, so we don't have to worry that
2230 * we might miss them. Uninitialized entries can only be moved higher
2231 * when new ones are inserted.
2236 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2238 G_WRITE_UNLOCK (&type_rw_lock);
2240 if (node->data->class.class_init)
2241 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2243 G_WRITE_LOCK (&type_rw_lock);
2245 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2247 /* finish initializing the interfaces through our holder info.
2248 * inherited interfaces are already init_state == INITIALIZED, because
2249 * they either got setup in the above base_init loop, or during
2250 * class_init from within type_add_interface_Wm() for this or
2251 * an anchestor type.
2254 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2256 entry = &entries->entry[i];
2257 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2258 entry->init_state == INITIALIZED)
2264 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2267 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2269 /* As in the loop above, additional initialized entries might be inserted
2270 * if the write lock is released, but that's harmless because the entries
2271 * we need to initialize only move higher in the list.
2276 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2280 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2283 IFaceEntries *entries;
2285 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2288 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2289 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2291 IFaceEntry *entry = &entries->entry[i];
2294 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2296 /* refetch entries, IFACES_ENTRIES might be modified */
2301 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2302 * iface vtable came from parent
2304 entry->vtable = NULL;
2305 entry->init_state = UNINITIALIZED;
2312 type_data_finalize_class_U (TypeNode *node,
2315 GTypeClass *class = cdata->class;
2318 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2320 if (cdata->class_finalize)
2321 cdata->class_finalize (class, (gpointer) cdata->class_data);
2323 /* call all base class destruction functions in descending order
2325 if (cdata->class_finalize_base)
2326 cdata->class_finalize_base (class);
2327 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2328 if (bnode->data->class.class_finalize_base)
2329 bnode->data->class.class_finalize_base (class);
2331 g_free (cdata->class);
2335 type_data_last_unref_Wm (TypeNode *node,
2338 g_return_if_fail (node != NULL && node->plugin != NULL);
2340 if (!node->data || NODE_REFCOUNT (node) == 0)
2342 g_warning ("cannot drop last reference to unreferenced type '%s'",
2347 /* call class cache hooks */
2348 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2352 G_WRITE_UNLOCK (&type_rw_lock);
2353 G_READ_LOCK (&type_rw_lock);
2354 for (i = 0; i < static_n_class_cache_funcs; i++)
2356 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2357 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2358 gboolean need_break;
2360 G_READ_UNLOCK (&type_rw_lock);
2361 need_break = cache_func (cache_data, node->data->class.class);
2362 G_READ_LOCK (&type_rw_lock);
2363 if (!node->data || NODE_REFCOUNT (node) == 0)
2364 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2368 G_READ_UNLOCK (&type_rw_lock);
2369 G_WRITE_LOCK (&type_rw_lock);
2372 /* may have been re-referenced meanwhile */
2373 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2375 GType ptype = NODE_PARENT_TYPE (node);
2378 if (node->is_instantiatable)
2380 /* destroy node->data->instance.mem_chunk */
2384 if (node->is_classed && tdata->class.class)
2386 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2387 type_data_finalize_class_ifaces_Wm (node);
2388 node->mutatable_check_cache = FALSE;
2390 G_WRITE_UNLOCK (&type_rw_lock);
2391 type_data_finalize_class_U (node, &tdata->class);
2392 G_WRITE_LOCK (&type_rw_lock);
2394 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2396 node->mutatable_check_cache = FALSE;
2398 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2400 G_WRITE_UNLOCK (&type_rw_lock);
2401 if (tdata->iface.dflt_finalize)
2402 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2403 if (tdata->iface.vtable_finalize_base)
2404 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2405 G_WRITE_LOCK (&type_rw_lock);
2407 g_free (tdata->iface.dflt_vtable);
2411 node->mutatable_check_cache = FALSE;
2415 /* freeing tdata->common.value_table and its contents is taken care of
2416 * by allocating it in one chunk with tdata
2420 G_WRITE_UNLOCK (&type_rw_lock);
2421 g_type_plugin_unuse (node->plugin);
2423 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2424 G_WRITE_LOCK (&type_rw_lock);
2429 type_data_unref_U (TypeNode *node,
2435 current = NODE_REFCOUNT (node);
2441 g_warning ("static type '%s' unreferenced too often",
2447 /* This is the last reference of a type from a plugin. We are
2448 * experimentally disabling support for unloading type
2449 * plugins, so don't allow the last ref to drop.
2454 g_assert (current > 0);
2456 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2457 G_WRITE_LOCK (&type_rw_lock);
2458 type_data_last_unref_Wm (node, uncached);
2459 G_WRITE_UNLOCK (&type_rw_lock);
2460 g_rec_mutex_unlock (&class_init_rec_mutex);
2463 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2467 * g_type_add_class_cache_func: (skip)
2468 * @cache_data: data to be passed to @cache_func
2469 * @cache_func: a #GTypeClassCacheFunc
2471 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2472 * class goes from one to zero. This can be used to prevent premature class
2473 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2474 * until one of them returns %TRUE. The functions have to check the class id
2475 * passed in to figure whether they actually want to cache the class of this
2476 * type, since all classes are routed through the same #GTypeClassCacheFunc
2480 g_type_add_class_cache_func (gpointer cache_data,
2481 GTypeClassCacheFunc cache_func)
2485 g_return_if_fail (cache_func != NULL);
2487 G_WRITE_LOCK (&type_rw_lock);
2488 i = static_n_class_cache_funcs++;
2489 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2490 static_class_cache_funcs[i].cache_data = cache_data;
2491 static_class_cache_funcs[i].cache_func = cache_func;
2492 G_WRITE_UNLOCK (&type_rw_lock);
2496 * g_type_remove_class_cache_func: (skip)
2497 * @cache_data: data that was given when adding @cache_func
2498 * @cache_func: a #GTypeClassCacheFunc
2500 * Removes a previously installed #GTypeClassCacheFunc. The cache
2501 * maintained by @cache_func has to be empty when calling
2502 * g_type_remove_class_cache_func() to avoid leaks.
2505 g_type_remove_class_cache_func (gpointer cache_data,
2506 GTypeClassCacheFunc cache_func)
2508 gboolean found_it = FALSE;
2511 g_return_if_fail (cache_func != NULL);
2513 G_WRITE_LOCK (&type_rw_lock);
2514 for (i = 0; i < static_n_class_cache_funcs; i++)
2515 if (static_class_cache_funcs[i].cache_data == cache_data &&
2516 static_class_cache_funcs[i].cache_func == cache_func)
2518 static_n_class_cache_funcs--;
2519 memmove (static_class_cache_funcs + i,
2520 static_class_cache_funcs + i + 1,
2521 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2522 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2526 G_WRITE_UNLOCK (&type_rw_lock);
2529 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2530 cache_func, cache_data);
2535 * g_type_add_interface_check: (skip)
2536 * @check_data: data to pass to @check_func
2537 * @check_func: function to be called after each interface
2540 * Adds a function to be called after an interface vtable is
2541 * initialized for any class (i.e. after the @interface_init
2542 * member of #GInterfaceInfo has been called).
2544 * This function is useful when you want to check an invariant
2545 * that depends on the interfaces of a class. For instance, the
2546 * implementation of #GObject uses this facility to check that an
2547 * object implements all of the properties that are defined on its
2553 g_type_add_interface_check (gpointer check_data,
2554 GTypeInterfaceCheckFunc check_func)
2558 g_return_if_fail (check_func != NULL);
2560 G_WRITE_LOCK (&type_rw_lock);
2561 i = static_n_iface_check_funcs++;
2562 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2563 static_iface_check_funcs[i].check_data = check_data;
2564 static_iface_check_funcs[i].check_func = check_func;
2565 G_WRITE_UNLOCK (&type_rw_lock);
2569 * g_type_remove_interface_check: (skip)
2570 * @check_data: callback data passed to g_type_add_interface_check()
2571 * @check_func: callback function passed to g_type_add_interface_check()
2573 * Removes an interface check function added with
2574 * g_type_add_interface_check().
2579 g_type_remove_interface_check (gpointer check_data,
2580 GTypeInterfaceCheckFunc check_func)
2582 gboolean found_it = FALSE;
2585 g_return_if_fail (check_func != NULL);
2587 G_WRITE_LOCK (&type_rw_lock);
2588 for (i = 0; i < static_n_iface_check_funcs; i++)
2589 if (static_iface_check_funcs[i].check_data == check_data &&
2590 static_iface_check_funcs[i].check_func == check_func)
2592 static_n_iface_check_funcs--;
2593 memmove (static_iface_check_funcs + i,
2594 static_iface_check_funcs + i + 1,
2595 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2596 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2600 G_WRITE_UNLOCK (&type_rw_lock);
2603 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2604 check_func, check_data);
2607 /* --- type registration --- */
2609 * g_type_register_fundamental:
2610 * @type_id: a predefined type identifier
2611 * @type_name: 0-terminated string used as the name of the new type
2612 * @info: #GTypeInfo structure for this type
2613 * @finfo: #GTypeFundamentalInfo structure for this type
2614 * @flags: bitwise combination of #GTypeFlags values
2616 * Registers @type_id as the predefined identifier and @type_name as the
2617 * name of a fundamental type. If @type_id is already registered, or a
2618 * type named @type_name is already registered, the behaviour is undefined.
2619 * The type system uses the information contained in the #GTypeInfo structure
2620 * pointed to by @info and the #GTypeFundamentalInfo structure pointed to by
2621 * @finfo to manage the type and its instances. The value of @flags determines
2622 * additional characteristics of the fundamental type.
2624 * Returns: the predefined type identifier
2627 g_type_register_fundamental (GType type_id,
2628 const gchar *type_name,
2629 const GTypeInfo *info,
2630 const GTypeFundamentalInfo *finfo,
2635 g_assert_type_system_initialized ();
2636 g_return_val_if_fail (type_id > 0, 0);
2637 g_return_val_if_fail (type_name != NULL, 0);
2638 g_return_val_if_fail (info != NULL, 0);
2639 g_return_val_if_fail (finfo != NULL, 0);
2641 if (!check_type_name_I (type_name))
2643 if ((type_id & TYPE_ID_MASK) ||
2644 type_id > G_TYPE_FUNDAMENTAL_MAX)
2646 g_warning ("attempt to register fundamental type '%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2651 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2652 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2654 g_warning ("cannot register instantiatable fundamental type '%s' as non-classed",
2658 if (lookup_type_node_I (type_id))
2660 g_warning ("cannot register existing fundamental type '%s' (as '%s')",
2661 type_descriptive_name_I (type_id),
2666 G_WRITE_LOCK (&type_rw_lock);
2667 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2668 type_add_flags_W (node, flags);
2670 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2671 type_data_make_W (node, info,
2672 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2673 G_WRITE_UNLOCK (&type_rw_lock);
2675 return NODE_TYPE (node);
2679 * g_type_register_static_simple: (skip)
2680 * @parent_type: type from which this type will be derived
2681 * @type_name: 0-terminated string used as the name of the new type
2682 * @class_size: size of the class structure (see #GTypeInfo)
2683 * @class_init: location of the class initialization function (see #GTypeInfo)
2684 * @instance_size: size of the instance structure (see #GTypeInfo)
2685 * @instance_init: location of the instance initialization function (see #GTypeInfo)
2686 * @flags: bitwise combination of #GTypeFlags values
2688 * Registers @type_name as the name of a new static type derived from
2689 * @parent_type. The value of @flags determines the nature (e.g.
2690 * abstract or not) of the type. It works by filling a #GTypeInfo
2691 * struct and calling g_type_register_static().
2695 * Returns: the new type identifier
2698 g_type_register_static_simple (GType parent_type,
2699 const gchar *type_name,
2701 GClassInitFunc class_init,
2702 guint instance_size,
2703 GInstanceInitFunc instance_init,
2708 /* Instances are not allowed to be larger than this. If you have a big
2709 * fixed-length array or something, point to it instead.
2711 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
2712 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
2714 info.class_size = class_size;
2715 info.base_init = NULL;
2716 info.base_finalize = NULL;
2717 info.class_init = class_init;
2718 info.class_finalize = NULL;
2719 info.class_data = NULL;
2720 info.instance_size = instance_size;
2721 info.n_preallocs = 0;
2722 info.instance_init = instance_init;
2723 info.value_table = NULL;
2725 return g_type_register_static (parent_type, type_name, &info, flags);
2729 * g_type_register_static:
2730 * @parent_type: type from which this type will be derived
2731 * @type_name: 0-terminated string used as the name of the new type
2732 * @info: #GTypeInfo structure for this type
2733 * @flags: bitwise combination of #GTypeFlags values
2735 * Registers @type_name as the name of a new static type derived from
2736 * @parent_type. The type system uses the information contained in the
2737 * #GTypeInfo structure pointed to by @info to manage the type and its
2738 * instances (if not abstract). The value of @flags determines the nature
2739 * (e.g. abstract or not) of the type.
2741 * Returns: the new type identifier
2744 g_type_register_static (GType parent_type,
2745 const gchar *type_name,
2746 const GTypeInfo *info,
2749 TypeNode *pnode, *node;
2752 g_assert_type_system_initialized ();
2753 g_return_val_if_fail (parent_type > 0, 0);
2754 g_return_val_if_fail (type_name != NULL, 0);
2755 g_return_val_if_fail (info != NULL, 0);
2757 if (!check_type_name_I (type_name) ||
2758 !check_derivation_I (parent_type, type_name))
2760 if (info->class_finalize)
2762 g_warning ("class finalizer specified for static type '%s'",
2767 pnode = lookup_type_node_I (parent_type);
2768 G_WRITE_LOCK (&type_rw_lock);
2769 type_data_ref_Wm (pnode);
2770 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2772 node = type_node_new_W (pnode, type_name, NULL);
2773 type_add_flags_W (node, flags);
2774 type = NODE_TYPE (node);
2775 type_data_make_W (node, info,
2776 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2778 G_WRITE_UNLOCK (&type_rw_lock);
2784 * g_type_register_dynamic:
2785 * @parent_type: type from which this type will be derived
2786 * @type_name: 0-terminated string used as the name of the new type
2787 * @plugin: #GTypePlugin structure to retrieve the #GTypeInfo from
2788 * @flags: bitwise combination of #GTypeFlags values
2790 * Registers @type_name as the name of a new dynamic type derived from
2791 * @parent_type. The type system uses the information contained in the
2792 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2793 * instances (if not abstract). The value of @flags determines the nature
2794 * (e.g. abstract or not) of the type.
2796 * Returns: the new type identifier or #G_TYPE_INVALID if registration failed
2799 g_type_register_dynamic (GType parent_type,
2800 const gchar *type_name,
2801 GTypePlugin *plugin,
2804 TypeNode *pnode, *node;
2807 g_assert_type_system_initialized ();
2808 g_return_val_if_fail (parent_type > 0, 0);
2809 g_return_val_if_fail (type_name != NULL, 0);
2810 g_return_val_if_fail (plugin != NULL, 0);
2812 if (!check_type_name_I (type_name) ||
2813 !check_derivation_I (parent_type, type_name) ||
2814 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2817 G_WRITE_LOCK (&type_rw_lock);
2818 pnode = lookup_type_node_I (parent_type);
2819 node = type_node_new_W (pnode, type_name, plugin);
2820 type_add_flags_W (node, flags);
2821 type = NODE_TYPE (node);
2822 G_WRITE_UNLOCK (&type_rw_lock);
2828 * g_type_add_interface_static:
2829 * @instance_type: #GType value of an instantiable type
2830 * @interface_type: #GType value of an interface type
2831 * @info: #GInterfaceInfo structure for this
2832 * (@instance_type, @interface_type) combination
2834 * Adds the static @interface_type to @instantiable_type.
2835 * The information contained in the #GInterfaceInfo structure
2836 * pointed to by @info is used to manage the relationship.
2839 g_type_add_interface_static (GType instance_type,
2840 GType interface_type,
2841 const GInterfaceInfo *info)
2843 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2844 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2845 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2847 /* we only need to lock class_init_rec_mutex if instance_type already has its
2848 * class initialized, however this function is rarely enough called to take
2849 * the simple route and always acquire class_init_rec_mutex.
2851 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2852 G_WRITE_LOCK (&type_rw_lock);
2853 if (check_add_interface_L (instance_type, interface_type))
2855 TypeNode *node = lookup_type_node_I (instance_type);
2856 TypeNode *iface = lookup_type_node_I (interface_type);
2857 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2858 type_add_interface_Wm (node, iface, info, NULL);
2860 G_WRITE_UNLOCK (&type_rw_lock);
2861 g_rec_mutex_unlock (&class_init_rec_mutex);
2865 * g_type_add_interface_dynamic:
2866 * @instance_type: #GType value of an instantiable type
2867 * @interface_type: #GType value of an interface type
2868 * @plugin: #GTypePlugin structure to retrieve the #GInterfaceInfo from
2870 * Adds the dynamic @interface_type to @instantiable_type. The information
2871 * contained in the #GTypePlugin structure pointed to by @plugin
2872 * is used to manage the relationship.
2875 g_type_add_interface_dynamic (GType instance_type,
2876 GType interface_type,
2877 GTypePlugin *plugin)
2880 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2881 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2882 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2884 node = lookup_type_node_I (instance_type);
2885 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2888 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2889 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2890 G_WRITE_LOCK (&type_rw_lock);
2891 if (check_add_interface_L (instance_type, interface_type))
2893 TypeNode *iface = lookup_type_node_I (interface_type);
2894 type_add_interface_Wm (node, iface, NULL, plugin);
2896 G_WRITE_UNLOCK (&type_rw_lock);
2897 g_rec_mutex_unlock (&class_init_rec_mutex);
2901 /* --- public API functions --- */
2904 * @type: type ID of a classed type
2906 * Increments the reference count of the class structure belonging to
2907 * @type. This function will demand-create the class if it doesn't
2910 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
2911 * structure for the given type ID
2914 g_type_class_ref (GType type)
2921 /* optimize for common code path */
2922 node = lookup_type_node_I (type);
2923 if (!node || !node->is_classed)
2925 g_warning ("cannot retrieve class for invalid (unclassed) type '%s'",
2926 type_descriptive_name_I (type));
2930 if (G_LIKELY (type_data_ref_U (node)))
2932 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2933 return node->data->class.class;
2939 /* here, we either have node->data->class.class == NULL, or a recursive
2940 * call to g_type_class_ref() with a partly initialized class, or
2941 * node->data->class.init_state == INITIALIZED, because any
2942 * concurrently running initialization was guarded by class_init_rec_mutex.
2944 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2946 /* we need an initialized parent class for initializing derived classes */
2947 ptype = NODE_PARENT_TYPE (node);
2948 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2950 G_WRITE_LOCK (&type_rw_lock);
2953 type_data_ref_Wm (node);
2955 if (!node->data->class.class) /* class uninitialized */
2956 type_class_init_Wm (node, pclass);
2958 G_WRITE_UNLOCK (&type_rw_lock);
2961 g_type_class_unref (pclass);
2963 g_rec_mutex_unlock (&class_init_rec_mutex);
2965 return node->data->class.class;
2969 * g_type_class_unref:
2970 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2972 * Decrements the reference count of the class structure being passed in.
2973 * Once the last reference count of a class has been released, classes
2974 * may be finalized by the type system, so further dereferencing of a
2975 * class pointer after g_type_class_unref() are invalid.
2978 g_type_class_unref (gpointer g_class)
2981 GTypeClass *class = g_class;
2983 g_return_if_fail (g_class != NULL);
2985 node = lookup_type_node_I (class->g_type);
2986 if (node && node->is_classed && NODE_REFCOUNT (node))
2987 type_data_unref_U (node, FALSE);
2989 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
2990 type_descriptive_name_I (class->g_type));
2994 * g_type_class_unref_uncached: (skip)
2995 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2997 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2998 * implementations. It unreferences a class without consulting the chain
2999 * of #GTypeClassCacheFuncs, avoiding the recursion which would occur
3003 g_type_class_unref_uncached (gpointer g_class)
3006 GTypeClass *class = g_class;
3008 g_return_if_fail (g_class != NULL);
3010 node = lookup_type_node_I (class->g_type);
3011 if (node && node->is_classed && NODE_REFCOUNT (node))
3012 type_data_unref_U (node, TRUE);
3014 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
3015 type_descriptive_name_I (class->g_type));
3019 * g_type_class_peek:
3020 * @type: type ID of a classed type
3022 * This function is essentially the same as g_type_class_ref(),
3023 * except that the classes reference count isn't incremented.
3024 * As a consequence, this function may return %NULL if the class
3025 * of the type passed in does not currently exist (hasn't been
3026 * referenced before).
3028 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3029 * structure for the given type ID or %NULL if the class does not
3033 g_type_class_peek (GType type)
3038 node = lookup_type_node_I (type);
3039 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3040 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3041 /* ref_count _may_ be 0 */
3042 class = node->data->class.class;
3050 * g_type_class_peek_static:
3051 * @type: type ID of a classed type
3053 * A more efficient version of g_type_class_peek() which works only for
3056 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3057 * structure for the given type ID or %NULL if the class does not
3058 * currently exist or is dynamically loaded
3063 g_type_class_peek_static (GType type)
3068 node = lookup_type_node_I (type);
3069 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3070 /* peek only static types: */ node->plugin == NULL &&
3071 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3072 /* ref_count _may_ be 0 */
3073 class = node->data->class.class;
3081 * g_type_class_peek_parent:
3082 * @g_class: (type GObject.TypeClass): the #GTypeClass structure to
3083 * retrieve the parent class for
3085 * This is a convenience function often needed in class initializers.
3086 * It returns the class structure of the immediate parent type of the
3087 * class passed in. Since derived classes hold a reference count on
3088 * their parent classes as long as they are instantiated, the returned
3089 * class will always exist.
3091 * This function is essentially equivalent to:
3092 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)))
3094 * Returns: (type GObject.TypeClass) (transfer none): the parent class
3098 g_type_class_peek_parent (gpointer g_class)
3101 gpointer class = NULL;
3103 g_return_val_if_fail (g_class != NULL, NULL);
3105 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3106 /* We used to acquire a read lock here. That is not necessary, since
3107 * parent->data->class.class is constant as long as the derived class
3110 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3112 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3113 class = node->data->class.class;
3115 else if (NODE_PARENT_TYPE (node))
3116 g_warning (G_STRLOC ": invalid class pointer '%p'", g_class);
3122 * g_type_interface_peek:
3123 * @instance_class: (type GObject.TypeClass): a #GTypeClass structure
3124 * @iface_type: an interface ID which this class conforms to
3126 * Returns the #GTypeInterface structure of an interface to which the
3127 * passed in class conforms.
3129 * Returns: (type GObject.TypeInterface) (transfer none): the #GTypeInterface
3130 * structure of @iface_type if implemented by @instance_class, %NULL
3134 g_type_interface_peek (gpointer instance_class,
3139 gpointer vtable = NULL;
3140 GTypeClass *class = instance_class;
3142 g_return_val_if_fail (instance_class != NULL, NULL);
3144 node = lookup_type_node_I (class->g_type);
3145 iface = lookup_type_node_I (iface_type);
3146 if (node && node->is_instantiatable && iface)
3147 type_lookup_iface_vtable_I (node, iface, &vtable);
3149 g_warning (G_STRLOC ": invalid class pointer '%p'", class);
3155 * g_type_interface_peek_parent:
3156 * @g_iface: (type GObject.TypeInterface): a #GTypeInterface structure
3158 * Returns the corresponding #GTypeInterface structure of the parent type
3159 * of the instance type to which @g_iface belongs. This is useful when
3160 * deriving the implementation of an interface from the parent type and
3161 * then possibly overriding some methods.
3163 * Returns: (transfer none) (type GObject.TypeInterface): the
3164 * corresponding #GTypeInterface structure of the parent type of the
3165 * instance type to which @g_iface belongs, or %NULL if the parent
3166 * type doesn't conform to the interface
3169 g_type_interface_peek_parent (gpointer g_iface)
3173 gpointer vtable = NULL;
3174 GTypeInterface *iface_class = g_iface;
3176 g_return_val_if_fail (g_iface != NULL, NULL);
3178 iface = lookup_type_node_I (iface_class->g_type);
3179 node = lookup_type_node_I (iface_class->g_instance_type);
3181 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3182 if (node && node->is_instantiatable && iface)
3183 type_lookup_iface_vtable_I (node, iface, &vtable);
3185 g_warning (G_STRLOC ": invalid interface pointer '%p'", g_iface);
3191 * g_type_default_interface_ref:
3192 * @g_type: an interface type
3194 * Increments the reference count for the interface type @g_type,
3195 * and returns the default interface vtable for the type.
3197 * If the type is not currently in use, then the default vtable
3198 * for the type will be created and initalized by calling
3199 * the base interface init and default vtable init functions for
3200 * the type (the @base_init and @class_init members of #GTypeInfo).
3201 * Calling g_type_default_interface_ref() is useful when you
3202 * want to make sure that signals and properties for an interface
3203 * have been installed.
3207 * Returns: (type GObject.TypeInterface) (transfer none): the default
3208 * vtable for the interface; call g_type_default_interface_unref()
3209 * when you are done using the interface.
3212 g_type_default_interface_ref (GType g_type)
3215 gpointer dflt_vtable;
3217 G_WRITE_LOCK (&type_rw_lock);
3219 node = lookup_type_node_I (g_type);
3220 if (!node || !NODE_IS_IFACE (node) ||
3221 (node->data && NODE_REFCOUNT (node) == 0))
3223 G_WRITE_UNLOCK (&type_rw_lock);
3224 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3225 type_descriptive_name_I (g_type));
3229 if (!node->data || !node->data->iface.dflt_vtable)
3231 G_WRITE_UNLOCK (&type_rw_lock);
3232 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3233 G_WRITE_LOCK (&type_rw_lock);
3234 node = lookup_type_node_I (g_type);
3235 type_data_ref_Wm (node);
3236 type_iface_ensure_dflt_vtable_Wm (node);
3237 g_rec_mutex_unlock (&class_init_rec_mutex);
3240 type_data_ref_Wm (node); /* ref_count >= 1 already */
3242 dflt_vtable = node->data->iface.dflt_vtable;
3243 G_WRITE_UNLOCK (&type_rw_lock);
3249 * g_type_default_interface_peek:
3250 * @g_type: an interface type
3252 * If the interface type @g_type is currently in use, returns its
3253 * default interface vtable.
3257 * Returns: (type GObject.TypeInterface) (transfer none): the default
3258 * vtable for the interface, or %NULL if the type is not currently
3262 g_type_default_interface_peek (GType g_type)
3267 node = lookup_type_node_I (g_type);
3268 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3269 vtable = node->data->iface.dflt_vtable;
3277 * g_type_default_interface_unref:
3278 * @g_iface: (type GObject.TypeInterface): the default vtable
3279 * structure for a interface, as returned by g_type_default_interface_ref()
3281 * Decrements the reference count for the type corresponding to the
3282 * interface default vtable @g_iface. If the type is dynamic, then
3283 * when no one is using the interface and all references have
3284 * been released, the finalize function for the interface's default
3285 * vtable (the @class_finalize member of #GTypeInfo) will be called.
3290 g_type_default_interface_unref (gpointer g_iface)
3293 GTypeInterface *vtable = g_iface;
3295 g_return_if_fail (g_iface != NULL);
3297 node = lookup_type_node_I (vtable->g_type);
3298 if (node && NODE_IS_IFACE (node))
3299 type_data_unref_U (node, FALSE);
3301 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3302 type_descriptive_name_I (vtable->g_type));
3307 * @type: type to return name for
3309 * Get the unique name that is assigned to a type ID. Note that this
3310 * function (like all other GType API) cannot cope with invalid type
3311 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3312 * other validly registered type ID, but randomized type IDs should
3313 * not be passed in and will most likely lead to a crash.
3315 * Returns: static type name or %NULL
3318 g_type_name (GType type)
3322 g_assert_type_system_initialized ();
3324 node = lookup_type_node_I (type);
3326 return node ? NODE_NAME (node) : NULL;
3331 * @type: type to return quark of type name for
3333 * Get the corresponding quark of the type IDs name.
3335 * Returns: the type names quark or 0
3338 g_type_qname (GType type)
3342 node = lookup_type_node_I (type);
3344 return node ? node->qname : 0;
3349 * @name: type name to lookup
3351 * Lookup the type ID from a given type name, returning 0 if no type
3352 * has been registered under this name (this is the preferred method
3353 * to find out by name whether a specific type has been registered
3356 * Returns: corresponding type ID or 0
3359 g_type_from_name (const gchar *name)
3363 g_return_val_if_fail (name != NULL, 0);
3365 G_READ_LOCK (&type_rw_lock);
3366 type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
3367 G_READ_UNLOCK (&type_rw_lock);
3374 * @type: the derived type
3376 * Return the direct parent type of the passed in type. If the passed
3377 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3379 * Returns: the parent type
3382 g_type_parent (GType type)
3386 node = lookup_type_node_I (type);
3388 return node ? NODE_PARENT_TYPE (node) : 0;
3395 * Returns the length of the ancestry of the passed in type. This
3396 * includes the type itself, so that e.g. a fundamental type has depth 1.
3398 * Returns: the depth of @type
3401 g_type_depth (GType type)
3405 node = lookup_type_node_I (type);
3407 return node ? node->n_supers + 1 : 0;
3412 * @leaf_type: descendant of @root_type and the type to be returned
3413 * @root_type: immediate parent of the returned type
3415 * Given a @leaf_type and a @root_type which is contained in its
3416 * anchestry, return the type that @root_type is the immediate parent
3417 * of. In other words, this function determines the type that is
3418 * derived directly from @root_type which is also a base class of
3419 * @leaf_type. Given a root type and a leaf type, this function can
3420 * be used to determine the types and order in which the leaf type is
3421 * descended from the root type.
3423 * Returns: immediate child of @root_type and anchestor of @leaf_type
3426 g_type_next_base (GType type,
3432 node = lookup_type_node_I (type);
3435 TypeNode *base_node = lookup_type_node_I (base_type);
3437 if (base_node && base_node->n_supers < node->n_supers)
3439 guint n = node->n_supers - base_node->n_supers;
3441 if (node->supers[n] == base_type)
3442 atype = node->supers[n - 1];
3449 static inline gboolean
3450 type_node_check_conformities_UorL (TypeNode *node,
3451 TypeNode *iface_node,
3452 /* support_inheritance */
3453 gboolean support_interfaces,
3454 gboolean support_prerequisites,
3459 if (/* support_inheritance && */
3460 NODE_IS_ANCESTOR (iface_node, node))
3463 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3464 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3466 if (support_interfaces)
3470 if (type_lookup_iface_entry_L (node, iface_node))
3475 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3480 support_prerequisites)
3483 G_READ_LOCK (&type_rw_lock);
3484 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3487 G_READ_UNLOCK (&type_rw_lock);
3493 type_node_is_a_L (TypeNode *node,
3494 TypeNode *iface_node)
3496 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3499 static inline gboolean
3500 type_node_conforms_to_U (TypeNode *node,
3501 TypeNode *iface_node,
3502 gboolean support_interfaces,
3503 gboolean support_prerequisites)
3505 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3510 * @type: type to check anchestry for
3511 * @is_a_type: possible anchestor of @type or interface that @type
3514 * If @is_a_type is a derivable type, check whether @type is a
3515 * descendant of @is_a_type. If @is_a_type is an interface, check
3516 * whether @type conforms to it.
3518 * Returns: %TRUE if @type is a @is_a_type
3521 g_type_is_a (GType type,
3524 TypeNode *node, *iface_node;
3527 if (type == iface_type)
3530 node = lookup_type_node_I (type);
3531 iface_node = lookup_type_node_I (iface_type);
3532 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3539 * @type: the parent type
3540 * @n_children: (out) (allow-none): location to store the length of
3541 * the returned array, or %NULL
3543 * Return a newly allocated and 0-terminated array of type IDs, listing
3544 * the child types of @type.
3546 * Returns: (array length=n_children) (transfer full): Newly allocated
3547 * and 0-terminated array of child types, free with g_free()
3550 g_type_children (GType type,
3555 node = lookup_type_node_I (type);
3560 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3561 children = g_new (GType, node->n_children + 1);
3562 memcpy (children, node->children, sizeof (GType) * node->n_children);
3563 children[node->n_children] = 0;
3566 *n_children = node->n_children;
3567 G_READ_UNLOCK (&type_rw_lock);
3581 * g_type_interfaces:
3582 * @type: the type to list interface types for
3583 * @n_interfaces: (out) (allow-none): location to store the length of
3584 * the returned array, or %NULL
3586 * Return a newly allocated and 0-terminated array of type IDs, listing
3587 * the interface types that @type conforms to.
3589 * Returns: (array length=n_interfaces) (transfer full): Newly allocated
3590 * and 0-terminated array of interface types, free with g_free()
3593 g_type_interfaces (GType type,
3594 guint *n_interfaces)
3598 node = lookup_type_node_I (type);
3599 if (node && node->is_instantiatable)
3601 IFaceEntries *entries;
3605 G_READ_LOCK (&type_rw_lock);
3606 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3609 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3610 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3611 ifaces[i] = entries->entry[i].iface_type;
3615 ifaces = g_new (GType, 1);
3622 G_READ_UNLOCK (&type_rw_lock);
3635 typedef struct _QData QData;
3647 static inline gpointer
3648 type_get_qdata_L (TypeNode *node,
3651 GData *gdata = node->global_gdata;
3653 if (quark && gdata && gdata->n_qdatas)
3655 QData *qdatas = gdata->qdatas - 1;
3656 guint n_qdatas = gdata->n_qdatas;
3663 i = (n_qdatas + 1) / 2;
3665 if (quark == check->quark)
3667 else if (quark > check->quark)
3672 else /* if (quark < check->quark) */
3683 * @quark: a #GQuark id to identify the data
3685 * Obtains data which has previously been attached to @type
3686 * with g_type_set_qdata().
3688 * Note that this does not take subtyping into account; data
3689 * attached to one type with g_type_set_qdata() cannot
3690 * be retrieved from a subtype using g_type_get_qdata().
3692 * Returns: (transfer none): the data, or %NULL if no data was found
3695 g_type_get_qdata (GType type,
3701 node = lookup_type_node_I (type);
3704 G_READ_LOCK (&type_rw_lock);
3705 data = type_get_qdata_L (node, quark);
3706 G_READ_UNLOCK (&type_rw_lock);
3710 g_return_val_if_fail (node != NULL, NULL);
3717 type_set_qdata_W (TypeNode *node,
3725 /* setup qdata list if necessary */
3726 if (!node->global_gdata)
3727 node->global_gdata = g_new0 (GData, 1);
3728 gdata = node->global_gdata;
3730 /* try resetting old data */
3731 qdata = gdata->qdatas;
3732 for (i = 0; i < gdata->n_qdatas; i++)
3733 if (qdata[i].quark == quark)
3735 qdata[i].data = data;
3741 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3742 qdata = gdata->qdatas;
3743 for (i = 0; i < gdata->n_qdatas - 1; i++)
3744 if (qdata[i].quark > quark)
3746 memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3747 qdata[i].quark = quark;
3748 qdata[i].data = data;
3754 * @quark: a #GQuark id to identify the data
3757 * Attaches arbitrary data to a type.
3760 g_type_set_qdata (GType type,
3766 g_return_if_fail (quark != 0);
3768 node = lookup_type_node_I (type);
3771 G_WRITE_LOCK (&type_rw_lock);
3772 type_set_qdata_W (node, quark, data);
3773 G_WRITE_UNLOCK (&type_rw_lock);
3776 g_return_if_fail (node != NULL);
3780 type_add_flags_W (TypeNode *node,
3785 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3786 g_return_if_fail (node != NULL);
3788 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3789 g_warning ("tagging type '%s' as abstract after class initialization", NODE_NAME (node));
3790 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3792 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3797 * @type: #GType of a static, classed type
3798 * @query: (out caller-allocates): a user provided structure that is
3799 * filled in with constant values upon success
3801 * Queries the type system for information about a specific type.
3802 * This function will fill in a user-provided structure to hold
3803 * type-specific information. If an invalid #GType is passed in, the
3804 * @type member of the #GTypeQuery is 0. All members filled into the
3805 * #GTypeQuery structure should be considered constant and have to be
3809 g_type_query (GType type,
3814 g_return_if_fail (query != NULL);
3816 /* if node is not static and classed, we won't allow query */
3818 node = lookup_type_node_I (type);
3819 if (node && node->is_classed && !node->plugin)
3821 /* type is classed and probably even instantiatable */
3822 G_READ_LOCK (&type_rw_lock);
3823 if (node->data) /* type is static or referenced */
3825 query->type = NODE_TYPE (node);
3826 query->type_name = NODE_NAME (node);
3827 query->class_size = node->data->class.class_size;
3828 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3830 G_READ_UNLOCK (&type_rw_lock);
3835 * g_type_get_instance_count:
3838 * Returns the number of instances allocated of the particular type;
3839 * this is only available if GLib is built with debugging support and
3840 * the instance_count debug flag is set (by setting the GOBJECT_DEBUG
3841 * variable to include instance-count).
3843 * Returns: the number of instances allocated of the given type;
3844 * if instance counts are not available, returns 0.
3849 g_type_get_instance_count (GType type)
3851 #ifdef G_ENABLE_DEBUG
3854 node = lookup_type_node_I (type);
3855 g_return_val_if_fail (node != NULL, 0);
3857 return g_atomic_int_get (&node->instance_count);
3863 /* --- implementation details --- */
3865 g_type_test_flags (GType type,
3869 gboolean result = FALSE;
3871 node = lookup_type_node_I (type);
3874 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3875 guint tflags = flags & TYPE_FLAG_MASK;
3879 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3881 fflags = (finfo->type_flags & fflags) == fflags;
3888 G_READ_LOCK (&type_rw_lock);
3889 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3890 G_READ_UNLOCK (&type_rw_lock);
3895 result = tflags && fflags;
3902 * g_type_get_plugin:
3903 * @type: #GType to retrieve the plugin for
3905 * Returns the #GTypePlugin structure for @type.
3907 * Returns: (transfer none): the corresponding plugin
3908 * if @type is a dynamic type, %NULL otherwise
3911 g_type_get_plugin (GType type)
3915 node = lookup_type_node_I (type);
3917 return node ? node->plugin : NULL;
3921 * g_type_interface_get_plugin:
3922 * @instance_type: #GType of an instantiatable type
3923 * @interface_type: #GType of an interface type
3925 * Returns the #GTypePlugin structure for the dynamic interface
3926 * @interface_type which has been added to @instance_type, or %NULL
3927 * if @interface_type has not been added to @instance_type or does
3928 * not have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3930 * Returns: (transfer none): the #GTypePlugin for the dynamic
3931 * interface @interface_type of @instance_type
3934 g_type_interface_get_plugin (GType instance_type,
3935 GType interface_type)
3940 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3942 node = lookup_type_node_I (instance_type);
3943 iface = lookup_type_node_I (interface_type);
3946 IFaceHolder *iholder;
3947 GTypePlugin *plugin;
3949 G_READ_LOCK (&type_rw_lock);
3951 iholder = iface_node_get_holders_L (iface);
3952 while (iholder && iholder->instance_type != instance_type)
3953 iholder = iholder->next;
3954 plugin = iholder ? iholder->plugin : NULL;
3956 G_READ_UNLOCK (&type_rw_lock);
3961 g_return_val_if_fail (node == NULL, NULL);
3962 g_return_val_if_fail (iface == NULL, NULL);
3964 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3970 * g_type_fundamental_next:
3972 * Returns the next free fundamental type id which can be used to
3973 * register a new fundamental type with g_type_register_fundamental().
3974 * The returned type ID represents the highest currently registered
3975 * fundamental type identifier.
3977 * Returns: the next available fundamental type ID to be registered,
3978 * or 0 if the type system ran out of fundamental type IDs
3981 g_type_fundamental_next (void)
3985 G_READ_LOCK (&type_rw_lock);
3986 type = static_fundamental_next;
3987 G_READ_UNLOCK (&type_rw_lock);
3988 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3989 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3993 * g_type_fundamental:
3994 * @type_id: valid type ID
3996 * Internal function, used to extract the fundamental type ID portion.
3997 * Use G_TYPE_FUNDAMENTAL() instead.
3999 * Returns: fundamental type ID
4002 g_type_fundamental (GType type_id)
4004 TypeNode *node = lookup_type_node_I (type_id);
4006 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
4010 g_type_check_instance_is_a (GTypeInstance *type_instance,
4013 TypeNode *node, *iface;
4016 if (!type_instance || !type_instance->g_class)
4019 node = lookup_type_node_I (type_instance->g_class->g_type);
4020 iface = lookup_type_node_I (iface_type);
4021 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4027 g_type_check_instance_is_fundamentally_a (GTypeInstance *type_instance,
4028 GType fundamental_type)
4031 if (!type_instance || !type_instance->g_class)
4033 node = lookup_type_node_I (type_instance->g_class->g_type);
4034 return node && (NODE_FUNDAMENTAL_TYPE(node) == fundamental_type);
4038 g_type_check_class_is_a (GTypeClass *type_class,
4041 TypeNode *node, *iface;
4047 node = lookup_type_node_I (type_class->g_type);
4048 iface = lookup_type_node_I (is_a_type);
4049 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4055 g_type_check_instance_cast (GTypeInstance *type_instance,
4060 if (type_instance->g_class)
4062 TypeNode *node, *iface;
4063 gboolean is_instantiatable, check;
4065 node = lookup_type_node_I (type_instance->g_class->g_type);
4066 is_instantiatable = node && node->is_instantiatable;
4067 iface = lookup_type_node_I (iface_type);
4068 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4070 return type_instance;
4072 if (is_instantiatable)
4073 g_warning ("invalid cast from '%s' to '%s'",
4074 type_descriptive_name_I (type_instance->g_class->g_type),
4075 type_descriptive_name_I (iface_type));
4077 g_warning ("invalid uninstantiatable type '%s' in cast to '%s'",
4078 type_descriptive_name_I (type_instance->g_class->g_type),
4079 type_descriptive_name_I (iface_type));
4082 g_warning ("invalid unclassed pointer in cast to '%s'",
4083 type_descriptive_name_I (iface_type));
4086 return type_instance;
4090 g_type_check_class_cast (GTypeClass *type_class,
4095 TypeNode *node, *iface;
4096 gboolean is_classed, check;
4098 node = lookup_type_node_I (type_class->g_type);
4099 is_classed = node && node->is_classed;
4100 iface = lookup_type_node_I (is_a_type);
4101 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4106 g_warning ("invalid class cast from '%s' to '%s'",
4107 type_descriptive_name_I (type_class->g_type),
4108 type_descriptive_name_I (is_a_type));
4110 g_warning ("invalid unclassed type '%s' in class cast to '%s'",
4111 type_descriptive_name_I (type_class->g_type),
4112 type_descriptive_name_I (is_a_type));
4115 g_warning ("invalid class cast from (NULL) pointer to '%s'",
4116 type_descriptive_name_I (is_a_type));
4121 * g_type_check_instance:
4122 * @instance: a valid #GTypeInstance structure
4124 * Private helper function to aid implementation of the
4125 * G_TYPE_CHECK_INSTANCE() macro.
4127 * Returns: %TRUE if @instance is valid, %FALSE otherwise
4130 g_type_check_instance (GTypeInstance *type_instance)
4132 /* this function is just here to make the signal system
4133 * conveniently elaborated on instance checks
4137 if (type_instance->g_class)
4139 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4141 if (node && node->is_instantiatable)
4144 g_warning ("instance of invalid non-instantiatable type '%s'",
4145 type_descriptive_name_I (type_instance->g_class->g_type));
4148 g_warning ("instance with invalid (NULL) class pointer");
4151 g_warning ("invalid (NULL) pointer instance");
4156 static inline gboolean
4157 type_check_is_value_type_U (GType type)
4159 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4162 /* common path speed up */
4163 node = lookup_type_node_I (type);
4164 if (node && node->mutatable_check_cache)
4167 G_READ_LOCK (&type_rw_lock);
4171 if (node->data && NODE_REFCOUNT (node) > 0 &&
4172 node->data->common.value_table->value_init)
4173 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4174 else if (NODE_IS_IFACE (node))
4178 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4180 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4181 TypeNode *prnode = lookup_type_node_I (prtype);
4183 if (prnode->is_instantiatable)
4186 node = lookup_type_node_I (type);
4192 G_READ_UNLOCK (&type_rw_lock);
4194 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4198 g_type_check_is_value_type (GType type)
4200 return type_check_is_value_type_U (type);
4204 g_type_check_value (GValue *value)
4206 return value && type_check_is_value_type_U (value->g_type);
4210 g_type_check_value_holds (GValue *value,
4213 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4217 * g_type_value_table_peek: (skip)
4220 * Returns the location of the #GTypeValueTable associated with @type.
4222 * Note that this function should only be used from source code
4223 * that implements or has internal knowledge of the implementation of
4226 * Returns: location of the #GTypeValueTable associated with @type or
4227 * %NULL if there is no #GTypeValueTable associated with @type
4230 g_type_value_table_peek (GType type)
4232 GTypeValueTable *vtable = NULL;
4233 TypeNode *node = lookup_type_node_I (type);
4234 gboolean has_refed_data, has_table;
4236 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4237 return node->data->common.value_table;
4239 G_READ_LOCK (&type_rw_lock);
4242 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4243 has_table = has_refed_data && node->data->common.value_table->value_init;
4247 vtable = node->data->common.value_table;
4248 else if (NODE_IS_IFACE (node))
4252 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4254 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4255 TypeNode *prnode = lookup_type_node_I (prtype);
4257 if (prnode->is_instantiatable)
4260 node = lookup_type_node_I (type);
4261 goto restart_table_peek;
4267 G_READ_UNLOCK (&type_rw_lock);
4273 g_warning (G_STRLOC ": type id '%" G_GSIZE_FORMAT "' is invalid", type);
4274 if (!has_refed_data)
4275 g_warning ("can't peek value table for type '%s' which is not currently referenced",
4276 type_descriptive_name_I (type));
4282 g_type_name_from_instance (GTypeInstance *instance)
4285 return "<NULL-instance>";
4287 return g_type_name_from_class (instance->g_class);
4291 g_type_name_from_class (GTypeClass *g_class)
4294 return "<NULL-class>";
4296 return g_type_name (g_class->g_type);
4300 /* --- private api for gboxed.c --- */
4302 _g_type_boxed_copy (GType type, gpointer value)
4304 TypeNode *node = lookup_type_node_I (type);
4306 return node->data->boxed.copy_func (value);
4310 _g_type_boxed_free (GType type, gpointer value)
4312 TypeNode *node = lookup_type_node_I (type);
4314 node->data->boxed.free_func (value);
4318 _g_type_boxed_init (GType type,
4319 GBoxedCopyFunc copy_func,
4320 GBoxedFreeFunc free_func)
4322 TypeNode *node = lookup_type_node_I (type);
4324 node->data->boxed.copy_func = copy_func;
4325 node->data->boxed.free_func = free_func;
4328 /* --- initialization --- */
4330 * g_type_init_with_debug_flags:
4331 * @debug_flags: bitwise combination of #GTypeDebugFlags values for
4332 * debugging purposes
4334 * This function used to initialise the type system with debugging
4335 * flags. Since GLib 2.36, the type system is initialised automatically
4336 * and this function does nothing.
4338 * If you need to enable debugging features, use the GOBJECT_DEBUG
4339 * environment variable.
4341 * Deprecated: 2.36: the type system is now initialised automatically
4344 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4346 g_assert_type_system_initialized ();
4349 g_message ("g_type_init_with_debug_flags() is no longer supported. Use the GOBJECT_DEBUG environment variable.");
4355 * This function used to initialise the type system. Since GLib 2.36,
4356 * the type system is initialised automatically and this function does
4359 * Deprecated: 2.36: the type system is now initialised automatically
4364 g_assert_type_system_initialized ();
4370 const gchar *env_string;
4375 /* Ensure GLib is initialized first, see
4376 * https://bugzilla.gnome.org/show_bug.cgi?id=756139
4378 GLIB_PRIVATE_CALL (glib_init) ();
4380 G_WRITE_LOCK (&type_rw_lock);
4382 /* setup GObject library wide debugging flags */
4383 env_string = g_getenv ("GOBJECT_DEBUG");
4384 if (env_string != NULL)
4386 GDebugKey debug_keys[] = {
4387 { "objects", G_TYPE_DEBUG_OBJECTS },
4388 { "instance-count", G_TYPE_DEBUG_INSTANCE_COUNT },
4389 { "signals", G_TYPE_DEBUG_SIGNALS },
4392 _g_type_debug_flags = g_parse_debug_string (env_string, debug_keys, G_N_ELEMENTS (debug_keys));
4396 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4397 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4398 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4400 /* type qname hash table */
4401 static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
4403 /* invalid type G_TYPE_INVALID (0)
4405 static_fundamental_type_nodes[0] = NULL;
4407 /* void type G_TYPE_NONE
4409 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4410 type = NODE_TYPE (node);
4411 g_assert (type == G_TYPE_NONE);
4413 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4415 memset (&info, 0, sizeof (info));
4416 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4417 type = NODE_TYPE (node);
4418 type_data_make_W (node, &info, NULL);
4419 g_assert (type == G_TYPE_INTERFACE);
4421 G_WRITE_UNLOCK (&type_rw_lock);
4425 /* G_TYPE_TYPE_PLUGIN
4427 g_type_ensure (g_type_plugin_get_type ());
4429 /* G_TYPE_* value types
4431 _g_value_types_init ();
4433 /* G_TYPE_ENUM & G_TYPE_FLAGS
4435 _g_enum_types_init ();
4439 _g_boxed_type_init ();
4443 _g_param_type_init ();
4447 _g_object_type_init ();
4449 /* G_TYPE_PARAM_* pspec types
4451 _g_param_spec_types_init ();
4453 /* Value Transformations
4455 _g_value_transforms_init ();
4462 #if defined (G_OS_WIN32)
4464 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
4466 LPVOID lpvReserved);
4469 DllMain (HINSTANCE hinstDLL,
4475 case DLL_PROCESS_ATTACH:
4487 #elif defined (G_HAS_CONSTRUCTORS)
4488 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
4489 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
4491 G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
4494 gobject_init_ctor (void)
4500 # error Your platform/compiler is missing constructor support
4504 * g_type_class_add_private:
4505 * @g_class: (type GObject.TypeClass): class structure for an instantiatable
4507 * @private_size: size of private structure
4509 * Registers a private structure for an instantiatable type.
4511 * When an object is allocated, the private structures for
4512 * the type and all of its parent types are allocated
4513 * sequentially in the same memory block as the public
4514 * structures, and are zero-filled.
4516 * Note that the accumulated size of the private structures of
4517 * a type and all its parent types cannot exceed 64 KiB.
4519 * This function should be called in the type's class_init() function.
4520 * The private structure can be retrieved using the
4521 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4523 * The following example shows attaching a private structure
4524 * MyObjectPrivate to an object MyObject defined in the standard
4525 * GObject fashion in the type's class_init() function.
4527 * Note the use of a structure member "priv" to avoid the overhead
4528 * of repeatedly calling MY_OBJECT_GET_PRIVATE().
4530 * |[<!-- language="C" -->
4531 * typedef struct _MyObject MyObject;
4532 * typedef struct _MyObjectPrivate MyObjectPrivate;
4534 * struct _MyObject {
4537 * MyObjectPrivate *priv;
4540 * struct _MyObjectPrivate {
4545 * my_object_class_init (MyObjectClass *klass)
4547 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4551 * my_object_init (MyObject *my_object)
4553 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4556 * // my_object->priv->some_field will be automatically initialised to 0
4560 * my_object_get_some_field (MyObject *my_object)
4562 * MyObjectPrivate *priv;
4564 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
4566 * priv = my_object->priv;
4568 * return priv->some_field;
4575 g_type_class_add_private (gpointer g_class,
4578 GType instance_type = ((GTypeClass *)g_class)->g_type;
4579 TypeNode *node = lookup_type_node_I (instance_type);
4581 g_return_if_fail (private_size > 0);
4582 g_return_if_fail (private_size <= 0xffff);
4584 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4586 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4587 type_descriptive_name_I (instance_type));
4591 if (NODE_PARENT_TYPE (node))
4593 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4594 if (node->data->instance.private_size != pnode->data->instance.private_size)
4596 g_warning ("g_type_class_add_private() called multiple times for the same type");
4601 G_WRITE_LOCK (&type_rw_lock);
4603 private_size = ALIGN_STRUCT (node->data->instance.private_size + private_size);
4604 g_assert (private_size <= 0xffff);
4605 node->data->instance.private_size = private_size;
4607 G_WRITE_UNLOCK (&type_rw_lock);
4610 /* semi-private, called only by the G_ADD_PRIVATE macro */
4612 g_type_add_instance_private (GType class_gtype,
4615 TypeNode *node = lookup_type_node_I (class_gtype);
4617 g_return_val_if_fail (private_size > 0, 0);
4618 g_return_val_if_fail (private_size <= 0xffff, 0);
4620 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4622 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4623 type_descriptive_name_I (class_gtype));
4627 if (node->plugin != NULL)
4629 g_warning ("cannot use g_type_add_instance_private() with dynamic type '%s'",
4630 type_descriptive_name_I (class_gtype));
4634 /* in the future, we want to register the private data size of a type
4635 * directly from the get_type() implementation so that we can take full
4636 * advantage of the type definition macros that we already have.
4638 * unfortunately, this does not behave correctly if a class in the middle
4639 * of the type hierarchy uses the "old style" of private data registration
4640 * from the class_init() implementation, as the private data offset is not
4641 * going to be known until the full class hierarchy is initialized.
4643 * in order to transition our code to the Glorious New Future™, we proceed
4644 * with a two-step implementation: first, we provide this new function to
4645 * register the private data size in the get_type() implementation and we
4646 * hide it behind a macro. the function will return the private size, instead
4647 * of the offset, which will be stored inside a static variable defined by
4648 * the G_DEFINE_TYPE_EXTENDED macro. the G_DEFINE_TYPE_EXTENDED macro will
4649 * check the variable and call g_type_class_add_instance_private(), which
4650 * will use the data size and actually register the private data, then
4651 * return the computed offset of the private data, which will be stored
4652 * inside the static variable, so we can use it to retrieve the pointer
4653 * to the private data structure.
4655 * once all our code has been migrated to the new idiomatic form of private
4656 * data registration, we will change the g_type_add_instance_private()
4657 * function to actually perform the registration and return the offset
4658 * of the private data; g_type_class_add_instance_private() already checks
4659 * if the passed argument is negative (meaning that it's an offset in the
4660 * GTypeInstance allocation) and becomes a no-op if that's the case. this
4661 * should make the migration fully transparent even if we're effectively
4662 * copying this macro into everybody's code.
4664 return private_size;
4667 /* semi-private function, should only be used by G_DEFINE_TYPE_EXTENDED */
4669 g_type_class_adjust_private_offset (gpointer g_class,
4670 gint *private_size_or_offset)
4672 GType class_gtype = ((GTypeClass *) g_class)->g_type;
4673 TypeNode *node = lookup_type_node_I (class_gtype);
4674 gssize private_size;
4676 g_return_if_fail (private_size_or_offset != NULL);
4678 /* if we have been passed the offset instead of the private data size,
4679 * then we consider this as a no-op, and just return the value. see the
4680 * comment in g_type_add_instance_private() for the full explanation.
4682 if (*private_size_or_offset > 0)
4683 g_return_if_fail (*private_size_or_offset <= 0xffff);
4687 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4689 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4690 type_descriptive_name_I (class_gtype));
4691 *private_size_or_offset = 0;
4695 if (NODE_PARENT_TYPE (node))
4697 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4698 if (node->data->instance.private_size != pnode->data->instance.private_size)
4700 g_warning ("g_type_add_instance_private() called multiple times for the same type");
4701 *private_size_or_offset = 0;
4706 G_WRITE_LOCK (&type_rw_lock);
4708 private_size = ALIGN_STRUCT (node->data->instance.private_size + *private_size_or_offset);
4709 g_assert (private_size <= 0xffff);
4710 node->data->instance.private_size = private_size;
4712 *private_size_or_offset = -(gint) node->data->instance.private_size;
4714 G_WRITE_UNLOCK (&type_rw_lock);
4718 g_type_instance_get_private (GTypeInstance *instance,
4723 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4725 node = lookup_type_node_I (private_type);
4726 if (G_UNLIKELY (!node || !node->is_instantiatable))
4728 g_warning ("instance of invalid non-instantiatable type '%s'",
4729 type_descriptive_name_I (instance->g_class->g_type));
4733 return ((gchar *) instance) - node->data->instance.private_size;
4737 * g_type_class_get_instance_private_offset: (skip)
4738 * @g_class: (type GObject.TypeClass): a #GTypeClass
4740 * Gets the offset of the private data for instances of @g_class.
4742 * This is how many bytes you should add to the instance pointer of a
4743 * class in order to get the private data for the type represented by
4746 * You can only call this function after you have registered a private
4747 * data area for @g_class using g_type_class_add_private().
4749 * Returns: the offset, in bytes
4754 g_type_class_get_instance_private_offset (gpointer g_class)
4756 GType instance_type;
4757 guint16 parent_size;
4760 g_assert (g_class != NULL);
4762 instance_type = ((GTypeClass *) g_class)->g_type;
4763 node = lookup_type_node_I (instance_type);
4765 g_assert (node != NULL);
4766 g_assert (node->is_instantiatable);
4768 if (NODE_PARENT_TYPE (node))
4770 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4772 parent_size = pnode->data->instance.private_size;
4777 if (node->data->instance.private_size == parent_size)
4778 g_error ("g_type_class_get_instance_private_offset() called on class %s but it has no private data",
4779 g_type_name (instance_type));
4781 return -(gint) node->data->instance.private_size;
4785 * g_type_add_class_private:
4786 * @class_type: GType of an classed type
4787 * @private_size: size of private structure
4789 * Registers a private class structure for a classed type;
4790 * when the class is allocated, the private structures for
4791 * the class and all of its parent types are allocated
4792 * sequentially in the same memory block as the public
4793 * structures, and are zero-filled.
4795 * This function should be called in the
4796 * type's get_type() function after the type is registered.
4797 * The private structure can be retrieved using the
4798 * G_TYPE_CLASS_GET_PRIVATE() macro.
4803 g_type_add_class_private (GType class_type,
4806 TypeNode *node = lookup_type_node_I (class_type);
4809 g_return_if_fail (private_size > 0);
4811 if (!node || !node->is_classed || !node->data)
4813 g_warning ("cannot add class private field to invalid type '%s'",
4814 type_descriptive_name_I (class_type));
4818 if (NODE_PARENT_TYPE (node))
4820 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4821 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4823 g_warning ("g_type_add_class_private() called multiple times for the same type");
4828 G_WRITE_LOCK (&type_rw_lock);
4830 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4831 node->data->class.class_private_size = offset + private_size;
4833 G_WRITE_UNLOCK (&type_rw_lock);
4837 g_type_class_get_private (GTypeClass *klass,
4840 TypeNode *class_node;
4841 TypeNode *private_node;
4842 TypeNode *parent_node;
4845 g_return_val_if_fail (klass != NULL, NULL);
4847 class_node = lookup_type_node_I (klass->g_type);
4848 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4850 g_warning ("class of invalid type '%s'",
4851 type_descriptive_name_I (klass->g_type));
4855 private_node = lookup_type_node_I (private_type);
4856 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4858 g_warning ("attempt to retrieve private data for invalid type '%s'",
4859 type_descriptive_name_I (private_type));
4863 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4865 if (NODE_PARENT_TYPE (private_node))
4867 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4868 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4870 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4872 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_add_class_private()");
4876 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4879 return G_STRUCT_MEMBER_P (klass, offset);
4886 * Ensures that the indicated @type has been registered with the
4887 * type system, and its _class_init() method has been run.
4889 * In theory, simply calling the type's _get_type() method (or using
4890 * the corresponding macro) is supposed take care of this. However,
4891 * _get_type() methods are often marked %G_GNUC_CONST for performance
4892 * reasons, even though this is technically incorrect (since
4893 * %G_GNUC_CONST requires that the function not have side effects,
4894 * which _get_type() methods do on the first call). As a result, if
4895 * you write a bare call to a _get_type() macro, it may get optimized
4896 * out by the compiler. Using g_type_ensure() guarantees that the
4897 * type's _get_type() method is called.
4902 g_type_ensure (GType type)
4904 /* In theory, @type has already been resolved and so there's nothing
4905 * to do here. But this protects us in the case where the function
4906 * gets inlined (as it might in gobject_init_ctor() above).
4908 if (G_UNLIKELY (type == (GType)-1))
4909 g_error ("can't happen");