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.
21 * @Short_description: The GLib Runtime type identification and management system
22 * @Title:Type Information
24 * The GType API is the foundation of the GObject system. It provides the
25 * facilities for registering and managing all fundamental data types,
26 * user-defined object and interface types. Before using any GType
27 * or GObject functions, g_type_init() must be called to initialize the
30 * For type creation and registration purposes, all types fall into one of
31 * two categories: static or dynamic. Static types are never loaded or
32 * unloaded at run-time as dynamic types may be. Static types are created
33 * with g_type_register_static() that gets type specific information passed
34 * in via a #GTypeInfo structure.
35 * Dynamic types are created with g_type_register_dynamic() which takes a
36 * #GTypePlugin structure instead. The remaining type information (the
37 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
38 * and the g_type_plugin_*() API.
39 * These registration functions are usually called only once from a
40 * function whose only purpose is to return the type identifier for a
41 * specific class. Once the type (or class or interface) is registered,
42 * it may be instantiated, inherited, or implemented depending on exactly
43 * what sort of type it is.
44 * There is also a third registration function for registering fundamental
45 * types called g_type_register_fundamental() which requires both a #GTypeInfo
46 * structure and a #GTypeFundamentalInfo structure but it is seldom used
47 * since most fundamental types are predefined rather than user-defined.
49 * A final word about type names.
50 * Such an identifier needs to be at least three characters long. There is no
51 * upper length limit. The first character needs to be a letter (a-z or A-Z)
52 * or an underscore '_'. Subsequent characters can be letters, numbers or
62 #include "gtypeplugin.h"
63 #include "gvaluecollector.h"
64 #include "gbsearcharray.h"
67 #include "gobjectalias.h"
69 /* NOTE: some functions (some internal variants and exported ones)
70 * invalidate data portions of the TypeNodes. if external functions/callbacks
71 * are called, pointers to memory maintained by TypeNodes have to be looked up
72 * again. this affects most of the struct TypeNode fields, e.g. ->children or
73 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
74 * not ->supers[]), as all those memory portions can get realloc()ed during
75 * callback invocation.
78 * - g_type_from_name() should do an ordered array lookup after fetching the
79 * the quark, instead of a second hashtable lookup.
82 * lock handling issues when calling static functions are indicated by
83 * uppercase letter postfixes, all static functions have to have
84 * one of the below postfixes:
85 * - _I: [Indifferent about locking]
86 * function doesn't care about locks at all
87 * - _U: [Unlocked invocation]
88 * no read or write lock has to be held across function invocation
89 * (locks may be acquired and released during invocation though)
90 * - _L: [Locked invocation]
91 * a write lock or more than 0 read locks have to be held across
93 * - _W: [Write-locked invocation]
94 * a write lock has to be held across function invocation
95 * - _Wm: [Write-locked invocation, mutatable]
96 * like _W, but the write lock might be released and reacquired
97 * during invocation, watch your pointers
98 * - _WmREC: [Write-locked invocation, mutatable, recursive]
99 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
103 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
104 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
105 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
106 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
108 #define G_READ_LOCK(rw_lock) g_static_rw_lock_reader_lock (rw_lock)
109 #define G_READ_UNLOCK(rw_lock) g_static_rw_lock_reader_unlock (rw_lock)
110 #define G_WRITE_LOCK(rw_lock) g_static_rw_lock_writer_lock (rw_lock)
111 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
113 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
114 static const gchar _action[] = " invalidly modified type "; \
115 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
117 g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
119 g_error ("%s()%s`%s'", _fname, _action, _tname); \
121 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{ \
124 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
125 "%s: initialization assertion failed, use %s() prior to this function", \
126 G_STRLOC, G_STRINGIFY (init_function)); \
127 return (return_value); \
131 #ifdef G_ENABLE_DEBUG
132 #define DEBUG_CODE(debug_type, code_block) G_STMT_START { \
133 if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
136 #else /* !G_ENABLE_DEBUG */
137 #define DEBUG_CODE(debug_type, code_block) /* code_block */
138 #endif /* G_ENABLE_DEBUG */
140 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
141 G_TYPE_FLAG_INSTANTIATABLE | \
142 G_TYPE_FLAG_DERIVABLE | \
143 G_TYPE_FLAG_DEEP_DERIVABLE)
144 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
145 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
146 sizeof (gpointer)), \
149 /* The 2*sizeof(size_t) alignment here is borrowed from
150 * GNU libc, so it should be good most everywhere.
151 * It is more conservative than is needed on some 64-bit
152 * platforms, but ia64 does require a 16-byte alignment.
153 * The SIMD extensions for x86 and ppc32 would want a
154 * larger alignment than this, but we don't need to
155 * do better than malloc.
157 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
158 #define ALIGN_STRUCT(offset) \
159 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
162 /* --- typedefs --- */
163 typedef struct _TypeNode TypeNode;
164 typedef struct _CommonData CommonData;
165 typedef struct _IFaceData IFaceData;
166 typedef struct _ClassData ClassData;
167 typedef struct _InstanceData InstanceData;
168 typedef union _TypeData TypeData;
169 typedef struct _IFaceEntry IFaceEntry;
170 typedef struct _IFaceHolder IFaceHolder;
173 /* --- prototypes --- */
174 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
175 static void type_add_flags_W (TypeNode *node,
177 static void type_data_make_W (TypeNode *node,
178 const GTypeInfo *info,
179 const GTypeValueTable *value_table);
180 static inline void type_data_ref_Wm (TypeNode *node);
181 static inline void type_data_unref_WmREC (TypeNode *node,
183 static void type_data_last_unref_Wm (GType type,
185 static inline gpointer type_get_qdata_L (TypeNode *node,
187 static inline void type_set_qdata_W (TypeNode *node,
190 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
191 GType instance_type);
192 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
194 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
196 static gboolean type_node_is_a_L (TypeNode *node,
197 TypeNode *iface_node);
200 /* --- enumeration --- */
202 /* The InitState enumeration is used to track the progress of initializing
203 * both classes and interface vtables. Keeping the state of initialization
204 * is necessary to handle new interfaces being added while we are initializing
205 * the class or other interfaces.
217 /* --- structures --- */
221 guint n_children : 12;
223 guint _prot_n_ifaces_prerequisites : 9;
224 guint is_classed : 1;
225 guint is_instantiatable : 1;
226 guint mutatable_check_cache : 1; /* combines some common path checks */
228 TypeData * volatile data;
232 IFaceEntry *iface_entries; /* for !iface types */
233 GType *prerequisistes;
235 GType supers[1]; /* flexible array */
237 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
238 #define MAX_N_SUPERS (255)
239 #define MAX_N_CHILDREN (4095)
240 #define MAX_N_IFACES (511)
241 #define MAX_N_PREREQUISITES (MAX_N_IFACES)
242 #define NODE_TYPE(node) (node->supers[0])
243 #define NODE_PARENT_TYPE(node) (node->supers[1])
244 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
245 #define NODE_NAME(node) (g_quark_to_string (node->qname))
246 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
247 #define CLASSED_NODE_N_IFACES(node) ((node)->_prot_n_ifaces_prerequisites)
248 #define CLASSED_NODE_IFACES_ENTRIES(node) ((node)->_prot.iface_entries)
249 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->_prot_n_ifaces_prerequisites)
250 #define IFACE_NODE_PREREQUISITES(node) ((node)->_prot.prerequisistes)
251 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
252 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
253 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
254 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
255 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
257 #define NODE_IS_ANCESTOR(ancestor, node) \
258 ((ancestor)->n_supers <= (node)->n_supers && \
259 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
265 GInterfaceInfo *info;
272 GTypeInterface *vtable;
273 InitState init_state;
278 GTypeValueTable *value_table;
284 GBaseInitFunc vtable_init_base;
285 GBaseFinalizeFunc vtable_finalize_base;
286 GClassInitFunc dflt_init;
287 GClassFinalizeFunc dflt_finalize;
288 gconstpointer dflt_data;
289 gpointer dflt_vtable;
295 guint init_state : 4;
296 GBaseInitFunc class_init_base;
297 GBaseFinalizeFunc class_finalize_base;
298 GClassInitFunc class_init;
299 GClassFinalizeFunc class_finalize;
300 gconstpointer class_data;
307 guint init_state : 4;
308 GBaseInitFunc class_init_base;
309 GBaseFinalizeFunc class_finalize_base;
310 GClassInitFunc class_init;
311 GClassFinalizeFunc class_finalize;
312 gconstpointer class_data;
314 guint16 instance_size;
315 guint16 private_size;
317 GInstanceInitFunc instance_init;
324 InstanceData instance;
328 GTypeClassCacheFunc cache_func;
332 GTypeInterfaceCheckFunc check_func;
336 /* --- variables --- */
337 static GStaticRWLock type_rw_lock = G_STATIC_RW_LOCK_INIT;
338 static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
339 static guint static_n_class_cache_funcs = 0;
340 static ClassCacheFunc *static_class_cache_funcs = NULL;
341 static guint static_n_iface_check_funcs = 0;
342 static IFaceCheckFunc *static_iface_check_funcs = NULL;
343 static GQuark static_quark_type_flags = 0;
344 static GQuark static_quark_iface_holder = 0;
345 static GQuark static_quark_dependants_array = 0;
346 GTypeDebugFlags _g_type_debug_flags = 0;
349 /* --- type nodes --- */
350 static GHashTable *static_type_nodes_ht = NULL;
351 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
352 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
354 static inline TypeNode*
355 lookup_type_node_I (register GType utype)
357 if (utype > G_TYPE_FUNDAMENTAL_MAX)
358 return (TypeNode*) (utype & ~TYPE_ID_MASK);
360 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
364 type_node_any_new_W (TypeNode *pnode,
368 GTypeFundamentalFlags type_flags)
373 guint i, node_size = 0;
375 n_supers = pnode ? pnode->n_supers + 1 : 0;
378 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
379 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
380 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
381 node = g_malloc0 (node_size);
382 if (!pnode) /* offset fundamental types */
384 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
385 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
391 g_assert ((type & TYPE_ID_MASK) == 0);
393 node->n_supers = n_supers;
396 node->supers[0] = type;
399 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
400 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
402 if (NODE_IS_IFACE (node))
404 IFACE_NODE_N_PREREQUISITES (node) = 0;
405 IFACE_NODE_PREREQUISITES (node) = NULL;
409 CLASSED_NODE_N_IFACES (node) = 0;
410 CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
415 node->supers[0] = type;
416 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
418 node->is_classed = pnode->is_classed;
419 node->is_instantiatable = pnode->is_instantiatable;
421 if (NODE_IS_IFACE (node))
423 IFACE_NODE_N_PREREQUISITES (node) = 0;
424 IFACE_NODE_PREREQUISITES (node) = NULL;
430 CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
431 CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
432 sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
433 CLASSED_NODE_N_IFACES (node));
434 for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
436 CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
437 CLASSED_NODE_IFACES_ENTRIES (node)[j].init_state = UNINITIALIZED;
441 i = pnode->n_children++;
442 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
443 pnode->children[i] = type;
446 node->plugin = plugin;
447 node->n_children = 0;
448 node->children = NULL;
450 node->qname = g_quark_from_string (name);
451 node->global_gdata = NULL;
453 g_hash_table_insert (static_type_nodes_ht,
454 GUINT_TO_POINTER (node->qname),
459 static inline GTypeFundamentalInfo*
460 type_node_fundamental_info_I (TypeNode *node)
462 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
464 if (ftype != NODE_TYPE (node))
465 node = lookup_type_node_I (ftype);
467 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
471 type_node_fundamental_new_W (GType ftype,
473 GTypeFundamentalFlags type_flags)
475 GTypeFundamentalInfo *finfo;
478 g_assert ((ftype & TYPE_ID_MASK) == 0);
479 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
481 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
482 static_fundamental_next++;
484 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
486 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
488 finfo = type_node_fundamental_info_I (node);
489 finfo->type_flags = type_flags;
495 type_node_new_W (TypeNode *pnode,
501 g_assert (pnode->n_supers < MAX_N_SUPERS);
502 g_assert (pnode->n_children < MAX_N_CHILDREN);
504 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
507 static inline IFaceEntry*
508 type_lookup_iface_entry_L (TypeNode *node,
509 TypeNode *iface_node)
511 if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
513 IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
514 guint n_ifaces = CLASSED_NODE_N_IFACES (node);
515 GType iface_type = NODE_TYPE (iface_node);
522 i = (n_ifaces + 1) >> 1;
524 if (iface_type == check->iface_type)
526 else if (iface_type > check->iface_type)
531 else /* if (iface_type < check->iface_type) */
540 static inline gboolean
541 type_lookup_prerequisite_L (TypeNode *iface,
542 GType prerequisite_type)
544 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
546 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
547 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
554 i = (n_prerequisites + 1) >> 1;
555 check = prerequisites + i;
556 if (prerequisite_type == *check)
558 else if (prerequisite_type > *check)
560 n_prerequisites -= i;
561 prerequisites = check;
563 else /* if (prerequisite_type < *check) */
564 n_prerequisites = i - 1;
566 while (n_prerequisites);
572 type_descriptive_name_I (GType type)
576 TypeNode *node = lookup_type_node_I (type);
578 return node ? NODE_NAME (node) : "<unknown>";
585 /* --- type consistency checks --- */
587 check_plugin_U (GTypePlugin *plugin,
588 gboolean need_complete_type_info,
589 gboolean need_complete_interface_info,
590 const gchar *type_name)
592 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
596 g_warning ("plugin handle for type `%s' is NULL",
600 if (!G_IS_TYPE_PLUGIN (plugin))
602 g_warning ("plugin pointer (%p) for type `%s' is invalid",
606 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
608 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
612 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
614 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
622 check_type_name_I (const gchar *type_name)
624 static const gchar extra_chars[] = "-_+";
625 const gchar *p = type_name;
628 if (!type_name[0] || !type_name[1] || !type_name[2])
630 g_warning ("type name `%s' is too short", type_name);
633 /* check the first letter */
634 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
635 for (p = type_name + 1; *p; p++)
636 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
637 (p[0] >= 'a' && p[0] <= 'z') ||
638 (p[0] >= '0' && p[0] <= '9') ||
639 strchr (extra_chars, p[0]));
642 g_warning ("type name `%s' contains invalid characters", type_name);
645 if (g_type_from_name (type_name))
647 g_warning ("cannot register existing type `%s'", type_name);
655 check_derivation_I (GType parent_type,
656 const gchar *type_name)
659 GTypeFundamentalInfo* finfo;
661 pnode = lookup_type_node_I (parent_type);
664 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
666 type_descriptive_name_I (parent_type));
669 finfo = type_node_fundamental_info_I (pnode);
670 /* ensure flat derivability */
671 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
673 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
678 /* ensure deep derivability */
679 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
680 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
682 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
692 check_collect_format_I (const gchar *collect_format)
694 const gchar *p = collect_format;
695 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
696 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
697 G_VALUE_COLLECT_POINTER, 0 };
700 if (!strchr (valid_format, *p++))
702 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
706 check_value_table_I (const gchar *type_name,
707 const GTypeValueTable *value_table)
711 else if (value_table->value_init == NULL)
713 if (value_table->value_free || value_table->value_copy ||
714 value_table->value_peek_pointer ||
715 value_table->collect_format || value_table->collect_value ||
716 value_table->lcopy_format || value_table->lcopy_value)
717 g_warning ("cannot handle uninitializable values of type `%s'",
721 else /* value_table->value_init != NULL */
723 if (!value_table->value_free)
726 * g_warning ("missing `value_free()' for type `%s'", type_name);
730 if (!value_table->value_copy)
732 g_warning ("missing `value_copy()' for type `%s'", type_name);
735 if ((value_table->collect_format || value_table->collect_value) &&
736 (!value_table->collect_format || !value_table->collect_value))
738 g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
742 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
744 g_warning ("the `%s' specification for type `%s' is too long or invalid",
749 if ((value_table->lcopy_format || value_table->lcopy_value) &&
750 (!value_table->lcopy_format || !value_table->lcopy_value))
752 g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
756 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
758 g_warning ("the `%s' specification for type `%s' is too long or invalid",
768 check_type_info_I (TypeNode *pnode,
770 const gchar *type_name,
771 const GTypeInfo *info)
773 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
774 gboolean is_interface = ftype == G_TYPE_INTERFACE;
776 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
778 /* check instance members */
779 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
780 (info->instance_size || info->n_preallocs || info->instance_init))
783 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
787 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
791 /* check class & interface members */
792 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
793 (info->class_init || info->class_finalize || info->class_data ||
794 info->class_size || info->base_init || info->base_finalize))
797 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
801 g_warning ("cannot create class for `%s' as non-classed fundamental",
805 /* check interface size */
806 if (is_interface && info->class_size < sizeof (GTypeInterface))
808 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
812 /* check class size */
813 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
815 if (info->class_size < sizeof (GTypeClass))
817 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
821 if (pnode && info->class_size < pnode->data->class.class_size)
823 g_warning ("specified class size for type `%s' is smaller "
824 "than the parent type's `%s' class size",
830 /* check instance size */
831 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
833 if (info->instance_size < sizeof (GTypeInstance))
835 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
839 if (pnode && info->instance_size < pnode->data->instance.instance_size)
841 g_warning ("specified instance size for type `%s' is smaller "
842 "than the parent type's `%s' instance size",
853 find_conforming_child_type_L (TypeNode *pnode,
856 TypeNode *node = NULL;
859 if (type_lookup_iface_entry_L (pnode, iface))
862 for (i = 0; i < pnode->n_children && !node; i++)
863 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
869 check_add_interface_L (GType instance_type,
872 TypeNode *node = lookup_type_node_I (instance_type);
873 TypeNode *iface = lookup_type_node_I (iface_type);
876 GType *prerequisites;
880 if (!node || !node->is_instantiatable)
882 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
883 type_descriptive_name_I (instance_type));
886 if (!iface || !NODE_IS_IFACE (iface))
888 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
889 type_descriptive_name_I (iface_type),
893 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
894 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
896 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
897 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
903 /* allow overriding of interface type introduced for parent type */
904 entry = type_lookup_iface_entry_L (node, iface);
905 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
907 /* ok, we do conform to this interface already, but the interface vtable was not
908 * yet intialized, and we just conform to the interface because it got added to
909 * one of our parents. so we allow overriding of holder info here.
913 /* check whether one of our children already conforms (or whether the interface
914 * got added to this node already)
916 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
919 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
925 prerequisites = IFACE_NODE_PREREQUISITES (iface);
926 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
928 tnode = lookup_type_node_I (prerequisites[i]);
929 if (!type_node_is_a_L (node, tnode))
931 g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
942 check_interface_info_I (TypeNode *iface,
944 const GInterfaceInfo *info)
946 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
948 g_warning ("interface type `%s' for type `%s' comes without initializer",
950 type_descriptive_name_I (instance_type));
957 /* --- type info (type node data) --- */
959 type_data_make_W (TypeNode *node,
960 const GTypeInfo *info,
961 const GTypeValueTable *value_table)
964 GTypeValueTable *vtable = NULL;
965 guint vtable_size = 0;
967 g_assert (node->data == NULL && info != NULL);
971 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
974 vtable = pnode->data->common.value_table;
977 static const GTypeValueTable zero_vtable = { NULL, };
979 value_table = &zero_vtable;
984 /* need to setup vtable_size since we have to allocate it with data in one chunk */
985 vtable_size = sizeof (GTypeValueTable);
986 if (value_table->collect_format)
987 vtable_size += strlen (value_table->collect_format);
988 if (value_table->lcopy_format)
989 vtable_size += strlen (value_table->lcopy_format);
993 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
995 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
997 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
998 data->instance.class_size = info->class_size;
999 data->instance.class_init_base = info->base_init;
1000 data->instance.class_finalize_base = info->base_finalize;
1001 data->instance.class_init = info->class_init;
1002 data->instance.class_finalize = info->class_finalize;
1003 data->instance.class_data = info->class_data;
1004 data->instance.class = NULL;
1005 data->instance.init_state = UNINITIALIZED;
1006 data->instance.instance_size = info->instance_size;
1007 /* We'll set the final value for data->instance.private size
1008 * after the parent class has been initialized
1010 data->instance.private_size = 0;
1011 #ifdef DISABLE_MEM_POOLS
1012 data->instance.n_preallocs = 0;
1013 #else /* !DISABLE_MEM_POOLS */
1014 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1015 #endif /* !DISABLE_MEM_POOLS */
1016 data->instance.instance_init = info->instance_init;
1018 else if (node->is_classed) /* only classed */
1020 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1022 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1023 data->class.class_size = info->class_size;
1024 data->class.class_init_base = info->base_init;
1025 data->class.class_finalize_base = info->base_finalize;
1026 data->class.class_init = info->class_init;
1027 data->class.class_finalize = info->class_finalize;
1028 data->class.class_data = info->class_data;
1029 data->class.class = NULL;
1030 data->class.init_state = UNINITIALIZED;
1032 else if (NODE_IS_IFACE (node))
1034 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1036 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1037 data->iface.vtable_size = info->class_size;
1038 data->iface.vtable_init_base = info->base_init;
1039 data->iface.vtable_finalize_base = info->base_finalize;
1040 data->iface.dflt_init = info->class_init;
1041 data->iface.dflt_finalize = info->class_finalize;
1042 data->iface.dflt_data = info->class_data;
1043 data->iface.dflt_vtable = NULL;
1047 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1049 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1053 node->data->common.ref_count = 1;
1059 /* we allocate the vtable and its strings together with the type data, so
1060 * children can take over their parent's vtable pointer, and we don't
1061 * need to worry freeing it or not when the child data is destroyed
1063 *vtable = *value_table;
1064 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1066 vtable->collect_format = p;
1067 if (value_table->collect_format)
1069 strcat (p, value_table->collect_format);
1070 p += strlen (value_table->collect_format);
1074 vtable->lcopy_format = p;
1075 if (value_table->lcopy_format)
1076 strcat (p, value_table->lcopy_format);
1078 node->data->common.value_table = vtable;
1079 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1080 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1081 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1083 g_assert (node->data->common.value_table != NULL); /* paranoid */
1087 type_data_ref_Wm (TypeNode *node)
1091 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1093 GTypeValueTable tmp_value_table;
1095 g_assert (node->plugin != NULL);
1099 type_data_ref_Wm (pnode);
1101 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1104 memset (&tmp_info, 0, sizeof (tmp_info));
1105 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1107 G_WRITE_UNLOCK (&type_rw_lock);
1108 g_type_plugin_use (node->plugin);
1109 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1110 G_WRITE_LOCK (&type_rw_lock);
1112 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1114 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1115 type_data_make_W (node, &tmp_info,
1116 check_value_table_I (NODE_NAME (node),
1117 &tmp_value_table) ? &tmp_value_table : NULL);
1121 g_assert (node->data->common.ref_count > 0);
1123 node->data->common.ref_count += 1;
1128 type_data_unref_WmREC (TypeNode *node,
1131 g_assert (node->data && node->data->common.ref_count);
1132 if (node->data->common.ref_count > 1)
1133 node->data->common.ref_count -= 1;
1136 GType node_type = NODE_TYPE (node);
1139 g_warning ("static type `%s' unreferenced too often",
1143 G_WRITE_UNLOCK (&type_rw_lock);
1144 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
1145 G_WRITE_LOCK (&type_rw_lock);
1146 type_data_last_unref_Wm (node_type, uncached);
1147 g_static_rec_mutex_unlock (&class_init_rec_mutex);
1152 type_node_add_iface_entry_W (TypeNode *node,
1154 IFaceEntry *parent_entry)
1156 IFaceEntry *entries;
1159 g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1161 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1162 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1163 if (entries[i].iface_type == iface_type)
1165 /* this can happen in two cases:
1166 * - our parent type already conformed to iface_type and node
1167 * got it's own holder info. here, our children already have
1168 * entries and NULL vtables, since this will only work for
1169 * uninitialized classes.
1170 * - an interface type is added to an ancestor after it was
1171 * added to a child type.
1174 g_assert (entries[i].vtable == NULL && entries[i].init_state == UNINITIALIZED);
1177 /* sick, interface is added to ancestor *after* child type;
1178 * nothing todo, the entry and our children were already setup correctly
1183 else if (entries[i].iface_type > iface_type)
1185 CLASSED_NODE_N_IFACES (node) += 1;
1186 CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1187 CLASSED_NODE_IFACES_ENTRIES (node),
1188 CLASSED_NODE_N_IFACES (node));
1189 entries = CLASSED_NODE_IFACES_ENTRIES (node);
1190 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1191 entries[i].iface_type = iface_type;
1192 entries[i].vtable = NULL;
1193 entries[i].init_state = UNINITIALIZED;
1197 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1199 entries[i].init_state = INITIALIZED;
1200 entries[i].vtable = parent_entry->vtable;
1202 for (i = 0; i < node->n_children; i++)
1203 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries[i]);
1208 type_add_interface_Wm (TypeNode *node,
1210 const GInterfaceInfo *info,
1211 GTypePlugin *plugin)
1213 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1217 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1219 iholder->next = iface_node_get_holders_L (iface);
1220 iface_node_set_holders_W (iface, iholder);
1221 iholder->instance_type = NODE_TYPE (node);
1222 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1223 iholder->plugin = plugin;
1225 /* create an iface entry for this type */
1226 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1228 /* if the class is already (partly) initialized, we may need to base
1229 * initalize and/or initialize the new interface.
1233 InitState class_state = node->data->class.init_state;
1235 if (class_state >= BASE_IFACE_INIT)
1236 type_iface_vtable_base_init_Wm (iface, node);
1238 if (class_state >= IFACE_INIT)
1239 type_iface_vtable_iface_init_Wm (iface, node);
1242 /* create iface entries for children of this type */
1243 entry = type_lookup_iface_entry_L (node, iface);
1244 for (i = 0; i < node->n_children; i++)
1245 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1249 type_iface_add_prerequisite_W (TypeNode *iface,
1250 TypeNode *prerequisite_node)
1252 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1253 GType *prerequisites, *dependants;
1254 guint n_dependants, i;
1256 g_assert (NODE_IS_IFACE (iface) &&
1257 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1258 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1260 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1261 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1262 if (prerequisites[i] == prerequisite_type)
1263 return; /* we already have that prerequisiste */
1264 else if (prerequisites[i] > prerequisite_type)
1266 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1267 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1268 IFACE_NODE_PREREQUISITES (iface),
1269 IFACE_NODE_N_PREREQUISITES (iface));
1270 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1271 g_memmove (prerequisites + i + 1, prerequisites + i,
1272 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1273 prerequisites[i] = prerequisite_type;
1275 /* we want to get notified when prerequisites get added to prerequisite_node */
1276 if (NODE_IS_IFACE (prerequisite_node))
1278 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1279 n_dependants = dependants ? dependants[0] : 0;
1281 dependants = g_renew (GType, dependants, n_dependants + 1);
1282 dependants[n_dependants] = NODE_TYPE (iface);
1283 dependants[0] = n_dependants;
1284 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1287 /* we need to notify all dependants */
1288 dependants = iface_node_get_dependants_array_L (iface);
1289 n_dependants = dependants ? dependants[0] : 0;
1290 for (i = 1; i <= n_dependants; i++)
1291 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1295 * g_type_interface_add_prerequisite:
1296 * @interface_type: #GType value of an interface type.
1297 * @prerequisite_type: #GType value of an interface or instantiatable type.
1299 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1300 * This means that any type implementing @interface_type must also implement
1301 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1302 * interface derivation (which GType doesn't support). An interface can have
1303 * at most one instantiatable prerequisite type.
1306 g_type_interface_add_prerequisite (GType interface_type,
1307 GType prerequisite_type)
1309 TypeNode *iface, *prerequisite_node;
1310 IFaceHolder *holders;
1312 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1313 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1314 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1316 iface = lookup_type_node_I (interface_type);
1317 prerequisite_node = lookup_type_node_I (prerequisite_type);
1318 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1320 g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1321 type_descriptive_name_I (interface_type),
1322 type_descriptive_name_I (prerequisite_type));
1325 G_WRITE_LOCK (&type_rw_lock);
1326 holders = iface_node_get_holders_L (iface);
1329 G_WRITE_UNLOCK (&type_rw_lock);
1330 g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1331 type_descriptive_name_I (prerequisite_type),
1332 type_descriptive_name_I (interface_type),
1333 type_descriptive_name_I (holders->instance_type));
1336 if (prerequisite_node->is_instantiatable)
1340 /* can have at most one publically installable instantiatable prerequisite */
1341 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1343 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1345 if (prnode->is_instantiatable)
1347 G_WRITE_UNLOCK (&type_rw_lock);
1348 g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1349 type_descriptive_name_I (prerequisite_type),
1350 type_descriptive_name_I (interface_type),
1351 type_descriptive_name_I (NODE_TYPE (prnode)));
1356 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1357 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1358 G_WRITE_UNLOCK (&type_rw_lock);
1360 else if (NODE_IS_IFACE (prerequisite_node))
1362 GType *prerequisites;
1365 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1366 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1367 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1368 type_iface_add_prerequisite_W (iface, prerequisite_node);
1369 G_WRITE_UNLOCK (&type_rw_lock);
1373 G_WRITE_UNLOCK (&type_rw_lock);
1374 g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1375 type_descriptive_name_I (prerequisite_type),
1376 type_descriptive_name_I (interface_type));
1381 * g_type_interface_prerequisites:
1382 * @interface_type: an interface type
1383 * @n_prerequisites: location to return the number of prerequisites, or %NULL
1385 * Returns the prerequisites of an interfaces type.
1388 * Returns: a newly-allocated zero-terminated array of #GType containing
1389 * the prerequisites of @interface_type
1392 g_type_interface_prerequisites (GType interface_type,
1393 guint *n_prerequisites)
1397 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1399 iface = lookup_type_node_I (interface_type);
1403 TypeNode *inode = NULL;
1406 G_READ_LOCK (&type_rw_lock);
1407 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1408 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1410 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1411 TypeNode *node = lookup_type_node_I (prerequisite);
1412 if (node->is_instantiatable &&
1413 (!inode || type_node_is_a_L (node, inode)))
1416 types[n++] = NODE_TYPE (node);
1419 types[n++] = NODE_TYPE (inode);
1421 if (n_prerequisites)
1422 *n_prerequisites = n;
1423 G_READ_UNLOCK (&type_rw_lock);
1429 if (n_prerequisites)
1430 *n_prerequisites = 0;
1438 type_iface_peek_holder_L (TypeNode *iface,
1439 GType instance_type)
1441 IFaceHolder *iholder;
1443 g_assert (NODE_IS_IFACE (iface));
1445 iholder = iface_node_get_holders_L (iface);
1446 while (iholder && iholder->instance_type != instance_type)
1447 iholder = iholder->next;
1452 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1453 GType instance_type,
1456 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1458 if (iholder && !iholder->info && need_info)
1460 GInterfaceInfo tmp_info;
1462 g_assert (iholder->plugin != NULL);
1464 type_data_ref_Wm (iface);
1466 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1468 memset (&tmp_info, 0, sizeof (tmp_info));
1470 G_WRITE_UNLOCK (&type_rw_lock);
1471 g_type_plugin_use (iholder->plugin);
1472 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1473 G_WRITE_LOCK (&type_rw_lock);
1475 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1477 check_interface_info_I (iface, instance_type, &tmp_info);
1478 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1481 return iholder; /* we don't modify write lock upon returning NULL */
1485 type_iface_blow_holder_info_Wm (TypeNode *iface,
1486 GType instance_type)
1488 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1490 g_assert (NODE_IS_IFACE (iface));
1492 while (iholder->instance_type != instance_type)
1493 iholder = iholder->next;
1495 if (iholder->info && iholder->plugin)
1497 g_free (iholder->info);
1498 iholder->info = NULL;
1500 G_WRITE_UNLOCK (&type_rw_lock);
1501 g_type_plugin_unuse (iholder->plugin);
1502 G_WRITE_LOCK (&type_rw_lock);
1504 type_data_unref_WmREC (iface, FALSE);
1508 /* Assumes type's class already exists
1510 static inline size_t
1511 type_total_instance_size_I (TypeNode *node)
1513 gsize total_instance_size;
1515 total_instance_size = node->data->instance.instance_size;
1516 if (node->data->instance.private_size != 0)
1517 total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1519 return total_instance_size;
1522 /* --- type structure creation/destruction --- */
1526 } InstanceRealClass;
1528 instance_real_class_cmp (gconstpointer p1,
1531 const InstanceRealClass *irc1 = p1;
1532 const InstanceRealClass *irc2 = p2;
1533 guint8 *i1 = irc1->instance;
1534 guint8 *i2 = irc2->instance;
1535 return G_BSEARCH_ARRAY_CMP (i1, i2);
1537 G_LOCK_DEFINE_STATIC (instance_real_class);
1538 static GBSearchArray *instance_real_class_bsa = NULL;
1539 static GBSearchConfig instance_real_class_bconfig = {
1540 sizeof (InstanceRealClass),
1541 instance_real_class_cmp,
1545 instance_real_class_set (gpointer instance,
1548 InstanceRealClass key;
1549 key.instance = instance;
1551 G_LOCK (instance_real_class);
1552 if (!instance_real_class_bsa)
1553 instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1554 instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1555 G_UNLOCK (instance_real_class);
1558 instance_real_class_remove (gpointer instance)
1560 InstanceRealClass key, *node;
1562 key.instance = instance;
1563 G_LOCK (instance_real_class);
1564 node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1565 index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1566 instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1567 if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1569 g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1570 instance_real_class_bsa = NULL;
1572 G_UNLOCK (instance_real_class);
1574 static inline GTypeClass*
1575 instance_real_class_get (gpointer instance)
1577 InstanceRealClass key, *node;
1579 key.instance = instance;
1580 G_LOCK (instance_real_class);
1581 node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1582 class = node ? node->class : NULL;
1583 G_UNLOCK (instance_real_class);
1588 * g_type_create_instance:
1589 * @type: An instantiatable type to create an instance for.
1591 * Creates and initializes an instance of @type if @type is valid and can
1592 * be instantiated. The type system only performs basic allocation and
1593 * structure setups for instances: actual instance creation should happen
1594 * through functions supplied by the type's fundamental type implementation.
1595 * So use of g_type_create_instance() is reserved for implementators of
1596 * fundamental types only. E.g. instances of the #GObject hierarchy
1597 * should be created via g_object_new() and <emphasis>never</emphasis>
1598 * directly through g_type_create_instance() which doesn't handle
1599 * things like singleton objects or object construction.
1600 * Note: Do <emphasis>not</emphasis> use this function, unless you're
1601 * implementing a fundamental type. Also language bindings should <emphasis>not</emphasis>
1602 * use this function but g_object_new() instead.
1604 * Returns: An allocated and initialized instance, subject to further
1605 * treatment by the fundamental type implementation.
1608 g_type_create_instance (GType type)
1611 GTypeInstance *instance;
1613 guint i, total_size;
1615 node = lookup_type_node_I (type);
1616 if (!node || !node->is_instantiatable)
1618 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1619 type_descriptive_name_I (type));
1622 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1623 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1625 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1626 type_descriptive_name_I (type));
1630 class = g_type_class_ref (type);
1631 total_size = type_total_instance_size_I (node);
1633 instance = g_slice_alloc0 (total_size);
1635 if (node->data->instance.private_size)
1636 instance_real_class_set (instance, class);
1637 for (i = node->n_supers; i > 0; i--)
1641 pnode = lookup_type_node_I (node->supers[i]);
1642 if (pnode->data->instance.instance_init)
1644 instance->g_class = pnode->data->instance.class;
1645 pnode->data->instance.instance_init (instance, class);
1648 if (node->data->instance.private_size)
1649 instance_real_class_remove (instance);
1651 instance->g_class = class;
1652 if (node->data->instance.instance_init)
1653 node->data->instance.instance_init (instance, class);
1659 * g_type_free_instance:
1660 * @instance: an instance of a type.
1662 * Frees an instance of a type, returning it to the instance pool for the type,
1665 * Like g_type_create_instance(), this function is reserved for implementors of
1666 * fundamental types.
1669 g_type_free_instance (GTypeInstance *instance)
1674 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1676 class = instance->g_class;
1677 node = lookup_type_node_I (class->g_type);
1678 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1680 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1681 type_descriptive_name_I (class->g_type));
1684 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1685 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1687 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1692 instance->g_class = NULL;
1693 #ifdef G_ENABLE_DEBUG
1694 memset (instance, 0xaa, type_total_instance_size_I (node));
1696 g_slice_free1 (type_total_instance_size_I (node), instance);
1698 g_type_class_unref (class);
1702 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1704 g_assert (iface->data);
1706 if (!iface->data->iface.dflt_vtable)
1708 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1709 iface->data->iface.dflt_vtable = vtable;
1710 vtable->g_type = NODE_TYPE (iface);
1711 vtable->g_instance_type = 0;
1712 if (iface->data->iface.vtable_init_base ||
1713 iface->data->iface.dflt_init)
1715 G_WRITE_UNLOCK (&type_rw_lock);
1716 if (iface->data->iface.vtable_init_base)
1717 iface->data->iface.vtable_init_base (vtable);
1718 if (iface->data->iface.dflt_init)
1719 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1720 G_WRITE_LOCK (&type_rw_lock);
1726 /* This is called to allocate and do the first part of initializing
1727 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1729 * A FALSE return indicates that we didn't find an init function for
1730 * this type/iface pair, so the vtable from the parent type should
1731 * be used. Note that the write lock is not modified upon a FALSE
1735 type_iface_vtable_base_init_Wm (TypeNode *iface,
1739 IFaceHolder *iholder;
1740 GTypeInterface *vtable = NULL;
1743 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1744 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1746 return FALSE; /* we don't modify write lock upon FALSE */
1748 type_iface_ensure_dflt_vtable_Wm (iface);
1750 entry = type_lookup_iface_entry_L (node, iface);
1752 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1754 entry->init_state = IFACE_INIT;
1756 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1757 if (pnode) /* want to copy over parent iface contents */
1759 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1762 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1765 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1766 entry->vtable = vtable;
1767 vtable->g_type = NODE_TYPE (iface);
1768 vtable->g_instance_type = NODE_TYPE (node);
1770 if (iface->data->iface.vtable_init_base)
1772 G_WRITE_UNLOCK (&type_rw_lock);
1773 iface->data->iface.vtable_init_base (vtable);
1774 G_WRITE_LOCK (&type_rw_lock);
1776 return TRUE; /* initialized the vtable */
1779 /* Finishes what type_iface_vtable_base_init_Wm started by
1780 * calling the interface init function.
1781 * this function may only be called for types with their
1782 * own interface holder info, i.e. types for which
1783 * g_type_add_interface*() was called and not children thereof.
1786 type_iface_vtable_iface_init_Wm (TypeNode *iface,
1789 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1790 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
1791 GTypeInterface *vtable = NULL;
1794 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
1795 g_assert (iface->data && entry && iholder && iholder->info);
1796 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
1798 entry->init_state = INITIALIZED;
1800 vtable = entry->vtable;
1802 if (iholder->info->interface_init)
1804 G_WRITE_UNLOCK (&type_rw_lock);
1805 if (iholder->info->interface_init)
1806 iholder->info->interface_init (vtable, iholder->info->interface_data);
1807 G_WRITE_LOCK (&type_rw_lock);
1810 for (i = 0; i < static_n_iface_check_funcs; i++)
1812 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
1813 gpointer check_data = static_iface_check_funcs[i].check_data;
1815 G_WRITE_UNLOCK (&type_rw_lock);
1816 check_func (check_data, (gpointer)vtable);
1817 G_WRITE_LOCK (&type_rw_lock);
1822 type_iface_vtable_finalize_Wm (TypeNode *iface,
1824 GTypeInterface *vtable)
1826 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1827 IFaceHolder *iholder;
1829 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1830 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1832 return FALSE; /* we don't modify write lock upon FALSE */
1834 g_assert (entry && entry->vtable == vtable && iholder->info);
1836 entry->vtable = NULL;
1837 entry->init_state = UNINITIALIZED;
1838 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1840 G_WRITE_UNLOCK (&type_rw_lock);
1841 if (iholder->info->interface_finalize)
1842 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1843 if (iface->data->iface.vtable_finalize_base)
1844 iface->data->iface.vtable_finalize_base (vtable);
1845 G_WRITE_LOCK (&type_rw_lock);
1848 vtable->g_instance_type = 0;
1851 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1853 return TRUE; /* write lock modified */
1857 type_class_init_Wm (TypeNode *node,
1860 GSList *slist, *init_slist = NULL;
1863 TypeNode *bnode, *pnode;
1866 g_assert (node->is_classed && node->data &&
1867 node->data->class.class_size &&
1868 !node->data->class.class &&
1869 node->data->class.init_state == UNINITIALIZED);
1871 class = g_malloc0 (node->data->class.class_size);
1872 node->data->class.class = class;
1873 node->data->class.init_state = BASE_CLASS_INIT;
1877 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1879 memcpy (class, pclass, pnode->data->class.class_size);
1881 if (node->is_instantiatable)
1883 /* We need to initialize the private_size here rather than in
1884 * type_data_make_W() since the class init for the parent
1885 * class may have changed pnode->data->instance.private_size.
1887 node->data->instance.private_size = pnode->data->instance.private_size;
1890 class->g_type = NODE_TYPE (node);
1892 G_WRITE_UNLOCK (&type_rw_lock);
1894 /* stack all base class initialization functions, so we
1895 * call them in ascending order.
1897 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1898 if (bnode->data->class.class_init_base)
1899 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1900 for (slist = init_slist; slist; slist = slist->next)
1902 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1904 class_init_base (class);
1906 g_slist_free (init_slist);
1908 G_WRITE_LOCK (&type_rw_lock);
1910 node->data->class.init_state = BASE_IFACE_INIT;
1912 /* Before we initialize the class, base initialize all interfaces, either
1913 * from parent, or through our holder info
1915 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1918 while (i < CLASSED_NODE_N_IFACES (node))
1920 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1921 while (i < CLASSED_NODE_N_IFACES (node) &&
1922 entry->init_state == IFACE_INIT)
1928 if (i == CLASSED_NODE_N_IFACES (node))
1931 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
1935 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
1936 * doesn't modify write lock upon FALSE, so entry is still valid;
1938 g_assert (pnode != NULL);
1940 for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1942 IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1944 if (pentry->iface_type == entry->iface_type)
1946 entry->vtable = pentry->vtable;
1947 entry->init_state = INITIALIZED;
1951 g_assert (entry->vtable != NULL);
1954 /* If the write lock was released, additional interface entries might
1955 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
1956 * be base-initialized when inserted, so we don't have to worry that
1957 * we might miss them. Uninitialized entries can only be moved higher
1958 * when new ones are inserted.
1963 node->data->class.init_state = CLASS_INIT;
1965 G_WRITE_UNLOCK (&type_rw_lock);
1967 if (node->data->class.class_init)
1968 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1970 G_WRITE_LOCK (&type_rw_lock);
1972 node->data->class.init_state = IFACE_INIT;
1974 /* finish initializing the interfaces through our holder info.
1975 * inherited interfaces are already init_state == INITIALIZED, because
1976 * they either got setup in the above base_init loop, or during
1977 * class_init from within type_add_interface_Wm() for this or
1978 * an anchestor type.
1983 entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1984 while (i < CLASSED_NODE_N_IFACES (node) &&
1985 entry->init_state == INITIALIZED)
1991 if (i == CLASSED_NODE_N_IFACES (node))
1994 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
1996 /* As in the loop above, additional initialized entries might be inserted
1997 * if the write lock is released, but that's harmless because the entries
1998 * we need to initialize only move higher in the list.
2003 node->data->class.init_state = INITIALIZED;
2007 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2011 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
2014 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2016 IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
2019 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2021 /* refetch entries, IFACES_ENTRIES might be modified */
2026 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2027 * iface vtable came from parent
2029 entry->vtable = NULL;
2030 entry->init_state = UNINITIALIZED;
2037 type_data_finalize_class_U (TypeNode *node,
2040 GTypeClass *class = cdata->class;
2043 g_assert (cdata->class && cdata->common.ref_count == 0);
2045 if (cdata->class_finalize)
2046 cdata->class_finalize (class, (gpointer) cdata->class_data);
2048 /* call all base class destruction functions in descending order
2050 if (cdata->class_finalize_base)
2051 cdata->class_finalize_base (class);
2052 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2053 if (bnode->data->class.class_finalize_base)
2054 bnode->data->class.class_finalize_base (class);
2056 g_free (cdata->class);
2060 type_data_last_unref_Wm (GType type,
2063 TypeNode *node = lookup_type_node_I (type);
2065 g_return_if_fail (node != NULL && node->plugin != NULL);
2067 if (!node->data || node->data->common.ref_count == 0)
2069 g_warning ("cannot drop last reference to unreferenced type `%s'",
2070 type_descriptive_name_I (type));
2074 /* call class cache hooks */
2075 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2079 G_WRITE_UNLOCK (&type_rw_lock);
2080 G_READ_LOCK (&type_rw_lock);
2081 for (i = 0; i < static_n_class_cache_funcs; i++)
2083 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2084 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2085 gboolean need_break;
2087 G_READ_UNLOCK (&type_rw_lock);
2088 need_break = cache_func (cache_data, node->data->class.class);
2089 G_READ_LOCK (&type_rw_lock);
2090 if (!node->data || node->data->common.ref_count == 0)
2091 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2095 G_READ_UNLOCK (&type_rw_lock);
2096 G_WRITE_LOCK (&type_rw_lock);
2099 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
2100 node->data->common.ref_count -= 1;
2103 GType ptype = NODE_PARENT_TYPE (node);
2106 node->data->common.ref_count = 0;
2108 if (node->is_instantiatable)
2110 /* destroy node->data->instance.mem_chunk */
2114 if (node->is_classed && tdata->class.class)
2116 if (CLASSED_NODE_N_IFACES (node))
2117 type_data_finalize_class_ifaces_Wm (node);
2118 node->mutatable_check_cache = FALSE;
2120 G_WRITE_UNLOCK (&type_rw_lock);
2121 type_data_finalize_class_U (node, &tdata->class);
2122 G_WRITE_LOCK (&type_rw_lock);
2124 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2126 node->mutatable_check_cache = FALSE;
2128 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2130 G_WRITE_UNLOCK (&type_rw_lock);
2131 if (tdata->iface.dflt_finalize)
2132 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2133 if (tdata->iface.vtable_finalize_base)
2134 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2135 G_WRITE_LOCK (&type_rw_lock);
2137 g_free (tdata->iface.dflt_vtable);
2141 node->mutatable_check_cache = FALSE;
2145 /* freeing tdata->common.value_table and its contents is taken care of
2146 * by allocating it in one chunk with tdata
2150 G_WRITE_UNLOCK (&type_rw_lock);
2151 g_type_plugin_unuse (node->plugin);
2152 G_WRITE_LOCK (&type_rw_lock);
2154 type_data_unref_WmREC (lookup_type_node_I (ptype), FALSE);
2159 * g_type_add_class_cache_func:
2160 * @cache_data: data to be passed to @cache_func
2161 * @cache_func: a #GTypeClassCacheFunc
2163 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2164 * class goes from one to zero. This can be used to prevent premature class
2165 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2166 * until one of them returns %TRUE. The functions have to check the class id
2167 * passed in to figure whether they actually want to cache the class of this
2168 * type, since all classes are routed through the same #GTypeClassCacheFunc
2172 g_type_add_class_cache_func (gpointer cache_data,
2173 GTypeClassCacheFunc cache_func)
2177 g_return_if_fail (cache_func != NULL);
2179 G_WRITE_LOCK (&type_rw_lock);
2180 i = static_n_class_cache_funcs++;
2181 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2182 static_class_cache_funcs[i].cache_data = cache_data;
2183 static_class_cache_funcs[i].cache_func = cache_func;
2184 G_WRITE_UNLOCK (&type_rw_lock);
2188 * g_type_remove_class_cache_func:
2189 * @cache_data: data that was given when adding @cache_func
2190 * @cache_func: a #GTypeClassCacheFunc
2192 * Removes a previously installed #GTypeClassCacheFunc. The cache maintained
2193 * by @cache_func has to be empty when calling g_type_remove_class_cache_func()
2197 g_type_remove_class_cache_func (gpointer cache_data,
2198 GTypeClassCacheFunc cache_func)
2200 gboolean found_it = FALSE;
2203 g_return_if_fail (cache_func != NULL);
2205 G_WRITE_LOCK (&type_rw_lock);
2206 for (i = 0; i < static_n_class_cache_funcs; i++)
2207 if (static_class_cache_funcs[i].cache_data == cache_data &&
2208 static_class_cache_funcs[i].cache_func == cache_func)
2210 static_n_class_cache_funcs--;
2211 g_memmove (static_class_cache_funcs + i,
2212 static_class_cache_funcs + i + 1,
2213 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2214 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2218 G_WRITE_UNLOCK (&type_rw_lock);
2221 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2222 cache_func, cache_data);
2227 * g_type_add_interface_check:
2228 * @check_data: data to pass to @check_func
2229 * @check_func: function to be called after each interface
2232 * Adds a function to be called after an interface vtable is
2233 * initialized for any class (i.e. after the @interface_init
2234 * member of #GInterfaceInfo has been called).
2236 * This function is useful when you want to check an invariant
2237 * that depends on the interfaces of a class. For instance,
2238 * the implementation of #GObject uses this facility to check
2239 * that an object implements all of the properties that are
2240 * defined on its interfaces.
2245 g_type_add_interface_check (gpointer check_data,
2246 GTypeInterfaceCheckFunc check_func)
2250 g_return_if_fail (check_func != NULL);
2252 G_WRITE_LOCK (&type_rw_lock);
2253 i = static_n_iface_check_funcs++;
2254 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2255 static_iface_check_funcs[i].check_data = check_data;
2256 static_iface_check_funcs[i].check_func = check_func;
2257 G_WRITE_UNLOCK (&type_rw_lock);
2261 * g_type_remove_interface_check:
2262 * @check_data: callback data passed to g_type_add_interface_check()
2263 * @check_func: callback function passed to g_type_add_interface_check()
2265 * Removes an interface check function added with
2266 * g_type_add_interface_check().
2271 g_type_remove_interface_check (gpointer check_data,
2272 GTypeInterfaceCheckFunc check_func)
2274 gboolean found_it = FALSE;
2277 g_return_if_fail (check_func != NULL);
2279 G_WRITE_LOCK (&type_rw_lock);
2280 for (i = 0; i < static_n_iface_check_funcs; i++)
2281 if (static_iface_check_funcs[i].check_data == check_data &&
2282 static_iface_check_funcs[i].check_func == check_func)
2284 static_n_iface_check_funcs--;
2285 g_memmove (static_iface_check_funcs + i,
2286 static_iface_check_funcs + i + 1,
2287 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2288 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2292 G_WRITE_UNLOCK (&type_rw_lock);
2295 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2296 check_func, check_data);
2299 /* --- type registration --- */
2301 * g_type_register_fundamental:
2302 * @type_id: A predefined type identifier.
2303 * @type_name: 0-terminated string used as the name of the new type.
2304 * @info: The #GTypeInfo structure for this type.
2305 * @finfo: The #GTypeFundamentalInfo structure for this type.
2306 * @flags: Bitwise combination of #GTypeFlags values.
2308 * Registers @type_id as the predefined identifier and @type_name as the
2309 * name of a fundamental type. The type system uses the information
2310 * contained in the #GTypeInfo structure pointed to by @info and the
2311 * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
2312 * type and its instances. The value of @flags determines additional
2313 * characteristics of the fundamental type.
2315 * Returns: The predefined type identifier.
2318 g_type_register_fundamental (GType type_id,
2319 const gchar *type_name,
2320 const GTypeInfo *info,
2321 const GTypeFundamentalInfo *finfo,
2326 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2327 g_return_val_if_fail (type_id > 0, 0);
2328 g_return_val_if_fail (type_name != NULL, 0);
2329 g_return_val_if_fail (info != NULL, 0);
2330 g_return_val_if_fail (finfo != NULL, 0);
2332 if (!check_type_name_I (type_name))
2334 if ((type_id & TYPE_ID_MASK) ||
2335 type_id > G_TYPE_FUNDAMENTAL_MAX)
2337 g_warning ("attempt to register fundamental type `%s' with invalid type id (%zu)",
2342 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2343 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2345 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2349 if (lookup_type_node_I (type_id))
2351 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2352 type_descriptive_name_I (type_id),
2357 G_WRITE_LOCK (&type_rw_lock);
2358 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2359 type_add_flags_W (node, flags);
2361 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2362 type_data_make_W (node, info,
2363 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2364 G_WRITE_UNLOCK (&type_rw_lock);
2366 return NODE_TYPE (node);
2370 * g_type_register_static_simple:
2371 * @parent_type: Type from which this type will be derived.
2372 * @type_name: 0-terminated string used as the name of the new type.
2373 * @class_size: Size of the class structure (see #GTypeInfo)
2374 * @class_init: Location of the class initialization function (see #GTypeInfo)
2375 * @instance_size: Size of the instance structure (see #GTypeInfo)
2376 * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2377 * @flags: Bitwise combination of #GTypeFlags values.
2379 * Registers @type_name as the name of a new static type derived from
2380 * @parent_type. The value of @flags determines the nature (e.g.
2381 * abstract or not) of the type. It works by filling a #GTypeInfo
2382 * struct and calling g_type_register_static().
2385 * Returns: The new type identifier.
2388 g_type_register_static_simple (GType parent_type,
2389 const gchar *type_name,
2391 GClassInitFunc class_init,
2392 guint instance_size,
2393 GInstanceInitFunc instance_init,
2398 info.class_size = class_size;
2399 info.base_init = NULL;
2400 info.base_finalize = NULL;
2401 info.class_init = class_init;
2402 info.class_finalize = NULL;
2403 info.class_data = NULL;
2404 info.instance_size = instance_size;
2405 info.n_preallocs = 0;
2406 info.instance_init = instance_init;
2407 info.value_table = NULL;
2409 return g_type_register_static (parent_type, type_name, &info, flags);
2413 * g_type_register_static:
2414 * @parent_type: Type from which this type will be derived.
2415 * @type_name: 0-terminated string used as the name of the new type.
2416 * @info: The #GTypeInfo structure for this type.
2417 * @flags: Bitwise combination of #GTypeFlags values.
2419 * Registers @type_name as the name of a new static type derived from
2420 * @parent_type. The type system uses the information contained in the
2421 * #GTypeInfo structure pointed to by @info to manage the type and its
2422 * instances (if not abstract). The value of @flags determines the nature
2423 * (e.g. abstract or not) of the type.
2425 * Returns: The new type identifier.
2428 g_type_register_static (GType parent_type,
2429 const gchar *type_name,
2430 const GTypeInfo *info,
2433 TypeNode *pnode, *node;
2436 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2437 g_return_val_if_fail (parent_type > 0, 0);
2438 g_return_val_if_fail (type_name != NULL, 0);
2439 g_return_val_if_fail (info != NULL, 0);
2441 if (!check_type_name_I (type_name) ||
2442 !check_derivation_I (parent_type, type_name))
2444 if (info->class_finalize)
2446 g_warning ("class finalizer specified for static type `%s'",
2451 pnode = lookup_type_node_I (parent_type);
2452 G_WRITE_LOCK (&type_rw_lock);
2453 type_data_ref_Wm (pnode);
2454 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2456 node = type_node_new_W (pnode, type_name, NULL);
2457 type_add_flags_W (node, flags);
2458 type = NODE_TYPE (node);
2459 type_data_make_W (node, info,
2460 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2462 G_WRITE_UNLOCK (&type_rw_lock);
2468 * g_type_register_dynamic:
2469 * @parent_type: Type from which this type will be derived.
2470 * @type_name: 0-terminated string used as the name of the new type.
2471 * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2472 * @flags: Bitwise combination of #GTypeFlags values.
2474 * Registers @type_name as the name of a new dynamic type derived from
2475 * @parent_type. The type system uses the information contained in the
2476 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2477 * instances (if not abstract). The value of @flags determines the nature
2478 * (e.g. abstract or not) of the type.
2480 * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2483 g_type_register_dynamic (GType parent_type,
2484 const gchar *type_name,
2485 GTypePlugin *plugin,
2488 TypeNode *pnode, *node;
2491 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2492 g_return_val_if_fail (parent_type > 0, 0);
2493 g_return_val_if_fail (type_name != NULL, 0);
2494 g_return_val_if_fail (plugin != NULL, 0);
2496 if (!check_type_name_I (type_name) ||
2497 !check_derivation_I (parent_type, type_name) ||
2498 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2501 G_WRITE_LOCK (&type_rw_lock);
2502 pnode = lookup_type_node_I (parent_type);
2503 node = type_node_new_W (pnode, type_name, plugin);
2504 type_add_flags_W (node, flags);
2505 type = NODE_TYPE (node);
2506 G_WRITE_UNLOCK (&type_rw_lock);
2512 * g_type_add_interface_static:
2513 * @instance_type: #GType value of an instantiable type.
2514 * @interface_type: #GType value of an interface type.
2515 * @info: The #GInterfaceInfo structure for this
2516 * (@instance_type, @interface_type) combination.
2518 * Adds the static @interface_type to @instantiable_type. The information
2519 * contained in the #GTypeInterfaceInfo structure pointed to by @info
2520 * is used to manage the relationship.
2523 g_type_add_interface_static (GType instance_type,
2524 GType interface_type,
2525 const GInterfaceInfo *info)
2527 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2528 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2529 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2531 /* we only need to lock class_init_rec_mutex if instance_type already has its
2532 * class initialized, however this function is rarely enough called to take
2533 * the simple route and always acquire class_init_rec_mutex.
2535 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2536 G_WRITE_LOCK (&type_rw_lock);
2537 if (check_add_interface_L (instance_type, interface_type))
2539 TypeNode *node = lookup_type_node_I (instance_type);
2540 TypeNode *iface = lookup_type_node_I (interface_type);
2541 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2542 type_add_interface_Wm (node, iface, info, NULL);
2544 G_WRITE_UNLOCK (&type_rw_lock);
2545 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2549 * g_type_add_interface_dynamic:
2550 * @instance_type: the #GType value of an instantiable type.
2551 * @interface_type: the #GType value of an interface type.
2552 * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2554 * Adds the dynamic @interface_type to @instantiable_type. The information
2555 * contained in the #GTypePlugin structure pointed to by @plugin
2556 * is used to manage the relationship.
2559 g_type_add_interface_dynamic (GType instance_type,
2560 GType interface_type,
2561 GTypePlugin *plugin)
2564 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2565 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2566 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2568 node = lookup_type_node_I (instance_type);
2569 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2572 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2573 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2574 G_WRITE_LOCK (&type_rw_lock);
2575 if (check_add_interface_L (instance_type, interface_type))
2577 TypeNode *iface = lookup_type_node_I (interface_type);
2578 type_add_interface_Wm (node, iface, NULL, plugin);
2580 G_WRITE_UNLOCK (&type_rw_lock);
2581 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2585 /* --- public API functions --- */
2588 * @type: Type ID of a classed type.
2590 * Increments the reference count of the class structure belonging to
2591 * @type. This function will demand-create the class if it doesn't
2594 * Returns: The #GTypeClass structure for the given type ID.
2597 g_type_class_ref (GType type)
2602 /* optimize for common code path */
2603 G_WRITE_LOCK (&type_rw_lock);
2604 node = lookup_type_node_I (type);
2605 if (node && node->is_classed && node->data &&
2606 node->data->class.class &&
2607 node->data->class.init_state == INITIALIZED)
2609 type_data_ref_Wm (node);
2610 G_WRITE_UNLOCK (&type_rw_lock);
2611 return node->data->class.class;
2613 if (!node || !node->is_classed ||
2614 (node->data && node->data->common.ref_count < 1))
2616 G_WRITE_UNLOCK (&type_rw_lock);
2617 g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2618 type_descriptive_name_I (type));
2621 type_data_ref_Wm (node);
2622 ptype = NODE_PARENT_TYPE (node);
2623 G_WRITE_UNLOCK (&type_rw_lock);
2625 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2626 /* here, we either have node->data->class.class == NULL, or a recursive
2627 * call to g_type_class_ref() with a partly initialized class, or
2628 * node->data->class.init_state == INITIALIZED, because any
2629 * concurrently running initialization was guarded by class_init_rec_mutex.
2631 if (!node->data->class.class) /* class uninitialized */
2633 /* acquire reference on parent class */
2634 GTypeClass *pclass = ptype ? g_type_class_ref (ptype) : NULL;
2635 G_WRITE_LOCK (&type_rw_lock);
2636 if (node->data->class.class) /* class was initialized during parent class initialization? */
2637 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2638 type_class_init_Wm (node, pclass);
2639 G_WRITE_UNLOCK (&type_rw_lock);
2641 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2643 return node->data->class.class;
2647 * g_type_class_unref:
2648 * @g_class: The #GTypeClass structure to unreference.
2650 * Decrements the reference count of the class structure being passed in.
2651 * Once the last reference count of a class has been released, classes
2652 * may be finalized by the type system, so further dereferencing of a
2653 * class pointer after g_type_class_unref() are invalid.
2656 g_type_class_unref (gpointer g_class)
2659 GTypeClass *class = g_class;
2661 g_return_if_fail (g_class != NULL);
2663 node = lookup_type_node_I (class->g_type);
2664 G_WRITE_LOCK (&type_rw_lock);
2665 if (node && node->is_classed && node->data &&
2666 node->data->class.class == class && node->data->common.ref_count > 0)
2667 type_data_unref_WmREC (node, FALSE);
2669 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2670 type_descriptive_name_I (class->g_type));
2671 G_WRITE_UNLOCK (&type_rw_lock);
2675 * g_type_class_unref_uncached:
2676 * @g_class: The #GTypeClass structure to unreference.
2678 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2679 * implementations. It unreferences a class without consulting the chain
2680 * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2684 g_type_class_unref_uncached (gpointer g_class)
2687 GTypeClass *class = g_class;
2689 g_return_if_fail (g_class != NULL);
2691 G_WRITE_LOCK (&type_rw_lock);
2692 node = lookup_type_node_I (class->g_type);
2693 if (node && node->is_classed && node->data &&
2694 node->data->class.class == class && node->data->common.ref_count > 0)
2695 type_data_unref_WmREC (node, TRUE);
2697 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2698 type_descriptive_name_I (class->g_type));
2699 G_WRITE_UNLOCK (&type_rw_lock);
2703 * g_type_class_peek:
2704 * @type: Type ID of a classed type.
2706 * This function is essentially the same as g_type_class_ref(), except that
2707 * the classes reference count isn't incremented. As a consequence, this function
2708 * may return %NULL if the class of the type passed in does not currently
2709 * exist (hasn't been referenced before).
2711 * Returns: The #GTypeClass structure for the given type ID or %NULL
2712 * if the class does not currently exist.
2715 g_type_class_peek (GType type)
2720 node = lookup_type_node_I (type);
2721 G_READ_LOCK (&type_rw_lock);
2722 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2723 class = node->data->class.class;
2726 G_READ_UNLOCK (&type_rw_lock);
2732 * g_type_class_peek_static:
2733 * @type: Type ID of a classed type.
2735 * A more efficient version of g_type_class_peek() which works only for
2739 * Returns: The #GTypeClass structure for the given type ID or %NULL
2740 * if the class does not currently exist or is dynamically loaded.
2743 g_type_class_peek_static (GType type)
2748 node = lookup_type_node_I (type);
2749 G_READ_LOCK (&type_rw_lock);
2750 if (node && node->is_classed && node->data &&
2751 /* peek only static types: */ node->plugin == NULL &&
2752 node->data->class.class) /* common.ref_count _may_ be 0 */
2753 class = node->data->class.class;
2756 G_READ_UNLOCK (&type_rw_lock);
2762 * g_type_class_peek_parent:
2763 * @g_class: The #GTypeClass structure to retrieve the parent class for.
2765 * This is a convenience function often needed in class initializers.
2766 * It returns the class structure of the immediate parent type of the class passed in.
2767 * Since derived classes hold
2768 * a reference count on their parent classes as long as they are instantiated,
2769 * the returned class will always exist. This function is essentially
2773 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
2776 * Returns: The parent class of @g_class.
2779 g_type_class_peek_parent (gpointer g_class)
2782 gpointer class = NULL;
2784 g_return_val_if_fail (g_class != NULL, NULL);
2786 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2787 /* We used to acquire a read lock here. That is not necessary, since
2788 * parent->data->class.class is constant as long as the derived class
2791 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2793 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2794 class = node->data->class.class;
2796 else if (NODE_PARENT_TYPE (node))
2797 g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2803 * g_type_interface_peek:
2804 * @instance_class: A #GTypeClass structure.
2805 * @iface_type: An interface ID which this class conforms to.
2807 * Returns the #GTypeInterface structure of an interface to which the passed in
2810 * Returns: The GTypeInterface structure of iface_type if implemented
2811 * by @instance_class, %NULL otherwise
2814 g_type_interface_peek (gpointer instance_class,
2819 gpointer vtable = NULL;
2820 GTypeClass *class = instance_class;
2822 g_return_val_if_fail (instance_class != NULL, NULL);
2824 node = lookup_type_node_I (class->g_type);
2825 iface = lookup_type_node_I (iface_type);
2826 if (node && node->is_instantiatable && iface)
2830 G_READ_LOCK (&type_rw_lock);
2832 entry = type_lookup_iface_entry_L (node, iface);
2833 if (entry && entry->vtable) /* entry is relocatable */
2834 vtable = entry->vtable;
2836 G_READ_UNLOCK (&type_rw_lock);
2839 g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2845 * g_type_interface_peek_parent:
2846 * @g_iface: A #GTypeInterface structure.
2848 * Returns the corresponding #GTypeInterface structure of the parent type
2849 * of the instance type to which @g_iface belongs. This is useful when
2850 * deriving the implementation of an interface from the parent type and
2851 * then possibly overriding some methods.
2853 * Returns: The corresponding #GTypeInterface structure of the parent type
2854 * of the instance type to which @g_iface belongs, or %NULL if the parent type
2855 * doesn't conform to the interface.
2858 g_type_interface_peek_parent (gpointer g_iface)
2862 gpointer vtable = NULL;
2863 GTypeInterface *iface_class = g_iface;
2865 g_return_val_if_fail (g_iface != NULL, NULL);
2867 iface = lookup_type_node_I (iface_class->g_type);
2868 node = lookup_type_node_I (iface_class->g_instance_type);
2870 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2871 if (node && node->is_instantiatable && iface)
2875 G_READ_LOCK (&type_rw_lock);
2877 entry = type_lookup_iface_entry_L (node, iface);
2878 if (entry && entry->vtable) /* entry is relocatable */
2879 vtable = entry->vtable;
2881 G_READ_UNLOCK (&type_rw_lock);
2884 g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2890 * g_type_default_interface_ref:
2891 * @g_type: an interface type
2893 * Increments the reference count for the interface type @g_type,
2894 * and returns the default interface vtable for the type.
2896 * If the type is not currently in use, then the default vtable
2897 * for the type will be created and initalized by calling
2898 * the base interface init and default vtable init functions for
2899 * the type (the @<structfield>base_init</structfield>
2900 * and <structfield>class_init</structfield> members of #GTypeInfo).
2901 * Calling g_type_default_interface_ref() is useful when you
2902 * want to make sure that signals and properties for an interface
2903 * have been installed.
2906 * Returns: the default vtable for the interface; call
2907 * g_type_default_interface_unref() when you are done using
2911 g_type_default_interface_ref (GType g_type)
2914 gpointer dflt_vtable;
2916 G_WRITE_LOCK (&type_rw_lock);
2918 node = lookup_type_node_I (g_type);
2919 if (!node || !NODE_IS_IFACE (node) ||
2920 (node->data && node->data->common.ref_count < 1))
2922 G_WRITE_UNLOCK (&type_rw_lock);
2923 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
2924 type_descriptive_name_I (g_type));
2928 if (!node->data || !node->data->iface.dflt_vtable)
2930 G_WRITE_UNLOCK (&type_rw_lock);
2931 g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2932 G_WRITE_LOCK (&type_rw_lock);
2933 node = lookup_type_node_I (g_type);
2934 type_data_ref_Wm (node);
2935 type_iface_ensure_dflt_vtable_Wm (node);
2936 g_static_rec_mutex_unlock (&class_init_rec_mutex);
2939 type_data_ref_Wm (node); /* ref_count >= 1 already */
2941 dflt_vtable = node->data->iface.dflt_vtable;
2942 G_WRITE_UNLOCK (&type_rw_lock);
2948 * g_type_default_interface_peek:
2949 * @g_type: an interface type
2951 * If the interface type @g_type is currently in use, returns
2952 * its default interface vtable.
2955 * Returns: the default vtable for the interface, or %NULL
2956 * if the type is not currently in use.
2959 g_type_default_interface_peek (GType g_type)
2964 node = lookup_type_node_I (g_type);
2965 G_READ_LOCK (&type_rw_lock);
2966 if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
2967 vtable = node->data->iface.dflt_vtable;
2970 G_READ_UNLOCK (&type_rw_lock);
2976 * g_type_default_interface_unref:
2977 * @g_iface: the default vtable structure for a interface, as
2978 * returned by g_type_default_interface_ref()
2980 * Decrements the reference count for the type corresponding to the
2981 * interface default vtable @g_iface. If the type is dynamic, then
2982 * when no one is using the interface and all references have
2983 * been released, the finalize function for the interface's default
2984 * vtable (the <structfield>class_finalize</structfield> member of
2985 * #GTypeInfo) will be called.
2990 g_type_default_interface_unref (gpointer g_iface)
2993 GTypeInterface *vtable = g_iface;
2995 g_return_if_fail (g_iface != NULL);
2997 node = lookup_type_node_I (vtable->g_type);
2998 G_WRITE_LOCK (&type_rw_lock);
2999 if (node && NODE_IS_IFACE (node) &&
3000 node->data->iface.dflt_vtable == g_iface &&
3001 node->data->common.ref_count > 0)
3002 type_data_unref_WmREC (node, FALSE);
3004 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3005 type_descriptive_name_I (vtable->g_type));
3006 G_WRITE_UNLOCK (&type_rw_lock);
3011 * @type: Type to return name for.
3013 * Get the unique name that is assigned to a type ID.
3014 * Note that this function (like all other GType API) cannot cope with invalid
3015 * type IDs. %G_TYPE_INVALID may be passed to this function, as may be any other
3016 * validly registered type ID, but randomized type IDs should not be passed in and
3017 * will most likely lead to a crash.
3019 * Returns: Static type name or %NULL.
3021 G_CONST_RETURN gchar*
3022 g_type_name (GType type)
3026 g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
3028 node = lookup_type_node_I (type);
3030 return node ? NODE_NAME (node) : NULL;
3035 * @type: Type to return quark of type name for.
3037 * Get the corresponding quark of the type IDs name.
3039 * Returns: The type names quark or 0.
3042 g_type_qname (GType type)
3046 node = lookup_type_node_I (type);
3048 return node ? node->qname : 0;
3053 * @name: Type name to lookup.
3055 * Lookup the type ID from a given type name, returning 0 if no type has been registered under this name
3056 * (this is the preferred method to find out by name whether a specific type has been registered yet).
3058 * Returns: Corresponding type ID or 0.
3061 g_type_from_name (const gchar *name)
3066 g_return_val_if_fail (name != NULL, 0);
3068 quark = g_quark_try_string (name);
3071 G_READ_LOCK (&type_rw_lock);
3072 type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
3073 G_READ_UNLOCK (&type_rw_lock);
3081 * @type: The derived type.
3083 * Return the direct parent type of the passed in type.
3084 * If the passed in type has no parent, i.e. is a fundamental type, 0 is returned.
3086 * Returns: The parent type.
3089 g_type_parent (GType type)
3093 node = lookup_type_node_I (type);
3095 return node ? NODE_PARENT_TYPE (node) : 0;
3100 * @type: A #GType value.
3102 * Returns the length of the ancestry of the passed in type. This includes the
3103 * type itself, so that e.g. a fundamental type has depth 1.
3105 * Returns: The depth of @type.
3108 g_type_depth (GType type)
3112 node = lookup_type_node_I (type);
3114 return node ? node->n_supers + 1 : 0;
3119 * @leaf_type: Descendant of @root_type and the type to be returned.
3120 * @root_type: Immediate parent of the returned type.
3122 * Given a @leaf_type and a @root_type which is contained in its anchestry, return
3123 * the type that @root_type is the immediate parent of.
3124 * In other words, this function determines the type that is derived directly from
3125 * @root_type which is also a base class of @leaf_type. Given a root type and a
3126 * leaf type, this function can be used to determine the types and order in which
3127 * the leaf type is descended from the root type.
3129 * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3132 g_type_next_base (GType type,
3138 node = lookup_type_node_I (type);
3141 TypeNode *base_node = lookup_type_node_I (base_type);
3143 if (base_node && base_node->n_supers < node->n_supers)
3145 guint n = node->n_supers - base_node->n_supers;
3147 if (node->supers[n] == base_type)
3148 atype = node->supers[n - 1];
3155 static inline gboolean
3156 type_node_check_conformities_UorL (TypeNode *node,
3157 TypeNode *iface_node,
3158 /* support_inheritance */
3159 gboolean support_interfaces,
3160 gboolean support_prerequisites,
3165 if (/* support_inheritance && */
3166 NODE_IS_ANCESTOR (iface_node, node))
3169 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3170 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3172 if (support_interfaces || support_prerequisites)
3175 G_READ_LOCK (&type_rw_lock);
3176 if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
3178 else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3181 G_READ_UNLOCK (&type_rw_lock);
3187 type_node_is_a_L (TypeNode *node,
3188 TypeNode *iface_node)
3190 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3193 static inline gboolean
3194 type_node_conforms_to_U (TypeNode *node,
3195 TypeNode *iface_node,
3196 gboolean support_interfaces,
3197 gboolean support_prerequisites)
3199 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3204 * @type: Type to check anchestry for.
3205 * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3207 * If @is_a_type is a derivable type, check whether @type is a descendant of @is_a_type.
3208 * If @is_a_type is an interface, check whether @type conforms to it.
3210 * Returns: %TRUE if @type is_a @is_a_type holds true.
3213 g_type_is_a (GType type,
3216 TypeNode *node, *iface_node;
3219 node = lookup_type_node_I (type);
3220 iface_node = lookup_type_node_I (iface_type);
3221 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3228 * @type: The parent type.
3229 * @n_children: Optional #guint pointer to contain the number of child types.
3231 * Return a newly allocated and 0-terminated array of type IDs, listing the
3232 * child types of @type. The return value has to be g_free()ed after use.
3234 * Returns: Newly allocated and 0-terminated array of child types.
3237 g_type_children (GType type,
3242 node = lookup_type_node_I (type);
3247 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3248 children = g_new (GType, node->n_children + 1);
3249 memcpy (children, node->children, sizeof (GType) * node->n_children);
3250 children[node->n_children] = 0;
3253 *n_children = node->n_children;
3254 G_READ_UNLOCK (&type_rw_lock);
3268 * g_type_interfaces:
3269 * @type: The type to list interface types for.
3270 * @n_interfaces: Optional #guint pointer to contain the number of interface types.
3272 * Return a newly allocated and 0-terminated array of type IDs, listing the
3273 * interface types that @type conforms to. The return value has to be
3274 * g_free()ed after use.
3276 * Returns: Newly allocated and 0-terminated array of interface types.
3279 g_type_interfaces (GType type,
3280 guint *n_interfaces)
3284 node = lookup_type_node_I (type);
3285 if (node && node->is_instantiatable)
3290 G_READ_LOCK (&type_rw_lock);
3291 ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
3292 for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
3293 ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
3297 *n_interfaces = CLASSED_NODE_N_IFACES (node);
3298 G_READ_UNLOCK (&type_rw_lock);
3311 typedef struct _QData QData;
3323 static inline gpointer
3324 type_get_qdata_L (TypeNode *node,
3327 GData *gdata = node->global_gdata;
3329 if (quark && gdata && gdata->n_qdatas)
3331 QData *qdatas = gdata->qdatas - 1;
3332 guint n_qdatas = gdata->n_qdatas;
3339 i = (n_qdatas + 1) / 2;
3341 if (quark == check->quark)
3343 else if (quark > check->quark)
3348 else /* if (quark < check->quark) */
3359 * @quark: a #GQuark id to identify the data
3361 * Obtains data which has previously been attached to @type
3362 * with g_type_set_qdata().
3364 * Returns: the data, or %NULL if no data was found
3367 g_type_get_qdata (GType type,
3373 node = lookup_type_node_I (type);
3376 G_READ_LOCK (&type_rw_lock);
3377 data = type_get_qdata_L (node, quark);
3378 G_READ_UNLOCK (&type_rw_lock);
3382 g_return_val_if_fail (node != NULL, NULL);
3389 type_set_qdata_W (TypeNode *node,
3397 /* setup qdata list if necessary */
3398 if (!node->global_gdata)
3399 node->global_gdata = g_new0 (GData, 1);
3400 gdata = node->global_gdata;
3402 /* try resetting old data */
3403 qdata = gdata->qdatas;
3404 for (i = 0; i < gdata->n_qdatas; i++)
3405 if (qdata[i].quark == quark)
3407 qdata[i].data = data;
3413 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3414 qdata = gdata->qdatas;
3415 for (i = 0; i < gdata->n_qdatas - 1; i++)
3416 if (qdata[i].quark > quark)
3418 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3419 qdata[i].quark = quark;
3420 qdata[i].data = data;
3426 * @quark: a #GQuark id to identify the data
3429 * Attaches arbitrary data to a type.
3432 g_type_set_qdata (GType type,
3438 g_return_if_fail (quark != 0);
3440 node = lookup_type_node_I (type);
3443 G_WRITE_LOCK (&type_rw_lock);
3444 type_set_qdata_W (node, quark, data);
3445 G_WRITE_UNLOCK (&type_rw_lock);
3448 g_return_if_fail (node != NULL);
3452 type_add_flags_W (TypeNode *node,
3457 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3458 g_return_if_fail (node != NULL);
3460 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3461 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3462 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3464 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3469 * @type: the #GType value of a static, classed type.
3470 * @query: A user provided structure that is filled in with constant values
3473 * Queries the type system for information about a specific type.
3474 * This function will fill in a user-provided structure to hold type-specific
3475 * information. If an invalid #GType is passed in, the @type member of the
3476 * #GTypeQuery is 0. All members filled into the #GTypeQuery structure should
3477 * be considered constant and have to be left untouched.
3480 g_type_query (GType type,
3485 g_return_if_fail (query != NULL);
3487 /* if node is not static and classed, we won't allow query */
3489 node = lookup_type_node_I (type);
3490 if (node && node->is_classed && !node->plugin)
3492 /* type is classed and probably even instantiatable */
3493 G_READ_LOCK (&type_rw_lock);
3494 if (node->data) /* type is static or referenced */
3496 query->type = NODE_TYPE (node);
3497 query->type_name = NODE_NAME (node);
3498 query->class_size = node->data->class.class_size;
3499 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3501 G_READ_UNLOCK (&type_rw_lock);
3506 /* --- implementation details --- */
3508 g_type_test_flags (GType type,
3512 gboolean result = FALSE;
3514 node = lookup_type_node_I (type);
3517 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3518 guint tflags = flags & TYPE_FLAG_MASK;
3522 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3524 fflags = (finfo->type_flags & fflags) == fflags;
3531 G_READ_LOCK (&type_rw_lock);
3532 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3533 G_READ_UNLOCK (&type_rw_lock);
3538 result = tflags && fflags;
3545 * g_type_get_plugin:
3546 * @type: The #GType to retrieve the plugin for.
3548 * Returns the #GTypePlugin structure for @type or
3549 * %NULL if @type does not have a #GTypePlugin structure.
3551 * Returns: The corresponding plugin if @type is a dynamic type,
3555 g_type_get_plugin (GType type)
3559 node = lookup_type_node_I (type);
3561 return node ? node->plugin : NULL;
3565 * g_type_interface_get_plugin:
3566 * @instance_type: the #GType value of an instantiatable type.
3567 * @interface_type: the #GType value of an interface type.
3569 * Returns the #GTypePlugin structure for the dynamic interface
3570 * @interface_type which has been added to @instance_type, or
3571 * %NULL if @interface_type has not been added to @instance_type or does
3572 * not have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3574 * Returns: the #GTypePlugin for the dynamic interface @interface_type
3575 * of @instance_type.
3578 g_type_interface_get_plugin (GType instance_type,
3579 GType interface_type)
3584 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3586 node = lookup_type_node_I (instance_type);
3587 iface = lookup_type_node_I (interface_type);
3590 IFaceHolder *iholder;
3591 GTypePlugin *plugin;
3593 G_READ_LOCK (&type_rw_lock);
3595 iholder = iface_node_get_holders_L (iface);
3596 while (iholder && iholder->instance_type != instance_type)
3597 iholder = iholder->next;
3598 plugin = iholder ? iholder->plugin : NULL;
3600 G_READ_UNLOCK (&type_rw_lock);
3605 g_return_val_if_fail (node == NULL, NULL);
3606 g_return_val_if_fail (iface == NULL, NULL);
3608 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3614 * g_type_fundamental_next:
3616 * Returns the next free fundamental type id which can be used to
3617 * register a new fundamental type with g_type_register_fundamental().
3618 * The returned type ID represents the highest currently registered
3619 * fundamental type identifier.
3621 * Returns: The nextmost fundamental type ID to be registered,
3622 * or 0 if the type system ran out of fundamental type IDs.
3625 g_type_fundamental_next (void)
3629 G_READ_LOCK (&type_rw_lock);
3630 type = static_fundamental_next;
3631 G_READ_UNLOCK (&type_rw_lock);
3632 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3633 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3637 * g_type_fundamental:
3638 * @type_id: valid type ID
3640 * Internal function, used to extract the fundamental type ID portion.
3641 * use G_TYPE_FUNDAMENTAL() instead.
3643 * Returns: fundamental type ID
3646 g_type_fundamental (GType type_id)
3648 TypeNode *node = lookup_type_node_I (type_id);
3650 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3654 g_type_check_instance_is_a (GTypeInstance *type_instance,
3657 TypeNode *node, *iface;
3660 if (!type_instance || !type_instance->g_class)
3663 node = lookup_type_node_I (type_instance->g_class->g_type);
3664 iface = lookup_type_node_I (iface_type);
3665 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3671 g_type_check_class_is_a (GTypeClass *type_class,
3674 TypeNode *node, *iface;
3680 node = lookup_type_node_I (type_class->g_type);
3681 iface = lookup_type_node_I (is_a_type);
3682 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3688 g_type_check_instance_cast (GTypeInstance *type_instance,
3693 if (type_instance->g_class)
3695 TypeNode *node, *iface;
3696 gboolean is_instantiatable, check;
3698 node = lookup_type_node_I (type_instance->g_class->g_type);
3699 is_instantiatable = node && node->is_instantiatable;
3700 iface = lookup_type_node_I (iface_type);
3701 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3703 return type_instance;
3705 if (is_instantiatable)
3706 g_warning ("invalid cast from `%s' to `%s'",
3707 type_descriptive_name_I (type_instance->g_class->g_type),
3708 type_descriptive_name_I (iface_type));
3710 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3711 type_descriptive_name_I (type_instance->g_class->g_type),
3712 type_descriptive_name_I (iface_type));
3715 g_warning ("invalid unclassed pointer in cast to `%s'",
3716 type_descriptive_name_I (iface_type));
3719 return type_instance;
3723 g_type_check_class_cast (GTypeClass *type_class,
3728 TypeNode *node, *iface;
3729 gboolean is_classed, check;
3731 node = lookup_type_node_I (type_class->g_type);
3732 is_classed = node && node->is_classed;
3733 iface = lookup_type_node_I (is_a_type);
3734 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3739 g_warning ("invalid class cast from `%s' to `%s'",
3740 type_descriptive_name_I (type_class->g_type),
3741 type_descriptive_name_I (is_a_type));
3743 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3744 type_descriptive_name_I (type_class->g_type),
3745 type_descriptive_name_I (is_a_type));
3748 g_warning ("invalid class cast from (NULL) pointer to `%s'",
3749 type_descriptive_name_I (is_a_type));
3754 g_type_check_instance (GTypeInstance *type_instance)
3756 /* this function is just here to make the signal system
3757 * conveniently elaborated on instance checks
3761 if (type_instance->g_class)
3763 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
3765 if (node && node->is_instantiatable)
3768 g_warning ("instance of invalid non-instantiatable type `%s'",
3769 type_descriptive_name_I (type_instance->g_class->g_type));
3772 g_warning ("instance with invalid (NULL) class pointer");
3775 g_warning ("invalid (NULL) pointer instance");
3780 static inline gboolean
3781 type_check_is_value_type_U (GType type)
3783 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
3786 /* common path speed up */
3787 node = lookup_type_node_I (type);
3788 if (node && node->mutatable_check_cache)
3791 G_READ_LOCK (&type_rw_lock);
3795 if (node->data && node->data->common.ref_count > 0 &&
3796 node->data->common.value_table->value_init)
3797 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3798 else if (NODE_IS_IFACE (node))
3802 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3804 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3805 TypeNode *prnode = lookup_type_node_I (prtype);
3807 if (prnode->is_instantiatable)
3810 node = lookup_type_node_I (type);
3816 G_READ_UNLOCK (&type_rw_lock);
3818 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
3822 g_type_check_is_value_type (GType type)
3824 return type_check_is_value_type_U (type);
3828 g_type_check_value (GValue *value)
3830 return value && type_check_is_value_type_U (value->g_type);
3834 g_type_check_value_holds (GValue *value,
3837 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
3841 * g_type_value_table_peek:
3842 * @type: A #GType value.
3844 * Returns the location of the #GTypeValueTable associated with @type.
3845 * <emphasis>Note that this function should only be used from source code
3846 * that implements or has internal knowledge of the implementation of
3849 * Returns: Location of the #GTypeValueTable associated with @type or
3850 * %NULL if there is no #GTypeValueTable associated with @type.
3853 g_type_value_table_peek (GType type)
3855 GTypeValueTable *vtable = NULL;
3856 TypeNode *node = lookup_type_node_I (type);
3857 gboolean has_refed_data, has_table;
3860 /* speed up common code path, we're not 100% safe here,
3861 * but we should only get called with referenced types anyway
3863 data = node ? node->data : NULL;
3864 if (node && node->mutatable_check_cache)
3865 return data->common.value_table;
3867 G_READ_LOCK (&type_rw_lock);
3870 has_refed_data = node && node->data && node->data->common.ref_count;
3871 has_table = has_refed_data && node->data->common.value_table->value_init;
3875 vtable = node->data->common.value_table;
3876 else if (NODE_IS_IFACE (node))
3880 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3882 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3883 TypeNode *prnode = lookup_type_node_I (prtype);
3885 if (prnode->is_instantiatable)
3888 node = lookup_type_node_I (type);
3889 goto restart_table_peek;
3895 G_READ_UNLOCK (&type_rw_lock);
3901 g_warning (G_STRLOC ": type id `%zu' is invalid", type);
3902 if (!has_refed_data)
3903 g_warning ("can't peek value table for type `%s' which is not currently referenced",
3904 type_descriptive_name_I (type));
3909 G_CONST_RETURN gchar*
3910 g_type_name_from_instance (GTypeInstance *instance)
3913 return "<NULL-instance>";
3915 return g_type_name_from_class (instance->g_class);
3918 G_CONST_RETURN gchar*
3919 g_type_name_from_class (GTypeClass *g_class)
3922 return "<NULL-class>";
3924 return g_type_name (g_class->g_type);
3928 /* --- initialization --- */
3930 * g_type_init_with_debug_flags:
3931 * @debug_flags: Bitwise combination of #GTypeDebugFlags values for debugging purposes.
3933 * Similar to g_type_init(), but additionally sets debug flags.
3936 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
3938 G_LOCK_DEFINE_STATIC (type_init_lock);
3939 const gchar *env_string;
3942 volatile GType votype;
3944 G_LOCK (type_init_lock);
3946 G_WRITE_LOCK (&type_rw_lock);
3948 if (static_quark_type_flags)
3950 G_WRITE_UNLOCK (&type_rw_lock);
3951 G_UNLOCK (type_init_lock);
3955 /* setup GObject library wide debugging flags */
3956 _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
3957 env_string = g_getenv ("GOBJECT_DEBUG");
3958 if (env_string != NULL)
3960 static GDebugKey debug_keys[] = {
3961 { "objects", G_TYPE_DEBUG_OBJECTS },
3962 { "signals", G_TYPE_DEBUG_SIGNALS },
3965 _g_type_debug_flags |= g_parse_debug_string (env_string,
3967 sizeof (debug_keys) / sizeof (debug_keys[0]));
3972 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3973 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3974 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3976 /* type qname hash table */
3977 static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3979 /* invalid type G_TYPE_INVALID (0)
3981 static_fundamental_type_nodes[0] = NULL;
3983 /* void type G_TYPE_NONE
3985 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
3986 votype = NODE_TYPE (node);
3987 g_assert (votype == G_TYPE_NONE);
3989 /* interface fundamental type G_TYPE_INTERFACE (!classed)
3991 memset (&info, 0, sizeof (info));
3992 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
3993 votype = NODE_TYPE (node);
3994 type_data_make_W (node, &info, NULL);
3995 g_assert (votype == G_TYPE_INTERFACE);
3997 G_WRITE_UNLOCK (&type_rw_lock);
4001 /* G_TYPE_TYPE_PLUGIN
4003 votype = g_type_plugin_get_type ();
4005 /* G_TYPE_* value types
4007 g_value_types_init ();
4009 /* G_TYPE_ENUM & G_TYPE_FLAGS
4011 g_enum_types_init ();
4015 g_boxed_type_init ();
4019 g_param_type_init ();
4023 g_object_type_init ();
4025 /* G_TYPE_PARAM_* pspec types
4027 g_param_spec_types_init ();
4029 /* Value Transformations
4031 g_value_transforms_init ();
4037 G_UNLOCK (type_init_lock);
4043 * Prior to any use of the type system, g_type_init() has to be called to initialize
4044 * the type system and assorted other code portions (such as the various fundamental
4045 * type implementations or the signal system).
4050 g_type_init_with_debug_flags (0);
4054 * g_type_class_add_private:
4055 * @g_class: class structure for an instantiatable type
4056 * @private_size: size of private structure.
4058 * Registers a private structure for an instantiatable type;
4059 * when an object is allocated, the private structures for
4060 * the type and all of its parent types are allocated
4061 * sequentially in the same memory block as the public
4062 * structures. This function should be called in the
4063 * type's class_init() function. The private structure can
4064 * be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.
4065 * The following example shows attaching a private structure
4066 * <structname>MyObjectPrivate</structname> to an object
4067 * <structname>MyObject</structname> defined in the standard GObject
4071 * typedef struct _MyObjectPrivate MyObjectPrivate;
4073 * struct _MyObjectPrivate {
4077 * #define MY_OBJECT_GET_PRIVATE(o) \
4078 * (G_TYPE_INSTANCE_GET_PRIVATE ((o), MY_TYPE_OBJECT, MyObjectPrivate))
4081 * my_object_class_init (MyObjectClass *klass)
4083 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4087 * my_object_get_some_field (MyObject *my_object)
4089 * MyObjectPrivate *priv = MY_OBJECT_GET_PRIVATE (my_object);
4091 * return priv->some_field;
4098 g_type_class_add_private (gpointer g_class,
4101 GType instance_type = ((GTypeClass *)g_class)->g_type;
4102 TypeNode *node = lookup_type_node_I (instance_type);
4105 g_return_if_fail (private_size > 0);
4107 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4109 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4110 type_descriptive_name_I (instance_type));
4114 if (NODE_PARENT_TYPE (node))
4116 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4117 if (node->data->instance.private_size != pnode->data->instance.private_size)
4119 g_warning ("g_type_add_private() called multiple times for the same type");
4124 G_WRITE_LOCK (&type_rw_lock);
4126 offset = ALIGN_STRUCT (node->data->instance.private_size);
4127 node->data->instance.private_size = offset + private_size;
4129 G_WRITE_UNLOCK (&type_rw_lock);
4133 g_type_instance_get_private (GTypeInstance *instance,
4136 TypeNode *instance_node;
4137 TypeNode *private_node;
4138 TypeNode *parent_node;
4142 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4144 /* while instances are initialized, their class pointers change,
4145 * so figure the instances real class first
4147 class = instance_real_class_get (instance);
4149 class = instance->g_class;
4151 instance_node = lookup_type_node_I (class->g_type);
4152 if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4154 g_warning ("instance of invalid non-instantiatable type `%s'",
4155 type_descriptive_name_I (instance->g_class->g_type));
4159 private_node = lookup_type_node_I (private_type);
4160 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4162 g_warning ("attempt to retrieve private data for invalid type '%s'",
4163 type_descriptive_name_I (private_type));
4167 /* Note that we don't need a read lock, since instance existing
4168 * means that the instance class and all parent classes
4169 * exist, so the node->data, node->data->instance.instance_size,
4170 * and node->data->instance.private_size are not going to be changed.
4171 * for any of the relevant types.
4174 offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4176 if (NODE_PARENT_TYPE (private_node))
4178 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4179 g_assert (parent_node->data && parent_node->data->common.ref_count);
4181 if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4183 g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4187 offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4190 return G_STRUCT_MEMBER_P (instance, offset);
4193 #define __G_TYPE_C__
4194 #include "gobjectaliasdef.c"