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 "gobjectalias.h"
34 #include "gatomicarray.h"
39 * @short_description: The GLib Runtime type identification and
41 * @title:Type Information
43 * The GType API is the foundation of the GObject system. It provides the
44 * facilities for registering and managing all fundamental data types,
45 * user-defined object and interface types. Before using any GType
46 * or GObject functions, g_type_init() must be called to initialize the
49 * For type creation and registration purposes, all types fall into one of
50 * two categories: static or dynamic. Static types are never loaded or
51 * unloaded at run-time as dynamic types may be. Static types are created
52 * with g_type_register_static() that gets type specific information passed
53 * in via a #GTypeInfo structure.
54 * Dynamic types are created with g_type_register_dynamic() which takes a
55 * #GTypePlugin structure instead. The remaining type information (the
56 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
57 * and the g_type_plugin_*() API.
58 * These registration functions are usually called only once from a
59 * function whose only purpose is to return the type identifier for a
60 * specific class. Once the type (or class or interface) is registered,
61 * it may be instantiated, inherited, or implemented depending on exactly
62 * what sort of type it is.
63 * There is also a third registration function for registering fundamental
64 * types called g_type_register_fundamental() which requires both a #GTypeInfo
65 * structure and a #GTypeFundamentalInfo structure but it is seldom used
66 * since most fundamental types are predefined rather than user-defined.
68 * A final word about type names.
69 * Such an identifier needs to be at least three characters long. There is no
70 * upper length limit. The first character needs to be a letter (a-z or A-Z)
71 * or an underscore '_'. Subsequent characters can be letters, numbers or
76 /* NOTE: some functions (some internal variants and exported ones)
77 * invalidate data portions of the TypeNodes. if external functions/callbacks
78 * are called, pointers to memory maintained by TypeNodes have to be looked up
79 * again. this affects most of the struct TypeNode fields, e.g. ->children or
80 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
81 * not ->supers[]), as all those memory portions can get realloc()ed during
82 * callback invocation.
85 * - g_type_from_name() should do an ordered array lookup after fetching the
86 * the quark, instead of a second hashtable lookup.
89 * lock handling issues when calling static functions are indicated by
90 * uppercase letter postfixes, all static functions have to have
91 * one of the below postfixes:
92 * - _I: [Indifferent about locking]
93 * function doesn't care about locks at all
94 * - _U: [Unlocked invocation]
95 * no read or write lock has to be held across function invocation
96 * (locks may be acquired and released during invocation though)
97 * - _L: [Locked invocation]
98 * a write lock or more than 0 read locks have to be held across
100 * - _W: [Write-locked invocation]
101 * a write lock has to be held across function invocation
102 * - _Wm: [Write-locked invocation, mutatable]
103 * like _W, but the write lock might be released and reacquired
104 * during invocation, watch your pointers
105 * - _WmREC: [Write-locked invocation, mutatable, recursive]
106 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
110 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
111 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
112 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
113 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
115 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
116 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
117 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
118 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
120 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
121 static const gchar _action[] = " invalidly modified type "; \
122 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
124 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
126 g_error ("%s()%s`%s'", _fname, _action, _tname); \
128 #define g_return_val_if_type_system_uninitialized(return_value) G_STMT_START{ \
129 if (G_UNLIKELY (!static_quark_type_flags)) \
131 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
132 "%s: You forgot to call g_type_init()", \
134 return (return_value); \
138 #ifdef G_ENABLE_DEBUG
139 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
140 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
143 #else /* !G_ENABLE_DEBUG */
144 #define DEBUG_CODE(debug_type, code_block) /* code_block */
145 #endif /* G_ENABLE_DEBUG */
147 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
148 G_TYPE_FLAG_INSTANTIATABLE | \
149 G_TYPE_FLAG_DERIVABLE | \
150 G_TYPE_FLAG_DEEP_DERIVABLE)
151 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
152 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
153 sizeof (gpointer)), \
156 /* The 2*sizeof(size_t) alignment here is borrowed from
157 * GNU libc, so it should be good most everywhere.
158 * It is more conservative than is needed on some 64-bit
159 * platforms, but ia64 does require a 16-byte alignment.
160 * The SIMD extensions for x86 and ppc32 would want a
161 * larger alignment than this, but we don't need to
162 * do better than malloc.
164 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
165 #define ALIGN_STRUCT(offset) \
166 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
169 /* --- typedefs --- */
170 typedef struct _TypeNode TypeNode;
171 typedef struct _CommonData CommonData;
172 typedef struct _BoxedData BoxedData;
173 typedef struct _IFaceData IFaceData;
174 typedef struct _ClassData ClassData;
175 typedef struct _InstanceData InstanceData;
176 typedef union _TypeData TypeData;
177 typedef struct _IFaceEntries IFaceEntries;
178 typedef struct _IFaceEntry IFaceEntry;
179 typedef struct _IFaceHolder IFaceHolder;
182 /* --- prototypes --- */
183 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
184 static void type_add_flags_W (TypeNode *node,
186 static void type_data_make_W (TypeNode *node,
187 const GTypeInfo *info,
188 const GTypeValueTable *value_table);
189 static inline void type_data_ref_Wm (TypeNode *node);
190 static inline void type_data_unref_U (TypeNode *node,
192 static void type_data_last_unref_Wm (TypeNode * node,
194 static inline gpointer type_get_qdata_L (TypeNode *node,
196 static inline void type_set_qdata_W (TypeNode *node,
199 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
200 GType instance_type);
201 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
203 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
205 static gboolean type_node_is_a_L (TypeNode *node,
206 TypeNode *iface_node);
209 /* --- enumeration --- */
211 /* The InitState enumeration is used to track the progress of initializing
212 * both classes and interface vtables. Keeping the state of initialization
213 * is necessary to handle new interfaces being added while we are initializing
214 * the class or other interfaces.
226 /* --- structures --- */
229 guint volatile ref_count;
231 guint n_children; /* writable with lock */
233 guint n_prerequisites : 9;
234 guint is_classed : 1;
235 guint is_instantiatable : 1;
236 guint mutatable_check_cache : 1; /* combines some common path checks */
237 GType *children; /* writable with lock */
238 TypeData * volatile data;
242 GAtomicArray iface_entries; /* for !iface types */
243 GAtomicArray offsets;
245 GType *prerequisites;
246 GType supers[1]; /* flexible array */
249 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
250 #define MAX_N_SUPERS (255)
251 #define MAX_N_CHILDREN (4095)
252 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
253 #define MAX_N_PREREQUISITES (511)
254 #define NODE_TYPE(node) (node->supers[0])
255 #define NODE_PARENT_TYPE(node) (node->supers[1])
256 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
257 #define NODE_NAME(node) (g_quark_to_string (node->qname))
258 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
259 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
260 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
261 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
262 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
263 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
264 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
265 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
266 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
267 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
268 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
269 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
271 #define NODE_IS_ANCESTOR(ancestor, node) \
272 ((ancestor)->n_supers <= (node)->n_supers && \
273 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
278 GInterfaceInfo *info;
286 GTypeInterface *vtable;
287 InitState init_state;
290 struct _IFaceEntries {
295 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
296 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
300 GTypeValueTable *value_table;
306 GBoxedCopyFunc copy_func;
307 GBoxedFreeFunc free_func;
314 GBaseInitFunc vtable_init_base;
315 GBaseFinalizeFunc vtable_finalize_base;
316 GClassInitFunc dflt_init;
317 GClassFinalizeFunc dflt_finalize;
318 gconstpointer dflt_data;
319 gpointer dflt_vtable;
326 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
327 GBaseInitFunc class_init_base;
328 GBaseFinalizeFunc class_finalize_base;
329 GClassInitFunc class_init;
330 GClassFinalizeFunc class_finalize;
331 gconstpointer class_data;
339 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
340 GBaseInitFunc class_init_base;
341 GBaseFinalizeFunc class_finalize_base;
342 GClassInitFunc class_init;
343 GClassFinalizeFunc class_finalize;
344 gconstpointer class_data;
346 guint16 instance_size;
347 guint16 private_size;
349 GInstanceInitFunc instance_init;
358 InstanceData instance;
363 GTypeClassCacheFunc cache_func;
368 GTypeInterfaceCheckFunc check_func;
372 /* --- variables --- */
373 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
374 static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
375 static guint static_n_class_cache_funcs = 0;
376 static ClassCacheFunc *static_class_cache_funcs = NULL;
377 static guint static_n_iface_check_funcs = 0;
378 static IFaceCheckFunc *static_iface_check_funcs = NULL;
379 static GQuark static_quark_type_flags = 0;
380 static GQuark static_quark_iface_holder = 0;
381 static GQuark static_quark_dependants_array = 0;
382 GTypeDebugFlags _g_type_debug_flags = 0;
384 /* --- type nodes --- */
385 static GHashTable *static_type_nodes_ht = NULL;
386 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
387 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
389 static inline TypeNode*
390 lookup_type_node_I (register GType utype)
392 if (utype > G_TYPE_FUNDAMENTAL_MAX)
393 return (TypeNode*) (utype & ~TYPE_ID_MASK);
395 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
399 type_node_any_new_W (TypeNode *pnode,
403 GTypeFundamentalFlags type_flags)
408 guint i, node_size = 0;
410 n_supers = pnode ? pnode->n_supers + 1 : 0;
413 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
414 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
415 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
416 node = g_malloc0 (node_size);
417 if (!pnode) /* offset fundamental types */
419 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
420 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
426 g_assert ((type & TYPE_ID_MASK) == 0);
428 node->n_supers = n_supers;
431 node->supers[0] = type;
434 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
435 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
437 if (NODE_IS_IFACE (node))
439 IFACE_NODE_N_PREREQUISITES (node) = 0;
440 IFACE_NODE_PREREQUISITES (node) = NULL;
443 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
447 node->supers[0] = type;
448 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
450 node->is_classed = pnode->is_classed;
451 node->is_instantiatable = pnode->is_instantiatable;
453 if (NODE_IS_IFACE (node))
455 IFACE_NODE_N_PREREQUISITES (node) = 0;
456 IFACE_NODE_PREREQUISITES (node) = NULL;
461 IFaceEntries *entries;
463 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
464 IFACE_ENTRIES_HEADER_SIZE,
468 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
470 entries->entry[j].vtable = NULL;
471 entries->entry[j].init_state = UNINITIALIZED;
473 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
478 i = pnode->n_children++;
479 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
480 pnode->children[i] = type;
483 node->plugin = plugin;
484 node->n_children = 0;
485 node->children = NULL;
487 node->qname = g_quark_from_string (name);
488 node->global_gdata = NULL;
490 g_hash_table_insert (static_type_nodes_ht,
491 GUINT_TO_POINTER (node->qname),
496 static inline GTypeFundamentalInfo*
497 type_node_fundamental_info_I (TypeNode *node)
499 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
501 if (ftype != NODE_TYPE (node))
502 node = lookup_type_node_I (ftype);
504 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
508 type_node_fundamental_new_W (GType ftype,
510 GTypeFundamentalFlags type_flags)
512 GTypeFundamentalInfo *finfo;
515 g_assert ((ftype & TYPE_ID_MASK) == 0);
516 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
518 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
519 static_fundamental_next++;
521 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
523 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
525 finfo = type_node_fundamental_info_I (node);
526 finfo->type_flags = type_flags;
532 type_node_new_W (TypeNode *pnode,
538 g_assert (pnode->n_supers < MAX_N_SUPERS);
539 g_assert (pnode->n_children < MAX_N_CHILDREN);
541 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
544 static inline IFaceEntry*
545 lookup_iface_entry_I (volatile IFaceEntries *entries,
546 TypeNode *iface_node)
557 G_ATOMIC_ARRAY_DO_TRANSACTION
558 (&iface_node->_prot.offsets, guint8,
561 offsets = transaction_data;
562 offset_index = entries->offset_index;
563 if (offsets != NULL &&
564 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
566 index = offsets[offset_index];
569 /* zero means unset, subtract one to get real index */
572 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
574 check = (IFaceEntry *)&entries->entry[index];
575 if (check->iface_type == NODE_TYPE (iface_node))
585 static inline IFaceEntry*
586 type_lookup_iface_entry_L (TypeNode *node,
587 TypeNode *iface_node)
589 if (!NODE_IS_IFACE (iface_node))
592 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
597 static inline gboolean
598 type_lookup_iface_vtable_I (TypeNode *node,
599 TypeNode *iface_node,
600 gpointer *vtable_ptr)
605 if (!NODE_IS_IFACE (iface_node))
612 G_ATOMIC_ARRAY_DO_TRANSACTION
613 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
615 entry = lookup_iface_entry_I (transaction_data, iface_node);
620 *vtable_ptr = entry->vtable;
629 static inline gboolean
630 type_lookup_prerequisite_L (TypeNode *iface,
631 GType prerequisite_type)
633 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
635 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
636 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
643 i = (n_prerequisites + 1) >> 1;
644 check = prerequisites + i;
645 if (prerequisite_type == *check)
647 else if (prerequisite_type > *check)
649 n_prerequisites -= i;
650 prerequisites = check;
652 else /* if (prerequisite_type < *check) */
653 n_prerequisites = i - 1;
655 while (n_prerequisites);
661 type_descriptive_name_I (GType type)
665 TypeNode *node = lookup_type_node_I (type);
667 return node ? NODE_NAME (node) : "<unknown>";
674 /* --- type consistency checks --- */
676 check_plugin_U (GTypePlugin *plugin,
677 gboolean need_complete_type_info,
678 gboolean need_complete_interface_info,
679 const gchar *type_name)
681 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
685 g_warning ("plugin handle for type `%s' is NULL",
689 if (!G_IS_TYPE_PLUGIN (plugin))
691 g_warning ("plugin pointer (%p) for type `%s' is invalid",
695 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
697 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
701 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
703 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
711 check_type_name_I (const gchar *type_name)
713 static const gchar extra_chars[] = "-_+";
714 const gchar *p = type_name;
717 if (!type_name[0] || !type_name[1] || !type_name[2])
719 g_warning ("type name `%s' is too short", type_name);
722 /* check the first letter */
723 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
724 for (p = type_name + 1; *p; p++)
725 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
726 (p[0] >= 'a' && p[0] <= 'z') ||
727 (p[0] >= '0' && p[0] <= '9') ||
728 strchr (extra_chars, p[0]));
731 g_warning ("type name `%s' contains invalid characters", type_name);
734 if (g_type_from_name (type_name))
736 g_warning ("cannot register existing type `%s'", type_name);
744 check_derivation_I (GType parent_type,
745 const gchar *type_name)
748 GTypeFundamentalInfo* finfo;
750 pnode = lookup_type_node_I (parent_type);
753 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
755 type_descriptive_name_I (parent_type));
758 finfo = type_node_fundamental_info_I (pnode);
759 /* ensure flat derivability */
760 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
762 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
767 /* ensure deep derivability */
768 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
769 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
771 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
781 check_collect_format_I (const gchar *collect_format)
783 const gchar *p = collect_format;
784 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
785 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
786 G_VALUE_COLLECT_POINTER, 0 };
789 if (!strchr (valid_format, *p++))
791 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
795 check_value_table_I (const gchar *type_name,
796 const GTypeValueTable *value_table)
800 else if (value_table->value_init == NULL)
802 if (value_table->value_free || value_table->value_copy ||
803 value_table->value_peek_pointer ||
804 value_table->collect_format || value_table->collect_value ||
805 value_table->lcopy_format || value_table->lcopy_value)
806 g_warning ("cannot handle uninitializable values of type `%s'",
810 else /* value_table->value_init != NULL */
812 if (!value_table->value_free)
815 * g_warning ("missing `value_free()' for type `%s'", type_name);
819 if (!value_table->value_copy)
821 g_warning ("missing `value_copy()' for type `%s'", type_name);
824 if ((value_table->collect_format || value_table->collect_value) &&
825 (!value_table->collect_format || !value_table->collect_value))
827 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
831 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
833 g_warning ("the `%s' specification for type `%s' is too long or invalid",
838 if ((value_table->lcopy_format || value_table->lcopy_value) &&
839 (!value_table->lcopy_format || !value_table->lcopy_value))
841 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
845 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
847 g_warning ("the `%s' specification for type `%s' is too long or invalid",
857 check_type_info_I (TypeNode *pnode,
859 const gchar *type_name,
860 const GTypeInfo *info)
862 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
863 gboolean is_interface = ftype == G_TYPE_INTERFACE;
865 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
867 /* check instance members */
868 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
869 (info->instance_size || info->n_preallocs || info->instance_init))
872 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
876 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
880 /* check class & interface members */
881 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
882 (info->class_init || info->class_finalize || info->class_data ||
883 info->class_size || info->base_init || info->base_finalize))
886 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
890 g_warning ("cannot create class for `%s' as non-classed fundamental",
894 /* check interface size */
895 if (is_interface && info->class_size < sizeof (GTypeInterface))
897 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
901 /* check class size */
902 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
904 if (info->class_size < sizeof (GTypeClass))
906 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
910 if (pnode && info->class_size < pnode->data->class.class_size)
912 g_warning ("specified class size for type `%s' is smaller "
913 "than the parent type's `%s' class size",
919 /* check instance size */
920 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
922 if (info->instance_size < sizeof (GTypeInstance))
924 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
928 if (pnode && info->instance_size < pnode->data->instance.instance_size)
930 g_warning ("specified instance size for type `%s' is smaller "
931 "than the parent type's `%s' instance size",
942 find_conforming_child_type_L (TypeNode *pnode,
945 TypeNode *node = NULL;
948 if (type_lookup_iface_entry_L (pnode, iface))
951 for (i = 0; i < pnode->n_children && !node; i++)
952 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
958 check_add_interface_L (GType instance_type,
961 TypeNode *node = lookup_type_node_I (instance_type);
962 TypeNode *iface = lookup_type_node_I (iface_type);
965 GType *prerequisites;
969 if (!node || !node->is_instantiatable)
971 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
972 type_descriptive_name_I (instance_type));
975 if (!iface || !NODE_IS_IFACE (iface))
977 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
978 type_descriptive_name_I (iface_type),
982 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
983 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
985 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
986 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
992 /* allow overriding of interface type introduced for parent type */
993 entry = type_lookup_iface_entry_L (node, iface);
994 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
996 /* ok, we do conform to this interface already, but the interface vtable was not
997 * yet intialized, and we just conform to the interface because it got added to
998 * one of our parents. so we allow overriding of holder info here.
1002 /* check whether one of our children already conforms (or whether the interface
1003 * got added to this node already)
1005 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1008 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
1014 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1015 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1017 tnode = lookup_type_node_I (prerequisites[i]);
1018 if (!type_node_is_a_L (node, tnode))
1020 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
1031 check_interface_info_I (TypeNode *iface,
1032 GType instance_type,
1033 const GInterfaceInfo *info)
1035 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1037 g_warning ("interface type `%s' for type `%s' comes without initializer",
1039 type_descriptive_name_I (instance_type));
1046 /* --- type info (type node data) --- */
1048 type_data_make_W (TypeNode *node,
1049 const GTypeInfo *info,
1050 const GTypeValueTable *value_table)
1053 GTypeValueTable *vtable = NULL;
1054 guint vtable_size = 0;
1056 g_assert (node->data == NULL && info != NULL);
1060 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1063 vtable = pnode->data->common.value_table;
1066 static const GTypeValueTable zero_vtable = { NULL, };
1068 value_table = &zero_vtable;
1073 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1074 vtable_size = sizeof (GTypeValueTable);
1075 if (value_table->collect_format)
1076 vtable_size += strlen (value_table->collect_format);
1077 if (value_table->lcopy_format)
1078 vtable_size += strlen (value_table->lcopy_format);
1082 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
1084 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1086 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1087 data->instance.class_size = info->class_size;
1088 data->instance.class_init_base = info->base_init;
1089 data->instance.class_finalize_base = info->base_finalize;
1090 data->instance.class_init = info->class_init;
1091 data->instance.class_finalize = info->class_finalize;
1092 data->instance.class_data = info->class_data;
1093 data->instance.class = NULL;
1094 data->instance.init_state = UNINITIALIZED;
1095 data->instance.instance_size = info->instance_size;
1096 /* We'll set the final value for data->instance.private size
1097 * after the parent class has been initialized
1099 data->instance.private_size = 0;
1100 #ifdef DISABLE_MEM_POOLS
1101 data->instance.n_preallocs = 0;
1102 #else /* !DISABLE_MEM_POOLS */
1103 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1104 #endif /* !DISABLE_MEM_POOLS */
1105 data->instance.instance_init = info->instance_init;
1107 else if (node->is_classed) /* only classed */
1109 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1111 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1112 data->class.class_size = info->class_size;
1113 data->class.class_init_base = info->base_init;
1114 data->class.class_finalize_base = info->base_finalize;
1115 data->class.class_init = info->class_init;
1116 data->class.class_finalize = info->class_finalize;
1117 data->class.class_data = info->class_data;
1118 data->class.class = NULL;
1119 data->class.init_state = UNINITIALIZED;
1121 else if (NODE_IS_IFACE (node))
1123 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1125 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1126 data->iface.vtable_size = info->class_size;
1127 data->iface.vtable_init_base = info->base_init;
1128 data->iface.vtable_finalize_base = info->base_finalize;
1129 data->iface.dflt_init = info->class_init;
1130 data->iface.dflt_finalize = info->class_finalize;
1131 data->iface.dflt_data = info->class_data;
1132 data->iface.dflt_vtable = NULL;
1134 else if (NODE_IS_BOXED (node))
1136 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1138 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1142 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1144 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1153 /* we allocate the vtable and its strings together with the type data, so
1154 * children can take over their parent's vtable pointer, and we don't
1155 * need to worry freeing it or not when the child data is destroyed
1157 *vtable = *value_table;
1158 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1160 vtable->collect_format = p;
1161 if (value_table->collect_format)
1163 strcat (p, value_table->collect_format);
1164 p += strlen (value_table->collect_format);
1168 vtable->lcopy_format = p;
1169 if (value_table->lcopy_format)
1170 strcat (p, value_table->lcopy_format);
1172 node->data->common.value_table = vtable;
1173 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1174 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1175 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1177 g_assert (node->data->common.value_table != NULL); /* paranoid */
1179 g_atomic_int_set ((int *) &node->ref_count, 1);
1183 type_data_ref_Wm (TypeNode *node)
1187 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1189 GTypeValueTable tmp_value_table;
1191 g_assert (node->plugin != NULL);
1195 type_data_ref_Wm (pnode);
1197 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1200 memset (&tmp_info, 0, sizeof (tmp_info));
1201 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1203 G_WRITE_UNLOCK (&type_rw_lock);
1204 g_type_plugin_use (node->plugin);
1205 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1206 G_WRITE_LOCK (&type_rw_lock);
1208 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1210 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1211 type_data_make_W (node, &tmp_info,
1212 check_value_table_I (NODE_NAME (node),
1213 &tmp_value_table) ? &tmp_value_table : NULL);
1217 g_assert (NODE_REFCOUNT (node) > 0);
1219 g_atomic_int_inc ((int *) &node->ref_count);
1223 static inline gboolean
1224 type_data_ref_U (TypeNode *node)
1229 current = NODE_REFCOUNT (node);
1233 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1239 iface_node_has_available_offset_L (TypeNode *iface_node,
1245 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1246 if (offsets == NULL)
1249 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1252 if (offsets[offset] == 0 ||
1253 offsets[offset] == for_index+1)
1260 find_free_iface_offset_L (IFaceEntries *entries)
1263 TypeNode *iface_node;
1268 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1273 for (i = 0; i < n_entries; i++)
1275 entry = &entries->entry[i];
1276 iface_node = lookup_type_node_I (entry->iface_type);
1278 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1282 while (i != n_entries);
1288 iface_node_set_offset_L (TypeNode *iface_node,
1292 guint8 *offsets, *old_offsets;
1293 int new_size, old_size;
1296 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1297 if (old_offsets == NULL)
1301 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1302 if (offset < old_size &&
1303 old_offsets[offset] == index + 1)
1304 return; /* Already set to this index, return */
1306 new_size = MAX (old_size, offset + 1);
1308 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1309 0, new_size - old_size);
1311 /* Mark new area as unused */
1312 for (i = old_size; i < new_size; i++)
1315 offsets[offset] = index + 1;
1317 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1321 type_node_add_iface_entry_W (TypeNode *node,
1323 IFaceEntry *parent_entry)
1325 IFaceEntries *entries;
1327 TypeNode *iface_node;
1331 g_assert (node->is_instantiatable);
1333 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1334 if (entries != NULL)
1336 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1338 g_assert (num_entries < MAX_N_INTERFACES);
1340 for (i = 0; i < num_entries; i++)
1342 entry = &entries->entry[i];
1343 if (entry->iface_type == iface_type)
1345 /* this can happen in two cases:
1346 * - our parent type already conformed to iface_type and node
1347 * got its own holder info. here, our children already have
1348 * entries and NULL vtables, since this will only work for
1349 * uninitialized classes.
1350 * - an interface type is added to an ancestor after it was
1351 * added to a child type.
1354 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1357 /* sick, interface is added to ancestor *after* child type;
1358 * nothing todo, the entry and our children were already setup correctly
1366 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1367 IFACE_ENTRIES_HEADER_SIZE,
1368 sizeof (IFaceEntry));
1369 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1370 i = num_entries - 1;
1372 entries->offset_index = 0;
1373 entries->entry[i].iface_type = iface_type;
1374 entries->entry[i].vtable = NULL;
1375 entries->entry[i].init_state = UNINITIALIZED;
1379 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1381 entries->entry[i].init_state = INITIALIZED;
1382 entries->entry[i].vtable = parent_entry->vtable;
1386 /* Update offsets in iface */
1387 iface_node = lookup_type_node_I (iface_type);
1389 if (iface_node_has_available_offset_L (iface_node,
1390 entries->offset_index,
1393 iface_node_set_offset_L (iface_node,
1394 entries->offset_index, i);
1398 entries->offset_index =
1399 find_free_iface_offset_L (entries);
1400 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1402 entry = &entries->entry[j];
1404 lookup_type_node_I (entry->iface_type);
1405 iface_node_set_offset_L (iface_node,
1406 entries->offset_index, j);
1410 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1414 for (i = 0; i < node->n_children; i++)
1415 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1420 type_add_interface_Wm (TypeNode *node,
1422 const GInterfaceInfo *info,
1423 GTypePlugin *plugin)
1425 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1429 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1431 iholder->next = iface_node_get_holders_L (iface);
1432 iface_node_set_holders_W (iface, iholder);
1433 iholder->instance_type = NODE_TYPE (node);
1434 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1435 iholder->plugin = plugin;
1437 /* create an iface entry for this type */
1438 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1440 /* if the class is already (partly) initialized, we may need to base
1441 * initalize and/or initialize the new interface.
1445 InitState class_state = node->data->class.init_state;
1447 if (class_state >= BASE_IFACE_INIT)
1448 type_iface_vtable_base_init_Wm (iface, node);
1450 if (class_state >= IFACE_INIT)
1451 type_iface_vtable_iface_init_Wm (iface, node);
1454 /* create iface entries for children of this type */
1455 entry = type_lookup_iface_entry_L (node, iface);
1456 for (i = 0; i < node->n_children; i++)
1457 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1461 type_iface_add_prerequisite_W (TypeNode *iface,
1462 TypeNode *prerequisite_node)
1464 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1465 GType *prerequisites, *dependants;
1466 guint n_dependants, i;
1468 g_assert (NODE_IS_IFACE (iface) &&
1469 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1470 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1472 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1473 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1474 if (prerequisites[i] == prerequisite_type)
1475 return; /* we already have that prerequisiste */
1476 else if (prerequisites[i] > prerequisite_type)
1478 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1479 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1480 IFACE_NODE_PREREQUISITES (iface),
1481 IFACE_NODE_N_PREREQUISITES (iface));
1482 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1483 g_memmove (prerequisites + i + 1, prerequisites + i,
1484 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1485 prerequisites[i] = prerequisite_type;
1487 /* we want to get notified when prerequisites get added to prerequisite_node */
1488 if (NODE_IS_IFACE (prerequisite_node))
1490 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1491 n_dependants = dependants ? dependants[0] : 0;
1493 dependants = g_renew (GType, dependants, n_dependants + 1);
1494 dependants[n_dependants] = NODE_TYPE (iface);
1495 dependants[0] = n_dependants;
1496 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1499 /* we need to notify all dependants */
1500 dependants = iface_node_get_dependants_array_L (iface);
1501 n_dependants = dependants ? dependants[0] : 0;
1502 for (i = 1; i <= n_dependants; i++)
1503 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1507 * g_type_interface_add_prerequisite:
1508 * @interface_type: #GType value of an interface type.
1509 * @prerequisite_type: #GType value of an interface or instantiatable type.
1511 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1512 * This means that any type implementing @interface_type must also implement
1513 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1514 * interface derivation (which GType doesn't support). An interface can have
1515 * at most one instantiatable prerequisite type.
1518 g_type_interface_add_prerequisite (GType interface_type,
1519 GType prerequisite_type)
1521 TypeNode *iface, *prerequisite_node;
1522 IFaceHolder *holders;
1524 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1525 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1526 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1528 iface = lookup_type_node_I (interface_type);
1529 prerequisite_node = lookup_type_node_I (prerequisite_type);
1530 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1532 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1533 type_descriptive_name_I (interface_type),
1534 type_descriptive_name_I (prerequisite_type));
1537 G_WRITE_LOCK (&type_rw_lock);
1538 holders = iface_node_get_holders_L (iface);
1541 G_WRITE_UNLOCK (&type_rw_lock);
1542 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1543 type_descriptive_name_I (prerequisite_type),
1544 type_descriptive_name_I (interface_type),
1545 type_descriptive_name_I (holders->instance_type));
1548 if (prerequisite_node->is_instantiatable)
1552 /* can have at most one publically installable instantiatable prerequisite */
1553 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1555 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1557 if (prnode->is_instantiatable)
1559 G_WRITE_UNLOCK (&type_rw_lock);
1560 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1561 type_descriptive_name_I (prerequisite_type),
1562 type_descriptive_name_I (interface_type),
1563 type_descriptive_name_I (NODE_TYPE (prnode)));
1568 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1569 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1570 G_WRITE_UNLOCK (&type_rw_lock);
1572 else if (NODE_IS_IFACE (prerequisite_node))
1574 GType *prerequisites;
1577 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1578 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1579 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1580 type_iface_add_prerequisite_W (iface, prerequisite_node);
1581 G_WRITE_UNLOCK (&type_rw_lock);
1585 G_WRITE_UNLOCK (&type_rw_lock);
1586 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1587 type_descriptive_name_I (prerequisite_type),
1588 type_descriptive_name_I (interface_type));
1593 * g_type_interface_prerequisites:
1594 * @interface_type: an interface type
1595 * @n_prerequisites: location to return the number of prerequisites, or %NULL
1597 * Returns the prerequisites of an interfaces type.
1601 * Returns: a newly-allocated zero-terminated array of #GType containing
1602 * the prerequisites of @interface_type
1605 g_type_interface_prerequisites (GType interface_type,
1606 guint *n_prerequisites)
1610 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1612 iface = lookup_type_node_I (interface_type);
1616 TypeNode *inode = NULL;
1619 G_READ_LOCK (&type_rw_lock);
1620 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1621 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1623 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1624 TypeNode *node = lookup_type_node_I (prerequisite);
1625 if (node->is_instantiatable)
1627 if (!inode || type_node_is_a_L (node, inode))
1631 types[n++] = NODE_TYPE (node);
1634 types[n++] = NODE_TYPE (inode);
1636 if (n_prerequisites)
1637 *n_prerequisites = n;
1638 G_READ_UNLOCK (&type_rw_lock);
1644 if (n_prerequisites)
1645 *n_prerequisites = 0;
1653 type_iface_peek_holder_L (TypeNode *iface,
1654 GType instance_type)
1656 IFaceHolder *iholder;
1658 g_assert (NODE_IS_IFACE (iface));
1660 iholder = iface_node_get_holders_L (iface);
1661 while (iholder && iholder->instance_type != instance_type)
1662 iholder = iholder->next;
1667 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1668 GType instance_type,
1671 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1673 if (iholder && !iholder->info && need_info)
1675 GInterfaceInfo tmp_info;
1677 g_assert (iholder->plugin != NULL);
1679 type_data_ref_Wm (iface);
1681 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1683 memset (&tmp_info, 0, sizeof (tmp_info));
1685 G_WRITE_UNLOCK (&type_rw_lock);
1686 g_type_plugin_use (iholder->plugin);
1687 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1688 G_WRITE_LOCK (&type_rw_lock);
1690 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1692 check_interface_info_I (iface, instance_type, &tmp_info);
1693 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1696 return iholder; /* we don't modify write lock upon returning NULL */
1700 type_iface_blow_holder_info_Wm (TypeNode *iface,
1701 GType instance_type)
1703 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1705 g_assert (NODE_IS_IFACE (iface));
1707 while (iholder->instance_type != instance_type)
1708 iholder = iholder->next;
1710 if (iholder->info && iholder->plugin)
1712 g_free (iholder->info);
1713 iholder->info = NULL;
1715 G_WRITE_UNLOCK (&type_rw_lock);
1716 g_type_plugin_unuse (iholder->plugin);
1717 type_data_unref_U (iface, FALSE);
1718 G_WRITE_LOCK (&type_rw_lock);
1722 /* Assumes type's class already exists
1724 static inline size_t
1725 type_total_instance_size_I (TypeNode *node)
1727 gsize total_instance_size;
1729 total_instance_size = node->data->instance.instance_size;
1730 if (node->data->instance.private_size != 0)
1731 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1733 return total_instance_size;
1736 /* --- type structure creation/destruction --- */
1740 } InstanceRealClass;
1743 instance_real_class_cmp (gconstpointer p1,
1746 const InstanceRealClass *irc1 = p1;
1747 const InstanceRealClass *irc2 = p2;
1748 guint8 *i1 = irc1->instance;
1749 guint8 *i2 = irc2->instance;
1750 return G_BSEARCH_ARRAY_CMP (i1, i2);
1753 G_LOCK_DEFINE_STATIC (instance_real_class);
1754 static GBSearchArray *instance_real_class_bsa = NULL;
1755 static GBSearchConfig instance_real_class_bconfig = {
1756 sizeof (InstanceRealClass),
1757 instance_real_class_cmp,
1762 instance_real_class_set (gpointer instance,
1765 InstanceRealClass key;
1766 key.instance = instance;
1768 G_LOCK (instance_real_class);
1769 if (!instance_real_class_bsa)
1770 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1771 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1772 G_UNLOCK (instance_real_class);
1776 instance_real_class_remove (gpointer instance)
1778 InstanceRealClass key, *node;
1780 key.instance = instance;
1781 G_LOCK (instance_real_class);
1782 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1783 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1784 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1785 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1787 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1788 instance_real_class_bsa = NULL;
1790 G_UNLOCK (instance_real_class);
1793 static inline GTypeClass*
1794 instance_real_class_get (gpointer instance)
1796 InstanceRealClass key, *node;
1798 key.instance = instance;
1799 G_LOCK (instance_real_class);
1800 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1801 class = node ? node->class : NULL;
1802 G_UNLOCK (instance_real_class);
1807 * g_type_create_instance:
1808 * @type: An instantiatable type to create an instance for.
1810 * Creates and initializes an instance of @type if @type is valid and
1811 * can be instantiated. The type system only performs basic allocation
1812 * and structure setups for instances: actual instance creation should
1813 * happen through functions supplied by the type's fundamental type
1814 * implementation. So use of g_type_create_instance() is reserved for
1815 * implementators of fundamental types only. E.g. instances of the
1816 * #GObject hierarchy should be created via g_object_new() and
1817 * <emphasis>never</emphasis> directly through
1818 * g_type_create_instance() which doesn't handle things like singleton
1819 * objects or object construction. Note: Do <emphasis>not</emphasis>
1820 * use this function, unless you're implementing a fundamental
1821 * type. Also language bindings should <emphasis>not</emphasis> use
1822 * this function but g_object_new() instead.
1824 * Returns: An allocated and initialized instance, subject to further
1825 * treatment by the fundamental type implementation.
1828 g_type_create_instance (GType type)
1831 GTypeInstance *instance;
1833 guint i, total_size;
1835 node = lookup_type_node_I (type);
1836 if (!node || !node->is_instantiatable)
1838 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1839 type_descriptive_name_I (type));
1842 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1843 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1845 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1846 type_descriptive_name_I (type));
1850 class = g_type_class_ref (type);
1851 total_size = type_total_instance_size_I (node);
1853 instance = g_slice_alloc0 (total_size);
1855 if (node->data->instance.private_size)
1856 instance_real_class_set (instance, class);
1857 for (i = node->n_supers; i > 0; i--)
1861 pnode = lookup_type_node_I (node->supers[i]);
1862 if (pnode->data->instance.instance_init)
1864 instance->g_class = pnode->data->instance.class;
1865 pnode->data->instance.instance_init (instance, class);
1868 if (node->data->instance.private_size)
1869 instance_real_class_remove (instance);
1871 instance->g_class = class;
1872 if (node->data->instance.instance_init)
1873 node->data->instance.instance_init (instance, class);
1879 * g_type_free_instance:
1880 * @instance: an instance of a type.
1882 * Frees an instance of a type, returning it to the instance pool for
1883 * the type, if there is one.
1885 * Like g_type_create_instance(), this function is reserved for
1886 * implementors of fundamental types.
1889 g_type_free_instance (GTypeInstance *instance)
1894 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1896 class = instance->g_class;
1897 node = lookup_type_node_I (class->g_type);
1898 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1900 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1901 type_descriptive_name_I (class->g_type));
1904 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1905 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1907 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1912 instance->g_class = NULL;
1913 #ifdef G_ENABLE_DEBUG
1914 memset (instance, 0xaa, type_total_instance_size_I (node));
1916 g_slice_free1 (type_total_instance_size_I (node), instance);
1918 g_type_class_unref (class);
1922 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1924 g_assert (iface->data);
1926 if (!iface->data->iface.dflt_vtable)
1928 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1929 iface->data->iface.dflt_vtable = vtable;
1930 vtable->g_type = NODE_TYPE (iface);
1931 vtable->g_instance_type = 0;
1932 if (iface->data->iface.vtable_init_base ||
1933 iface->data->iface.dflt_init)
1935 G_WRITE_UNLOCK (&type_rw_lock);
1936 if (iface->data->iface.vtable_init_base)
1937 iface->data->iface.vtable_init_base (vtable);
1938 if (iface->data->iface.dflt_init)
1939 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1940 G_WRITE_LOCK (&type_rw_lock);
1946 /* This is called to allocate and do the first part of initializing
1947 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1949 * A FALSE return indicates that we didn't find an init function for
1950 * this type/iface pair, so the vtable from the parent type should
1951 * be used. Note that the write lock is not modified upon a FALSE
1955 type_iface_vtable_base_init_Wm (TypeNode *iface,
1959 IFaceHolder *iholder;
1960 GTypeInterface *vtable = NULL;
1963 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1964 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1966 return FALSE; /* we don't modify write lock upon FALSE */
1968 type_iface_ensure_dflt_vtable_Wm (iface);
1970 entry = type_lookup_iface_entry_L (node, iface);
1972 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1974 entry->init_state = IFACE_INIT;
1976 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1977 if (pnode) /* want to copy over parent iface contents */
1979 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1982 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1985 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1986 entry->vtable = vtable;
1987 vtable->g_type = NODE_TYPE (iface);
1988 vtable->g_instance_type = NODE_TYPE (node);
1990 if (iface->data->iface.vtable_init_base)
1992 G_WRITE_UNLOCK (&type_rw_lock);
1993 iface->data->iface.vtable_init_base (vtable);
1994 G_WRITE_LOCK (&type_rw_lock);
1996 return TRUE; /* initialized the vtable */
1999 /* Finishes what type_iface_vtable_base_init_Wm started by
2000 * calling the interface init function.
2001 * this function may only be called for types with their
2002 * own interface holder info, i.e. types for which
2003 * g_type_add_interface*() was called and not children thereof.
2006 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2009 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2010 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2011 GTypeInterface *vtable = NULL;
2014 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2015 g_assert (iface->data && entry && iholder && iholder->info);
2016 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2018 entry->init_state = INITIALIZED;
2020 vtable = entry->vtable;
2022 if (iholder->info->interface_init)
2024 G_WRITE_UNLOCK (&type_rw_lock);
2025 if (iholder->info->interface_init)
2026 iholder->info->interface_init (vtable, iholder->info->interface_data);
2027 G_WRITE_LOCK (&type_rw_lock);
2030 for (i = 0; i < static_n_iface_check_funcs; i++)
2032 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2033 gpointer check_data = static_iface_check_funcs[i].check_data;
2035 G_WRITE_UNLOCK (&type_rw_lock);
2036 check_func (check_data, (gpointer)vtable);
2037 G_WRITE_LOCK (&type_rw_lock);
2042 type_iface_vtable_finalize_Wm (TypeNode *iface,
2044 GTypeInterface *vtable)
2046 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2047 IFaceHolder *iholder;
2049 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2050 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2052 return FALSE; /* we don't modify write lock upon FALSE */
2054 g_assert (entry && entry->vtable == vtable && iholder->info);
2056 entry->vtable = NULL;
2057 entry->init_state = UNINITIALIZED;
2058 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2060 G_WRITE_UNLOCK (&type_rw_lock);
2061 if (iholder->info->interface_finalize)
2062 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2063 if (iface->data->iface.vtable_finalize_base)
2064 iface->data->iface.vtable_finalize_base (vtable);
2065 G_WRITE_LOCK (&type_rw_lock);
2068 vtable->g_instance_type = 0;
2071 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2073 return TRUE; /* write lock modified */
2077 type_class_init_Wm (TypeNode *node,
2080 GSList *slist, *init_slist = NULL;
2082 IFaceEntries *entries;
2084 TypeNode *bnode, *pnode;
2087 g_assert (node->is_classed && node->data &&
2088 node->data->class.class_size &&
2089 !node->data->class.class &&
2090 node->data->class.init_state == UNINITIALIZED);
2092 class = g_malloc0 (node->data->class.class_size);
2093 node->data->class.class = class;
2094 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2098 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2100 memcpy (class, pclass, pnode->data->class.class_size);
2102 if (node->is_instantiatable)
2104 /* We need to initialize the private_size here rather than in
2105 * type_data_make_W() since the class init for the parent
2106 * class may have changed pnode->data->instance.private_size.
2108 node->data->instance.private_size = pnode->data->instance.private_size;
2111 class->g_type = NODE_TYPE (node);
2113 G_WRITE_UNLOCK (&type_rw_lock);
2115 /* stack all base class initialization functions, so we
2116 * call them in ascending order.
2118 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2119 if (bnode->data->class.class_init_base)
2120 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2121 for (slist = init_slist; slist; slist = slist->next)
2123 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2125 class_init_base (class);
2127 g_slist_free (init_slist);
2129 G_WRITE_LOCK (&type_rw_lock);
2131 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2133 /* Before we initialize the class, base initialize all interfaces, either
2134 * from parent, or through our holder info
2136 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2139 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2140 i < IFACE_ENTRIES_N_ENTRIES (entries))
2142 entry = &entries->entry[i];
2143 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2144 entry->init_state == IFACE_INIT)
2150 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2153 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2156 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2158 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2159 * doesn't modify write lock upon FALSE, so entry is still valid;
2161 g_assert (pnode != NULL);
2164 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2166 IFaceEntry *pentry = &pentries->entry[j];
2168 if (pentry->iface_type == entry->iface_type)
2170 entry->vtable = pentry->vtable;
2171 entry->init_state = INITIALIZED;
2175 g_assert (entry->vtable != NULL);
2178 /* If the write lock was released, additional interface entries might
2179 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2180 * be base-initialized when inserted, so we don't have to worry that
2181 * we might miss them. Uninitialized entries can only be moved higher
2182 * when new ones are inserted.
2187 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2189 G_WRITE_UNLOCK (&type_rw_lock);
2191 if (node->data->class.class_init)
2192 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2194 G_WRITE_LOCK (&type_rw_lock);
2196 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2198 /* finish initializing the interfaces through our holder info.
2199 * inherited interfaces are already init_state == INITIALIZED, because
2200 * they either got setup in the above base_init loop, or during
2201 * class_init from within type_add_interface_Wm() for this or
2202 * an anchestor type.
2205 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2207 entry = &entries->entry[i];
2208 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2209 entry->init_state == INITIALIZED)
2215 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2218 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2220 /* As in the loop above, additional initialized entries might be inserted
2221 * if the write lock is released, but that's harmless because the entries
2222 * we need to initialize only move higher in the list.
2227 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2231 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2234 IFaceEntries *entries;
2236 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2239 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2240 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2242 IFaceEntry *entry = &entries->entry[i];
2245 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2247 /* refetch entries, IFACES_ENTRIES might be modified */
2252 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2253 * iface vtable came from parent
2255 entry->vtable = NULL;
2256 entry->init_state = UNINITIALIZED;
2263 type_data_finalize_class_U (TypeNode *node,
2266 GTypeClass *class = cdata->class;
2269 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2271 if (cdata->class_finalize)
2272 cdata->class_finalize (class, (gpointer) cdata->class_data);
2274 /* call all base class destruction functions in descending order
2276 if (cdata->class_finalize_base)
2277 cdata->class_finalize_base (class);
2278 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2279 if (bnode->data->class.class_finalize_base)
2280 bnode->data->class.class_finalize_base (class);
2282 g_free (cdata->class);
2286 type_data_last_unref_Wm (TypeNode *node,
2289 g_return_if_fail (node != NULL && node->plugin != NULL);
2291 if (!node->data || NODE_REFCOUNT (node) == 0)
2293 g_warning ("cannot drop last reference to unreferenced type `%s'",
2298 /* call class cache hooks */
2299 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2303 G_WRITE_UNLOCK (&type_rw_lock);
2304 G_READ_LOCK (&type_rw_lock);
2305 for (i = 0; i < static_n_class_cache_funcs; i++)
2307 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2308 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2309 gboolean need_break;
2311 G_READ_UNLOCK (&type_rw_lock);
2312 need_break = cache_func (cache_data, node->data->class.class);
2313 G_READ_LOCK (&type_rw_lock);
2314 if (!node->data || NODE_REFCOUNT (node) == 0)
2315 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2319 G_READ_UNLOCK (&type_rw_lock);
2320 G_WRITE_LOCK (&type_rw_lock);
2323 /* may have been re-referenced meanwhile */
2324 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2326 GType ptype = NODE_PARENT_TYPE (node);
2329 if (node->is_instantiatable)
2331 /* destroy node->data->instance.mem_chunk */
2335 if (node->is_classed && tdata->class.class)
2337 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2338 type_data_finalize_class_ifaces_Wm (node);
2339 node->mutatable_check_cache = FALSE;
2341 G_WRITE_UNLOCK (&type_rw_lock);
2342 type_data_finalize_class_U (node, &tdata->class);
2343 G_WRITE_LOCK (&type_rw_lock);
2345 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2347 node->mutatable_check_cache = FALSE;
2349 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2351 G_WRITE_UNLOCK (&type_rw_lock);
2352 if (tdata->iface.dflt_finalize)
2353 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2354 if (tdata->iface.vtable_finalize_base)
2355 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2356 G_WRITE_LOCK (&type_rw_lock);
2358 g_free (tdata->iface.dflt_vtable);
2362 node->mutatable_check_cache = FALSE;
2366 /* freeing tdata->common.value_table and its contents is taken care of
2367 * by allocating it in one chunk with tdata
2371 G_WRITE_UNLOCK (&type_rw_lock);
2372 g_type_plugin_unuse (node->plugin);
2374 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2375 G_WRITE_LOCK (&type_rw_lock);
2380 type_data_unref_U (TypeNode *node,
2386 current = NODE_REFCOUNT (node);
2392 g_warning ("static type `%s' unreferenced too often",
2397 g_assert (current > 0);
2399 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2400 G_WRITE_LOCK (&type_rw_lock);
2401 type_data_last_unref_Wm (node, uncached);
2402 G_WRITE_UNLOCK (&type_rw_lock);
2403 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2406 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2410 * g_type_add_class_cache_func:
2411 * @cache_data: data to be passed to @cache_func
2412 * @cache_func: a #GTypeClassCacheFunc
2414 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2415 * class goes from one to zero. This can be used to prevent premature class
2416 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2417 * until one of them returns %TRUE. The functions have to check the class id
2418 * passed in to figure whether they actually want to cache the class of this
2419 * type, since all classes are routed through the same #GTypeClassCacheFunc
2423 g_type_add_class_cache_func (gpointer cache_data,
2424 GTypeClassCacheFunc cache_func)
2428 g_return_if_fail (cache_func != NULL);
2430 G_WRITE_LOCK (&type_rw_lock);
2431 i = static_n_class_cache_funcs++;
2432 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2433 static_class_cache_funcs[i].cache_data = cache_data;
2434 static_class_cache_funcs[i].cache_func = cache_func;
2435 G_WRITE_UNLOCK (&type_rw_lock);
2439 * g_type_remove_class_cache_func:
2440 * @cache_data: data that was given when adding @cache_func
2441 * @cache_func: a #GTypeClassCacheFunc
2443 * Removes a previously installed #GTypeClassCacheFunc. The cache
2444 * maintained by @cache_func has to be empty when calling
2445 * g_type_remove_class_cache_func() to avoid leaks.
2448 g_type_remove_class_cache_func (gpointer cache_data,
2449 GTypeClassCacheFunc cache_func)
2451 gboolean found_it = FALSE;
2454 g_return_if_fail (cache_func != NULL);
2456 G_WRITE_LOCK (&type_rw_lock);
2457 for (i = 0; i < static_n_class_cache_funcs; i++)
2458 if (static_class_cache_funcs[i].cache_data == cache_data &&
2459 static_class_cache_funcs[i].cache_func == cache_func)
2461 static_n_class_cache_funcs--;
2462 g_memmove (static_class_cache_funcs + i,
2463 static_class_cache_funcs + i + 1,
2464 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2465 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2469 G_WRITE_UNLOCK (&type_rw_lock);
2472 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2473 cache_func, cache_data);
2478 * g_type_add_interface_check:
2479 * @check_data: data to pass to @check_func
2480 * @check_func: function to be called after each interface
2483 * Adds a function to be called after an interface vtable is
2484 * initialized for any class (i.e. after the @interface_init member of
2485 * #GInterfaceInfo has been called).
2487 * This function is useful when you want to check an invariant that
2488 * depends on the interfaces of a class. For instance, the
2489 * implementation of #GObject uses this facility to check that an
2490 * object implements all of the properties that are defined on its
2496 g_type_add_interface_check (gpointer check_data,
2497 GTypeInterfaceCheckFunc check_func)
2501 g_return_if_fail (check_func != NULL);
2503 G_WRITE_LOCK (&type_rw_lock);
2504 i = static_n_iface_check_funcs++;
2505 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2506 static_iface_check_funcs[i].check_data = check_data;
2507 static_iface_check_funcs[i].check_func = check_func;
2508 G_WRITE_UNLOCK (&type_rw_lock);
2512 * g_type_remove_interface_check:
2513 * @check_data: callback data passed to g_type_add_interface_check()
2514 * @check_func: callback function passed to g_type_add_interface_check()
2516 * Removes an interface check function added with
2517 * g_type_add_interface_check().
2522 g_type_remove_interface_check (gpointer check_data,
2523 GTypeInterfaceCheckFunc check_func)
2525 gboolean found_it = FALSE;
2528 g_return_if_fail (check_func != NULL);
2530 G_WRITE_LOCK (&type_rw_lock);
2531 for (i = 0; i < static_n_iface_check_funcs; i++)
2532 if (static_iface_check_funcs[i].check_data == check_data &&
2533 static_iface_check_funcs[i].check_func == check_func)
2535 static_n_iface_check_funcs--;
2536 g_memmove (static_iface_check_funcs + i,
2537 static_iface_check_funcs + i + 1,
2538 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2539 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2543 G_WRITE_UNLOCK (&type_rw_lock);
2546 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2547 check_func, check_data);
2550 /* --- type registration --- */
2552 * g_type_register_fundamental:
2553 * @type_id: A predefined type identifier.
2554 * @type_name: 0-terminated string used as the name of the new type.
2555 * @info: The #GTypeInfo structure for this type.
2556 * @finfo: The #GTypeFundamentalInfo structure for this type.
2557 * @flags: Bitwise combination of #GTypeFlags values.
2559 * Registers @type_id as the predefined identifier and @type_name as the
2560 * name of a fundamental type. The type system uses the information
2561 * contained in the #GTypeInfo structure pointed to by @info and the
2562 * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
2563 * type and its instances. The value of @flags determines additional
2564 * characteristics of the fundamental type.
2566 * Returns: The predefined type identifier.
2569 g_type_register_fundamental (GType type_id,
2570 const gchar *type_name,
2571 const GTypeInfo *info,
2572 const GTypeFundamentalInfo *finfo,
2577 g_return_val_if_type_system_uninitialized (0);
2578 g_return_val_if_fail (type_id > 0, 0);
2579 g_return_val_if_fail (type_name != NULL, 0);
2580 g_return_val_if_fail (info != NULL, 0);
2581 g_return_val_if_fail (finfo != NULL, 0);
2583 if (!check_type_name_I (type_name))
2585 if ((type_id & TYPE_ID_MASK) ||
2586 type_id > G_TYPE_FUNDAMENTAL_MAX)
2588 g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2593 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2594 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2596 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2600 if (lookup_type_node_I (type_id))
2602 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2603 type_descriptive_name_I (type_id),
2608 G_WRITE_LOCK (&type_rw_lock);
2609 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2610 type_add_flags_W (node, flags);
2612 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2613 type_data_make_W (node, info,
2614 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2615 G_WRITE_UNLOCK (&type_rw_lock);
2617 return NODE_TYPE (node);
2621 * g_type_register_static_simple:
2622 * @parent_type: Type from which this type will be derived.
2623 * @type_name: 0-terminated string used as the name of the new type.
2624 * @class_size: Size of the class structure (see #GTypeInfo)
2625 * @class_init: Location of the class initialization function (see #GTypeInfo)
2626 * @instance_size: Size of the instance structure (see #GTypeInfo)
2627 * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2628 * @flags: Bitwise combination of #GTypeFlags values.
2630 * Registers @type_name as the name of a new static type derived from
2631 * @parent_type. The value of @flags determines the nature (e.g.
2632 * abstract or not) of the type. It works by filling a #GTypeInfo
2633 * struct and calling g_type_register_static().
2637 * Returns: The new type identifier.
2640 g_type_register_static_simple (GType parent_type,
2641 const gchar *type_name,
2643 GClassInitFunc class_init,
2644 guint instance_size,
2645 GInstanceInitFunc instance_init,
2650 info.class_size = class_size;
2651 info.base_init = NULL;
2652 info.base_finalize = NULL;
2653 info.class_init = class_init;
2654 info.class_finalize = NULL;
2655 info.class_data = NULL;
2656 info.instance_size = instance_size;
2657 info.n_preallocs = 0;
2658 info.instance_init = instance_init;
2659 info.value_table = NULL;
2661 return g_type_register_static (parent_type, type_name, &info, flags);
2665 * g_type_register_static:
2666 * @parent_type: Type from which this type will be derived.
2667 * @type_name: 0-terminated string used as the name of the new type.
2668 * @info: The #GTypeInfo structure for this type.
2669 * @flags: Bitwise combination of #GTypeFlags values.
2671 * Registers @type_name as the name of a new static type derived from
2672 * @parent_type. The type system uses the information contained in the
2673 * #GTypeInfo structure pointed to by @info to manage the type and its
2674 * instances (if not abstract). The value of @flags determines the nature
2675 * (e.g. abstract or not) of the type.
2677 * Returns: The new type identifier.
2680 g_type_register_static (GType parent_type,
2681 const gchar *type_name,
2682 const GTypeInfo *info,
2685 TypeNode *pnode, *node;
2688 g_return_val_if_type_system_uninitialized (0);
2689 g_return_val_if_fail (parent_type > 0, 0);
2690 g_return_val_if_fail (type_name != NULL, 0);
2691 g_return_val_if_fail (info != NULL, 0);
2693 if (!check_type_name_I (type_name) ||
2694 !check_derivation_I (parent_type, type_name))
2696 if (info->class_finalize)
2698 g_warning ("class finalizer specified for static type `%s'",
2703 pnode = lookup_type_node_I (parent_type);
2704 G_WRITE_LOCK (&type_rw_lock);
2705 type_data_ref_Wm (pnode);
2706 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2708 node = type_node_new_W (pnode, type_name, NULL);
2709 type_add_flags_W (node, flags);
2710 type = NODE_TYPE (node);
2711 type_data_make_W (node, info,
2712 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2714 G_WRITE_UNLOCK (&type_rw_lock);
2720 * g_type_register_dynamic:
2721 * @parent_type: Type from which this type will be derived.
2722 * @type_name: 0-terminated string used as the name of the new type.
2723 * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2724 * @flags: Bitwise combination of #GTypeFlags values.
2726 * Registers @type_name as the name of a new dynamic type derived from
2727 * @parent_type. The type system uses the information contained in the
2728 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2729 * instances (if not abstract). The value of @flags determines the nature
2730 * (e.g. abstract or not) of the type.
2732 * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2735 g_type_register_dynamic (GType parent_type,
2736 const gchar *type_name,
2737 GTypePlugin *plugin,
2740 TypeNode *pnode, *node;
2743 g_return_val_if_type_system_uninitialized (0);
2744 g_return_val_if_fail (parent_type > 0, 0);
2745 g_return_val_if_fail (type_name != NULL, 0);
2746 g_return_val_if_fail (plugin != NULL, 0);
2748 if (!check_type_name_I (type_name) ||
2749 !check_derivation_I (parent_type, type_name) ||
2750 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2753 G_WRITE_LOCK (&type_rw_lock);
2754 pnode = lookup_type_node_I (parent_type);
2755 node = type_node_new_W (pnode, type_name, plugin);
2756 type_add_flags_W (node, flags);
2757 type = NODE_TYPE (node);
2758 G_WRITE_UNLOCK (&type_rw_lock);
2764 * g_type_add_interface_static:
2765 * @instance_type: #GType value of an instantiable type.
2766 * @interface_type: #GType value of an interface type.
2767 * @info: The #GInterfaceInfo structure for this
2768 * (@instance_type, @interface_type) combination.
2770 * Adds the static @interface_type to @instantiable_type. The information
2771 * contained in the #GTypeInterfaceInfo structure pointed to by @info
2772 * is used to manage the relationship.
2775 g_type_add_interface_static (GType instance_type,
2776 GType interface_type,
2777 const GInterfaceInfo *info)
2779 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2780 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2781 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2783 /* we only need to lock class_init_rec_mutex if instance_type already has its
2784 * class initialized, however this function is rarely enough called to take
2785 * the simple route and always acquire class_init_rec_mutex.
2787 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2788 G_WRITE_LOCK (&type_rw_lock);
2789 if (check_add_interface_L (instance_type, interface_type))
2791 TypeNode *node = lookup_type_node_I (instance_type);
2792 TypeNode *iface = lookup_type_node_I (interface_type);
2793 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2794 type_add_interface_Wm (node, iface, info, NULL);
2796 G_WRITE_UNLOCK (&type_rw_lock);
2797 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2801 * g_type_add_interface_dynamic:
2802 * @instance_type: the #GType value of an instantiable type.
2803 * @interface_type: the #GType value of an interface type.
2804 * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2806 * Adds the dynamic @interface_type to @instantiable_type. The information
2807 * contained in the #GTypePlugin structure pointed to by @plugin
2808 * is used to manage the relationship.
2811 g_type_add_interface_dynamic (GType instance_type,
2812 GType interface_type,
2813 GTypePlugin *plugin)
2816 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2817 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2818 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2820 node = lookup_type_node_I (instance_type);
2821 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2824 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2825 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2826 G_WRITE_LOCK (&type_rw_lock);
2827 if (check_add_interface_L (instance_type, interface_type))
2829 TypeNode *iface = lookup_type_node_I (interface_type);
2830 type_add_interface_Wm (node, iface, NULL, plugin);
2832 G_WRITE_UNLOCK (&type_rw_lock);
2833 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2837 /* --- public API functions --- */
2840 * @type: Type ID of a classed type.
2842 * Increments the reference count of the class structure belonging to
2843 * @type. This function will demand-create the class if it doesn't
2846 * Returns: The #GTypeClass structure for the given type ID.
2849 g_type_class_ref (GType type)
2856 /* optimize for common code path */
2857 node = lookup_type_node_I (type);
2858 if (!node || !node->is_classed)
2860 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2861 type_descriptive_name_I (type));
2865 if (G_LIKELY (type_data_ref_U (node)))
2867 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2868 return node->data->class.class;
2874 /* here, we either have node->data->class.class == NULL, or a recursive
2875 * call to g_type_class_ref() with a partly initialized class, or
2876 * node->data->class.init_state == INITIALIZED, because any
2877 * concurrently running initialization was guarded by class_init_rec_mutex.
2879 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2881 /* we need an initialized parent class for initializing derived classes */
2882 ptype = NODE_PARENT_TYPE (node);
2883 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2885 G_WRITE_LOCK (&type_rw_lock);
2888 type_data_ref_Wm (node);
2890 if (!node->data->class.class) /* class uninitialized */
2891 type_class_init_Wm (node, pclass);
2893 G_WRITE_UNLOCK (&type_rw_lock);
2896 g_type_class_unref (pclass);
2898 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2900 return node->data->class.class;
2904 * g_type_class_unref:
2905 * @g_class: The #GTypeClass structure to unreference.
2907 * Decrements the reference count of the class structure being passed in.
2908 * Once the last reference count of a class has been released, classes
2909 * may be finalized by the type system, so further dereferencing of a
2910 * class pointer after g_type_class_unref() are invalid.
2913 g_type_class_unref (gpointer g_class)
2916 GTypeClass *class = g_class;
2918 g_return_if_fail (g_class != NULL);
2920 node = lookup_type_node_I (class->g_type);
2921 if (node && node->is_classed && NODE_REFCOUNT (node))
2922 type_data_unref_U (node, FALSE);
2924 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2925 type_descriptive_name_I (class->g_type));
2929 * g_type_class_unref_uncached:
2930 * @g_class: The #GTypeClass structure to unreference.
2932 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2933 * implementations. It unreferences a class without consulting the chain
2934 * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2938 g_type_class_unref_uncached (gpointer g_class)
2941 GTypeClass *class = g_class;
2943 g_return_if_fail (g_class != NULL);
2945 node = lookup_type_node_I (class->g_type);
2946 if (node && node->is_classed && NODE_REFCOUNT (node))
2947 type_data_unref_U (node, TRUE);
2949 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2950 type_descriptive_name_I (class->g_type));
2954 * g_type_class_peek:
2955 * @type: Type ID of a classed type.
2957 * This function is essentially the same as g_type_class_ref(), except that
2958 * the classes reference count isn't incremented. As a consequence, this function
2959 * may return %NULL if the class of the type passed in does not currently
2960 * exist (hasn't been referenced before).
2962 * Returns: The #GTypeClass structure for the given type ID or %NULL
2963 * if the class does not currently exist.
2966 g_type_class_peek (GType type)
2971 node = lookup_type_node_I (type);
2972 if (node && node->is_classed && NODE_REFCOUNT (node) &&
2973 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
2974 /* ref_count _may_ be 0 */
2975 class = node->data->class.class;
2983 * g_type_class_peek_static:
2984 * @type: Type ID of a classed type.
2986 * A more efficient version of g_type_class_peek() which works only for
2990 * Returns: The #GTypeClass structure for the given type ID or %NULL
2991 * if the class does not currently exist or is dynamically loaded.
2994 g_type_class_peek_static (GType type)
2999 node = lookup_type_node_I (type);
3000 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3001 /* peek only static types: */ node->plugin == NULL &&
3002 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3003 /* ref_count _may_ be 0 */
3004 class = node->data->class.class;
3012 * g_type_class_peek_parent:
3013 * @g_class: The #GTypeClass structure to retrieve the parent class for.
3015 * This is a convenience function often needed in class initializers.
3016 * It returns the class structure of the immediate parent type of the
3017 * class passed in. Since derived classes hold a reference count on
3018 * their parent classes as long as they are instantiated, the returned
3019 * class will always exist. This function is essentially equivalent
3023 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
3026 * Returns: The parent class of @g_class.
3029 g_type_class_peek_parent (gpointer g_class)
3032 gpointer class = NULL;
3034 g_return_val_if_fail (g_class != NULL, NULL);
3036 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3037 /* We used to acquire a read lock here. That is not necessary, since
3038 * parent->data->class.class is constant as long as the derived class
3041 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3043 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3044 class = node->data->class.class;
3046 else if (NODE_PARENT_TYPE (node))
3047 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
3053 * g_type_interface_peek:
3054 * @instance_class: A #GTypeClass structure.
3055 * @iface_type: An interface ID which this class conforms to.
3057 * Returns the #GTypeInterface structure of an interface to which the
3058 * passed in class conforms.
3060 * Returns: The GTypeInterface structure of iface_type if implemented
3061 * by @instance_class, %NULL otherwise
3064 g_type_interface_peek (gpointer instance_class,
3069 gpointer vtable = NULL;
3070 GTypeClass *class = instance_class;
3072 g_return_val_if_fail (instance_class != NULL, NULL);
3074 node = lookup_type_node_I (class->g_type);
3075 iface = lookup_type_node_I (iface_type);
3076 if (node && node->is_instantiatable && iface)
3077 type_lookup_iface_vtable_I (node, iface, &vtable);
3079 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
3085 * g_type_interface_peek_parent:
3086 * @g_iface: A #GTypeInterface structure.
3088 * Returns the corresponding #GTypeInterface structure of the parent type
3089 * of the instance type to which @g_iface belongs. This is useful when
3090 * deriving the implementation of an interface from the parent type and
3091 * then possibly overriding some methods.
3093 * Returns: The corresponding #GTypeInterface structure of the parent
3094 * type of the instance type to which @g_iface belongs, or
3095 * %NULL if the parent type doesn't conform to the interface.
3098 g_type_interface_peek_parent (gpointer g_iface)
3102 gpointer vtable = NULL;
3103 GTypeInterface *iface_class = g_iface;
3105 g_return_val_if_fail (g_iface != NULL, NULL);
3107 iface = lookup_type_node_I (iface_class->g_type);
3108 node = lookup_type_node_I (iface_class->g_instance_type);
3110 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3111 if (node && node->is_instantiatable && iface)
3112 type_lookup_iface_vtable_I (node, iface, &vtable);
3114 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
3120 * g_type_default_interface_ref:
3121 * @g_type: an interface type
3123 * Increments the reference count for the interface type @g_type,
3124 * and returns the default interface vtable for the type.
3126 * If the type is not currently in use, then the default vtable
3127 * for the type will be created and initalized by calling
3128 * the base interface init and default vtable init functions for
3129 * the type (the @<structfield>base_init</structfield>
3130 * and <structfield>class_init</structfield> members of #GTypeInfo).
3131 * Calling g_type_default_interface_ref() is useful when you
3132 * want to make sure that signals and properties for an interface
3133 * have been installed.
3137 * Returns: the default vtable for the interface; call
3138 * g_type_default_interface_unref() when you are done using
3142 g_type_default_interface_ref (GType g_type)
3145 gpointer dflt_vtable;
3147 G_WRITE_LOCK (&type_rw_lock);
3149 node = lookup_type_node_I (g_type);
3150 if (!node || !NODE_IS_IFACE (node) ||
3151 (node->data && NODE_REFCOUNT (node) == 0))
3153 G_WRITE_UNLOCK (&type_rw_lock);
3154 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3155 type_descriptive_name_I (g_type));
3159 if (!node->data || !node->data->iface.dflt_vtable)
3161 G_WRITE_UNLOCK (&type_rw_lock);
3162 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3163 G_WRITE_LOCK (&type_rw_lock);
3164 node = lookup_type_node_I (g_type);
3165 type_data_ref_Wm (node);
3166 type_iface_ensure_dflt_vtable_Wm (node);
3167 g_static_rec_mutex_unlock (&class_init_rec_mutex);
3170 type_data_ref_Wm (node); /* ref_count >= 1 already */
3172 dflt_vtable = node->data->iface.dflt_vtable;
3173 G_WRITE_UNLOCK (&type_rw_lock);
3179 * g_type_default_interface_peek:
3180 * @g_type: an interface type
3182 * If the interface type @g_type is currently in use, returns its
3183 * default interface vtable.
3187 * Returns: the default vtable for the interface, or %NULL
3188 * if the type is not currently in use.
3191 g_type_default_interface_peek (GType g_type)
3196 node = lookup_type_node_I (g_type);
3197 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3198 vtable = node->data->iface.dflt_vtable;
3206 * g_type_default_interface_unref:
3207 * @g_iface: the default vtable structure for a interface, as
3208 * returned by g_type_default_interface_ref()
3210 * Decrements the reference count for the type corresponding to the
3211 * interface default vtable @g_iface. If the type is dynamic, then
3212 * when no one is using the interface and all references have
3213 * been released, the finalize function for the interface's default
3214 * vtable (the <structfield>class_finalize</structfield> member of
3215 * #GTypeInfo) will be called.
3220 g_type_default_interface_unref (gpointer g_iface)
3223 GTypeInterface *vtable = g_iface;
3225 g_return_if_fail (g_iface != NULL);
3227 node = lookup_type_node_I (vtable->g_type);
3228 if (node && NODE_IS_IFACE (node))
3229 type_data_unref_U (node, FALSE);
3231 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3232 type_descriptive_name_I (vtable->g_type));
3237 * @type: Type to return name for.
3239 * Get the unique name that is assigned to a type ID. Note that this
3240 * function (like all other GType API) cannot cope with invalid type
3241 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3242 * other validly registered type ID, but randomized type IDs should
3243 * not be passed in and will most likely lead to a crash.
3245 * Returns: Static type name or %NULL.
3247 G_CONST_RETURN gchar*
3248 g_type_name (GType type)
3252 g_return_val_if_type_system_uninitialized (NULL);
3254 node = lookup_type_node_I (type);
3256 return node ? NODE_NAME (node) : NULL;
3261 * @type: Type to return quark of type name for.
3263 * Get the corresponding quark of the type IDs name.
3265 * Returns: The type names quark or 0.
3268 g_type_qname (GType type)
3272 node = lookup_type_node_I (type);
3274 return node ? node->qname : 0;
3279 * @name: Type name to lookup.
3281 * Lookup the type ID from a given type name, returning 0 if no type
3282 * has been registered under this name (this is the preferred method
3283 * to find out by name whether a specific type has been registered
3286 * Returns: Corresponding type ID or 0.
3289 g_type_from_name (const gchar *name)
3294 g_return_val_if_fail (name != NULL, 0);
3296 quark = g_quark_try_string (name);
3299 G_READ_LOCK (&type_rw_lock);
3300 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
3301 G_READ_UNLOCK (&type_rw_lock);
3309 * @type: The derived type.
3311 * Return the direct parent type of the passed in type. If the passed
3312 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3314 * Returns: The parent type.
3317 g_type_parent (GType type)
3321 node = lookup_type_node_I (type);
3323 return node ? NODE_PARENT_TYPE (node) : 0;
3328 * @type: A #GType value.
3330 * Returns the length of the ancestry of the passed in type. This
3331 * includes the type itself, so that e.g. a fundamental type has depth 1.
3333 * Returns: The depth of @type.
3336 g_type_depth (GType type)
3340 node = lookup_type_node_I (type);
3342 return node ? node->n_supers + 1 : 0;
3347 * @leaf_type: Descendant of @root_type and the type to be returned.
3348 * @root_type: Immediate parent of the returned type.
3350 * Given a @leaf_type and a @root_type which is contained in its
3351 * anchestry, return the type that @root_type is the immediate parent
3352 * of. In other words, this function determines the type that is
3353 * derived directly from @root_type which is also a base class of
3354 * @leaf_type. Given a root type and a leaf type, this function can
3355 * be used to determine the types and order in which the leaf type is
3356 * descended from the root type.
3358 * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3361 g_type_next_base (GType type,
3367 node = lookup_type_node_I (type);
3370 TypeNode *base_node = lookup_type_node_I (base_type);
3372 if (base_node && base_node->n_supers < node->n_supers)
3374 guint n = node->n_supers - base_node->n_supers;
3376 if (node->supers[n] == base_type)
3377 atype = node->supers[n - 1];
3384 static inline gboolean
3385 type_node_check_conformities_UorL (TypeNode *node,
3386 TypeNode *iface_node,
3387 /* support_inheritance */
3388 gboolean support_interfaces,
3389 gboolean support_prerequisites,
3394 if (/* support_inheritance && */
3395 NODE_IS_ANCESTOR (iface_node, node))
3398 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3399 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3401 if (support_interfaces)
3405 if (type_lookup_iface_entry_L (node, iface_node))
3410 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3415 support_prerequisites)
3418 G_READ_LOCK (&type_rw_lock);
3419 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3422 G_READ_UNLOCK (&type_rw_lock);
3428 type_node_is_a_L (TypeNode *node,
3429 TypeNode *iface_node)
3431 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3434 static inline gboolean
3435 type_node_conforms_to_U (TypeNode *node,
3436 TypeNode *iface_node,
3437 gboolean support_interfaces,
3438 gboolean support_prerequisites)
3440 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3445 * @type: Type to check anchestry for.
3446 * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3448 * If @is_a_type is a derivable type, check whether @type is a
3449 * descendant of @is_a_type. If @is_a_type is an interface, check
3450 * whether @type conforms to it.
3452 * Returns: %TRUE if @type is_a @is_a_type holds true.
3455 g_type_is_a (GType type,
3458 TypeNode *node, *iface_node;
3461 node = lookup_type_node_I (type);
3462 iface_node = lookup_type_node_I (iface_type);
3463 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3470 * @type: The parent type.
3471 * @n_children: Optional #guint pointer to contain the number of child types.
3473 * Return a newly allocated and 0-terminated array of type IDs, listing the
3474 * child types of @type. The return value has to be g_free()ed after use.
3476 * Returns: Newly allocated and 0-terminated array of child types.
3479 g_type_children (GType type,
3484 node = lookup_type_node_I (type);
3489 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3490 children = g_new (GType, node->n_children + 1);
3491 memcpy (children, node->children, sizeof (GType) * node->n_children);
3492 children[node->n_children] = 0;
3495 *n_children = node->n_children;
3496 G_READ_UNLOCK (&type_rw_lock);
3510 * g_type_interfaces:
3511 * @type: The type to list interface types for.
3512 * @n_interfaces: Optional #guint pointer to contain the number of
3515 * Return a newly allocated and 0-terminated array of type IDs, listing the
3516 * interface types that @type conforms to. The return value has to be
3517 * g_free()ed after use.
3519 * Returns: Newly allocated and 0-terminated array of interface types.
3522 g_type_interfaces (GType type,
3523 guint *n_interfaces)
3527 node = lookup_type_node_I (type);
3528 if (node && node->is_instantiatable)
3530 IFaceEntries *entries;
3534 G_READ_LOCK (&type_rw_lock);
3535 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3538 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3539 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3540 ifaces[i] = entries->entry[i].iface_type;
3544 ifaces = g_new (GType, 1);
3551 G_READ_UNLOCK (&type_rw_lock);
3564 typedef struct _QData QData;
3576 static inline gpointer
3577 type_get_qdata_L (TypeNode *node,
3580 GData *gdata = node->global_gdata;
3582 if (quark && gdata && gdata->n_qdatas)
3584 QData *qdatas = gdata->qdatas - 1;
3585 guint n_qdatas = gdata->n_qdatas;
3592 i = (n_qdatas + 1) / 2;
3594 if (quark == check->quark)
3596 else if (quark > check->quark)
3601 else /* if (quark < check->quark) */
3612 * @quark: a #GQuark id to identify the data
3614 * Obtains data which has previously been attached to @type
3615 * with g_type_set_qdata().
3617 * Returns: the data, or %NULL if no data was found
3620 g_type_get_qdata (GType type,
3626 node = lookup_type_node_I (type);
3629 G_READ_LOCK (&type_rw_lock);
3630 data = type_get_qdata_L (node, quark);
3631 G_READ_UNLOCK (&type_rw_lock);
3635 g_return_val_if_fail (node != NULL, NULL);
3642 type_set_qdata_W (TypeNode *node,
3650 /* setup qdata list if necessary */
3651 if (!node->global_gdata)
3652 node->global_gdata = g_new0 (GData, 1);
3653 gdata = node->global_gdata;
3655 /* try resetting old data */
3656 qdata = gdata->qdatas;
3657 for (i = 0; i < gdata->n_qdatas; i++)
3658 if (qdata[i].quark == quark)
3660 qdata[i].data = data;
3666 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3667 qdata = gdata->qdatas;
3668 for (i = 0; i < gdata->n_qdatas - 1; i++)
3669 if (qdata[i].quark > quark)
3671 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3672 qdata[i].quark = quark;
3673 qdata[i].data = data;
3679 * @quark: a #GQuark id to identify the data
3682 * Attaches arbitrary data to a type.
3685 g_type_set_qdata (GType type,
3691 g_return_if_fail (quark != 0);
3693 node = lookup_type_node_I (type);
3696 G_WRITE_LOCK (&type_rw_lock);
3697 type_set_qdata_W (node, quark, data);
3698 G_WRITE_UNLOCK (&type_rw_lock);
3701 g_return_if_fail (node != NULL);
3705 type_add_flags_W (TypeNode *node,
3710 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3711 g_return_if_fail (node != NULL);
3713 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3714 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3715 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3717 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3722 * @type: the #GType value of a static, classed type.
3723 * @query: A user provided structure that is filled in with constant values
3726 * Queries the type system for information about a specific type.
3727 * This function will fill in a user-provided structure to hold
3728 * type-specific information. If an invalid #GType is passed in, the
3729 * @type member of the #GTypeQuery is 0. All members filled into the
3730 * #GTypeQuery structure should be considered constant and have to be
3734 g_type_query (GType type,
3739 g_return_if_fail (query != NULL);
3741 /* if node is not static and classed, we won't allow query */
3743 node = lookup_type_node_I (type);
3744 if (node && node->is_classed && !node->plugin)
3746 /* type is classed and probably even instantiatable */
3747 G_READ_LOCK (&type_rw_lock);
3748 if (node->data) /* type is static or referenced */
3750 query->type = NODE_TYPE (node);
3751 query->type_name = NODE_NAME (node);
3752 query->class_size = node->data->class.class_size;
3753 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3755 G_READ_UNLOCK (&type_rw_lock);
3760 /* --- implementation details --- */
3762 g_type_test_flags (GType type,
3766 gboolean result = FALSE;
3768 node = lookup_type_node_I (type);
3771 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3772 guint tflags = flags & TYPE_FLAG_MASK;
3776 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3778 fflags = (finfo->type_flags & fflags) == fflags;
3785 G_READ_LOCK (&type_rw_lock);
3786 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3787 G_READ_UNLOCK (&type_rw_lock);
3792 result = tflags && fflags;
3799 * g_type_get_plugin:
3800 * @type: The #GType to retrieve the plugin for.
3802 * Returns the #GTypePlugin structure for @type or
3803 * %NULL if @type does not have a #GTypePlugin structure.
3805 * Returns: The corresponding plugin if @type is a dynamic type,
3809 g_type_get_plugin (GType type)
3813 node = lookup_type_node_I (type);
3815 return node ? node->plugin : NULL;
3819 * g_type_interface_get_plugin:
3820 * @instance_type: the #GType value of an instantiatable type.
3821 * @interface_type: the #GType value of an interface type.
3823 * Returns the #GTypePlugin structure for the dynamic interface
3824 * @interface_type which has been added to @instance_type, or %NULL if
3825 * @interface_type has not been added to @instance_type or does not
3826 * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3828 * Returns: the #GTypePlugin for the dynamic interface @interface_type
3829 * of @instance_type.
3832 g_type_interface_get_plugin (GType instance_type,
3833 GType interface_type)
3838 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3840 node = lookup_type_node_I (instance_type);
3841 iface = lookup_type_node_I (interface_type);
3844 IFaceHolder *iholder;
3845 GTypePlugin *plugin;
3847 G_READ_LOCK (&type_rw_lock);
3849 iholder = iface_node_get_holders_L (iface);
3850 while (iholder && iholder->instance_type != instance_type)
3851 iholder = iholder->next;
3852 plugin = iholder ? iholder->plugin : NULL;
3854 G_READ_UNLOCK (&type_rw_lock);
3859 g_return_val_if_fail (node == NULL, NULL);
3860 g_return_val_if_fail (iface == NULL, NULL);
3862 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3868 * g_type_fundamental_next:
3870 * Returns the next free fundamental type id which can be used to
3871 * register a new fundamental type with g_type_register_fundamental().
3872 * The returned type ID represents the highest currently registered
3873 * fundamental type identifier.
3875 * Returns: The nextmost fundamental type ID to be registered,
3876 * or 0 if the type system ran out of fundamental type IDs.
3879 g_type_fundamental_next (void)
3883 G_READ_LOCK (&type_rw_lock);
3884 type = static_fundamental_next;
3885 G_READ_UNLOCK (&type_rw_lock);
3886 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3887 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3891 * g_type_fundamental:
3892 * @type_id: valid type ID
3894 * Internal function, used to extract the fundamental type ID portion.
3895 * use G_TYPE_FUNDAMENTAL() instead.
3897 * Returns: fundamental type ID
3900 g_type_fundamental (GType type_id)
3902 TypeNode *node = lookup_type_node_I (type_id);
3904 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3908 g_type_check_instance_is_a (GTypeInstance *type_instance,
3911 TypeNode *node, *iface;
3914 if (!type_instance || !type_instance->g_class)
3917 node = lookup_type_node_I (type_instance->g_class->g_type);
3918 iface = lookup_type_node_I (iface_type);
3919 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3925 g_type_check_class_is_a (GTypeClass *type_class,
3928 TypeNode *node, *iface;
3934 node = lookup_type_node_I (type_class->g_type);
3935 iface = lookup_type_node_I (is_a_type);
3936 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3942 g_type_check_instance_cast (GTypeInstance *type_instance,
3947 if (type_instance->g_class)
3949 TypeNode *node, *iface;
3950 gboolean is_instantiatable, check;
3952 node = lookup_type_node_I (type_instance->g_class->g_type);
3953 is_instantiatable = node && node->is_instantiatable;
3954 iface = lookup_type_node_I (iface_type);
3955 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3957 return type_instance;
3959 if (is_instantiatable)
3960 g_warning ("invalid cast from `%s' to `%s'",
3961 type_descriptive_name_I (type_instance->g_class->g_type),
3962 type_descriptive_name_I (iface_type));
3964 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3965 type_descriptive_name_I (type_instance->g_class->g_type),
3966 type_descriptive_name_I (iface_type));
3969 g_warning ("invalid unclassed pointer in cast to `%s'",
3970 type_descriptive_name_I (iface_type));
3973 return type_instance;
3977 g_type_check_class_cast (GTypeClass *type_class,
3982 TypeNode *node, *iface;
3983 gboolean is_classed, check;
3985 node = lookup_type_node_I (type_class->g_type);
3986 is_classed = node && node->is_classed;
3987 iface = lookup_type_node_I (is_a_type);
3988 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3993 g_warning ("invalid class cast from `%s' to `%s'",
3994 type_descriptive_name_I (type_class->g_type),
3995 type_descriptive_name_I (is_a_type));
3997 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3998 type_descriptive_name_I (type_class->g_type),
3999 type_descriptive_name_I (is_a_type));
4002 g_warning ("invalid class cast from (NULL) pointer to `%s'",
4003 type_descriptive_name_I (is_a_type));
4008 * g_type_check_instance:
4009 * @instance: A valid #GTypeInstance structure.
4011 * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
4014 * @Returns: #TRUE if @instance is valid, #FALSE otherwise.
4017 g_type_check_instance (GTypeInstance *type_instance)
4019 /* this function is just here to make the signal system
4020 * conveniently elaborated on instance checks
4024 if (type_instance->g_class)
4026 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4028 if (node && node->is_instantiatable)
4031 g_warning ("instance of invalid non-instantiatable type `%s'",
4032 type_descriptive_name_I (type_instance->g_class->g_type));
4035 g_warning ("instance with invalid (NULL) class pointer");
4038 g_warning ("invalid (NULL) pointer instance");
4043 static inline gboolean
4044 type_check_is_value_type_U (GType type)
4046 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4049 /* common path speed up */
4050 node = lookup_type_node_I (type);
4051 if (node && node->mutatable_check_cache)
4054 G_READ_LOCK (&type_rw_lock);
4058 if (node->data && NODE_REFCOUNT (node) > 0 &&
4059 node->data->common.value_table->value_init)
4060 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4061 else if (NODE_IS_IFACE (node))
4065 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4067 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4068 TypeNode *prnode = lookup_type_node_I (prtype);
4070 if (prnode->is_instantiatable)
4073 node = lookup_type_node_I (type);
4079 G_READ_UNLOCK (&type_rw_lock);
4081 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4085 g_type_check_is_value_type (GType type)
4087 return type_check_is_value_type_U (type);
4091 g_type_check_value (GValue *value)
4093 return value && type_check_is_value_type_U (value->g_type);
4097 g_type_check_value_holds (GValue *value,
4100 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4104 * g_type_value_table_peek:
4105 * @type: A #GType value.
4107 * Returns the location of the #GTypeValueTable associated with @type.
4108 * <emphasis>Note that this function should only be used from source code
4109 * that implements or has internal knowledge of the implementation of
4112 * Returns: Location of the #GTypeValueTable associated with @type or
4113 * %NULL if there is no #GTypeValueTable associated with @type.
4116 g_type_value_table_peek (GType type)
4118 GTypeValueTable *vtable = NULL;
4119 TypeNode *node = lookup_type_node_I (type);
4120 gboolean has_refed_data, has_table;
4122 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4123 return node->data->common.value_table;
4125 G_READ_LOCK (&type_rw_lock);
4128 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4129 has_table = has_refed_data && node->data->common.value_table->value_init;
4133 vtable = node->data->common.value_table;
4134 else if (NODE_IS_IFACE (node))
4138 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4140 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4141 TypeNode *prnode = lookup_type_node_I (prtype);
4143 if (prnode->is_instantiatable)
4146 node = lookup_type_node_I (type);
4147 goto restart_table_peek;
4153 G_READ_UNLOCK (&type_rw_lock);
4159 g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
4160 if (!has_refed_data)
4161 g_warning ("can't peek value table for type `%s' which is not currently referenced",
4162 type_descriptive_name_I (type));
4167 G_CONST_RETURN gchar*
4168 g_type_name_from_instance (GTypeInstance *instance)
4171 return "<NULL-instance>";
4173 return g_type_name_from_class (instance->g_class);
4176 G_CONST_RETURN gchar*
4177 g_type_name_from_class (GTypeClass *g_class)
4180 return "<NULL-class>";
4182 return g_type_name (g_class->g_type);
4186 /* --- private api for gboxed.c --- */
4188 _g_type_boxed_copy (GType type, gpointer value)
4190 TypeNode *node = lookup_type_node_I (type);
4192 return node->data->boxed.copy_func (value);
4196 _g_type_boxed_free (GType type, gpointer value)
4198 TypeNode *node = lookup_type_node_I (type);
4200 node->data->boxed.free_func (value);
4204 _g_type_boxed_init (GType type,
4205 GBoxedCopyFunc copy_func,
4206 GBoxedFreeFunc free_func)
4208 TypeNode *node = lookup_type_node_I (type);
4210 node->data->boxed.copy_func = copy_func;
4211 node->data->boxed.free_func = free_func;
4214 /* --- initialization --- */
4216 * g_type_init_with_debug_flags:
4217 * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
4218 * debugging purposes.
4220 * Similar to g_type_init(), but additionally sets debug flags.
4223 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4225 G_LOCK_DEFINE_STATIC (type_init_lock);
4226 const gchar *env_string;
4229 volatile GType votype;
4231 #ifdef G_THREADS_ENABLED
4232 if (!g_thread_get_initialized())
4233 g_thread_init (NULL);
4236 G_LOCK (type_init_lock);
4238 G_WRITE_LOCK (&type_rw_lock);
4240 if (static_quark_type_flags)
4242 G_WRITE_UNLOCK (&type_rw_lock);
4243 G_UNLOCK (type_init_lock);
4247 /* setup GObject library wide debugging flags */
4248 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
4249 env_string = g_getenv ("GOBJECT_DEBUG");
4250 if (env_string != NULL)
4252 static GDebugKey debug_keys[] = {
4253 { "objects", G_TYPE_DEBUG_OBJECTS },
4254 { "signals", G_TYPE_DEBUG_SIGNALS },
4257 _g_type_debug_flags |= g_parse_debug_string (env_string,
4259 sizeof (debug_keys) / sizeof (debug_keys[0]));
4264 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4265 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4266 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4268 /* type qname hash table */
4269 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
4271 /* invalid type G_TYPE_INVALID (0)
4273 static_fundamental_type_nodes[0] = NULL;
4275 /* void type G_TYPE_NONE
4277 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4278 votype = NODE_TYPE (node);
4279 g_assert (votype == G_TYPE_NONE);
4281 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4283 memset (&info, 0, sizeof (info));
4284 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4285 votype = NODE_TYPE (node);
4286 type_data_make_W (node, &info, NULL);
4287 g_assert (votype == G_TYPE_INTERFACE);
4289 G_WRITE_UNLOCK (&type_rw_lock);
4293 /* G_TYPE_TYPE_PLUGIN
4295 votype = g_type_plugin_get_type ();
4297 /* G_TYPE_* value types
4299 g_value_types_init ();
4301 /* G_TYPE_ENUM & G_TYPE_FLAGS
4303 g_enum_types_init ();
4307 g_boxed_type_init ();
4311 g_param_type_init ();
4315 g_object_type_init ();
4317 /* G_TYPE_PARAM_* pspec types
4319 g_param_spec_types_init ();
4321 /* Value Transformations
4323 g_value_transforms_init ();
4329 G_UNLOCK (type_init_lock);
4335 * Prior to any use of the type system, g_type_init() has to be called
4336 * to initialize the type system and assorted other code portions
4337 * (such as the various fundamental type implementations or the signal
4340 * Since version 2.24 this also initializes the thread system
4345 g_type_init_with_debug_flags (0);
4349 * g_type_class_add_private:
4350 * @g_class: class structure for an instantiatable type
4351 * @private_size: size of private structure.
4353 * Registers a private structure for an instantiatable type;
4354 * when an object is allocated, the private structures for
4355 * the type and all of its parent types are allocated
4356 * sequentially in the same memory block as the public
4357 * structures. This function should be called in the
4358 * type's class_init() function. The private structure can
4359 * be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.
4360 * The following example shows attaching a private structure
4361 * <structname>MyObjectPrivate</structname> to an object
4362 * <structname>MyObject</structname> defined in the standard GObject
4366 * typedef struct _MyObjectPrivate MyObjectPrivate;
4368 * struct _MyObjectPrivate {
4372 * #define MY_OBJECT_GET_PRIVATE(o) \
4373 * (G_TYPE_INSTANCE_GET_PRIVATE ((o), MY_TYPE_OBJECT, MyObjectPrivate))
4376 * my_object_class_init (MyObjectClass *klass)
4378 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4382 * my_object_get_some_field (MyObject *my_object)
4384 * MyObjectPrivate *priv = MY_OBJECT_GET_PRIVATE (my_object);
4386 * return priv->some_field;
4393 g_type_class_add_private (gpointer g_class,
4396 GType instance_type = ((GTypeClass *)g_class)->g_type;
4397 TypeNode *node = lookup_type_node_I (instance_type);
4400 g_return_if_fail (private_size > 0);
4402 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4404 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4405 type_descriptive_name_I (instance_type));
4409 if (NODE_PARENT_TYPE (node))
4411 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4412 if (node->data->instance.private_size != pnode->data->instance.private_size)
4414 g_warning ("g_type_add_private() called multiple times for the same type");
4419 G_WRITE_LOCK (&type_rw_lock);
4421 offset = ALIGN_STRUCT (node->data->instance.private_size);
4422 node->data->instance.private_size = offset + private_size;
4424 G_WRITE_UNLOCK (&type_rw_lock);
4428 g_type_instance_get_private (GTypeInstance *instance,
4431 TypeNode *instance_node;
4432 TypeNode *private_node;
4433 TypeNode *parent_node;
4437 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4439 /* while instances are initialized, their class pointers change,
4440 * so figure the instances real class first
4442 class = instance_real_class_get (instance);
4444 class = instance->g_class;
4446 instance_node = lookup_type_node_I (class->g_type);
4447 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4449 g_warning ("instance of invalid non-instantiatable type `%s'",
4450 type_descriptive_name_I (instance->g_class->g_type));
4454 private_node = lookup_type_node_I (private_type);
4455 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4457 g_warning ("attempt to retrieve private data for invalid type '%s'",
4458 type_descriptive_name_I (private_type));
4462 /* Note that we don't need a read lock, since instance existing
4463 * means that the instance class and all parent classes
4464 * exist, so the node->data, node->data->instance.instance_size,
4465 * and node->data->instance.private_size are not going to be changed.
4466 * for any of the relevant types.
4469 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4471 if (NODE_PARENT_TYPE (private_node))
4473 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4474 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4476 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4478 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4482 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4485 return G_STRUCT_MEMBER_P (instance, offset);
4488 #define __G_TYPE_C__
4489 #include "gobjectaliasdef.c"