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