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