1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
29 #include "gtype-private.h"
30 #include "gtypeplugin.h"
31 #include "gvaluecollector.h"
32 #include "gbsearcharray.h"
33 #include "gatomicarray.h"
34 #include "gobject_trace.h"
39 * @short_description: The GLib Runtime type identification and
41 * @title:Type Information
43 * The GType API is the foundation of the GObject system. It provides the
44 * facilities for registering and managing all fundamental data types,
45 * user-defined object and interface types. Before using any GType
46 * or GObject functions, g_type_init() must be called to initialize the
49 * For type creation and registration purposes, all types fall into one of
50 * two categories: static or dynamic. Static types are never loaded or
51 * unloaded at run-time as dynamic types may be. Static types are created
52 * with g_type_register_static() that gets type specific information passed
53 * in via a #GTypeInfo structure.
54 * Dynamic types are created with g_type_register_dynamic() which takes a
55 * #GTypePlugin structure instead. The remaining type information (the
56 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
57 * and the g_type_plugin_*() API.
58 * These registration functions are usually called only once from a
59 * function whose only purpose is to return the type identifier for a
60 * specific class. Once the type (or class or interface) is registered,
61 * it may be instantiated, inherited, or implemented depending on exactly
62 * what sort of type it is.
63 * There is also a third registration function for registering fundamental
64 * types called g_type_register_fundamental() which requires both a #GTypeInfo
65 * structure and a #GTypeFundamentalInfo structure but it is seldom used
66 * since most fundamental types are predefined rather than user-defined.
68 * Type instance and class structs are limited to a total of 64 KiB,
69 * including all parent types. Similarly, type instances' private data
70 * (as created by g_type_class_add_private()) are limited to a total of
71 * 64 KiB. If a type instance needs a large static buffer, allocate it
72 * separately (typically by using #GArray or #GPtrArray) and put a pointer
73 * to the buffer in the structure.
75 * A final word about type names.
76 * Such an identifier needs to be at least three characters long. There is no
77 * upper length limit. The first character needs to be a letter (a-z or A-Z)
78 * or an underscore '_'. Subsequent characters can be letters, numbers or
83 /* NOTE: some functions (some internal variants and exported ones)
84 * invalidate data portions of the TypeNodes. if external functions/callbacks
85 * are called, pointers to memory maintained by TypeNodes have to be looked up
86 * again. this affects most of the struct TypeNode fields, e.g. ->children or
87 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
88 * not ->supers[]), as all those memory portions can get realloc()ed during
89 * callback invocation.
92 * lock handling issues when calling static functions are indicated by
93 * uppercase letter postfixes, all static functions have to have
94 * one of the below postfixes:
95 * - _I: [Indifferent about locking]
96 * function doesn't care about locks at all
97 * - _U: [Unlocked invocation]
98 * no read or write lock has to be held across function invocation
99 * (locks may be acquired and released during invocation though)
100 * - _L: [Locked invocation]
101 * a write lock or more than 0 read locks have to be held across
102 * function invocation
103 * - _W: [Write-locked invocation]
104 * a write lock has to be held across function invocation
105 * - _Wm: [Write-locked invocation, mutatable]
106 * like _W, but the write lock might be released and reacquired
107 * during invocation, watch your pointers
108 * - _WmREC: [Write-locked invocation, mutatable, recursive]
109 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
113 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_rw_lock_reader_lock (rw_lock); } while (0)
114 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_rw_lock_reader_unlock (rw_lock); } while (0)
115 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_rw_lock_writer_lock (rw_lock); } while (0)
116 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_rw_lock_writer_unlock (rw_lock); } while (0)
118 #define G_READ_LOCK(rw_lock) g_rw_lock_reader_lock (rw_lock)
119 #define G_READ_UNLOCK(rw_lock) g_rw_lock_reader_unlock (rw_lock)
120 #define G_WRITE_LOCK(rw_lock) g_rw_lock_writer_lock (rw_lock)
121 #define G_WRITE_UNLOCK(rw_lock) g_rw_lock_writer_unlock (rw_lock)
123 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
124 static const gchar _action[] = " invalidly modified type "; \
125 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
127 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
129 g_error ("%s()%s`%s'", _fname, _action, _tname); \
131 #define g_return_val_if_type_system_uninitialized(return_value) G_STMT_START{ \
132 if (G_UNLIKELY (!static_quark_type_flags)) \
134 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
135 "%s: You forgot to call g_type_init()", \
137 return (return_value); \
141 #ifdef G_ENABLE_DEBUG
142 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
143 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
146 #else /* !G_ENABLE_DEBUG */
147 #define DEBUG_CODE(debug_type, code_block) /* code_block */
148 #endif /* G_ENABLE_DEBUG */
150 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
151 G_TYPE_FLAG_INSTANTIATABLE | \
152 G_TYPE_FLAG_DERIVABLE | \
153 G_TYPE_FLAG_DEEP_DERIVABLE)
154 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
155 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
156 sizeof (gpointer)), \
159 /* The 2*sizeof(size_t) alignment here is borrowed from
160 * GNU libc, so it should be good most everywhere.
161 * It is more conservative than is needed on some 64-bit
162 * platforms, but ia64 does require a 16-byte alignment.
163 * The SIMD extensions for x86 and ppc32 would want a
164 * larger alignment than this, but we don't need to
165 * do better than malloc.
167 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
168 #define ALIGN_STRUCT(offset) \
169 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
172 /* --- typedefs --- */
173 typedef struct _TypeNode TypeNode;
174 typedef struct _CommonData CommonData;
175 typedef struct _BoxedData BoxedData;
176 typedef struct _IFaceData IFaceData;
177 typedef struct _ClassData ClassData;
178 typedef struct _InstanceData InstanceData;
179 typedef union _TypeData TypeData;
180 typedef struct _IFaceEntries IFaceEntries;
181 typedef struct _IFaceEntry IFaceEntry;
182 typedef struct _IFaceHolder IFaceHolder;
185 /* --- prototypes --- */
186 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
187 static void type_add_flags_W (TypeNode *node,
189 static void type_data_make_W (TypeNode *node,
190 const GTypeInfo *info,
191 const GTypeValueTable *value_table);
192 static inline void type_data_ref_Wm (TypeNode *node);
193 static inline void type_data_unref_U (TypeNode *node,
195 static void type_data_last_unref_Wm (TypeNode * node,
197 static inline gpointer type_get_qdata_L (TypeNode *node,
199 static inline void type_set_qdata_W (TypeNode *node,
202 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
203 GType instance_type);
204 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
206 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
208 static gboolean type_node_is_a_L (TypeNode *node,
209 TypeNode *iface_node);
212 /* --- enumeration --- */
214 /* The InitState enumeration is used to track the progress of initializing
215 * both classes and interface vtables. Keeping the state of initialization
216 * is necessary to handle new interfaces being added while we are initializing
217 * the class or other interfaces.
229 /* --- structures --- */
232 guint volatile ref_count;
234 guint n_children; /* writable with lock */
236 guint n_prerequisites : 9;
237 guint is_classed : 1;
238 guint is_instantiatable : 1;
239 guint mutatable_check_cache : 1; /* combines some common path checks */
240 GType *children; /* writable with lock */
241 TypeData * volatile data;
245 GAtomicArray iface_entries; /* for !iface types */
246 GAtomicArray offsets;
248 GType *prerequisites;
249 GType supers[1]; /* flexible array */
252 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
253 #define MAX_N_SUPERS (255)
254 #define MAX_N_CHILDREN (4095)
255 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
256 #define MAX_N_PREREQUISITES (511)
257 #define NODE_TYPE(node) (node->supers[0])
258 #define NODE_PARENT_TYPE(node) (node->supers[1])
259 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
260 #define NODE_NAME(node) (g_quark_to_string (node->qname))
261 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
262 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
263 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
264 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
265 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
266 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
267 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
268 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
269 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
270 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
271 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
272 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
274 #define NODE_IS_ANCESTOR(ancestor, node) \
275 ((ancestor)->n_supers <= (node)->n_supers && \
276 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
281 GInterfaceInfo *info;
289 GTypeInterface *vtable;
290 InitState init_state;
293 struct _IFaceEntries {
298 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
299 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
303 GTypeValueTable *value_table;
309 GBoxedCopyFunc copy_func;
310 GBoxedFreeFunc free_func;
317 GBaseInitFunc vtable_init_base;
318 GBaseFinalizeFunc vtable_finalize_base;
319 GClassInitFunc dflt_init;
320 GClassFinalizeFunc dflt_finalize;
321 gconstpointer dflt_data;
322 gpointer dflt_vtable;
329 guint16 class_private_size;
330 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
331 GBaseInitFunc class_init_base;
332 GBaseFinalizeFunc class_finalize_base;
333 GClassInitFunc class_init;
334 GClassFinalizeFunc class_finalize;
335 gconstpointer class_data;
343 guint16 class_private_size;
344 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
345 GBaseInitFunc class_init_base;
346 GBaseFinalizeFunc class_finalize_base;
347 GClassInitFunc class_init;
348 GClassFinalizeFunc class_finalize;
349 gconstpointer class_data;
351 guint16 instance_size;
352 guint16 private_size;
354 GInstanceInitFunc instance_init;
363 InstanceData instance;
368 GTypeClassCacheFunc cache_func;
373 GTypeInterfaceCheckFunc check_func;
377 /* --- variables --- */
378 static GRWLock type_rw_lock;
379 static GRecMutex class_init_rec_mutex;
380 static guint static_n_class_cache_funcs = 0;
381 static ClassCacheFunc *static_class_cache_funcs = NULL;
382 static guint static_n_iface_check_funcs = 0;
383 static IFaceCheckFunc *static_iface_check_funcs = NULL;
384 static GQuark static_quark_type_flags = 0;
385 static GQuark static_quark_iface_holder = 0;
386 static GQuark static_quark_dependants_array = 0;
387 GTypeDebugFlags _g_type_debug_flags = 0;
389 /* --- type nodes --- */
390 static GHashTable *static_type_nodes_ht = NULL;
391 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
392 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
394 static inline TypeNode*
395 lookup_type_node_I (register GType utype)
397 if (utype > G_TYPE_FUNDAMENTAL_MAX)
398 return (TypeNode*) (utype & ~TYPE_ID_MASK);
400 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
404 type_node_any_new_W (TypeNode *pnode,
408 GTypeFundamentalFlags type_flags)
413 guint i, node_size = 0;
415 n_supers = pnode ? pnode->n_supers + 1 : 0;
418 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
419 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
420 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
421 node = g_malloc0 (node_size);
422 if (!pnode) /* offset fundamental types */
424 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
425 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
431 g_assert ((type & TYPE_ID_MASK) == 0);
433 node->n_supers = n_supers;
436 node->supers[0] = type;
439 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
440 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
442 if (NODE_IS_IFACE (node))
444 IFACE_NODE_N_PREREQUISITES (node) = 0;
445 IFACE_NODE_PREREQUISITES (node) = NULL;
448 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
452 node->supers[0] = type;
453 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
455 node->is_classed = pnode->is_classed;
456 node->is_instantiatable = pnode->is_instantiatable;
458 if (NODE_IS_IFACE (node))
460 IFACE_NODE_N_PREREQUISITES (node) = 0;
461 IFACE_NODE_PREREQUISITES (node) = NULL;
466 IFaceEntries *entries;
468 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
469 IFACE_ENTRIES_HEADER_SIZE,
473 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
475 entries->entry[j].vtable = NULL;
476 entries->entry[j].init_state = UNINITIALIZED;
478 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
483 i = pnode->n_children++;
484 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
485 pnode->children[i] = type;
488 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
490 node->plugin = plugin;
491 node->n_children = 0;
492 node->children = NULL;
494 node->qname = g_quark_from_string (name);
495 node->global_gdata = NULL;
497 g_hash_table_insert (static_type_nodes_ht,
498 (gpointer) g_quark_to_string (node->qname),
503 static inline GTypeFundamentalInfo*
504 type_node_fundamental_info_I (TypeNode *node)
506 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
508 if (ftype != NODE_TYPE (node))
509 node = lookup_type_node_I (ftype);
511 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
515 type_node_fundamental_new_W (GType ftype,
517 GTypeFundamentalFlags type_flags)
519 GTypeFundamentalInfo *finfo;
522 g_assert ((ftype & TYPE_ID_MASK) == 0);
523 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
525 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
526 static_fundamental_next++;
528 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
530 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
532 finfo = type_node_fundamental_info_I (node);
533 finfo->type_flags = type_flags;
539 type_node_new_W (TypeNode *pnode,
545 g_assert (pnode->n_supers < MAX_N_SUPERS);
546 g_assert (pnode->n_children < MAX_N_CHILDREN);
548 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
551 static inline IFaceEntry*
552 lookup_iface_entry_I (volatile IFaceEntries *entries,
553 TypeNode *iface_node)
564 G_ATOMIC_ARRAY_DO_TRANSACTION
565 (&iface_node->_prot.offsets, guint8,
568 offsets = transaction_data;
569 offset_index = entries->offset_index;
570 if (offsets != NULL &&
571 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
573 index = offsets[offset_index];
576 /* zero means unset, subtract one to get real index */
579 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
581 check = (IFaceEntry *)&entries->entry[index];
582 if (check->iface_type == NODE_TYPE (iface_node))
592 static inline IFaceEntry*
593 type_lookup_iface_entry_L (TypeNode *node,
594 TypeNode *iface_node)
596 if (!NODE_IS_IFACE (iface_node))
599 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
604 static inline gboolean
605 type_lookup_iface_vtable_I (TypeNode *node,
606 TypeNode *iface_node,
607 gpointer *vtable_ptr)
612 if (!NODE_IS_IFACE (iface_node))
619 G_ATOMIC_ARRAY_DO_TRANSACTION
620 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
622 entry = lookup_iface_entry_I (transaction_data, iface_node);
627 *vtable_ptr = entry->vtable;
636 static inline gboolean
637 type_lookup_prerequisite_L (TypeNode *iface,
638 GType prerequisite_type)
640 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
642 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
643 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
650 i = (n_prerequisites + 1) >> 1;
651 check = prerequisites + i;
652 if (prerequisite_type == *check)
654 else if (prerequisite_type > *check)
656 n_prerequisites -= i;
657 prerequisites = check;
659 else /* if (prerequisite_type < *check) */
660 n_prerequisites = i - 1;
662 while (n_prerequisites);
668 type_descriptive_name_I (GType type)
672 TypeNode *node = lookup_type_node_I (type);
674 return node ? NODE_NAME (node) : "<unknown>";
681 /* --- type consistency checks --- */
683 check_plugin_U (GTypePlugin *plugin,
684 gboolean need_complete_type_info,
685 gboolean need_complete_interface_info,
686 const gchar *type_name)
688 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
692 g_warning ("plugin handle for type `%s' is NULL",
696 if (!G_IS_TYPE_PLUGIN (plugin))
698 g_warning ("plugin pointer (%p) for type `%s' is invalid",
702 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
704 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
708 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
710 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
718 check_type_name_I (const gchar *type_name)
720 static const gchar extra_chars[] = "-_+";
721 const gchar *p = type_name;
724 if (!type_name[0] || !type_name[1] || !type_name[2])
726 g_warning ("type name `%s' is too short", type_name);
729 /* check the first letter */
730 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
731 for (p = type_name + 1; *p; p++)
732 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
733 (p[0] >= 'a' && p[0] <= 'z') ||
734 (p[0] >= '0' && p[0] <= '9') ||
735 strchr (extra_chars, p[0]));
738 g_warning ("type name `%s' contains invalid characters", type_name);
741 if (g_type_from_name (type_name))
743 g_warning ("cannot register existing type `%s'", type_name);
751 check_derivation_I (GType parent_type,
752 const gchar *type_name)
755 GTypeFundamentalInfo* finfo;
757 pnode = lookup_type_node_I (parent_type);
760 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
762 type_descriptive_name_I (parent_type));
765 finfo = type_node_fundamental_info_I (pnode);
766 /* ensure flat derivability */
767 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
769 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
774 /* ensure deep derivability */
775 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
776 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
778 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
788 check_collect_format_I (const gchar *collect_format)
790 const gchar *p = collect_format;
791 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
792 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
793 G_VALUE_COLLECT_POINTER, 0 };
796 if (!strchr (valid_format, *p++))
798 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
802 check_value_table_I (const gchar *type_name,
803 const GTypeValueTable *value_table)
807 else if (value_table->value_init == NULL)
809 if (value_table->value_free || value_table->value_copy ||
810 value_table->value_peek_pointer ||
811 value_table->collect_format || value_table->collect_value ||
812 value_table->lcopy_format || value_table->lcopy_value)
813 g_warning ("cannot handle uninitializable values of type `%s'",
817 else /* value_table->value_init != NULL */
819 if (!value_table->value_free)
822 * g_warning ("missing `value_free()' for type `%s'", type_name);
826 if (!value_table->value_copy)
828 g_warning ("missing `value_copy()' for type `%s'", type_name);
831 if ((value_table->collect_format || value_table->collect_value) &&
832 (!value_table->collect_format || !value_table->collect_value))
834 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
838 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
840 g_warning ("the `%s' specification for type `%s' is too long or invalid",
845 if ((value_table->lcopy_format || value_table->lcopy_value) &&
846 (!value_table->lcopy_format || !value_table->lcopy_value))
848 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
852 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
854 g_warning ("the `%s' specification for type `%s' is too long or invalid",
864 check_type_info_I (TypeNode *pnode,
866 const gchar *type_name,
867 const GTypeInfo *info)
869 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
870 gboolean is_interface = ftype == G_TYPE_INTERFACE;
872 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
874 /* check instance members */
875 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
876 (info->instance_size || info->n_preallocs || info->instance_init))
879 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
883 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
887 /* check class & interface members */
888 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
889 (info->class_init || info->class_finalize || info->class_data ||
890 info->class_size || info->base_init || info->base_finalize))
893 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
897 g_warning ("cannot create class for `%s' as non-classed fundamental",
901 /* check interface size */
902 if (is_interface && info->class_size < sizeof (GTypeInterface))
904 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
908 /* check class size */
909 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
911 if (info->class_size < sizeof (GTypeClass))
913 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
917 if (pnode && info->class_size < pnode->data->class.class_size)
919 g_warning ("specified class size for type `%s' is smaller "
920 "than the parent type's `%s' class size",
926 /* check instance size */
927 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
929 if (info->instance_size < sizeof (GTypeInstance))
931 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
935 if (pnode && info->instance_size < pnode->data->instance.instance_size)
937 g_warning ("specified instance size for type `%s' is smaller "
938 "than the parent type's `%s' instance size",
949 find_conforming_child_type_L (TypeNode *pnode,
952 TypeNode *node = NULL;
955 if (type_lookup_iface_entry_L (pnode, iface))
958 for (i = 0; i < pnode->n_children && !node; i++)
959 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
965 check_add_interface_L (GType instance_type,
968 TypeNode *node = lookup_type_node_I (instance_type);
969 TypeNode *iface = lookup_type_node_I (iface_type);
972 GType *prerequisites;
976 if (!node || !node->is_instantiatable)
978 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
979 type_descriptive_name_I (instance_type));
982 if (!iface || !NODE_IS_IFACE (iface))
984 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
985 type_descriptive_name_I (iface_type),
989 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
990 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
992 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
993 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
999 /* allow overriding of interface type introduced for parent type */
1000 entry = type_lookup_iface_entry_L (node, iface);
1001 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1003 /* ok, we do conform to this interface already, but the interface vtable was not
1004 * yet intialized, and we just conform to the interface because it got added to
1005 * one of our parents. so we allow overriding of holder info here.
1009 /* check whether one of our children already conforms (or whether the interface
1010 * got added to this node already)
1012 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1015 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
1021 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1022 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1024 tnode = lookup_type_node_I (prerequisites[i]);
1025 if (!type_node_is_a_L (node, tnode))
1027 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
1038 check_interface_info_I (TypeNode *iface,
1039 GType instance_type,
1040 const GInterfaceInfo *info)
1042 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1044 g_warning ("interface type `%s' for type `%s' comes without initializer",
1046 type_descriptive_name_I (instance_type));
1053 /* --- type info (type node data) --- */
1055 type_data_make_W (TypeNode *node,
1056 const GTypeInfo *info,
1057 const GTypeValueTable *value_table)
1060 GTypeValueTable *vtable = NULL;
1061 guint vtable_size = 0;
1063 g_assert (node->data == NULL && info != NULL);
1067 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1070 vtable = pnode->data->common.value_table;
1073 static const GTypeValueTable zero_vtable = { NULL, };
1075 value_table = &zero_vtable;
1080 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1081 vtable_size = sizeof (GTypeValueTable);
1082 if (value_table->collect_format)
1083 vtable_size += strlen (value_table->collect_format);
1084 if (value_table->lcopy_format)
1085 vtable_size += strlen (value_table->lcopy_format);
1089 if (node->is_instantiatable) /* careful, is_instantiatable is also is_classed */
1091 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1093 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1095 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1096 data->instance.class_size = info->class_size;
1097 data->instance.class_init_base = info->base_init;
1098 data->instance.class_finalize_base = info->base_finalize;
1099 data->instance.class_init = info->class_init;
1100 data->instance.class_finalize = info->class_finalize;
1101 data->instance.class_data = info->class_data;
1102 data->instance.class = NULL;
1103 data->instance.init_state = UNINITIALIZED;
1104 data->instance.instance_size = info->instance_size;
1105 /* We'll set the final value for data->instance.private size
1106 * after the parent class has been initialized
1108 data->instance.private_size = 0;
1109 data->instance.class_private_size = 0;
1111 data->instance.class_private_size = pnode->data->instance.class_private_size;
1112 #ifdef DISABLE_MEM_POOLS
1113 data->instance.n_preallocs = 0;
1114 #else /* !DISABLE_MEM_POOLS */
1115 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1116 #endif /* !DISABLE_MEM_POOLS */
1117 data->instance.instance_init = info->instance_init;
1119 else if (node->is_classed) /* only classed */
1121 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1123 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1125 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1126 data->class.class_size = info->class_size;
1127 data->class.class_init_base = info->base_init;
1128 data->class.class_finalize_base = info->base_finalize;
1129 data->class.class_init = info->class_init;
1130 data->class.class_finalize = info->class_finalize;
1131 data->class.class_data = info->class_data;
1132 data->class.class = NULL;
1133 data->class.class_private_size = 0;
1135 data->class.class_private_size = pnode->data->class.class_private_size;
1136 data->class.init_state = UNINITIALIZED;
1138 else if (NODE_IS_IFACE (node))
1140 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1142 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1143 data->iface.vtable_size = info->class_size;
1144 data->iface.vtable_init_base = info->base_init;
1145 data->iface.vtable_finalize_base = info->base_finalize;
1146 data->iface.dflt_init = info->class_init;
1147 data->iface.dflt_finalize = info->class_finalize;
1148 data->iface.dflt_data = info->class_data;
1149 data->iface.dflt_vtable = NULL;
1151 else if (NODE_IS_BOXED (node))
1153 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1155 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1159 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1161 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1170 /* we allocate the vtable and its strings together with the type data, so
1171 * children can take over their parent's vtable pointer, and we don't
1172 * need to worry freeing it or not when the child data is destroyed
1174 *vtable = *value_table;
1175 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1177 vtable->collect_format = p;
1178 if (value_table->collect_format)
1180 strcat (p, value_table->collect_format);
1181 p += strlen (value_table->collect_format);
1185 vtable->lcopy_format = p;
1186 if (value_table->lcopy_format)
1187 strcat (p, value_table->lcopy_format);
1189 node->data->common.value_table = vtable;
1190 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1191 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1192 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1194 g_assert (node->data->common.value_table != NULL); /* paranoid */
1196 g_atomic_int_set ((int *) &node->ref_count, 1);
1200 type_data_ref_Wm (TypeNode *node)
1204 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1206 GTypeValueTable tmp_value_table;
1208 g_assert (node->plugin != NULL);
1212 type_data_ref_Wm (pnode);
1214 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1217 memset (&tmp_info, 0, sizeof (tmp_info));
1218 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1220 G_WRITE_UNLOCK (&type_rw_lock);
1221 g_type_plugin_use (node->plugin);
1222 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1223 G_WRITE_LOCK (&type_rw_lock);
1225 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1227 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1228 type_data_make_W (node, &tmp_info,
1229 check_value_table_I (NODE_NAME (node),
1230 &tmp_value_table) ? &tmp_value_table : NULL);
1234 g_assert (NODE_REFCOUNT (node) > 0);
1236 g_atomic_int_inc ((int *) &node->ref_count);
1240 static inline gboolean
1241 type_data_ref_U (TypeNode *node)
1246 current = NODE_REFCOUNT (node);
1250 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1256 iface_node_has_available_offset_L (TypeNode *iface_node,
1262 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1263 if (offsets == NULL)
1266 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1269 if (offsets[offset] == 0 ||
1270 offsets[offset] == for_index+1)
1277 find_free_iface_offset_L (IFaceEntries *entries)
1280 TypeNode *iface_node;
1285 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1290 for (i = 0; i < n_entries; i++)
1292 entry = &entries->entry[i];
1293 iface_node = lookup_type_node_I (entry->iface_type);
1295 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1299 while (i != n_entries);
1305 iface_node_set_offset_L (TypeNode *iface_node,
1309 guint8 *offsets, *old_offsets;
1310 int new_size, old_size;
1313 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1314 if (old_offsets == NULL)
1318 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1319 if (offset < old_size &&
1320 old_offsets[offset] == index + 1)
1321 return; /* Already set to this index, return */
1323 new_size = MAX (old_size, offset + 1);
1325 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1326 0, new_size - old_size);
1328 /* Mark new area as unused */
1329 for (i = old_size; i < new_size; i++)
1332 offsets[offset] = index + 1;
1334 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1338 type_node_add_iface_entry_W (TypeNode *node,
1340 IFaceEntry *parent_entry)
1342 IFaceEntries *entries;
1344 TypeNode *iface_node;
1348 g_assert (node->is_instantiatable);
1350 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1351 if (entries != NULL)
1353 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1355 g_assert (num_entries < MAX_N_INTERFACES);
1357 for (i = 0; i < num_entries; i++)
1359 entry = &entries->entry[i];
1360 if (entry->iface_type == iface_type)
1362 /* this can happen in two cases:
1363 * - our parent type already conformed to iface_type and node
1364 * got its own holder info. here, our children already have
1365 * entries and NULL vtables, since this will only work for
1366 * uninitialized classes.
1367 * - an interface type is added to an ancestor after it was
1368 * added to a child type.
1371 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1374 /* sick, interface is added to ancestor *after* child type;
1375 * nothing todo, the entry and our children were already setup correctly
1383 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1384 IFACE_ENTRIES_HEADER_SIZE,
1385 sizeof (IFaceEntry));
1386 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1387 i = num_entries - 1;
1389 entries->offset_index = 0;
1390 entries->entry[i].iface_type = iface_type;
1391 entries->entry[i].vtable = NULL;
1392 entries->entry[i].init_state = UNINITIALIZED;
1396 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1398 entries->entry[i].init_state = INITIALIZED;
1399 entries->entry[i].vtable = parent_entry->vtable;
1403 /* Update offsets in iface */
1404 iface_node = lookup_type_node_I (iface_type);
1406 if (iface_node_has_available_offset_L (iface_node,
1407 entries->offset_index,
1410 iface_node_set_offset_L (iface_node,
1411 entries->offset_index, i);
1415 entries->offset_index =
1416 find_free_iface_offset_L (entries);
1417 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1419 entry = &entries->entry[j];
1421 lookup_type_node_I (entry->iface_type);
1422 iface_node_set_offset_L (iface_node,
1423 entries->offset_index, j);
1427 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1431 for (i = 0; i < node->n_children; i++)
1432 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1437 type_add_interface_Wm (TypeNode *node,
1439 const GInterfaceInfo *info,
1440 GTypePlugin *plugin)
1442 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1446 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1448 iholder->next = iface_node_get_holders_L (iface);
1449 iface_node_set_holders_W (iface, iholder);
1450 iholder->instance_type = NODE_TYPE (node);
1451 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1452 iholder->plugin = plugin;
1454 /* create an iface entry for this type */
1455 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1457 /* if the class is already (partly) initialized, we may need to base
1458 * initalize and/or initialize the new interface.
1462 InitState class_state = node->data->class.init_state;
1464 if (class_state >= BASE_IFACE_INIT)
1465 type_iface_vtable_base_init_Wm (iface, node);
1467 if (class_state >= IFACE_INIT)
1468 type_iface_vtable_iface_init_Wm (iface, node);
1471 /* create iface entries for children of this type */
1472 entry = type_lookup_iface_entry_L (node, iface);
1473 for (i = 0; i < node->n_children; i++)
1474 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1478 type_iface_add_prerequisite_W (TypeNode *iface,
1479 TypeNode *prerequisite_node)
1481 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1482 GType *prerequisites, *dependants;
1483 guint n_dependants, i;
1485 g_assert (NODE_IS_IFACE (iface) &&
1486 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1487 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1489 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1490 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1491 if (prerequisites[i] == prerequisite_type)
1492 return; /* we already have that prerequisiste */
1493 else if (prerequisites[i] > prerequisite_type)
1495 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1496 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1497 IFACE_NODE_PREREQUISITES (iface),
1498 IFACE_NODE_N_PREREQUISITES (iface));
1499 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1500 g_memmove (prerequisites + i + 1, prerequisites + i,
1501 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1502 prerequisites[i] = prerequisite_type;
1504 /* we want to get notified when prerequisites get added to prerequisite_node */
1505 if (NODE_IS_IFACE (prerequisite_node))
1507 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1508 n_dependants = dependants ? dependants[0] : 0;
1510 dependants = g_renew (GType, dependants, n_dependants + 1);
1511 dependants[n_dependants] = NODE_TYPE (iface);
1512 dependants[0] = n_dependants;
1513 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1516 /* we need to notify all dependants */
1517 dependants = iface_node_get_dependants_array_L (iface);
1518 n_dependants = dependants ? dependants[0] : 0;
1519 for (i = 1; i <= n_dependants; i++)
1520 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1524 * g_type_interface_add_prerequisite:
1525 * @interface_type: #GType value of an interface type.
1526 * @prerequisite_type: #GType value of an interface or instantiatable type.
1528 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1529 * This means that any type implementing @interface_type must also implement
1530 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1531 * interface derivation (which GType doesn't support). An interface can have
1532 * at most one instantiatable prerequisite type.
1535 g_type_interface_add_prerequisite (GType interface_type,
1536 GType prerequisite_type)
1538 TypeNode *iface, *prerequisite_node;
1539 IFaceHolder *holders;
1541 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1542 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1543 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1545 iface = lookup_type_node_I (interface_type);
1546 prerequisite_node = lookup_type_node_I (prerequisite_type);
1547 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1549 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1550 type_descriptive_name_I (interface_type),
1551 type_descriptive_name_I (prerequisite_type));
1554 G_WRITE_LOCK (&type_rw_lock);
1555 holders = iface_node_get_holders_L (iface);
1558 G_WRITE_UNLOCK (&type_rw_lock);
1559 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1560 type_descriptive_name_I (prerequisite_type),
1561 type_descriptive_name_I (interface_type),
1562 type_descriptive_name_I (holders->instance_type));
1565 if (prerequisite_node->is_instantiatable)
1569 /* can have at most one publicly installable instantiatable prerequisite */
1570 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1572 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1574 if (prnode->is_instantiatable)
1576 G_WRITE_UNLOCK (&type_rw_lock);
1577 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1578 type_descriptive_name_I (prerequisite_type),
1579 type_descriptive_name_I (interface_type),
1580 type_descriptive_name_I (NODE_TYPE (prnode)));
1585 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1586 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1587 G_WRITE_UNLOCK (&type_rw_lock);
1589 else if (NODE_IS_IFACE (prerequisite_node))
1591 GType *prerequisites;
1594 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1595 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1596 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1597 type_iface_add_prerequisite_W (iface, prerequisite_node);
1598 G_WRITE_UNLOCK (&type_rw_lock);
1602 G_WRITE_UNLOCK (&type_rw_lock);
1603 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1604 type_descriptive_name_I (prerequisite_type),
1605 type_descriptive_name_I (interface_type));
1610 * g_type_interface_prerequisites:
1611 * @interface_type: an interface type
1612 * @n_prerequisites: (out) (allow-none): location to return the number
1613 * of prerequisites, or %NULL
1615 * Returns the prerequisites of an interfaces type.
1619 * Returns: (array length=n_prerequisites) (transfer full): a
1620 * newly-allocated zero-terminated array of #GType containing
1621 * the prerequisites of @interface_type
1624 g_type_interface_prerequisites (GType interface_type,
1625 guint *n_prerequisites)
1629 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1631 iface = lookup_type_node_I (interface_type);
1635 TypeNode *inode = NULL;
1638 G_READ_LOCK (&type_rw_lock);
1639 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1640 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1642 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1643 TypeNode *node = lookup_type_node_I (prerequisite);
1644 if (node->is_instantiatable)
1646 if (!inode || type_node_is_a_L (node, inode))
1650 types[n++] = NODE_TYPE (node);
1653 types[n++] = NODE_TYPE (inode);
1655 if (n_prerequisites)
1656 *n_prerequisites = n;
1657 G_READ_UNLOCK (&type_rw_lock);
1663 if (n_prerequisites)
1664 *n_prerequisites = 0;
1672 type_iface_peek_holder_L (TypeNode *iface,
1673 GType instance_type)
1675 IFaceHolder *iholder;
1677 g_assert (NODE_IS_IFACE (iface));
1679 iholder = iface_node_get_holders_L (iface);
1680 while (iholder && iholder->instance_type != instance_type)
1681 iholder = iholder->next;
1686 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1687 GType instance_type,
1690 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1692 if (iholder && !iholder->info && need_info)
1694 GInterfaceInfo tmp_info;
1696 g_assert (iholder->plugin != NULL);
1698 type_data_ref_Wm (iface);
1700 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1702 memset (&tmp_info, 0, sizeof (tmp_info));
1704 G_WRITE_UNLOCK (&type_rw_lock);
1705 g_type_plugin_use (iholder->plugin);
1706 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1707 G_WRITE_LOCK (&type_rw_lock);
1709 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1711 check_interface_info_I (iface, instance_type, &tmp_info);
1712 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1715 return iholder; /* we don't modify write lock upon returning NULL */
1719 type_iface_blow_holder_info_Wm (TypeNode *iface,
1720 GType instance_type)
1722 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1724 g_assert (NODE_IS_IFACE (iface));
1726 while (iholder->instance_type != instance_type)
1727 iholder = iholder->next;
1729 if (iholder->info && iholder->plugin)
1731 g_free (iholder->info);
1732 iholder->info = NULL;
1734 G_WRITE_UNLOCK (&type_rw_lock);
1735 g_type_plugin_unuse (iholder->plugin);
1736 type_data_unref_U (iface, FALSE);
1737 G_WRITE_LOCK (&type_rw_lock);
1741 /* Assumes type's class already exists
1743 static inline size_t
1744 type_total_instance_size_I (TypeNode *node)
1746 gsize total_instance_size;
1748 total_instance_size = node->data->instance.instance_size;
1749 if (node->data->instance.private_size != 0)
1750 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1752 return total_instance_size;
1755 /* --- type structure creation/destruction --- */
1759 } InstanceRealClass;
1762 instance_real_class_cmp (gconstpointer p1,
1765 const InstanceRealClass *irc1 = p1;
1766 const InstanceRealClass *irc2 = p2;
1767 guint8 *i1 = irc1->instance;
1768 guint8 *i2 = irc2->instance;
1769 return G_BSEARCH_ARRAY_CMP (i1, i2);
1772 G_LOCK_DEFINE_STATIC (instance_real_class);
1773 static GBSearchArray *instance_real_class_bsa = NULL;
1774 static GBSearchConfig instance_real_class_bconfig = {
1775 sizeof (InstanceRealClass),
1776 instance_real_class_cmp,
1781 instance_real_class_set (gpointer instance,
1784 InstanceRealClass key;
1785 key.instance = instance;
1787 G_LOCK (instance_real_class);
1788 if (!instance_real_class_bsa)
1789 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1790 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1791 G_UNLOCK (instance_real_class);
1795 instance_real_class_remove (gpointer instance)
1797 InstanceRealClass key, *node;
1799 key.instance = instance;
1800 G_LOCK (instance_real_class);
1801 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1802 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1803 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1804 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1806 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1807 instance_real_class_bsa = NULL;
1809 G_UNLOCK (instance_real_class);
1812 static inline GTypeClass*
1813 instance_real_class_get (gpointer instance)
1815 InstanceRealClass key, *node;
1817 key.instance = instance;
1818 G_LOCK (instance_real_class);
1819 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1820 class = node ? node->class : NULL;
1821 G_UNLOCK (instance_real_class);
1826 * g_type_create_instance: (skip)
1827 * @type: An instantiatable type to create an instance for.
1829 * Creates and initializes an instance of @type if @type is valid and
1830 * can be instantiated. The type system only performs basic allocation
1831 * and structure setups for instances: actual instance creation should
1832 * happen through functions supplied by the type's fundamental type
1833 * implementation. So use of g_type_create_instance() is reserved for
1834 * implementators of fundamental types only. E.g. instances of the
1835 * #GObject hierarchy should be created via g_object_new() and
1836 * <emphasis>never</emphasis> directly through
1837 * g_type_create_instance() which doesn't handle things like singleton
1838 * objects or object construction. Note: Do <emphasis>not</emphasis>
1839 * use this function, unless you're implementing a fundamental
1840 * type. Also language bindings should <emphasis>not</emphasis> use
1841 * this function but g_object_new() instead.
1843 * Returns: An allocated and initialized instance, subject to further
1844 * treatment by the fundamental type implementation.
1847 g_type_create_instance (GType type)
1850 GTypeInstance *instance;
1852 guint i, total_size;
1854 node = lookup_type_node_I (type);
1855 if (!node || !node->is_instantiatable)
1857 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1858 type_descriptive_name_I (type));
1861 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1862 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1864 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1865 type_descriptive_name_I (type));
1869 class = g_type_class_ref (type);
1870 total_size = type_total_instance_size_I (node);
1872 instance = g_slice_alloc0 (total_size);
1874 if (node->data->instance.private_size)
1875 instance_real_class_set (instance, class);
1876 for (i = node->n_supers; i > 0; i--)
1880 pnode = lookup_type_node_I (node->supers[i]);
1881 if (pnode->data->instance.instance_init)
1883 instance->g_class = pnode->data->instance.class;
1884 pnode->data->instance.instance_init (instance, class);
1887 if (node->data->instance.private_size)
1888 instance_real_class_remove (instance);
1890 instance->g_class = class;
1891 if (node->data->instance.instance_init)
1892 node->data->instance.instance_init (instance, class);
1894 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1900 * g_type_free_instance:
1901 * @instance: an instance of a type.
1903 * Frees an instance of a type, returning it to the instance pool for
1904 * the type, if there is one.
1906 * Like g_type_create_instance(), this function is reserved for
1907 * implementors of fundamental types.
1910 g_type_free_instance (GTypeInstance *instance)
1915 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1917 class = instance->g_class;
1918 node = lookup_type_node_I (class->g_type);
1919 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1921 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1922 type_descriptive_name_I (class->g_type));
1925 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1926 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1928 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1933 instance->g_class = NULL;
1934 #ifdef G_ENABLE_DEBUG
1935 memset (instance, 0xaa, type_total_instance_size_I (node));
1937 g_slice_free1 (type_total_instance_size_I (node), instance);
1939 g_type_class_unref (class);
1943 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1945 g_assert (iface->data);
1947 if (!iface->data->iface.dflt_vtable)
1949 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1950 iface->data->iface.dflt_vtable = vtable;
1951 vtable->g_type = NODE_TYPE (iface);
1952 vtable->g_instance_type = 0;
1953 if (iface->data->iface.vtable_init_base ||
1954 iface->data->iface.dflt_init)
1956 G_WRITE_UNLOCK (&type_rw_lock);
1957 if (iface->data->iface.vtable_init_base)
1958 iface->data->iface.vtable_init_base (vtable);
1959 if (iface->data->iface.dflt_init)
1960 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1961 G_WRITE_LOCK (&type_rw_lock);
1967 /* This is called to allocate and do the first part of initializing
1968 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1970 * A FALSE return indicates that we didn't find an init function for
1971 * this type/iface pair, so the vtable from the parent type should
1972 * be used. Note that the write lock is not modified upon a FALSE
1976 type_iface_vtable_base_init_Wm (TypeNode *iface,
1980 IFaceHolder *iholder;
1981 GTypeInterface *vtable = NULL;
1984 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1985 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1987 return FALSE; /* we don't modify write lock upon FALSE */
1989 type_iface_ensure_dflt_vtable_Wm (iface);
1991 entry = type_lookup_iface_entry_L (node, iface);
1993 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1995 entry->init_state = IFACE_INIT;
1997 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1998 if (pnode) /* want to copy over parent iface contents */
2000 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2003 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2006 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2007 entry->vtable = vtable;
2008 vtable->g_type = NODE_TYPE (iface);
2009 vtable->g_instance_type = NODE_TYPE (node);
2011 if (iface->data->iface.vtable_init_base)
2013 G_WRITE_UNLOCK (&type_rw_lock);
2014 iface->data->iface.vtable_init_base (vtable);
2015 G_WRITE_LOCK (&type_rw_lock);
2017 return TRUE; /* initialized the vtable */
2020 /* Finishes what type_iface_vtable_base_init_Wm started by
2021 * calling the interface init function.
2022 * this function may only be called for types with their
2023 * own interface holder info, i.e. types for which
2024 * g_type_add_interface*() was called and not children thereof.
2027 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2030 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2031 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2032 GTypeInterface *vtable = NULL;
2035 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2036 g_assert (iface->data && entry && iholder && iholder->info);
2037 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2039 entry->init_state = INITIALIZED;
2041 vtable = entry->vtable;
2043 if (iholder->info->interface_init)
2045 G_WRITE_UNLOCK (&type_rw_lock);
2046 if (iholder->info->interface_init)
2047 iholder->info->interface_init (vtable, iholder->info->interface_data);
2048 G_WRITE_LOCK (&type_rw_lock);
2051 for (i = 0; i < static_n_iface_check_funcs; i++)
2053 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2054 gpointer check_data = static_iface_check_funcs[i].check_data;
2056 G_WRITE_UNLOCK (&type_rw_lock);
2057 check_func (check_data, (gpointer)vtable);
2058 G_WRITE_LOCK (&type_rw_lock);
2063 type_iface_vtable_finalize_Wm (TypeNode *iface,
2065 GTypeInterface *vtable)
2067 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2068 IFaceHolder *iholder;
2070 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2071 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2073 return FALSE; /* we don't modify write lock upon FALSE */
2075 g_assert (entry && entry->vtable == vtable && iholder->info);
2077 entry->vtable = NULL;
2078 entry->init_state = UNINITIALIZED;
2079 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2081 G_WRITE_UNLOCK (&type_rw_lock);
2082 if (iholder->info->interface_finalize)
2083 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2084 if (iface->data->iface.vtable_finalize_base)
2085 iface->data->iface.vtable_finalize_base (vtable);
2086 G_WRITE_LOCK (&type_rw_lock);
2089 vtable->g_instance_type = 0;
2092 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2094 return TRUE; /* write lock modified */
2098 type_class_init_Wm (TypeNode *node,
2101 GSList *slist, *init_slist = NULL;
2103 IFaceEntries *entries;
2105 TypeNode *bnode, *pnode;
2108 /* Accessing data->class will work for instantiable types
2109 * too because ClassData is a subset of InstanceData
2111 g_assert (node->is_classed && node->data &&
2112 node->data->class.class_size &&
2113 !node->data->class.class &&
2114 node->data->class.init_state == UNINITIALIZED);
2115 if (node->data->class.class_private_size)
2116 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2118 class = g_malloc0 (node->data->class.class_size);
2119 node->data->class.class = class;
2120 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2124 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2126 memcpy (class, pclass, pnode->data->class.class_size);
2127 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);
2129 if (node->is_instantiatable)
2131 /* We need to initialize the private_size here rather than in
2132 * type_data_make_W() since the class init for the parent
2133 * class may have changed pnode->data->instance.private_size.
2135 node->data->instance.private_size = pnode->data->instance.private_size;
2138 class->g_type = NODE_TYPE (node);
2140 G_WRITE_UNLOCK (&type_rw_lock);
2142 /* stack all base class initialization functions, so we
2143 * call them in ascending order.
2145 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2146 if (bnode->data->class.class_init_base)
2147 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2148 for (slist = init_slist; slist; slist = slist->next)
2150 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2152 class_init_base (class);
2154 g_slist_free (init_slist);
2156 G_WRITE_LOCK (&type_rw_lock);
2158 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2160 /* Before we initialize the class, base initialize all interfaces, either
2161 * from parent, or through our holder info
2163 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2166 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2167 i < IFACE_ENTRIES_N_ENTRIES (entries))
2169 entry = &entries->entry[i];
2170 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2171 entry->init_state == IFACE_INIT)
2177 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2180 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2183 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2185 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2186 * doesn't modify write lock upon FALSE, so entry is still valid;
2188 g_assert (pnode != NULL);
2191 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2193 IFaceEntry *pentry = &pentries->entry[j];
2195 if (pentry->iface_type == entry->iface_type)
2197 entry->vtable = pentry->vtable;
2198 entry->init_state = INITIALIZED;
2202 g_assert (entry->vtable != NULL);
2205 /* If the write lock was released, additional interface entries might
2206 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2207 * be base-initialized when inserted, so we don't have to worry that
2208 * we might miss them. Uninitialized entries can only be moved higher
2209 * when new ones are inserted.
2214 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2216 G_WRITE_UNLOCK (&type_rw_lock);
2218 if (node->data->class.class_init)
2219 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2221 G_WRITE_LOCK (&type_rw_lock);
2223 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2225 /* finish initializing the interfaces through our holder info.
2226 * inherited interfaces are already init_state == INITIALIZED, because
2227 * they either got setup in the above base_init loop, or during
2228 * class_init from within type_add_interface_Wm() for this or
2229 * an anchestor type.
2232 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2234 entry = &entries->entry[i];
2235 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2236 entry->init_state == INITIALIZED)
2242 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2245 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2247 /* As in the loop above, additional initialized entries might be inserted
2248 * if the write lock is released, but that's harmless because the entries
2249 * we need to initialize only move higher in the list.
2254 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2258 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2261 IFaceEntries *entries;
2263 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2266 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2267 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2269 IFaceEntry *entry = &entries->entry[i];
2272 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2274 /* refetch entries, IFACES_ENTRIES might be modified */
2279 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2280 * iface vtable came from parent
2282 entry->vtable = NULL;
2283 entry->init_state = UNINITIALIZED;
2290 type_data_finalize_class_U (TypeNode *node,
2293 GTypeClass *class = cdata->class;
2296 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2298 if (cdata->class_finalize)
2299 cdata->class_finalize (class, (gpointer) cdata->class_data);
2301 /* call all base class destruction functions in descending order
2303 if (cdata->class_finalize_base)
2304 cdata->class_finalize_base (class);
2305 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2306 if (bnode->data->class.class_finalize_base)
2307 bnode->data->class.class_finalize_base (class);
2309 g_free (cdata->class);
2313 type_data_last_unref_Wm (TypeNode *node,
2316 g_return_if_fail (node != NULL && node->plugin != NULL);
2318 if (!node->data || NODE_REFCOUNT (node) == 0)
2320 g_warning ("cannot drop last reference to unreferenced type `%s'",
2325 /* call class cache hooks */
2326 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2330 G_WRITE_UNLOCK (&type_rw_lock);
2331 G_READ_LOCK (&type_rw_lock);
2332 for (i = 0; i < static_n_class_cache_funcs; i++)
2334 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2335 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2336 gboolean need_break;
2338 G_READ_UNLOCK (&type_rw_lock);
2339 need_break = cache_func (cache_data, node->data->class.class);
2340 G_READ_LOCK (&type_rw_lock);
2341 if (!node->data || NODE_REFCOUNT (node) == 0)
2342 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2346 G_READ_UNLOCK (&type_rw_lock);
2347 G_WRITE_LOCK (&type_rw_lock);
2350 /* may have been re-referenced meanwhile */
2351 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2353 GType ptype = NODE_PARENT_TYPE (node);
2356 if (node->is_instantiatable)
2358 /* destroy node->data->instance.mem_chunk */
2362 if (node->is_classed && tdata->class.class)
2364 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2365 type_data_finalize_class_ifaces_Wm (node);
2366 node->mutatable_check_cache = FALSE;
2368 G_WRITE_UNLOCK (&type_rw_lock);
2369 type_data_finalize_class_U (node, &tdata->class);
2370 G_WRITE_LOCK (&type_rw_lock);
2372 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2374 node->mutatable_check_cache = FALSE;
2376 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2378 G_WRITE_UNLOCK (&type_rw_lock);
2379 if (tdata->iface.dflt_finalize)
2380 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2381 if (tdata->iface.vtable_finalize_base)
2382 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2383 G_WRITE_LOCK (&type_rw_lock);
2385 g_free (tdata->iface.dflt_vtable);
2389 node->mutatable_check_cache = FALSE;
2393 /* freeing tdata->common.value_table and its contents is taken care of
2394 * by allocating it in one chunk with tdata
2398 G_WRITE_UNLOCK (&type_rw_lock);
2399 g_type_plugin_unuse (node->plugin);
2401 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2402 G_WRITE_LOCK (&type_rw_lock);
2407 type_data_unref_U (TypeNode *node,
2413 current = NODE_REFCOUNT (node);
2419 g_warning ("static type `%s' unreferenced too often",
2424 g_assert (current > 0);
2426 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2427 G_WRITE_LOCK (&type_rw_lock);
2428 type_data_last_unref_Wm (node, uncached);
2429 G_WRITE_UNLOCK (&type_rw_lock);
2430 g_rec_mutex_unlock (&class_init_rec_mutex);
2433 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2437 * g_type_add_class_cache_func: (skip)
2438 * @cache_data: data to be passed to @cache_func
2439 * @cache_func: a #GTypeClassCacheFunc
2441 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2442 * class goes from one to zero. This can be used to prevent premature class
2443 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2444 * until one of them returns %TRUE. The functions have to check the class id
2445 * passed in to figure whether they actually want to cache the class of this
2446 * type, since all classes are routed through the same #GTypeClassCacheFunc
2450 g_type_add_class_cache_func (gpointer cache_data,
2451 GTypeClassCacheFunc cache_func)
2455 g_return_if_fail (cache_func != NULL);
2457 G_WRITE_LOCK (&type_rw_lock);
2458 i = static_n_class_cache_funcs++;
2459 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2460 static_class_cache_funcs[i].cache_data = cache_data;
2461 static_class_cache_funcs[i].cache_func = cache_func;
2462 G_WRITE_UNLOCK (&type_rw_lock);
2466 * g_type_remove_class_cache_func: (skip)
2467 * @cache_data: data that was given when adding @cache_func
2468 * @cache_func: a #GTypeClassCacheFunc
2470 * Removes a previously installed #GTypeClassCacheFunc. The cache
2471 * maintained by @cache_func has to be empty when calling
2472 * g_type_remove_class_cache_func() to avoid leaks.
2475 g_type_remove_class_cache_func (gpointer cache_data,
2476 GTypeClassCacheFunc cache_func)
2478 gboolean found_it = FALSE;
2481 g_return_if_fail (cache_func != NULL);
2483 G_WRITE_LOCK (&type_rw_lock);
2484 for (i = 0; i < static_n_class_cache_funcs; i++)
2485 if (static_class_cache_funcs[i].cache_data == cache_data &&
2486 static_class_cache_funcs[i].cache_func == cache_func)
2488 static_n_class_cache_funcs--;
2489 g_memmove (static_class_cache_funcs + i,
2490 static_class_cache_funcs + i + 1,
2491 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2492 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2496 G_WRITE_UNLOCK (&type_rw_lock);
2499 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2500 cache_func, cache_data);
2505 * g_type_add_interface_check: (skip)
2506 * @check_data: data to pass to @check_func
2507 * @check_func: function to be called after each interface
2510 * Adds a function to be called after an interface vtable is
2511 * initialized for any class (i.e. after the @interface_init member of
2512 * #GInterfaceInfo has been called).
2514 * This function is useful when you want to check an invariant that
2515 * depends on the interfaces of a class. For instance, the
2516 * implementation of #GObject uses this facility to check that an
2517 * object implements all of the properties that are defined on its
2523 g_type_add_interface_check (gpointer check_data,
2524 GTypeInterfaceCheckFunc check_func)
2528 g_return_if_fail (check_func != NULL);
2530 G_WRITE_LOCK (&type_rw_lock);
2531 i = static_n_iface_check_funcs++;
2532 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2533 static_iface_check_funcs[i].check_data = check_data;
2534 static_iface_check_funcs[i].check_func = check_func;
2535 G_WRITE_UNLOCK (&type_rw_lock);
2539 * g_type_remove_interface_check: (skip)
2540 * @check_data: callback data passed to g_type_add_interface_check()
2541 * @check_func: callback function passed to g_type_add_interface_check()
2543 * Removes an interface check function added with
2544 * g_type_add_interface_check().
2549 g_type_remove_interface_check (gpointer check_data,
2550 GTypeInterfaceCheckFunc check_func)
2552 gboolean found_it = FALSE;
2555 g_return_if_fail (check_func != NULL);
2557 G_WRITE_LOCK (&type_rw_lock);
2558 for (i = 0; i < static_n_iface_check_funcs; i++)
2559 if (static_iface_check_funcs[i].check_data == check_data &&
2560 static_iface_check_funcs[i].check_func == check_func)
2562 static_n_iface_check_funcs--;
2563 g_memmove (static_iface_check_funcs + i,
2564 static_iface_check_funcs + i + 1,
2565 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2566 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2570 G_WRITE_UNLOCK (&type_rw_lock);
2573 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2574 check_func, check_data);
2577 /* --- type registration --- */
2579 * g_type_register_fundamental:
2580 * @type_id: A predefined type identifier.
2581 * @type_name: 0-terminated string used as the name of the new type.
2582 * @info: The #GTypeInfo structure for this type.
2583 * @finfo: The #GTypeFundamentalInfo structure for this type.
2584 * @flags: Bitwise combination of #GTypeFlags values.
2586 * Registers @type_id as the predefined identifier and @type_name as the
2587 * name of a fundamental type. If @type_id is already registered, or a type
2588 * named @type_name is already registered, the behaviour is undefined. The type
2589 * system uses the information contained in the #GTypeInfo structure pointed to
2590 * by @info and the #GTypeFundamentalInfo structure pointed to by @finfo to
2591 * manage the type and its instances. The value of @flags determines additional
2592 * characteristics of the fundamental type.
2594 * Returns: The predefined type identifier.
2597 g_type_register_fundamental (GType type_id,
2598 const gchar *type_name,
2599 const GTypeInfo *info,
2600 const GTypeFundamentalInfo *finfo,
2605 g_return_val_if_type_system_uninitialized (0);
2606 g_return_val_if_fail (type_id > 0, 0);
2607 g_return_val_if_fail (type_name != NULL, 0);
2608 g_return_val_if_fail (info != NULL, 0);
2609 g_return_val_if_fail (finfo != NULL, 0);
2611 if (!check_type_name_I (type_name))
2613 if ((type_id & TYPE_ID_MASK) ||
2614 type_id > G_TYPE_FUNDAMENTAL_MAX)
2616 g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2621 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2622 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2624 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2628 if (lookup_type_node_I (type_id))
2630 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2631 type_descriptive_name_I (type_id),
2636 G_WRITE_LOCK (&type_rw_lock);
2637 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2638 type_add_flags_W (node, flags);
2640 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2641 type_data_make_W (node, info,
2642 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2643 G_WRITE_UNLOCK (&type_rw_lock);
2645 return NODE_TYPE (node);
2649 * g_type_register_static_simple: (skip)
2650 * @parent_type: Type from which this type will be derived.
2651 * @type_name: 0-terminated string used as the name of the new type.
2652 * @class_size: Size of the class structure (see #GTypeInfo)
2653 * @class_init: Location of the class initialization function (see #GTypeInfo)
2654 * @instance_size: Size of the instance structure (see #GTypeInfo)
2655 * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2656 * @flags: Bitwise combination of #GTypeFlags values.
2658 * Registers @type_name as the name of a new static type derived from
2659 * @parent_type. The value of @flags determines the nature (e.g.
2660 * abstract or not) of the type. It works by filling a #GTypeInfo
2661 * struct and calling g_type_register_static().
2665 * Returns: The new type identifier.
2668 g_type_register_static_simple (GType parent_type,
2669 const gchar *type_name,
2671 GClassInitFunc class_init,
2672 guint instance_size,
2673 GInstanceInitFunc instance_init,
2678 /* Instances are not allowed to be larger than this. If you have a big
2679 * fixed-length array or something, point to it instead.
2681 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
2682 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
2684 info.class_size = class_size;
2685 info.base_init = NULL;
2686 info.base_finalize = NULL;
2687 info.class_init = class_init;
2688 info.class_finalize = NULL;
2689 info.class_data = NULL;
2690 info.instance_size = instance_size;
2691 info.n_preallocs = 0;
2692 info.instance_init = instance_init;
2693 info.value_table = NULL;
2695 return g_type_register_static (parent_type, type_name, &info, flags);
2699 * g_type_register_static:
2700 * @parent_type: Type from which this type will be derived.
2701 * @type_name: 0-terminated string used as the name of the new type.
2702 * @info: The #GTypeInfo structure for this type.
2703 * @flags: Bitwise combination of #GTypeFlags values.
2705 * Registers @type_name as the name of a new static type derived from
2706 * @parent_type. The type system uses the information contained in the
2707 * #GTypeInfo structure pointed to by @info to manage the type and its
2708 * instances (if not abstract). The value of @flags determines the nature
2709 * (e.g. abstract or not) of the type.
2711 * Returns: The new type identifier.
2714 g_type_register_static (GType parent_type,
2715 const gchar *type_name,
2716 const GTypeInfo *info,
2719 TypeNode *pnode, *node;
2722 g_return_val_if_type_system_uninitialized (0);
2723 g_return_val_if_fail (parent_type > 0, 0);
2724 g_return_val_if_fail (type_name != NULL, 0);
2725 g_return_val_if_fail (info != NULL, 0);
2727 if (!check_type_name_I (type_name) ||
2728 !check_derivation_I (parent_type, type_name))
2730 if (info->class_finalize)
2732 g_warning ("class finalizer specified for static type `%s'",
2737 pnode = lookup_type_node_I (parent_type);
2738 G_WRITE_LOCK (&type_rw_lock);
2739 type_data_ref_Wm (pnode);
2740 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2742 node = type_node_new_W (pnode, type_name, NULL);
2743 type_add_flags_W (node, flags);
2744 type = NODE_TYPE (node);
2745 type_data_make_W (node, info,
2746 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2748 G_WRITE_UNLOCK (&type_rw_lock);
2754 * g_type_register_dynamic:
2755 * @parent_type: Type from which this type will be derived.
2756 * @type_name: 0-terminated string used as the name of the new type.
2757 * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2758 * @flags: Bitwise combination of #GTypeFlags values.
2760 * Registers @type_name as the name of a new dynamic type derived from
2761 * @parent_type. The type system uses the information contained in the
2762 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2763 * instances (if not abstract). The value of @flags determines the nature
2764 * (e.g. abstract or not) of the type.
2766 * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2769 g_type_register_dynamic (GType parent_type,
2770 const gchar *type_name,
2771 GTypePlugin *plugin,
2774 TypeNode *pnode, *node;
2777 g_return_val_if_type_system_uninitialized (0);
2778 g_return_val_if_fail (parent_type > 0, 0);
2779 g_return_val_if_fail (type_name != NULL, 0);
2780 g_return_val_if_fail (plugin != NULL, 0);
2782 if (!check_type_name_I (type_name) ||
2783 !check_derivation_I (parent_type, type_name) ||
2784 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2787 G_WRITE_LOCK (&type_rw_lock);
2788 pnode = lookup_type_node_I (parent_type);
2789 node = type_node_new_W (pnode, type_name, plugin);
2790 type_add_flags_W (node, flags);
2791 type = NODE_TYPE (node);
2792 G_WRITE_UNLOCK (&type_rw_lock);
2798 * g_type_add_interface_static:
2799 * @instance_type: #GType value of an instantiable type.
2800 * @interface_type: #GType value of an interface type.
2801 * @info: The #GInterfaceInfo structure for this
2802 * (@instance_type, @interface_type) combination.
2804 * Adds the static @interface_type to @instantiable_type. The
2805 * information contained in the #GInterfaceInfo structure pointed to by
2806 * @info is used to manage the relationship.
2809 g_type_add_interface_static (GType instance_type,
2810 GType interface_type,
2811 const GInterfaceInfo *info)
2813 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2814 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2815 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2817 /* we only need to lock class_init_rec_mutex if instance_type already has its
2818 * class initialized, however this function is rarely enough called to take
2819 * the simple route and always acquire class_init_rec_mutex.
2821 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2822 G_WRITE_LOCK (&type_rw_lock);
2823 if (check_add_interface_L (instance_type, interface_type))
2825 TypeNode *node = lookup_type_node_I (instance_type);
2826 TypeNode *iface = lookup_type_node_I (interface_type);
2827 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2828 type_add_interface_Wm (node, iface, info, NULL);
2830 G_WRITE_UNLOCK (&type_rw_lock);
2831 g_rec_mutex_unlock (&class_init_rec_mutex);
2835 * g_type_add_interface_dynamic:
2836 * @instance_type: the #GType value of an instantiable type.
2837 * @interface_type: the #GType value of an interface type.
2838 * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2840 * Adds the dynamic @interface_type to @instantiable_type. The information
2841 * contained in the #GTypePlugin structure pointed to by @plugin
2842 * is used to manage the relationship.
2845 g_type_add_interface_dynamic (GType instance_type,
2846 GType interface_type,
2847 GTypePlugin *plugin)
2850 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2851 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2852 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2854 node = lookup_type_node_I (instance_type);
2855 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2858 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2859 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2860 G_WRITE_LOCK (&type_rw_lock);
2861 if (check_add_interface_L (instance_type, interface_type))
2863 TypeNode *iface = lookup_type_node_I (interface_type);
2864 type_add_interface_Wm (node, iface, NULL, plugin);
2866 G_WRITE_UNLOCK (&type_rw_lock);
2867 g_rec_mutex_unlock (&class_init_rec_mutex);
2871 /* --- public API functions --- */
2874 * @type: Type ID of a classed type.
2876 * Increments the reference count of the class structure belonging to
2877 * @type. This function will demand-create the class if it doesn't
2880 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
2881 * structure for the given type ID.
2884 g_type_class_ref (GType type)
2891 /* optimize for common code path */
2892 node = lookup_type_node_I (type);
2893 if (!node || !node->is_classed)
2895 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2896 type_descriptive_name_I (type));
2900 if (G_LIKELY (type_data_ref_U (node)))
2902 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2903 return node->data->class.class;
2909 /* here, we either have node->data->class.class == NULL, or a recursive
2910 * call to g_type_class_ref() with a partly initialized class, or
2911 * node->data->class.init_state == INITIALIZED, because any
2912 * concurrently running initialization was guarded by class_init_rec_mutex.
2914 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2916 /* we need an initialized parent class for initializing derived classes */
2917 ptype = NODE_PARENT_TYPE (node);
2918 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2920 G_WRITE_LOCK (&type_rw_lock);
2923 type_data_ref_Wm (node);
2925 if (!node->data->class.class) /* class uninitialized */
2926 type_class_init_Wm (node, pclass);
2928 G_WRITE_UNLOCK (&type_rw_lock);
2931 g_type_class_unref (pclass);
2933 g_rec_mutex_unlock (&class_init_rec_mutex);
2935 return node->data->class.class;
2939 * g_type_class_unref:
2940 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
2943 * Decrements the reference count of the class structure being passed in.
2944 * Once the last reference count of a class has been released, classes
2945 * may be finalized by the type system, so further dereferencing of a
2946 * class pointer after g_type_class_unref() are invalid.
2949 g_type_class_unref (gpointer g_class)
2952 GTypeClass *class = g_class;
2954 g_return_if_fail (g_class != NULL);
2956 node = lookup_type_node_I (class->g_type);
2957 if (node && node->is_classed && NODE_REFCOUNT (node))
2958 type_data_unref_U (node, FALSE);
2960 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2961 type_descriptive_name_I (class->g_type));
2965 * g_type_class_unref_uncached: (skip)
2966 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
2969 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2970 * implementations. It unreferences a class without consulting the chain
2971 * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2975 g_type_class_unref_uncached (gpointer g_class)
2978 GTypeClass *class = g_class;
2980 g_return_if_fail (g_class != NULL);
2982 node = lookup_type_node_I (class->g_type);
2983 if (node && node->is_classed && NODE_REFCOUNT (node))
2984 type_data_unref_U (node, TRUE);
2986 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2987 type_descriptive_name_I (class->g_type));
2991 * g_type_class_peek:
2992 * @type: Type ID of a classed type.
2994 * This function is essentially the same as g_type_class_ref(), except that
2995 * the classes reference count isn't incremented. As a consequence, this function
2996 * may return %NULL if the class of the type passed in does not currently
2997 * exist (hasn't been referenced before).
2999 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
3000 * structure for the given type ID or %NULL if the class does not
3004 g_type_class_peek (GType type)
3009 node = lookup_type_node_I (type);
3010 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3011 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3012 /* ref_count _may_ be 0 */
3013 class = node->data->class.class;
3021 * g_type_class_peek_static:
3022 * @type: Type ID of a classed type.
3024 * A more efficient version of g_type_class_peek() which works only for
3028 * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
3029 * structure for the given type ID or %NULL if the class does not
3030 * currently exist or is dynamically loaded.
3033 g_type_class_peek_static (GType type)
3038 node = lookup_type_node_I (type);
3039 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3040 /* peek only static types: */ node->plugin == NULL &&
3041 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3042 /* ref_count _may_ be 0 */
3043 class = node->data->class.class;
3051 * g_type_class_peek_parent:
3052 * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
3053 * retrieve the parent class for.
3055 * This is a convenience function often needed in class initializers.
3056 * It returns the class structure of the immediate parent type of the
3057 * class passed in. Since derived classes hold a reference count on
3058 * their parent classes as long as they are instantiated, the returned
3059 * class will always exist. This function is essentially equivalent
3063 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
3066 * Returns: (type GObject.TypeClass) (transfer none): The parent class
3070 g_type_class_peek_parent (gpointer g_class)
3073 gpointer class = NULL;
3075 g_return_val_if_fail (g_class != NULL, NULL);
3077 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3078 /* We used to acquire a read lock here. That is not necessary, since
3079 * parent->data->class.class is constant as long as the derived class
3082 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3084 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3085 class = node->data->class.class;
3087 else if (NODE_PARENT_TYPE (node))
3088 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
3094 * g_type_interface_peek:
3095 * @instance_class: (type GObject.TypeClass): A #GTypeClass structure.
3096 * @iface_type: An interface ID which this class conforms to.
3098 * Returns the #GTypeInterface structure of an interface to which the
3099 * passed in class conforms.
3101 * Returns: (type GObject.TypeInterface) (transfer none): The GTypeInterface
3102 * structure of iface_type if implemented by @instance_class, %NULL
3106 g_type_interface_peek (gpointer instance_class,
3111 gpointer vtable = NULL;
3112 GTypeClass *class = instance_class;
3114 g_return_val_if_fail (instance_class != NULL, NULL);
3116 node = lookup_type_node_I (class->g_type);
3117 iface = lookup_type_node_I (iface_type);
3118 if (node && node->is_instantiatable && iface)
3119 type_lookup_iface_vtable_I (node, iface, &vtable);
3121 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
3127 * g_type_interface_peek_parent:
3128 * @g_iface: (type GObject.TypeInterface): A #GTypeInterface structure.
3130 * Returns the corresponding #GTypeInterface structure of the parent type
3131 * of the instance type to which @g_iface belongs. This is useful when
3132 * deriving the implementation of an interface from the parent type and
3133 * then possibly overriding some methods.
3135 * Returns: (transfer none) (type GObject.TypeInterface): The
3136 * corresponding #GTypeInterface structure of the parent type of the
3137 * instance type to which @g_iface belongs, or %NULL if the parent
3138 * type doesn't conform to the interface.
3141 g_type_interface_peek_parent (gpointer g_iface)
3145 gpointer vtable = NULL;
3146 GTypeInterface *iface_class = g_iface;
3148 g_return_val_if_fail (g_iface != NULL, NULL);
3150 iface = lookup_type_node_I (iface_class->g_type);
3151 node = lookup_type_node_I (iface_class->g_instance_type);
3153 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3154 if (node && node->is_instantiatable && iface)
3155 type_lookup_iface_vtable_I (node, iface, &vtable);
3157 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
3163 * g_type_default_interface_ref:
3164 * @g_type: an interface type
3166 * Increments the reference count for the interface type @g_type,
3167 * and returns the default interface vtable for the type.
3169 * If the type is not currently in use, then the default vtable
3170 * for the type will be created and initalized by calling
3171 * the base interface init and default vtable init functions for
3172 * the type (the @<structfield>base_init</structfield>
3173 * and <structfield>class_init</structfield> members of #GTypeInfo).
3174 * Calling g_type_default_interface_ref() is useful when you
3175 * want to make sure that signals and properties for an interface
3176 * have been installed.
3180 * Returns: (type GObject.TypeInterface) (transfer none): the default
3181 * vtable for the interface; call g_type_default_interface_unref()
3182 * when you are done using the interface.
3185 g_type_default_interface_ref (GType g_type)
3188 gpointer dflt_vtable;
3190 G_WRITE_LOCK (&type_rw_lock);
3192 node = lookup_type_node_I (g_type);
3193 if (!node || !NODE_IS_IFACE (node) ||
3194 (node->data && NODE_REFCOUNT (node) == 0))
3196 G_WRITE_UNLOCK (&type_rw_lock);
3197 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3198 type_descriptive_name_I (g_type));
3202 if (!node->data || !node->data->iface.dflt_vtable)
3204 G_WRITE_UNLOCK (&type_rw_lock);
3205 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3206 G_WRITE_LOCK (&type_rw_lock);
3207 node = lookup_type_node_I (g_type);
3208 type_data_ref_Wm (node);
3209 type_iface_ensure_dflt_vtable_Wm (node);
3210 g_rec_mutex_unlock (&class_init_rec_mutex);
3213 type_data_ref_Wm (node); /* ref_count >= 1 already */
3215 dflt_vtable = node->data->iface.dflt_vtable;
3216 G_WRITE_UNLOCK (&type_rw_lock);
3222 * g_type_default_interface_peek:
3223 * @g_type: an interface type
3225 * If the interface type @g_type is currently in use, returns its
3226 * default interface vtable.
3230 * Returns: (type GObject.TypeInterface) (transfer none): the default
3231 * vtable for the interface, or %NULL if the type is not currently in
3235 g_type_default_interface_peek (GType g_type)
3240 node = lookup_type_node_I (g_type);
3241 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3242 vtable = node->data->iface.dflt_vtable;
3250 * g_type_default_interface_unref:
3251 * @g_iface: (type GObject.TypeInterface): the default vtable
3252 * structure for a interface, as returned by
3253 * g_type_default_interface_ref()
3255 * Decrements the reference count for the type corresponding to the
3256 * interface default vtable @g_iface. If the type is dynamic, then
3257 * when no one is using the interface and all references have
3258 * been released, the finalize function for the interface's default
3259 * vtable (the <structfield>class_finalize</structfield> member of
3260 * #GTypeInfo) will be called.
3265 g_type_default_interface_unref (gpointer g_iface)
3268 GTypeInterface *vtable = g_iface;
3270 g_return_if_fail (g_iface != NULL);
3272 node = lookup_type_node_I (vtable->g_type);
3273 if (node && NODE_IS_IFACE (node))
3274 type_data_unref_U (node, FALSE);
3276 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3277 type_descriptive_name_I (vtable->g_type));
3282 * @type: Type to return name for.
3284 * Get the unique name that is assigned to a type ID. Note that this
3285 * function (like all other GType API) cannot cope with invalid type
3286 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3287 * other validly registered type ID, but randomized type IDs should
3288 * not be passed in and will most likely lead to a crash.
3290 * Returns: Static type name or %NULL.
3293 g_type_name (GType type)
3297 g_return_val_if_type_system_uninitialized (NULL);
3299 node = lookup_type_node_I (type);
3301 return node ? NODE_NAME (node) : NULL;
3306 * @type: Type to return quark of type name for.
3308 * Get the corresponding quark of the type IDs name.
3310 * Returns: The type names quark or 0.
3313 g_type_qname (GType type)
3317 node = lookup_type_node_I (type);
3319 return node ? node->qname : 0;
3324 * @name: Type name to lookup.
3326 * Lookup the type ID from a given type name, returning 0 if no type
3327 * has been registered under this name (this is the preferred method
3328 * to find out by name whether a specific type has been registered
3331 * Returns: Corresponding type ID or 0.
3334 g_type_from_name (const gchar *name)
3338 g_return_val_if_fail (name != NULL, 0);
3340 G_READ_LOCK (&type_rw_lock);
3341 type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
3342 G_READ_UNLOCK (&type_rw_lock);
3349 * @type: The derived type.
3351 * Return the direct parent type of the passed in type. If the passed
3352 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3354 * Returns: The parent type.
3357 g_type_parent (GType type)
3361 node = lookup_type_node_I (type);
3363 return node ? NODE_PARENT_TYPE (node) : 0;
3368 * @type: A #GType value.
3370 * Returns the length of the ancestry of the passed in type. This
3371 * includes the type itself, so that e.g. a fundamental type has depth 1.
3373 * Returns: The depth of @type.
3376 g_type_depth (GType type)
3380 node = lookup_type_node_I (type);
3382 return node ? node->n_supers + 1 : 0;
3387 * @leaf_type: Descendant of @root_type and the type to be returned.
3388 * @root_type: Immediate parent of the returned type.
3390 * Given a @leaf_type and a @root_type which is contained in its
3391 * anchestry, return the type that @root_type is the immediate parent
3392 * of. In other words, this function determines the type that is
3393 * derived directly from @root_type which is also a base class of
3394 * @leaf_type. Given a root type and a leaf type, this function can
3395 * be used to determine the types and order in which the leaf type is
3396 * descended from the root type.
3398 * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3401 g_type_next_base (GType type,
3407 node = lookup_type_node_I (type);
3410 TypeNode *base_node = lookup_type_node_I (base_type);
3412 if (base_node && base_node->n_supers < node->n_supers)
3414 guint n = node->n_supers - base_node->n_supers;
3416 if (node->supers[n] == base_type)
3417 atype = node->supers[n - 1];
3424 static inline gboolean
3425 type_node_check_conformities_UorL (TypeNode *node,
3426 TypeNode *iface_node,
3427 /* support_inheritance */
3428 gboolean support_interfaces,
3429 gboolean support_prerequisites,
3434 if (/* support_inheritance && */
3435 NODE_IS_ANCESTOR (iface_node, node))
3438 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3439 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3441 if (support_interfaces)
3445 if (type_lookup_iface_entry_L (node, iface_node))
3450 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3455 support_prerequisites)
3458 G_READ_LOCK (&type_rw_lock);
3459 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3462 G_READ_UNLOCK (&type_rw_lock);
3468 type_node_is_a_L (TypeNode *node,
3469 TypeNode *iface_node)
3471 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3474 static inline gboolean
3475 type_node_conforms_to_U (TypeNode *node,
3476 TypeNode *iface_node,
3477 gboolean support_interfaces,
3478 gboolean support_prerequisites)
3480 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3485 * @type: Type to check anchestry for.
3486 * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3488 * If @is_a_type is a derivable type, check whether @type is a
3489 * descendant of @is_a_type. If @is_a_type is an interface, check
3490 * whether @type conforms to it.
3492 * Returns: %TRUE if @type is_a @is_a_type holds true.
3495 g_type_is_a (GType type,
3498 TypeNode *node, *iface_node;
3501 node = lookup_type_node_I (type);
3502 iface_node = lookup_type_node_I (iface_type);
3503 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3510 * @type: The parent type.
3511 * @n_children: (out) (allow-none): Optional #guint pointer to contain
3512 * the number of child types.
3514 * Return a newly allocated and 0-terminated array of type IDs, listing the
3515 * child types of @type. The return value has to be g_free()ed after use.
3517 * Returns: (array length=n_children) (transfer full): Newly allocated
3518 * and 0-terminated array of child types.
3521 g_type_children (GType type,
3526 node = lookup_type_node_I (type);
3531 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3532 children = g_new (GType, node->n_children + 1);
3533 memcpy (children, node->children, sizeof (GType) * node->n_children);
3534 children[node->n_children] = 0;
3537 *n_children = node->n_children;
3538 G_READ_UNLOCK (&type_rw_lock);
3552 * g_type_interfaces:
3553 * @type: The type to list interface types for.
3554 * @n_interfaces: (out) (allow-none): Optional #guint pointer to
3555 * contain the number of interface types.
3557 * Return a newly allocated and 0-terminated array of type IDs, listing the
3558 * interface types that @type conforms to. The return value has to be
3559 * g_free()ed after use.
3561 * Returns: (array length=n_interfaces) (transfer full): Newly
3562 * allocated and 0-terminated array of interface types.
3565 g_type_interfaces (GType type,
3566 guint *n_interfaces)
3570 node = lookup_type_node_I (type);
3571 if (node && node->is_instantiatable)
3573 IFaceEntries *entries;
3577 G_READ_LOCK (&type_rw_lock);
3578 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3581 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3582 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3583 ifaces[i] = entries->entry[i].iface_type;
3587 ifaces = g_new (GType, 1);
3594 G_READ_UNLOCK (&type_rw_lock);
3607 typedef struct _QData QData;
3619 static inline gpointer
3620 type_get_qdata_L (TypeNode *node,
3623 GData *gdata = node->global_gdata;
3625 if (quark && gdata && gdata->n_qdatas)
3627 QData *qdatas = gdata->qdatas - 1;
3628 guint n_qdatas = gdata->n_qdatas;
3635 i = (n_qdatas + 1) / 2;
3637 if (quark == check->quark)
3639 else if (quark > check->quark)
3644 else /* if (quark < check->quark) */
3655 * @quark: a #GQuark id to identify the data
3657 * Obtains data which has previously been attached to @type
3658 * with g_type_set_qdata().
3660 * Note that this does not take subtyping into account; data
3661 * attached to one type with g_type_set_qdata() cannot
3662 * be retrieved from a subtype using g_type_get_qdata().
3664 * Returns: (transfer none): the data, or %NULL if no data was found
3667 g_type_get_qdata (GType type,
3673 node = lookup_type_node_I (type);
3676 G_READ_LOCK (&type_rw_lock);
3677 data = type_get_qdata_L (node, quark);
3678 G_READ_UNLOCK (&type_rw_lock);
3682 g_return_val_if_fail (node != NULL, NULL);
3689 type_set_qdata_W (TypeNode *node,
3697 /* setup qdata list if necessary */
3698 if (!node->global_gdata)
3699 node->global_gdata = g_new0 (GData, 1);
3700 gdata = node->global_gdata;
3702 /* try resetting old data */
3703 qdata = gdata->qdatas;
3704 for (i = 0; i < gdata->n_qdatas; i++)
3705 if (qdata[i].quark == quark)
3707 qdata[i].data = data;
3713 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3714 qdata = gdata->qdatas;
3715 for (i = 0; i < gdata->n_qdatas - 1; i++)
3716 if (qdata[i].quark > quark)
3718 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3719 qdata[i].quark = quark;
3720 qdata[i].data = data;
3726 * @quark: a #GQuark id to identify the data
3729 * Attaches arbitrary data to a type.
3732 g_type_set_qdata (GType type,
3738 g_return_if_fail (quark != 0);
3740 node = lookup_type_node_I (type);
3743 G_WRITE_LOCK (&type_rw_lock);
3744 type_set_qdata_W (node, quark, data);
3745 G_WRITE_UNLOCK (&type_rw_lock);
3748 g_return_if_fail (node != NULL);
3752 type_add_flags_W (TypeNode *node,
3757 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3758 g_return_if_fail (node != NULL);
3760 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3761 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3762 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3764 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3769 * @type: the #GType value of a static, classed type.
3770 * @query: (out caller-allocates): A user provided structure that is
3771 * filled in with constant values upon success.
3773 * Queries the type system for information about a specific type.
3774 * This function will fill in a user-provided structure to hold
3775 * type-specific information. If an invalid #GType is passed in, the
3776 * @type member of the #GTypeQuery is 0. All members filled into the
3777 * #GTypeQuery structure should be considered constant and have to be
3781 g_type_query (GType type,
3786 g_return_if_fail (query != NULL);
3788 /* if node is not static and classed, we won't allow query */
3790 node = lookup_type_node_I (type);
3791 if (node && node->is_classed && !node->plugin)
3793 /* type is classed and probably even instantiatable */
3794 G_READ_LOCK (&type_rw_lock);
3795 if (node->data) /* type is static or referenced */
3797 query->type = NODE_TYPE (node);
3798 query->type_name = NODE_NAME (node);
3799 query->class_size = node->data->class.class_size;
3800 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3802 G_READ_UNLOCK (&type_rw_lock);
3807 /* --- implementation details --- */
3809 g_type_test_flags (GType type,
3813 gboolean result = FALSE;
3815 node = lookup_type_node_I (type);
3818 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3819 guint tflags = flags & TYPE_FLAG_MASK;
3823 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3825 fflags = (finfo->type_flags & fflags) == fflags;
3832 G_READ_LOCK (&type_rw_lock);
3833 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3834 G_READ_UNLOCK (&type_rw_lock);
3839 result = tflags && fflags;
3846 * g_type_get_plugin:
3847 * @type: The #GType to retrieve the plugin for.
3849 * Returns the #GTypePlugin structure for @type or
3850 * %NULL if @type does not have a #GTypePlugin structure.
3852 * Returns: (transfer none): The corresponding plugin if @type is a
3853 * dynamic type, %NULL otherwise.
3856 g_type_get_plugin (GType type)
3860 node = lookup_type_node_I (type);
3862 return node ? node->plugin : NULL;
3866 * g_type_interface_get_plugin:
3867 * @instance_type: the #GType value of an instantiatable type.
3868 * @interface_type: the #GType value of an interface type.
3870 * Returns the #GTypePlugin structure for the dynamic interface
3871 * @interface_type which has been added to @instance_type, or %NULL if
3872 * @interface_type has not been added to @instance_type or does not
3873 * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3875 * Returns: (transfer none): the #GTypePlugin for the dynamic
3876 * interface @interface_type of @instance_type.
3879 g_type_interface_get_plugin (GType instance_type,
3880 GType interface_type)
3885 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3887 node = lookup_type_node_I (instance_type);
3888 iface = lookup_type_node_I (interface_type);
3891 IFaceHolder *iholder;
3892 GTypePlugin *plugin;
3894 G_READ_LOCK (&type_rw_lock);
3896 iholder = iface_node_get_holders_L (iface);
3897 while (iholder && iholder->instance_type != instance_type)
3898 iholder = iholder->next;
3899 plugin = iholder ? iholder->plugin : NULL;
3901 G_READ_UNLOCK (&type_rw_lock);
3906 g_return_val_if_fail (node == NULL, NULL);
3907 g_return_val_if_fail (iface == NULL, NULL);
3909 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3915 * g_type_fundamental_next:
3917 * Returns the next free fundamental type id which can be used to
3918 * register a new fundamental type with g_type_register_fundamental().
3919 * The returned type ID represents the highest currently registered
3920 * fundamental type identifier.
3922 * Returns: The nextmost fundamental type ID to be registered,
3923 * or 0 if the type system ran out of fundamental type IDs.
3926 g_type_fundamental_next (void)
3930 G_READ_LOCK (&type_rw_lock);
3931 type = static_fundamental_next;
3932 G_READ_UNLOCK (&type_rw_lock);
3933 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3934 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3938 * g_type_fundamental:
3939 * @type_id: valid type ID
3941 * Internal function, used to extract the fundamental type ID portion.
3942 * use G_TYPE_FUNDAMENTAL() instead.
3944 * Returns: fundamental type ID
3947 g_type_fundamental (GType type_id)
3949 TypeNode *node = lookup_type_node_I (type_id);
3951 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3955 g_type_check_instance_is_a (GTypeInstance *type_instance,
3958 TypeNode *node, *iface;
3961 if (!type_instance || !type_instance->g_class)
3964 node = lookup_type_node_I (type_instance->g_class->g_type);
3965 iface = lookup_type_node_I (iface_type);
3966 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3972 g_type_check_class_is_a (GTypeClass *type_class,
3975 TypeNode *node, *iface;
3981 node = lookup_type_node_I (type_class->g_type);
3982 iface = lookup_type_node_I (is_a_type);
3983 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3989 g_type_check_instance_cast (GTypeInstance *type_instance,
3994 if (type_instance->g_class)
3996 TypeNode *node, *iface;
3997 gboolean is_instantiatable, check;
3999 node = lookup_type_node_I (type_instance->g_class->g_type);
4000 is_instantiatable = node && node->is_instantiatable;
4001 iface = lookup_type_node_I (iface_type);
4002 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4004 return type_instance;
4006 if (is_instantiatable)
4007 g_warning ("invalid cast from `%s' to `%s'",
4008 type_descriptive_name_I (type_instance->g_class->g_type),
4009 type_descriptive_name_I (iface_type));
4011 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
4012 type_descriptive_name_I (type_instance->g_class->g_type),
4013 type_descriptive_name_I (iface_type));
4016 g_warning ("invalid unclassed pointer in cast to `%s'",
4017 type_descriptive_name_I (iface_type));
4020 return type_instance;
4024 g_type_check_class_cast (GTypeClass *type_class,
4029 TypeNode *node, *iface;
4030 gboolean is_classed, check;
4032 node = lookup_type_node_I (type_class->g_type);
4033 is_classed = node && node->is_classed;
4034 iface = lookup_type_node_I (is_a_type);
4035 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4040 g_warning ("invalid class cast from `%s' to `%s'",
4041 type_descriptive_name_I (type_class->g_type),
4042 type_descriptive_name_I (is_a_type));
4044 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
4045 type_descriptive_name_I (type_class->g_type),
4046 type_descriptive_name_I (is_a_type));
4049 g_warning ("invalid class cast from (NULL) pointer to `%s'",
4050 type_descriptive_name_I (is_a_type));
4055 * g_type_check_instance:
4056 * @instance: A valid #GTypeInstance structure.
4058 * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
4061 * Returns: %TRUE if @instance is valid, %FALSE otherwise.
4064 g_type_check_instance (GTypeInstance *type_instance)
4066 /* this function is just here to make the signal system
4067 * conveniently elaborated on instance checks
4071 if (type_instance->g_class)
4073 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4075 if (node && node->is_instantiatable)
4078 g_warning ("instance of invalid non-instantiatable type `%s'",
4079 type_descriptive_name_I (type_instance->g_class->g_type));
4082 g_warning ("instance with invalid (NULL) class pointer");
4085 g_warning ("invalid (NULL) pointer instance");
4090 static inline gboolean
4091 type_check_is_value_type_U (GType type)
4093 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4096 /* common path speed up */
4097 node = lookup_type_node_I (type);
4098 if (node && node->mutatable_check_cache)
4101 G_READ_LOCK (&type_rw_lock);
4105 if (node->data && NODE_REFCOUNT (node) > 0 &&
4106 node->data->common.value_table->value_init)
4107 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4108 else if (NODE_IS_IFACE (node))
4112 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4114 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4115 TypeNode *prnode = lookup_type_node_I (prtype);
4117 if (prnode->is_instantiatable)
4120 node = lookup_type_node_I (type);
4126 G_READ_UNLOCK (&type_rw_lock);
4128 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4132 g_type_check_is_value_type (GType type)
4134 return type_check_is_value_type_U (type);
4138 g_type_check_value (GValue *value)
4140 return value && type_check_is_value_type_U (value->g_type);
4144 g_type_check_value_holds (GValue *value,
4147 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4151 * g_type_value_table_peek: (skip)
4152 * @type: A #GType value.
4154 * Returns the location of the #GTypeValueTable associated with @type.
4155 * <emphasis>Note that this function should only be used from source code
4156 * that implements or has internal knowledge of the implementation of
4159 * Returns: Location of the #GTypeValueTable associated with @type or
4160 * %NULL if there is no #GTypeValueTable associated with @type.
4163 g_type_value_table_peek (GType type)
4165 GTypeValueTable *vtable = NULL;
4166 TypeNode *node = lookup_type_node_I (type);
4167 gboolean has_refed_data, has_table;
4169 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4170 return node->data->common.value_table;
4172 G_READ_LOCK (&type_rw_lock);
4175 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4176 has_table = has_refed_data && node->data->common.value_table->value_init;
4180 vtable = node->data->common.value_table;
4181 else if (NODE_IS_IFACE (node))
4185 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4187 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4188 TypeNode *prnode = lookup_type_node_I (prtype);
4190 if (prnode->is_instantiatable)
4193 node = lookup_type_node_I (type);
4194 goto restart_table_peek;
4200 G_READ_UNLOCK (&type_rw_lock);
4206 g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
4207 if (!has_refed_data)
4208 g_warning ("can't peek value table for type `%s' which is not currently referenced",
4209 type_descriptive_name_I (type));
4215 g_type_name_from_instance (GTypeInstance *instance)
4218 return "<NULL-instance>";
4220 return g_type_name_from_class (instance->g_class);
4224 g_type_name_from_class (GTypeClass *g_class)
4227 return "<NULL-class>";
4229 return g_type_name (g_class->g_type);
4233 /* --- private api for gboxed.c --- */
4235 _g_type_boxed_copy (GType type, gpointer value)
4237 TypeNode *node = lookup_type_node_I (type);
4239 return node->data->boxed.copy_func (value);
4243 _g_type_boxed_free (GType type, gpointer value)
4245 TypeNode *node = lookup_type_node_I (type);
4247 node->data->boxed.free_func (value);
4251 _g_type_boxed_init (GType type,
4252 GBoxedCopyFunc copy_func,
4253 GBoxedFreeFunc free_func)
4255 TypeNode *node = lookup_type_node_I (type);
4257 node->data->boxed.copy_func = copy_func;
4258 node->data->boxed.free_func = free_func;
4261 /* --- initialization --- */
4263 * g_type_init_with_debug_flags:
4264 * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
4265 * debugging purposes.
4267 * Similar to g_type_init(), but additionally sets debug flags.
4269 * This function is idempotent.
4272 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4274 G_LOCK_DEFINE_STATIC (type_init_lock);
4275 const gchar *env_string;
4280 G_LOCK (type_init_lock);
4282 G_WRITE_LOCK (&type_rw_lock);
4284 if (static_quark_type_flags)
4286 G_WRITE_UNLOCK (&type_rw_lock);
4287 G_UNLOCK (type_init_lock);
4291 /* setup GObject library wide debugging flags */
4292 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
4293 env_string = g_getenv ("GOBJECT_DEBUG");
4294 if (env_string != NULL)
4296 GDebugKey debug_keys[] = {
4297 { "objects", G_TYPE_DEBUG_OBJECTS },
4298 { "signals", G_TYPE_DEBUG_SIGNALS },
4301 _g_type_debug_flags |= g_parse_debug_string (env_string, debug_keys, G_N_ELEMENTS (debug_keys));
4305 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4306 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4307 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4309 /* type qname hash table */
4310 static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
4312 /* invalid type G_TYPE_INVALID (0)
4314 static_fundamental_type_nodes[0] = NULL;
4316 /* void type G_TYPE_NONE
4318 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4319 type = NODE_TYPE (node);
4320 g_assert (type == G_TYPE_NONE);
4322 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4324 memset (&info, 0, sizeof (info));
4325 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4326 type = NODE_TYPE (node);
4327 type_data_make_W (node, &info, NULL);
4328 g_assert (type == G_TYPE_INTERFACE);
4330 G_WRITE_UNLOCK (&type_rw_lock);
4334 /* G_TYPE_TYPE_PLUGIN
4336 g_type_ensure (g_type_plugin_get_type ());
4338 /* G_TYPE_* value types
4340 _g_value_types_init ();
4342 /* G_TYPE_ENUM & G_TYPE_FLAGS
4344 _g_enum_types_init ();
4348 _g_boxed_type_init ();
4352 _g_param_type_init ();
4356 _g_object_type_init ();
4358 /* G_TYPE_PARAM_* pspec types
4360 _g_param_spec_types_init ();
4362 /* Value Transformations
4364 _g_value_transforms_init ();
4370 G_UNLOCK (type_init_lock);
4376 * Prior to any use of the type system, g_type_init() has to be called
4377 * to initialize the type system and assorted other code portions
4378 * (such as the various fundamental type implementations or the signal
4381 * This function is idempotent.
4383 * Since version 2.24 this also initializes the thread system
4388 g_type_init_with_debug_flags (0);
4392 * g_type_class_add_private:
4393 * @g_class: class structure for an instantiatable type
4394 * @private_size: size of private structure.
4396 * Registers a private structure for an instantiatable type.
4398 * When an object is allocated, the private structures for
4399 * the type and all of its parent types are allocated
4400 * sequentially in the same memory block as the public
4403 * Note that the accumulated size of the private structures of
4404 * a type and all its parent types cannot excced 64 KiB.
4406 * This function should be called in the type's class_init() function.
4407 * The private structure can be retrieved using the
4408 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4410 * The following example shows attaching a private structure
4411 * <structname>MyObjectPrivate</structname> to an object
4412 * <structname>MyObject</structname> defined in the standard GObject
4414 * type's class_init() function.
4415 * Note the use of a structure member "priv" to avoid the overhead
4416 * of repeatedly calling MY_OBJECT_GET_PRIVATE().
4419 * typedef struct _MyObject MyObject;
4420 * typedef struct _MyObjectPrivate MyObjectPrivate;
4422 * struct _MyObject {
4425 * MyObjectPrivate *priv;
4428 * struct _MyObjectPrivate {
4433 * my_object_class_init (MyObjectClass *klass)
4435 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4439 * my_object_init (MyObject *my_object)
4441 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4447 * my_object_get_some_field (MyObject *my_object)
4449 * MyObjectPrivate *priv;
4451 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
4453 * priv = my_object->priv;
4455 * return priv->some_field;
4462 g_type_class_add_private (gpointer g_class,
4465 GType instance_type = ((GTypeClass *)g_class)->g_type;
4466 TypeNode *node = lookup_type_node_I (instance_type);
4469 g_return_if_fail (private_size > 0);
4470 g_return_if_fail (private_size <= 0xffff);
4472 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4474 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4475 type_descriptive_name_I (instance_type));
4479 if (NODE_PARENT_TYPE (node))
4481 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4482 if (node->data->instance.private_size != pnode->data->instance.private_size)
4484 g_warning ("g_type_add_private() called multiple times for the same type");
4489 G_WRITE_LOCK (&type_rw_lock);
4491 offset = ALIGN_STRUCT (node->data->instance.private_size);
4493 g_assert (offset + private_size <= 0xffff);
4495 node->data->instance.private_size = offset + private_size;
4497 G_WRITE_UNLOCK (&type_rw_lock);
4501 g_type_instance_get_private (GTypeInstance *instance,
4504 TypeNode *instance_node;
4505 TypeNode *private_node;
4506 TypeNode *parent_node;
4510 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4512 /* while instances are initialized, their class pointers change,
4513 * so figure the instances real class first
4515 class = instance_real_class_get (instance);
4517 class = instance->g_class;
4519 instance_node = lookup_type_node_I (class->g_type);
4520 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4522 g_warning ("instance of invalid non-instantiatable type `%s'",
4523 type_descriptive_name_I (instance->g_class->g_type));
4527 private_node = lookup_type_node_I (private_type);
4528 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4530 g_warning ("attempt to retrieve private data for invalid type '%s'",
4531 type_descriptive_name_I (private_type));
4535 /* Note that we don't need a read lock, since instance existing
4536 * means that the instance class and all parent classes
4537 * exist, so the node->data, node->data->instance.instance_size,
4538 * and node->data->instance.private_size are not going to be changed.
4539 * for any of the relevant types.
4542 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4544 if (NODE_PARENT_TYPE (private_node))
4546 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4547 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4549 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4551 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4555 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4558 return G_STRUCT_MEMBER_P (instance, offset);
4562 * g_type_add_class_private:
4563 * @class_type: GType of an classed type.
4564 * @private_size: size of private structure.
4566 * Registers a private class structure for a classed type;
4567 * when the class is allocated, the private structures for
4568 * the class and all of its parent types are allocated
4569 * sequentially in the same memory block as the public
4570 * structures. This function should be called in the
4571 * type's get_type() function after the type is registered.
4572 * The private structure can be retrieved using the
4573 * G_TYPE_CLASS_GET_PRIVATE() macro.
4578 g_type_add_class_private (GType class_type,
4581 TypeNode *node = lookup_type_node_I (class_type);
4584 g_return_if_fail (private_size > 0);
4586 if (!node || !node->is_classed || !node->data)
4588 g_warning ("cannot add class private field to invalid type '%s'",
4589 type_descriptive_name_I (class_type));
4593 if (NODE_PARENT_TYPE (node))
4595 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4596 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4598 g_warning ("g_type_add_class_private() called multiple times for the same type");
4603 G_WRITE_LOCK (&type_rw_lock);
4605 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4606 node->data->class.class_private_size = offset + private_size;
4608 G_WRITE_UNLOCK (&type_rw_lock);
4612 g_type_class_get_private (GTypeClass *klass,
4615 TypeNode *class_node;
4616 TypeNode *private_node;
4617 TypeNode *parent_node;
4620 g_return_val_if_fail (klass != NULL, NULL);
4622 class_node = lookup_type_node_I (klass->g_type);
4623 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4625 g_warning ("class of invalid type `%s'",
4626 type_descriptive_name_I (klass->g_type));
4630 private_node = lookup_type_node_I (private_type);
4631 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4633 g_warning ("attempt to retrieve private data for invalid type '%s'",
4634 type_descriptive_name_I (private_type));
4638 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4640 if (NODE_PARENT_TYPE (private_node))
4642 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4643 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4645 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4647 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_class_add_class_private()");
4651 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4654 return G_STRUCT_MEMBER_P (klass, offset);
4661 * Ensures that the indicated @type has been registered with the
4662 * type system, and its _class_init() method has been run.
4664 * In theory, simply calling the type's _get_type() method (or using
4665 * the corresponding macro) is supposed take care of this. However,
4666 * _get_type() methods are often marked %G_GNUC_CONST for performance
4667 * reasons, even though this is technically incorrect (since
4668 * %G_GNUC_CONST requires that the function not have side effects,
4669 * which _get_type() methods do on the first call). As a result, if
4670 * you write a bare call to a _get_type() macro, it may get optimized
4671 * out by the compiler. Using g_type_ensure() guarantees that the
4672 * type's _get_type() method is called.
4677 g_type_ensure (GType type)
4679 /* In theory, @type has already been resolved and so there's nothing
4680 * to do here. But this protects us in the case where the function
4681 * gets inlined (as it might in g_type_init_with_debug_flags()
4684 if (G_UNLIKELY (type == (GType)-1))
4685 g_error ("can't happen");