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 "gtypeplugin.h"
30 #include "gvaluecollector.h"
31 #include "gbsearcharray.h"
32 #include "gobjectalias.h"
33 #include "gatomicarray.h"
38 * @short_description: The GLib Runtime type identification and
40 * @title:Type Information
42 * The GType API is the foundation of the GObject system. It provides the
43 * facilities for registering and managing all fundamental data types,
44 * user-defined object and interface types. Before using any GType
45 * or GObject functions, g_type_init() must be called to initialize the
48 * For type creation and registration purposes, all types fall into one of
49 * two categories: static or dynamic. Static types are never loaded or
50 * unloaded at run-time as dynamic types may be. Static types are created
51 * with g_type_register_static() that gets type specific information passed
52 * in via a #GTypeInfo structure.
53 * Dynamic types are created with g_type_register_dynamic() which takes a
54 * #GTypePlugin structure instead. The remaining type information (the
55 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
56 * and the g_type_plugin_*() API.
57 * These registration functions are usually called only once from a
58 * function whose only purpose is to return the type identifier for a
59 * specific class. Once the type (or class or interface) is registered,
60 * it may be instantiated, inherited, or implemented depending on exactly
61 * what sort of type it is.
62 * There is also a third registration function for registering fundamental
63 * types called g_type_register_fundamental() which requires both a #GTypeInfo
64 * structure and a #GTypeFundamentalInfo structure but it is seldom used
65 * since most fundamental types are predefined rather than user-defined.
67 * A final word about type names.
68 * Such an identifier needs to be at least three characters long. There is no
69 * upper length limit. The first character needs to be a letter (a-z or A-Z)
70 * or an underscore '_'. Subsequent characters can be letters, numbers or
75 /* NOTE: some functions (some internal variants and exported ones)
76 * invalidate data portions of the TypeNodes. if external functions/callbacks
77 * are called, pointers to memory maintained by TypeNodes have to be looked up
78 * again. this affects most of the struct TypeNode fields, e.g. ->children or
79 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
80 * not ->supers[]), as all those memory portions can get realloc()ed during
81 * callback invocation.
84 * - g_type_from_name() should do an ordered array lookup after fetching the
85 * the quark, instead of a second hashtable lookup.
88 * lock handling issues when calling static functions are indicated by
89 * uppercase letter postfixes, all static functions have to have
90 * one of the below postfixes:
91 * - _I: [Indifferent about locking]
92 * function doesn't care about locks at all
93 * - _U: [Unlocked invocation]
94 * no read or write lock has to be held across function invocation
95 * (locks may be acquired and released during invocation though)
96 * - _L: [Locked invocation]
97 * a write lock or more than 0 read locks have to be held across
99 * - _W: [Write-locked invocation]
100 * a write lock has to be held across function invocation
101 * - _Wm: [Write-locked invocation, mutatable]
102 * like _W, but the write lock might be released and reacquired
103 * during invocation, watch your pointers
104 * - _WmREC: [Write-locked invocation, mutatable, recursive]
105 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
109 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
110 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
111 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
112 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
114 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
115 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
116 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
117 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
119 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
120 static const gchar _action[] = " invalidly modified type "; \
121 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
123 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
125 g_error ("%s()%s`%s'", _fname, _action, _tname); \
127 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
130 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
131 "%s: initialization assertion failed, use %s() prior to this function", \
132 G_STRLOC, G_STRINGIFY (init_function)); \
133 return (return_value); \
137 #ifdef G_ENABLE_DEBUG
138 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
139 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
142 #else /* !G_ENABLE_DEBUG */
143 #define DEBUG_CODE(debug_type, code_block) /* code_block */
144 #endif /* G_ENABLE_DEBUG */
146 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
147 G_TYPE_FLAG_INSTANTIATABLE | \
148 G_TYPE_FLAG_DERIVABLE | \
149 G_TYPE_FLAG_DEEP_DERIVABLE)
150 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
151 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
152 sizeof (gpointer)), \
155 /* The 2*sizeof(size_t) alignment here is borrowed from
156 * GNU libc, so it should be good most everywhere.
157 * It is more conservative than is needed on some 64-bit
158 * platforms, but ia64 does require a 16-byte alignment.
159 * The SIMD extensions for x86 and ppc32 would want a
160 * larger alignment than this, but we don't need to
161 * do better than malloc.
163 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
164 #define ALIGN_STRUCT(offset) \
165 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
168 /* --- typedefs --- */
169 typedef struct _TypeNode TypeNode;
170 typedef struct _CommonData CommonData;
171 typedef struct _IFaceData IFaceData;
172 typedef struct _ClassData ClassData;
173 typedef struct _InstanceData InstanceData;
174 typedef union _TypeData TypeData;
175 typedef struct _IFaceEntries IFaceEntries;
176 typedef struct _IFaceEntry IFaceEntry;
177 typedef struct _IFaceHolder IFaceHolder;
180 /* --- prototypes --- */
181 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
182 static void type_add_flags_W (TypeNode *node,
184 static void type_data_make_W (TypeNode *node,
185 const GTypeInfo *info,
186 const GTypeValueTable *value_table);
187 static inline void type_data_ref_Wm (TypeNode *node);
188 static inline void type_data_unref_WmREC (TypeNode *node,
190 static void type_data_last_unref_Wm (TypeNode * node,
192 static inline gpointer type_get_qdata_L (TypeNode *node,
194 static inline void type_set_qdata_W (TypeNode *node,
197 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
198 GType instance_type);
199 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
201 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
203 static gboolean type_node_is_a_L (TypeNode *node,
204 TypeNode *iface_node);
207 /* --- enumeration --- */
209 /* The InitState enumeration is used to track the progress of initializing
210 * both classes and interface vtables. Keeping the state of initialization
211 * is necessary to handle new interfaces being added while we are initializing
212 * the class or other interfaces.
224 /* --- structures --- */
227 guint volatile ref_count;
229 guint n_children; /* writable with lock */
231 guint _prot_n_prerequisites : 9;
232 guint is_classed : 1;
233 guint is_instantiatable : 1;
234 guint mutatable_check_cache : 1; /* combines some common path checks */
235 GType *children; /* writable with lock */
236 TypeData * volatile data;
240 GAtomicArray iface_entries; /* for !iface types */
241 GType *prerequisistes;
243 GType supers[1]; /* flexible array */
246 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
247 #define MAX_N_SUPERS (255)
248 #define MAX_N_CHILDREN (4095)
249 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
250 #define MAX_N_PREREQUISITES (511)
251 #define NODE_TYPE(node) (node->supers[0])
252 #define NODE_PARENT_TYPE(node) (node->supers[1])
253 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
254 #define NODE_NAME(node) (g_quark_to_string (node->qname))
255 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
256 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
257 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
258 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_prerequisites)
259 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
260 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
261 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
262 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
263 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
264 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
266 #define NODE_IS_ANCESTOR(ancestor, node) \
267 ((ancestor)->n_supers <= (node)->n_supers && \
268 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
273 GInterfaceInfo *info;
281 GTypeInterface *vtable;
282 InitState init_state;
285 struct _IFaceEntries {
290 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
291 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
295 GTypeValueTable *value_table;
302 GBaseInitFunc vtable_init_base;
303 GBaseFinalizeFunc vtable_finalize_base;
304 GClassInitFunc dflt_init;
305 GClassFinalizeFunc dflt_finalize;
306 gconstpointer dflt_data;
307 gpointer dflt_vtable;
308 GAtomicArray offsets;
315 guint init_state : 4;
316 GBaseInitFunc class_init_base;
317 GBaseFinalizeFunc class_finalize_base;
318 GClassInitFunc class_init;
319 GClassFinalizeFunc class_finalize;
320 gconstpointer class_data;
328 guint init_state : 4;
329 GBaseInitFunc class_init_base;
330 GBaseFinalizeFunc class_finalize_base;
331 GClassInitFunc class_init;
332 GClassFinalizeFunc class_finalize;
333 gconstpointer class_data;
335 guint16 instance_size;
336 guint16 private_size;
338 GInstanceInitFunc instance_init;
346 InstanceData instance;
351 GTypeClassCacheFunc cache_func;
356 GTypeInterfaceCheckFunc check_func;
360 /* --- variables --- */
361 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
362 static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
363 static guint static_n_class_cache_funcs = 0;
364 static ClassCacheFunc *static_class_cache_funcs = NULL;
365 static guint static_n_iface_check_funcs = 0;
366 static IFaceCheckFunc *static_iface_check_funcs = NULL;
367 static GQuark static_quark_type_flags = 0;
368 static GQuark static_quark_iface_holder = 0;
369 static GQuark static_quark_dependants_array = 0;
370 GTypeDebugFlags _g_type_debug_flags = 0;
372 /* --- type nodes --- */
373 static GHashTable *static_type_nodes_ht = NULL;
374 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
375 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
377 static inline TypeNode*
378 lookup_type_node_I (register GType utype)
380 if (utype > G_TYPE_FUNDAMENTAL_MAX)
381 return (TypeNode*) (utype & ~TYPE_ID_MASK);
383 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
387 type_node_any_new_W (TypeNode *pnode,
391 GTypeFundamentalFlags type_flags)
396 guint i, node_size = 0;
398 n_supers = pnode ? pnode->n_supers + 1 : 0;
401 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
402 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
403 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
404 node = g_malloc0 (node_size);
405 if (!pnode) /* offset fundamental types */
407 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
408 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
414 g_assert ((type & TYPE_ID_MASK) == 0);
416 node->n_supers = n_supers;
419 node->supers[0] = type;
422 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
423 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
425 if (NODE_IS_IFACE (node))
427 IFACE_NODE_N_PREREQUISITES (node) = 0;
428 IFACE_NODE_PREREQUISITES (node) = NULL;
431 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
435 node->supers[0] = type;
436 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
438 node->is_classed = pnode->is_classed;
439 node->is_instantiatable = pnode->is_instantiatable;
441 if (NODE_IS_IFACE (node))
443 IFACE_NODE_N_PREREQUISITES (node) = 0;
444 IFACE_NODE_PREREQUISITES (node) = NULL;
449 IFaceEntries *entries;
451 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
452 IFACE_ENTRIES_HEADER_SIZE,
456 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
458 entries->entry[j].vtable = NULL;
459 entries->entry[j].init_state = UNINITIALIZED;
461 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
466 i = pnode->n_children++;
467 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
468 pnode->children[i] = type;
471 node->plugin = plugin;
472 node->n_children = 0;
473 node->children = NULL;
475 node->qname = g_quark_from_string (name);
476 node->global_gdata = NULL;
478 g_hash_table_insert (static_type_nodes_ht,
479 GUINT_TO_POINTER (node->qname),
484 static inline GTypeFundamentalInfo*
485 type_node_fundamental_info_I (TypeNode *node)
487 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
489 if (ftype != NODE_TYPE (node))
490 node = lookup_type_node_I (ftype);
492 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
496 type_node_fundamental_new_W (GType ftype,
498 GTypeFundamentalFlags type_flags)
500 GTypeFundamentalInfo *finfo;
503 g_assert ((ftype & TYPE_ID_MASK) == 0);
504 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
506 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
507 static_fundamental_next++;
509 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
511 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
513 finfo = type_node_fundamental_info_I (node);
514 finfo->type_flags = type_flags;
520 type_node_new_W (TypeNode *pnode,
526 g_assert (pnode->n_supers < MAX_N_SUPERS);
527 g_assert (pnode->n_children < MAX_N_CHILDREN);
529 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
532 static inline IFaceEntry*
533 lookup_iface_entry_I (volatile IFaceEntries *entries,
534 TypeNode *iface_node)
545 G_ATOMIC_ARRAY_DO_TRANSACTION
546 (&iface_node->data->iface.offsets, guint8,
549 offsets = transaction_data;
550 offset_index = entries->offset_index;
551 if (offsets != NULL &&
552 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
554 index = offsets[offset_index];
557 /* zero means unset, subtract one to get real index */
560 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
562 check = (IFaceEntry *)&entries->entry[index];
563 if (check->iface_type == NODE_TYPE (iface_node))
573 static inline IFaceEntry*
574 type_lookup_iface_entry_L (TypeNode *node,
575 TypeNode *iface_node)
577 if (!NODE_IS_IFACE (iface_node))
580 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
585 static inline gboolean
586 type_lookup_iface_vtable_I (TypeNode *node,
587 TypeNode *iface_node,
588 gpointer *vtable_ptr)
593 if (!NODE_IS_IFACE (iface_node))
600 G_ATOMIC_ARRAY_DO_TRANSACTION
601 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
603 entry = lookup_iface_entry_I (transaction_data, iface_node);
608 *vtable_ptr = entry->vtable;
617 static inline gboolean
618 type_lookup_prerequisite_L (TypeNode *iface,
619 GType prerequisite_type)
621 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
623 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
624 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
631 i = (n_prerequisites + 1) >> 1;
632 check = prerequisites + i;
633 if (prerequisite_type == *check)
635 else if (prerequisite_type > *check)
637 n_prerequisites -= i;
638 prerequisites = check;
640 else /* if (prerequisite_type < *check) */
641 n_prerequisites = i - 1;
643 while (n_prerequisites);
649 type_descriptive_name_I (GType type)
653 TypeNode *node = lookup_type_node_I (type);
655 return node ? NODE_NAME (node) : "<unknown>";
662 /* --- type consistency checks --- */
664 check_plugin_U (GTypePlugin *plugin,
665 gboolean need_complete_type_info,
666 gboolean need_complete_interface_info,
667 const gchar *type_name)
669 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
673 g_warning ("plugin handle for type `%s' is NULL",
677 if (!G_IS_TYPE_PLUGIN (plugin))
679 g_warning ("plugin pointer (%p) for type `%s' is invalid",
683 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
685 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
689 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
691 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
699 check_type_name_I (const gchar *type_name)
701 static const gchar extra_chars[] = "-_+";
702 const gchar *p = type_name;
705 if (!type_name[0] || !type_name[1] || !type_name[2])
707 g_warning ("type name `%s' is too short", type_name);
710 /* check the first letter */
711 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
712 for (p = type_name + 1; *p; p++)
713 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
714 (p[0] >= 'a' && p[0] <= 'z') ||
715 (p[0] >= '0' && p[0] <= '9') ||
716 strchr (extra_chars, p[0]));
719 g_warning ("type name `%s' contains invalid characters", type_name);
722 if (g_type_from_name (type_name))
724 g_warning ("cannot register existing type `%s'", type_name);
732 check_derivation_I (GType parent_type,
733 const gchar *type_name)
736 GTypeFundamentalInfo* finfo;
738 pnode = lookup_type_node_I (parent_type);
741 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
743 type_descriptive_name_I (parent_type));
746 finfo = type_node_fundamental_info_I (pnode);
747 /* ensure flat derivability */
748 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
750 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
755 /* ensure deep derivability */
756 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
757 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
759 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
769 check_collect_format_I (const gchar *collect_format)
771 const gchar *p = collect_format;
772 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
773 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
774 G_VALUE_COLLECT_POINTER, 0 };
777 if (!strchr (valid_format, *p++))
779 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
783 check_value_table_I (const gchar *type_name,
784 const GTypeValueTable *value_table)
788 else if (value_table->value_init == NULL)
790 if (value_table->value_free || value_table->value_copy ||
791 value_table->value_peek_pointer ||
792 value_table->collect_format || value_table->collect_value ||
793 value_table->lcopy_format || value_table->lcopy_value)
794 g_warning ("cannot handle uninitializable values of type `%s'",
798 else /* value_table->value_init != NULL */
800 if (!value_table->value_free)
803 * g_warning ("missing `value_free()' for type `%s'", type_name);
807 if (!value_table->value_copy)
809 g_warning ("missing `value_copy()' for type `%s'", type_name);
812 if ((value_table->collect_format || value_table->collect_value) &&
813 (!value_table->collect_format || !value_table->collect_value))
815 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
819 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
821 g_warning ("the `%s' specification for type `%s' is too long or invalid",
826 if ((value_table->lcopy_format || value_table->lcopy_value) &&
827 (!value_table->lcopy_format || !value_table->lcopy_value))
829 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
833 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
835 g_warning ("the `%s' specification for type `%s' is too long or invalid",
845 check_type_info_I (TypeNode *pnode,
847 const gchar *type_name,
848 const GTypeInfo *info)
850 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
851 gboolean is_interface = ftype == G_TYPE_INTERFACE;
853 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
855 /* check instance members */
856 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
857 (info->instance_size || info->n_preallocs || info->instance_init))
860 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
864 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
868 /* check class & interface members */
869 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
870 (info->class_init || info->class_finalize || info->class_data ||
871 info->class_size || info->base_init || info->base_finalize))
874 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
878 g_warning ("cannot create class for `%s' as non-classed fundamental",
882 /* check interface size */
883 if (is_interface && info->class_size < sizeof (GTypeInterface))
885 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
889 /* check class size */
890 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
892 if (info->class_size < sizeof (GTypeClass))
894 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
898 if (pnode && info->class_size < pnode->data->class.class_size)
900 g_warning ("specified class size for type `%s' is smaller "
901 "than the parent type's `%s' class size",
907 /* check instance size */
908 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
910 if (info->instance_size < sizeof (GTypeInstance))
912 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
916 if (pnode && info->instance_size < pnode->data->instance.instance_size)
918 g_warning ("specified instance size for type `%s' is smaller "
919 "than the parent type's `%s' instance size",
930 find_conforming_child_type_L (TypeNode *pnode,
933 TypeNode *node = NULL;
936 if (type_lookup_iface_entry_L (pnode, iface))
939 for (i = 0; i < pnode->n_children && !node; i++)
940 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
946 check_add_interface_L (GType instance_type,
949 TypeNode *node = lookup_type_node_I (instance_type);
950 TypeNode *iface = lookup_type_node_I (iface_type);
953 GType *prerequisites;
957 if (!node || !node->is_instantiatable)
959 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
960 type_descriptive_name_I (instance_type));
963 if (!iface || !NODE_IS_IFACE (iface))
965 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
966 type_descriptive_name_I (iface_type),
970 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
971 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
973 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
974 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
980 /* allow overriding of interface type introduced for parent type */
981 entry = type_lookup_iface_entry_L (node, iface);
982 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
984 /* ok, we do conform to this interface already, but the interface vtable was not
985 * yet intialized, and we just conform to the interface because it got added to
986 * one of our parents. so we allow overriding of holder info here.
990 /* check whether one of our children already conforms (or whether the interface
991 * got added to this node already)
993 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
996 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
1002 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1003 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1005 tnode = lookup_type_node_I (prerequisites[i]);
1006 if (!type_node_is_a_L (node, tnode))
1008 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
1019 check_interface_info_I (TypeNode *iface,
1020 GType instance_type,
1021 const GInterfaceInfo *info)
1023 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1025 g_warning ("interface type `%s' for type `%s' comes without initializer",
1027 type_descriptive_name_I (instance_type));
1034 /* --- type info (type node data) --- */
1036 type_data_make_W (TypeNode *node,
1037 const GTypeInfo *info,
1038 const GTypeValueTable *value_table)
1041 GTypeValueTable *vtable = NULL;
1042 guint vtable_size = 0;
1044 g_assert (node->data == NULL && info != NULL);
1048 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1051 vtable = pnode->data->common.value_table;
1054 static const GTypeValueTable zero_vtable = { NULL, };
1056 value_table = &zero_vtable;
1061 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1062 vtable_size = sizeof (GTypeValueTable);
1063 if (value_table->collect_format)
1064 vtable_size += strlen (value_table->collect_format);
1065 if (value_table->lcopy_format)
1066 vtable_size += strlen (value_table->lcopy_format);
1070 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
1072 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1074 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1075 data->instance.class_size = info->class_size;
1076 data->instance.class_init_base = info->base_init;
1077 data->instance.class_finalize_base = info->base_finalize;
1078 data->instance.class_init = info->class_init;
1079 data->instance.class_finalize = info->class_finalize;
1080 data->instance.class_data = info->class_data;
1081 data->instance.class = NULL;
1082 data->instance.init_state = UNINITIALIZED;
1083 data->instance.instance_size = info->instance_size;
1084 /* We'll set the final value for data->instance.private size
1085 * after the parent class has been initialized
1087 data->instance.private_size = 0;
1088 #ifdef DISABLE_MEM_POOLS
1089 data->instance.n_preallocs = 0;
1090 #else /* !DISABLE_MEM_POOLS */
1091 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1092 #endif /* !DISABLE_MEM_POOLS */
1093 data->instance.instance_init = info->instance_init;
1095 else if (node->is_classed) /* only classed */
1097 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1099 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1100 data->class.class_size = info->class_size;
1101 data->class.class_init_base = info->base_init;
1102 data->class.class_finalize_base = info->base_finalize;
1103 data->class.class_init = info->class_init;
1104 data->class.class_finalize = info->class_finalize;
1105 data->class.class_data = info->class_data;
1106 data->class.class = NULL;
1107 data->class.init_state = UNINITIALIZED;
1109 else if (NODE_IS_IFACE (node))
1111 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1113 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1114 data->iface.vtable_size = info->class_size;
1115 data->iface.vtable_init_base = info->base_init;
1116 data->iface.vtable_finalize_base = info->base_finalize;
1117 data->iface.dflt_init = info->class_init;
1118 data->iface.dflt_finalize = info->class_finalize;
1119 data->iface.dflt_data = info->class_data;
1120 data->iface.dflt_vtable = NULL;
1124 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1126 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1130 node->ref_count = 1;
1136 /* we allocate the vtable and its strings together with the type data, so
1137 * children can take over their parent's vtable pointer, and we don't
1138 * need to worry freeing it or not when the child data is destroyed
1140 *vtable = *value_table;
1141 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1143 vtable->collect_format = p;
1144 if (value_table->collect_format)
1146 strcat (p, value_table->collect_format);
1147 p += strlen (value_table->collect_format);
1151 vtable->lcopy_format = p;
1152 if (value_table->lcopy_format)
1153 strcat (p, value_table->lcopy_format);
1155 node->data->common.value_table = vtable;
1156 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1157 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1158 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1160 g_assert (node->data->common.value_table != NULL); /* paranoid */
1164 type_data_ref_Wm (TypeNode *node)
1168 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1170 GTypeValueTable tmp_value_table;
1172 g_assert (node->plugin != NULL);
1176 type_data_ref_Wm (pnode);
1178 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1181 memset (&tmp_info, 0, sizeof (tmp_info));
1182 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1184 G_WRITE_UNLOCK (&type_rw_lock);
1185 g_type_plugin_use (node->plugin);
1186 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1187 G_WRITE_LOCK (&type_rw_lock);
1189 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1191 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1192 type_data_make_W (node, &tmp_info,
1193 check_value_table_I (NODE_NAME (node),
1194 &tmp_value_table) ? &tmp_value_table : NULL);
1198 g_assert (node->ref_count > 0);
1200 node->ref_count += 1;
1205 iface_node_has_available_offset_L (TypeNode *iface_node,
1211 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->data->iface.offsets, guint8);
1212 if (offsets == NULL)
1215 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1218 if (offsets[offset] == 0 ||
1219 offsets[offset] == for_index+1)
1226 find_free_iface_offset_L (IFaceEntries *entries)
1229 TypeNode *iface_node;
1234 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1239 for (i = 0; i < n_entries; i++)
1241 entry = &entries->entry[i];
1242 iface_node = lookup_type_node_I (entry->iface_type);
1244 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1248 while (i != n_entries);
1254 iface_node_set_offset_L (TypeNode *iface_node,
1258 guint8 *offsets, *old_offsets;
1259 int new_size, old_size;
1262 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->data->iface.offsets, guint8);
1263 if (old_offsets == NULL)
1267 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1268 if (offset < old_size &&
1269 old_offsets[offset] == index + 1)
1270 return; /* Already set to this index, return */
1272 new_size = MAX (old_size, offset + 1);
1274 offsets = _g_atomic_array_copy (&iface_node->data->iface.offsets,
1275 0, new_size - old_size);
1277 /* Mark new area as unused */
1278 for (i = old_size; i < new_size; i++)
1281 offsets[offset] = index + 1;
1283 _g_atomic_array_update (&iface_node->data->iface.offsets, offsets);
1287 type_node_add_iface_entry_W (TypeNode *node,
1289 IFaceEntry *parent_entry)
1291 IFaceEntries *entries;
1293 TypeNode *iface_node;
1297 g_assert (node->is_instantiatable);
1299 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1300 if (entries != NULL)
1302 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1304 g_assert (num_entries < MAX_N_INTERFACES);
1306 for (i = 0; i < num_entries; i++)
1308 entry = &entries->entry[i];
1309 if (entry->iface_type == iface_type)
1311 /* this can happen in two cases:
1312 * - our parent type already conformed to iface_type and node
1313 * got its own holder info. here, our children already have
1314 * entries and NULL vtables, since this will only work for
1315 * uninitialized classes.
1316 * - an interface type is added to an ancestor after it was
1317 * added to a child type.
1320 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1323 /* sick, interface is added to ancestor *after* child type;
1324 * nothing todo, the entry and our children were already setup correctly
1332 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1333 IFACE_ENTRIES_HEADER_SIZE,
1334 sizeof (IFaceEntry));
1335 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1336 i = num_entries - 1;
1338 entries->offset_index = 0;
1339 entries->entry[i].iface_type = iface_type;
1340 entries->entry[i].vtable = NULL;
1341 entries->entry[i].init_state = UNINITIALIZED;
1345 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1347 entries->entry[i].init_state = INITIALIZED;
1348 entries->entry[i].vtable = parent_entry->vtable;
1352 /* Update offsets in iface */
1353 iface_node = lookup_type_node_I (iface_type);
1355 if (iface_node_has_available_offset_L (iface_node,
1356 entries->offset_index,
1359 iface_node_set_offset_L (iface_node,
1360 entries->offset_index, i);
1364 entries->offset_index =
1365 find_free_iface_offset_L (entries);
1366 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1368 entry = &entries->entry[j];
1370 lookup_type_node_I (entry->iface_type);
1371 iface_node_set_offset_L (iface_node,
1372 entries->offset_index, j);
1376 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1380 for (i = 0; i < node->n_children; i++)
1381 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1386 type_add_interface_Wm (TypeNode *node,
1388 const GInterfaceInfo *info,
1389 GTypePlugin *plugin)
1391 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1395 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1397 iholder->next = iface_node_get_holders_L (iface);
1398 iface_node_set_holders_W (iface, iholder);
1399 iholder->instance_type = NODE_TYPE (node);
1400 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1401 iholder->plugin = plugin;
1403 /* create an iface entry for this type */
1404 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1406 /* if the class is already (partly) initialized, we may need to base
1407 * initalize and/or initialize the new interface.
1411 InitState class_state = node->data->class.init_state;
1413 if (class_state >= BASE_IFACE_INIT)
1414 type_iface_vtable_base_init_Wm (iface, node);
1416 if (class_state >= IFACE_INIT)
1417 type_iface_vtable_iface_init_Wm (iface, node);
1420 /* create iface entries for children of this type */
1421 entry = type_lookup_iface_entry_L (node, iface);
1422 for (i = 0; i < node->n_children; i++)
1423 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1427 type_iface_add_prerequisite_W (TypeNode *iface,
1428 TypeNode *prerequisite_node)
1430 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1431 GType *prerequisites, *dependants;
1432 guint n_dependants, i;
1434 g_assert (NODE_IS_IFACE (iface) &&
1435 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1436 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1438 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1439 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1440 if (prerequisites[i] == prerequisite_type)
1441 return; /* we already have that prerequisiste */
1442 else if (prerequisites[i] > prerequisite_type)
1444 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1445 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1446 IFACE_NODE_PREREQUISITES (iface),
1447 IFACE_NODE_N_PREREQUISITES (iface));
1448 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1449 g_memmove (prerequisites + i + 1, prerequisites + i,
1450 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1451 prerequisites[i] = prerequisite_type;
1453 /* we want to get notified when prerequisites get added to prerequisite_node */
1454 if (NODE_IS_IFACE (prerequisite_node))
1456 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1457 n_dependants = dependants ? dependants[0] : 0;
1459 dependants = g_renew (GType, dependants, n_dependants + 1);
1460 dependants[n_dependants] = NODE_TYPE (iface);
1461 dependants[0] = n_dependants;
1462 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1465 /* we need to notify all dependants */
1466 dependants = iface_node_get_dependants_array_L (iface);
1467 n_dependants = dependants ? dependants[0] : 0;
1468 for (i = 1; i <= n_dependants; i++)
1469 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1473 * g_type_interface_add_prerequisite:
1474 * @interface_type: #GType value of an interface type.
1475 * @prerequisite_type: #GType value of an interface or instantiatable type.
1477 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1478 * This means that any type implementing @interface_type must also implement
1479 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1480 * interface derivation (which GType doesn't support). An interface can have
1481 * at most one instantiatable prerequisite type.
1484 g_type_interface_add_prerequisite (GType interface_type,
1485 GType prerequisite_type)
1487 TypeNode *iface, *prerequisite_node;
1488 IFaceHolder *holders;
1490 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1491 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1492 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1494 iface = lookup_type_node_I (interface_type);
1495 prerequisite_node = lookup_type_node_I (prerequisite_type);
1496 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1498 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1499 type_descriptive_name_I (interface_type),
1500 type_descriptive_name_I (prerequisite_type));
1503 G_WRITE_LOCK (&type_rw_lock);
1504 holders = iface_node_get_holders_L (iface);
1507 G_WRITE_UNLOCK (&type_rw_lock);
1508 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1509 type_descriptive_name_I (prerequisite_type),
1510 type_descriptive_name_I (interface_type),
1511 type_descriptive_name_I (holders->instance_type));
1514 if (prerequisite_node->is_instantiatable)
1518 /* can have at most one publically installable instantiatable prerequisite */
1519 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1521 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1523 if (prnode->is_instantiatable)
1525 G_WRITE_UNLOCK (&type_rw_lock);
1526 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1527 type_descriptive_name_I (prerequisite_type),
1528 type_descriptive_name_I (interface_type),
1529 type_descriptive_name_I (NODE_TYPE (prnode)));
1534 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1535 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1536 G_WRITE_UNLOCK (&type_rw_lock);
1538 else if (NODE_IS_IFACE (prerequisite_node))
1540 GType *prerequisites;
1543 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1544 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1545 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1546 type_iface_add_prerequisite_W (iface, prerequisite_node);
1547 G_WRITE_UNLOCK (&type_rw_lock);
1551 G_WRITE_UNLOCK (&type_rw_lock);
1552 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1553 type_descriptive_name_I (prerequisite_type),
1554 type_descriptive_name_I (interface_type));
1559 * g_type_interface_prerequisites:
1560 * @interface_type: an interface type
1561 * @n_prerequisites: location to return the number of prerequisites, or %NULL
1563 * Returns the prerequisites of an interfaces type.
1567 * Returns: a newly-allocated zero-terminated array of #GType containing
1568 * the prerequisites of @interface_type
1571 g_type_interface_prerequisites (GType interface_type,
1572 guint *n_prerequisites)
1576 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1578 iface = lookup_type_node_I (interface_type);
1582 TypeNode *inode = NULL;
1585 G_READ_LOCK (&type_rw_lock);
1586 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1587 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1589 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1590 TypeNode *node = lookup_type_node_I (prerequisite);
1591 if (node->is_instantiatable &&
1592 (!inode || type_node_is_a_L (node, inode)))
1595 types[n++] = NODE_TYPE (node);
1598 types[n++] = NODE_TYPE (inode);
1600 if (n_prerequisites)
1601 *n_prerequisites = n;
1602 G_READ_UNLOCK (&type_rw_lock);
1608 if (n_prerequisites)
1609 *n_prerequisites = 0;
1617 type_iface_peek_holder_L (TypeNode *iface,
1618 GType instance_type)
1620 IFaceHolder *iholder;
1622 g_assert (NODE_IS_IFACE (iface));
1624 iholder = iface_node_get_holders_L (iface);
1625 while (iholder && iholder->instance_type != instance_type)
1626 iholder = iholder->next;
1631 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1632 GType instance_type,
1635 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1637 if (iholder && !iholder->info && need_info)
1639 GInterfaceInfo tmp_info;
1641 g_assert (iholder->plugin != NULL);
1643 type_data_ref_Wm (iface);
1645 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1647 memset (&tmp_info, 0, sizeof (tmp_info));
1649 G_WRITE_UNLOCK (&type_rw_lock);
1650 g_type_plugin_use (iholder->plugin);
1651 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1652 G_WRITE_LOCK (&type_rw_lock);
1654 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1656 check_interface_info_I (iface, instance_type, &tmp_info);
1657 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1660 return iholder; /* we don't modify write lock upon returning NULL */
1664 type_iface_blow_holder_info_Wm (TypeNode *iface,
1665 GType instance_type)
1667 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1669 g_assert (NODE_IS_IFACE (iface));
1671 while (iholder->instance_type != instance_type)
1672 iholder = iholder->next;
1674 if (iholder->info && iholder->plugin)
1676 g_free (iholder->info);
1677 iholder->info = NULL;
1679 G_WRITE_UNLOCK (&type_rw_lock);
1680 g_type_plugin_unuse (iholder->plugin);
1681 G_WRITE_LOCK (&type_rw_lock);
1683 type_data_unref_WmREC (iface, FALSE);
1687 /* Assumes type's class already exists
1689 static inline size_t
1690 type_total_instance_size_I (TypeNode *node)
1692 gsize total_instance_size;
1694 total_instance_size = node->data->instance.instance_size;
1695 if (node->data->instance.private_size != 0)
1696 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1698 return total_instance_size;
1701 /* --- type structure creation/destruction --- */
1705 } InstanceRealClass;
1708 instance_real_class_cmp (gconstpointer p1,
1711 const InstanceRealClass *irc1 = p1;
1712 const InstanceRealClass *irc2 = p2;
1713 guint8 *i1 = irc1->instance;
1714 guint8 *i2 = irc2->instance;
1715 return G_BSEARCH_ARRAY_CMP (i1, i2);
1718 G_LOCK_DEFINE_STATIC (instance_real_class);
1719 static GBSearchArray *instance_real_class_bsa = NULL;
1720 static GBSearchConfig instance_real_class_bconfig = {
1721 sizeof (InstanceRealClass),
1722 instance_real_class_cmp,
1727 instance_real_class_set (gpointer instance,
1730 InstanceRealClass key;
1731 key.instance = instance;
1733 G_LOCK (instance_real_class);
1734 if (!instance_real_class_bsa)
1735 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1736 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1737 G_UNLOCK (instance_real_class);
1741 instance_real_class_remove (gpointer instance)
1743 InstanceRealClass key, *node;
1745 key.instance = instance;
1746 G_LOCK (instance_real_class);
1747 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1748 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1749 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1750 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1752 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1753 instance_real_class_bsa = NULL;
1755 G_UNLOCK (instance_real_class);
1758 static inline GTypeClass*
1759 instance_real_class_get (gpointer instance)
1761 InstanceRealClass key, *node;
1763 key.instance = instance;
1764 G_LOCK (instance_real_class);
1765 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1766 class = node ? node->class : NULL;
1767 G_UNLOCK (instance_real_class);
1772 * g_type_create_instance:
1773 * @type: An instantiatable type to create an instance for.
1775 * Creates and initializes an instance of @type if @type is valid and
1776 * can be instantiated. The type system only performs basic allocation
1777 * and structure setups for instances: actual instance creation should
1778 * happen through functions supplied by the type's fundamental type
1779 * implementation. So use of g_type_create_instance() is reserved for
1780 * implementators of fundamental types only. E.g. instances of the
1781 * #GObject hierarchy should be created via g_object_new() and
1782 * <emphasis>never</emphasis> directly through
1783 * g_type_create_instance() which doesn't handle things like singleton
1784 * objects or object construction. Note: Do <emphasis>not</emphasis>
1785 * use this function, unless you're implementing a fundamental
1786 * type. Also language bindings should <emphasis>not</emphasis> use
1787 * this function but g_object_new() instead.
1789 * Returns: An allocated and initialized instance, subject to further
1790 * treatment by the fundamental type implementation.
1793 g_type_create_instance (GType type)
1796 GTypeInstance *instance;
1798 guint i, total_size;
1800 node = lookup_type_node_I (type);
1801 if (!node || !node->is_instantiatable)
1803 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1804 type_descriptive_name_I (type));
1807 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1808 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1810 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1811 type_descriptive_name_I (type));
1815 class = g_type_class_ref (type);
1816 total_size = type_total_instance_size_I (node);
1818 instance = g_slice_alloc0 (total_size);
1820 if (node->data->instance.private_size)
1821 instance_real_class_set (instance, class);
1822 for (i = node->n_supers; i > 0; i--)
1826 pnode = lookup_type_node_I (node->supers[i]);
1827 if (pnode->data->instance.instance_init)
1829 instance->g_class = pnode->data->instance.class;
1830 pnode->data->instance.instance_init (instance, class);
1833 if (node->data->instance.private_size)
1834 instance_real_class_remove (instance);
1836 instance->g_class = class;
1837 if (node->data->instance.instance_init)
1838 node->data->instance.instance_init (instance, class);
1844 * g_type_free_instance:
1845 * @instance: an instance of a type.
1847 * Frees an instance of a type, returning it to the instance pool for
1848 * the type, if there is one.
1850 * Like g_type_create_instance(), this function is reserved for
1851 * implementors of fundamental types.
1854 g_type_free_instance (GTypeInstance *instance)
1859 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1861 class = instance->g_class;
1862 node = lookup_type_node_I (class->g_type);
1863 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1865 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1866 type_descriptive_name_I (class->g_type));
1869 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1870 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1872 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1877 instance->g_class = NULL;
1878 #ifdef G_ENABLE_DEBUG
1879 memset (instance, 0xaa, type_total_instance_size_I (node));
1881 g_slice_free1 (type_total_instance_size_I (node), instance);
1883 g_type_class_unref (class);
1887 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1889 g_assert (iface->data);
1891 if (!iface->data->iface.dflt_vtable)
1893 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1894 iface->data->iface.dflt_vtable = vtable;
1895 vtable->g_type = NODE_TYPE (iface);
1896 vtable->g_instance_type = 0;
1897 if (iface->data->iface.vtable_init_base ||
1898 iface->data->iface.dflt_init)
1900 G_WRITE_UNLOCK (&type_rw_lock);
1901 if (iface->data->iface.vtable_init_base)
1902 iface->data->iface.vtable_init_base (vtable);
1903 if (iface->data->iface.dflt_init)
1904 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1905 G_WRITE_LOCK (&type_rw_lock);
1911 /* This is called to allocate and do the first part of initializing
1912 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1914 * A FALSE return indicates that we didn't find an init function for
1915 * this type/iface pair, so the vtable from the parent type should
1916 * be used. Note that the write lock is not modified upon a FALSE
1920 type_iface_vtable_base_init_Wm (TypeNode *iface,
1924 IFaceHolder *iholder;
1925 GTypeInterface *vtable = NULL;
1928 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1929 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1931 return FALSE; /* we don't modify write lock upon FALSE */
1933 type_iface_ensure_dflt_vtable_Wm (iface);
1935 entry = type_lookup_iface_entry_L (node, iface);
1937 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1939 entry->init_state = IFACE_INIT;
1941 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1942 if (pnode) /* want to copy over parent iface contents */
1944 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1947 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1950 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1951 entry->vtable = vtable;
1952 vtable->g_type = NODE_TYPE (iface);
1953 vtable->g_instance_type = NODE_TYPE (node);
1955 if (iface->data->iface.vtable_init_base)
1957 G_WRITE_UNLOCK (&type_rw_lock);
1958 iface->data->iface.vtable_init_base (vtable);
1959 G_WRITE_LOCK (&type_rw_lock);
1961 return TRUE; /* initialized the vtable */
1964 /* Finishes what type_iface_vtable_base_init_Wm started by
1965 * calling the interface init function.
1966 * this function may only be called for types with their
1967 * own interface holder info, i.e. types for which
1968 * g_type_add_interface*() was called and not children thereof.
1971 type_iface_vtable_iface_init_Wm (TypeNode *iface,
1974 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1975 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
1976 GTypeInterface *vtable = NULL;
1979 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
1980 g_assert (iface->data && entry && iholder && iholder->info);
1981 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
1983 entry->init_state = INITIALIZED;
1985 vtable = entry->vtable;
1987 if (iholder->info->interface_init)
1989 G_WRITE_UNLOCK (&type_rw_lock);
1990 if (iholder->info->interface_init)
1991 iholder->info->interface_init (vtable, iholder->info->interface_data);
1992 G_WRITE_LOCK (&type_rw_lock);
1995 for (i = 0; i < static_n_iface_check_funcs; i++)
1997 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
1998 gpointer check_data = static_iface_check_funcs[i].check_data;
2000 G_WRITE_UNLOCK (&type_rw_lock);
2001 check_func (check_data, (gpointer)vtable);
2002 G_WRITE_LOCK (&type_rw_lock);
2007 type_iface_vtable_finalize_Wm (TypeNode *iface,
2009 GTypeInterface *vtable)
2011 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2012 IFaceHolder *iholder;
2014 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2015 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2017 return FALSE; /* we don't modify write lock upon FALSE */
2019 g_assert (entry && entry->vtable == vtable && iholder->info);
2021 entry->vtable = NULL;
2022 entry->init_state = UNINITIALIZED;
2023 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2025 G_WRITE_UNLOCK (&type_rw_lock);
2026 if (iholder->info->interface_finalize)
2027 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2028 if (iface->data->iface.vtable_finalize_base)
2029 iface->data->iface.vtable_finalize_base (vtable);
2030 G_WRITE_LOCK (&type_rw_lock);
2033 vtable->g_instance_type = 0;
2036 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2038 return TRUE; /* write lock modified */
2042 type_class_init_Wm (TypeNode *node,
2045 GSList *slist, *init_slist = NULL;
2047 IFaceEntries *entries;
2049 TypeNode *bnode, *pnode;
2052 g_assert (node->is_classed && node->data &&
2053 node->data->class.class_size &&
2054 !node->data->class.class &&
2055 node->data->class.init_state == UNINITIALIZED);
2057 class = g_malloc0 (node->data->class.class_size);
2058 node->data->class.class = class;
2059 node->data->class.init_state = BASE_CLASS_INIT;
2063 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2065 memcpy (class, pclass, pnode->data->class.class_size);
2067 if (node->is_instantiatable)
2069 /* We need to initialize the private_size here rather than in
2070 * type_data_make_W() since the class init for the parent
2071 * class may have changed pnode->data->instance.private_size.
2073 node->data->instance.private_size = pnode->data->instance.private_size;
2076 class->g_type = NODE_TYPE (node);
2078 G_WRITE_UNLOCK (&type_rw_lock);
2080 /* stack all base class initialization functions, so we
2081 * call them in ascending order.
2083 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2084 if (bnode->data->class.class_init_base)
2085 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2086 for (slist = init_slist; slist; slist = slist->next)
2088 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2090 class_init_base (class);
2092 g_slist_free (init_slist);
2094 G_WRITE_LOCK (&type_rw_lock);
2096 node->data->class.init_state = BASE_IFACE_INIT;
2098 /* Before we initialize the class, base initialize all interfaces, either
2099 * from parent, or through our holder info
2101 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2104 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2105 i < IFACE_ENTRIES_N_ENTRIES (entries))
2107 entry = &entries->entry[i];
2108 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2109 entry->init_state == IFACE_INIT)
2115 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2118 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2121 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2123 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2124 * doesn't modify write lock upon FALSE, so entry is still valid;
2126 g_assert (pnode != NULL);
2129 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2131 IFaceEntry *pentry = &pentries->entry[j];
2133 if (pentry->iface_type == entry->iface_type)
2135 entry->vtable = pentry->vtable;
2136 entry->init_state = INITIALIZED;
2140 g_assert (entry->vtable != NULL);
2143 /* If the write lock was released, additional interface entries might
2144 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2145 * be base-initialized when inserted, so we don't have to worry that
2146 * we might miss them. Uninitialized entries can only be moved higher
2147 * when new ones are inserted.
2152 node->data->class.init_state = CLASS_INIT;
2154 G_WRITE_UNLOCK (&type_rw_lock);
2156 if (node->data->class.class_init)
2157 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2159 G_WRITE_LOCK (&type_rw_lock);
2161 node->data->class.init_state = IFACE_INIT;
2163 /* finish initializing the interfaces through our holder info.
2164 * inherited interfaces are already init_state == INITIALIZED, because
2165 * they either got setup in the above base_init loop, or during
2166 * class_init from within type_add_interface_Wm() for this or
2167 * an anchestor type.
2170 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2172 entry = &entries->entry[i];
2173 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2174 entry->init_state == INITIALIZED)
2180 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2183 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2185 /* As in the loop above, additional initialized entries might be inserted
2186 * if the write lock is released, but that's harmless because the entries
2187 * we need to initialize only move higher in the list.
2192 node->data->class.init_state = INITIALIZED;
2196 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2199 IFaceEntries *entries;
2201 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->ref_count == 0);
2204 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2205 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2207 IFaceEntry *entry = &entries->entry[i];
2210 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2212 /* refetch entries, IFACES_ENTRIES might be modified */
2217 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2218 * iface vtable came from parent
2220 entry->vtable = NULL;
2221 entry->init_state = UNINITIALIZED;
2228 type_data_finalize_class_U (TypeNode *node,
2231 GTypeClass *class = cdata->class;
2234 g_assert (cdata->class && node->ref_count == 0);
2236 if (cdata->class_finalize)
2237 cdata->class_finalize (class, (gpointer) cdata->class_data);
2239 /* call all base class destruction functions in descending order
2241 if (cdata->class_finalize_base)
2242 cdata->class_finalize_base (class);
2243 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2244 if (bnode->data->class.class_finalize_base)
2245 bnode->data->class.class_finalize_base (class);
2247 g_free (cdata->class);
2251 type_data_last_unref_Wm (TypeNode *node,
2254 g_return_if_fail (node != NULL && node->plugin != NULL);
2256 if (!node->data || node->ref_count == 0)
2258 g_warning ("cannot drop last reference to unreferenced type `%s'",
2263 /* call class cache hooks */
2264 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2268 G_WRITE_UNLOCK (&type_rw_lock);
2269 G_READ_LOCK (&type_rw_lock);
2270 for (i = 0; i < static_n_class_cache_funcs; i++)
2272 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2273 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2274 gboolean need_break;
2276 G_READ_UNLOCK (&type_rw_lock);
2277 need_break = cache_func (cache_data, node->data->class.class);
2278 G_READ_LOCK (&type_rw_lock);
2279 if (!node->data || node->ref_count == 0)
2280 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2284 G_READ_UNLOCK (&type_rw_lock);
2285 G_WRITE_LOCK (&type_rw_lock);
2288 if (node->ref_count > 1) /* may have been re-referenced meanwhile */
2289 node->ref_count -= 1;
2292 GType ptype = NODE_PARENT_TYPE (node);
2295 node->ref_count = 0;
2297 if (node->is_instantiatable)
2299 /* destroy node->data->instance.mem_chunk */
2303 if (node->is_classed && tdata->class.class)
2305 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2306 type_data_finalize_class_ifaces_Wm (node);
2307 node->mutatable_check_cache = FALSE;
2309 G_WRITE_UNLOCK (&type_rw_lock);
2310 type_data_finalize_class_U (node, &tdata->class);
2311 G_WRITE_LOCK (&type_rw_lock);
2313 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2315 node->mutatable_check_cache = FALSE;
2317 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2319 G_WRITE_UNLOCK (&type_rw_lock);
2320 if (tdata->iface.dflt_finalize)
2321 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2322 if (tdata->iface.vtable_finalize_base)
2323 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2324 G_WRITE_LOCK (&type_rw_lock);
2326 g_free (tdata->iface.dflt_vtable);
2330 node->mutatable_check_cache = FALSE;
2334 /* freeing tdata->common.value_table and its contents is taken care of
2335 * by allocating it in one chunk with tdata
2339 G_WRITE_UNLOCK (&type_rw_lock);
2340 g_type_plugin_unuse (node->plugin);
2341 G_WRITE_LOCK (&type_rw_lock);
2343 type_data_unref_WmREC (lookup_type_node_I (ptype), FALSE);
2348 type_data_unref_WmREC (TypeNode *node,
2351 g_assert (node->data && node->ref_count);
2352 if (node->ref_count > 1)
2353 node->ref_count -= 1;
2358 g_warning ("static type `%s' unreferenced too often",
2362 G_WRITE_UNLOCK (&type_rw_lock);
2363 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2364 G_WRITE_LOCK (&type_rw_lock);
2365 type_data_last_unref_Wm (node, uncached);
2366 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2371 * g_type_add_class_cache_func:
2372 * @cache_data: data to be passed to @cache_func
2373 * @cache_func: a #GTypeClassCacheFunc
2375 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2376 * class goes from one to zero. This can be used to prevent premature class
2377 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2378 * until one of them returns %TRUE. The functions have to check the class id
2379 * passed in to figure whether they actually want to cache the class of this
2380 * type, since all classes are routed through the same #GTypeClassCacheFunc
2384 g_type_add_class_cache_func (gpointer cache_data,
2385 GTypeClassCacheFunc cache_func)
2389 g_return_if_fail (cache_func != NULL);
2391 G_WRITE_LOCK (&type_rw_lock);
2392 i = static_n_class_cache_funcs++;
2393 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2394 static_class_cache_funcs[i].cache_data = cache_data;
2395 static_class_cache_funcs[i].cache_func = cache_func;
2396 G_WRITE_UNLOCK (&type_rw_lock);
2400 * g_type_remove_class_cache_func:
2401 * @cache_data: data that was given when adding @cache_func
2402 * @cache_func: a #GTypeClassCacheFunc
2404 * Removes a previously installed #GTypeClassCacheFunc. The cache
2405 * maintained by @cache_func has to be empty when calling
2406 * g_type_remove_class_cache_func() to avoid leaks.
2409 g_type_remove_class_cache_func (gpointer cache_data,
2410 GTypeClassCacheFunc cache_func)
2412 gboolean found_it = FALSE;
2415 g_return_if_fail (cache_func != NULL);
2417 G_WRITE_LOCK (&type_rw_lock);
2418 for (i = 0; i < static_n_class_cache_funcs; i++)
2419 if (static_class_cache_funcs[i].cache_data == cache_data &&
2420 static_class_cache_funcs[i].cache_func == cache_func)
2422 static_n_class_cache_funcs--;
2423 g_memmove (static_class_cache_funcs + i,
2424 static_class_cache_funcs + i + 1,
2425 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2426 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2430 G_WRITE_UNLOCK (&type_rw_lock);
2433 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2434 cache_func, cache_data);
2439 * g_type_add_interface_check:
2440 * @check_data: data to pass to @check_func
2441 * @check_func: function to be called after each interface
2444 * Adds a function to be called after an interface vtable is
2445 * initialized for any class (i.e. after the @interface_init member of
2446 * #GInterfaceInfo has been called).
2448 * This function is useful when you want to check an invariant that
2449 * depends on the interfaces of a class. For instance, the
2450 * implementation of #GObject uses this facility to check that an
2451 * object implements all of the properties that are defined on its
2457 g_type_add_interface_check (gpointer check_data,
2458 GTypeInterfaceCheckFunc check_func)
2462 g_return_if_fail (check_func != NULL);
2464 G_WRITE_LOCK (&type_rw_lock);
2465 i = static_n_iface_check_funcs++;
2466 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2467 static_iface_check_funcs[i].check_data = check_data;
2468 static_iface_check_funcs[i].check_func = check_func;
2469 G_WRITE_UNLOCK (&type_rw_lock);
2473 * g_type_remove_interface_check:
2474 * @check_data: callback data passed to g_type_add_interface_check()
2475 * @check_func: callback function passed to g_type_add_interface_check()
2477 * Removes an interface check function added with
2478 * g_type_add_interface_check().
2483 g_type_remove_interface_check (gpointer check_data,
2484 GTypeInterfaceCheckFunc check_func)
2486 gboolean found_it = FALSE;
2489 g_return_if_fail (check_func != NULL);
2491 G_WRITE_LOCK (&type_rw_lock);
2492 for (i = 0; i < static_n_iface_check_funcs; i++)
2493 if (static_iface_check_funcs[i].check_data == check_data &&
2494 static_iface_check_funcs[i].check_func == check_func)
2496 static_n_iface_check_funcs--;
2497 g_memmove (static_iface_check_funcs + i,
2498 static_iface_check_funcs + i + 1,
2499 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2500 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2504 G_WRITE_UNLOCK (&type_rw_lock);
2507 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2508 check_func, check_data);
2511 /* --- type registration --- */
2513 * g_type_register_fundamental:
2514 * @type_id: A predefined type identifier.
2515 * @type_name: 0-terminated string used as the name of the new type.
2516 * @info: The #GTypeInfo structure for this type.
2517 * @finfo: The #GTypeFundamentalInfo structure for this type.
2518 * @flags: Bitwise combination of #GTypeFlags values.
2520 * Registers @type_id as the predefined identifier and @type_name as the
2521 * name of a fundamental type. The type system uses the information
2522 * contained in the #GTypeInfo structure pointed to by @info and the
2523 * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
2524 * type and its instances. The value of @flags determines additional
2525 * characteristics of the fundamental type.
2527 * Returns: The predefined type identifier.
2530 g_type_register_fundamental (GType type_id,
2531 const gchar *type_name,
2532 const GTypeInfo *info,
2533 const GTypeFundamentalInfo *finfo,
2538 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2539 g_return_val_if_fail (type_id > 0, 0);
2540 g_return_val_if_fail (type_name != NULL, 0);
2541 g_return_val_if_fail (info != NULL, 0);
2542 g_return_val_if_fail (finfo != NULL, 0);
2544 if (!check_type_name_I (type_name))
2546 if ((type_id & TYPE_ID_MASK) ||
2547 type_id > G_TYPE_FUNDAMENTAL_MAX)
2549 g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2554 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2555 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2557 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2561 if (lookup_type_node_I (type_id))
2563 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2564 type_descriptive_name_I (type_id),
2569 G_WRITE_LOCK (&type_rw_lock);
2570 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2571 type_add_flags_W (node, flags);
2573 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2574 type_data_make_W (node, info,
2575 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2576 G_WRITE_UNLOCK (&type_rw_lock);
2578 return NODE_TYPE (node);
2582 * g_type_register_static_simple:
2583 * @parent_type: Type from which this type will be derived.
2584 * @type_name: 0-terminated string used as the name of the new type.
2585 * @class_size: Size of the class structure (see #GTypeInfo)
2586 * @class_init: Location of the class initialization function (see #GTypeInfo)
2587 * @instance_size: Size of the instance structure (see #GTypeInfo)
2588 * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2589 * @flags: Bitwise combination of #GTypeFlags values.
2591 * Registers @type_name as the name of a new static type derived from
2592 * @parent_type. The value of @flags determines the nature (e.g.
2593 * abstract or not) of the type. It works by filling a #GTypeInfo
2594 * struct and calling g_type_register_static().
2598 * Returns: The new type identifier.
2601 g_type_register_static_simple (GType parent_type,
2602 const gchar *type_name,
2604 GClassInitFunc class_init,
2605 guint instance_size,
2606 GInstanceInitFunc instance_init,
2611 info.class_size = class_size;
2612 info.base_init = NULL;
2613 info.base_finalize = NULL;
2614 info.class_init = class_init;
2615 info.class_finalize = NULL;
2616 info.class_data = NULL;
2617 info.instance_size = instance_size;
2618 info.n_preallocs = 0;
2619 info.instance_init = instance_init;
2620 info.value_table = NULL;
2622 return g_type_register_static (parent_type, type_name, &info, flags);
2626 * g_type_register_static:
2627 * @parent_type: Type from which this type will be derived.
2628 * @type_name: 0-terminated string used as the name of the new type.
2629 * @info: The #GTypeInfo structure for this type.
2630 * @flags: Bitwise combination of #GTypeFlags values.
2632 * Registers @type_name as the name of a new static type derived from
2633 * @parent_type. The type system uses the information contained in the
2634 * #GTypeInfo structure pointed to by @info to manage the type and its
2635 * instances (if not abstract). The value of @flags determines the nature
2636 * (e.g. abstract or not) of the type.
2638 * Returns: The new type identifier.
2641 g_type_register_static (GType parent_type,
2642 const gchar *type_name,
2643 const GTypeInfo *info,
2646 TypeNode *pnode, *node;
2649 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2650 g_return_val_if_fail (parent_type > 0, 0);
2651 g_return_val_if_fail (type_name != NULL, 0);
2652 g_return_val_if_fail (info != NULL, 0);
2654 if (!check_type_name_I (type_name) ||
2655 !check_derivation_I (parent_type, type_name))
2657 if (info->class_finalize)
2659 g_warning ("class finalizer specified for static type `%s'",
2664 pnode = lookup_type_node_I (parent_type);
2665 G_WRITE_LOCK (&type_rw_lock);
2666 type_data_ref_Wm (pnode);
2667 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2669 node = type_node_new_W (pnode, type_name, NULL);
2670 type_add_flags_W (node, flags);
2671 type = NODE_TYPE (node);
2672 type_data_make_W (node, info,
2673 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2675 G_WRITE_UNLOCK (&type_rw_lock);
2681 * g_type_register_dynamic:
2682 * @parent_type: Type from which this type will be derived.
2683 * @type_name: 0-terminated string used as the name of the new type.
2684 * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2685 * @flags: Bitwise combination of #GTypeFlags values.
2687 * Registers @type_name as the name of a new dynamic type derived from
2688 * @parent_type. The type system uses the information contained in the
2689 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2690 * instances (if not abstract). The value of @flags determines the nature
2691 * (e.g. abstract or not) of the type.
2693 * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2696 g_type_register_dynamic (GType parent_type,
2697 const gchar *type_name,
2698 GTypePlugin *plugin,
2701 TypeNode *pnode, *node;
2704 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2705 g_return_val_if_fail (parent_type > 0, 0);
2706 g_return_val_if_fail (type_name != NULL, 0);
2707 g_return_val_if_fail (plugin != NULL, 0);
2709 if (!check_type_name_I (type_name) ||
2710 !check_derivation_I (parent_type, type_name) ||
2711 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2714 G_WRITE_LOCK (&type_rw_lock);
2715 pnode = lookup_type_node_I (parent_type);
2716 node = type_node_new_W (pnode, type_name, plugin);
2717 type_add_flags_W (node, flags);
2718 type = NODE_TYPE (node);
2719 G_WRITE_UNLOCK (&type_rw_lock);
2725 * g_type_add_interface_static:
2726 * @instance_type: #GType value of an instantiable type.
2727 * @interface_type: #GType value of an interface type.
2728 * @info: The #GInterfaceInfo structure for this
2729 * (@instance_type, @interface_type) combination.
2731 * Adds the static @interface_type to @instantiable_type. The information
2732 * contained in the #GTypeInterfaceInfo structure pointed to by @info
2733 * is used to manage the relationship.
2736 g_type_add_interface_static (GType instance_type,
2737 GType interface_type,
2738 const GInterfaceInfo *info)
2740 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2741 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2742 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2744 /* we only need to lock class_init_rec_mutex if instance_type already has its
2745 * class initialized, however this function is rarely enough called to take
2746 * the simple route and always acquire class_init_rec_mutex.
2748 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2749 G_WRITE_LOCK (&type_rw_lock);
2750 if (check_add_interface_L (instance_type, interface_type))
2752 TypeNode *node = lookup_type_node_I (instance_type);
2753 TypeNode *iface = lookup_type_node_I (interface_type);
2754 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2755 type_add_interface_Wm (node, iface, info, NULL);
2757 G_WRITE_UNLOCK (&type_rw_lock);
2758 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2762 * g_type_add_interface_dynamic:
2763 * @instance_type: the #GType value of an instantiable type.
2764 * @interface_type: the #GType value of an interface type.
2765 * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2767 * Adds the dynamic @interface_type to @instantiable_type. The information
2768 * contained in the #GTypePlugin structure pointed to by @plugin
2769 * is used to manage the relationship.
2772 g_type_add_interface_dynamic (GType instance_type,
2773 GType interface_type,
2774 GTypePlugin *plugin)
2777 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2778 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2779 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2781 node = lookup_type_node_I (instance_type);
2782 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2785 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2786 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2787 G_WRITE_LOCK (&type_rw_lock);
2788 if (check_add_interface_L (instance_type, interface_type))
2790 TypeNode *iface = lookup_type_node_I (interface_type);
2791 type_add_interface_Wm (node, iface, NULL, plugin);
2793 G_WRITE_UNLOCK (&type_rw_lock);
2794 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2798 /* --- public API functions --- */
2801 * @type: Type ID of a classed type.
2803 * Increments the reference count of the class structure belonging to
2804 * @type. This function will demand-create the class if it doesn't
2807 * Returns: The #GTypeClass structure for the given type ID.
2810 g_type_class_ref (GType type)
2815 /* optimize for common code path */
2816 G_WRITE_LOCK (&type_rw_lock);
2817 node = lookup_type_node_I (type);
2818 if (node && node->is_classed && node->data &&
2819 node->data->class.class &&
2820 node->data->class.init_state == INITIALIZED)
2822 type_data_ref_Wm (node);
2823 G_WRITE_UNLOCK (&type_rw_lock);
2824 return node->data->class.class;
2826 if (!node || !node->is_classed ||
2827 (node->data && node->ref_count < 1))
2829 G_WRITE_UNLOCK (&type_rw_lock);
2830 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2831 type_descriptive_name_I (type));
2834 type_data_ref_Wm (node);
2835 ptype = NODE_PARENT_TYPE (node);
2836 G_WRITE_UNLOCK (&type_rw_lock);
2838 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2839 /* here, we either have node->data->class.class == NULL, or a recursive
2840 * call to g_type_class_ref() with a partly initialized class, or
2841 * node->data->class.init_state == INITIALIZED, because any
2842 * concurrently running initialization was guarded by class_init_rec_mutex.
2844 if (!node->data->class.class) /* class uninitialized */
2846 /* we need an initialized parent class for initializing derived classes */
2847 GTypeClass *pclass = ptype ? g_type_class_ref (ptype) : NULL;
2848 G_WRITE_LOCK (&type_rw_lock);
2849 if (node->data->class.class) /* class was initialized during parent class initialization? */
2850 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2851 type_class_init_Wm (node, pclass);
2852 G_WRITE_UNLOCK (&type_rw_lock);
2854 g_type_class_unref (pclass);
2856 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2858 return node->data->class.class;
2862 * g_type_class_unref:
2863 * @g_class: The #GTypeClass structure to unreference.
2865 * Decrements the reference count of the class structure being passed in.
2866 * Once the last reference count of a class has been released, classes
2867 * may be finalized by the type system, so further dereferencing of a
2868 * class pointer after g_type_class_unref() are invalid.
2871 g_type_class_unref (gpointer g_class)
2874 GTypeClass *class = g_class;
2876 g_return_if_fail (g_class != NULL);
2878 node = lookup_type_node_I (class->g_type);
2879 G_WRITE_LOCK (&type_rw_lock);
2880 if (node && node->is_classed && node->data &&
2881 node->data->class.class == class && node->ref_count > 0)
2882 type_data_unref_WmREC (node, FALSE);
2884 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2885 type_descriptive_name_I (class->g_type));
2886 G_WRITE_UNLOCK (&type_rw_lock);
2890 * g_type_class_unref_uncached:
2891 * @g_class: The #GTypeClass structure to unreference.
2893 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2894 * implementations. It unreferences a class without consulting the chain
2895 * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2899 g_type_class_unref_uncached (gpointer g_class)
2902 GTypeClass *class = g_class;
2904 g_return_if_fail (g_class != NULL);
2906 G_WRITE_LOCK (&type_rw_lock);
2907 node = lookup_type_node_I (class->g_type);
2908 if (node && node->is_classed && node->data &&
2909 node->data->class.class == class && node->ref_count > 0)
2910 type_data_unref_WmREC (node, TRUE);
2912 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2913 type_descriptive_name_I (class->g_type));
2914 G_WRITE_UNLOCK (&type_rw_lock);
2918 * g_type_class_peek:
2919 * @type: Type ID of a classed type.
2921 * This function is essentially the same as g_type_class_ref(), except that
2922 * the classes reference count isn't incremented. As a consequence, this function
2923 * may return %NULL if the class of the type passed in does not currently
2924 * exist (hasn't been referenced before).
2926 * Returns: The #GTypeClass structure for the given type ID or %NULL
2927 * if the class does not currently exist.
2930 g_type_class_peek (GType type)
2935 node = lookup_type_node_I (type);
2936 G_READ_LOCK (&type_rw_lock);
2937 if (node && node->is_classed && node->data && node->data->class.class) /* ref_count _may_ be 0 */
2938 class = node->data->class.class;
2941 G_READ_UNLOCK (&type_rw_lock);
2947 * g_type_class_peek_static:
2948 * @type: Type ID of a classed type.
2950 * A more efficient version of g_type_class_peek() which works only for
2954 * Returns: The #GTypeClass structure for the given type ID or %NULL
2955 * if the class does not currently exist or is dynamically loaded.
2958 g_type_class_peek_static (GType type)
2963 node = lookup_type_node_I (type);
2964 G_READ_LOCK (&type_rw_lock);
2965 if (node && node->is_classed && node->data &&
2966 /* peek only static types: */ node->plugin == NULL &&
2967 node->data->class.class) /* ref_count _may_ be 0 */
2968 class = node->data->class.class;
2971 G_READ_UNLOCK (&type_rw_lock);
2977 * g_type_class_peek_parent:
2978 * @g_class: The #GTypeClass structure to retrieve the parent class for.
2980 * This is a convenience function often needed in class initializers.
2981 * It returns the class structure of the immediate parent type of the
2982 * class passed in. Since derived classes hold a reference count on
2983 * their parent classes as long as they are instantiated, the returned
2984 * class will always exist. This function is essentially equivalent
2988 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
2991 * Returns: The parent class of @g_class.
2994 g_type_class_peek_parent (gpointer g_class)
2997 gpointer class = NULL;
2999 g_return_val_if_fail (g_class != NULL, NULL);
3001 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3002 /* We used to acquire a read lock here. That is not necessary, since
3003 * parent->data->class.class is constant as long as the derived class
3006 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3008 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3009 class = node->data->class.class;
3011 else if (NODE_PARENT_TYPE (node))
3012 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
3018 * g_type_interface_peek:
3019 * @instance_class: A #GTypeClass structure.
3020 * @iface_type: An interface ID which this class conforms to.
3022 * Returns the #GTypeInterface structure of an interface to which the
3023 * passed in class conforms.
3025 * Returns: The GTypeInterface structure of iface_type if implemented
3026 * by @instance_class, %NULL otherwise
3029 g_type_interface_peek (gpointer instance_class,
3034 gpointer vtable = NULL;
3035 GTypeClass *class = instance_class;
3037 g_return_val_if_fail (instance_class != NULL, NULL);
3039 node = lookup_type_node_I (class->g_type);
3040 iface = lookup_type_node_I (iface_type);
3041 if (node && node->is_instantiatable && iface)
3042 type_lookup_iface_vtable_I (node, iface, &vtable);
3044 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
3050 * g_type_interface_peek_parent:
3051 * @g_iface: A #GTypeInterface structure.
3053 * Returns the corresponding #GTypeInterface structure of the parent type
3054 * of the instance type to which @g_iface belongs. This is useful when
3055 * deriving the implementation of an interface from the parent type and
3056 * then possibly overriding some methods.
3058 * Returns: The corresponding #GTypeInterface structure of the parent
3059 * type of the instance type to which @g_iface belongs, or
3060 * %NULL if the parent type doesn't conform to the interface.
3063 g_type_interface_peek_parent (gpointer g_iface)
3067 gpointer vtable = NULL;
3068 GTypeInterface *iface_class = g_iface;
3070 g_return_val_if_fail (g_iface != NULL, NULL);
3072 iface = lookup_type_node_I (iface_class->g_type);
3073 node = lookup_type_node_I (iface_class->g_instance_type);
3075 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3076 if (node && node->is_instantiatable && iface)
3077 type_lookup_iface_vtable_I (node, iface, &vtable);
3079 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
3085 * g_type_default_interface_ref:
3086 * @g_type: an interface type
3088 * Increments the reference count for the interface type @g_type,
3089 * and returns the default interface vtable for the type.
3091 * If the type is not currently in use, then the default vtable
3092 * for the type will be created and initalized by calling
3093 * the base interface init and default vtable init functions for
3094 * the type (the @<structfield>base_init</structfield>
3095 * and <structfield>class_init</structfield> members of #GTypeInfo).
3096 * Calling g_type_default_interface_ref() is useful when you
3097 * want to make sure that signals and properties for an interface
3098 * have been installed.
3102 * Returns: the default vtable for the interface; call
3103 * g_type_default_interface_unref() when you are done using
3107 g_type_default_interface_ref (GType g_type)
3110 gpointer dflt_vtable;
3112 G_WRITE_LOCK (&type_rw_lock);
3114 node = lookup_type_node_I (g_type);
3115 if (!node || !NODE_IS_IFACE (node) ||
3116 (node->data && node->ref_count < 1))
3118 G_WRITE_UNLOCK (&type_rw_lock);
3119 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3120 type_descriptive_name_I (g_type));
3124 if (!node->data || !node->data->iface.dflt_vtable)
3126 G_WRITE_UNLOCK (&type_rw_lock);
3127 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3128 G_WRITE_LOCK (&type_rw_lock);
3129 node = lookup_type_node_I (g_type);
3130 type_data_ref_Wm (node);
3131 type_iface_ensure_dflt_vtable_Wm (node);
3132 g_static_rec_mutex_unlock (&class_init_rec_mutex);
3135 type_data_ref_Wm (node); /* ref_count >= 1 already */
3137 dflt_vtable = node->data->iface.dflt_vtable;
3138 G_WRITE_UNLOCK (&type_rw_lock);
3144 * g_type_default_interface_peek:
3145 * @g_type: an interface type
3147 * If the interface type @g_type is currently in use, returns its
3148 * default interface vtable.
3152 * Returns: the default vtable for the interface, or %NULL
3153 * if the type is not currently in use.
3156 g_type_default_interface_peek (GType g_type)
3161 node = lookup_type_node_I (g_type);
3162 G_READ_LOCK (&type_rw_lock);
3163 if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
3164 vtable = node->data->iface.dflt_vtable;
3167 G_READ_UNLOCK (&type_rw_lock);
3173 * g_type_default_interface_unref:
3174 * @g_iface: the default vtable structure for a interface, as
3175 * returned by g_type_default_interface_ref()
3177 * Decrements the reference count for the type corresponding to the
3178 * interface default vtable @g_iface. If the type is dynamic, then
3179 * when no one is using the interface and all references have
3180 * been released, the finalize function for the interface's default
3181 * vtable (the <structfield>class_finalize</structfield> member of
3182 * #GTypeInfo) will be called.
3187 g_type_default_interface_unref (gpointer g_iface)
3190 GTypeInterface *vtable = g_iface;
3192 g_return_if_fail (g_iface != NULL);
3194 node = lookup_type_node_I (vtable->g_type);
3195 G_WRITE_LOCK (&type_rw_lock);
3196 if (node && NODE_IS_IFACE (node) &&
3197 node->data->iface.dflt_vtable == g_iface &&
3198 node->ref_count > 0)
3199 type_data_unref_WmREC (node, FALSE);
3201 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3202 type_descriptive_name_I (vtable->g_type));
3203 G_WRITE_UNLOCK (&type_rw_lock);
3208 * @type: Type to return name for.
3210 * Get the unique name that is assigned to a type ID. Note that this
3211 * function (like all other GType API) cannot cope with invalid type
3212 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3213 * other validly registered type ID, but randomized type IDs should
3214 * not be passed in and will most likely lead to a crash.
3216 * Returns: Static type name or %NULL.
3218 G_CONST_RETURN gchar*
3219 g_type_name (GType type)
3223 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
3225 node = lookup_type_node_I (type);
3227 return node ? NODE_NAME (node) : NULL;
3232 * @type: Type to return quark of type name for.
3234 * Get the corresponding quark of the type IDs name.
3236 * Returns: The type names quark or 0.
3239 g_type_qname (GType type)
3243 node = lookup_type_node_I (type);
3245 return node ? node->qname : 0;
3250 * @name: Type name to lookup.
3252 * Lookup the type ID from a given type name, returning 0 if no type
3253 * has been registered under this name (this is the preferred method
3254 * to find out by name whether a specific type has been registered
3257 * Returns: Corresponding type ID or 0.
3260 g_type_from_name (const gchar *name)
3265 g_return_val_if_fail (name != NULL, 0);
3267 quark = g_quark_try_string (name);
3270 G_READ_LOCK (&type_rw_lock);
3271 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
3272 G_READ_UNLOCK (&type_rw_lock);
3280 * @type: The derived type.
3282 * Return the direct parent type of the passed in type. If the passed
3283 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3285 * Returns: The parent type.
3288 g_type_parent (GType type)
3292 node = lookup_type_node_I (type);
3294 return node ? NODE_PARENT_TYPE (node) : 0;
3299 * @type: A #GType value.
3301 * Returns the length of the ancestry of the passed in type. This
3302 * includes the type itself, so that e.g. a fundamental type has depth 1.
3304 * Returns: The depth of @type.
3307 g_type_depth (GType type)
3311 node = lookup_type_node_I (type);
3313 return node ? node->n_supers + 1 : 0;
3318 * @leaf_type: Descendant of @root_type and the type to be returned.
3319 * @root_type: Immediate parent of the returned type.
3321 * Given a @leaf_type and a @root_type which is contained in its
3322 * anchestry, return the type that @root_type is the immediate parent
3323 * of. In other words, this function determines the type that is
3324 * derived directly from @root_type which is also a base class of
3325 * @leaf_type. Given a root type and a leaf type, this function can
3326 * be used to determine the types and order in which the leaf type is
3327 * descended from the root type.
3329 * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3332 g_type_next_base (GType type,
3338 node = lookup_type_node_I (type);
3341 TypeNode *base_node = lookup_type_node_I (base_type);
3343 if (base_node && base_node->n_supers < node->n_supers)
3345 guint n = node->n_supers - base_node->n_supers;
3347 if (node->supers[n] == base_type)
3348 atype = node->supers[n - 1];
3355 static inline gboolean
3356 type_node_check_conformities_UorL (TypeNode *node,
3357 TypeNode *iface_node,
3358 /* support_inheritance */
3359 gboolean support_interfaces,
3360 gboolean support_prerequisites,
3365 if (/* support_inheritance && */
3366 NODE_IS_ANCESTOR (iface_node, node))
3369 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3370 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3372 if (support_interfaces)
3376 if (type_lookup_iface_entry_L (node, iface_node))
3381 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3386 support_prerequisites)
3389 G_READ_LOCK (&type_rw_lock);
3390 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3393 G_READ_UNLOCK (&type_rw_lock);
3399 type_node_is_a_L (TypeNode *node,
3400 TypeNode *iface_node)
3402 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3405 static inline gboolean
3406 type_node_conforms_to_U (TypeNode *node,
3407 TypeNode *iface_node,
3408 gboolean support_interfaces,
3409 gboolean support_prerequisites)
3411 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3416 * @type: Type to check anchestry for.
3417 * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3419 * If @is_a_type is a derivable type, check whether @type is a
3420 * descendant of @is_a_type. If @is_a_type is an interface, check
3421 * whether @type conforms to it.
3423 * Returns: %TRUE if @type is_a @is_a_type holds true.
3426 g_type_is_a (GType type,
3429 TypeNode *node, *iface_node;
3432 node = lookup_type_node_I (type);
3433 iface_node = lookup_type_node_I (iface_type);
3434 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3441 * @type: The parent type.
3442 * @n_children: Optional #guint pointer to contain the number of child types.
3444 * Return a newly allocated and 0-terminated array of type IDs, listing the
3445 * child types of @type. The return value has to be g_free()ed after use.
3447 * Returns: Newly allocated and 0-terminated array of child types.
3450 g_type_children (GType type,
3455 node = lookup_type_node_I (type);
3460 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3461 children = g_new (GType, node->n_children + 1);
3462 memcpy (children, node->children, sizeof (GType) * node->n_children);
3463 children[node->n_children] = 0;
3466 *n_children = node->n_children;
3467 G_READ_UNLOCK (&type_rw_lock);
3481 * g_type_interfaces:
3482 * @type: The type to list interface types for.
3483 * @n_interfaces: Optional #guint pointer to contain the number of
3486 * Return a newly allocated and 0-terminated array of type IDs, listing the
3487 * interface types that @type conforms to. The return value has to be
3488 * g_free()ed after use.
3490 * Returns: Newly allocated and 0-terminated array of interface types.
3493 g_type_interfaces (GType type,
3494 guint *n_interfaces)
3498 node = lookup_type_node_I (type);
3499 if (node && node->is_instantiatable)
3501 IFaceEntries *entries;
3505 G_READ_LOCK (&type_rw_lock);
3506 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3509 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3510 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3511 ifaces[i] = entries->entry[i].iface_type;
3515 ifaces = g_new (GType, 1);
3522 G_READ_UNLOCK (&type_rw_lock);
3535 typedef struct _QData QData;
3547 static inline gpointer
3548 type_get_qdata_L (TypeNode *node,
3551 GData *gdata = node->global_gdata;
3553 if (quark && gdata && gdata->n_qdatas)
3555 QData *qdatas = gdata->qdatas - 1;
3556 guint n_qdatas = gdata->n_qdatas;
3563 i = (n_qdatas + 1) / 2;
3565 if (quark == check->quark)
3567 else if (quark > check->quark)
3572 else /* if (quark < check->quark) */
3583 * @quark: a #GQuark id to identify the data
3585 * Obtains data which has previously been attached to @type
3586 * with g_type_set_qdata().
3588 * Returns: the data, or %NULL if no data was found
3591 g_type_get_qdata (GType type,
3597 node = lookup_type_node_I (type);
3600 G_READ_LOCK (&type_rw_lock);
3601 data = type_get_qdata_L (node, quark);
3602 G_READ_UNLOCK (&type_rw_lock);
3606 g_return_val_if_fail (node != NULL, NULL);
3613 type_set_qdata_W (TypeNode *node,
3621 /* setup qdata list if necessary */
3622 if (!node->global_gdata)
3623 node->global_gdata = g_new0 (GData, 1);
3624 gdata = node->global_gdata;
3626 /* try resetting old data */
3627 qdata = gdata->qdatas;
3628 for (i = 0; i < gdata->n_qdatas; i++)
3629 if (qdata[i].quark == quark)
3631 qdata[i].data = data;
3637 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3638 qdata = gdata->qdatas;
3639 for (i = 0; i < gdata->n_qdatas - 1; i++)
3640 if (qdata[i].quark > quark)
3642 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3643 qdata[i].quark = quark;
3644 qdata[i].data = data;
3650 * @quark: a #GQuark id to identify the data
3653 * Attaches arbitrary data to a type.
3656 g_type_set_qdata (GType type,
3662 g_return_if_fail (quark != 0);
3664 node = lookup_type_node_I (type);
3667 G_WRITE_LOCK (&type_rw_lock);
3668 type_set_qdata_W (node, quark, data);
3669 G_WRITE_UNLOCK (&type_rw_lock);
3672 g_return_if_fail (node != NULL);
3676 type_add_flags_W (TypeNode *node,
3681 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3682 g_return_if_fail (node != NULL);
3684 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3685 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3686 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3688 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3693 * @type: the #GType value of a static, classed type.
3694 * @query: A user provided structure that is filled in with constant values
3697 * Queries the type system for information about a specific type.
3698 * This function will fill in a user-provided structure to hold
3699 * type-specific information. If an invalid #GType is passed in, the
3700 * @type member of the #GTypeQuery is 0. All members filled into the
3701 * #GTypeQuery structure should be considered constant and have to be
3705 g_type_query (GType type,
3710 g_return_if_fail (query != NULL);
3712 /* if node is not static and classed, we won't allow query */
3714 node = lookup_type_node_I (type);
3715 if (node && node->is_classed && !node->plugin)
3717 /* type is classed and probably even instantiatable */
3718 G_READ_LOCK (&type_rw_lock);
3719 if (node->data) /* type is static or referenced */
3721 query->type = NODE_TYPE (node);
3722 query->type_name = NODE_NAME (node);
3723 query->class_size = node->data->class.class_size;
3724 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3726 G_READ_UNLOCK (&type_rw_lock);
3731 /* --- implementation details --- */
3733 g_type_test_flags (GType type,
3737 gboolean result = FALSE;
3739 node = lookup_type_node_I (type);
3742 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3743 guint tflags = flags & TYPE_FLAG_MASK;
3747 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3749 fflags = (finfo->type_flags & fflags) == fflags;
3756 G_READ_LOCK (&type_rw_lock);
3757 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3758 G_READ_UNLOCK (&type_rw_lock);
3763 result = tflags && fflags;
3770 * g_type_get_plugin:
3771 * @type: The #GType to retrieve the plugin for.
3773 * Returns the #GTypePlugin structure for @type or
3774 * %NULL if @type does not have a #GTypePlugin structure.
3776 * Returns: The corresponding plugin if @type is a dynamic type,
3780 g_type_get_plugin (GType type)
3784 node = lookup_type_node_I (type);
3786 return node ? node->plugin : NULL;
3790 * g_type_interface_get_plugin:
3791 * @instance_type: the #GType value of an instantiatable type.
3792 * @interface_type: the #GType value of an interface type.
3794 * Returns the #GTypePlugin structure for the dynamic interface
3795 * @interface_type which has been added to @instance_type, or %NULL if
3796 * @interface_type has not been added to @instance_type or does not
3797 * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3799 * Returns: the #GTypePlugin for the dynamic interface @interface_type
3800 * of @instance_type.
3803 g_type_interface_get_plugin (GType instance_type,
3804 GType interface_type)
3809 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3811 node = lookup_type_node_I (instance_type);
3812 iface = lookup_type_node_I (interface_type);
3815 IFaceHolder *iholder;
3816 GTypePlugin *plugin;
3818 G_READ_LOCK (&type_rw_lock);
3820 iholder = iface_node_get_holders_L (iface);
3821 while (iholder && iholder->instance_type != instance_type)
3822 iholder = iholder->next;
3823 plugin = iholder ? iholder->plugin : NULL;
3825 G_READ_UNLOCK (&type_rw_lock);
3830 g_return_val_if_fail (node == NULL, NULL);
3831 g_return_val_if_fail (iface == NULL, NULL);
3833 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3839 * g_type_fundamental_next:
3841 * Returns the next free fundamental type id which can be used to
3842 * register a new fundamental type with g_type_register_fundamental().
3843 * The returned type ID represents the highest currently registered
3844 * fundamental type identifier.
3846 * Returns: The nextmost fundamental type ID to be registered,
3847 * or 0 if the type system ran out of fundamental type IDs.
3850 g_type_fundamental_next (void)
3854 G_READ_LOCK (&type_rw_lock);
3855 type = static_fundamental_next;
3856 G_READ_UNLOCK (&type_rw_lock);
3857 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3858 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3862 * g_type_fundamental:
3863 * @type_id: valid type ID
3865 * Internal function, used to extract the fundamental type ID portion.
3866 * use G_TYPE_FUNDAMENTAL() instead.
3868 * Returns: fundamental type ID
3871 g_type_fundamental (GType type_id)
3873 TypeNode *node = lookup_type_node_I (type_id);
3875 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3879 g_type_check_instance_is_a (GTypeInstance *type_instance,
3882 TypeNode *node, *iface;
3885 if (!type_instance || !type_instance->g_class)
3888 node = lookup_type_node_I (type_instance->g_class->g_type);
3889 iface = lookup_type_node_I (iface_type);
3890 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3896 g_type_check_class_is_a (GTypeClass *type_class,
3899 TypeNode *node, *iface;
3905 node = lookup_type_node_I (type_class->g_type);
3906 iface = lookup_type_node_I (is_a_type);
3907 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3913 g_type_check_instance_cast (GTypeInstance *type_instance,
3918 if (type_instance->g_class)
3920 TypeNode *node, *iface;
3921 gboolean is_instantiatable, check;
3923 node = lookup_type_node_I (type_instance->g_class->g_type);
3924 is_instantiatable = node && node->is_instantiatable;
3925 iface = lookup_type_node_I (iface_type);
3926 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3928 return type_instance;
3930 if (is_instantiatable)
3931 g_warning ("invalid cast from `%s' to `%s'",
3932 type_descriptive_name_I (type_instance->g_class->g_type),
3933 type_descriptive_name_I (iface_type));
3935 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3936 type_descriptive_name_I (type_instance->g_class->g_type),
3937 type_descriptive_name_I (iface_type));
3940 g_warning ("invalid unclassed pointer in cast to `%s'",
3941 type_descriptive_name_I (iface_type));
3944 return type_instance;
3948 g_type_check_class_cast (GTypeClass *type_class,
3953 TypeNode *node, *iface;
3954 gboolean is_classed, check;
3956 node = lookup_type_node_I (type_class->g_type);
3957 is_classed = node && node->is_classed;
3958 iface = lookup_type_node_I (is_a_type);
3959 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3964 g_warning ("invalid class cast from `%s' to `%s'",
3965 type_descriptive_name_I (type_class->g_type),
3966 type_descriptive_name_I (is_a_type));
3968 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3969 type_descriptive_name_I (type_class->g_type),
3970 type_descriptive_name_I (is_a_type));
3973 g_warning ("invalid class cast from (NULL) pointer to `%s'",
3974 type_descriptive_name_I (is_a_type));
3979 * g_type_check_instance:
3980 * @instance: A valid #GTypeInstance structure.
3982 * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
3985 * @Returns: #TRUE if @instance is valid, #FALSE otherwise.
3988 g_type_check_instance (GTypeInstance *type_instance)
3990 /* this function is just here to make the signal system
3991 * conveniently elaborated on instance checks
3995 if (type_instance->g_class)
3997 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
3999 if (node && node->is_instantiatable)
4002 g_warning ("instance of invalid non-instantiatable type `%s'",
4003 type_descriptive_name_I (type_instance->g_class->g_type));
4006 g_warning ("instance with invalid (NULL) class pointer");
4009 g_warning ("invalid (NULL) pointer instance");
4014 static inline gboolean
4015 type_check_is_value_type_U (GType type)
4017 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4020 /* common path speed up */
4021 node = lookup_type_node_I (type);
4022 if (node && node->mutatable_check_cache)
4025 G_READ_LOCK (&type_rw_lock);
4029 if (node->data && node->ref_count > 0 &&
4030 node->data->common.value_table->value_init)
4031 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4032 else if (NODE_IS_IFACE (node))
4036 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4038 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4039 TypeNode *prnode = lookup_type_node_I (prtype);
4041 if (prnode->is_instantiatable)
4044 node = lookup_type_node_I (type);
4050 G_READ_UNLOCK (&type_rw_lock);
4052 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4056 g_type_check_is_value_type (GType type)
4058 return type_check_is_value_type_U (type);
4062 g_type_check_value (GValue *value)
4064 return value && type_check_is_value_type_U (value->g_type);
4068 g_type_check_value_holds (GValue *value,
4071 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4075 * g_type_value_table_peek:
4076 * @type: A #GType value.
4078 * Returns the location of the #GTypeValueTable associated with @type.
4079 * <emphasis>Note that this function should only be used from source code
4080 * that implements or has internal knowledge of the implementation of
4083 * Returns: Location of the #GTypeValueTable associated with @type or
4084 * %NULL if there is no #GTypeValueTable associated with @type.
4087 g_type_value_table_peek (GType type)
4089 GTypeValueTable *vtable = NULL;
4090 TypeNode *node = lookup_type_node_I (type);
4091 gboolean has_refed_data, has_table;
4094 /* speed up common code path, we're not 100% safe here,
4095 * but we should only get called with referenced types anyway
4097 data = node ? node->data : NULL;
4098 if (node && node->mutatable_check_cache)
4099 return data->common.value_table;
4101 G_READ_LOCK (&type_rw_lock);
4104 has_refed_data = node && node->data && node->ref_count;
4105 has_table = has_refed_data && node->data->common.value_table->value_init;
4109 vtable = node->data->common.value_table;
4110 else if (NODE_IS_IFACE (node))
4114 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4116 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4117 TypeNode *prnode = lookup_type_node_I (prtype);
4119 if (prnode->is_instantiatable)
4122 node = lookup_type_node_I (type);
4123 goto restart_table_peek;
4129 G_READ_UNLOCK (&type_rw_lock);
4135 g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
4136 if (!has_refed_data)
4137 g_warning ("can't peek value table for type `%s' which is not currently referenced",
4138 type_descriptive_name_I (type));
4143 G_CONST_RETURN gchar*
4144 g_type_name_from_instance (GTypeInstance *instance)
4147 return "<NULL-instance>";
4149 return g_type_name_from_class (instance->g_class);
4152 G_CONST_RETURN gchar*
4153 g_type_name_from_class (GTypeClass *g_class)
4156 return "<NULL-class>";
4158 return g_type_name (g_class->g_type);
4162 /* --- initialization --- */
4164 * g_type_init_with_debug_flags:
4165 * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
4166 * debugging purposes.
4168 * Similar to g_type_init(), but additionally sets debug flags.
4171 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4173 G_LOCK_DEFINE_STATIC (type_init_lock);
4174 const gchar *env_string;
4177 volatile GType votype;
4179 G_LOCK (type_init_lock);
4181 G_WRITE_LOCK (&type_rw_lock);
4183 if (static_quark_type_flags)
4185 G_WRITE_UNLOCK (&type_rw_lock);
4186 G_UNLOCK (type_init_lock);
4190 /* setup GObject library wide debugging flags */
4191 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
4192 env_string = g_getenv ("GOBJECT_DEBUG");
4193 if (env_string != NULL)
4195 static GDebugKey debug_keys[] = {
4196 { "objects", G_TYPE_DEBUG_OBJECTS },
4197 { "signals", G_TYPE_DEBUG_SIGNALS },
4200 _g_type_debug_flags |= g_parse_debug_string (env_string,
4202 sizeof (debug_keys) / sizeof (debug_keys[0]));
4207 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4208 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4209 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4211 /* type qname hash table */
4212 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
4214 /* invalid type G_TYPE_INVALID (0)
4216 static_fundamental_type_nodes[0] = NULL;
4218 /* void type G_TYPE_NONE
4220 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4221 votype = NODE_TYPE (node);
4222 g_assert (votype == G_TYPE_NONE);
4224 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4226 memset (&info, 0, sizeof (info));
4227 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4228 votype = NODE_TYPE (node);
4229 type_data_make_W (node, &info, NULL);
4230 g_assert (votype == G_TYPE_INTERFACE);
4232 G_WRITE_UNLOCK (&type_rw_lock);
4236 /* G_TYPE_TYPE_PLUGIN
4238 votype = g_type_plugin_get_type ();
4240 /* G_TYPE_* value types
4242 g_value_types_init ();
4244 /* G_TYPE_ENUM & G_TYPE_FLAGS
4246 g_enum_types_init ();
4250 g_boxed_type_init ();
4254 g_param_type_init ();
4258 g_object_type_init ();
4260 /* G_TYPE_PARAM_* pspec types
4262 g_param_spec_types_init ();
4264 /* Value Transformations
4266 g_value_transforms_init ();
4272 G_UNLOCK (type_init_lock);
4278 * Prior to any use of the type system, g_type_init() has to be called
4279 * to initialize the type system and assorted other code portions
4280 * (such as the various fundamental type implementations or the signal
4286 g_type_init_with_debug_flags (0);
4290 * g_type_class_add_private:
4291 * @g_class: class structure for an instantiatable type
4292 * @private_size: size of private structure.
4294 * Registers a private structure for an instantiatable type;
4295 * when an object is allocated, the private structures for
4296 * the type and all of its parent types are allocated
4297 * sequentially in the same memory block as the public
4298 * structures. This function should be called in the
4299 * type's class_init() function. The private structure can
4300 * be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.
4301 * The following example shows attaching a private structure
4302 * <structname>MyObjectPrivate</structname> to an object
4303 * <structname>MyObject</structname> defined in the standard GObject
4307 * typedef struct _MyObjectPrivate MyObjectPrivate;
4309 * struct _MyObjectPrivate {
4313 * #define MY_OBJECT_GET_PRIVATE(o) \
4314 * (G_TYPE_INSTANCE_GET_PRIVATE ((o), MY_TYPE_OBJECT, MyObjectPrivate))
4317 * my_object_class_init (MyObjectClass *klass)
4319 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4323 * my_object_get_some_field (MyObject *my_object)
4325 * MyObjectPrivate *priv = MY_OBJECT_GET_PRIVATE (my_object);
4327 * return priv->some_field;
4334 g_type_class_add_private (gpointer g_class,
4337 GType instance_type = ((GTypeClass *)g_class)->g_type;
4338 TypeNode *node = lookup_type_node_I (instance_type);
4341 g_return_if_fail (private_size > 0);
4343 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4345 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4346 type_descriptive_name_I (instance_type));
4350 if (NODE_PARENT_TYPE (node))
4352 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4353 if (node->data->instance.private_size != pnode->data->instance.private_size)
4355 g_warning ("g_type_add_private() called multiple times for the same type");
4360 G_WRITE_LOCK (&type_rw_lock);
4362 offset = ALIGN_STRUCT (node->data->instance.private_size);
4363 node->data->instance.private_size = offset + private_size;
4365 G_WRITE_UNLOCK (&type_rw_lock);
4369 g_type_instance_get_private (GTypeInstance *instance,
4372 TypeNode *instance_node;
4373 TypeNode *private_node;
4374 TypeNode *parent_node;
4378 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4380 /* while instances are initialized, their class pointers change,
4381 * so figure the instances real class first
4383 class = instance_real_class_get (instance);
4385 class = instance->g_class;
4387 instance_node = lookup_type_node_I (class->g_type);
4388 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4390 g_warning ("instance of invalid non-instantiatable type `%s'",
4391 type_descriptive_name_I (instance->g_class->g_type));
4395 private_node = lookup_type_node_I (private_type);
4396 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4398 g_warning ("attempt to retrieve private data for invalid type '%s'",
4399 type_descriptive_name_I (private_type));
4403 /* Note that we don't need a read lock, since instance existing
4404 * means that the instance class and all parent classes
4405 * exist, so the node->data, node->data->instance.instance_size,
4406 * and node->data->instance.private_size are not going to be changed.
4407 * for any of the relevant types.
4410 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4412 if (NODE_PARENT_TYPE (private_node))
4414 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4415 g_assert (parent_node->data && parent_node->ref_count);
4417 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4419 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4423 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4426 return G_STRUCT_MEMBER_P (instance, offset);
4429 #define __G_TYPE_C__
4430 #include "gobjectaliasdef.c"