Fix g_type_class_add_private() documentation
[platform/upstream/glib.git] / gobject / gtype.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
21  * MT safe
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "gtype.h"
29 #include "gtype-private.h"
30 #include "gtypeplugin.h"
31 #include "gvaluecollector.h"
32 #include "gbsearcharray.h"
33 #include "gobjectalias.h"
34 #include "gatomicarray.h"
35
36
37 /**
38  * SECTION:gtype
39  * @short_description: The GLib Runtime type identification and
40  *     management system
41  * @title:Type Information
42  *
43  * The GType API is the foundation of the GObject system.  It provides the
44  * facilities for registering and managing all fundamental data types,
45  * user-defined object and interface types.  Before using any GType
46  * or GObject functions, g_type_init() must be called to initialize the
47  * type system.
48  *
49  * For type creation and registration purposes, all types fall into one of
50  * two categories: static or dynamic.  Static types are never loaded or
51  * unloaded at run-time as dynamic types may be.  Static types are created
52  * with g_type_register_static() that gets type specific information passed
53  * in via a #GTypeInfo structure.
54  * Dynamic types are created with g_type_register_dynamic() which takes a
55  * #GTypePlugin structure instead. The remaining type information (the
56  * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
57  * and the g_type_plugin_*() API.
58  * These registration functions are usually called only once from a
59  * function whose only purpose is to return the type identifier for a
60  * specific class.  Once the type (or class or interface) is registered,
61  * it may be instantiated, inherited, or implemented depending on exactly
62  * what sort of type it is.
63  * There is also a third registration function for registering fundamental
64  * types called g_type_register_fundamental() which requires both a #GTypeInfo
65  * structure and a #GTypeFundamentalInfo structure but it is seldom used
66  * since most fundamental types are predefined rather than user-defined.
67  *
68  * A final word about type names.
69  * Such an identifier needs to be at least three characters long. There is no
70  * upper length limit. The first character needs to be a letter (a-z or A-Z)
71  * or an underscore '_'. Subsequent characters can be letters, numbers or
72  * any of '-_+'.
73  */
74
75
76 /* NOTE: some functions (some internal variants and exported ones)
77  * invalidate data portions of the TypeNodes. if external functions/callbacks
78  * are called, pointers to memory maintained by TypeNodes have to be looked up
79  * again. this affects most of the struct TypeNode fields, e.g. ->children or
80  * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
81  * not ->supers[]), as all those memory portions can get realloc()ed during
82  * callback invocation.
83  *
84  * TODO:
85  * - g_type_from_name() should do an ordered array lookup after fetching the
86  *   the quark, instead of a second hashtable lookup.
87  *
88  * LOCKING:
89  * lock handling issues when calling static functions are indicated by
90  * uppercase letter postfixes, all static functions have to have
91  * one of the below postfixes:
92  * - _I:        [Indifferent about locking]
93  *   function doesn't care about locks at all
94  * - _U:        [Unlocked invocation]
95  *   no read or write lock has to be held across function invocation
96  *   (locks may be acquired and released during invocation though)
97  * - _L:        [Locked invocation]
98  *   a write lock or more than 0 read locks have to be held across
99  *   function invocation
100  * - _W:        [Write-locked invocation]
101  *   a write lock has to be held across function invocation
102  * - _Wm:       [Write-locked invocation, mutatable]
103  *   like _W, but the write lock might be released and reacquired
104  *   during invocation, watch your pointers
105  * - _WmREC:    [Write-locked invocation, mutatable, recursive]
106  *   like _Wm, but also acquires recursive mutex class_init_rec_mutex
107  */
108
109 #ifdef LOCK_DEBUG
110 #define G_READ_LOCK(rw_lock)    do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
111 #define G_READ_UNLOCK(rw_lock)  do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
112 #define G_WRITE_LOCK(rw_lock)   do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
113 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
114 #else
115 #define G_READ_LOCK(rw_lock)    g_static_rw_lock_reader_lock (rw_lock)
116 #define G_READ_UNLOCK(rw_lock)  g_static_rw_lock_reader_unlock (rw_lock)
117 #define G_WRITE_LOCK(rw_lock)   g_static_rw_lock_writer_lock (rw_lock)
118 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
119 #endif
120 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
121     static const gchar _action[] = " invalidly modified type ";  \
122     gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
123     if (_arg) \
124       g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
125     else \
126       g_error ("%s()%s`%s'", _fname, _action, _tname); \
127 }G_STMT_END
128 #define g_return_val_if_type_system_uninitialized(return_value) G_STMT_START{ \
129     if (G_UNLIKELY (!static_quark_type_flags))                                \
130       {                                                                       \
131         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,                            \
132                "%s: You forgot to call g_type_init()",                        \
133                G_STRLOC);                                                     \
134         return (return_value);                                                \
135       }                                                                       \
136 }G_STMT_END
137
138 #ifdef  G_ENABLE_DEBUG
139 #define DEBUG_CODE(debug_type, code_block)  G_STMT_START {    \
140     if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
141       { code_block; }                                     \
142 } G_STMT_END
143 #else /* !G_ENABLE_DEBUG */
144 #define DEBUG_CODE(debug_type, code_block)  /* code_block */
145 #endif  /* G_ENABLE_DEBUG */
146
147 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
148                                     G_TYPE_FLAG_INSTANTIATABLE | \
149                                     G_TYPE_FLAG_DERIVABLE | \
150                                     G_TYPE_FLAG_DEEP_DERIVABLE)
151 #define TYPE_FLAG_MASK             (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
152 #define SIZEOF_FUNDAMENTAL_INFO    ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
153                                                        sizeof (gpointer)), \
154                                                   sizeof (glong)))
155
156 /* The 2*sizeof(size_t) alignment here is borrowed from
157  * GNU libc, so it should be good most everywhere.
158  * It is more conservative than is needed on some 64-bit
159  * platforms, but ia64 does require a 16-byte alignment.
160  * The SIMD extensions for x86 and ppc32 would want a
161  * larger alignment than this, but we don't need to
162  * do better than malloc.
163  */
164 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
165 #define ALIGN_STRUCT(offset) \
166       ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
167
168
169 /* --- typedefs --- */
170 typedef struct _TypeNode        TypeNode;
171 typedef struct _CommonData      CommonData;
172 typedef struct _BoxedData       BoxedData;
173 typedef struct _IFaceData       IFaceData;
174 typedef struct _ClassData       ClassData;
175 typedef struct _InstanceData    InstanceData;
176 typedef union  _TypeData        TypeData;
177 typedef struct _IFaceEntries    IFaceEntries;
178 typedef struct _IFaceEntry      IFaceEntry;
179 typedef struct _IFaceHolder     IFaceHolder;
180
181
182 /* --- prototypes --- */
183 static inline GTypeFundamentalInfo*     type_node_fundamental_info_I    (TypeNode               *node);
184 static        void                      type_add_flags_W                (TypeNode               *node,
185                                                                          GTypeFlags              flags);
186 static        void                      type_data_make_W                (TypeNode               *node,
187                                                                          const GTypeInfo        *info,
188                                                                          const GTypeValueTable  *value_table);
189 static inline void                      type_data_ref_Wm                (TypeNode               *node);
190 static inline void                      type_data_unref_U               (TypeNode               *node,
191                                                                          gboolean                uncached);
192 static void                             type_data_last_unref_Wm         (TypeNode *              node,
193                                                                          gboolean                uncached);
194 static inline gpointer                  type_get_qdata_L                (TypeNode               *node,
195                                                                          GQuark                  quark);
196 static inline void                      type_set_qdata_W                (TypeNode               *node,
197                                                                          GQuark                  quark,
198                                                                          gpointer                data);
199 static IFaceHolder*                     type_iface_peek_holder_L        (TypeNode               *iface,
200                                                                          GType                   instance_type);
201 static gboolean                         type_iface_vtable_base_init_Wm  (TypeNode               *iface,
202                                                                          TypeNode               *node);
203 static void                             type_iface_vtable_iface_init_Wm (TypeNode               *iface,
204                                                                          TypeNode               *node);
205 static gboolean                         type_node_is_a_L                (TypeNode               *node,
206                                                                          TypeNode               *iface_node);
207
208
209 /* --- enumeration --- */
210
211 /* The InitState enumeration is used to track the progress of initializing
212  * both classes and interface vtables. Keeping the state of initialization
213  * is necessary to handle new interfaces being added while we are initializing
214  * the class or other interfaces.
215  */
216 typedef enum
217 {
218   UNINITIALIZED,
219   BASE_CLASS_INIT,
220   BASE_IFACE_INIT,
221   CLASS_INIT,
222   IFACE_INIT,
223   INITIALIZED
224 } InitState;
225
226 /* --- structures --- */
227 struct _TypeNode
228 {
229   guint volatile ref_count;
230   GTypePlugin *plugin;
231   guint        n_children; /* writable with lock */
232   guint        n_supers : 8;
233   guint        n_prerequisites : 9;
234   guint        is_classed : 1;
235   guint        is_instantiatable : 1;
236   guint        mutatable_check_cache : 1;       /* combines some common path checks */
237   GType       *children; /* writable with lock */
238   TypeData * volatile data;
239   GQuark       qname;
240   GData       *global_gdata;
241   union {
242     GAtomicArray iface_entries;         /* for !iface types */
243     GAtomicArray offsets;
244   } _prot;
245   GType       *prerequisites;
246   GType        supers[1]; /* flexible array */
247 };
248
249 #define SIZEOF_BASE_TYPE_NODE()                 (G_STRUCT_OFFSET (TypeNode, supers))
250 #define MAX_N_SUPERS                            (255)
251 #define MAX_N_CHILDREN                          (4095)
252 #define MAX_N_INTERFACES                        (255) /* Limited by offsets being 8 bits */
253 #define MAX_N_PREREQUISITES                     (511)
254 #define NODE_TYPE(node)                         (node->supers[0])
255 #define NODE_PARENT_TYPE(node)                  (node->supers[1])
256 #define NODE_FUNDAMENTAL_TYPE(node)             (node->supers[node->n_supers])
257 #define NODE_NAME(node)                         (g_quark_to_string (node->qname))
258 #define NODE_REFCOUNT(node)                     ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
259 #define NODE_IS_BOXED(node)                     (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
260 #define NODE_IS_IFACE(node)                     (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
261 #define CLASSED_NODE_IFACES_ENTRIES(node)       (&(node)->_prot.iface_entries)
262 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
263 #define IFACE_NODE_N_PREREQUISITES(node)        ((node)->n_prerequisites)
264 #define IFACE_NODE_PREREQUISITES(node)          ((node)->prerequisites)
265 #define iface_node_get_holders_L(node)          ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
266 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
267 #define iface_node_get_dependants_array_L(n)    ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
268 #define iface_node_set_dependants_array_W(n,d)  (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
269 #define TYPE_ID_MASK                            ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
270
271 #define NODE_IS_ANCESTOR(ancestor, node)                                                    \
272         ((ancestor)->n_supers <= (node)->n_supers &&                                        \
273          (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
274
275 struct _IFaceHolder
276 {
277   GType           instance_type;
278   GInterfaceInfo *info;
279   GTypePlugin    *plugin;
280   IFaceHolder    *next;
281 };
282
283 struct _IFaceEntry
284 {
285   GType           iface_type;
286   GTypeInterface *vtable;
287   InitState       init_state;
288 };
289
290 struct _IFaceEntries {
291   guint offset_index;
292   IFaceEntry entry[1];
293 };
294
295 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
296 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
297
298 struct _CommonData
299 {
300   GTypeValueTable  *value_table;
301 };
302
303 struct _BoxedData
304 {
305   CommonData         data;
306   GBoxedCopyFunc     copy_func;
307   GBoxedFreeFunc     free_func;
308 };
309
310 struct _IFaceData
311 {
312   CommonData         common;
313   guint16            vtable_size;
314   GBaseInitFunc      vtable_init_base;
315   GBaseFinalizeFunc  vtable_finalize_base;
316   GClassInitFunc     dflt_init;
317   GClassFinalizeFunc dflt_finalize;
318   gconstpointer      dflt_data;
319   gpointer           dflt_vtable;
320 };
321
322 struct _ClassData
323 {
324   CommonData         common;
325   guint16            class_size;
326   guint16            class_private_size;
327   int volatile       init_state; /* atomic - g_type_class_ref reads it unlocked */
328   GBaseInitFunc      class_init_base;
329   GBaseFinalizeFunc  class_finalize_base;
330   GClassInitFunc     class_init;
331   GClassFinalizeFunc class_finalize;
332   gconstpointer      class_data;
333   gpointer           class;
334 };
335
336 struct _InstanceData
337 {
338   CommonData         common;
339   guint16            class_size;
340   guint16            class_private_size;
341   int volatile       init_state; /* atomic - g_type_class_ref reads it unlocked */
342   GBaseInitFunc      class_init_base;
343   GBaseFinalizeFunc  class_finalize_base;
344   GClassInitFunc     class_init;
345   GClassFinalizeFunc class_finalize;
346   gconstpointer      class_data;
347   gpointer           class;
348   guint16            instance_size;
349   guint16            private_size;
350   guint16            n_preallocs;
351   GInstanceInitFunc  instance_init;
352 };
353
354 union _TypeData
355 {
356   CommonData         common;
357   BoxedData          boxed;
358   IFaceData          iface;
359   ClassData          class;
360   InstanceData       instance;
361 };
362
363 typedef struct {
364   gpointer            cache_data;
365   GTypeClassCacheFunc cache_func;
366 } ClassCacheFunc;
367
368 typedef struct {
369   gpointer                check_data;
370   GTypeInterfaceCheckFunc check_func;
371 } IFaceCheckFunc;
372
373
374 /* --- variables --- */
375 static GStaticRWLock   type_rw_lock = G_STATIC_RW_LOCK_INIT;
376 static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
377 static guint           static_n_class_cache_funcs = 0;
378 static ClassCacheFunc *static_class_cache_funcs = NULL;
379 static guint           static_n_iface_check_funcs = 0;
380 static IFaceCheckFunc *static_iface_check_funcs = NULL;
381 static GQuark          static_quark_type_flags = 0;
382 static GQuark          static_quark_iface_holder = 0;
383 static GQuark          static_quark_dependants_array = 0;
384 GTypeDebugFlags        _g_type_debug_flags = 0;
385
386 /* --- type nodes --- */
387 static GHashTable       *static_type_nodes_ht = NULL;
388 static TypeNode         *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
389 static GType             static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
390
391 static inline TypeNode*
392 lookup_type_node_I (register GType utype)
393 {
394   if (utype > G_TYPE_FUNDAMENTAL_MAX)
395     return (TypeNode*) (utype & ~TYPE_ID_MASK);
396   else
397     return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
398 }
399
400 static TypeNode*
401 type_node_any_new_W (TypeNode             *pnode,
402                      GType                 ftype,
403                      const gchar          *name,
404                      GTypePlugin          *plugin,
405                      GTypeFundamentalFlags type_flags)
406 {
407   guint n_supers;
408   GType type;
409   TypeNode *node;
410   guint i, node_size = 0;
411
412   n_supers = pnode ? pnode->n_supers + 1 : 0;
413   
414   if (!pnode)
415     node_size += SIZEOF_FUNDAMENTAL_INFO;             /* fundamental type info */
416   node_size += SIZEOF_BASE_TYPE_NODE ();              /* TypeNode structure */
417   node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
418   node = g_malloc0 (node_size);
419   if (!pnode)                                         /* offset fundamental types */
420     {
421       node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
422       static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
423       type = ftype;
424     }
425   else
426     type = (GType) node;
427   
428   g_assert ((type & TYPE_ID_MASK) == 0);
429   
430   node->n_supers = n_supers;
431   if (!pnode)
432     {
433       node->supers[0] = type;
434       node->supers[1] = 0;
435       
436       node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
437       node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
438       
439       if (NODE_IS_IFACE (node))
440         {
441           IFACE_NODE_N_PREREQUISITES (node) = 0;
442           IFACE_NODE_PREREQUISITES (node) = NULL;
443         }
444       else
445         _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
446     }
447   else
448     {
449       node->supers[0] = type;
450       memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
451       
452       node->is_classed = pnode->is_classed;
453       node->is_instantiatable = pnode->is_instantiatable;
454       
455       if (NODE_IS_IFACE (node))
456         {
457           IFACE_NODE_N_PREREQUISITES (node) = 0;
458           IFACE_NODE_PREREQUISITES (node) = NULL;
459         }
460       else
461         {
462           guint j;
463           IFaceEntries *entries;
464
465           entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
466                                           IFACE_ENTRIES_HEADER_SIZE,
467                                           0);
468           if (entries)
469             {
470               for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
471                 {
472                   entries->entry[j].vtable = NULL;
473                   entries->entry[j].init_state = UNINITIALIZED;
474                 }
475               _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
476                                       entries);
477             }
478         }
479
480       i = pnode->n_children++;
481       pnode->children = g_renew (GType, pnode->children, pnode->n_children);
482       pnode->children[i] = type;
483     }
484   
485   node->plugin = plugin;
486   node->n_children = 0;
487   node->children = NULL;
488   node->data = NULL;
489   node->qname = g_quark_from_string (name);
490   node->global_gdata = NULL;
491   
492   g_hash_table_insert (static_type_nodes_ht,
493                        GUINT_TO_POINTER (node->qname),
494                        (gpointer) type);
495   return node;
496 }
497
498 static inline GTypeFundamentalInfo*
499 type_node_fundamental_info_I (TypeNode *node)
500 {
501   GType ftype = NODE_FUNDAMENTAL_TYPE (node);
502   
503   if (ftype != NODE_TYPE (node))
504     node = lookup_type_node_I (ftype);
505   
506   return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
507 }
508
509 static TypeNode*
510 type_node_fundamental_new_W (GType                 ftype,
511                              const gchar          *name,
512                              GTypeFundamentalFlags type_flags)
513 {
514   GTypeFundamentalInfo *finfo;
515   TypeNode *node;
516   
517   g_assert ((ftype & TYPE_ID_MASK) == 0);
518   g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
519   
520   if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
521     static_fundamental_next++;
522   
523   type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
524   
525   node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
526   
527   finfo = type_node_fundamental_info_I (node);
528   finfo->type_flags = type_flags;
529   
530   return node;
531 }
532
533 static TypeNode*
534 type_node_new_W (TypeNode    *pnode,
535                  const gchar *name,
536                  GTypePlugin *plugin)
537      
538 {
539   g_assert (pnode);
540   g_assert (pnode->n_supers < MAX_N_SUPERS);
541   g_assert (pnode->n_children < MAX_N_CHILDREN);
542   
543   return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
544 }
545
546 static inline IFaceEntry*
547 lookup_iface_entry_I (volatile IFaceEntries *entries,
548                       TypeNode *iface_node)
549 {
550   guint8 *offsets;
551   guint offset_index;
552   IFaceEntry *check;
553   int index;
554   IFaceEntry *entry;
555
556   if (entries == NULL)
557     return NULL;
558
559   G_ATOMIC_ARRAY_DO_TRANSACTION
560     (&iface_node->_prot.offsets, guint8,
561
562      entry = NULL;
563      offsets = transaction_data;
564      offset_index = entries->offset_index;
565      if (offsets != NULL &&
566          offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
567        {
568          index = offsets[offset_index];
569          if (index > 0)
570            {
571              /* zero means unset, subtract one to get real index */
572              index -= 1;
573
574              if (index < IFACE_ENTRIES_N_ENTRIES (entries))
575                {
576                  check = (IFaceEntry *)&entries->entry[index];
577                  if (check->iface_type == NODE_TYPE (iface_node))
578                    entry = check;
579                }
580            }
581        }
582      );
583
584  return entry;
585 }
586
587 static inline IFaceEntry*
588 type_lookup_iface_entry_L (TypeNode *node,
589                            TypeNode *iface_node)
590 {
591   if (!NODE_IS_IFACE (iface_node))
592     return NULL;
593
594   return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
595                                iface_node);
596 }
597
598
599 static inline gboolean
600 type_lookup_iface_vtable_I (TypeNode *node,
601                             TypeNode *iface_node,
602                             gpointer *vtable_ptr)
603 {
604   IFaceEntry *entry;
605   gboolean res;
606
607   if (!NODE_IS_IFACE (iface_node))
608     {
609       if (vtable_ptr)
610         *vtable_ptr = NULL;
611       return FALSE;
612     }
613
614   G_ATOMIC_ARRAY_DO_TRANSACTION
615     (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
616
617      entry = lookup_iface_entry_I (transaction_data, iface_node);
618      res = entry != NULL;
619      if (vtable_ptr)
620        {
621          if (entry)
622            *vtable_ptr = entry->vtable;
623          else
624            *vtable_ptr = NULL;
625        }
626      );
627
628   return res;
629 }
630
631 static inline gboolean
632 type_lookup_prerequisite_L (TypeNode *iface,
633                             GType     prerequisite_type)
634 {
635   if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
636     {
637       GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
638       guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
639       
640       do
641         {
642           guint i;
643           GType *check;
644           
645           i = (n_prerequisites + 1) >> 1;
646           check = prerequisites + i;
647           if (prerequisite_type == *check)
648             return TRUE;
649           else if (prerequisite_type > *check)
650             {
651               n_prerequisites -= i;
652               prerequisites = check;
653             }
654           else /* if (prerequisite_type < *check) */
655             n_prerequisites = i - 1;
656         }
657       while (n_prerequisites);
658     }
659   return FALSE;
660 }
661
662 static const gchar*
663 type_descriptive_name_I (GType type)
664 {
665   if (type)
666     {
667       TypeNode *node = lookup_type_node_I (type);
668       
669       return node ? NODE_NAME (node) : "<unknown>";
670     }
671   else
672     return "<invalid>";
673 }
674
675
676 /* --- type consistency checks --- */
677 static gboolean
678 check_plugin_U (GTypePlugin *plugin,
679                 gboolean     need_complete_type_info,
680                 gboolean     need_complete_interface_info,
681                 const gchar *type_name)
682 {
683   /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U 
684    */
685   if (!plugin)
686     {
687       g_warning ("plugin handle for type `%s' is NULL",
688                  type_name);
689       return FALSE;
690     }
691   if (!G_IS_TYPE_PLUGIN (plugin))
692     {
693       g_warning ("plugin pointer (%p) for type `%s' is invalid",
694                  plugin, type_name);
695       return FALSE;
696     }
697   if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
698     {
699       g_warning ("plugin for type `%s' has no complete_type_info() implementation",
700                  type_name);
701       return FALSE;
702     }
703   if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
704     {
705       g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
706                  type_name);
707       return FALSE;
708     }
709   return TRUE;
710 }
711
712 static gboolean
713 check_type_name_I (const gchar *type_name)
714 {
715   static const gchar extra_chars[] = "-_+";
716   const gchar *p = type_name;
717   gboolean name_valid;
718   
719   if (!type_name[0] || !type_name[1] || !type_name[2])
720     {
721       g_warning ("type name `%s' is too short", type_name);
722       return FALSE;
723     }
724   /* check the first letter */
725   name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
726   for (p = type_name + 1; *p; p++)
727     name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
728                    (p[0] >= 'a' && p[0] <= 'z') ||
729                    (p[0] >= '0' && p[0] <= '9') ||
730                    strchr (extra_chars, p[0]));
731   if (!name_valid)
732     {
733       g_warning ("type name `%s' contains invalid characters", type_name);
734       return FALSE;
735     }
736   if (g_type_from_name (type_name))
737     {
738       g_warning ("cannot register existing type `%s'", type_name);
739       return FALSE;
740     }
741   
742   return TRUE;
743 }
744
745 static gboolean
746 check_derivation_I (GType        parent_type,
747                     const gchar *type_name)
748 {
749   TypeNode *pnode;
750   GTypeFundamentalInfo* finfo;
751   
752   pnode = lookup_type_node_I (parent_type);
753   if (!pnode)
754     {
755       g_warning ("cannot derive type `%s' from invalid parent type `%s'",
756                  type_name,
757                  type_descriptive_name_I (parent_type));
758       return FALSE;
759     }
760   finfo = type_node_fundamental_info_I (pnode);
761   /* ensure flat derivability */
762   if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
763     {
764       g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
765                  type_name,
766                  NODE_NAME (pnode));
767       return FALSE;
768     }
769   /* ensure deep derivability */
770   if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
771       !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
772     {
773       g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
774                  type_name,
775                  NODE_NAME (pnode));
776       return FALSE;
777     }
778   
779   return TRUE;
780 }
781
782 static gboolean
783 check_collect_format_I (const gchar *collect_format)
784 {
785   const gchar *p = collect_format;
786   gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
787                            G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
788                            G_VALUE_COLLECT_POINTER, 0 };
789   
790   while (*p)
791     if (!strchr (valid_format, *p++))
792       return FALSE;
793   return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
794 }
795
796 static gboolean
797 check_value_table_I (const gchar           *type_name,
798                      const GTypeValueTable *value_table)
799 {
800   if (!value_table)
801     return FALSE;
802   else if (value_table->value_init == NULL)
803     {
804       if (value_table->value_free || value_table->value_copy ||
805           value_table->value_peek_pointer ||
806           value_table->collect_format || value_table->collect_value ||
807           value_table->lcopy_format || value_table->lcopy_value)
808         g_warning ("cannot handle uninitializable values of type `%s'",
809                    type_name);
810       return FALSE;
811     }
812   else /* value_table->value_init != NULL */
813     {
814       if (!value_table->value_free)
815         {
816           /* +++ optional +++
817            * g_warning ("missing `value_free()' for type `%s'", type_name);
818            * return FALSE;
819            */
820         }
821       if (!value_table->value_copy)
822         {
823           g_warning ("missing `value_copy()' for type `%s'", type_name);
824           return FALSE;
825         }
826       if ((value_table->collect_format || value_table->collect_value) &&
827           (!value_table->collect_format || !value_table->collect_value))
828         {
829           g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
830                      type_name);
831           return FALSE;
832         }
833       if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
834         {
835           g_warning ("the `%s' specification for type `%s' is too long or invalid",
836                      "collect_format",
837                      type_name);
838           return FALSE;
839         }
840       if ((value_table->lcopy_format || value_table->lcopy_value) &&
841           (!value_table->lcopy_format || !value_table->lcopy_value))
842         {
843           g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
844                      type_name);
845           return FALSE;
846         }
847       if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
848         {
849           g_warning ("the `%s' specification for type `%s' is too long or invalid",
850                      "lcopy_format",
851                      type_name);
852           return FALSE;
853         }
854     }
855   return TRUE;
856 }
857
858 static gboolean
859 check_type_info_I (TypeNode        *pnode,
860                    GType            ftype,
861                    const gchar     *type_name,
862                    const GTypeInfo *info)
863 {
864   GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
865   gboolean is_interface = ftype == G_TYPE_INTERFACE;
866   
867   g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
868   
869   /* check instance members */
870   if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
871       (info->instance_size || info->n_preallocs || info->instance_init))
872     {
873       if (pnode)
874         g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
875                    type_name,
876                    NODE_NAME (pnode));
877       else
878         g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
879                    type_name);
880       return FALSE;
881     }
882   /* check class & interface members */
883   if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
884       (info->class_init || info->class_finalize || info->class_data ||
885        info->class_size || info->base_init || info->base_finalize))
886     {
887       if (pnode)
888         g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
889                    type_name,
890                    NODE_NAME (pnode));
891       else
892         g_warning ("cannot create class for `%s' as non-classed fundamental",
893                    type_name);
894       return FALSE;
895     }
896   /* check interface size */
897   if (is_interface && info->class_size < sizeof (GTypeInterface))
898     {
899       g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
900                  type_name);
901       return FALSE;
902     }
903   /* check class size */
904   if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
905     {
906       if (info->class_size < sizeof (GTypeClass))
907         {
908           g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
909                      type_name);
910           return FALSE;
911         }
912       if (pnode && info->class_size < pnode->data->class.class_size)
913         {
914           g_warning ("specified class size for type `%s' is smaller "
915                      "than the parent type's `%s' class size",
916                      type_name,
917                      NODE_NAME (pnode));
918           return FALSE;
919         }
920     }
921   /* check instance size */
922   if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
923     {
924       if (info->instance_size < sizeof (GTypeInstance))
925         {
926           g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
927                      type_name);
928           return FALSE;
929         }
930       if (pnode && info->instance_size < pnode->data->instance.instance_size)
931         {
932           g_warning ("specified instance size for type `%s' is smaller "
933                      "than the parent type's `%s' instance size",
934                      type_name,
935                      NODE_NAME (pnode));
936           return FALSE;
937         }
938     }
939   
940   return TRUE;
941 }
942
943 static TypeNode*
944 find_conforming_child_type_L (TypeNode *pnode,
945                               TypeNode *iface)
946 {
947   TypeNode *node = NULL;
948   guint i;
949   
950   if (type_lookup_iface_entry_L (pnode, iface))
951     return pnode;
952   
953   for (i = 0; i < pnode->n_children && !node; i++)
954     node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
955   
956   return node;
957 }
958
959 static gboolean
960 check_add_interface_L (GType instance_type,
961                        GType iface_type)
962 {
963   TypeNode *node = lookup_type_node_I (instance_type);
964   TypeNode *iface = lookup_type_node_I (iface_type);
965   IFaceEntry *entry;
966   TypeNode *tnode;
967   GType *prerequisites;
968   guint i;
969
970   
971   if (!node || !node->is_instantiatable)
972     {
973       g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
974                  type_descriptive_name_I (instance_type));
975       return FALSE;
976     }
977   if (!iface || !NODE_IS_IFACE (iface))
978     {
979       g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
980                  type_descriptive_name_I (iface_type),
981                  NODE_NAME (node));
982       return FALSE;
983     }
984   tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
985   if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
986     {
987       /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
988       g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
989                  NODE_NAME (iface),
990                  NODE_NAME (node),
991                  NODE_NAME (tnode));
992       return FALSE;
993     }
994   /* allow overriding of interface type introduced for parent type */
995   entry = type_lookup_iface_entry_L (node, iface);
996   if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
997     {
998       /* ok, we do conform to this interface already, but the interface vtable was not
999        * yet intialized, and we just conform to the interface because it got added to
1000        * one of our parents. so we allow overriding of holder info here.
1001        */
1002       return TRUE;
1003     }
1004   /* check whether one of our children already conforms (or whether the interface
1005    * got added to this node already)
1006    */
1007   tnode = find_conforming_child_type_L (node, iface);  /* tnode is_a node */
1008   if (tnode)
1009     {
1010       g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
1011                  NODE_NAME (iface),
1012                  NODE_NAME (node),
1013                  NODE_NAME (tnode));
1014       return FALSE;
1015     }
1016   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1017   for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1018     {
1019       tnode = lookup_type_node_I (prerequisites[i]);
1020       if (!type_node_is_a_L (node, tnode))
1021         {
1022           g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
1023                      NODE_NAME (iface),
1024                      NODE_NAME (node),
1025                      NODE_NAME (tnode));
1026           return FALSE;
1027         }
1028     }
1029   return TRUE;
1030 }
1031
1032 static gboolean
1033 check_interface_info_I (TypeNode             *iface,
1034                         GType                 instance_type,
1035                         const GInterfaceInfo *info)
1036 {
1037   if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1038     {
1039       g_warning ("interface type `%s' for type `%s' comes without initializer",
1040                  NODE_NAME (iface),
1041                  type_descriptive_name_I (instance_type));
1042       return FALSE;
1043     }
1044   
1045   return TRUE;
1046 }
1047
1048 /* --- type info (type node data) --- */
1049 static void
1050 type_data_make_W (TypeNode              *node,
1051                   const GTypeInfo       *info,
1052                   const GTypeValueTable *value_table)
1053 {
1054   TypeData *data;
1055   GTypeValueTable *vtable = NULL;
1056   guint vtable_size = 0;
1057   
1058   g_assert (node->data == NULL && info != NULL);
1059   
1060   if (!value_table)
1061     {
1062       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1063       
1064       if (pnode)
1065         vtable = pnode->data->common.value_table;
1066       else
1067         {
1068           static const GTypeValueTable zero_vtable = { NULL, };
1069           
1070           value_table = &zero_vtable;
1071         }
1072     }
1073   if (value_table)
1074     {
1075       /* need to setup vtable_size since we have to allocate it with data in one chunk */
1076       vtable_size = sizeof (GTypeValueTable);
1077       if (value_table->collect_format)
1078         vtable_size += strlen (value_table->collect_format);
1079       if (value_table->lcopy_format)
1080         vtable_size += strlen (value_table->lcopy_format);
1081       vtable_size += 2;
1082     }
1083    
1084   if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
1085     {
1086       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1087
1088       data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1089       if (vtable_size)
1090         vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1091       data->instance.class_size = info->class_size;
1092       data->instance.class_init_base = info->base_init;
1093       data->instance.class_finalize_base = info->base_finalize;
1094       data->instance.class_init = info->class_init;
1095       data->instance.class_finalize = info->class_finalize;
1096       data->instance.class_data = info->class_data;
1097       data->instance.class = NULL;
1098       data->instance.init_state = UNINITIALIZED;
1099       data->instance.instance_size = info->instance_size;
1100       /* We'll set the final value for data->instance.private size
1101        * after the parent class has been initialized
1102        */
1103       data->instance.private_size = 0;
1104       data->instance.class_private_size = 0;
1105       if (pnode)
1106         data->instance.class_private_size = pnode->data->instance.class_private_size;
1107 #ifdef  DISABLE_MEM_POOLS
1108       data->instance.n_preallocs = 0;
1109 #else   /* !DISABLE_MEM_POOLS */
1110       data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1111 #endif  /* !DISABLE_MEM_POOLS */
1112       data->instance.instance_init = info->instance_init;
1113     }
1114   else if (node->is_classed) /* only classed */
1115     {
1116       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1117
1118       data = g_malloc0 (sizeof (ClassData) + vtable_size);
1119       if (vtable_size)
1120         vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1121       data->class.class_size = info->class_size;
1122       data->class.class_init_base = info->base_init;
1123       data->class.class_finalize_base = info->base_finalize;
1124       data->class.class_init = info->class_init;
1125       data->class.class_finalize = info->class_finalize;
1126       data->class.class_data = info->class_data;
1127       data->class.class = NULL;
1128       data->class.class_private_size = 0;
1129       if (pnode)
1130         data->class.class_private_size = pnode->data->class.class_private_size;
1131       data->class.init_state = UNINITIALIZED;
1132     }
1133   else if (NODE_IS_IFACE (node))
1134     {
1135       data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1136       if (vtable_size)
1137         vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1138       data->iface.vtable_size = info->class_size;
1139       data->iface.vtable_init_base = info->base_init;
1140       data->iface.vtable_finalize_base = info->base_finalize;
1141       data->iface.dflt_init = info->class_init;
1142       data->iface.dflt_finalize = info->class_finalize;
1143       data->iface.dflt_data = info->class_data;
1144       data->iface.dflt_vtable = NULL;
1145     }
1146   else if (NODE_IS_BOXED (node))
1147     {
1148       data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1149       if (vtable_size)
1150         vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1151     }
1152   else
1153     {
1154       data = g_malloc0 (sizeof (CommonData) + vtable_size);
1155       if (vtable_size)
1156         vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1157     }
1158   
1159   node->data = data;
1160   
1161   if (vtable_size)
1162     {
1163       gchar *p;
1164       
1165       /* we allocate the vtable and its strings together with the type data, so
1166        * children can take over their parent's vtable pointer, and we don't
1167        * need to worry freeing it or not when the child data is destroyed
1168        */
1169       *vtable = *value_table;
1170       p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1171       p[0] = 0;
1172       vtable->collect_format = p;
1173       if (value_table->collect_format)
1174         {
1175           strcat (p, value_table->collect_format);
1176           p += strlen (value_table->collect_format);
1177         }
1178       p++;
1179       p[0] = 0;
1180       vtable->lcopy_format = p;
1181       if (value_table->lcopy_format)
1182         strcat  (p, value_table->lcopy_format);
1183     }
1184   node->data->common.value_table = vtable;
1185   node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1186                                  !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1187                                    GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1188   
1189   g_assert (node->data->common.value_table != NULL); /* paranoid */
1190
1191   g_atomic_int_set ((int *) &node->ref_count, 1);
1192 }
1193
1194 static inline void
1195 type_data_ref_Wm (TypeNode *node)
1196 {
1197   if (!node->data)
1198     {
1199       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1200       GTypeInfo tmp_info;
1201       GTypeValueTable tmp_value_table;
1202       
1203       g_assert (node->plugin != NULL);
1204       
1205       if (pnode)
1206         {
1207           type_data_ref_Wm (pnode);
1208           if (node->data)
1209             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1210         }
1211       
1212       memset (&tmp_info, 0, sizeof (tmp_info));
1213       memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1214       
1215       G_WRITE_UNLOCK (&type_rw_lock);
1216       g_type_plugin_use (node->plugin);
1217       g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1218       G_WRITE_LOCK (&type_rw_lock);
1219       if (node->data)
1220         INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1221       
1222       check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1223       type_data_make_W (node, &tmp_info,
1224                         check_value_table_I (NODE_NAME (node),
1225                                              &tmp_value_table) ? &tmp_value_table : NULL);
1226     }
1227   else
1228     {
1229       g_assert (NODE_REFCOUNT (node) > 0);
1230       
1231       g_atomic_int_inc ((int *) &node->ref_count);
1232     }
1233 }
1234
1235 static inline gboolean
1236 type_data_ref_U (TypeNode *node)
1237 {
1238   guint current;
1239
1240   do {
1241     current = NODE_REFCOUNT (node);
1242
1243     if (current < 1)
1244       return FALSE;
1245   } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1246
1247   return TRUE;
1248 }
1249
1250 static gboolean
1251 iface_node_has_available_offset_L (TypeNode *iface_node,
1252                                    int offset,
1253                                    int for_index)
1254 {
1255   guint8 *offsets;
1256
1257   offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1258   if (offsets == NULL)
1259     return TRUE;
1260
1261   if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1262     return TRUE;
1263
1264   if (offsets[offset] == 0 ||
1265       offsets[offset] == for_index+1)
1266     return TRUE;
1267
1268   return FALSE;
1269 }
1270
1271 static int
1272 find_free_iface_offset_L (IFaceEntries *entries)
1273 {
1274   IFaceEntry *entry;
1275   TypeNode *iface_node;
1276   int offset;
1277   int i;
1278   int n_entries;
1279
1280   n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1281   offset = -1;
1282   do
1283     {
1284       offset++;
1285       for (i = 0; i < n_entries; i++)
1286         {
1287           entry = &entries->entry[i];
1288           iface_node = lookup_type_node_I (entry->iface_type);
1289
1290           if (!iface_node_has_available_offset_L (iface_node, offset, i))
1291             break;
1292         }
1293     }
1294   while (i != n_entries);
1295
1296   return offset;
1297 }
1298
1299 static void
1300 iface_node_set_offset_L (TypeNode *iface_node,
1301                          int offset,
1302                          int index)
1303 {
1304   guint8 *offsets, *old_offsets;
1305   int new_size, old_size;
1306   int i;
1307
1308   old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1309   if (old_offsets == NULL)
1310     old_size = 0;
1311   else
1312     {
1313       old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1314       if (offset < old_size &&
1315           old_offsets[offset] == index + 1)
1316         return; /* Already set to this index, return */
1317     }
1318   new_size = MAX (old_size, offset + 1);
1319
1320   offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1321                                   0, new_size - old_size);
1322
1323   /* Mark new area as unused */
1324   for (i = old_size; i < new_size; i++)
1325     offsets[i] = 0;
1326
1327   offsets[offset] = index + 1;
1328
1329   _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1330 }
1331
1332 static void
1333 type_node_add_iface_entry_W (TypeNode   *node,
1334                              GType       iface_type,
1335                              IFaceEntry *parent_entry)
1336 {
1337   IFaceEntries *entries;
1338   IFaceEntry *entry;
1339   TypeNode *iface_node;
1340   guint i, j;
1341   int num_entries;
1342
1343   g_assert (node->is_instantiatable);
1344
1345   entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1346   if (entries != NULL)
1347     {
1348       num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1349
1350       g_assert (num_entries < MAX_N_INTERFACES);
1351
1352       for (i = 0; i < num_entries; i++)
1353         {
1354           entry = &entries->entry[i];
1355           if (entry->iface_type == iface_type)
1356             {
1357               /* this can happen in two cases:
1358                * - our parent type already conformed to iface_type and node
1359                *   got its own holder info. here, our children already have
1360                *   entries and NULL vtables, since this will only work for
1361                *   uninitialized classes.
1362                * - an interface type is added to an ancestor after it was
1363                *   added to a child type.
1364                */
1365               if (!parent_entry)
1366                 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1367               else
1368                 {
1369                   /* sick, interface is added to ancestor *after* child type;
1370                    * nothing todo, the entry and our children were already setup correctly
1371                    */
1372                 }
1373               return;
1374             }
1375         }
1376     }
1377
1378   entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1379                                   IFACE_ENTRIES_HEADER_SIZE,
1380                                   sizeof (IFaceEntry));
1381   num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1382   i = num_entries - 1;
1383   if (i == 0)
1384     entries->offset_index = 0;
1385   entries->entry[i].iface_type = iface_type;
1386   entries->entry[i].vtable = NULL;
1387   entries->entry[i].init_state = UNINITIALIZED;
1388
1389   if (parent_entry)
1390     {
1391       if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1392         {
1393           entries->entry[i].init_state = INITIALIZED;
1394           entries->entry[i].vtable = parent_entry->vtable;
1395         }
1396     }
1397
1398   /* Update offsets in iface */
1399   iface_node = lookup_type_node_I (iface_type);
1400
1401   if (iface_node_has_available_offset_L (iface_node,
1402                                          entries->offset_index,
1403                                          i))
1404     {
1405       iface_node_set_offset_L (iface_node,
1406                                entries->offset_index, i);
1407     }
1408   else
1409    {
1410       entries->offset_index =
1411         find_free_iface_offset_L (entries);
1412       for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1413         {
1414           entry = &entries->entry[j];
1415           iface_node =
1416             lookup_type_node_I (entry->iface_type);
1417           iface_node_set_offset_L (iface_node,
1418                                    entries->offset_index, j);
1419         }
1420     }
1421
1422   _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1423
1424   if (parent_entry)
1425     {
1426       for (i = 0; i < node->n_children; i++)
1427         type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1428     }
1429 }
1430
1431 static void
1432 type_add_interface_Wm (TypeNode             *node,
1433                        TypeNode             *iface,
1434                        const GInterfaceInfo *info,
1435                        GTypePlugin          *plugin)
1436 {
1437   IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1438   IFaceEntry *entry;
1439   guint i;
1440
1441   g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1442   
1443   iholder->next = iface_node_get_holders_L (iface);
1444   iface_node_set_holders_W (iface, iholder);
1445   iholder->instance_type = NODE_TYPE (node);
1446   iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1447   iholder->plugin = plugin;
1448
1449   /* create an iface entry for this type */
1450   type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1451   
1452   /* if the class is already (partly) initialized, we may need to base
1453    * initalize and/or initialize the new interface.
1454    */
1455   if (node->data)
1456     {
1457       InitState class_state = node->data->class.init_state;
1458       
1459       if (class_state >= BASE_IFACE_INIT)
1460         type_iface_vtable_base_init_Wm (iface, node);
1461       
1462       if (class_state >= IFACE_INIT)
1463         type_iface_vtable_iface_init_Wm (iface, node);
1464     }
1465   
1466   /* create iface entries for children of this type */
1467   entry = type_lookup_iface_entry_L (node, iface);
1468   for (i = 0; i < node->n_children; i++)
1469     type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1470 }
1471
1472 static void
1473 type_iface_add_prerequisite_W (TypeNode *iface,
1474                                TypeNode *prerequisite_node)
1475 {
1476   GType prerequisite_type = NODE_TYPE (prerequisite_node);
1477   GType *prerequisites, *dependants;
1478   guint n_dependants, i;
1479   
1480   g_assert (NODE_IS_IFACE (iface) &&
1481             IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1482             (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1483   
1484   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1485   for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1486     if (prerequisites[i] == prerequisite_type)
1487       return;                   /* we already have that prerequisiste */
1488     else if (prerequisites[i] > prerequisite_type)
1489       break;
1490   IFACE_NODE_N_PREREQUISITES (iface) += 1;
1491   IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1492                                               IFACE_NODE_PREREQUISITES (iface),
1493                                               IFACE_NODE_N_PREREQUISITES (iface));
1494   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1495   g_memmove (prerequisites + i + 1, prerequisites + i,
1496              sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1497   prerequisites[i] = prerequisite_type;
1498   
1499   /* we want to get notified when prerequisites get added to prerequisite_node */
1500   if (NODE_IS_IFACE (prerequisite_node))
1501     {
1502       dependants = iface_node_get_dependants_array_L (prerequisite_node);
1503       n_dependants = dependants ? dependants[0] : 0;
1504       n_dependants += 1;
1505       dependants = g_renew (GType, dependants, n_dependants + 1);
1506       dependants[n_dependants] = NODE_TYPE (iface);
1507       dependants[0] = n_dependants;
1508       iface_node_set_dependants_array_W (prerequisite_node, dependants);
1509     }
1510   
1511   /* we need to notify all dependants */
1512   dependants = iface_node_get_dependants_array_L (iface);
1513   n_dependants = dependants ? dependants[0] : 0;
1514   for (i = 1; i <= n_dependants; i++)
1515     type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1516 }
1517
1518 /**
1519  * g_type_interface_add_prerequisite:
1520  * @interface_type: #GType value of an interface type.
1521  * @prerequisite_type: #GType value of an interface or instantiatable type.
1522  *
1523  * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1524  * This means that any type implementing @interface_type must also implement
1525  * @prerequisite_type. Prerequisites can be thought of as an alternative to
1526  * interface derivation (which GType doesn't support). An interface can have
1527  * at most one instantiatable prerequisite type.
1528  */
1529 void
1530 g_type_interface_add_prerequisite (GType interface_type,
1531                                    GType prerequisite_type)
1532 {
1533   TypeNode *iface, *prerequisite_node;
1534   IFaceHolder *holders;
1535   
1536   g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));      /* G_TYPE_IS_INTERFACE() is an external call: _U */
1537   g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1538   g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1539   
1540   iface = lookup_type_node_I (interface_type);
1541   prerequisite_node = lookup_type_node_I (prerequisite_type);
1542   if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1543     {
1544       g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1545                  type_descriptive_name_I (interface_type),
1546                  type_descriptive_name_I (prerequisite_type));
1547       return;
1548     }
1549   G_WRITE_LOCK (&type_rw_lock);
1550   holders = iface_node_get_holders_L (iface);
1551   if (holders)
1552     {
1553       G_WRITE_UNLOCK (&type_rw_lock);
1554       g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1555                  type_descriptive_name_I (prerequisite_type),
1556                  type_descriptive_name_I (interface_type),
1557                  type_descriptive_name_I (holders->instance_type));
1558       return;
1559     }
1560   if (prerequisite_node->is_instantiatable)
1561     {
1562       guint i;
1563       
1564       /* can have at most one publically installable instantiatable prerequisite */
1565       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1566         {
1567           TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1568           
1569           if (prnode->is_instantiatable)
1570             {
1571               G_WRITE_UNLOCK (&type_rw_lock);
1572               g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1573                          type_descriptive_name_I (prerequisite_type),
1574                          type_descriptive_name_I (interface_type),
1575                          type_descriptive_name_I (NODE_TYPE (prnode)));
1576               return;
1577             }
1578         }
1579       
1580       for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1581         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1582       G_WRITE_UNLOCK (&type_rw_lock);
1583     }
1584   else if (NODE_IS_IFACE (prerequisite_node))
1585     {
1586       GType *prerequisites;
1587       guint i;
1588       
1589       prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1590       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1591         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1592       type_iface_add_prerequisite_W (iface, prerequisite_node);
1593       G_WRITE_UNLOCK (&type_rw_lock);
1594     }
1595   else
1596     {
1597       G_WRITE_UNLOCK (&type_rw_lock);
1598       g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1599                  type_descriptive_name_I (prerequisite_type),
1600                  type_descriptive_name_I (interface_type));
1601     }
1602 }
1603
1604 /**
1605  * g_type_interface_prerequisites:
1606  * @interface_type: an interface type
1607  * @n_prerequisites: location to return the number of prerequisites, or %NULL
1608  *
1609  * Returns the prerequisites of an interfaces type.
1610  *
1611  * Since: 2.2
1612  *
1613  * Returns: a newly-allocated zero-terminated array of #GType containing
1614  *  the prerequisites of @interface_type
1615  */
1616 GType*
1617 g_type_interface_prerequisites (GType  interface_type,
1618                                 guint *n_prerequisites)
1619 {
1620   TypeNode *iface;
1621   
1622   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1623
1624   iface = lookup_type_node_I (interface_type);
1625   if (iface)
1626     {
1627       GType *types;
1628       TypeNode *inode = NULL;
1629       guint i, n = 0;
1630       
1631       G_READ_LOCK (&type_rw_lock);
1632       types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1633       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1634         {
1635           GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1636           TypeNode *node = lookup_type_node_I (prerequisite);
1637           if (node->is_instantiatable)
1638             {
1639               if (!inode || type_node_is_a_L (node, inode))
1640                 inode = node;
1641             }
1642           else
1643             types[n++] = NODE_TYPE (node);
1644         }
1645       if (inode)
1646         types[n++] = NODE_TYPE (inode);
1647       
1648       if (n_prerequisites)
1649         *n_prerequisites = n;
1650       G_READ_UNLOCK (&type_rw_lock);
1651       
1652       return types;
1653     }
1654   else
1655     {
1656       if (n_prerequisites)
1657         *n_prerequisites = 0;
1658       
1659       return NULL;
1660     }
1661 }
1662
1663
1664 static IFaceHolder*
1665 type_iface_peek_holder_L (TypeNode *iface,
1666                           GType     instance_type)
1667 {
1668   IFaceHolder *iholder;
1669   
1670   g_assert (NODE_IS_IFACE (iface));
1671   
1672   iholder = iface_node_get_holders_L (iface);
1673   while (iholder && iholder->instance_type != instance_type)
1674     iholder = iholder->next;
1675   return iholder;
1676 }
1677
1678 static IFaceHolder*
1679 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1680                                     GType     instance_type,
1681                                     gboolean  need_info)
1682 {
1683   IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1684   
1685   if (iholder && !iholder->info && need_info)
1686     {
1687       GInterfaceInfo tmp_info;
1688       
1689       g_assert (iholder->plugin != NULL);
1690       
1691       type_data_ref_Wm (iface);
1692       if (iholder->info)
1693         INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1694       
1695       memset (&tmp_info, 0, sizeof (tmp_info));
1696       
1697       G_WRITE_UNLOCK (&type_rw_lock);
1698       g_type_plugin_use (iholder->plugin);
1699       g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1700       G_WRITE_LOCK (&type_rw_lock);
1701       if (iholder->info)
1702         INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1703       
1704       check_interface_info_I (iface, instance_type, &tmp_info);
1705       iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1706     }
1707   
1708   return iholder;       /* we don't modify write lock upon returning NULL */
1709 }
1710
1711 static void
1712 type_iface_blow_holder_info_Wm (TypeNode *iface,
1713                                 GType     instance_type)
1714 {
1715   IFaceHolder *iholder = iface_node_get_holders_L (iface);
1716   
1717   g_assert (NODE_IS_IFACE (iface));
1718   
1719   while (iholder->instance_type != instance_type)
1720     iholder = iholder->next;
1721   
1722   if (iholder->info && iholder->plugin)
1723     {
1724       g_free (iholder->info);
1725       iholder->info = NULL;
1726       
1727       G_WRITE_UNLOCK (&type_rw_lock);
1728       g_type_plugin_unuse (iholder->plugin);
1729       type_data_unref_U (iface, FALSE);
1730       G_WRITE_LOCK (&type_rw_lock);
1731     }
1732 }
1733
1734 /* Assumes type's class already exists
1735  */
1736 static inline size_t
1737 type_total_instance_size_I (TypeNode *node)
1738 {
1739   gsize total_instance_size;
1740
1741   total_instance_size = node->data->instance.instance_size;
1742   if (node->data->instance.private_size != 0)
1743     total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1744
1745   return total_instance_size;
1746 }
1747
1748 /* --- type structure creation/destruction --- */
1749 typedef struct {
1750   gpointer instance;
1751   gpointer class;
1752 } InstanceRealClass;
1753
1754 static gint
1755 instance_real_class_cmp (gconstpointer p1,
1756                          gconstpointer p2)
1757 {
1758   const InstanceRealClass *irc1 = p1;
1759   const InstanceRealClass *irc2 = p2;
1760   guint8 *i1 = irc1->instance;
1761   guint8 *i2 = irc2->instance;
1762   return G_BSEARCH_ARRAY_CMP (i1, i2);
1763 }
1764
1765 G_LOCK_DEFINE_STATIC (instance_real_class);
1766 static GBSearchArray *instance_real_class_bsa = NULL;
1767 static GBSearchConfig instance_real_class_bconfig = {
1768   sizeof (InstanceRealClass),
1769   instance_real_class_cmp,
1770   0,
1771 };
1772
1773 static inline void
1774 instance_real_class_set (gpointer    instance,
1775                          GTypeClass *class)
1776 {
1777   InstanceRealClass key;
1778   key.instance = instance;
1779   key.class = class;
1780   G_LOCK (instance_real_class);
1781   if (!instance_real_class_bsa)
1782     instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1783   instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1784   G_UNLOCK (instance_real_class);
1785 }
1786
1787 static inline void
1788 instance_real_class_remove (gpointer instance)
1789 {
1790   InstanceRealClass key, *node;
1791   guint index;
1792   key.instance = instance;
1793   G_LOCK (instance_real_class);
1794   node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1795   index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1796   instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1797   if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1798     {
1799       g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1800       instance_real_class_bsa = NULL;
1801     }
1802   G_UNLOCK (instance_real_class);
1803 }
1804
1805 static inline GTypeClass*
1806 instance_real_class_get (gpointer instance)
1807 {
1808   InstanceRealClass key, *node;
1809   GTypeClass *class;
1810   key.instance = instance;
1811   G_LOCK (instance_real_class);
1812   node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1813   class = node ? node->class : NULL;
1814   G_UNLOCK (instance_real_class);
1815   return class;
1816 }
1817
1818 /**
1819  * g_type_create_instance:
1820  * @type: An instantiatable type to create an instance for.
1821  *
1822  * Creates and initializes an instance of @type if @type is valid and
1823  * can be instantiated. The type system only performs basic allocation
1824  * and structure setups for instances: actual instance creation should
1825  * happen through functions supplied by the type's fundamental type
1826  * implementation.  So use of g_type_create_instance() is reserved for
1827  * implementators of fundamental types only. E.g. instances of the
1828  * #GObject hierarchy should be created via g_object_new() and
1829  * <emphasis>never</emphasis> directly through
1830  * g_type_create_instance() which doesn't handle things like singleton
1831  * objects or object construction.  Note: Do <emphasis>not</emphasis>
1832  * use this function, unless you're implementing a fundamental
1833  * type. Also language bindings should <emphasis>not</emphasis> use
1834  * this function but g_object_new() instead.
1835  *
1836  * Returns: An allocated and initialized instance, subject to further
1837  *  treatment by the fundamental type implementation.
1838  */
1839 GTypeInstance*
1840 g_type_create_instance (GType type)
1841 {
1842   TypeNode *node;
1843   GTypeInstance *instance;
1844   GTypeClass *class;
1845   guint i, total_size;
1846   
1847   node = lookup_type_node_I (type);
1848   if (!node || !node->is_instantiatable)
1849     {
1850       g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1851                  type_descriptive_name_I (type));
1852       return NULL;
1853     }
1854   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1855   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1856     {
1857       g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1858                  type_descriptive_name_I (type));
1859       return NULL;
1860     }
1861   
1862   class = g_type_class_ref (type);
1863   total_size = type_total_instance_size_I (node);
1864
1865   instance = g_slice_alloc0 (total_size);
1866
1867   if (node->data->instance.private_size)
1868     instance_real_class_set (instance, class);
1869   for (i = node->n_supers; i > 0; i--)
1870     {
1871       TypeNode *pnode;
1872       
1873       pnode = lookup_type_node_I (node->supers[i]);
1874       if (pnode->data->instance.instance_init)
1875         {
1876           instance->g_class = pnode->data->instance.class;
1877           pnode->data->instance.instance_init (instance, class);
1878         }
1879     }
1880   if (node->data->instance.private_size)
1881     instance_real_class_remove (instance);
1882
1883   instance->g_class = class;
1884   if (node->data->instance.instance_init)
1885     node->data->instance.instance_init (instance, class);
1886   
1887   return instance;
1888 }
1889
1890 /**
1891  * g_type_free_instance:
1892  * @instance: an instance of a type.
1893  *
1894  * Frees an instance of a type, returning it to the instance pool for
1895  * the type, if there is one.
1896  *
1897  * Like g_type_create_instance(), this function is reserved for
1898  * implementors of fundamental types.
1899  */
1900 void
1901 g_type_free_instance (GTypeInstance *instance)
1902 {
1903   TypeNode *node;
1904   GTypeClass *class;
1905   
1906   g_return_if_fail (instance != NULL && instance->g_class != NULL);
1907   
1908   class = instance->g_class;
1909   node = lookup_type_node_I (class->g_type);
1910   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1911     {
1912       g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1913                  type_descriptive_name_I (class->g_type));
1914       return;
1915     }
1916   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1917   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1918     {
1919       g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1920                  NODE_NAME (node));
1921       return;
1922     }
1923   
1924   instance->g_class = NULL;
1925 #ifdef G_ENABLE_DEBUG  
1926   memset (instance, 0xaa, type_total_instance_size_I (node));
1927 #endif
1928   g_slice_free1 (type_total_instance_size_I (node), instance);
1929
1930   g_type_class_unref (class);
1931 }
1932
1933 static void
1934 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1935 {
1936   g_assert (iface->data);
1937
1938   if (!iface->data->iface.dflt_vtable)
1939     {
1940       GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1941       iface->data->iface.dflt_vtable = vtable;
1942       vtable->g_type = NODE_TYPE (iface);
1943       vtable->g_instance_type = 0;
1944       if (iface->data->iface.vtable_init_base ||
1945           iface->data->iface.dflt_init)
1946         {
1947           G_WRITE_UNLOCK (&type_rw_lock);
1948           if (iface->data->iface.vtable_init_base)
1949             iface->data->iface.vtable_init_base (vtable);
1950           if (iface->data->iface.dflt_init)
1951             iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1952           G_WRITE_LOCK (&type_rw_lock);
1953         }
1954     }
1955 }
1956
1957
1958 /* This is called to allocate and do the first part of initializing
1959  * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1960  *
1961  * A FALSE return indicates that we didn't find an init function for
1962  * this type/iface pair, so the vtable from the parent type should
1963  * be used. Note that the write lock is not modified upon a FALSE
1964  * return.
1965  */
1966 static gboolean
1967 type_iface_vtable_base_init_Wm (TypeNode *iface,
1968                                 TypeNode *node)
1969 {
1970   IFaceEntry *entry;
1971   IFaceHolder *iholder;
1972   GTypeInterface *vtable = NULL;
1973   TypeNode *pnode;
1974   
1975   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1976   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1977   if (!iholder)
1978     return FALSE;       /* we don't modify write lock upon FALSE */
1979
1980   type_iface_ensure_dflt_vtable_Wm (iface);
1981
1982   entry = type_lookup_iface_entry_L (node, iface);
1983
1984   g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1985   
1986   entry->init_state = IFACE_INIT;
1987
1988   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1989   if (pnode)    /* want to copy over parent iface contents */
1990     {
1991       IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1992       
1993       if (pentry)
1994         vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1995     }
1996   if (!vtable)
1997     vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1998   entry->vtable = vtable;
1999   vtable->g_type = NODE_TYPE (iface);
2000   vtable->g_instance_type = NODE_TYPE (node);
2001   
2002   if (iface->data->iface.vtable_init_base)
2003     {
2004       G_WRITE_UNLOCK (&type_rw_lock);
2005       iface->data->iface.vtable_init_base (vtable);
2006       G_WRITE_LOCK (&type_rw_lock);
2007     }
2008   return TRUE;  /* initialized the vtable */
2009 }
2010
2011 /* Finishes what type_iface_vtable_base_init_Wm started by
2012  * calling the interface init function.
2013  * this function may only be called for types with their
2014  * own interface holder info, i.e. types for which
2015  * g_type_add_interface*() was called and not children thereof.
2016  */
2017 static void
2018 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2019                                  TypeNode *node)
2020 {
2021   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2022   IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2023   GTypeInterface *vtable = NULL;
2024   guint i;
2025   
2026   /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2027   g_assert (iface->data && entry && iholder && iholder->info);
2028   g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2029   
2030   entry->init_state = INITIALIZED;
2031       
2032   vtable = entry->vtable;
2033
2034   if (iholder->info->interface_init)
2035     {
2036       G_WRITE_UNLOCK (&type_rw_lock);
2037       if (iholder->info->interface_init)
2038         iholder->info->interface_init (vtable, iholder->info->interface_data);
2039       G_WRITE_LOCK (&type_rw_lock);
2040     }
2041   
2042   for (i = 0; i < static_n_iface_check_funcs; i++)
2043     {
2044       GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2045       gpointer check_data = static_iface_check_funcs[i].check_data;
2046
2047       G_WRITE_UNLOCK (&type_rw_lock);
2048       check_func (check_data, (gpointer)vtable);
2049       G_WRITE_LOCK (&type_rw_lock);      
2050     }
2051 }
2052
2053 static gboolean
2054 type_iface_vtable_finalize_Wm (TypeNode       *iface,
2055                                TypeNode       *node,
2056                                GTypeInterface *vtable)
2057 {
2058   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2059   IFaceHolder *iholder;
2060   
2061   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2062   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2063   if (!iholder)
2064     return FALSE;       /* we don't modify write lock upon FALSE */
2065   
2066   g_assert (entry && entry->vtable == vtable && iholder->info);
2067   
2068   entry->vtable = NULL;
2069   entry->init_state = UNINITIALIZED;
2070   if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2071     {
2072       G_WRITE_UNLOCK (&type_rw_lock);
2073       if (iholder->info->interface_finalize)
2074         iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2075       if (iface->data->iface.vtable_finalize_base)
2076         iface->data->iface.vtable_finalize_base (vtable);
2077       G_WRITE_LOCK (&type_rw_lock);
2078     }
2079   vtable->g_type = 0;
2080   vtable->g_instance_type = 0;
2081   g_free (vtable);
2082   
2083   type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2084   
2085   return TRUE;  /* write lock modified */
2086 }
2087
2088 static void
2089 type_class_init_Wm (TypeNode   *node,
2090                     GTypeClass *pclass)
2091 {
2092   GSList *slist, *init_slist = NULL;
2093   GTypeClass *class;
2094   IFaceEntries *entries;
2095   IFaceEntry *entry;
2096   TypeNode *bnode, *pnode;
2097   guint i;
2098   
2099   /* Accessing data->class will work for instantiable types
2100    * too because ClassData is a subset of InstanceData
2101    */
2102   g_assert (node->is_classed && node->data &&
2103             node->data->class.class_size &&
2104             !node->data->class.class &&
2105             node->data->class.init_state == UNINITIALIZED);
2106   if (node->data->class.class_private_size)
2107     class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2108   else
2109     class = g_malloc0 (node->data->class.class_size);
2110   node->data->class.class = class;
2111   g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2112   
2113   if (pclass)
2114     {
2115       TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2116       
2117       memcpy (class, pclass, pnode->data->class.class_size);
2118       memcpy (G_STRUCT_MEMBER_P (class, ALIGN_STRUCT (node->data->class.class_size)), G_STRUCT_MEMBER_P (pclass, ALIGN_STRUCT (pnode->data->class.class_size)), pnode->data->class.class_private_size);
2119
2120       if (node->is_instantiatable)
2121         {
2122           /* We need to initialize the private_size here rather than in
2123            * type_data_make_W() since the class init for the parent
2124            * class may have changed pnode->data->instance.private_size.
2125            */
2126           node->data->instance.private_size = pnode->data->instance.private_size;
2127         }
2128     }
2129   class->g_type = NODE_TYPE (node);
2130   
2131   G_WRITE_UNLOCK (&type_rw_lock);
2132   
2133   /* stack all base class initialization functions, so we
2134    * call them in ascending order.
2135    */
2136   for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2137     if (bnode->data->class.class_init_base)
2138       init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2139   for (slist = init_slist; slist; slist = slist->next)
2140     {
2141       GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2142       
2143       class_init_base (class);
2144     }
2145   g_slist_free (init_slist);
2146   
2147   G_WRITE_LOCK (&type_rw_lock);
2148
2149   g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2150   
2151   /* Before we initialize the class, base initialize all interfaces, either
2152    * from parent, or through our holder info
2153    */
2154   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2155
2156   i = 0;
2157   while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2158           i < IFACE_ENTRIES_N_ENTRIES (entries))
2159     {
2160       entry = &entries->entry[i];
2161       while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2162              entry->init_state == IFACE_INIT)
2163         {
2164           entry++;
2165           i++;
2166         }
2167
2168       if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2169         break;
2170
2171       if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2172         {
2173           guint j;
2174           IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2175           
2176           /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2177            * doesn't modify write lock upon FALSE, so entry is still valid; 
2178            */
2179           g_assert (pnode != NULL);
2180
2181           if (pentries)
2182             for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2183               {
2184                 IFaceEntry *pentry = &pentries->entry[j];
2185
2186                 if (pentry->iface_type == entry->iface_type)
2187                   {
2188                     entry->vtable = pentry->vtable;
2189                     entry->init_state = INITIALIZED;
2190                     break;
2191                   }
2192               }
2193           g_assert (entry->vtable != NULL);
2194         }
2195
2196       /* If the write lock was released, additional interface entries might
2197        * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2198        * be base-initialized when inserted, so we don't have to worry that
2199        * we might miss them. Uninitialized entries can only be moved higher
2200        * when new ones are inserted.
2201        */
2202       i++;
2203     }
2204   
2205   g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2206   
2207   G_WRITE_UNLOCK (&type_rw_lock);
2208
2209   if (node->data->class.class_init)
2210     node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2211   
2212   G_WRITE_LOCK (&type_rw_lock);
2213   
2214   g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2215   
2216   /* finish initializing the interfaces through our holder info.
2217    * inherited interfaces are already init_state == INITIALIZED, because
2218    * they either got setup in the above base_init loop, or during
2219    * class_init from within type_add_interface_Wm() for this or
2220    * an anchestor type.
2221    */
2222   i = 0;
2223   while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2224     {
2225       entry = &entries->entry[i];
2226       while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2227              entry->init_state == INITIALIZED)
2228         {
2229           entry++;
2230           i++;
2231         }
2232
2233       if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2234         break;
2235
2236       type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2237       
2238       /* As in the loop above, additional initialized entries might be inserted
2239        * if the write lock is released, but that's harmless because the entries
2240        * we need to initialize only move higher in the list.
2241        */
2242       i++;
2243     }
2244   
2245   g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2246 }
2247
2248 static void
2249 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2250 {
2251   guint i;
2252   IFaceEntries *entries;
2253
2254   g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2255
2256  reiterate:
2257   entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2258   for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2259     {
2260       IFaceEntry *entry = &entries->entry[i];
2261       if (entry->vtable)
2262         {
2263           if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2264             {
2265               /* refetch entries, IFACES_ENTRIES might be modified */
2266               goto reiterate;
2267             }
2268           else
2269             {
2270               /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2271                * iface vtable came from parent
2272                */
2273               entry->vtable = NULL;
2274               entry->init_state = UNINITIALIZED;
2275             }
2276         }
2277     }
2278 }
2279
2280 static void
2281 type_data_finalize_class_U (TypeNode  *node,
2282                             ClassData *cdata)
2283 {
2284   GTypeClass *class = cdata->class;
2285   TypeNode *bnode;
2286   
2287   g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2288   
2289   if (cdata->class_finalize)
2290     cdata->class_finalize (class, (gpointer) cdata->class_data);
2291   
2292   /* call all base class destruction functions in descending order
2293    */
2294   if (cdata->class_finalize_base)
2295     cdata->class_finalize_base (class);
2296   for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2297     if (bnode->data->class.class_finalize_base)
2298       bnode->data->class.class_finalize_base (class);
2299   
2300   g_free (cdata->class);
2301 }
2302
2303 static void
2304 type_data_last_unref_Wm (TypeNode *node,
2305                          gboolean  uncached)
2306 {
2307   g_return_if_fail (node != NULL && node->plugin != NULL);
2308   
2309   if (!node->data || NODE_REFCOUNT (node) == 0)
2310     {
2311       g_warning ("cannot drop last reference to unreferenced type `%s'",
2312                  NODE_NAME (node));
2313       return;
2314     }
2315
2316   /* call class cache hooks */
2317   if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2318     {
2319       guint i;
2320       
2321       G_WRITE_UNLOCK (&type_rw_lock);
2322       G_READ_LOCK (&type_rw_lock);
2323       for (i = 0; i < static_n_class_cache_funcs; i++)
2324         {
2325           GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2326           gpointer cache_data = static_class_cache_funcs[i].cache_data;
2327           gboolean need_break;
2328           
2329           G_READ_UNLOCK (&type_rw_lock);
2330           need_break = cache_func (cache_data, node->data->class.class);
2331           G_READ_LOCK (&type_rw_lock);
2332           if (!node->data || NODE_REFCOUNT (node) == 0)
2333             INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2334           if (need_break)
2335             break;
2336         }
2337       G_READ_UNLOCK (&type_rw_lock);
2338       G_WRITE_LOCK (&type_rw_lock);
2339     }
2340   
2341   /* may have been re-referenced meanwhile */
2342   if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2343     {
2344       GType ptype = NODE_PARENT_TYPE (node);
2345       TypeData *tdata;
2346       
2347       if (node->is_instantiatable)
2348         {
2349           /* destroy node->data->instance.mem_chunk */
2350         }
2351       
2352       tdata = node->data;
2353       if (node->is_classed && tdata->class.class)
2354         {
2355           if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2356             type_data_finalize_class_ifaces_Wm (node);
2357           node->mutatable_check_cache = FALSE;
2358           node->data = NULL;
2359           G_WRITE_UNLOCK (&type_rw_lock);
2360           type_data_finalize_class_U (node, &tdata->class);
2361           G_WRITE_LOCK (&type_rw_lock);
2362         }
2363       else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2364         {
2365           node->mutatable_check_cache = FALSE;
2366           node->data = NULL;
2367           if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2368             {
2369               G_WRITE_UNLOCK (&type_rw_lock);
2370               if (tdata->iface.dflt_finalize)
2371                 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2372               if (tdata->iface.vtable_finalize_base)
2373                 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2374               G_WRITE_LOCK (&type_rw_lock);
2375             }
2376           g_free (tdata->iface.dflt_vtable);
2377         }
2378       else
2379         {
2380           node->mutatable_check_cache = FALSE;
2381           node->data = NULL;
2382         }
2383
2384       /* freeing tdata->common.value_table and its contents is taken care of
2385        * by allocating it in one chunk with tdata
2386        */
2387       g_free (tdata);
2388       
2389       G_WRITE_UNLOCK (&type_rw_lock);
2390       g_type_plugin_unuse (node->plugin);
2391       if (ptype)
2392         type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2393       G_WRITE_LOCK (&type_rw_lock);
2394     }
2395 }
2396
2397 static inline void
2398 type_data_unref_U (TypeNode *node,
2399                    gboolean  uncached)
2400 {
2401   guint current;
2402
2403   do {
2404     current = NODE_REFCOUNT (node);
2405
2406     if (current <= 1)
2407     {
2408       if (!node->plugin)
2409         {
2410           g_warning ("static type `%s' unreferenced too often",
2411                      NODE_NAME (node));
2412           return;
2413         }
2414
2415       g_assert (current > 0);
2416
2417       g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2418       G_WRITE_LOCK (&type_rw_lock);
2419       type_data_last_unref_Wm (node, uncached);
2420       G_WRITE_UNLOCK (&type_rw_lock);
2421       g_static_rec_mutex_unlock (&class_init_rec_mutex);
2422       return;
2423     }
2424   } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2425 }
2426
2427 /**
2428  * g_type_add_class_cache_func:
2429  * @cache_data: data to be passed to @cache_func
2430  * @cache_func: a #GTypeClassCacheFunc
2431  *
2432  * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2433  * class goes from one to zero. This can be used to prevent premature class
2434  * destruction. All installed #GTypeClassCacheFunc functions will be chained
2435  * until one of them returns %TRUE. The functions have to check the class id
2436  * passed in to figure whether they actually want to cache the class of this
2437  * type, since all classes are routed through the same #GTypeClassCacheFunc
2438  * chain.
2439  */
2440 void
2441 g_type_add_class_cache_func (gpointer            cache_data,
2442                              GTypeClassCacheFunc cache_func)
2443 {
2444   guint i;
2445   
2446   g_return_if_fail (cache_func != NULL);
2447   
2448   G_WRITE_LOCK (&type_rw_lock);
2449   i = static_n_class_cache_funcs++;
2450   static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2451   static_class_cache_funcs[i].cache_data = cache_data;
2452   static_class_cache_funcs[i].cache_func = cache_func;
2453   G_WRITE_UNLOCK (&type_rw_lock);
2454 }
2455
2456 /**
2457  * g_type_remove_class_cache_func:
2458  * @cache_data: data that was given when adding @cache_func
2459  * @cache_func: a #GTypeClassCacheFunc
2460  *
2461  * Removes a previously installed #GTypeClassCacheFunc. The cache
2462  * maintained by @cache_func has to be empty when calling
2463  * g_type_remove_class_cache_func() to avoid leaks.
2464  */
2465 void
2466 g_type_remove_class_cache_func (gpointer            cache_data,
2467                                 GTypeClassCacheFunc cache_func)
2468 {
2469   gboolean found_it = FALSE;
2470   guint i;
2471   
2472   g_return_if_fail (cache_func != NULL);
2473   
2474   G_WRITE_LOCK (&type_rw_lock);
2475   for (i = 0; i < static_n_class_cache_funcs; i++)
2476     if (static_class_cache_funcs[i].cache_data == cache_data &&
2477         static_class_cache_funcs[i].cache_func == cache_func)
2478       {
2479         static_n_class_cache_funcs--;
2480         g_memmove (static_class_cache_funcs + i,
2481                    static_class_cache_funcs + i + 1,
2482                    sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2483         static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2484         found_it = TRUE;
2485         break;
2486       }
2487   G_WRITE_UNLOCK (&type_rw_lock);
2488   
2489   if (!found_it)
2490     g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2491                cache_func, cache_data);
2492 }
2493
2494
2495 /**
2496  * g_type_add_interface_check:
2497  * @check_data: data to pass to @check_func
2498  * @check_func: function to be called after each interface
2499  *              is initialized.
2500  *
2501  * Adds a function to be called after an interface vtable is
2502  * initialized for any class (i.e. after the @interface_init member of
2503  * #GInterfaceInfo has been called).
2504  *
2505  * This function is useful when you want to check an invariant that
2506  * depends on the interfaces of a class. For instance, the
2507  * implementation of #GObject uses this facility to check that an
2508  * object implements all of the properties that are defined on its
2509  * interfaces.
2510  *
2511  * Since: 2.4
2512  */
2513 void
2514 g_type_add_interface_check (gpointer                check_data,
2515                             GTypeInterfaceCheckFunc check_func)
2516 {
2517   guint i;
2518   
2519   g_return_if_fail (check_func != NULL);
2520   
2521   G_WRITE_LOCK (&type_rw_lock);
2522   i = static_n_iface_check_funcs++;
2523   static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2524   static_iface_check_funcs[i].check_data = check_data;
2525   static_iface_check_funcs[i].check_func = check_func;
2526   G_WRITE_UNLOCK (&type_rw_lock);
2527 }
2528
2529 /**
2530  * g_type_remove_interface_check:
2531  * @check_data: callback data passed to g_type_add_interface_check()
2532  * @check_func: callback function passed to g_type_add_interface_check()
2533  *
2534  * Removes an interface check function added with
2535  * g_type_add_interface_check().
2536  *
2537  * Since: 2.4
2538  */
2539 void
2540 g_type_remove_interface_check (gpointer                check_data,
2541                                GTypeInterfaceCheckFunc check_func)
2542 {
2543   gboolean found_it = FALSE;
2544   guint i;
2545   
2546   g_return_if_fail (check_func != NULL);
2547   
2548   G_WRITE_LOCK (&type_rw_lock);
2549   for (i = 0; i < static_n_iface_check_funcs; i++)
2550     if (static_iface_check_funcs[i].check_data == check_data &&
2551         static_iface_check_funcs[i].check_func == check_func)
2552       {
2553         static_n_iface_check_funcs--;
2554         g_memmove (static_iface_check_funcs + i,
2555                    static_iface_check_funcs + i + 1,
2556                    sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2557         static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2558         found_it = TRUE;
2559         break;
2560       }
2561   G_WRITE_UNLOCK (&type_rw_lock);
2562   
2563   if (!found_it)
2564     g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2565                check_func, check_data);
2566 }
2567
2568 /* --- type registration --- */
2569 /**
2570  * g_type_register_fundamental:
2571  * @type_id: A predefined type identifier.
2572  * @type_name: 0-terminated string used as the name of the new type.
2573  * @info: The #GTypeInfo structure for this type.
2574  * @finfo: The #GTypeFundamentalInfo structure for this type.
2575  * @flags: Bitwise combination of #GTypeFlags values.
2576  *
2577  * Registers @type_id as the predefined identifier and @type_name as the
2578  * name of a fundamental type.  The type system uses the information
2579  * contained in the #GTypeInfo structure pointed to by @info and the
2580  * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
2581  * type and its instances.  The value of @flags determines additional
2582  * characteristics of the fundamental type.
2583  *
2584  * Returns: The predefined type identifier.
2585  */
2586 GType
2587 g_type_register_fundamental (GType                       type_id,
2588                              const gchar                *type_name,
2589                              const GTypeInfo            *info,
2590                              const GTypeFundamentalInfo *finfo,
2591                              GTypeFlags                  flags)
2592 {
2593   TypeNode *node;
2594   
2595   g_return_val_if_type_system_uninitialized (0);
2596   g_return_val_if_fail (type_id > 0, 0);
2597   g_return_val_if_fail (type_name != NULL, 0);
2598   g_return_val_if_fail (info != NULL, 0);
2599   g_return_val_if_fail (finfo != NULL, 0);
2600   
2601   if (!check_type_name_I (type_name))
2602     return 0;
2603   if ((type_id & TYPE_ID_MASK) ||
2604       type_id > G_TYPE_FUNDAMENTAL_MAX)
2605     {
2606       g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2607                  type_name,
2608                  type_id);
2609       return 0;
2610     }
2611   if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2612       !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2613     {
2614       g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2615                  type_name);
2616       return 0;
2617     }
2618   if (lookup_type_node_I (type_id))
2619     {
2620       g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2621                  type_descriptive_name_I (type_id),
2622                  type_name);
2623       return 0;
2624     }
2625   
2626   G_WRITE_LOCK (&type_rw_lock);
2627   node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2628   type_add_flags_W (node, flags);
2629   
2630   if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2631     type_data_make_W (node, info,
2632                       check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2633   G_WRITE_UNLOCK (&type_rw_lock);
2634   
2635   return NODE_TYPE (node);
2636 }
2637
2638 /**
2639  * g_type_register_static_simple:
2640  * @parent_type: Type from which this type will be derived.
2641  * @type_name: 0-terminated string used as the name of the new type.
2642  * @class_size: Size of the class structure (see #GTypeInfo)
2643  * @class_init: Location of the class initialization function (see #GTypeInfo)
2644  * @instance_size: Size of the instance structure (see #GTypeInfo)
2645  * @instance_init: Location of the instance initialization function (see #GTypeInfo)
2646  * @flags: Bitwise combination of #GTypeFlags values.
2647  *
2648  * Registers @type_name as the name of a new static type derived from
2649  * @parent_type.  The value of @flags determines the nature (e.g.
2650  * abstract or not) of the type. It works by filling a #GTypeInfo
2651  * struct and calling g_type_register_static().
2652  *
2653  * Since: 2.12
2654  *
2655  * Returns: The new type identifier.
2656  */
2657 GType
2658 g_type_register_static_simple (GType             parent_type,
2659                                const gchar      *type_name,
2660                                guint             class_size,
2661                                GClassInitFunc    class_init,
2662                                guint             instance_size,
2663                                GInstanceInitFunc instance_init,
2664                                GTypeFlags        flags)
2665 {
2666   GTypeInfo info;
2667
2668   info.class_size = class_size;
2669   info.base_init = NULL;
2670   info.base_finalize = NULL;
2671   info.class_init = class_init;
2672   info.class_finalize = NULL;
2673   info.class_data = NULL;
2674   info.instance_size = instance_size;
2675   info.n_preallocs = 0;
2676   info.instance_init = instance_init;
2677   info.value_table = NULL;
2678
2679   return g_type_register_static (parent_type, type_name, &info, flags);
2680 }
2681
2682 /**
2683  * g_type_register_static:
2684  * @parent_type: Type from which this type will be derived.
2685  * @type_name: 0-terminated string used as the name of the new type.
2686  * @info: The #GTypeInfo structure for this type.
2687  * @flags: Bitwise combination of #GTypeFlags values.
2688  *
2689  * Registers @type_name as the name of a new static type derived from
2690  * @parent_type.  The type system uses the information contained in the
2691  * #GTypeInfo structure pointed to by @info to manage the type and its
2692  * instances (if not abstract).  The value of @flags determines the nature
2693  * (e.g. abstract or not) of the type.
2694  *
2695  * Returns: The new type identifier.
2696  */
2697 GType
2698 g_type_register_static (GType            parent_type,
2699                         const gchar     *type_name,
2700                         const GTypeInfo *info,
2701                         GTypeFlags       flags)
2702 {
2703   TypeNode *pnode, *node;
2704   GType type = 0;
2705   
2706   g_return_val_if_type_system_uninitialized (0);
2707   g_return_val_if_fail (parent_type > 0, 0);
2708   g_return_val_if_fail (type_name != NULL, 0);
2709   g_return_val_if_fail (info != NULL, 0);
2710   
2711   if (!check_type_name_I (type_name) ||
2712       !check_derivation_I (parent_type, type_name))
2713     return 0;
2714   if (info->class_finalize)
2715     {
2716       g_warning ("class finalizer specified for static type `%s'",
2717                  type_name);
2718       return 0;
2719     }
2720   
2721   pnode = lookup_type_node_I (parent_type);
2722   G_WRITE_LOCK (&type_rw_lock);
2723   type_data_ref_Wm (pnode);
2724   if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2725     {
2726       node = type_node_new_W (pnode, type_name, NULL);
2727       type_add_flags_W (node, flags);
2728       type = NODE_TYPE (node);
2729       type_data_make_W (node, info,
2730                         check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2731     }
2732   G_WRITE_UNLOCK (&type_rw_lock);
2733   
2734   return type;
2735 }
2736
2737 /**
2738  * g_type_register_dynamic:
2739  * @parent_type: Type from which this type will be derived.
2740  * @type_name: 0-terminated string used as the name of the new type.
2741  * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
2742  * @flags: Bitwise combination of #GTypeFlags values.
2743  *
2744  * Registers @type_name as the name of a new dynamic type derived from
2745  * @parent_type.  The type system uses the information contained in the
2746  * #GTypePlugin structure pointed to by @plugin to manage the type and its
2747  * instances (if not abstract).  The value of @flags determines the nature
2748  * (e.g. abstract or not) of the type.
2749  *
2750  * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
2751  */
2752 GType
2753 g_type_register_dynamic (GType        parent_type,
2754                          const gchar *type_name,
2755                          GTypePlugin *plugin,
2756                          GTypeFlags   flags)
2757 {
2758   TypeNode *pnode, *node;
2759   GType type;
2760   
2761   g_return_val_if_type_system_uninitialized (0);
2762   g_return_val_if_fail (parent_type > 0, 0);
2763   g_return_val_if_fail (type_name != NULL, 0);
2764   g_return_val_if_fail (plugin != NULL, 0);
2765   
2766   if (!check_type_name_I (type_name) ||
2767       !check_derivation_I (parent_type, type_name) ||
2768       !check_plugin_U (plugin, TRUE, FALSE, type_name))
2769     return 0;
2770   
2771   G_WRITE_LOCK (&type_rw_lock);
2772   pnode = lookup_type_node_I (parent_type);
2773   node = type_node_new_W (pnode, type_name, plugin);
2774   type_add_flags_W (node, flags);
2775   type = NODE_TYPE (node);
2776   G_WRITE_UNLOCK (&type_rw_lock);
2777   
2778   return type;
2779 }
2780
2781 /**
2782  * g_type_add_interface_static:
2783  * @instance_type: #GType value of an instantiable type.
2784  * @interface_type: #GType value of an interface type.
2785  * @info: The #GInterfaceInfo structure for this
2786  *        (@instance_type, @interface_type) combination.
2787  *
2788  * Adds the static @interface_type to @instantiable_type.  The information
2789  * contained in the #GTypeInterfaceInfo structure pointed to by @info
2790  * is used to manage the relationship.
2791  */
2792 void
2793 g_type_add_interface_static (GType                 instance_type,
2794                              GType                 interface_type,
2795                              const GInterfaceInfo *info)
2796 {
2797   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2798   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2799   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2800
2801   /* we only need to lock class_init_rec_mutex if instance_type already has its
2802    * class initialized, however this function is rarely enough called to take
2803    * the simple route and always acquire class_init_rec_mutex.
2804    */
2805   g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2806   G_WRITE_LOCK (&type_rw_lock);
2807   if (check_add_interface_L (instance_type, interface_type))
2808     {
2809       TypeNode *node = lookup_type_node_I (instance_type);
2810       TypeNode *iface = lookup_type_node_I (interface_type);
2811       if (check_interface_info_I (iface, NODE_TYPE (node), info))
2812         type_add_interface_Wm (node, iface, info, NULL);
2813     }
2814   G_WRITE_UNLOCK (&type_rw_lock);
2815   g_static_rec_mutex_unlock (&class_init_rec_mutex);
2816 }
2817
2818 /**
2819  * g_type_add_interface_dynamic:
2820  * @instance_type: the #GType value of an instantiable type.
2821  * @interface_type: the #GType value of an interface type.
2822  * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
2823  *
2824  * Adds the dynamic @interface_type to @instantiable_type. The information
2825  * contained in the #GTypePlugin structure pointed to by @plugin
2826  * is used to manage the relationship.
2827  */
2828 void
2829 g_type_add_interface_dynamic (GType        instance_type,
2830                               GType        interface_type,
2831                               GTypePlugin *plugin)
2832 {
2833   TypeNode *node;
2834   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2835   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2836   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2837
2838   node = lookup_type_node_I (instance_type);
2839   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2840     return;
2841
2842   /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2843   g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2844   G_WRITE_LOCK (&type_rw_lock);
2845   if (check_add_interface_L (instance_type, interface_type))
2846     {
2847       TypeNode *iface = lookup_type_node_I (interface_type);
2848       type_add_interface_Wm (node, iface, NULL, plugin);
2849     }
2850   G_WRITE_UNLOCK (&type_rw_lock);
2851   g_static_rec_mutex_unlock (&class_init_rec_mutex);
2852 }
2853
2854
2855 /* --- public API functions --- */
2856 /**
2857  * g_type_class_ref:
2858  * @type: Type ID of a classed type.
2859  *
2860  * Increments the reference count of the class structure belonging to
2861  * @type. This function will demand-create the class if it doesn't
2862  * exist already.
2863  *
2864  * Returns: The #GTypeClass structure for the given type ID.
2865  */
2866 gpointer
2867 g_type_class_ref (GType type)
2868 {
2869   TypeNode *node;
2870   GType ptype;
2871   gboolean holds_ref;
2872   GTypeClass *pclass;
2873
2874   /* optimize for common code path */
2875   node = lookup_type_node_I (type);
2876   if (!node || !node->is_classed)
2877     {
2878       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2879                  type_descriptive_name_I (type));
2880       return NULL;
2881     }
2882
2883   if (G_LIKELY (type_data_ref_U (node)))
2884     {
2885       if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2886         return node->data->class.class;
2887       holds_ref = TRUE;
2888     }
2889   else
2890     holds_ref = FALSE;
2891   
2892   /* here, we either have node->data->class.class == NULL, or a recursive
2893    * call to g_type_class_ref() with a partly initialized class, or
2894    * node->data->class.init_state == INITIALIZED, because any
2895    * concurrently running initialization was guarded by class_init_rec_mutex.
2896    */
2897   g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2898
2899   /* we need an initialized parent class for initializing derived classes */
2900   ptype = NODE_PARENT_TYPE (node);
2901   pclass = ptype ? g_type_class_ref (ptype) : NULL;
2902
2903   G_WRITE_LOCK (&type_rw_lock);
2904
2905   if (!holds_ref)
2906     type_data_ref_Wm (node);
2907
2908   if (!node->data->class.class) /* class uninitialized */
2909     type_class_init_Wm (node, pclass);
2910
2911   G_WRITE_UNLOCK (&type_rw_lock);
2912
2913   if (pclass)
2914     g_type_class_unref (pclass);
2915
2916   g_static_rec_mutex_unlock (&class_init_rec_mutex);
2917
2918   return node->data->class.class;
2919 }
2920
2921 /**
2922  * g_type_class_unref:
2923  * @g_class: The #GTypeClass structure to unreference.
2924  *
2925  * Decrements the reference count of the class structure being passed in.
2926  * Once the last reference count of a class has been released, classes
2927  * may be finalized by the type system, so further dereferencing of a
2928  * class pointer after g_type_class_unref() are invalid.
2929  */
2930 void
2931 g_type_class_unref (gpointer g_class)
2932 {
2933   TypeNode *node;
2934   GTypeClass *class = g_class;
2935   
2936   g_return_if_fail (g_class != NULL);
2937   
2938   node = lookup_type_node_I (class->g_type);
2939   if (node && node->is_classed && NODE_REFCOUNT (node))
2940     type_data_unref_U (node, FALSE);
2941   else
2942     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2943                type_descriptive_name_I (class->g_type));
2944 }
2945
2946 /**
2947  * g_type_class_unref_uncached:
2948  * @g_class: The #GTypeClass structure to unreference.
2949  *
2950  * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2951  * implementations. It unreferences a class without consulting the chain
2952  * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
2953  * otherwise.
2954  */
2955 void
2956 g_type_class_unref_uncached (gpointer g_class)
2957 {
2958   TypeNode *node;
2959   GTypeClass *class = g_class;
2960   
2961   g_return_if_fail (g_class != NULL);
2962   
2963   node = lookup_type_node_I (class->g_type);
2964   if (node && node->is_classed && NODE_REFCOUNT (node))
2965     type_data_unref_U (node, TRUE);
2966   else
2967     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2968                type_descriptive_name_I (class->g_type));
2969 }
2970
2971 /**
2972  * g_type_class_peek:
2973  * @type: Type ID of a classed type.
2974  *
2975  * This function is essentially the same as g_type_class_ref(), except that
2976  * the classes reference count isn't incremented. As a consequence, this function
2977  * may return %NULL if the class of the type passed in does not currently
2978  * exist (hasn't been referenced before).
2979  *
2980  * Returns: The #GTypeClass structure for the given type ID or %NULL
2981  *  if the class does not currently exist.
2982  */
2983 gpointer
2984 g_type_class_peek (GType type)
2985 {
2986   TypeNode *node;
2987   gpointer class;
2988   
2989   node = lookup_type_node_I (type);
2990   if (node && node->is_classed && NODE_REFCOUNT (node) &&
2991       g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
2992     /* ref_count _may_ be 0 */
2993     class = node->data->class.class;
2994   else
2995     class = NULL;
2996   
2997   return class;
2998 }
2999
3000 /**
3001  * g_type_class_peek_static:
3002  * @type: Type ID of a classed type.
3003  *
3004  * A more efficient version of g_type_class_peek() which works only for
3005  * static types.
3006  * 
3007  * Since: 2.4
3008  * Returns: The #GTypeClass structure for the given type ID or %NULL
3009  *  if the class does not currently exist or is dynamically loaded.
3010  */
3011 gpointer
3012 g_type_class_peek_static (GType type)
3013 {
3014   TypeNode *node;
3015   gpointer class;
3016   
3017   node = lookup_type_node_I (type);
3018   if (node && node->is_classed && NODE_REFCOUNT (node) &&
3019       /* peek only static types: */ node->plugin == NULL &&
3020       g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3021     /* ref_count _may_ be 0 */
3022     class = node->data->class.class;
3023   else
3024     class = NULL;
3025   
3026   return class;
3027 }
3028
3029 /**
3030  * g_type_class_peek_parent:
3031  * @g_class: The #GTypeClass structure to retrieve the parent class for.
3032  *
3033  * This is a convenience function often needed in class initializers.
3034  * It returns the class structure of the immediate parent type of the
3035  * class passed in.  Since derived classes hold a reference count on
3036  * their parent classes as long as they are instantiated, the returned
3037  * class will always exist. This function is essentially equivalent
3038  * to:
3039  *
3040  * <programlisting>
3041  * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
3042  * </programlisting>
3043  *
3044  * Returns: The parent class of @g_class.
3045  */
3046 gpointer
3047 g_type_class_peek_parent (gpointer g_class)
3048 {
3049   TypeNode *node;
3050   gpointer class = NULL;
3051   
3052   g_return_val_if_fail (g_class != NULL, NULL);
3053   
3054   node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3055   /* We used to acquire a read lock here. That is not necessary, since 
3056    * parent->data->class.class is constant as long as the derived class
3057    * exists. 
3058    */
3059   if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3060     {
3061       node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3062       class = node->data->class.class;
3063     }
3064   else if (NODE_PARENT_TYPE (node))
3065     g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
3066   
3067   return class;
3068 }
3069
3070 /**
3071  * g_type_interface_peek:
3072  * @instance_class: A #GTypeClass structure.
3073  * @iface_type: An interface ID which this class conforms to.
3074  *
3075  * Returns the #GTypeInterface structure of an interface to which the
3076  * passed in class conforms.
3077  *
3078  * Returns: The GTypeInterface structure of iface_type if implemented
3079  *          by @instance_class, %NULL otherwise
3080  */
3081 gpointer
3082 g_type_interface_peek (gpointer instance_class,
3083                        GType    iface_type)
3084 {
3085   TypeNode *node;
3086   TypeNode *iface;
3087   gpointer vtable = NULL;
3088   GTypeClass *class = instance_class;
3089   
3090   g_return_val_if_fail (instance_class != NULL, NULL);
3091   
3092   node = lookup_type_node_I (class->g_type);
3093   iface = lookup_type_node_I (iface_type);
3094   if (node && node->is_instantiatable && iface)
3095     type_lookup_iface_vtable_I (node, iface, &vtable);
3096   else
3097     g_warning (G_STRLOC ": invalid class pointer `%p'", class);
3098   
3099   return vtable;
3100 }
3101
3102 /**
3103  * g_type_interface_peek_parent:
3104  * @g_iface: A #GTypeInterface structure.
3105  *
3106  * Returns the corresponding #GTypeInterface structure of the parent type
3107  * of the instance type to which @g_iface belongs. This is useful when
3108  * deriving the implementation of an interface from the parent type and
3109  * then possibly overriding some methods.
3110  *
3111  * Returns: The corresponding #GTypeInterface structure of the parent
3112  *          type of the instance type to which @g_iface belongs, or
3113  *          %NULL if the parent type doesn't conform to the interface.
3114  */
3115 gpointer
3116 g_type_interface_peek_parent (gpointer g_iface)
3117 {
3118   TypeNode *node;
3119   TypeNode *iface;
3120   gpointer vtable = NULL;
3121   GTypeInterface *iface_class = g_iface;
3122   
3123   g_return_val_if_fail (g_iface != NULL, NULL);
3124   
3125   iface = lookup_type_node_I (iface_class->g_type);
3126   node = lookup_type_node_I (iface_class->g_instance_type);
3127   if (node)
3128     node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3129   if (node && node->is_instantiatable && iface)
3130     type_lookup_iface_vtable_I (node, iface, &vtable);
3131   else if (node)
3132     g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
3133   
3134   return vtable;
3135 }
3136
3137 /**
3138  * g_type_default_interface_ref:
3139  * @g_type: an interface type
3140  *
3141  * Increments the reference count for the interface type @g_type,
3142  * and returns the default interface vtable for the type.
3143  *
3144  * If the type is not currently in use, then the default vtable
3145  * for the type will be created and initalized by calling
3146  * the base interface init and default vtable init functions for
3147  * the type (the @<structfield>base_init</structfield>
3148  * and <structfield>class_init</structfield> members of #GTypeInfo).
3149  * Calling g_type_default_interface_ref() is useful when you
3150  * want to make sure that signals and properties for an interface
3151  * have been installed.
3152  *
3153  * Since: 2.4
3154  *
3155  * Returns: the default vtable for the interface; call
3156  *          g_type_default_interface_unref() when you are done using
3157  *          the interface.
3158  */
3159 gpointer
3160 g_type_default_interface_ref (GType g_type)
3161 {
3162   TypeNode *node;
3163   gpointer dflt_vtable;
3164
3165   G_WRITE_LOCK (&type_rw_lock);
3166
3167   node = lookup_type_node_I (g_type);
3168   if (!node || !NODE_IS_IFACE (node) ||
3169       (node->data && NODE_REFCOUNT (node) == 0))
3170     {
3171       G_WRITE_UNLOCK (&type_rw_lock);
3172       g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3173                  type_descriptive_name_I (g_type));
3174       return NULL;
3175     }
3176
3177   if (!node->data || !node->data->iface.dflt_vtable)
3178     {
3179       G_WRITE_UNLOCK (&type_rw_lock);
3180       g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3181       G_WRITE_LOCK (&type_rw_lock);
3182       node = lookup_type_node_I (g_type);
3183       type_data_ref_Wm (node);
3184       type_iface_ensure_dflt_vtable_Wm (node);
3185       g_static_rec_mutex_unlock (&class_init_rec_mutex);
3186     }
3187   else
3188     type_data_ref_Wm (node); /* ref_count >= 1 already */
3189
3190   dflt_vtable = node->data->iface.dflt_vtable;
3191   G_WRITE_UNLOCK (&type_rw_lock);
3192
3193   return dflt_vtable;
3194 }
3195
3196 /**
3197  * g_type_default_interface_peek:
3198  * @g_type: an interface type
3199  *
3200  * If the interface type @g_type is currently in use, returns its
3201  * default interface vtable.
3202  *
3203  * Since: 2.4
3204  *
3205  * Returns: the default vtable for the interface, or %NULL
3206  *          if the type is not currently in use.
3207  */
3208 gpointer
3209 g_type_default_interface_peek (GType g_type)
3210 {
3211   TypeNode *node;
3212   gpointer vtable;
3213   
3214   node = lookup_type_node_I (g_type);
3215   if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3216     vtable = node->data->iface.dflt_vtable;
3217   else
3218     vtable = NULL;
3219   
3220   return vtable;
3221 }
3222
3223 /**
3224  * g_type_default_interface_unref:
3225  * @g_iface: the default vtable structure for a interface, as
3226  *           returned by g_type_default_interface_ref()
3227  *
3228  * Decrements the reference count for the type corresponding to the
3229  * interface default vtable @g_iface. If the type is dynamic, then
3230  * when no one is using the interface and all references have
3231  * been released, the finalize function for the interface's default
3232  * vtable (the <structfield>class_finalize</structfield> member of
3233  * #GTypeInfo) will be called.
3234  *
3235  * Since: 2.4
3236  */
3237 void
3238 g_type_default_interface_unref (gpointer g_iface)
3239 {
3240   TypeNode *node;
3241   GTypeInterface *vtable = g_iface;
3242   
3243   g_return_if_fail (g_iface != NULL);
3244   
3245   node = lookup_type_node_I (vtable->g_type);
3246   if (node && NODE_IS_IFACE (node))
3247     type_data_unref_U (node, FALSE);
3248   else
3249     g_warning ("cannot unreference invalid interface default vtable for '%s'",
3250                type_descriptive_name_I (vtable->g_type));
3251 }
3252
3253 /**
3254  * g_type_name:
3255  * @type: Type to return name for.
3256  *
3257  * Get the unique name that is assigned to a type ID.  Note that this
3258  * function (like all other GType API) cannot cope with invalid type
3259  * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3260  * other validly registered type ID, but randomized type IDs should
3261  * not be passed in and will most likely lead to a crash.
3262  *
3263  * Returns: Static type name or %NULL.
3264  */
3265 G_CONST_RETURN gchar*
3266 g_type_name (GType type)
3267 {
3268   TypeNode *node;
3269   
3270   g_return_val_if_type_system_uninitialized (NULL);
3271   
3272   node = lookup_type_node_I (type);
3273   
3274   return node ? NODE_NAME (node) : NULL;
3275 }
3276
3277 /**
3278  * g_type_qname:
3279  * @type: Type to return quark of type name for.
3280  *
3281  * Get the corresponding quark of the type IDs name.
3282  *
3283  * Returns: The type names quark or 0.
3284  */
3285 GQuark
3286 g_type_qname (GType type)
3287 {
3288   TypeNode *node;
3289   
3290   node = lookup_type_node_I (type);
3291   
3292   return node ? node->qname : 0;
3293 }
3294
3295 /**
3296  * g_type_from_name:
3297  * @name: Type name to lookup.
3298  *
3299  * Lookup the type ID from a given type name, returning 0 if no type
3300  * has been registered under this name (this is the preferred method
3301  * to find out by name whether a specific type has been registered
3302  * yet).
3303  *
3304  * Returns: Corresponding type ID or 0.
3305  */
3306 GType
3307 g_type_from_name (const gchar *name)
3308 {
3309   GType type = 0;
3310   GQuark quark;
3311   
3312   g_return_val_if_fail (name != NULL, 0);
3313   
3314   quark = g_quark_try_string (name);
3315   if (quark)
3316     {
3317       G_READ_LOCK (&type_rw_lock);
3318       type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
3319       G_READ_UNLOCK (&type_rw_lock);
3320     }
3321   
3322   return type;
3323 }
3324
3325 /**
3326  * g_type_parent:
3327  * @type: The derived type.
3328  *
3329  * Return the direct parent type of the passed in type.  If the passed
3330  * in type has no parent, i.e. is a fundamental type, 0 is returned.
3331  *
3332  * Returns: The parent type.
3333  */
3334 GType
3335 g_type_parent (GType type)
3336 {
3337   TypeNode *node;
3338   
3339   node = lookup_type_node_I (type);
3340   
3341   return node ? NODE_PARENT_TYPE (node) : 0;
3342 }
3343
3344 /**
3345  * g_type_depth:
3346  * @type: A #GType value.
3347  *
3348  * Returns the length of the ancestry of the passed in type. This
3349  * includes the type itself, so that e.g. a fundamental type has depth 1.
3350  *
3351  * Returns: The depth of @type.
3352  */
3353 guint
3354 g_type_depth (GType type)
3355 {
3356   TypeNode *node;
3357   
3358   node = lookup_type_node_I (type);
3359   
3360   return node ? node->n_supers + 1 : 0;
3361 }
3362
3363 /**
3364  * g_type_next_base:
3365  * @leaf_type: Descendant of @root_type and the type to be returned.
3366  * @root_type: Immediate parent of the returned type.
3367  *
3368  * Given a @leaf_type and a @root_type which is contained in its
3369  * anchestry, return the type that @root_type is the immediate parent
3370  * of.  In other words, this function determines the type that is
3371  * derived directly from @root_type which is also a base class of
3372  * @leaf_type.  Given a root type and a leaf type, this function can
3373  * be used to determine the types and order in which the leaf type is
3374  * descended from the root type.
3375  *
3376  * Returns: Immediate child of @root_type and anchestor of @leaf_type.
3377  */
3378 GType
3379 g_type_next_base (GType type,
3380                   GType base_type)
3381 {
3382   GType atype = 0;
3383   TypeNode *node;
3384   
3385   node = lookup_type_node_I (type);
3386   if (node)
3387     {
3388       TypeNode *base_node = lookup_type_node_I (base_type);
3389       
3390       if (base_node && base_node->n_supers < node->n_supers)
3391         {
3392           guint n = node->n_supers - base_node->n_supers;
3393           
3394           if (node->supers[n] == base_type)
3395             atype = node->supers[n - 1];
3396         }
3397     }
3398   
3399   return atype;
3400 }
3401
3402 static inline gboolean
3403 type_node_check_conformities_UorL (TypeNode *node,
3404                                    TypeNode *iface_node,
3405                                    /*        support_inheritance */
3406                                    gboolean  support_interfaces,
3407                                    gboolean  support_prerequisites,
3408                                    gboolean  have_lock)
3409 {
3410   gboolean match;
3411
3412   if (/* support_inheritance && */
3413       NODE_IS_ANCESTOR (iface_node, node))
3414     return TRUE;
3415
3416   support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3417   support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3418   match = FALSE;
3419   if (support_interfaces)
3420     {
3421       if (have_lock)
3422         {
3423           if (type_lookup_iface_entry_L (node, iface_node))
3424             match = TRUE;
3425         }
3426       else
3427         {
3428           if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3429             match = TRUE;
3430         }
3431     }
3432   if (!match &&
3433       support_prerequisites)
3434     {
3435       if (!have_lock)
3436         G_READ_LOCK (&type_rw_lock);
3437       if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3438         match = TRUE;
3439       if (!have_lock)
3440         G_READ_UNLOCK (&type_rw_lock);
3441     }
3442   return match;
3443 }
3444
3445 static gboolean
3446 type_node_is_a_L (TypeNode *node,
3447                   TypeNode *iface_node)
3448 {
3449   return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3450 }
3451
3452 static inline gboolean
3453 type_node_conforms_to_U (TypeNode *node,
3454                          TypeNode *iface_node,
3455                          gboolean  support_interfaces,
3456                          gboolean  support_prerequisites)
3457 {
3458   return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3459 }
3460
3461 /**
3462  * g_type_is_a:
3463  * @type: Type to check anchestry for.
3464  * @is_a_type: Possible anchestor of @type or interface @type could conform to.
3465  *
3466  * If @is_a_type is a derivable type, check whether @type is a
3467  * descendant of @is_a_type.  If @is_a_type is an interface, check
3468  * whether @type conforms to it.
3469  *
3470  * Returns: %TRUE if @type is_a @is_a_type holds true.
3471  */
3472 gboolean
3473 g_type_is_a (GType type,
3474              GType iface_type)
3475 {
3476   TypeNode *node, *iface_node;
3477   gboolean is_a;
3478   
3479   node = lookup_type_node_I (type);
3480   iface_node = lookup_type_node_I (iface_type);
3481   is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3482   
3483   return is_a;
3484 }
3485
3486 /**
3487  * g_type_children:
3488  * @type: The parent type.
3489  * @n_children: Optional #guint pointer to contain the number of child types.
3490  *
3491  * Return a newly allocated and 0-terminated array of type IDs, listing the
3492  * child types of @type. The return value has to be g_free()ed after use.
3493  *
3494  * Returns: Newly allocated and 0-terminated array of child types.
3495  */
3496 GType*
3497 g_type_children (GType  type,
3498                  guint *n_children)
3499 {
3500   TypeNode *node;
3501   
3502   node = lookup_type_node_I (type);
3503   if (node)
3504     {
3505       GType *children;
3506       
3507       G_READ_LOCK (&type_rw_lock);      /* ->children is relocatable */
3508       children = g_new (GType, node->n_children + 1);
3509       memcpy (children, node->children, sizeof (GType) * node->n_children);
3510       children[node->n_children] = 0;
3511       
3512       if (n_children)
3513         *n_children = node->n_children;
3514       G_READ_UNLOCK (&type_rw_lock);
3515       
3516       return children;
3517     }
3518   else
3519     {
3520       if (n_children)
3521         *n_children = 0;
3522       
3523       return NULL;
3524     }
3525 }
3526
3527 /**
3528  * g_type_interfaces:
3529  * @type: The type to list interface types for.
3530  * @n_interfaces: Optional #guint pointer to contain the number of
3531  *                interface types.
3532  *
3533  * Return a newly allocated and 0-terminated array of type IDs, listing the
3534  * interface types that @type conforms to. The return value has to be
3535  * g_free()ed after use.
3536  *
3537  * Returns: Newly allocated and 0-terminated array of interface types.
3538  */
3539 GType*
3540 g_type_interfaces (GType  type,
3541                    guint *n_interfaces)
3542 {
3543   TypeNode *node;
3544   
3545   node = lookup_type_node_I (type);
3546   if (node && node->is_instantiatable)
3547     {
3548       IFaceEntries *entries;
3549       GType *ifaces;
3550       guint i;
3551       
3552       G_READ_LOCK (&type_rw_lock);
3553       entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3554       if (entries)
3555         {
3556           ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3557           for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3558             ifaces[i] = entries->entry[i].iface_type;
3559         }
3560       else
3561         {
3562           ifaces = g_new (GType, 1);
3563           i = 0;
3564         }
3565       ifaces[i] = 0;
3566       
3567       if (n_interfaces)
3568         *n_interfaces = i;
3569       G_READ_UNLOCK (&type_rw_lock);
3570       
3571       return ifaces;
3572     }
3573   else
3574     {
3575       if (n_interfaces)
3576         *n_interfaces = 0;
3577       
3578       return NULL;
3579     }
3580 }
3581
3582 typedef struct _QData QData;
3583 struct _GData
3584 {
3585   guint  n_qdatas;
3586   QData *qdatas;
3587 };
3588 struct _QData
3589 {
3590   GQuark   quark;
3591   gpointer data;
3592 };
3593
3594 static inline gpointer
3595 type_get_qdata_L (TypeNode *node,
3596                   GQuark    quark)
3597 {
3598   GData *gdata = node->global_gdata;
3599   
3600   if (quark && gdata && gdata->n_qdatas)
3601     {
3602       QData *qdatas = gdata->qdatas - 1;
3603       guint n_qdatas = gdata->n_qdatas;
3604       
3605       do
3606         {
3607           guint i;
3608           QData *check;
3609           
3610           i = (n_qdatas + 1) / 2;
3611           check = qdatas + i;
3612           if (quark == check->quark)
3613             return check->data;
3614           else if (quark > check->quark)
3615             {
3616               n_qdatas -= i;
3617               qdatas = check;
3618             }
3619           else /* if (quark < check->quark) */
3620             n_qdatas = i - 1;
3621         }
3622       while (n_qdatas);
3623     }
3624   return NULL;
3625 }
3626
3627 /**
3628  * g_type_get_qdata:
3629  * @type: a #GType
3630  * @quark: a #GQuark id to identify the data
3631  *
3632  * Obtains data which has previously been attached to @type
3633  * with g_type_set_qdata().
3634  *
3635  * Returns: the data, or %NULL if no data was found
3636  */
3637 gpointer
3638 g_type_get_qdata (GType  type,
3639                   GQuark quark)
3640 {
3641   TypeNode *node;
3642   gpointer data;
3643   
3644   node = lookup_type_node_I (type);
3645   if (node)
3646     {
3647       G_READ_LOCK (&type_rw_lock);
3648       data = type_get_qdata_L (node, quark);
3649       G_READ_UNLOCK (&type_rw_lock);
3650     }
3651   else
3652     {
3653       g_return_val_if_fail (node != NULL, NULL);
3654       data = NULL;
3655     }
3656   return data;
3657 }
3658
3659 static inline void
3660 type_set_qdata_W (TypeNode *node,
3661                   GQuark    quark,
3662                   gpointer  data)
3663 {
3664   GData *gdata;
3665   QData *qdata;
3666   guint i;
3667   
3668   /* setup qdata list if necessary */
3669   if (!node->global_gdata)
3670     node->global_gdata = g_new0 (GData, 1);
3671   gdata = node->global_gdata;
3672   
3673   /* try resetting old data */
3674   qdata = gdata->qdatas;
3675   for (i = 0; i < gdata->n_qdatas; i++)
3676     if (qdata[i].quark == quark)
3677       {
3678         qdata[i].data = data;
3679         return;
3680       }
3681   
3682   /* add new entry */
3683   gdata->n_qdatas++;
3684   gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3685   qdata = gdata->qdatas;
3686   for (i = 0; i < gdata->n_qdatas - 1; i++)
3687     if (qdata[i].quark > quark)
3688       break;
3689   g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3690   qdata[i].quark = quark;
3691   qdata[i].data = data;
3692 }
3693
3694 /**
3695  * g_type_set_qdata:
3696  * @type: a #GType
3697  * @quark: a #GQuark id to identify the data
3698  * @data: the data
3699  *
3700  * Attaches arbitrary data to a type.
3701  */
3702 void
3703 g_type_set_qdata (GType    type,
3704                   GQuark   quark,
3705                   gpointer data)
3706 {
3707   TypeNode *node;
3708   
3709   g_return_if_fail (quark != 0);
3710   
3711   node = lookup_type_node_I (type);
3712   if (node)
3713     {
3714       G_WRITE_LOCK (&type_rw_lock);
3715       type_set_qdata_W (node, quark, data);
3716       G_WRITE_UNLOCK (&type_rw_lock);
3717     }
3718   else
3719     g_return_if_fail (node != NULL);
3720 }
3721
3722 static void
3723 type_add_flags_W (TypeNode  *node,
3724                   GTypeFlags flags)
3725 {
3726   guint dflags;
3727   
3728   g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3729   g_return_if_fail (node != NULL);
3730   
3731   if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3732     g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
3733   dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3734   dflags |= flags;
3735   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3736 }
3737
3738 /**
3739  * g_type_query:
3740  * @type: the #GType value of a static, classed type.
3741  * @query: A user provided structure that is filled in with constant values
3742  *  upon success.
3743  *
3744  * Queries the type system for information about a specific type.
3745  * This function will fill in a user-provided structure to hold
3746  * type-specific information. If an invalid #GType is passed in, the
3747  * @type member of the #GTypeQuery is 0. All members filled into the
3748  * #GTypeQuery structure should be considered constant and have to be
3749  * left untouched.
3750  */
3751 void
3752 g_type_query (GType       type,
3753               GTypeQuery *query)
3754 {
3755   TypeNode *node;
3756   
3757   g_return_if_fail (query != NULL);
3758   
3759   /* if node is not static and classed, we won't allow query */
3760   query->type = 0;
3761   node = lookup_type_node_I (type);
3762   if (node && node->is_classed && !node->plugin)
3763     {
3764       /* type is classed and probably even instantiatable */
3765       G_READ_LOCK (&type_rw_lock);
3766       if (node->data)   /* type is static or referenced */
3767         {
3768           query->type = NODE_TYPE (node);
3769           query->type_name = NODE_NAME (node);
3770           query->class_size = node->data->class.class_size;
3771           query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3772         }
3773       G_READ_UNLOCK (&type_rw_lock);
3774     }
3775 }
3776
3777
3778 /* --- implementation details --- */
3779 gboolean
3780 g_type_test_flags (GType type,
3781                    guint flags)
3782 {
3783   TypeNode *node;
3784   gboolean result = FALSE;
3785   
3786   node = lookup_type_node_I (type);
3787   if (node)
3788     {
3789       guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3790       guint tflags = flags & TYPE_FLAG_MASK;
3791       
3792       if (fflags)
3793         {
3794           GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3795           
3796           fflags = (finfo->type_flags & fflags) == fflags;
3797         }
3798       else
3799         fflags = TRUE;
3800       
3801       if (tflags)
3802         {
3803           G_READ_LOCK (&type_rw_lock);
3804           tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3805           G_READ_UNLOCK (&type_rw_lock);
3806         }
3807       else
3808         tflags = TRUE;
3809       
3810       result = tflags && fflags;
3811     }
3812   
3813   return result;
3814 }
3815
3816 /**
3817  * g_type_get_plugin:
3818  * @type: The #GType to retrieve the plugin for.
3819  *
3820  * Returns the #GTypePlugin structure for @type or
3821  * %NULL if @type does not have a #GTypePlugin structure.
3822  *
3823  * Returns: The corresponding plugin if @type is a dynamic type,
3824  *  %NULL otherwise.
3825  */
3826 GTypePlugin*
3827 g_type_get_plugin (GType type)
3828 {
3829   TypeNode *node;
3830   
3831   node = lookup_type_node_I (type);
3832   
3833   return node ? node->plugin : NULL;
3834 }
3835
3836 /**
3837  * g_type_interface_get_plugin:
3838  * @instance_type: the #GType value of an instantiatable type.
3839  * @interface_type: the #GType value of an interface type.
3840  *
3841  * Returns the #GTypePlugin structure for the dynamic interface
3842  * @interface_type which has been added to @instance_type, or %NULL if
3843  * @interface_type has not been added to @instance_type or does not
3844  * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3845  *
3846  * Returns: the #GTypePlugin for the dynamic interface @interface_type
3847  *  of @instance_type.
3848  */
3849 GTypePlugin*
3850 g_type_interface_get_plugin (GType instance_type,
3851                              GType interface_type)
3852 {
3853   TypeNode *node;
3854   TypeNode *iface;
3855   
3856   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);    /* G_TYPE_IS_INTERFACE() is an external call: _U */
3857   
3858   node = lookup_type_node_I (instance_type);  
3859   iface = lookup_type_node_I (interface_type);
3860   if (node && iface)
3861     {
3862       IFaceHolder *iholder;
3863       GTypePlugin *plugin;
3864       
3865       G_READ_LOCK (&type_rw_lock);
3866       
3867       iholder = iface_node_get_holders_L (iface);
3868       while (iholder && iholder->instance_type != instance_type)
3869         iholder = iholder->next;
3870       plugin = iholder ? iholder->plugin : NULL;
3871       
3872       G_READ_UNLOCK (&type_rw_lock);
3873       
3874       return plugin;
3875     }
3876   
3877   g_return_val_if_fail (node == NULL, NULL);
3878   g_return_val_if_fail (iface == NULL, NULL);
3879   
3880   g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3881   
3882   return NULL;
3883 }
3884
3885 /**
3886  * g_type_fundamental_next:
3887  *
3888  * Returns the next free fundamental type id which can be used to
3889  * register a new fundamental type with g_type_register_fundamental().
3890  * The returned type ID represents the highest currently registered
3891  * fundamental type identifier.
3892  *
3893  * Returns: The nextmost fundamental type ID to be registered,
3894  *          or 0 if the type system ran out of fundamental type IDs.
3895  */
3896 GType
3897 g_type_fundamental_next (void)
3898 {
3899   GType type;
3900   
3901   G_READ_LOCK (&type_rw_lock);
3902   type = static_fundamental_next;
3903   G_READ_UNLOCK (&type_rw_lock);
3904   type = G_TYPE_MAKE_FUNDAMENTAL (type);
3905   return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3906 }
3907
3908 /**
3909  * g_type_fundamental:
3910  * @type_id: valid type ID
3911  * 
3912  * Internal function, used to extract the fundamental type ID portion.
3913  * use G_TYPE_FUNDAMENTAL() instead.
3914  * 
3915  * Returns: fundamental type ID
3916  */
3917 GType
3918 g_type_fundamental (GType type_id)
3919 {
3920   TypeNode *node = lookup_type_node_I (type_id);
3921   
3922   return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3923 }
3924
3925 gboolean
3926 g_type_check_instance_is_a (GTypeInstance *type_instance,
3927                             GType          iface_type)
3928 {
3929   TypeNode *node, *iface;
3930   gboolean check;
3931   
3932   if (!type_instance || !type_instance->g_class)
3933     return FALSE;
3934   
3935   node = lookup_type_node_I (type_instance->g_class->g_type);
3936   iface = lookup_type_node_I (iface_type);
3937   check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3938   
3939   return check;
3940 }
3941
3942 gboolean
3943 g_type_check_class_is_a (GTypeClass *type_class,
3944                          GType       is_a_type)
3945 {
3946   TypeNode *node, *iface;
3947   gboolean check;
3948   
3949   if (!type_class)
3950     return FALSE;
3951   
3952   node = lookup_type_node_I (type_class->g_type);
3953   iface = lookup_type_node_I (is_a_type);
3954   check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3955   
3956   return check;
3957 }
3958
3959 GTypeInstance*
3960 g_type_check_instance_cast (GTypeInstance *type_instance,
3961                             GType          iface_type)
3962 {
3963   if (type_instance)
3964     {
3965       if (type_instance->g_class)
3966         {
3967           TypeNode *node, *iface;
3968           gboolean is_instantiatable, check;
3969           
3970           node = lookup_type_node_I (type_instance->g_class->g_type);
3971           is_instantiatable = node && node->is_instantiatable;
3972           iface = lookup_type_node_I (iface_type);
3973           check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3974           if (check)
3975             return type_instance;
3976           
3977           if (is_instantiatable)
3978             g_warning ("invalid cast from `%s' to `%s'",
3979                        type_descriptive_name_I (type_instance->g_class->g_type),
3980                        type_descriptive_name_I (iface_type));
3981           else
3982             g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3983                        type_descriptive_name_I (type_instance->g_class->g_type),
3984                        type_descriptive_name_I (iface_type));
3985         }
3986       else
3987         g_warning ("invalid unclassed pointer in cast to `%s'",
3988                    type_descriptive_name_I (iface_type));
3989     }
3990   
3991   return type_instance;
3992 }
3993
3994 GTypeClass*
3995 g_type_check_class_cast (GTypeClass *type_class,
3996                          GType       is_a_type)
3997 {
3998   if (type_class)
3999     {
4000       TypeNode *node, *iface;
4001       gboolean is_classed, check;
4002       
4003       node = lookup_type_node_I (type_class->g_type);
4004       is_classed = node && node->is_classed;
4005       iface = lookup_type_node_I (is_a_type);
4006       check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4007       if (check)
4008         return type_class;
4009       
4010       if (is_classed)
4011         g_warning ("invalid class cast from `%s' to `%s'",
4012                    type_descriptive_name_I (type_class->g_type),
4013                    type_descriptive_name_I (is_a_type));
4014       else
4015         g_warning ("invalid unclassed type `%s' in class cast to `%s'",
4016                    type_descriptive_name_I (type_class->g_type),
4017                    type_descriptive_name_I (is_a_type));
4018     }
4019   else
4020     g_warning ("invalid class cast from (NULL) pointer to `%s'",
4021                type_descriptive_name_I (is_a_type));
4022   return type_class;
4023 }
4024
4025 /**
4026  * g_type_check_instance:
4027  * @instance: A valid #GTypeInstance structure.
4028  *
4029  * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
4030  * macro.
4031  *
4032  * @Returns:  #TRUE if @instance is valid, #FALSE otherwise.
4033  */
4034 gboolean
4035 g_type_check_instance (GTypeInstance *type_instance)
4036 {
4037   /* this function is just here to make the signal system
4038    * conveniently elaborated on instance checks
4039    */
4040   if (type_instance)
4041     {
4042       if (type_instance->g_class)
4043         {
4044           TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4045           
4046           if (node && node->is_instantiatable)
4047             return TRUE;
4048           
4049           g_warning ("instance of invalid non-instantiatable type `%s'",
4050                      type_descriptive_name_I (type_instance->g_class->g_type));
4051         }
4052       else
4053         g_warning ("instance with invalid (NULL) class pointer");
4054     }
4055   else
4056     g_warning ("invalid (NULL) pointer instance");
4057   
4058   return FALSE;
4059 }
4060
4061 static inline gboolean
4062 type_check_is_value_type_U (GType type)
4063 {
4064   GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4065   TypeNode *node;
4066   
4067   /* common path speed up */
4068   node = lookup_type_node_I (type);
4069   if (node && node->mutatable_check_cache)
4070     return TRUE;
4071   
4072   G_READ_LOCK (&type_rw_lock);
4073  restart_check:
4074   if (node)
4075     {
4076       if (node->data && NODE_REFCOUNT (node) > 0 &&
4077           node->data->common.value_table->value_init)
4078         tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4079       else if (NODE_IS_IFACE (node))
4080         {
4081           guint i;
4082           
4083           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4084             {
4085               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4086               TypeNode *prnode = lookup_type_node_I (prtype);
4087               
4088               if (prnode->is_instantiatable)
4089                 {
4090                   type = prtype;
4091                   node = lookup_type_node_I (type);
4092                   goto restart_check;
4093                 }
4094             }
4095         }
4096     }
4097   G_READ_UNLOCK (&type_rw_lock);
4098   
4099   return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4100 }
4101
4102 gboolean
4103 g_type_check_is_value_type (GType type)
4104 {
4105   return type_check_is_value_type_U (type);
4106 }
4107
4108 gboolean
4109 g_type_check_value (GValue *value)
4110 {
4111   return value && type_check_is_value_type_U (value->g_type);
4112 }
4113
4114 gboolean
4115 g_type_check_value_holds (GValue *value,
4116                           GType   type)
4117 {
4118   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4119 }
4120
4121 /**
4122  * g_type_value_table_peek:
4123  * @type: A #GType value.
4124  *
4125  * Returns the location of the #GTypeValueTable associated with @type.
4126  * <emphasis>Note that this function should only be used from source code
4127  * that implements or has internal knowledge of the implementation of
4128  * @type.</emphasis>
4129  *
4130  * Returns: Location of the #GTypeValueTable associated with @type or
4131  *  %NULL if there is no #GTypeValueTable associated with @type.
4132  */
4133 GTypeValueTable*
4134 g_type_value_table_peek (GType type)
4135 {
4136   GTypeValueTable *vtable = NULL;
4137   TypeNode *node = lookup_type_node_I (type);
4138   gboolean has_refed_data, has_table;
4139
4140   if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4141     return node->data->common.value_table;
4142
4143   G_READ_LOCK (&type_rw_lock);
4144   
4145  restart_table_peek:
4146   has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4147   has_table = has_refed_data && node->data->common.value_table->value_init;
4148   if (has_refed_data)
4149     {
4150       if (has_table)
4151         vtable = node->data->common.value_table;
4152       else if (NODE_IS_IFACE (node))
4153         {
4154           guint i;
4155           
4156           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4157             {
4158               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4159               TypeNode *prnode = lookup_type_node_I (prtype);
4160               
4161               if (prnode->is_instantiatable)
4162                 {
4163                   type = prtype;
4164                   node = lookup_type_node_I (type);
4165                   goto restart_table_peek;
4166                 }
4167             }
4168         }
4169     }
4170   
4171   G_READ_UNLOCK (&type_rw_lock);
4172   
4173   if (vtable)
4174     return vtable;
4175   
4176   if (!node)
4177     g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
4178   if (!has_refed_data)
4179     g_warning ("can't peek value table for type `%s' which is not currently referenced",
4180                type_descriptive_name_I (type));
4181   
4182   return NULL;
4183 }
4184
4185 G_CONST_RETURN gchar*
4186 g_type_name_from_instance (GTypeInstance *instance)
4187 {
4188   if (!instance)
4189     return "<NULL-instance>";
4190   else
4191     return g_type_name_from_class (instance->g_class);
4192 }
4193
4194 G_CONST_RETURN gchar*
4195 g_type_name_from_class (GTypeClass *g_class)
4196 {
4197   if (!g_class)
4198     return "<NULL-class>";
4199   else
4200     return g_type_name (g_class->g_type);
4201 }
4202
4203
4204 /* --- private api for gboxed.c --- */
4205 gpointer
4206 _g_type_boxed_copy (GType type, gpointer value)
4207 {
4208   TypeNode *node = lookup_type_node_I (type);
4209
4210   return node->data->boxed.copy_func (value);
4211 }
4212
4213 void
4214 _g_type_boxed_free (GType type, gpointer value)
4215 {
4216   TypeNode *node = lookup_type_node_I (type);
4217
4218   node->data->boxed.free_func (value);
4219 }
4220
4221 void
4222 _g_type_boxed_init (GType          type,
4223                     GBoxedCopyFunc copy_func,
4224                     GBoxedFreeFunc free_func)
4225 {
4226   TypeNode *node = lookup_type_node_I (type);
4227
4228   node->data->boxed.copy_func = copy_func;
4229   node->data->boxed.free_func = free_func;
4230 }
4231
4232 /* --- initialization --- */
4233 /**
4234  * g_type_init_with_debug_flags:
4235  * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
4236  *               debugging purposes.
4237  *
4238  * Similar to g_type_init(), but additionally sets debug flags.
4239  */
4240 void
4241 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4242 {
4243   G_LOCK_DEFINE_STATIC (type_init_lock);
4244   const gchar *env_string;
4245   GTypeInfo info;
4246   TypeNode *node;
4247   volatile GType votype;
4248
4249 #ifdef G_THREADS_ENABLED
4250   if (!g_thread_get_initialized())
4251     g_thread_init (NULL);
4252 #endif
4253
4254   G_LOCK (type_init_lock);
4255   
4256   G_WRITE_LOCK (&type_rw_lock);
4257   
4258   if (static_quark_type_flags)
4259     {
4260       G_WRITE_UNLOCK (&type_rw_lock);
4261       G_UNLOCK (type_init_lock);
4262       return;
4263     }
4264   
4265   /* setup GObject library wide debugging flags */
4266   _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
4267   env_string = g_getenv ("GOBJECT_DEBUG");
4268   if (env_string != NULL)
4269     {
4270       static GDebugKey debug_keys[] = {
4271         { "objects", G_TYPE_DEBUG_OBJECTS },
4272         { "signals", G_TYPE_DEBUG_SIGNALS },
4273       };
4274       
4275       _g_type_debug_flags |= g_parse_debug_string (env_string,
4276                                                    debug_keys,
4277                                                    sizeof (debug_keys) / sizeof (debug_keys[0]));
4278       env_string = NULL;
4279     }
4280   
4281   /* quarks */
4282   static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4283   static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4284   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4285   
4286   /* type qname hash table */
4287   static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
4288   
4289   /* invalid type G_TYPE_INVALID (0)
4290    */
4291   static_fundamental_type_nodes[0] = NULL;
4292   
4293   /* void type G_TYPE_NONE
4294    */
4295   node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4296   votype = NODE_TYPE (node);
4297   g_assert (votype == G_TYPE_NONE);
4298   
4299   /* interface fundamental type G_TYPE_INTERFACE (!classed)
4300    */
4301   memset (&info, 0, sizeof (info));
4302   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4303   votype = NODE_TYPE (node);
4304   type_data_make_W (node, &info, NULL);
4305   g_assert (votype == G_TYPE_INTERFACE);
4306   
4307   G_WRITE_UNLOCK (&type_rw_lock);
4308   
4309   g_value_c_init ();
4310
4311   /* G_TYPE_TYPE_PLUGIN
4312    */
4313   votype = g_type_plugin_get_type ();
4314   
4315   /* G_TYPE_* value types
4316    */
4317   g_value_types_init ();
4318   
4319   /* G_TYPE_ENUM & G_TYPE_FLAGS
4320    */
4321   g_enum_types_init ();
4322   
4323   /* G_TYPE_BOXED
4324    */
4325   g_boxed_type_init ();
4326   
4327   /* G_TYPE_PARAM
4328    */
4329   g_param_type_init ();
4330   
4331   /* G_TYPE_OBJECT
4332    */
4333   g_object_type_init ();
4334   
4335   /* G_TYPE_PARAM_* pspec types
4336    */
4337   g_param_spec_types_init ();
4338   
4339   /* Value Transformations
4340    */
4341   g_value_transforms_init ();
4342   
4343   /* Signal system
4344    */
4345   g_signal_init ();
4346   
4347   G_UNLOCK (type_init_lock);
4348 }
4349
4350 /**
4351  * g_type_init:
4352  *
4353  * Prior to any use of the type system, g_type_init() has to be called
4354  * to initialize the type system and assorted other code portions
4355  * (such as the various fundamental type implementations or the signal
4356  * system).
4357  *
4358  * Since version 2.24 this also initializes the thread system
4359  */
4360 void
4361 g_type_init (void)
4362 {
4363   g_type_init_with_debug_flags (0);
4364 }
4365
4366 /**
4367  * g_type_class_add_private:
4368  * @g_class: class structure for an instantiatable type
4369  * @private_size: size of private structure.
4370  *
4371  * Registers a private structure for an instantiatable type;
4372  * when an object is allocated, the private structures for
4373  * the type and all of its parent types are allocated
4374  * sequentially in the same memory block as the public
4375  * structures. This function should be called in the
4376  * type's class_init() function. The private structure can
4377  * be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.
4378  * The following example shows attaching a private structure
4379  * <structname>MyObjectPrivate</structname> to an object
4380  * <structname>MyObject</structname> defined in the standard GObject
4381  * fashion.
4382  * type's class_init() function.
4383  *
4384  * |[
4385  * typedef struct _MyObject        MyObject;
4386  * typedef struct _MyObjectPrivate MyObjectPrivate;
4387  *
4388  * struct _MyObject {
4389  *  GObject parent;
4390  *
4391  *  MyObjectPrivate *priv;
4392  * };
4393  *
4394  * struct _MyObjectPrivate {
4395  *   int some_field;
4396  * };
4397  *
4398  * static void
4399  * my_object_class_init (MyObjectClass *klass)
4400  * {
4401  *   g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4402  * }
4403  *
4404  * static void
4405  * my_object_init (MyObject *my_object)
4406  * {
4407  *   my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4408  *                                                  MY_TYPE_OBJECT,
4409  *                                                  MyObjectPrivate);
4410  * }
4411  *
4412  * static int
4413  * my_object_get_some_field (MyObject *my_object)
4414  * {
4415  *   MyObjectPrivate *priv = my_object->priv;
4416  *
4417  *   return priv->some_field;
4418  * }
4419  * ]|
4420  *
4421  * Since: 2.4
4422  */
4423 void
4424 g_type_class_add_private (gpointer g_class,
4425                           gsize    private_size)
4426 {
4427   GType instance_type = ((GTypeClass *)g_class)->g_type;
4428   TypeNode *node = lookup_type_node_I (instance_type);
4429   gsize offset;
4430
4431   g_return_if_fail (private_size > 0);
4432
4433   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4434     {
4435       g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4436                  type_descriptive_name_I (instance_type));
4437       return;
4438     }
4439
4440   if (NODE_PARENT_TYPE (node))
4441     {
4442       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4443       if (node->data->instance.private_size != pnode->data->instance.private_size)
4444         {
4445           g_warning ("g_type_add_private() called multiple times for the same type");
4446           return;
4447         }
4448     }
4449   
4450   G_WRITE_LOCK (&type_rw_lock);
4451
4452   offset = ALIGN_STRUCT (node->data->instance.private_size);
4453   node->data->instance.private_size = offset + private_size;
4454   
4455   G_WRITE_UNLOCK (&type_rw_lock);
4456 }
4457
4458 gpointer
4459 g_type_instance_get_private (GTypeInstance *instance,
4460                              GType          private_type)
4461 {
4462   TypeNode *instance_node;
4463   TypeNode *private_node;
4464   TypeNode *parent_node;
4465   GTypeClass *class;
4466   gsize offset;
4467
4468   g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4469
4470   /* while instances are initialized, their class pointers change,
4471    * so figure the instances real class first
4472    */
4473   class = instance_real_class_get (instance);
4474   if (!class)
4475     class = instance->g_class;
4476
4477   instance_node = lookup_type_node_I (class->g_type);
4478   if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
4479     {
4480       g_warning ("instance of invalid non-instantiatable type `%s'",
4481                  type_descriptive_name_I (instance->g_class->g_type));
4482       return NULL;
4483     }
4484
4485   private_node = lookup_type_node_I (private_type);
4486   if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
4487     {
4488       g_warning ("attempt to retrieve private data for invalid type '%s'",
4489                  type_descriptive_name_I (private_type));
4490       return NULL;
4491     }
4492
4493   /* Note that we don't need a read lock, since instance existing
4494    * means that the instance class and all parent classes
4495    * exist, so the node->data, node->data->instance.instance_size,
4496    * and node->data->instance.private_size are not going to be changed.
4497    * for any of the relevant types.
4498    */
4499
4500   offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
4501
4502   if (NODE_PARENT_TYPE (private_node))
4503     {
4504       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4505       g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4506
4507       if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
4508         {
4509           g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
4510           return NULL;
4511         }
4512
4513       offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
4514     }
4515
4516   return G_STRUCT_MEMBER_P (instance, offset);
4517 }
4518
4519 /**
4520  * g_type_add_class_private:
4521  * @class_type: GType of an classed type.
4522  * @private_size: size of private structure.
4523  *
4524  * Registers a private class structure for a classed type;
4525  * when the class is allocated, the private structures for
4526  * the class and all of its parent types are allocated
4527  * sequentially in the same memory block as the public
4528  * structures. This function should be called in the
4529  * type's get_type() function after the type is registered.
4530  * The private structure can be retrieved using the
4531  * G_TYPE_CLASS_GET_PRIVATE() macro.
4532  *
4533  * Since: 2.24
4534  */
4535 void
4536 g_type_add_class_private (GType    class_type,
4537                           gsize    private_size)
4538 {
4539   TypeNode *node = lookup_type_node_I (class_type);
4540   gsize offset;
4541
4542   g_return_if_fail (private_size > 0);
4543
4544   if (!node || !node->is_classed || !node->data)
4545     {
4546       g_warning ("cannot add class private field to invalid type '%s'",
4547                  type_descriptive_name_I (class_type));
4548       return;
4549     }
4550
4551   if (NODE_PARENT_TYPE (node))
4552     {
4553       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4554       if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4555         {
4556           g_warning ("g_type_add_class_private() called multiple times for the same type");
4557           return;
4558         }
4559     }
4560   
4561   G_WRITE_LOCK (&type_rw_lock);
4562
4563   offset = ALIGN_STRUCT (node->data->class.class_private_size);
4564   node->data->class.class_private_size = offset + private_size;
4565
4566   G_WRITE_UNLOCK (&type_rw_lock);
4567 }
4568
4569 gpointer
4570 g_type_class_get_private (GTypeClass *klass,
4571                           GType       private_type)
4572 {
4573   TypeNode *class_node;
4574   TypeNode *private_node;
4575   TypeNode *parent_node;
4576   gsize offset;
4577
4578   g_return_val_if_fail (klass != NULL, NULL);
4579
4580   class_node = lookup_type_node_I (klass->g_type);
4581   if (G_UNLIKELY (!class_node || !class_node->is_classed))
4582     {
4583       g_warning ("class of invalid type `%s'",
4584                  type_descriptive_name_I (klass->g_type));
4585       return NULL;
4586     }
4587
4588   private_node = lookup_type_node_I (private_type);
4589   if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4590     {
4591       g_warning ("attempt to retrieve private data for invalid type '%s'",
4592                  type_descriptive_name_I (private_type));
4593       return NULL;
4594     }
4595
4596   offset = ALIGN_STRUCT (class_node->data->class.class_size);
4597
4598   if (NODE_PARENT_TYPE (private_node))
4599     {
4600       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4601       g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4602
4603       if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4604         {
4605           g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_class_add_class_private()");
4606           return NULL;
4607         }
4608
4609       offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4610     }
4611
4612   return G_STRUCT_MEMBER_P (klass, offset);
4613 }
4614
4615 #define __G_TYPE_C__
4616 #include "gobjectaliasdef.c"