Use <malloc.h> also with Digital Mars compiler on Win32. (#346808, Serhat
[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 #include        <config.h>
20 #include        "gtype.h"
21
22 /*
23  * MT safe
24  */
25
26 #include        "gtypeplugin.h"
27 #include        "gvaluecollector.h"
28 #include        "gbsearcharray.h"
29 #include        <string.h>
30
31 #include        "gobjectalias.h"
32
33 /* NOTE: some functions (some internal variants and exported ones)
34  * invalidate data portions of the TypeNodes. if external functions/callbacks
35  * are called, pointers to memory maintained by TypeNodes have to be looked up
36  * again. this affects most of the struct TypeNode fields, e.g. ->children or
37  * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
38  * not ->supers[]), as all those memory portions can get realloc()ed during
39  * callback invocation.
40  *
41  * TODO:
42  * - g_type_from_name() should do an ordered array lookup after fetching the
43  *   the quark, instead of a second hashtable lookup.
44  *
45  * LOCKING:
46  * lock handling issues when calling static functions are indicated by
47  * uppercase letter postfixes, all static functions have to have
48  * one of the below postfixes:
49  * - _I:        [Indifferent about locking]
50  *   function doesn't care about locks at all
51  * - _U:        [Unlocked invocation]
52  *   no read or write lock has to be held across function invocation
53  *   (locks may be acquired and released during invocation though)
54  * - _L:        [Locked invocation]
55  *   a write lock or more than 0 read locks have to be held across
56  *   function invocation
57  * - _W:        [Write-locked invocation]
58  *   a write lock has to be held across function invocation
59  * - _Wm:       [Write-locked invocation, mutatable]
60  *   like _W, but the write lock might be released and reacquired
61  *   during invocation, watch your pointers
62  */
63
64 static GStaticRWLock            type_rw_lock = G_STATIC_RW_LOCK_INIT;
65 #ifdef LOCK_DEBUG
66 #define G_READ_LOCK(rw_lock)    do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
67 #define G_READ_UNLOCK(rw_lock)  do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
68 #define G_WRITE_LOCK(rw_lock)   do { g_printerr (G_STRLOC ": writeL++\n"); g_static_rw_lock_writer_lock (rw_lock); } while (0)
69 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_static_rw_lock_writer_unlock (rw_lock); } while (0)
70 #else
71 #define G_READ_LOCK(rw_lock)    g_static_rw_lock_reader_lock (rw_lock)
72 #define G_READ_UNLOCK(rw_lock)  g_static_rw_lock_reader_unlock (rw_lock)
73 #define G_WRITE_LOCK(rw_lock)   g_static_rw_lock_writer_lock (rw_lock)
74 #define G_WRITE_UNLOCK(rw_lock) g_static_rw_lock_writer_unlock (rw_lock)
75 #endif
76 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
77     static const gchar _action[] = " invalidly modified type ";  \
78     gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
79     if (_arg) \
80       g_error ("%s(%p)%s`%s'", _fname, _arg, _action, _tname); \
81     else \
82       g_error ("%s()%s`%s'", _fname, _action, _tname); \
83 }G_STMT_END
84 #define g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{     \
85   if (!(condition))                                                                             \
86     {                                                                                           \
87       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,                                                \
88              "%s: initialization assertion failed, use %s() prior to this function",            \
89              G_STRLOC, G_STRINGIFY (init_function));                                            \
90       return (return_value);                                                                    \
91     }                                                                                           \
92 }G_STMT_END
93
94 #ifdef  G_ENABLE_DEBUG
95 #define DEBUG_CODE(debug_type, code_block)  G_STMT_START {    \
96     if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) \
97       { code_block; }                                     \
98 } G_STMT_END
99 #else /* !G_ENABLE_DEBUG */
100 #define DEBUG_CODE(debug_type, code_block)  /* code_block */
101 #endif  /* G_ENABLE_DEBUG */
102
103 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
104                                     G_TYPE_FLAG_INSTANTIATABLE | \
105                                     G_TYPE_FLAG_DERIVABLE | \
106                                     G_TYPE_FLAG_DEEP_DERIVABLE)
107 #define TYPE_FLAG_MASK             (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
108 #define SIZEOF_FUNDAMENTAL_INFO    ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
109                                                        sizeof (gpointer)), \
110                                                   sizeof (glong)))
111
112 /* The 2*sizeof(size_t) alignment here is borrowed from
113  * GNU libc, so it should be good most everywhere.
114  * It is more conservative than is needed on some 64-bit
115  * platforms, but ia64 does require a 16-byte alignment.
116  * The SIMD extensions for x86 and ppc32 would want a
117  * larger alignment than this, but we don't need to
118  * do better than malloc.
119  */
120 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
121 #define ALIGN_STRUCT(offset) \
122       ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
123
124
125 /* --- typedefs --- */
126 typedef struct _TypeNode        TypeNode;
127 typedef struct _CommonData      CommonData;
128 typedef struct _IFaceData       IFaceData;
129 typedef struct _ClassData       ClassData;
130 typedef struct _InstanceData    InstanceData;
131 typedef union  _TypeData        TypeData;
132 typedef struct _IFaceEntry      IFaceEntry;
133 typedef struct _IFaceHolder     IFaceHolder;
134
135
136 /* --- prototypes --- */
137 static inline GTypeFundamentalInfo*     type_node_fundamental_info_I    (TypeNode               *node);
138 static        void                      type_add_flags_W                (TypeNode               *node,
139                                                                          GTypeFlags              flags);
140 static        void                      type_data_make_W                (TypeNode               *node,
141                                                                          const GTypeInfo        *info,
142                                                                          const GTypeValueTable  *value_table);
143 static inline void                      type_data_ref_Wm                (TypeNode               *node);
144 static inline void                      type_data_unref_Wm              (TypeNode               *node,
145                                                                          gboolean                uncached);
146 static void                             type_data_last_unref_Wm         (GType                   type,
147                                                                          gboolean                uncached);
148 static inline gpointer                  type_get_qdata_L                (TypeNode               *node,
149                                                                          GQuark                  quark);
150 static inline void                      type_set_qdata_W                (TypeNode               *node,
151                                                                          GQuark                  quark,
152                                                                          gpointer                data);
153 static IFaceHolder*                     type_iface_peek_holder_L        (TypeNode               *iface,
154                                                                          GType                   instance_type);
155 static gboolean                         type_iface_vtable_base_init_Wm  (TypeNode               *iface,
156                                                                          TypeNode               *node);
157 static void                             type_iface_vtable_iface_init_Wm (TypeNode               *iface,
158                                                                          TypeNode               *node);
159 static gboolean                         type_node_is_a_L                (TypeNode               *node,
160                                                                          TypeNode               *iface_node);
161
162
163 /* --- enumeration --- */
164
165 /* The InitState enumeration is used to track the progress of initializing
166  * both classes and interface vtables. Keeping the state of initialization
167  * is necessary to handle new interfaces being added while we are initializing
168  * the class or other interfaces.
169  */
170 typedef enum
171 {
172   UNINITIALIZED,
173   BASE_CLASS_INIT,
174   BASE_IFACE_INIT,
175   CLASS_INIT,
176   IFACE_INIT,
177   INITIALIZED
178 } InitState;
179
180 /* --- structures --- */
181 struct _TypeNode
182 {
183   GTypePlugin *plugin;
184   guint        n_children : 12;
185   guint        n_supers : 8;
186   guint        _prot_n_ifaces_prerequisites : 9;
187   guint        is_classed : 1;
188   guint        is_instantiatable : 1;
189   guint        mutatable_check_cache : 1;       /* combines some common path checks */
190   GType       *children;
191   TypeData * volatile data;
192   GQuark       qname;
193   GData       *global_gdata;
194   union {
195     IFaceEntry  *iface_entries;         /* for !iface types */
196     GType       *prerequisistes;
197   } _prot;
198   GType        supers[1]; /* flexible array */
199 };
200 #define SIZEOF_BASE_TYPE_NODE()                 (G_STRUCT_OFFSET (TypeNode, supers))
201 #define MAX_N_SUPERS                            (255)
202 #define MAX_N_CHILDREN                          (4095)
203 #define MAX_N_IFACES                            (511)
204 #define MAX_N_PREREQUISITES                     (MAX_N_IFACES)
205 #define NODE_TYPE(node)                         (node->supers[0])
206 #define NODE_PARENT_TYPE(node)                  (node->supers[1])
207 #define NODE_FUNDAMENTAL_TYPE(node)             (node->supers[node->n_supers])
208 #define NODE_NAME(node)                         (g_quark_to_string (node->qname))
209 #define NODE_IS_IFACE(node)                     (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
210 #define CLASSED_NODE_N_IFACES(node)             ((node)->_prot_n_ifaces_prerequisites)
211 #define CLASSED_NODE_IFACES_ENTRIES(node)       ((node)->_prot.iface_entries)
212 #define IFACE_NODE_N_PREREQUISITES(node)        ((node)->_prot_n_ifaces_prerequisites)
213 #define IFACE_NODE_PREREQUISITES(node)          ((node)->_prot.prerequisistes)
214 #define iface_node_get_holders_L(node)          ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
215 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
216 #define iface_node_get_dependants_array_L(n)    ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
217 #define iface_node_set_dependants_array_W(n,d)  (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
218 #define TYPE_ID_MASK                            ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
219
220 #define NODE_IS_ANCESTOR(ancestor, node)                                                    \
221         ((ancestor)->n_supers <= (node)->n_supers &&                                        \
222          (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
223
224
225 struct _IFaceHolder
226 {
227   GType           instance_type;
228   GInterfaceInfo *info;
229   GTypePlugin    *plugin;
230   IFaceHolder    *next;
231 };
232 struct _IFaceEntry
233 {
234   GType           iface_type;
235   GTypeInterface *vtable;
236   InitState       init_state;
237 };
238 struct _CommonData
239 {
240   guint             ref_count;
241   GTypeValueTable  *value_table;
242 };
243 struct _IFaceData
244 {
245   CommonData         common;
246   guint16            vtable_size;
247   GBaseInitFunc      vtable_init_base;
248   GBaseFinalizeFunc  vtable_finalize_base;
249   GClassInitFunc     dflt_init;
250   GClassFinalizeFunc dflt_finalize;
251   gconstpointer      dflt_data;
252   gpointer           dflt_vtable;
253 };
254 struct _ClassData
255 {
256   CommonData         common;
257   guint16            class_size;
258   guint              init_state : 4;
259   GBaseInitFunc      class_init_base;
260   GBaseFinalizeFunc  class_finalize_base;
261   GClassInitFunc     class_init;
262   GClassFinalizeFunc class_finalize;
263   gconstpointer      class_data;
264   gpointer           class;
265 };
266 struct _InstanceData
267 {
268   CommonData         common;
269   guint16            class_size;
270   guint              init_state : 4;
271   GBaseInitFunc      class_init_base;
272   GBaseFinalizeFunc  class_finalize_base;
273   GClassInitFunc     class_init;
274   GClassFinalizeFunc class_finalize;
275   gconstpointer      class_data;
276   gpointer           class;
277   guint16            instance_size;
278   guint16            private_size;
279   guint16            n_preallocs;
280   GInstanceInitFunc  instance_init;
281 };
282 union _TypeData
283 {
284   CommonData         common;
285   IFaceData          iface;
286   ClassData          class;
287   InstanceData       instance;
288 };
289 typedef struct {
290   gpointer            cache_data;
291   GTypeClassCacheFunc cache_func;
292 } ClassCacheFunc;
293 typedef struct {
294   gpointer                check_data;
295   GTypeInterfaceCheckFunc check_func;
296 } IFaceCheckFunc;
297
298
299 /* --- variables --- */
300 static guint           static_n_class_cache_funcs = 0;
301 static ClassCacheFunc *static_class_cache_funcs = NULL;
302 static guint           static_n_iface_check_funcs = 0;
303 static IFaceCheckFunc *static_iface_check_funcs = NULL;
304 static GQuark          static_quark_type_flags = 0;
305 static GQuark          static_quark_iface_holder = 0;
306 static GQuark          static_quark_dependants_array = 0;
307 GTypeDebugFlags        _g_type_debug_flags = 0;
308
309
310 /* --- type nodes --- */
311 static GHashTable       *static_type_nodes_ht = NULL;
312 static TypeNode         *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
313 static GType             static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
314
315 static inline TypeNode*
316 lookup_type_node_I (register GType utype)
317 {
318   if (utype > G_TYPE_FUNDAMENTAL_MAX)
319     return (TypeNode*) (utype & ~TYPE_ID_MASK);
320   else
321     return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
322 }
323
324 static TypeNode*
325 type_node_any_new_W (TypeNode             *pnode,
326                      GType                 ftype,
327                      const gchar          *name,
328                      GTypePlugin          *plugin,
329                      GTypeFundamentalFlags type_flags)
330 {
331   guint n_supers;
332   GType type;
333   TypeNode *node;
334   guint i, node_size = 0;
335   
336   n_supers = pnode ? pnode->n_supers + 1 : 0;
337   
338   if (!pnode)
339     node_size += SIZEOF_FUNDAMENTAL_INFO;             /* fundamental type info */
340   node_size += SIZEOF_BASE_TYPE_NODE ();              /* TypeNode structure */
341   node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
342   node = g_malloc0 (node_size);
343   if (!pnode)                                         /* offset fundamental types */
344     {
345       node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
346       static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
347       type = ftype;
348     }
349   else
350     type = (GType) node;
351   
352   g_assert ((type & TYPE_ID_MASK) == 0);
353   
354   node->n_supers = n_supers;
355   if (!pnode)
356     {
357       node->supers[0] = type;
358       node->supers[1] = 0;
359       
360       node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
361       node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
362       
363       if (NODE_IS_IFACE (node))
364         {
365           IFACE_NODE_N_PREREQUISITES (node) = 0;
366           IFACE_NODE_PREREQUISITES (node) = NULL;
367         }
368       else
369         {
370           CLASSED_NODE_N_IFACES (node) = 0;
371           CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
372         }
373     }
374   else
375     {
376       node->supers[0] = type;
377       memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
378       
379       node->is_classed = pnode->is_classed;
380       node->is_instantiatable = pnode->is_instantiatable;
381       
382       if (NODE_IS_IFACE (node))
383         {
384           IFACE_NODE_N_PREREQUISITES (node) = 0;
385           IFACE_NODE_PREREQUISITES (node) = NULL;
386         }
387       else
388         {
389           guint j;
390           
391           CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
392           CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
393                                                          sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
394                                                          CLASSED_NODE_N_IFACES (node));
395           for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
396             {
397               CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
398               CLASSED_NODE_IFACES_ENTRIES (node)[j].init_state = UNINITIALIZED;
399             }
400         }
401       
402       i = pnode->n_children++;
403       pnode->children = g_renew (GType, pnode->children, pnode->n_children);
404       pnode->children[i] = type;
405     }
406   
407   node->plugin = plugin;
408   node->n_children = 0;
409   node->children = NULL;
410   node->data = NULL;
411   node->qname = g_quark_from_string (name);
412   node->global_gdata = NULL;
413   
414   g_hash_table_insert (static_type_nodes_ht,
415                        GUINT_TO_POINTER (node->qname),
416                        (gpointer) type);
417   return node;
418 }
419
420 static inline GTypeFundamentalInfo*
421 type_node_fundamental_info_I (TypeNode *node)
422 {
423   GType ftype = NODE_FUNDAMENTAL_TYPE (node);
424   
425   if (ftype != NODE_TYPE (node))
426     node = lookup_type_node_I (ftype);
427   
428   return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
429 }
430
431 static TypeNode*
432 type_node_fundamental_new_W (GType                 ftype,
433                              const gchar          *name,
434                              GTypeFundamentalFlags type_flags)
435 {
436   GTypeFundamentalInfo *finfo;
437   TypeNode *node;
438   
439   g_assert ((ftype & TYPE_ID_MASK) == 0);
440   g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
441   
442   if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
443     static_fundamental_next++;
444   
445   type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
446   
447   node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
448   
449   finfo = type_node_fundamental_info_I (node);
450   finfo->type_flags = type_flags;
451   
452   return node;
453 }
454
455 static TypeNode*
456 type_node_new_W (TypeNode    *pnode,
457                  const gchar *name,
458                  GTypePlugin *plugin)
459      
460 {
461   g_assert (pnode);
462   g_assert (pnode->n_supers < MAX_N_SUPERS);
463   g_assert (pnode->n_children < MAX_N_CHILDREN);
464   
465   return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
466 }
467
468 static inline IFaceEntry*
469 type_lookup_iface_entry_L (TypeNode *node,
470                            TypeNode *iface_node)
471 {
472   if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
473     {
474       IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
475       guint n_ifaces = CLASSED_NODE_N_IFACES (node);
476       GType iface_type = NODE_TYPE (iface_node);
477       
478       do
479         {
480           guint i;
481           IFaceEntry *check;
482           
483           i = (n_ifaces + 1) >> 1;
484           check = ifaces + i;
485           if (iface_type == check->iface_type)
486             return check;
487           else if (iface_type > check->iface_type)
488             {
489               n_ifaces -= i;
490               ifaces = check;
491             }
492           else /* if (iface_type < check->iface_type) */
493             n_ifaces = i - 1;
494         }
495       while (n_ifaces);
496     }
497   
498   return NULL;
499 }
500
501 static inline gboolean
502 type_lookup_prerequisite_L (TypeNode *iface,
503                             GType     prerequisite_type)
504 {
505   if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
506     {
507       GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
508       guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
509       
510       do
511         {
512           guint i;
513           GType *check;
514           
515           i = (n_prerequisites + 1) >> 1;
516           check = prerequisites + i;
517           if (prerequisite_type == *check)
518             return TRUE;
519           else if (prerequisite_type > *check)
520             {
521               n_prerequisites -= i;
522               prerequisites = check;
523             }
524           else /* if (prerequisite_type < *check) */
525             n_prerequisites = i - 1;
526         }
527       while (n_prerequisites);
528     }
529   return FALSE;
530 }
531
532 static gchar*
533 type_descriptive_name_I (GType type)
534 {
535   if (type)
536     {
537       TypeNode *node = lookup_type_node_I (type);
538       
539       return node ? NODE_NAME (node) : "<unknown>";
540     }
541   else
542     return "<invalid>";
543 }
544
545
546 /* --- type consistency checks --- */
547 static gboolean
548 check_plugin_U (GTypePlugin *plugin,
549                 gboolean     need_complete_type_info,
550                 gboolean     need_complete_interface_info,
551                 const gchar *type_name)
552 {
553   /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U 
554    */
555   if (!plugin)
556     {
557       g_warning ("plugin handle for type `%s' is NULL",
558                  type_name);
559       return FALSE;
560     }
561   if (!G_IS_TYPE_PLUGIN (plugin))
562     {
563       g_warning ("plugin pointer (%p) for type `%s' is invalid",
564                  plugin, type_name);
565       return FALSE;
566     }
567   if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
568     {
569       g_warning ("plugin for type `%s' has no complete_type_info() implementation",
570                  type_name);
571       return FALSE;
572     }
573   if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
574     {
575       g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
576                  type_name);
577       return FALSE;
578     }
579   return TRUE;
580 }
581
582 static gboolean
583 check_type_name_I (const gchar *type_name)
584 {
585   static const gchar extra_chars[] = "-_+";
586   const gchar *p = type_name;
587   gboolean name_valid;
588   
589   if (!type_name[0] || !type_name[1] || !type_name[2])
590     {
591       g_warning ("type name `%s' is too short", type_name);
592       return FALSE;
593     }
594   /* check the first letter */
595   name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
596   for (p = type_name + 1; *p; p++)
597     name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
598                    (p[0] >= 'a' && p[0] <= 'z') ||
599                    (p[0] >= '0' && p[0] <= '9') ||
600                    strchr (extra_chars, p[0]));
601   if (!name_valid)
602     {
603       g_warning ("type name `%s' contains invalid characters", type_name);
604       return FALSE;
605     }
606   if (g_type_from_name (type_name))
607     {
608       g_warning ("cannot register existing type `%s'", type_name);
609       return FALSE;
610     }
611   
612   return TRUE;
613 }
614
615 static gboolean
616 check_derivation_I (GType        parent_type,
617                     const gchar *type_name)
618 {
619   TypeNode *pnode;
620   GTypeFundamentalInfo* finfo;
621   
622   pnode = lookup_type_node_I (parent_type);
623   if (!pnode)
624     {
625       g_warning ("cannot derive type `%s' from invalid parent type `%s'",
626                  type_name,
627                  type_descriptive_name_I (parent_type));
628       return FALSE;
629     }
630   finfo = type_node_fundamental_info_I (pnode);
631   /* ensure flat derivability */
632   if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
633     {
634       g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
635                  type_name,
636                  NODE_NAME (pnode));
637       return FALSE;
638     }
639   /* ensure deep derivability */
640   if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
641       !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
642     {
643       g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
644                  type_name,
645                  NODE_NAME (pnode));
646       return FALSE;
647     }
648   
649   return TRUE;
650 }
651
652 static gboolean
653 check_collect_format_I (const gchar *collect_format)
654 {
655   const gchar *p = collect_format;
656   gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
657                            G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
658                            G_VALUE_COLLECT_POINTER, 0 };
659   
660   while (*p)
661     if (!strchr (valid_format, *p++))
662       return FALSE;
663   return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
664 }
665
666 static gboolean
667 check_value_table_I (const gchar           *type_name,
668                      const GTypeValueTable *value_table)
669 {
670   if (!value_table)
671     return FALSE;
672   else if (value_table->value_init == NULL)
673     {
674       if (value_table->value_free || value_table->value_copy ||
675           value_table->value_peek_pointer ||
676           value_table->collect_format || value_table->collect_value ||
677           value_table->lcopy_format || value_table->lcopy_value)
678         g_warning ("cannot handle uninitializable values of type `%s'",
679                    type_name);
680       return FALSE;
681     }
682   else /* value_table->value_init != NULL */
683     {
684       if (!value_table->value_free)
685         {
686           /* +++ optional +++
687            * g_warning ("missing `value_free()' for type `%s'", type_name);
688            * return FALSE;
689            */
690         }
691       if (!value_table->value_copy)
692         {
693           g_warning ("missing `value_copy()' for type `%s'", type_name);
694           return FALSE;
695         }
696       if ((value_table->collect_format || value_table->collect_value) &&
697           (!value_table->collect_format || !value_table->collect_value))
698         {
699           g_warning ("one of `collect_format' and `collect_value()' is unspecified for type `%s'",
700                      type_name);
701           return FALSE;
702         }
703       if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
704         {
705           g_warning ("the `%s' specification for type `%s' is too long or invalid",
706                      "collect_format",
707                      type_name);
708           return FALSE;
709         }
710       if ((value_table->lcopy_format || value_table->lcopy_value) &&
711           (!value_table->lcopy_format || !value_table->lcopy_value))
712         {
713           g_warning ("one of `lcopy_format' and `lcopy_value()' is unspecified for type `%s'",
714                      type_name);
715           return FALSE;
716         }
717       if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
718         {
719           g_warning ("the `%s' specification for type `%s' is too long or invalid",
720                      "lcopy_format",
721                      type_name);
722           return FALSE;
723         }
724     }
725   return TRUE;
726 }
727
728 static gboolean
729 check_type_info_I (TypeNode        *pnode,
730                    GType            ftype,
731                    const gchar     *type_name,
732                    const GTypeInfo *info)
733 {
734   GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
735   gboolean is_interface = ftype == G_TYPE_INTERFACE;
736   
737   g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
738   
739   /* check instance members */
740   if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
741       (info->instance_size || info->n_preallocs || info->instance_init))
742     {
743       if (pnode)
744         g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
745                    type_name,
746                    NODE_NAME (pnode));
747       else
748         g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
749                    type_name);
750       return FALSE;
751     }
752   /* check class & interface members */
753   if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
754       (info->class_init || info->class_finalize || info->class_data ||
755        info->class_size || info->base_init || info->base_finalize))
756     {
757       if (pnode)
758         g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
759                    type_name,
760                    NODE_NAME (pnode));
761       else
762         g_warning ("cannot create class for `%s' as non-classed fundamental",
763                    type_name);
764       return FALSE;
765     }
766   /* check interface size */
767   if (is_interface && info->class_size < sizeof (GTypeInterface))
768     {
769       g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
770                  type_name);
771       return FALSE;
772     }
773   /* check class size */
774   if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
775     {
776       if (info->class_size < sizeof (GTypeClass))
777         {
778           g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
779                      type_name);
780           return FALSE;
781         }
782       if (pnode && info->class_size < pnode->data->class.class_size)
783         {
784           g_warning ("specified class size for type `%s' is smaller "
785                      "than the parent type's `%s' class size",
786                      type_name,
787                      NODE_NAME (pnode));
788           return FALSE;
789         }
790     }
791   /* check instance size */
792   if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
793     {
794       if (info->instance_size < sizeof (GTypeInstance))
795         {
796           g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
797                      type_name);
798           return FALSE;
799         }
800       if (pnode && info->instance_size < pnode->data->instance.instance_size)
801         {
802           g_warning ("specified instance size for type `%s' is smaller "
803                      "than the parent type's `%s' instance size",
804                      type_name,
805                      NODE_NAME (pnode));
806           return FALSE;
807         }
808     }
809   
810   return TRUE;
811 }
812
813 static TypeNode*
814 find_conforming_child_type_L (TypeNode *pnode,
815                               TypeNode *iface)
816 {
817   TypeNode *node = NULL;
818   guint i;
819   
820   if (type_lookup_iface_entry_L (pnode, iface))
821     return pnode;
822   
823   for (i = 0; i < pnode->n_children && !node; i++)
824     node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
825   
826   return node;
827 }
828
829 static gboolean
830 check_add_interface_L (GType instance_type,
831                        GType iface_type)
832 {
833   TypeNode *node = lookup_type_node_I (instance_type);
834   TypeNode *iface = lookup_type_node_I (iface_type);
835   IFaceEntry *entry;
836   TypeNode *tnode;
837   GType *prerequisites;
838   guint i;
839
840   
841   if (!node || !node->is_instantiatable)
842     {
843       g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
844                  type_descriptive_name_I (instance_type));
845       return FALSE;
846     }
847   if (!iface || !NODE_IS_IFACE (iface))
848     {
849       g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
850                  type_descriptive_name_I (iface_type),
851                  NODE_NAME (node));
852       return FALSE;
853     }
854   tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
855   if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
856     {
857       /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
858       g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
859                  NODE_NAME (iface),
860                  NODE_NAME (node),
861                  NODE_NAME (tnode));
862       return FALSE;
863     }
864   /* allow overriding of interface type introduced for parent type */
865   entry = type_lookup_iface_entry_L (node, iface);
866   if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
867     {
868       /* ok, we do conform to this interface already, but the interface vtable was not
869        * yet intialized, and we just conform to the interface because it got added to
870        * one of our parents. so we allow overriding of holder info here.
871        */
872       return TRUE;
873     }
874   /* check whether one of our children already conforms (or whether the interface
875    * got added to this node already)
876    */
877   tnode = find_conforming_child_type_L (node, iface);  /* tnode is_a node */
878   if (tnode)
879     {
880       g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
881                  NODE_NAME (iface),
882                  NODE_NAME (node),
883                  NODE_NAME (tnode));
884       return FALSE;
885     }
886   prerequisites = IFACE_NODE_PREREQUISITES (iface);
887   for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
888     {
889       tnode = lookup_type_node_I (prerequisites[i]);
890       if (!type_node_is_a_L (node, tnode))
891         {
892           g_warning ("cannot add interface type `%s' to type `%s' which does not conform to prerequisite `%s'",
893                      NODE_NAME (iface),
894                      NODE_NAME (node),
895                      NODE_NAME (tnode));
896           return FALSE;
897         }
898     }
899   return TRUE;
900 }
901
902 static gboolean
903 check_interface_info_I (TypeNode             *iface,
904                         GType                 instance_type,
905                         const GInterfaceInfo *info)
906 {
907   if ((info->interface_finalize || info->interface_data) && !info->interface_init)
908     {
909       g_warning ("interface type `%s' for type `%s' comes without initializer",
910                  NODE_NAME (iface),
911                  type_descriptive_name_I (instance_type));
912       return FALSE;
913     }
914   
915   return TRUE;
916 }
917
918 /* --- type info (type node data) --- */
919 static void
920 type_data_make_W (TypeNode              *node,
921                   const GTypeInfo       *info,
922                   const GTypeValueTable *value_table)
923 {
924   TypeData *data;
925   GTypeValueTable *vtable = NULL;
926   guint vtable_size = 0;
927   
928   g_assert (node->data == NULL && info != NULL);
929   
930   if (!value_table)
931     {
932       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
933       
934       if (pnode)
935         vtable = pnode->data->common.value_table;
936       else
937         {
938           static const GTypeValueTable zero_vtable = { NULL, };
939           
940           value_table = &zero_vtable;
941         }
942     }
943   if (value_table)
944     {
945       /* need to setup vtable_size since we have to allocate it with data in one chunk */
946       vtable_size = sizeof (GTypeValueTable);
947       if (value_table->collect_format)
948         vtable_size += strlen (value_table->collect_format);
949       if (value_table->lcopy_format)
950         vtable_size += strlen (value_table->lcopy_format);
951       vtable_size += 2;
952     }
953    
954   if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
955     {
956       data = g_malloc0 (sizeof (InstanceData) + vtable_size);
957       if (vtable_size)
958         vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
959       data->instance.class_size = info->class_size;
960       data->instance.class_init_base = info->base_init;
961       data->instance.class_finalize_base = info->base_finalize;
962       data->instance.class_init = info->class_init;
963       data->instance.class_finalize = info->class_finalize;
964       data->instance.class_data = info->class_data;
965       data->instance.class = NULL;
966       data->instance.init_state = UNINITIALIZED;
967       data->instance.instance_size = info->instance_size;
968       /* We'll set the final value for data->instance.private size
969        * after the parent class has been initialized
970        */
971       data->instance.private_size = 0;
972 #ifdef  DISABLE_MEM_POOLS
973       data->instance.n_preallocs = 0;
974 #else   /* !DISABLE_MEM_POOLS */
975       data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
976 #endif  /* !DISABLE_MEM_POOLS */
977       data->instance.instance_init = info->instance_init;
978     }
979   else if (node->is_classed) /* only classed */
980     {
981       data = g_malloc0 (sizeof (ClassData) + vtable_size);
982       if (vtable_size)
983         vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
984       data->class.class_size = info->class_size;
985       data->class.class_init_base = info->base_init;
986       data->class.class_finalize_base = info->base_finalize;
987       data->class.class_init = info->class_init;
988       data->class.class_finalize = info->class_finalize;
989       data->class.class_data = info->class_data;
990       data->class.class = NULL;
991       data->class.init_state = UNINITIALIZED;
992     }
993   else if (NODE_IS_IFACE (node))
994     {
995       data = g_malloc0 (sizeof (IFaceData) + vtable_size);
996       if (vtable_size)
997         vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
998       data->iface.vtable_size = info->class_size;
999       data->iface.vtable_init_base = info->base_init;
1000       data->iface.vtable_finalize_base = info->base_finalize;
1001       data->iface.dflt_init = info->class_init;
1002       data->iface.dflt_finalize = info->class_finalize;
1003       data->iface.dflt_data = info->class_data;
1004       data->iface.dflt_vtable = NULL;
1005     }
1006   else
1007     {
1008       data = g_malloc0 (sizeof (CommonData) + vtable_size);
1009       if (vtable_size)
1010         vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1011     }
1012   
1013   node->data = data;
1014   node->data->common.ref_count = 1;
1015   
1016   if (vtable_size)
1017     {
1018       gchar *p;
1019       
1020       /* we allocate the vtable and its strings together with the type data, so
1021        * children can take over their parent's vtable pointer, and we don't
1022        * need to worry freeing it or not when the child data is destroyed
1023        */
1024       *vtable = *value_table;
1025       p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1026       p[0] = 0;
1027       vtable->collect_format = p;
1028       if (value_table->collect_format)
1029         {
1030           strcat (p, value_table->collect_format);
1031           p += strlen (value_table->collect_format);
1032         }
1033       p++;
1034       p[0] = 0;
1035       vtable->lcopy_format = p;
1036       if (value_table->lcopy_format)
1037         strcat  (p, value_table->lcopy_format);
1038     }
1039   node->data->common.value_table = vtable;
1040   node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1041                                  !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1042                                    GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1043   
1044   g_assert (node->data->common.value_table != NULL); /* paranoid */
1045 }
1046
1047 static inline void
1048 type_data_ref_Wm (TypeNode *node)
1049 {
1050   if (!node->data)
1051     {
1052       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1053       GTypeInfo tmp_info;
1054       GTypeValueTable tmp_value_table;
1055       
1056       g_assert (node->plugin != NULL);
1057       
1058       if (pnode)
1059         {
1060           type_data_ref_Wm (pnode);
1061           if (node->data)
1062             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1063         }
1064       
1065       memset (&tmp_info, 0, sizeof (tmp_info));
1066       memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1067       
1068       G_WRITE_UNLOCK (&type_rw_lock);
1069       g_type_plugin_use (node->plugin);
1070       g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1071       G_WRITE_LOCK (&type_rw_lock);
1072       if (node->data)
1073         INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1074       
1075       check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1076       type_data_make_W (node, &tmp_info,
1077                         check_value_table_I (NODE_NAME (node),
1078                                              &tmp_value_table) ? &tmp_value_table : NULL);
1079     }
1080   else
1081     {
1082       g_assert (node->data->common.ref_count > 0);
1083       
1084       node->data->common.ref_count += 1;
1085     }
1086 }
1087
1088 static inline void
1089 type_data_unref_Wm (TypeNode *node,
1090                     gboolean  uncached)
1091 {
1092   g_assert (node->data && node->data->common.ref_count);
1093   
1094   if (node->data->common.ref_count > 1)
1095     node->data->common.ref_count -= 1;
1096   else
1097     {
1098       if (!node->plugin)
1099         {
1100           g_warning ("static type `%s' unreferenced too often",
1101                      NODE_NAME (node));
1102           return;
1103         }
1104       
1105       type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1106     }
1107 }
1108
1109 static void
1110 type_node_add_iface_entry_W (TypeNode   *node,
1111                              GType       iface_type,
1112                              IFaceEntry *parent_entry)
1113 {
1114   IFaceEntry *entries;
1115   guint i;
1116   
1117   g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1118   
1119   entries = CLASSED_NODE_IFACES_ENTRIES (node);
1120   for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1121     if (entries[i].iface_type == iface_type)
1122       {
1123         /* this can happen in two cases:
1124          * - our parent type already conformed to iface_type and node
1125          *   got it's own holder info. here, our children already have
1126          *   entries and NULL vtables, since this will only work for
1127          *   uninitialized classes.
1128          * - an interface type is added to an ancestor after it was
1129          *   added to a child type.
1130          */
1131         if (!parent_entry)
1132           g_assert (entries[i].vtable == NULL && entries[i].init_state == UNINITIALIZED);
1133         else
1134           {
1135             /* sick, interface is added to ancestor *after* child type;
1136              * nothing todo, the entry and our children were already setup correctly
1137              */
1138           }
1139         return;
1140       }
1141     else if (entries[i].iface_type > iface_type)
1142       break;
1143   CLASSED_NODE_N_IFACES (node) += 1;
1144   CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1145                                                 CLASSED_NODE_IFACES_ENTRIES (node),
1146                                                 CLASSED_NODE_N_IFACES (node));
1147   entries = CLASSED_NODE_IFACES_ENTRIES (node);
1148   g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1149   entries[i].iface_type = iface_type;
1150   entries[i].vtable = NULL;
1151   entries[i].init_state = UNINITIALIZED;
1152
1153   if (parent_entry)
1154     {
1155       if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1156         {
1157           entries[i].init_state = INITIALIZED;
1158           entries[i].vtable = parent_entry->vtable;
1159         }
1160       for (i = 0; i < node->n_children; i++)
1161         type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries[i]);
1162     }
1163 }
1164
1165 static void
1166 type_add_interface_Wm (TypeNode             *node,
1167                        TypeNode             *iface,
1168                        const GInterfaceInfo *info,
1169                        GTypePlugin          *plugin)
1170 {
1171   IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1172   IFaceEntry *entry;
1173   guint i;
1174   
1175   g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1176   
1177   iholder->next = iface_node_get_holders_L (iface);
1178   iface_node_set_holders_W (iface, iholder);
1179   iholder->instance_type = NODE_TYPE (node);
1180   iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1181   iholder->plugin = plugin;
1182
1183   /* create an iface entry for this type */
1184   type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1185   
1186   /* if the class is already (partly) initialized, we may need to base
1187    * initalize and/or initialize the new interface.
1188    */
1189   if (node->data)
1190     {
1191       InitState class_state = node->data->class.init_state;
1192       
1193       if (class_state >= BASE_IFACE_INIT)
1194         type_iface_vtable_base_init_Wm (iface, node);
1195       
1196       if (class_state >= IFACE_INIT)
1197         type_iface_vtable_iface_init_Wm (iface, node);
1198     }
1199   
1200   /* create iface entries for children of this type */
1201   entry = type_lookup_iface_entry_L (node, iface);
1202   for (i = 0; i < node->n_children; i++)
1203     type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1204 }
1205
1206 static void
1207 type_iface_add_prerequisite_W (TypeNode *iface,
1208                                TypeNode *prerequisite_node)
1209 {
1210   GType prerequisite_type = NODE_TYPE (prerequisite_node);
1211   GType *prerequisites, *dependants;
1212   guint n_dependants, i;
1213   
1214   g_assert (NODE_IS_IFACE (iface) &&
1215             IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1216             (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1217   
1218   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1219   for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1220     if (prerequisites[i] == prerequisite_type)
1221       return;                   /* we already have that prerequisiste */
1222     else if (prerequisites[i] > prerequisite_type)
1223       break;
1224   IFACE_NODE_N_PREREQUISITES (iface) += 1;
1225   IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1226                                               IFACE_NODE_PREREQUISITES (iface),
1227                                               IFACE_NODE_N_PREREQUISITES (iface));
1228   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1229   g_memmove (prerequisites + i + 1, prerequisites + i,
1230              sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1231   prerequisites[i] = prerequisite_type;
1232   
1233   /* we want to get notified when prerequisites get added to prerequisite_node */
1234   if (NODE_IS_IFACE (prerequisite_node))
1235     {
1236       dependants = iface_node_get_dependants_array_L (prerequisite_node);
1237       n_dependants = dependants ? dependants[0] : 0;
1238       n_dependants += 1;
1239       dependants = g_renew (GType, dependants, n_dependants + 1);
1240       dependants[n_dependants] = NODE_TYPE (iface);
1241       dependants[0] = n_dependants;
1242       iface_node_set_dependants_array_W (prerequisite_node, dependants);
1243     }
1244   
1245   /* we need to notify all dependants */
1246   dependants = iface_node_get_dependants_array_L (iface);
1247   n_dependants = dependants ? dependants[0] : 0;
1248   for (i = 1; i <= n_dependants; i++)
1249     type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1250 }
1251
1252 void
1253 g_type_interface_add_prerequisite (GType interface_type,
1254                                    GType prerequisite_type)
1255 {
1256   TypeNode *iface, *prerequisite_node;
1257   IFaceHolder *holders;
1258   
1259   g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));      /* G_TYPE_IS_INTERFACE() is an external call: _U */
1260   g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1261   g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1262   
1263   iface = lookup_type_node_I (interface_type);
1264   prerequisite_node = lookup_type_node_I (prerequisite_type);
1265   if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1266     {
1267       g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1268                  type_descriptive_name_I (interface_type),
1269                  type_descriptive_name_I (prerequisite_type));
1270       return;
1271     }
1272   G_WRITE_LOCK (&type_rw_lock);
1273   holders = iface_node_get_holders_L (iface);
1274   if (holders)
1275     {
1276       G_WRITE_UNLOCK (&type_rw_lock);
1277       g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1278                  type_descriptive_name_I (prerequisite_type),
1279                  type_descriptive_name_I (interface_type),
1280                  type_descriptive_name_I (holders->instance_type));
1281       return;
1282     }
1283   if (prerequisite_node->is_instantiatable)
1284     {
1285       guint i;
1286       
1287       /* can have at most one publically installable instantiatable prerequisite */
1288       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1289         {
1290           TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1291           
1292           if (prnode->is_instantiatable)
1293             {
1294               G_WRITE_UNLOCK (&type_rw_lock);
1295               g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1296                          type_descriptive_name_I (prerequisite_type),
1297                          type_descriptive_name_I (interface_type),
1298                          type_descriptive_name_I (NODE_TYPE (prnode)));
1299               return;
1300             }
1301         }
1302       
1303       for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1304         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1305       G_WRITE_UNLOCK (&type_rw_lock);
1306     }
1307   else if (NODE_IS_IFACE (prerequisite_node))
1308     {
1309       GType *prerequisites;
1310       guint i;
1311       
1312       prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1313       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1314         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1315       type_iface_add_prerequisite_W (iface, prerequisite_node);
1316       G_WRITE_UNLOCK (&type_rw_lock);
1317     }
1318   else
1319     {
1320       G_WRITE_UNLOCK (&type_rw_lock);
1321       g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1322                  type_descriptive_name_I (prerequisite_type),
1323                  type_descriptive_name_I (interface_type));
1324     }
1325 }
1326
1327 GType* /* free result */
1328 g_type_interface_prerequisites (GType  interface_type,
1329                                 guint *n_prerequisites)
1330 {
1331   TypeNode *iface;
1332   
1333   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1334
1335   iface = lookup_type_node_I (interface_type);
1336   if (iface)
1337     {
1338       GType *types;
1339       TypeNode *inode = NULL;
1340       guint i, n = 0;
1341       
1342       G_READ_LOCK (&type_rw_lock);
1343       types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1344       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1345         {
1346           GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1347           TypeNode *node = lookup_type_node_I (prerequisite);
1348           if (node->is_instantiatable &&
1349               (!inode || type_node_is_a_L (node, inode)))
1350             inode = node;
1351           else
1352             types[n++] = NODE_TYPE (node);
1353         }
1354       if (inode)
1355         types[n++] = NODE_TYPE (inode);
1356       
1357       if (n_prerequisites)
1358         *n_prerequisites = n;
1359       G_READ_UNLOCK (&type_rw_lock);
1360       
1361       return types;
1362     }
1363   else
1364     {
1365       if (n_prerequisites)
1366         *n_prerequisites = 0;
1367       
1368       return NULL;
1369     }
1370 }
1371
1372
1373 static IFaceHolder*
1374 type_iface_peek_holder_L (TypeNode *iface,
1375                           GType     instance_type)
1376 {
1377   IFaceHolder *iholder;
1378   
1379   g_assert (NODE_IS_IFACE (iface));
1380   
1381   iholder = iface_node_get_holders_L (iface);
1382   while (iholder && iholder->instance_type != instance_type)
1383     iholder = iholder->next;
1384   return iholder;
1385 }
1386
1387 static IFaceHolder*
1388 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1389                                     GType     instance_type,
1390                                     gboolean  need_info)
1391 {
1392   IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1393   
1394   if (iholder && !iholder->info && need_info)
1395     {
1396       GInterfaceInfo tmp_info;
1397       
1398       g_assert (iholder->plugin != NULL);
1399       
1400       type_data_ref_Wm (iface);
1401       if (iholder->info)
1402         INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1403       
1404       memset (&tmp_info, 0, sizeof (tmp_info));
1405       
1406       G_WRITE_UNLOCK (&type_rw_lock);
1407       g_type_plugin_use (iholder->plugin);
1408       g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1409       G_WRITE_LOCK (&type_rw_lock);
1410       if (iholder->info)
1411         INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1412       
1413       check_interface_info_I (iface, instance_type, &tmp_info);
1414       iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1415     }
1416   
1417   return iholder;       /* we don't modify write lock upon returning NULL */
1418 }
1419
1420 static void
1421 type_iface_blow_holder_info_Wm (TypeNode *iface,
1422                                 GType     instance_type)
1423 {
1424   IFaceHolder *iholder = iface_node_get_holders_L (iface);
1425   
1426   g_assert (NODE_IS_IFACE (iface));
1427   
1428   while (iholder->instance_type != instance_type)
1429     iholder = iholder->next;
1430   
1431   if (iholder->info && iholder->plugin)
1432     {
1433       g_free (iholder->info);
1434       iholder->info = NULL;
1435       
1436       G_WRITE_UNLOCK (&type_rw_lock);
1437       g_type_plugin_unuse (iholder->plugin);
1438       G_WRITE_LOCK (&type_rw_lock);
1439       
1440       type_data_unref_Wm (iface, FALSE);
1441     }
1442 }
1443
1444 /* Assumes type's class already exists
1445  */
1446 static inline size_t
1447 type_total_instance_size_I (TypeNode *node)
1448 {
1449   gsize total_instance_size;
1450
1451   total_instance_size = node->data->instance.instance_size;
1452   if (node->data->instance.private_size != 0)
1453     total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1454
1455   return total_instance_size;
1456 }
1457
1458 /* --- type structure creation/destruction --- */
1459 typedef struct {
1460   gpointer instance;
1461   gpointer class;
1462 } InstanceRealClass;
1463 static gint
1464 instance_real_class_cmp (gconstpointer p1,
1465                          gconstpointer p2)
1466 {
1467   const InstanceRealClass *irc1 = p1;
1468   const InstanceRealClass *irc2 = p2;
1469   guint8 *i1 = irc1->instance;
1470   guint8 *i2 = irc2->instance;
1471   return G_BSEARCH_ARRAY_CMP (i1, i2);
1472 }
1473 G_LOCK_DEFINE_STATIC (instance_real_class);
1474 static GBSearchArray *instance_real_class_bsa = NULL;
1475 static GBSearchConfig instance_real_class_bconfig = {
1476   sizeof (InstanceRealClass),
1477   instance_real_class_cmp,
1478   0,
1479 };
1480 static inline void
1481 instance_real_class_set (gpointer    instance,
1482                          GTypeClass *class)
1483 {
1484   InstanceRealClass key;
1485   key.instance = instance;
1486   key.class = class;
1487   G_LOCK (instance_real_class);
1488   if (!instance_real_class_bsa)
1489     instance_real_class_bsa = g_bsearch_array_create (&instance_real_class_bconfig);
1490   instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1491   G_UNLOCK (instance_real_class);
1492 }
1493 static inline void
1494 instance_real_class_remove (gpointer instance)
1495 {
1496   InstanceRealClass key, *node;
1497   guint index;
1498   key.instance = instance;
1499   G_LOCK (instance_real_class);
1500   node = g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key);
1501   index = g_bsearch_array_get_index (instance_real_class_bsa, &instance_real_class_bconfig, node);
1502   instance_real_class_bsa = g_bsearch_array_remove (instance_real_class_bsa, &instance_real_class_bconfig, index);
1503   if (!g_bsearch_array_get_n_nodes (instance_real_class_bsa))
1504     {
1505       g_bsearch_array_free (instance_real_class_bsa, &instance_real_class_bconfig);
1506       instance_real_class_bsa = NULL;
1507     }
1508   G_UNLOCK (instance_real_class);
1509 }
1510 static inline GTypeClass*
1511 instance_real_class_get (gpointer instance)
1512 {
1513   InstanceRealClass key, *node;
1514   GTypeClass *class;
1515   key.instance = instance;
1516   G_LOCK (instance_real_class);
1517   node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
1518   class = node ? node->class : NULL;
1519   G_UNLOCK (instance_real_class);
1520   return class;
1521 }
1522
1523 GTypeInstance*
1524 g_type_create_instance (GType type)
1525 {
1526   TypeNode *node;
1527   GTypeInstance *instance;
1528   GTypeClass *class;
1529   guint i, total_size;
1530   
1531   node = lookup_type_node_I (type);
1532   if (!node || !node->is_instantiatable)
1533     {
1534       g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1535                  type_descriptive_name_I (type));
1536       return NULL;
1537     }
1538   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1539   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1540     {
1541       g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1542                  type_descriptive_name_I (type));
1543       return NULL;
1544     }
1545   
1546   class = g_type_class_ref (type);
1547   total_size = type_total_instance_size_I (node);
1548
1549   instance = g_slice_alloc0 (total_size);
1550
1551   if (node->data->instance.private_size)
1552     instance_real_class_set (instance, class);
1553   for (i = node->n_supers; i > 0; i--)
1554     {
1555       TypeNode *pnode;
1556       
1557       pnode = lookup_type_node_I (node->supers[i]);
1558       if (pnode->data->instance.instance_init)
1559         {
1560           instance->g_class = pnode->data->instance.class;
1561           pnode->data->instance.instance_init (instance, class);
1562         }
1563     }
1564   if (node->data->instance.private_size)
1565     instance_real_class_remove (instance);
1566
1567   instance->g_class = class;
1568   if (node->data->instance.instance_init)
1569     node->data->instance.instance_init (instance, class);
1570   
1571   return instance;
1572 }
1573
1574 void
1575 g_type_free_instance (GTypeInstance *instance)
1576 {
1577   TypeNode *node;
1578   GTypeClass *class;
1579   
1580   g_return_if_fail (instance != NULL && instance->g_class != NULL);
1581   
1582   class = instance->g_class;
1583   node = lookup_type_node_I (class->g_type);
1584   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1585     {
1586       g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1587                  type_descriptive_name_I (class->g_type));
1588       return;
1589     }
1590   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1591   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1592     {
1593       g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1594                  NODE_NAME (node));
1595       return;
1596     }
1597   
1598   instance->g_class = NULL;
1599 #ifdef G_ENABLE_DEBUG  
1600   memset (instance, 0xaa, type_total_instance_size_I (node));
1601 #endif
1602   g_slice_free1 (type_total_instance_size_I (node), instance);
1603
1604   g_type_class_unref (class);
1605 }
1606
1607 static void
1608 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1609 {
1610   g_assert (iface->data);
1611
1612   if (!iface->data->iface.dflt_vtable)
1613     {
1614       GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1615       iface->data->iface.dflt_vtable = vtable;
1616       vtable->g_type = NODE_TYPE (iface);
1617       vtable->g_instance_type = 0;
1618       if (iface->data->iface.vtable_init_base ||
1619           iface->data->iface.dflt_init)
1620         {
1621           G_WRITE_UNLOCK (&type_rw_lock);
1622           if (iface->data->iface.vtable_init_base)
1623             iface->data->iface.vtable_init_base (vtable);
1624           if (iface->data->iface.dflt_init)
1625             iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1626           G_WRITE_LOCK (&type_rw_lock);
1627         }
1628     }
1629 }
1630
1631
1632 /* This is called to allocate and do the first part of initializing
1633  * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1634  *
1635  * A FALSE return indicates that we didn't find an init function for
1636  * this type/iface pair, so the vtable from the parent type should
1637  * be used. Note that the write lock is not modified upon a FALSE
1638  * return.
1639  */
1640 static gboolean
1641 type_iface_vtable_base_init_Wm (TypeNode *iface,
1642                                 TypeNode *node)
1643 {
1644   IFaceEntry *entry;
1645   IFaceHolder *iholder;
1646   GTypeInterface *vtable = NULL;
1647   TypeNode *pnode;
1648   
1649   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1650   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1651   if (!iholder)
1652     return FALSE;       /* we don't modify write lock upon FALSE */
1653
1654   type_iface_ensure_dflt_vtable_Wm (iface);
1655
1656   entry = type_lookup_iface_entry_L (node, iface);
1657
1658   g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1659   
1660   entry->init_state = IFACE_INIT;
1661
1662   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1663   if (pnode)    /* want to copy over parent iface contents */
1664     {
1665       IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1666       
1667       if (pentry)
1668         vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1669     }
1670   if (!vtable)
1671     vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
1672   entry->vtable = vtable;
1673   vtable->g_type = NODE_TYPE (iface);
1674   vtable->g_instance_type = NODE_TYPE (node);
1675   
1676   if (iface->data->iface.vtable_init_base)
1677     {
1678       G_WRITE_UNLOCK (&type_rw_lock);
1679       iface->data->iface.vtable_init_base (vtable);
1680       G_WRITE_LOCK (&type_rw_lock);
1681     }
1682   return TRUE;  /* initialized the vtable */
1683 }
1684
1685 /* Finishes what type_iface_vtable_base_init_Wm started by
1686  * calling the interface init function.
1687  * this function may only be called for types with their
1688  * own interface holder info, i.e. types for which
1689  * g_type_add_interface*() was called and not children thereof.
1690  */
1691 static void
1692 type_iface_vtable_iface_init_Wm (TypeNode *iface,
1693                                  TypeNode *node)
1694 {
1695   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1696   IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
1697   GTypeInterface *vtable = NULL;
1698   guint i;
1699   
1700   /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
1701   g_assert (iface->data && entry && iholder && iholder->info);
1702   g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
1703   
1704   entry->init_state = INITIALIZED;
1705       
1706   vtable = entry->vtable;
1707
1708   if (iholder->info->interface_init)
1709     {
1710       G_WRITE_UNLOCK (&type_rw_lock);
1711       if (iholder->info->interface_init)
1712         iholder->info->interface_init (vtable, iholder->info->interface_data);
1713       G_WRITE_LOCK (&type_rw_lock);
1714     }
1715   
1716   for (i = 0; i < static_n_iface_check_funcs; i++)
1717     {
1718       GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
1719       gpointer check_data = static_iface_check_funcs[i].check_data;
1720
1721       G_WRITE_UNLOCK (&type_rw_lock);
1722       check_func (check_data, (gpointer)vtable);
1723       G_WRITE_LOCK (&type_rw_lock);      
1724     }
1725 }
1726
1727 static gboolean
1728 type_iface_vtable_finalize_Wm (TypeNode       *iface,
1729                                TypeNode       *node,
1730                                GTypeInterface *vtable)
1731 {
1732   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1733   IFaceHolder *iholder;
1734   
1735   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1736   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1737   if (!iholder)
1738     return FALSE;       /* we don't modify write lock upon FALSE */
1739   
1740   g_assert (entry && entry->vtable == vtable && iholder->info);
1741   
1742   entry->vtable = NULL;
1743   entry->init_state = UNINITIALIZED;
1744   if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1745     {
1746       G_WRITE_UNLOCK (&type_rw_lock);
1747       if (iholder->info->interface_finalize)
1748         iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1749       if (iface->data->iface.vtable_finalize_base)
1750         iface->data->iface.vtable_finalize_base (vtable);
1751       G_WRITE_LOCK (&type_rw_lock);
1752     }
1753   vtable->g_type = 0;
1754   vtable->g_instance_type = 0;
1755   g_free (vtable);
1756   
1757   type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1758   
1759   return TRUE;  /* write lock modified */
1760 }
1761
1762 static void
1763 type_class_init_Wm (TypeNode   *node,
1764                     GTypeClass *pclass)
1765 {
1766   GSList *slist, *init_slist = NULL;
1767   GTypeClass *class;
1768   IFaceEntry *entry;
1769   TypeNode *bnode, *pnode;
1770   guint i;
1771   
1772   g_assert (node->is_classed && node->data &&
1773             node->data->class.class_size &&
1774             !node->data->class.class &&
1775             node->data->class.init_state == UNINITIALIZED);
1776
1777   class = g_malloc0 (node->data->class.class_size);
1778   node->data->class.class = class;
1779   node->data->class.init_state = BASE_CLASS_INIT;
1780   
1781   if (pclass)
1782     {
1783       TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1784       
1785       memcpy (class, pclass, pnode->data->class.class_size);
1786
1787       if (node->is_instantiatable)
1788         {
1789           /* We need to initialize the private_size here rather than in
1790            * type_data_make_W() since the class init for the parent
1791            * class may have changed pnode->data->instance.private_size.
1792            */
1793           node->data->instance.private_size = pnode->data->instance.private_size;
1794         }
1795     }
1796   class->g_type = NODE_TYPE (node);
1797   
1798   G_WRITE_UNLOCK (&type_rw_lock);
1799   
1800   /* stack all base class initialization functions, so we
1801    * call them in ascending order.
1802    */
1803   for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1804     if (bnode->data->class.class_init_base)
1805       init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1806   for (slist = init_slist; slist; slist = slist->next)
1807     {
1808       GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1809       
1810       class_init_base (class);
1811     }
1812   g_slist_free (init_slist);
1813   
1814   G_WRITE_LOCK (&type_rw_lock);
1815
1816   node->data->class.init_state = BASE_IFACE_INIT;
1817   
1818   /* Before we initialize the class, base initialize all interfaces, either
1819    * from parent, or through our holder info
1820    */
1821   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1822
1823   i = 0;
1824   while (i < CLASSED_NODE_N_IFACES (node))
1825     {
1826       entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1827       while (i < CLASSED_NODE_N_IFACES (node) &&
1828              entry->init_state == IFACE_INIT)
1829         {
1830           entry++;
1831           i++;
1832         }
1833
1834       if (i == CLASSED_NODE_N_IFACES (node))
1835         break;
1836
1837       if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
1838         {
1839           guint j;
1840           
1841           /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
1842            * doesn't modify write lock upon FALSE, so entry is still valid; 
1843            */
1844           g_assert (pnode != NULL);
1845           
1846           for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1847             {
1848               IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1849               
1850               if (pentry->iface_type == entry->iface_type)
1851                 {
1852                   entry->vtable = pentry->vtable;
1853                   entry->init_state = INITIALIZED;
1854                   break;
1855                 }
1856             }
1857           g_assert (entry->vtable != NULL);
1858         }
1859
1860       /* If the write lock was released, additional interface entries might
1861        * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
1862        * be base-initialized when inserted, so we don't have to worry that
1863        * we might miss them. Uninitialized entries can only be moved higher
1864        * when new ones are inserted.
1865        */
1866       i++;
1867     }
1868   
1869   node->data->class.init_state = CLASS_INIT;
1870   
1871   G_WRITE_UNLOCK (&type_rw_lock);
1872
1873   if (node->data->class.class_init)
1874     node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1875   
1876   G_WRITE_LOCK (&type_rw_lock);
1877   
1878   node->data->class.init_state = IFACE_INIT;
1879   
1880   /* finish initializing the interfaces through our holder info.
1881    * inherited interfaces are already init_state == INITIALIZED, because
1882    * they either got setup in the above base_init loop, or during
1883    * class_init from within type_add_interface_Wm() for this or
1884    * an anchestor type.
1885    */
1886   i = 0;
1887   while (TRUE)
1888     {
1889       entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
1890       while (i < CLASSED_NODE_N_IFACES (node) &&
1891              entry->init_state == INITIALIZED)
1892         {
1893           entry++;
1894           i++;
1895         }
1896
1897       if (i == CLASSED_NODE_N_IFACES (node))
1898         break;
1899
1900       type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
1901       
1902       /* As in the loop above, additional initialized entries might be inserted
1903        * if the write lock is released, but that's harmless because the entries
1904        * we need to initialize only move higher in the list.
1905        */
1906       i++;
1907     }
1908   
1909   node->data->class.init_state = INITIALIZED;
1910 }
1911
1912 static void
1913 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1914 {
1915   guint i;
1916
1917   g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1918
1919  reiterate:
1920   for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1921     {
1922       IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1923       if (entry->vtable)
1924         {
1925           if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1926             {
1927               /* refetch entries, IFACES_ENTRIES might be modified */
1928               goto reiterate;
1929             }
1930           else
1931             {
1932               /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1933                * iface vtable came from parent
1934                */
1935               entry->vtable = NULL;
1936               entry->init_state = UNINITIALIZED;
1937             }
1938         }
1939     }
1940 }
1941
1942 static void
1943 type_data_finalize_class_U (TypeNode  *node,
1944                             ClassData *cdata)
1945 {
1946   GTypeClass *class = cdata->class;
1947   TypeNode *bnode;
1948   
1949   g_assert (cdata->class && cdata->common.ref_count == 0);
1950   
1951   if (cdata->class_finalize)
1952     cdata->class_finalize (class, (gpointer) cdata->class_data);
1953   
1954   /* call all base class destruction functions in descending order
1955    */
1956   if (cdata->class_finalize_base)
1957     cdata->class_finalize_base (class);
1958   for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1959     if (bnode->data->class.class_finalize_base)
1960       bnode->data->class.class_finalize_base (class);
1961   
1962   g_free (cdata->class);
1963 }
1964
1965 static void
1966 type_data_last_unref_Wm (GType    type,
1967                          gboolean uncached)
1968 {
1969   TypeNode *node = lookup_type_node_I (type);
1970   
1971   g_return_if_fail (node != NULL && node->plugin != NULL);
1972   
1973   if (!node->data || node->data->common.ref_count == 0)
1974     {
1975       g_warning ("cannot drop last reference to unreferenced type `%s'",
1976                  type_descriptive_name_I (type));
1977       return;
1978     }
1979
1980   /* call class cache hooks */
1981   if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
1982     {
1983       guint i;
1984       
1985       G_WRITE_UNLOCK (&type_rw_lock);
1986       G_READ_LOCK (&type_rw_lock);
1987       for (i = 0; i < static_n_class_cache_funcs; i++)
1988         {
1989           GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1990           gpointer cache_data = static_class_cache_funcs[i].cache_data;
1991           gboolean need_break;
1992           
1993           G_READ_UNLOCK (&type_rw_lock);
1994           need_break = cache_func (cache_data, node->data->class.class);
1995           G_READ_LOCK (&type_rw_lock);
1996           if (!node->data || node->data->common.ref_count == 0)
1997             INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1998           if (need_break)
1999             break;
2000         }
2001       G_READ_UNLOCK (&type_rw_lock);
2002       G_WRITE_LOCK (&type_rw_lock);
2003     }
2004   
2005   if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
2006     node->data->common.ref_count -= 1;
2007   else
2008     {
2009       GType ptype = NODE_PARENT_TYPE (node);
2010       TypeData *tdata;
2011       
2012       node->data->common.ref_count = 0;
2013       
2014       if (node->is_instantiatable)
2015         {
2016           /* destroy node->data->instance.mem_chunk */
2017         }
2018       
2019       tdata = node->data;
2020       if (node->is_classed && tdata->class.class)
2021         {
2022           if (CLASSED_NODE_N_IFACES (node))
2023             type_data_finalize_class_ifaces_Wm (node);
2024           node->mutatable_check_cache = FALSE;
2025           node->data = NULL;
2026           G_WRITE_UNLOCK (&type_rw_lock);
2027           type_data_finalize_class_U (node, &tdata->class);
2028           G_WRITE_LOCK (&type_rw_lock);
2029         }
2030       else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2031         {
2032           node->mutatable_check_cache = FALSE;
2033           node->data = NULL;
2034           if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2035             {
2036               G_WRITE_UNLOCK (&type_rw_lock);
2037               if (tdata->iface.dflt_finalize)
2038                 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2039               if (tdata->iface.vtable_finalize_base)
2040                 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2041               G_WRITE_LOCK (&type_rw_lock);
2042             }
2043           g_free (tdata->iface.dflt_vtable);
2044         }
2045       else
2046         {
2047           node->mutatable_check_cache = FALSE;
2048           node->data = NULL;
2049         }
2050
2051       /* freeing tdata->common.value_table and its contents is taken care of
2052        * by allocating it in one chunk with tdata
2053        */
2054       g_free (tdata);
2055       
2056       G_WRITE_UNLOCK (&type_rw_lock);
2057       g_type_plugin_unuse (node->plugin);
2058       G_WRITE_LOCK (&type_rw_lock);
2059       if (ptype)
2060         type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
2061     }
2062 }
2063
2064 void
2065 g_type_add_class_cache_func (gpointer            cache_data,
2066                              GTypeClassCacheFunc cache_func)
2067 {
2068   guint i;
2069   
2070   g_return_if_fail (cache_func != NULL);
2071   
2072   G_WRITE_LOCK (&type_rw_lock);
2073   i = static_n_class_cache_funcs++;
2074   static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2075   static_class_cache_funcs[i].cache_data = cache_data;
2076   static_class_cache_funcs[i].cache_func = cache_func;
2077   G_WRITE_UNLOCK (&type_rw_lock);
2078 }
2079
2080 void
2081 g_type_remove_class_cache_func (gpointer            cache_data,
2082                                 GTypeClassCacheFunc cache_func)
2083 {
2084   gboolean found_it = FALSE;
2085   guint i;
2086   
2087   g_return_if_fail (cache_func != NULL);
2088   
2089   G_WRITE_LOCK (&type_rw_lock);
2090   for (i = 0; i < static_n_class_cache_funcs; i++)
2091     if (static_class_cache_funcs[i].cache_data == cache_data &&
2092         static_class_cache_funcs[i].cache_func == cache_func)
2093       {
2094         static_n_class_cache_funcs--;
2095         g_memmove (static_class_cache_funcs + i,
2096                    static_class_cache_funcs + i + 1,
2097                    sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2098         static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2099         found_it = TRUE;
2100         break;
2101       }
2102   G_WRITE_UNLOCK (&type_rw_lock);
2103   
2104   if (!found_it)
2105     g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2106                cache_func, cache_data);
2107 }
2108
2109
2110 void
2111 g_type_add_interface_check (gpointer                check_data,
2112                             GTypeInterfaceCheckFunc check_func)
2113 {
2114   guint i;
2115   
2116   g_return_if_fail (check_func != NULL);
2117   
2118   G_WRITE_LOCK (&type_rw_lock);
2119   i = static_n_iface_check_funcs++;
2120   static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2121   static_iface_check_funcs[i].check_data = check_data;
2122   static_iface_check_funcs[i].check_func = check_func;
2123   G_WRITE_UNLOCK (&type_rw_lock);
2124 }
2125
2126 void
2127 g_type_remove_interface_check (gpointer                check_data,
2128                                GTypeInterfaceCheckFunc check_func)
2129 {
2130   gboolean found_it = FALSE;
2131   guint i;
2132   
2133   g_return_if_fail (check_func != NULL);
2134   
2135   G_WRITE_LOCK (&type_rw_lock);
2136   for (i = 0; i < static_n_iface_check_funcs; i++)
2137     if (static_iface_check_funcs[i].check_data == check_data &&
2138         static_iface_check_funcs[i].check_func == check_func)
2139       {
2140         static_n_iface_check_funcs--;
2141         g_memmove (static_iface_check_funcs + i,
2142                    static_iface_check_funcs + i + 1,
2143                    sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2144         static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2145         found_it = TRUE;
2146         break;
2147       }
2148   G_WRITE_UNLOCK (&type_rw_lock);
2149   
2150   if (!found_it)
2151     g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2152                check_func, check_data);
2153 }
2154
2155 /* --- type registration --- */
2156 GType
2157 g_type_register_fundamental (GType                       type_id,
2158                              const gchar                *type_name,
2159                              const GTypeInfo            *info,
2160                              const GTypeFundamentalInfo *finfo,
2161                              GTypeFlags                  flags)
2162 {
2163   TypeNode *node;
2164   
2165   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2166   g_return_val_if_fail (type_id > 0, 0);
2167   g_return_val_if_fail (type_name != NULL, 0);
2168   g_return_val_if_fail (info != NULL, 0);
2169   g_return_val_if_fail (finfo != NULL, 0);
2170   
2171   if (!check_type_name_I (type_name))
2172     return 0;
2173   if ((type_id & TYPE_ID_MASK) ||
2174       type_id > G_TYPE_FUNDAMENTAL_MAX)
2175     {
2176       g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
2177                  type_name,
2178                  type_id);
2179       return 0;
2180     }
2181   if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2182       !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2183     {
2184       g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
2185                  type_name);
2186       return 0;
2187     }
2188   if (lookup_type_node_I (type_id))
2189     {
2190       g_warning ("cannot register existing fundamental type `%s' (as `%s')",
2191                  type_descriptive_name_I (type_id),
2192                  type_name);
2193       return 0;
2194     }
2195   
2196   G_WRITE_LOCK (&type_rw_lock);
2197   node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2198   type_add_flags_W (node, flags);
2199   
2200   if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2201     type_data_make_W (node, info,
2202                       check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2203   G_WRITE_UNLOCK (&type_rw_lock);
2204   
2205   return NODE_TYPE (node);
2206 }
2207
2208 GType
2209 g_type_register_static_simple (GType             parent_type,
2210                                const gchar      *type_name,
2211                                guint             class_size,
2212                                GClassInitFunc    class_init,
2213                                guint             instance_size,
2214                                GInstanceInitFunc instance_init,
2215                                GTypeFlags        flags)
2216 {
2217   GTypeInfo info;
2218
2219   info.class_size = class_size;
2220   info.base_init = NULL;
2221   info.base_finalize = NULL;
2222   info.class_init = class_init;
2223   info.class_finalize = NULL;
2224   info.class_data = NULL;
2225   info.instance_size = instance_size;
2226   info.n_preallocs = 0;
2227   info.instance_init = instance_init;
2228   info.value_table = NULL;
2229
2230   return g_type_register_static (parent_type, type_name, &info, flags);
2231 }
2232
2233 GType
2234 g_type_register_static (GType            parent_type,
2235                         const gchar     *type_name,
2236                         const GTypeInfo *info,
2237                         GTypeFlags       flags)
2238 {
2239   TypeNode *pnode, *node;
2240   GType type = 0;
2241   
2242   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2243   g_return_val_if_fail (parent_type > 0, 0);
2244   g_return_val_if_fail (type_name != NULL, 0);
2245   g_return_val_if_fail (info != NULL, 0);
2246   
2247   if (!check_type_name_I (type_name) ||
2248       !check_derivation_I (parent_type, type_name))
2249     return 0;
2250   if (info->class_finalize)
2251     {
2252       g_warning ("class finalizer specified for static type `%s'",
2253                  type_name);
2254       return 0;
2255     }
2256   
2257   pnode = lookup_type_node_I (parent_type);
2258   G_WRITE_LOCK (&type_rw_lock);
2259   type_data_ref_Wm (pnode);
2260   if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2261     {
2262       node = type_node_new_W (pnode, type_name, NULL);
2263       type_add_flags_W (node, flags);
2264       type = NODE_TYPE (node);
2265       type_data_make_W (node, info,
2266                         check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2267     }
2268   G_WRITE_UNLOCK (&type_rw_lock);
2269   
2270   return type;
2271 }
2272
2273 GType
2274 g_type_register_dynamic (GType        parent_type,
2275                          const gchar *type_name,
2276                          GTypePlugin *plugin,
2277                          GTypeFlags   flags)
2278 {
2279   TypeNode *pnode, *node;
2280   GType type;
2281   
2282   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
2283   g_return_val_if_fail (parent_type > 0, 0);
2284   g_return_val_if_fail (type_name != NULL, 0);
2285   g_return_val_if_fail (plugin != NULL, 0);
2286   
2287   if (!check_type_name_I (type_name) ||
2288       !check_derivation_I (parent_type, type_name) ||
2289       !check_plugin_U (plugin, TRUE, FALSE, type_name))
2290     return 0;
2291   
2292   G_WRITE_LOCK (&type_rw_lock);
2293   pnode = lookup_type_node_I (parent_type);
2294   node = type_node_new_W (pnode, type_name, plugin);
2295   type_add_flags_W (node, flags);
2296   type = NODE_TYPE (node);
2297   G_WRITE_UNLOCK (&type_rw_lock);
2298   
2299   return type;
2300 }
2301
2302 void
2303 g_type_add_interface_static (GType                 instance_type,
2304                              GType                 interface_type,
2305                              const GInterfaceInfo *info)
2306 {
2307   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2308   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2309   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2310   
2311   G_WRITE_LOCK (&type_rw_lock);
2312   if (check_add_interface_L (instance_type, interface_type))
2313     {
2314       TypeNode *node = lookup_type_node_I (instance_type);
2315       TypeNode *iface = lookup_type_node_I (interface_type);
2316       
2317       if (check_interface_info_I (iface, NODE_TYPE (node), info))
2318         type_add_interface_Wm (node, iface, info, NULL);
2319     }
2320   G_WRITE_UNLOCK (&type_rw_lock);
2321 }
2322
2323 void
2324 g_type_add_interface_dynamic (GType        instance_type,
2325                               GType        interface_type,
2326                               GTypePlugin *plugin)
2327 {
2328   TypeNode *node;
2329   
2330   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2331   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2332   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2333   
2334   node = lookup_type_node_I (instance_type);
2335   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2336     return;
2337   
2338   G_WRITE_LOCK (&type_rw_lock);
2339   if (check_add_interface_L (instance_type, interface_type))
2340     {
2341       TypeNode *iface = lookup_type_node_I (interface_type);
2342       
2343       type_add_interface_Wm (node, iface, NULL, plugin);
2344     }
2345   G_WRITE_UNLOCK (&type_rw_lock);
2346 }
2347
2348
2349 /* --- public API functions --- */
2350 gpointer
2351 g_type_class_ref (GType type)
2352 {
2353   TypeNode *node;
2354   
2355   /* optimize for common code path
2356    */
2357   G_WRITE_LOCK (&type_rw_lock);
2358   node = lookup_type_node_I (type);
2359   if (node && node->is_classed && node->data &&
2360       node->data->class.class && node->data->common.ref_count > 0)
2361     {
2362       type_data_ref_Wm (node);
2363       G_WRITE_UNLOCK (&type_rw_lock);
2364       
2365       return node->data->class.class;
2366     }
2367   
2368   if (!node || !node->is_classed ||
2369       (node->data && node->data->common.ref_count < 1))
2370     {
2371       G_WRITE_UNLOCK (&type_rw_lock);
2372       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2373                  type_descriptive_name_I (type));
2374       return NULL;
2375     }
2376   
2377   type_data_ref_Wm (node);
2378   
2379   if (!node->data->class.class)
2380     {
2381       GType ptype = NODE_PARENT_TYPE (node);
2382       GTypeClass *pclass = NULL;
2383       
2384       if (ptype)
2385         {
2386           G_WRITE_UNLOCK (&type_rw_lock);
2387           pclass = g_type_class_ref (ptype);
2388           if (node->data->class.class)
2389             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2390           G_WRITE_LOCK (&type_rw_lock);
2391         }
2392       
2393       type_class_init_Wm (node, pclass);
2394     }
2395   G_WRITE_UNLOCK (&type_rw_lock);
2396   
2397   return node->data->class.class;
2398 }
2399
2400 void
2401 g_type_class_unref (gpointer g_class)
2402 {
2403   TypeNode *node;
2404   GTypeClass *class = g_class;
2405   
2406   g_return_if_fail (g_class != NULL);
2407   
2408   node = lookup_type_node_I (class->g_type);
2409   G_WRITE_LOCK (&type_rw_lock);
2410   if (node && node->is_classed && node->data &&
2411       node->data->class.class == class && node->data->common.ref_count > 0)
2412     type_data_unref_Wm (node, FALSE);
2413   else
2414     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2415                type_descriptive_name_I (class->g_type));
2416   G_WRITE_UNLOCK (&type_rw_lock);
2417 }
2418
2419 void
2420 g_type_class_unref_uncached (gpointer g_class)
2421 {
2422   TypeNode *node;
2423   GTypeClass *class = g_class;
2424   
2425   g_return_if_fail (g_class != NULL);
2426   
2427   G_WRITE_LOCK (&type_rw_lock);
2428   node = lookup_type_node_I (class->g_type);
2429   if (node && node->is_classed && node->data &&
2430       node->data->class.class == class && node->data->common.ref_count > 0)
2431     type_data_unref_Wm (node, TRUE);
2432   else
2433     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2434                type_descriptive_name_I (class->g_type));
2435   G_WRITE_UNLOCK (&type_rw_lock);
2436 }
2437
2438 gpointer
2439 g_type_class_peek (GType type)
2440 {
2441   TypeNode *node;
2442   gpointer class;
2443   
2444   node = lookup_type_node_I (type);
2445   G_READ_LOCK (&type_rw_lock);
2446   if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2447     class = node->data->class.class;
2448   else
2449     class = NULL;
2450   G_READ_UNLOCK (&type_rw_lock);
2451   
2452   return class;
2453 }
2454
2455 gpointer
2456 g_type_class_peek_static (GType type)
2457 {
2458   TypeNode *node;
2459   gpointer class;
2460   
2461   node = lookup_type_node_I (type);
2462   G_READ_LOCK (&type_rw_lock);
2463   if (node && node->is_classed && node->data &&
2464       /* peek only static types: */ node->plugin == NULL &&
2465       node->data->class.class) /* common.ref_count _may_ be 0 */
2466     class = node->data->class.class;
2467   else
2468     class = NULL;
2469   G_READ_UNLOCK (&type_rw_lock);
2470   
2471   return class;
2472 }
2473
2474 gpointer
2475 g_type_class_peek_parent (gpointer g_class)
2476 {
2477   TypeNode *node;
2478   gpointer class = NULL;
2479   
2480   g_return_val_if_fail (g_class != NULL, NULL);
2481   
2482   node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2483   /* We used to acquire a read lock here. That is not necessary, since 
2484    * parent->data->class.class is constant as long as the derived class
2485    * exists. 
2486    */
2487   if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2488     {
2489       node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2490       class = node->data->class.class;
2491     }
2492   else if (NODE_PARENT_TYPE (node))
2493     g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2494   
2495   return class;
2496 }
2497
2498 gpointer
2499 g_type_interface_peek (gpointer instance_class,
2500                        GType    iface_type)
2501 {
2502   TypeNode *node;
2503   TypeNode *iface;
2504   gpointer vtable = NULL;
2505   GTypeClass *class = instance_class;
2506   
2507   g_return_val_if_fail (instance_class != NULL, NULL);
2508   
2509   node = lookup_type_node_I (class->g_type);
2510   iface = lookup_type_node_I (iface_type);
2511   if (node && node->is_instantiatable && iface)
2512     {
2513       IFaceEntry *entry;
2514       
2515       G_READ_LOCK (&type_rw_lock);
2516       
2517       entry = type_lookup_iface_entry_L (node, iface);
2518       if (entry && entry->vtable)       /* entry is relocatable */
2519         vtable = entry->vtable;
2520       
2521       G_READ_UNLOCK (&type_rw_lock);
2522     }
2523   else
2524     g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2525   
2526   return vtable;
2527 }
2528
2529 gpointer
2530 g_type_interface_peek_parent (gpointer g_iface)
2531 {
2532   TypeNode *node;
2533   TypeNode *iface;
2534   gpointer vtable = NULL;
2535   GTypeInterface *iface_class = g_iface;
2536   
2537   g_return_val_if_fail (g_iface != NULL, NULL);
2538   
2539   iface = lookup_type_node_I (iface_class->g_type);
2540   node = lookup_type_node_I (iface_class->g_instance_type);
2541   if (node)
2542     node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2543   if (node && node->is_instantiatable && iface)
2544     {
2545       IFaceEntry *entry;
2546       
2547       G_READ_LOCK (&type_rw_lock);
2548       
2549       entry = type_lookup_iface_entry_L (node, iface);
2550       if (entry && entry->vtable)       /* entry is relocatable */
2551         vtable = entry->vtable;
2552       
2553       G_READ_UNLOCK (&type_rw_lock);
2554     }
2555   else if (node)
2556     g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2557   
2558   return vtable;
2559 }
2560
2561 gpointer
2562 g_type_default_interface_ref (GType g_type)
2563 {
2564   TypeNode *node;
2565   
2566   G_WRITE_LOCK (&type_rw_lock);
2567   
2568   node = lookup_type_node_I (g_type);
2569   if (!node || !NODE_IS_IFACE (node) ||
2570       (node->data && node->data->common.ref_count < 1))
2571     {
2572       G_WRITE_UNLOCK (&type_rw_lock);
2573       g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
2574                  type_descriptive_name_I (g_type));
2575       return NULL;
2576     }
2577   
2578   type_data_ref_Wm (node);
2579
2580   type_iface_ensure_dflt_vtable_Wm (node);
2581
2582   G_WRITE_UNLOCK (&type_rw_lock);
2583   
2584   return node->data->iface.dflt_vtable;
2585 }
2586
2587 gpointer
2588 g_type_default_interface_peek (GType g_type)
2589 {
2590   TypeNode *node;
2591   gpointer vtable;
2592   
2593   node = lookup_type_node_I (g_type);
2594   G_READ_LOCK (&type_rw_lock);
2595   if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
2596     vtable = node->data->iface.dflt_vtable;
2597   else
2598     vtable = NULL;
2599   G_READ_UNLOCK (&type_rw_lock);
2600   
2601   return vtable;
2602 }
2603
2604 void
2605 g_type_default_interface_unref (gpointer g_iface)
2606 {
2607   TypeNode *node;
2608   GTypeInterface *vtable = g_iface;
2609   
2610   g_return_if_fail (g_iface != NULL);
2611   
2612   node = lookup_type_node_I (vtable->g_type);
2613   G_WRITE_LOCK (&type_rw_lock);
2614   if (node && NODE_IS_IFACE (node) &&
2615       node->data->iface.dflt_vtable == g_iface &&
2616       node->data->common.ref_count > 0)
2617     type_data_unref_Wm (node, FALSE);
2618   else
2619     g_warning ("cannot unreference invalid interface default vtable for '%s'",
2620                type_descriptive_name_I (vtable->g_type));
2621   G_WRITE_UNLOCK (&type_rw_lock);
2622 }
2623
2624 G_CONST_RETURN gchar*
2625 g_type_name (GType type)
2626 {
2627   TypeNode *node;
2628   
2629   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2630   
2631   node = lookup_type_node_I (type);
2632   
2633   return node ? NODE_NAME (node) : NULL;
2634 }
2635
2636 GQuark
2637 g_type_qname (GType type)
2638 {
2639   TypeNode *node;
2640   
2641   node = lookup_type_node_I (type);
2642   
2643   return node ? node->qname : 0;
2644 }
2645
2646 GType
2647 g_type_from_name (const gchar *name)
2648 {
2649   GType type = 0;
2650   GQuark quark;
2651   
2652   g_return_val_if_fail (name != NULL, 0);
2653   
2654   quark = g_quark_try_string (name);
2655   if (quark)
2656     {
2657       G_READ_LOCK (&type_rw_lock);
2658       type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2659       G_READ_UNLOCK (&type_rw_lock);
2660     }
2661   
2662   return type;
2663 }
2664
2665 GType
2666 g_type_parent (GType type)
2667 {
2668   TypeNode *node;
2669   
2670   node = lookup_type_node_I (type);
2671   
2672   return node ? NODE_PARENT_TYPE (node) : 0;
2673 }
2674
2675 guint
2676 g_type_depth (GType type)
2677 {
2678   TypeNode *node;
2679   
2680   node = lookup_type_node_I (type);
2681   
2682   return node ? node->n_supers + 1 : 0;
2683 }
2684
2685 GType
2686 g_type_next_base (GType type,
2687                   GType base_type)
2688 {
2689   GType atype = 0;
2690   TypeNode *node;
2691   
2692   node = lookup_type_node_I (type);
2693   if (node)
2694     {
2695       TypeNode *base_node = lookup_type_node_I (base_type);
2696       
2697       if (base_node && base_node->n_supers < node->n_supers)
2698         {
2699           guint n = node->n_supers - base_node->n_supers;
2700           
2701           if (node->supers[n] == base_type)
2702             atype = node->supers[n - 1];
2703         }
2704     }
2705   
2706   return atype;
2707 }
2708
2709 static inline gboolean
2710 type_node_check_conformities_UorL (TypeNode *node,
2711                                    TypeNode *iface_node,
2712                                    /*        support_inheritance */
2713                                    gboolean  support_interfaces,
2714                                    gboolean  support_prerequisites,
2715                                    gboolean  have_lock)
2716 {
2717   gboolean match;
2718   
2719   if (/* support_inheritance && */
2720       NODE_IS_ANCESTOR (iface_node, node))
2721     return TRUE;
2722   
2723   support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2724   support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2725   match = FALSE;
2726   if (support_interfaces || support_prerequisites)
2727     {
2728       if (!have_lock)
2729         G_READ_LOCK (&type_rw_lock);
2730       if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2731         match = TRUE;
2732       else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2733         match = TRUE;
2734       if (!have_lock)
2735         G_READ_UNLOCK (&type_rw_lock);
2736     }
2737   return match;
2738 }
2739
2740 static gboolean
2741 type_node_is_a_L (TypeNode *node,
2742                   TypeNode *iface_node)
2743 {
2744   return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2745 }
2746
2747 static inline gboolean
2748 type_node_conforms_to_U (TypeNode *node,
2749                          TypeNode *iface_node,
2750                          gboolean  support_interfaces,
2751                          gboolean  support_prerequisites)
2752 {
2753   return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2754 }
2755
2756 gboolean
2757 g_type_is_a (GType type,
2758              GType iface_type)
2759 {
2760   TypeNode *node, *iface_node;
2761   gboolean is_a;
2762   
2763   node = lookup_type_node_I (type);
2764   iface_node = lookup_type_node_I (iface_type);
2765   is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2766   
2767   return is_a;
2768 }
2769
2770 GType* /* free result */
2771 g_type_children (GType  type,
2772                  guint *n_children)
2773 {
2774   TypeNode *node;
2775   
2776   node = lookup_type_node_I (type);
2777   if (node)
2778     {
2779       GType *children;
2780       
2781       G_READ_LOCK (&type_rw_lock);      /* ->children is relocatable */
2782       children = g_new (GType, node->n_children + 1);
2783       memcpy (children, node->children, sizeof (GType) * node->n_children);
2784       children[node->n_children] = 0;
2785       
2786       if (n_children)
2787         *n_children = node->n_children;
2788       G_READ_UNLOCK (&type_rw_lock);
2789       
2790       return children;
2791     }
2792   else
2793     {
2794       if (n_children)
2795         *n_children = 0;
2796       
2797       return NULL;
2798     }
2799 }
2800
2801 GType* /* free result */
2802 g_type_interfaces (GType  type,
2803                    guint *n_interfaces)
2804 {
2805   TypeNode *node;
2806   
2807   node = lookup_type_node_I (type);
2808   if (node && node->is_instantiatable)
2809     {
2810       GType *ifaces;
2811       guint i;
2812       
2813       G_READ_LOCK (&type_rw_lock);
2814       ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2815       for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2816         ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2817       ifaces[i] = 0;
2818       
2819       if (n_interfaces)
2820         *n_interfaces = CLASSED_NODE_N_IFACES (node);
2821       G_READ_UNLOCK (&type_rw_lock);
2822       
2823       return ifaces;
2824     }
2825   else
2826     {
2827       if (n_interfaces)
2828         *n_interfaces = 0;
2829       
2830       return NULL;
2831     }
2832 }
2833
2834 typedef struct _QData QData;
2835 struct _GData
2836 {
2837   guint  n_qdatas;
2838   QData *qdatas;
2839 };
2840 struct _QData
2841 {
2842   GQuark   quark;
2843   gpointer data;
2844 };
2845
2846 static inline gpointer
2847 type_get_qdata_L (TypeNode *node,
2848                   GQuark    quark)
2849 {
2850   GData *gdata = node->global_gdata;
2851   
2852   if (quark && gdata && gdata->n_qdatas)
2853     {
2854       QData *qdatas = gdata->qdatas - 1;
2855       guint n_qdatas = gdata->n_qdatas;
2856       
2857       do
2858         {
2859           guint i;
2860           QData *check;
2861           
2862           i = (n_qdatas + 1) / 2;
2863           check = qdatas + i;
2864           if (quark == check->quark)
2865             return check->data;
2866           else if (quark > check->quark)
2867             {
2868               n_qdatas -= i;
2869               qdatas = check;
2870             }
2871           else /* if (quark < check->quark) */
2872             n_qdatas = i - 1;
2873         }
2874       while (n_qdatas);
2875     }
2876   return NULL;
2877 }
2878
2879 gpointer
2880 g_type_get_qdata (GType  type,
2881                   GQuark quark)
2882 {
2883   TypeNode *node;
2884   gpointer data;
2885   
2886   node = lookup_type_node_I (type);
2887   if (node)
2888     {
2889       G_READ_LOCK (&type_rw_lock);
2890       data = type_get_qdata_L (node, quark);
2891       G_READ_UNLOCK (&type_rw_lock);
2892     }
2893   else
2894     {
2895       g_return_val_if_fail (node != NULL, NULL);
2896       data = NULL;
2897     }
2898   return data;
2899 }
2900
2901 static inline void
2902 type_set_qdata_W (TypeNode *node,
2903                   GQuark    quark,
2904                   gpointer  data)
2905 {
2906   GData *gdata;
2907   QData *qdata;
2908   guint i;
2909   
2910   /* setup qdata list if necessary */
2911   if (!node->global_gdata)
2912     node->global_gdata = g_new0 (GData, 1);
2913   gdata = node->global_gdata;
2914   
2915   /* try resetting old data */
2916   qdata = gdata->qdatas;
2917   for (i = 0; i < gdata->n_qdatas; i++)
2918     if (qdata[i].quark == quark)
2919       {
2920         qdata[i].data = data;
2921         return;
2922       }
2923   
2924   /* add new entry */
2925   gdata->n_qdatas++;
2926   gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2927   qdata = gdata->qdatas;
2928   for (i = 0; i < gdata->n_qdatas - 1; i++)
2929     if (qdata[i].quark > quark)
2930       break;
2931   g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2932   qdata[i].quark = quark;
2933   qdata[i].data = data;
2934 }
2935
2936 void
2937 g_type_set_qdata (GType    type,
2938                   GQuark   quark,
2939                   gpointer data)
2940 {
2941   TypeNode *node;
2942   
2943   g_return_if_fail (quark != 0);
2944   
2945   node = lookup_type_node_I (type);
2946   if (node)
2947     {
2948       G_WRITE_LOCK (&type_rw_lock);
2949       type_set_qdata_W (node, quark, data);
2950       G_WRITE_UNLOCK (&type_rw_lock);
2951     }
2952   else
2953     g_return_if_fail (node != NULL);
2954 }
2955
2956 static void
2957 type_add_flags_W (TypeNode  *node,
2958                   GTypeFlags flags)
2959 {
2960   guint dflags;
2961   
2962   g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2963   g_return_if_fail (node != NULL);
2964   
2965   if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2966     g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2967   dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2968   dflags |= flags;
2969   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2970 }
2971
2972 void
2973 g_type_query (GType       type,
2974               GTypeQuery *query)
2975 {
2976   TypeNode *node;
2977   
2978   g_return_if_fail (query != NULL);
2979   
2980   /* if node is not static and classed, we won't allow query */
2981   query->type = 0;
2982   node = lookup_type_node_I (type);
2983   if (node && node->is_classed && !node->plugin)
2984     {
2985       /* type is classed and probably even instantiatable */
2986       G_READ_LOCK (&type_rw_lock);
2987       if (node->data)   /* type is static or referenced */
2988         {
2989           query->type = NODE_TYPE (node);
2990           query->type_name = NODE_NAME (node);
2991           query->class_size = node->data->class.class_size;
2992           query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2993         }
2994       G_READ_UNLOCK (&type_rw_lock);
2995     }
2996 }
2997
2998
2999 /* --- implementation details --- */
3000 gboolean
3001 g_type_test_flags (GType type,
3002                    guint flags)
3003 {
3004   TypeNode *node;
3005   gboolean result = FALSE;
3006   
3007   node = lookup_type_node_I (type);
3008   if (node)
3009     {
3010       guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3011       guint tflags = flags & TYPE_FLAG_MASK;
3012       
3013       if (fflags)
3014         {
3015           GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3016           
3017           fflags = (finfo->type_flags & fflags) == fflags;
3018         }
3019       else
3020         fflags = TRUE;
3021       
3022       if (tflags)
3023         {
3024           G_READ_LOCK (&type_rw_lock);
3025           tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3026           G_READ_UNLOCK (&type_rw_lock);
3027         }
3028       else
3029         tflags = TRUE;
3030       
3031       result = tflags && fflags;
3032     }
3033   
3034   return result;
3035 }
3036
3037 GTypePlugin*
3038 g_type_get_plugin (GType type)
3039 {
3040   TypeNode *node;
3041   
3042   node = lookup_type_node_I (type);
3043   
3044   return node ? node->plugin : NULL;
3045 }
3046
3047 GTypePlugin*
3048 g_type_interface_get_plugin (GType instance_type,
3049                              GType interface_type)
3050 {
3051   TypeNode *node;
3052   TypeNode *iface;
3053   
3054   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);    /* G_TYPE_IS_INTERFACE() is an external call: _U */
3055   
3056   node = lookup_type_node_I (instance_type);  
3057   iface = lookup_type_node_I (interface_type);
3058   if (node && iface)
3059     {
3060       IFaceHolder *iholder;
3061       GTypePlugin *plugin;
3062       
3063       G_READ_LOCK (&type_rw_lock);
3064       
3065       iholder = iface_node_get_holders_L (iface);
3066       while (iholder && iholder->instance_type != instance_type)
3067         iholder = iholder->next;
3068       plugin = iholder ? iholder->plugin : NULL;
3069       
3070       G_READ_UNLOCK (&type_rw_lock);
3071       
3072       return plugin;
3073     }
3074   
3075   g_return_val_if_fail (node == NULL, NULL);
3076   g_return_val_if_fail (iface == NULL, NULL);
3077   
3078   g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3079   
3080   return NULL;
3081 }
3082
3083 GType
3084 g_type_fundamental_next (void)
3085 {
3086   GType type;
3087   
3088   G_READ_LOCK (&type_rw_lock);
3089   type = static_fundamental_next;
3090   G_READ_UNLOCK (&type_rw_lock);
3091   type = G_TYPE_MAKE_FUNDAMENTAL (type);
3092   return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3093 }
3094
3095 GType
3096 g_type_fundamental (GType type_id)
3097 {
3098   TypeNode *node = lookup_type_node_I (type_id);
3099   
3100   return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
3101 }
3102
3103 gboolean
3104 g_type_check_instance_is_a (GTypeInstance *type_instance,
3105                             GType          iface_type)
3106 {
3107   TypeNode *node, *iface;
3108   gboolean check;
3109   
3110   if (!type_instance || !type_instance->g_class)
3111     return FALSE;
3112   
3113   node = lookup_type_node_I (type_instance->g_class->g_type);
3114   iface = lookup_type_node_I (iface_type);
3115   check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3116   
3117   return check;
3118 }
3119
3120 gboolean
3121 g_type_check_class_is_a (GTypeClass *type_class,
3122                          GType       is_a_type)
3123 {
3124   TypeNode *node, *iface;
3125   gboolean check;
3126   
3127   if (!type_class)
3128     return FALSE;
3129   
3130   node = lookup_type_node_I (type_class->g_type);
3131   iface = lookup_type_node_I (is_a_type);
3132   check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3133   
3134   return check;
3135 }
3136
3137 GTypeInstance*
3138 g_type_check_instance_cast (GTypeInstance *type_instance,
3139                             GType          iface_type)
3140 {
3141   if (type_instance)
3142     {
3143       if (type_instance->g_class)
3144         {
3145           TypeNode *node, *iface;
3146           gboolean is_instantiatable, check;
3147           
3148           node = lookup_type_node_I (type_instance->g_class->g_type);
3149           is_instantiatable = node && node->is_instantiatable;
3150           iface = lookup_type_node_I (iface_type);
3151           check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
3152           if (check)
3153             return type_instance;
3154           
3155           if (is_instantiatable)
3156             g_warning ("invalid cast from `%s' to `%s'",
3157                        type_descriptive_name_I (type_instance->g_class->g_type),
3158                        type_descriptive_name_I (iface_type));
3159           else
3160             g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
3161                        type_descriptive_name_I (type_instance->g_class->g_type),
3162                        type_descriptive_name_I (iface_type));
3163         }
3164       else
3165         g_warning ("invalid unclassed pointer in cast to `%s'",
3166                    type_descriptive_name_I (iface_type));
3167     }
3168   
3169   return type_instance;
3170 }
3171
3172 GTypeClass*
3173 g_type_check_class_cast (GTypeClass *type_class,
3174                          GType       is_a_type)
3175 {
3176   if (type_class)
3177     {
3178       TypeNode *node, *iface;
3179       gboolean is_classed, check;
3180       
3181       node = lookup_type_node_I (type_class->g_type);
3182       is_classed = node && node->is_classed;
3183       iface = lookup_type_node_I (is_a_type);
3184       check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
3185       if (check)
3186         return type_class;
3187       
3188       if (is_classed)
3189         g_warning ("invalid class cast from `%s' to `%s'",
3190                    type_descriptive_name_I (type_class->g_type),
3191                    type_descriptive_name_I (is_a_type));
3192       else
3193         g_warning ("invalid unclassed type `%s' in class cast to `%s'",
3194                    type_descriptive_name_I (type_class->g_type),
3195                    type_descriptive_name_I (is_a_type));
3196     }
3197   else
3198     g_warning ("invalid class cast from (NULL) pointer to `%s'",
3199                type_descriptive_name_I (is_a_type));
3200   return type_class;
3201 }
3202
3203 gboolean
3204 g_type_check_instance (GTypeInstance *type_instance)
3205 {
3206   /* this function is just here to make the signal system
3207    * conveniently elaborated on instance checks
3208    */
3209   if (type_instance)
3210     {
3211       if (type_instance->g_class)
3212         {
3213           TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
3214           
3215           if (node && node->is_instantiatable)
3216             return TRUE;
3217           
3218           g_warning ("instance of invalid non-instantiatable type `%s'",
3219                      type_descriptive_name_I (type_instance->g_class->g_type));
3220         }
3221       else
3222         g_warning ("instance with invalid (NULL) class pointer");
3223     }
3224   else
3225     g_warning ("invalid (NULL) pointer instance");
3226   
3227   return FALSE;
3228 }
3229
3230 static inline gboolean
3231 type_check_is_value_type_U (GType type)
3232 {
3233   GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
3234   TypeNode *node;
3235   
3236   /* common path speed up */
3237   node = lookup_type_node_I (type);
3238   if (node && node->mutatable_check_cache)
3239     return TRUE;
3240   
3241   G_READ_LOCK (&type_rw_lock);
3242  restart_check:
3243   if (node)
3244     {
3245       if (node->data && node->data->common.ref_count > 0 &&
3246           node->data->common.value_table->value_init)
3247         tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3248       else if (NODE_IS_IFACE (node))
3249         {
3250           guint i;
3251           
3252           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3253             {
3254               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3255               TypeNode *prnode = lookup_type_node_I (prtype);
3256               
3257               if (prnode->is_instantiatable)
3258                 {
3259                   type = prtype;
3260                   node = lookup_type_node_I (type);
3261                   goto restart_check;
3262                 }
3263             }
3264         }
3265     }
3266   G_READ_UNLOCK (&type_rw_lock);
3267   
3268   return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
3269 }
3270
3271 gboolean
3272 g_type_check_is_value_type (GType type)
3273 {
3274   return type_check_is_value_type_U (type);
3275 }
3276
3277 gboolean
3278 g_type_check_value (GValue *value)
3279 {
3280   return value && type_check_is_value_type_U (value->g_type);
3281 }
3282
3283 gboolean
3284 g_type_check_value_holds (GValue *value,
3285                           GType   type)
3286 {
3287   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
3288 }
3289
3290 GTypeValueTable*
3291 g_type_value_table_peek (GType type)
3292 {
3293   GTypeValueTable *vtable = NULL;
3294   TypeNode *node = lookup_type_node_I (type);
3295   gboolean has_refed_data, has_table;
3296   TypeData *data;
3297
3298   /* speed up common code path, we're not 100% safe here,
3299    * but we should only get called with referenced types anyway
3300    */
3301   data = node ? node->data : NULL;
3302   if (node && node->mutatable_check_cache)
3303     return data->common.value_table;
3304
3305   G_READ_LOCK (&type_rw_lock);
3306   
3307  restart_table_peek:
3308   has_refed_data = node && node->data && node->data->common.ref_count;
3309   has_table = has_refed_data && node->data->common.value_table->value_init;
3310   if (has_refed_data)
3311     {
3312       if (has_table)
3313         vtable = node->data->common.value_table;
3314       else if (NODE_IS_IFACE (node))
3315         {
3316           guint i;
3317           
3318           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
3319             {
3320               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
3321               TypeNode *prnode = lookup_type_node_I (prtype);
3322               
3323               if (prnode->is_instantiatable)
3324                 {
3325                   type = prtype;
3326                   node = lookup_type_node_I (type);
3327                   goto restart_table_peek;
3328                 }
3329             }
3330         }
3331     }
3332   
3333   G_READ_UNLOCK (&type_rw_lock);
3334   
3335   if (vtable)
3336     return vtable;
3337   
3338   if (!node)
3339     g_warning (G_STRLOC ": type id `%lu' is invalid", type);
3340   if (!has_refed_data)
3341     g_warning ("can't peek value table for type `%s' which is not currently referenced",
3342                type_descriptive_name_I (type));
3343   
3344   return NULL;
3345 }
3346
3347 G_CONST_RETURN gchar*
3348 g_type_name_from_instance (GTypeInstance *instance)
3349 {
3350   if (!instance)
3351     return "<NULL-instance>";
3352   else
3353     return g_type_name_from_class (instance->g_class);
3354 }
3355
3356 G_CONST_RETURN gchar*
3357 g_type_name_from_class (GTypeClass *g_class)
3358 {
3359   if (!g_class)
3360     return "<NULL-class>";
3361   else
3362     return g_type_name (g_class->g_type);
3363 }
3364
3365
3366 /* --- initialization --- */
3367 void
3368 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
3369 {
3370   G_LOCK_DEFINE_STATIC (type_init_lock);
3371   const gchar *env_string;
3372   GTypeInfo info;
3373   TypeNode *node;
3374   GType type;
3375   
3376   G_LOCK (type_init_lock);
3377   
3378   G_WRITE_LOCK (&type_rw_lock);
3379   
3380   if (static_quark_type_flags)
3381     {
3382       G_WRITE_UNLOCK (&type_rw_lock);
3383       G_UNLOCK (type_init_lock);
3384       return;
3385     }
3386   
3387   /* setup GObject library wide debugging flags */
3388   _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
3389   env_string = g_getenv ("GOBJECT_DEBUG");
3390   if (env_string != NULL)
3391     {
3392       static GDebugKey debug_keys[] = {
3393         { "objects", G_TYPE_DEBUG_OBJECTS },
3394         { "signals", G_TYPE_DEBUG_SIGNALS },
3395       };
3396       
3397       _g_type_debug_flags |= g_parse_debug_string (env_string,
3398                                                    debug_keys,
3399                                                    sizeof (debug_keys) / sizeof (debug_keys[0]));
3400       env_string = NULL;
3401     }
3402   
3403   /* quarks */
3404   static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3405   static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3406   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3407   
3408   /* type qname hash table */
3409   static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3410   
3411   /* invalid type G_TYPE_INVALID (0)
3412    */
3413   static_fundamental_type_nodes[0] = NULL;
3414   
3415   /* void type G_TYPE_NONE
3416    */
3417   node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
3418   type = NODE_TYPE (node);
3419   g_assert (type == G_TYPE_NONE);
3420   
3421   /* interface fundamental type G_TYPE_INTERFACE (!classed)
3422    */
3423   memset (&info, 0, sizeof (info));
3424   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
3425   type = NODE_TYPE (node);
3426   type_data_make_W (node, &info, NULL);
3427   g_assert (type == G_TYPE_INTERFACE);
3428   
3429   G_WRITE_UNLOCK (&type_rw_lock);
3430   
3431   g_value_c_init ();
3432
3433   /* G_TYPE_TYPE_PLUGIN
3434    */
3435   g_type_plugin_get_type ();
3436   
3437   /* G_TYPE_* value types
3438    */
3439   g_value_types_init ();
3440   
3441   /* G_TYPE_ENUM & G_TYPE_FLAGS
3442    */
3443   g_enum_types_init ();
3444   
3445   /* G_TYPE_BOXED
3446    */
3447   g_boxed_type_init ();
3448   
3449   /* G_TYPE_PARAM
3450    */
3451   g_param_type_init ();
3452   
3453   /* G_TYPE_OBJECT
3454    */
3455   g_object_type_init ();
3456   
3457   /* G_TYPE_PARAM_* pspec types
3458    */
3459   g_param_spec_types_init ();
3460   
3461   /* Value Transformations
3462    */
3463   g_value_transforms_init ();
3464   
3465   /* Signal system
3466    */
3467   g_signal_init ();
3468   
3469   G_UNLOCK (type_init_lock);
3470 }
3471
3472 void 
3473 g_type_init (void)
3474 {
3475   g_type_init_with_debug_flags (0);
3476 }
3477
3478 void
3479 g_type_class_add_private (gpointer g_class,
3480                           gsize    private_size)
3481 {
3482   GType instance_type = ((GTypeClass *)g_class)->g_type;
3483   TypeNode *node = lookup_type_node_I (instance_type);
3484   gsize offset;
3485
3486   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3487     {
3488       g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3489                  type_descriptive_name_I (instance_type));
3490       return;
3491     }
3492
3493   if (NODE_PARENT_TYPE (node))
3494     {
3495       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3496       if (node->data->instance.private_size != pnode->data->instance.private_size)
3497         {
3498           g_warning ("g_type_add_private() called multiple times for the same type");
3499           return;
3500         }
3501     }
3502   
3503   G_WRITE_LOCK (&type_rw_lock);
3504
3505   offset = ALIGN_STRUCT (node->data->instance.private_size);
3506   node->data->instance.private_size = offset + private_size;
3507   
3508   G_WRITE_UNLOCK (&type_rw_lock);
3509 }
3510
3511 gpointer
3512 g_type_instance_get_private (GTypeInstance *instance,
3513                              GType          private_type)
3514 {
3515   TypeNode *instance_node;
3516   TypeNode *private_node;
3517   TypeNode *parent_node;
3518   GTypeClass *class;
3519   gsize offset;
3520
3521   g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3522
3523   /* while instances are initialized, their class pointers change,
3524    * so figure the instances real class first
3525    */
3526   class = instance_real_class_get (instance);
3527   if (!class)
3528     class = instance->g_class;
3529
3530   instance_node = lookup_type_node_I (class->g_type);
3531   if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3532     {
3533       g_warning ("instance of invalid non-instantiatable type `%s'",
3534                  type_descriptive_name_I (instance->g_class->g_type));
3535       return NULL;
3536     }
3537
3538   private_node = lookup_type_node_I (private_type);
3539   if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3540     {
3541       g_warning ("attempt to retrieve private data for invalid type '%s'",
3542                  type_descriptive_name_I (private_type));
3543       return NULL;
3544     }
3545
3546   /* Note that we don't need a read lock, since instance existing
3547    * means that the instance class and all parent classes
3548    * exist, so the node->data, node->data->instance.instance_size,
3549    * and node->data->instance.private_size are not going to be changed.
3550    * for any of the relevant types.
3551    */
3552
3553   offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3554
3555   if (NODE_PARENT_TYPE (private_node))
3556     {
3557       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3558       g_assert (parent_node->data && parent_node->data->common.ref_count);
3559
3560       if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
3561         {
3562           g_warning ("g_type_instance_get_private() requires a prior call to g_type_class_add_private()");
3563           return NULL;
3564         }
3565
3566       offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3567     }
3568
3569   return G_STRUCT_MEMBER_P (instance, offset);
3570 }
3571
3572 #define __G_TYPE_C__
3573 #include "gobjectaliasdef.c"