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