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