Add some generated files.
[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 #ifndef G_DISABLE_ASSERT
1395   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1396 #endif
1397   IFaceHolder *iholder = type_iface_retrive_holder_info_Wm (iface, NODE_TYPE (node));
1398   GTypeInterface *vtable;
1399   
1400   g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1401   
1402   vtable = g_malloc0 (iface->data->iface.vtable_size);
1403   type_propagate_iface_vtable_W (node, iface, vtable);
1404   vtable->g_type = NODE_TYPE (iface);
1405   vtable->g_instance_type = NODE_TYPE (node);
1406   
1407   if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1408     {
1409       G_WRITE_UNLOCK (&type_rw_lock);
1410       if (iface->data->iface.vtable_init_base)
1411         iface->data->iface.vtable_init_base (vtable);
1412       if (iholder->info->interface_init)
1413         iholder->info->interface_init (vtable, iholder->info->interface_data);
1414       G_WRITE_LOCK (&type_rw_lock);
1415     }
1416 }
1417
1418 static void
1419 type_iface_vtable_finalize_Wm (TypeNode       *iface,
1420                                TypeNode       *node,
1421                                GTypeInterface *vtable)
1422 {
1423 #ifndef G_DISABLE_ASSERT
1424   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1425 #endif
1426   IFaceHolder *iholder = iface_node_get_holders_L (iface);
1427   
1428   g_assert (entry && entry->vtable == vtable);
1429   
1430   while (iholder->instance_type != NODE_TYPE (node))
1431     iholder = iholder->next;
1432   g_assert (iholder && iholder->info);
1433   
1434   type_propagate_iface_vtable_W (node, iface, NULL);
1435   if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1436     {
1437       G_WRITE_UNLOCK (&type_rw_lock);
1438       if (iholder->info->interface_finalize)
1439         iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1440       if (iface->data->iface.vtable_finalize_base)
1441         iface->data->iface.vtable_finalize_base (vtable);
1442       G_WRITE_LOCK (&type_rw_lock);
1443     }
1444   vtable->g_type = 0;
1445   vtable->g_instance_type = 0;
1446   g_free (vtable);
1447   
1448   type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1449 }
1450
1451 static void
1452 type_class_init_Wm (TypeNode   *node,
1453                     GTypeClass *pclass)
1454 {
1455   GSList *slist, *init_slist = NULL;
1456   GTypeClass *class;
1457   IFaceEntry *entry;
1458   TypeNode *bnode;
1459   guint i;
1460   
1461   g_assert (node->is_classed && node->data &&
1462             node->data->class.class_size &&
1463             !node->data->class.class);
1464   
1465   class = g_malloc0 (node->data->class.class_size);
1466   node->data->class.class = class;
1467   
1468   if (pclass)
1469     {
1470       TypeNode *pnode = lookup_type_node_L (pclass->g_type);
1471       
1472       memcpy (class, pclass, pnode->data->class.class_size);
1473     }
1474   class->g_type = NODE_TYPE (node);
1475   
1476   G_WRITE_UNLOCK (&type_rw_lock);
1477   
1478   /* stack all base class initialization functions, so we
1479    * call them in ascending order.
1480    */
1481   G_READ_LOCK (&type_rw_lock);
1482   for (bnode = node; bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1483     if (bnode->data->class.class_init_base)
1484       init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1485   G_READ_UNLOCK (&type_rw_lock);
1486   for (slist = init_slist; slist; slist = slist->next)
1487     {
1488       GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1489       
1490       class_init_base (class);
1491     }
1492   g_slist_free (init_slist);
1493   
1494   if (node->data->class.class_init)
1495     node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1496   
1497   G_WRITE_LOCK (&type_rw_lock);
1498   
1499   /* ok, we got the class done, now initialize all interfaces */
1500   for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1501     if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1502       entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1503   while (entry)
1504     {
1505       type_iface_vtable_init_Wm (lookup_type_node_L (entry->iface_type), node);
1506       
1507       for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1508         if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1509           entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1510     }
1511 }
1512
1513 static void
1514 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1515 {
1516   IFaceEntry *entry;
1517   guint i;
1518   
1519   g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1520   
1521   g_message ("finalizing interfaces for %sClass `%s'",
1522              type_descriptive_name_L (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1523              type_descriptive_name_L (NODE_TYPE (node)));
1524   
1525   for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1526     if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1527         CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1528       entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1529   while (entry)
1530     {
1531       type_iface_vtable_finalize_Wm (lookup_type_node_L (entry->iface_type), node, entry->vtable);
1532       
1533       for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1534         if (CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable &&
1535             CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable->g_instance_type == NODE_TYPE (node))
1536           entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1537     }
1538 }
1539
1540 static void
1541 type_data_finalize_class_U (TypeNode  *node,
1542                             ClassData *cdata)
1543 {
1544   GTypeClass *class = cdata->class;
1545   TypeNode *bnode;
1546   
1547   g_assert (cdata->class && cdata->common.ref_count == 0);
1548   
1549   if (cdata->class_finalize)
1550     cdata->class_finalize (class, (gpointer) cdata->class_data);
1551   
1552   /* call all base class destruction functions in descending order
1553    */
1554   if (cdata->class_finalize_base)
1555     cdata->class_finalize_base (class);
1556   G_READ_LOCK (&type_rw_lock);
1557   for (bnode = lookup_type_node_L (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_L (NODE_PARENT_TYPE (bnode)))
1558     if (bnode->data->class.class_finalize_base)
1559       {
1560         G_READ_UNLOCK (&type_rw_lock);
1561         bnode->data->class.class_finalize_base (class);
1562         G_READ_LOCK (&type_rw_lock);
1563       }
1564   G_READ_UNLOCK (&type_rw_lock);
1565   
1566   class->g_type = 0;
1567   g_free (cdata->class);
1568 }
1569
1570 static void
1571 type_data_last_unref_Wm (GType    type,
1572                          gboolean uncached)
1573 {
1574   TypeNode *node = lookup_type_node_L (type);
1575   
1576   g_return_if_fail (node != NULL && node->plugin != NULL);
1577   
1578   if (!node->data || node->data->common.ref_count == 0)
1579     {
1580       g_warning ("cannot drop last reference to unreferenced type `%s'",
1581                  type_descriptive_name_U (type));
1582       return;
1583     }
1584   
1585   if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1586     {
1587       guint i;
1588       
1589       G_WRITE_UNLOCK (&type_rw_lock);
1590       G_READ_LOCK (&type_rw_lock);
1591       for (i = 0; i < static_n_class_cache_funcs; i++)
1592         {
1593           GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1594           gpointer cache_data = static_class_cache_funcs[i].cache_data;
1595           gboolean need_break;
1596           
1597           G_READ_UNLOCK (&type_rw_lock);
1598           need_break = cache_func (cache_data, node->data->class.class);
1599           G_READ_LOCK (&type_rw_lock);
1600           if (!node->data || node->data->common.ref_count == 0)
1601             INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1602           if (need_break)
1603             break;
1604         }
1605       G_READ_UNLOCK (&type_rw_lock);
1606       G_WRITE_LOCK (&type_rw_lock);
1607     }
1608   
1609   if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1610     node->data->common.ref_count -= 1;
1611   else
1612     {
1613       GType ptype = NODE_PARENT_TYPE (node);
1614       TypeData *tdata;
1615       
1616       node->data->common.ref_count = 0;
1617       
1618       if (node->is_instantiatable && node->data->instance.mem_chunk)
1619         {
1620           g_mem_chunk_destroy (node->data->instance.mem_chunk);
1621           node->data->instance.mem_chunk = NULL;
1622         }
1623       
1624       tdata = node->data;
1625       if (node->is_classed && tdata->class.class)
1626         {
1627           if (CLASSED_NODE_N_IFACES (node))
1628             type_data_finalize_class_ifaces_Wm (node);
1629           node->data = NULL;
1630           G_WRITE_UNLOCK (&type_rw_lock);
1631           type_data_finalize_class_U (node, &tdata->class);
1632           G_WRITE_LOCK (&type_rw_lock);
1633         }
1634       else
1635         node->data = NULL;
1636
1637       /* freeing tdata->common.value_table and its contents is taking care of
1638        * by allocating it in one chunk with tdata
1639        */
1640       g_free (tdata);
1641       
1642       if (ptype)
1643         type_data_unref_Wm (lookup_type_node_L (ptype), FALSE);
1644       G_WRITE_UNLOCK (&type_rw_lock);
1645       g_type_plugin_unuse (node->plugin);
1646       G_WRITE_LOCK (&type_rw_lock);
1647     }
1648 }
1649
1650 void
1651 g_type_add_class_cache_func (gpointer            cache_data,
1652                              GTypeClassCacheFunc cache_func)
1653 {
1654   guint i;
1655   
1656   g_return_if_fail (cache_func != NULL);
1657   
1658   G_WRITE_LOCK (&type_rw_lock);
1659   i = static_n_class_cache_funcs++;
1660   static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1661   static_class_cache_funcs[i].cache_data = cache_data;
1662   static_class_cache_funcs[i].cache_func = cache_func;
1663   G_WRITE_UNLOCK (&type_rw_lock);
1664 }
1665
1666 void
1667 g_type_remove_class_cache_func (gpointer            cache_data,
1668                                 GTypeClassCacheFunc cache_func)
1669 {
1670   gboolean found_it = FALSE;
1671   guint i;
1672   
1673   g_return_if_fail (cache_func != NULL);
1674   
1675   G_WRITE_LOCK (&type_rw_lock);
1676   for (i = 0; i < static_n_class_cache_funcs; i++)
1677     if (static_class_cache_funcs[i].cache_data == cache_data &&
1678         static_class_cache_funcs[i].cache_func == cache_func)
1679       {
1680         static_n_class_cache_funcs--;
1681         g_memmove (static_class_cache_funcs + i,
1682                    static_class_cache_funcs + i + 1,
1683                    sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1684         static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1685         found_it = TRUE;
1686         break;
1687       }
1688   G_WRITE_UNLOCK (&type_rw_lock);
1689   
1690   if (!found_it)
1691     g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1692                cache_func, cache_data);
1693 }
1694
1695
1696 /* --- type registration --- */
1697 GType
1698 g_type_register_fundamental (GType                       type_id,
1699                              const gchar                *type_name,
1700                              const GTypeInfo            *info,
1701                              const GTypeFundamentalInfo *finfo,
1702                              GTypeFlags                  flags)
1703 {
1704   GTypeFundamentalInfo *node_finfo;
1705   TypeNode *node;
1706   
1707   g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1708   g_return_val_if_fail (type_id > 0, 0);
1709   g_return_val_if_fail (type_name != NULL, 0);
1710   g_return_val_if_fail (info != NULL, 0);
1711   g_return_val_if_fail (finfo != NULL, 0);
1712   
1713   if (!check_type_name_U (type_name))
1714     return 0;
1715   if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1716     {
1717       g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1718                  type_name,
1719                  type_id);
1720       return 0;
1721     }
1722   if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1723       !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1724     {
1725       g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1726                  type_name);
1727       return 0;
1728     }
1729   G_WRITE_LOCK (&type_rw_lock);
1730   if (lookup_type_node_L (type_id))
1731     {
1732       G_WRITE_UNLOCK (&type_rw_lock);
1733       g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1734                  type_descriptive_name_U (type_id),
1735                  type_name);
1736       return 0;
1737     }
1738   
1739   node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1740   node_finfo = type_node_fundamental_info_L (node);
1741   type_add_flags_W (node, flags);
1742   
1743   if (check_type_info_L (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1744     type_data_make_W (node, info,
1745                       check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1746   G_WRITE_UNLOCK (&type_rw_lock);
1747   
1748   return NODE_TYPE (node);
1749 }
1750
1751 GType
1752 g_type_register_static (GType            parent_type,
1753                         const gchar     *type_name,
1754                         const GTypeInfo *info,
1755                         GTypeFlags       flags)
1756 {
1757   TypeNode *pnode, *node;
1758   GType type = 0;
1759   
1760   g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1761   g_return_val_if_fail (parent_type > 0, 0);
1762   g_return_val_if_fail (type_name != NULL, 0);
1763   g_return_val_if_fail (info != NULL, 0);
1764   
1765   if (!check_type_name_U (type_name) ||
1766       !check_derivation_U (parent_type, type_name))
1767     return 0;
1768   if (info->class_finalize)
1769     {
1770       g_warning ("class finalizer specified for static type `%s'",
1771                  type_name);
1772       return 0;
1773     }
1774   
1775   G_WRITE_LOCK (&type_rw_lock);
1776   pnode = lookup_type_node_L (parent_type);
1777   type_data_ref_Wm (pnode);
1778   if (check_type_info_L (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1779     {
1780       node = type_node_new_W (pnode, type_name, NULL);
1781       type_add_flags_W (node, flags);
1782       type = NODE_TYPE (node);
1783       type_data_make_W (node, info,
1784                         check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1785     }
1786   G_WRITE_UNLOCK (&type_rw_lock);
1787   
1788   return type;
1789 }
1790
1791 GType
1792 g_type_register_dynamic (GType        parent_type,
1793                          const gchar *type_name,
1794                          GTypePlugin *plugin,
1795                          GTypeFlags   flags)
1796 {
1797   TypeNode *pnode, *node;
1798   GType type;
1799   
1800   g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, 0);
1801   g_return_val_if_fail (parent_type > 0, 0);
1802   g_return_val_if_fail (type_name != NULL, 0);
1803   g_return_val_if_fail (plugin != NULL, 0);
1804   
1805   if (!check_type_name_U (type_name) ||
1806       !check_derivation_U (parent_type, type_name) ||
1807       !check_plugin_U (plugin, TRUE, FALSE, type_name))
1808     return 0;
1809   
1810   G_WRITE_LOCK (&type_rw_lock);
1811   pnode = lookup_type_node_L (parent_type);
1812   node = type_node_new_W (pnode, type_name, plugin);
1813   type_add_flags_W (node, flags);
1814   type = NODE_TYPE (node);
1815   G_WRITE_UNLOCK (&type_rw_lock);
1816   
1817   return type;
1818 }
1819
1820 void
1821 g_type_add_interface_static (GType                 instance_type,
1822                              GType                 interface_type,
1823                              const GInterfaceInfo *info)
1824 {
1825   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1826   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1827   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1828   
1829   G_WRITE_LOCK (&type_rw_lock);
1830   if (check_add_interface_L (instance_type, interface_type))
1831     {
1832       TypeNode *node = lookup_type_node_L (instance_type);
1833       TypeNode *iface = lookup_type_node_L (interface_type);
1834       
1835       if (check_interface_info_L (iface, NODE_TYPE (node), info))
1836         type_add_interface_W (node, iface, info, NULL);
1837     }
1838   G_WRITE_UNLOCK (&type_rw_lock);
1839 }
1840
1841 void
1842 g_type_add_interface_dynamic (GType        instance_type,
1843                               GType        interface_type,
1844                               GTypePlugin *plugin)
1845 {
1846   TypeNode *node;
1847   
1848   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1849   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1850   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1851   
1852   G_READ_LOCK (&type_rw_lock);
1853   node = lookup_type_node_L (instance_type);
1854   G_READ_UNLOCK (&type_rw_lock);
1855   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
1856     return;
1857   
1858   G_WRITE_LOCK (&type_rw_lock);
1859   if (check_add_interface_L (instance_type, interface_type))
1860     {
1861       TypeNode *iface = lookup_type_node_L (interface_type);
1862       
1863       type_add_interface_W (node, iface, NULL, plugin);
1864     }
1865   G_WRITE_UNLOCK (&type_rw_lock);
1866 }
1867
1868
1869 /* --- public API functions --- */
1870 gpointer
1871 g_type_class_ref (GType type)
1872 {
1873   TypeNode *node;
1874   
1875   /* optimize for common code path
1876    */
1877   G_WRITE_LOCK (&type_rw_lock);
1878   node = lookup_type_node_L (type);
1879   if (node && node->is_classed && node->data &&
1880       node->data->class.class && node->data->common.ref_count > 0)
1881     {
1882       type_data_ref_Wm (node);
1883       G_WRITE_UNLOCK (&type_rw_lock);
1884       
1885       return node->data->class.class;
1886     }
1887   
1888   if (!node || !node->is_classed ||
1889       (node->data && node->data->common.ref_count < 1))
1890     {
1891       G_WRITE_UNLOCK (&type_rw_lock);
1892       g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1893                  type_descriptive_name_U (type));
1894       return NULL;
1895     }
1896   
1897   type_data_ref_Wm (node);
1898   
1899   if (!node->data->class.class)
1900     {
1901       GType ptype = NODE_PARENT_TYPE (node);
1902       GTypeClass *pclass = NULL;
1903       
1904       if (ptype)
1905         {
1906           G_WRITE_UNLOCK (&type_rw_lock);
1907           pclass = g_type_class_ref (ptype);
1908           if (node->data->class.class)
1909             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1910           G_WRITE_LOCK (&type_rw_lock);
1911         }
1912       
1913       type_class_init_Wm (node, pclass);
1914     }
1915   G_WRITE_UNLOCK (&type_rw_lock);
1916   
1917   return node->data->class.class;
1918 }
1919
1920 void
1921 g_type_class_unref (gpointer g_class)
1922 {
1923   TypeNode *node;
1924   GTypeClass *class = g_class;
1925   
1926   g_return_if_fail (g_class != NULL);
1927   
1928   G_WRITE_LOCK (&type_rw_lock);
1929   node = lookup_type_node_L (class->g_type);
1930   if (node && node->is_classed && node->data &&
1931       node->data->class.class == class && node->data->common.ref_count > 0)
1932     type_data_unref_Wm (node, FALSE);
1933   else
1934     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1935                type_descriptive_name_L (class->g_type));
1936   G_WRITE_UNLOCK (&type_rw_lock);
1937 }
1938
1939 void
1940 g_type_class_unref_uncached (gpointer g_class)
1941 {
1942   TypeNode *node;
1943   GTypeClass *class = g_class;
1944   
1945   g_return_if_fail (g_class != NULL);
1946   
1947   G_WRITE_LOCK (&type_rw_lock);
1948   node = lookup_type_node_L (class->g_type);
1949   if (node && node->is_classed && node->data &&
1950       node->data->class.class == class && node->data->common.ref_count > 0)
1951     type_data_unref_Wm (node, TRUE);
1952   else
1953     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1954                type_descriptive_name_L (class->g_type));
1955   G_WRITE_UNLOCK (&type_rw_lock);
1956 }
1957
1958 gpointer
1959 g_type_class_peek (GType type)
1960 {
1961   TypeNode *node;
1962   gpointer class;
1963   
1964   G_READ_LOCK (&type_rw_lock);
1965   node = lookup_type_node_L (type);
1966   if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1967     class = node->data->class.class;
1968   else
1969     class = NULL;
1970   G_READ_UNLOCK (&type_rw_lock);
1971   
1972   return class;
1973 }
1974
1975 gpointer
1976 g_type_class_peek_parent (gpointer g_class)
1977 {
1978   TypeNode *node;
1979   gpointer class;
1980   
1981   g_return_val_if_fail (g_class != NULL, NULL);
1982   
1983   G_READ_LOCK (&type_rw_lock);
1984   node = lookup_type_node_L (G_TYPE_FROM_CLASS (g_class));
1985   if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1986     {
1987       node = lookup_type_node_L (NODE_PARENT_TYPE (node));
1988       class = node->data->class.class;
1989     }
1990   else
1991     class = NULL;
1992   G_READ_UNLOCK (&type_rw_lock);
1993   
1994   return class;
1995 }
1996
1997 gpointer
1998 g_type_interface_peek (gpointer instance_class,
1999                        GType    iface_type)
2000 {
2001   TypeNode *node;
2002   TypeNode *iface;
2003   gpointer vtable = NULL;
2004   GTypeClass *class = instance_class;
2005   
2006   g_return_val_if_fail (instance_class != NULL, NULL);
2007   
2008   G_READ_LOCK (&type_rw_lock);
2009   node = lookup_type_node_L (class->g_type);
2010   iface = lookup_type_node_L (iface_type);
2011   if (node && node->is_instantiatable && iface)
2012     {
2013       IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2014       
2015       if (entry && entry->vtable)
2016         vtable = entry->vtable;
2017     }
2018   G_READ_UNLOCK (&type_rw_lock);
2019   
2020   return vtable;
2021 }
2022
2023 G_CONST_RETURN gchar*
2024 g_type_name (GType type)
2025 {
2026   TypeNode *node;
2027   
2028   g_return_val_if_uninitialized (static_last_fundamental_id, g_type_init, NULL);
2029
2030   G_READ_LOCK (&type_rw_lock);
2031   node = lookup_type_node_L (type);
2032   G_READ_UNLOCK (&type_rw_lock);
2033   
2034   return node ? NODE_NAME (node) : NULL;
2035 }
2036
2037 GQuark
2038 g_type_qname (GType type)
2039 {
2040   TypeNode *node;
2041   
2042   G_READ_LOCK (&type_rw_lock);
2043   node = lookup_type_node_L (type);
2044   G_READ_UNLOCK (&type_rw_lock);
2045   
2046   return node ? node->qname : 0;
2047 }
2048
2049 GType
2050 g_type_from_name (const gchar *name)
2051 {
2052   GType type = 0;
2053   GQuark quark;
2054   
2055   g_return_val_if_fail (name != NULL, 0);
2056   
2057   quark = g_quark_try_string (name);
2058   if (quark)
2059     {
2060       G_READ_LOCK (&type_rw_lock);
2061       type = GPOINTER_TO_UINT (g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark)));
2062       G_READ_UNLOCK (&type_rw_lock);
2063     }
2064   
2065   return type;
2066 }
2067
2068 GType
2069 g_type_parent (GType type)
2070 {
2071   TypeNode *node;
2072   
2073   G_READ_LOCK (&type_rw_lock);
2074   node = lookup_type_node_L (type);
2075   G_READ_UNLOCK (&type_rw_lock);
2076   
2077   return node ? NODE_PARENT_TYPE (node) : 0;
2078 }
2079
2080 guint
2081 g_type_depth (GType type)
2082 {
2083   TypeNode *node;
2084   
2085   G_READ_LOCK (&type_rw_lock);
2086   node = lookup_type_node_L (type);
2087   G_READ_UNLOCK (&type_rw_lock);
2088   
2089   return node ? node->n_supers + 1 : 0;
2090 }
2091
2092 GType
2093 g_type_next_base (GType type,
2094                   GType base_type)
2095 {
2096   GType atype = 0;
2097   TypeNode *node;
2098   
2099   G_READ_LOCK (&type_rw_lock);
2100   node = lookup_type_node_L (type);
2101   if (node)
2102     {
2103       TypeNode *base_node = lookup_type_node_L (base_type);
2104       
2105       if (base_node && base_node->n_supers < node->n_supers)
2106         {
2107           guint n = node->n_supers - base_node->n_supers;
2108           
2109           if (node->supers[n] == base_type)
2110             atype = node->supers[n - 1];
2111         }
2112     }
2113   G_READ_UNLOCK (&type_rw_lock);
2114   
2115   return atype;
2116 }
2117
2118 static inline gboolean
2119 type_node_is_a_L (TypeNode *node,
2120                   TypeNode *iface_node,
2121                   /*        support_inheritance */
2122                   gboolean  support_interfaces,
2123                   gboolean  support_prerequisites)
2124 {
2125   if (support_interfaces &&
2126       node->is_instantiatable && NODE_IS_IFACE (iface_node) &&
2127       type_lookup_iface_entry_L (node, iface_node) != NULL)
2128     return TRUE;
2129   else if (/* support_inheritance && */
2130            iface_node->n_supers <= node->n_supers &&
2131            node->supers[node->n_supers - iface_node->n_supers] == NODE_TYPE (iface_node))
2132     return TRUE;
2133   else if (support_prerequisites &&
2134            NODE_IS_IFACE (node) &&
2135            type_lookup_prerequisite (node, NODE_TYPE (iface_node)))
2136     return TRUE;
2137   else
2138     return FALSE;
2139 }
2140
2141 gboolean
2142 g_type_is_a (GType type,
2143              GType iface_type)
2144 {
2145   TypeNode *node, *iface_node;
2146   gboolean is_a;
2147
2148   G_READ_LOCK (&type_rw_lock);
2149   node = lookup_type_node_L (type);
2150   iface_node = lookup_type_node_L (iface_type);
2151   is_a = node && iface_node && type_node_is_a_L (node, iface_node, TRUE, TRUE);
2152   G_READ_UNLOCK (&type_rw_lock);
2153
2154   return is_a;
2155 }
2156
2157 guint
2158 g_type_fundamental_branch_last (GType type)
2159 {
2160   GType ftype = G_TYPE_FUNDAMENTAL (type);
2161   guint last_type;
2162   
2163   G_READ_LOCK (&type_rw_lock);
2164   last_type = ftype < static_last_fundamental_id ? static_branch_seqnos[ftype] : 0;
2165   G_READ_UNLOCK (&type_rw_lock);
2166   
2167   return last_type;
2168 }
2169
2170 GType* /* free result */
2171 g_type_children (GType  type,
2172                  guint *n_children)
2173 {
2174   TypeNode *node;
2175   
2176   G_READ_LOCK (&type_rw_lock);
2177   node = lookup_type_node_L (type);
2178   if (node)
2179     {
2180       GType *children = g_new (GType, node->n_children + 1);
2181       
2182       memcpy (children, node->children, sizeof (GType) * node->n_children);
2183       children[node->n_children] = 0;
2184       
2185       if (n_children)
2186         *n_children = node->n_children;
2187       G_READ_UNLOCK (&type_rw_lock);
2188       
2189       return children;
2190     }
2191   else
2192     {
2193       G_READ_UNLOCK (&type_rw_lock);
2194       if (n_children)
2195         *n_children = 0;
2196       
2197       return NULL;
2198     }
2199 }
2200
2201 GType* /* free result */
2202 g_type_interfaces (GType  type,
2203                    guint *n_interfaces)
2204 {
2205   TypeNode *node;
2206   
2207   G_READ_LOCK (&type_rw_lock);
2208   node = lookup_type_node_L (type);
2209   if (node && node->is_instantiatable)
2210     {
2211       GType *ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2212       guint i;
2213       
2214       for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2215         ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2216       ifaces[i] = 0;
2217       
2218       if (n_interfaces)
2219         *n_interfaces = CLASSED_NODE_N_IFACES (node);
2220       G_READ_UNLOCK (&type_rw_lock);
2221       
2222       return ifaces;
2223     }
2224   else
2225     {
2226       G_READ_UNLOCK (&type_rw_lock);
2227       if (n_interfaces)
2228         *n_interfaces = 0;
2229       
2230       return NULL;
2231     }
2232 }
2233
2234 typedef struct _QData QData;
2235 struct _GData
2236 {
2237   guint  n_qdatas;
2238   QData *qdatas;
2239 };
2240 struct _QData
2241 {
2242   GQuark   quark;
2243   gpointer data;
2244 };
2245
2246 static inline gpointer
2247 type_get_qdata_L (TypeNode *node,
2248                   GQuark    quark)
2249 {
2250   GData *gdata = node->global_gdata;
2251   
2252   if (quark && gdata && gdata->n_qdatas)
2253     {
2254       QData *qdatas = gdata->qdatas - 1;
2255       guint n_qdatas = gdata->n_qdatas;
2256       
2257       do
2258         {
2259           guint i;
2260           QData *check;
2261           
2262           i = (n_qdatas + 1) / 2;
2263           check = qdatas + i;
2264           if (quark == check->quark)
2265             return check->data;
2266           else if (quark > check->quark)
2267             {
2268               n_qdatas -= i;
2269               qdatas = check;
2270             }
2271           else /* if (quark < check->quark) */
2272             n_qdatas = i - 1;
2273         }
2274       while (n_qdatas);
2275     }
2276   return NULL;
2277 }
2278
2279 gpointer
2280 g_type_get_qdata (GType  type,
2281                   GQuark quark)
2282 {
2283   TypeNode *node;
2284   gpointer data;
2285   
2286   G_READ_LOCK (&type_rw_lock);
2287   node = lookup_type_node_L (type);
2288   if (node)
2289     {
2290       data = type_get_qdata_L (node, quark);
2291       G_READ_UNLOCK (&type_rw_lock);
2292     }
2293   else
2294     {
2295       G_READ_UNLOCK (&type_rw_lock);
2296       g_return_val_if_fail (node != NULL, NULL);
2297       data = NULL;
2298     }
2299   return data;
2300 }
2301
2302 static inline void
2303 type_set_qdata_W (TypeNode *node,
2304                   GQuark    quark,
2305                   gpointer  data)
2306 {
2307   GData *gdata;
2308   QData *qdata;
2309   guint i;
2310   
2311   /* setup qdata list if necessary */
2312   if (!node->global_gdata)
2313     node->global_gdata = g_new0 (GData, 1);
2314   gdata = node->global_gdata;
2315   
2316   /* try resetting old data */
2317   qdata = gdata->qdatas;
2318   for (i = 0; i < gdata->n_qdatas; i++)
2319     if (qdata[i].quark == quark)
2320       {
2321         qdata[i].data = data;
2322         return;
2323       }
2324   
2325   /* add new entry */
2326   gdata->n_qdatas++;
2327   gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2328   qdata = gdata->qdatas;
2329   for (i = 0; i < gdata->n_qdatas - 1; i++)
2330     if (qdata[i].quark > quark)
2331       break;
2332   g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2333   qdata[i].quark = quark;
2334   qdata[i].data = data;
2335 }
2336
2337 void
2338 g_type_set_qdata (GType    type,
2339                   GQuark   quark,
2340                   gpointer data)
2341 {
2342   TypeNode *node;
2343   
2344   g_return_if_fail (quark != 0);
2345   
2346   G_WRITE_LOCK (&type_rw_lock);
2347   node = lookup_type_node_L (type);
2348   if (node)
2349     {
2350       type_set_qdata_W (node, quark, data);
2351       G_WRITE_UNLOCK (&type_rw_lock);
2352     }
2353   else
2354     {
2355       G_WRITE_UNLOCK (&type_rw_lock);
2356       g_return_if_fail (node != NULL);
2357     }
2358 }
2359
2360 static void
2361 type_add_flags_W (TypeNode  *node,
2362                   GTypeFlags flags)
2363 {
2364   guint dflags;
2365   
2366   g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2367   g_return_if_fail (node != NULL);
2368   
2369   if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2370     g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2371   dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2372   dflags |= flags;
2373   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2374 }
2375
2376 void
2377 g_type_query (GType       type,
2378               GTypeQuery *query)
2379 {
2380   TypeNode *node;
2381
2382   g_return_if_fail (query != NULL);
2383   
2384   G_READ_LOCK (&type_rw_lock);
2385   node = lookup_type_node_L (type);
2386   if (node && node->is_classed && !node->plugin && node->data)
2387     {
2388       /* type is classed and static, probably even instantiatable */
2389
2390       query->type = NODE_TYPE (node);
2391       query->type_name = NODE_NAME (node);
2392       query->class_size = node->data->class.class_size;
2393       query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2394     }
2395   else
2396     {
2397       /* node is not static and classed, won't allow query */
2398       query->type = 0;
2399     }
2400   G_READ_UNLOCK (&type_rw_lock);
2401 }
2402
2403
2404 /* --- implementation details --- */
2405 gboolean
2406 g_type_check_flags (GType type,
2407                     guint flags)
2408 {
2409   TypeNode *node;
2410   gboolean result = FALSE;
2411   
2412   G_READ_LOCK (&type_rw_lock);
2413   node = lookup_type_node_L (type);
2414   if (node)
2415     {
2416       guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2417       guint tflags = flags & TYPE_FLAG_MASK;
2418       
2419       if (fflags)
2420         {
2421           GTypeFundamentalInfo *finfo = type_node_fundamental_info_L (node);
2422           
2423           fflags = (finfo->type_flags & fflags) == fflags;
2424         }
2425       else
2426         fflags = TRUE;
2427       
2428       if (tflags)
2429         tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2430       else
2431         tflags = TRUE;
2432       
2433       result = tflags && fflags;
2434     }
2435   G_READ_UNLOCK (&type_rw_lock);
2436   
2437   return result;
2438 }
2439
2440 GTypePlugin*
2441 g_type_get_plugin (GType type)
2442 {
2443   TypeNode *node;
2444   
2445   G_READ_LOCK (&type_rw_lock);
2446   node = lookup_type_node_L (type);
2447   G_READ_UNLOCK (&type_rw_lock);
2448   
2449   return node ? node->plugin : NULL;
2450 }
2451
2452 GTypePlugin*
2453 g_type_interface_get_plugin (GType instance_type,
2454                              GType interface_type)
2455 {
2456   TypeNode *node;
2457   TypeNode *iface;
2458   
2459   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
2460   
2461   G_READ_LOCK (&type_rw_lock);
2462   node = lookup_type_node_L (instance_type);  
2463   iface = lookup_type_node_L (interface_type);
2464   if (node && iface)
2465     {
2466       IFaceHolder *iholder = iface_node_get_holders_L (iface);
2467       
2468       while (iholder && iholder->instance_type != instance_type)
2469         iholder = iholder->next;
2470       G_READ_UNLOCK (&type_rw_lock);
2471       
2472       if (iholder)
2473         return iholder->plugin;
2474     }
2475   else
2476     G_READ_UNLOCK (&type_rw_lock);
2477   
2478   g_return_val_if_fail (node == NULL, NULL);
2479   g_return_val_if_fail (iface == NULL, NULL);
2480   
2481   g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2482   
2483   return NULL;
2484 }
2485
2486 GType
2487 g_type_fundamental_last (void)
2488 {
2489   GType type;
2490   
2491   G_READ_LOCK (&type_rw_lock);
2492   type = static_last_fundamental_id;
2493   G_READ_UNLOCK (&type_rw_lock);
2494   
2495   return type;
2496 }
2497
2498 gboolean
2499 g_type_instance_is_a (GTypeInstance *type_instance,
2500                       GType          iface_type)
2501 {
2502   TypeNode *node, *iface;
2503   gboolean check;
2504
2505   if (!type_instance || !type_instance->g_class)
2506     return FALSE;
2507
2508   G_READ_LOCK (&type_rw_lock);
2509   node = lookup_type_node_L (type_instance->g_class->g_type);
2510   iface = lookup_type_node_L (iface_type);
2511   check = node && node->is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2512   G_READ_UNLOCK (&type_rw_lock);
2513
2514   return check;
2515 }
2516
2517 gboolean
2518 g_type_class_is_a (GTypeClass *type_class,
2519                    GType       is_a_type)
2520 {
2521   TypeNode *node, *iface;
2522   gboolean check;
2523
2524   if (!type_class)
2525     return FALSE;
2526
2527   G_READ_LOCK (&type_rw_lock);
2528   node = lookup_type_node_L (type_class->g_type);
2529   iface = lookup_type_node_L (is_a_type);
2530   check = node && node->is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2531   G_READ_UNLOCK (&type_rw_lock);
2532
2533   return check;
2534 }
2535
2536 GTypeInstance*
2537 g_type_check_instance_cast (GTypeInstance *type_instance,
2538                             GType          iface_type)
2539 {
2540   if (type_instance)
2541     {
2542       if (type_instance->g_class)
2543         {
2544           TypeNode *node, *iface;
2545           gboolean is_instantiatable, check;
2546
2547           G_READ_LOCK (&type_rw_lock);
2548           node = lookup_type_node_L (type_instance->g_class->g_type);
2549           is_instantiatable = node && node->is_instantiatable;
2550           iface = lookup_type_node_L (iface_type);
2551           check = is_instantiatable && iface && type_node_is_a_L (node, iface, TRUE, FALSE);
2552           G_READ_UNLOCK (&type_rw_lock);
2553           if (check)
2554             return type_instance;
2555
2556           if (is_instantiatable)
2557             g_warning ("invalid cast from `%s' to `%s'",
2558                        type_descriptive_name_U (type_instance->g_class->g_type),
2559                        type_descriptive_name_U (iface_type));
2560           else
2561             g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2562                        type_descriptive_name_U (type_instance->g_class->g_type),
2563                        type_descriptive_name_U (iface_type));
2564         }
2565       else
2566         g_warning ("invalid unclassed pointer in cast to `%s'",
2567                    type_descriptive_name_U (iface_type));
2568     }
2569   else
2570     g_warning ("invalid cast from (NULL) pointer to `%s'",
2571                type_descriptive_name_U (iface_type));
2572   
2573   return type_instance;
2574 }
2575
2576 GTypeClass*
2577 g_type_check_class_cast (GTypeClass *type_class,
2578                          GType       is_a_type)
2579 {
2580   if (type_class)
2581     {
2582       TypeNode *node, *iface;
2583       gboolean is_classed, check;
2584       
2585       G_READ_LOCK (&type_rw_lock);
2586       node = lookup_type_node_L (type_class->g_type);
2587       is_classed = node && node->is_classed;
2588       iface = lookup_type_node_L (is_a_type);
2589       check = is_classed && iface && type_node_is_a_L (node, iface, FALSE, FALSE);
2590       G_READ_UNLOCK (&type_rw_lock);
2591       if (check)
2592         return type_class;
2593
2594       if (is_classed)
2595         g_warning ("invalid class cast from `%s' to `%s'",
2596                    type_descriptive_name_U (type_class->g_type),
2597                    type_descriptive_name_U (is_a_type));
2598       else
2599         g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2600                    type_descriptive_name_U (type_class->g_type),
2601                    type_descriptive_name_U (is_a_type));
2602     }
2603   else
2604     g_warning ("invalid class cast from (NULL) pointer to `%s'",
2605                type_descriptive_name_U (is_a_type));
2606   return type_class;
2607 }
2608
2609 gboolean
2610 g_type_check_instance (GTypeInstance *type_instance)
2611 {
2612   /* this function is just here to make the signal system
2613    * conveniently elaborated on instance checks
2614    */
2615   if (type_instance)
2616     {
2617       if (type_instance->g_class)
2618         {
2619           TypeNode *node;
2620           gboolean is_instantiatable;
2621           
2622           G_READ_LOCK (&type_rw_lock);
2623           node = lookup_type_node_L (type_instance->g_class->g_type);
2624           is_instantiatable = node && node->is_instantiatable;
2625           G_READ_UNLOCK (&type_rw_lock);
2626           if (is_instantiatable)
2627             return TRUE;
2628
2629           g_warning ("instance of invalid non-instantiatable type `%s'",
2630                      type_descriptive_name_U (type_instance->g_class->g_type));
2631         }
2632       else
2633         g_warning ("instance with invalid (NULL) class pointer");
2634     }
2635   else
2636     g_warning ("invalid (NULL) pointer instance");
2637
2638   return FALSE;
2639 }
2640
2641 static inline gboolean
2642 type_check_is_value_type_U (GType type)
2643 {
2644   GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2645   TypeNode *node;
2646
2647   G_READ_LOCK (&type_rw_lock);
2648  restart_check:
2649   node = lookup_type_node_L (type);
2650   if (node)
2651     {
2652       if (node->data && node->data->common.ref_count > 0 &&
2653           node->data->common.value_table->value_init)
2654         tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2655       else if (NODE_IS_IFACE (node))
2656         {
2657           guint i;
2658
2659           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2660             {
2661               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2662               TypeNode *prnode = lookup_type_node_L (prtype);
2663
2664               if (prnode->is_instantiatable)
2665                 {
2666                   type = prtype;
2667                   goto restart_check;
2668                 }
2669             }
2670         }
2671     }
2672   G_READ_UNLOCK (&type_rw_lock);
2673
2674   return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2675 }
2676
2677 gboolean
2678 g_type_check_is_value_type (GType type)
2679 {
2680   return type_check_is_value_type_U (type);
2681 }
2682
2683 gboolean
2684 g_type_check_value (GValue *value)
2685 {
2686   return value && type_check_is_value_type_U (value->g_type);
2687 }
2688
2689 gboolean
2690 g_type_check_value_holds (GValue *value,
2691                           GType   type)
2692 {
2693   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2694 }
2695
2696 GTypeValueTable*
2697 g_type_value_table_peek (GType type)
2698 {
2699   TypeNode *node;
2700   GTypeValueTable *vtable;
2701   
2702   G_READ_LOCK (&type_rw_lock);
2703  restart_table_peek:
2704   node = lookup_type_node_L (type);
2705   if (!node)
2706     {
2707       g_warning ("type id `%u' is invalid", type);
2708       G_READ_UNLOCK (&type_rw_lock);
2709       return NULL;
2710     }
2711   if (!node->data || node->data->common.ref_count < 1)
2712     {
2713       g_warning ("can't peek value table for type `%s' which is not currently referenced",
2714                  type_descriptive_name_L (type));
2715       G_READ_UNLOCK (&type_rw_lock);
2716       return NULL;
2717     }
2718   if (node->data->common.value_table->value_init)
2719     vtable = node->data->common.value_table;
2720   else if (NODE_IS_IFACE (node))
2721     {
2722       guint i;
2723
2724       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2725         {
2726           GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2727           TypeNode *prnode = lookup_type_node_L (prtype);
2728
2729           if (prnode->is_instantiatable)
2730             {
2731               type = prtype;
2732               goto restart_table_peek;
2733             }
2734         }
2735       vtable = NULL;
2736     }
2737   else
2738     vtable = NULL;
2739   G_READ_UNLOCK (&type_rw_lock);
2740   
2741   return vtable;
2742 }
2743
2744 G_CONST_RETURN gchar*
2745 g_type_name_from_instance (GTypeInstance *instance)
2746 {
2747   if (!instance)
2748     return "<NULL-instance>";
2749   else
2750     return g_type_name_from_class (instance->g_class);
2751 }
2752
2753 G_CONST_RETURN gchar*
2754 g_type_name_from_class (GTypeClass *g_class)
2755 {
2756   if (!g_class)
2757     return "<NULL-class>";
2758   else
2759     return g_type_name (g_class->g_type);
2760 }
2761
2762
2763 /* --- foreign prototypes --- */
2764 extern void     g_value_types_init      (void); /* sync with gvaluetypes.c */
2765 extern void     g_enum_types_init       (void); /* sync with genums.c */
2766 extern void     g_param_type_init       (void); /* sync with gparam.c */
2767 extern void     g_boxed_type_init       (void); /* sync with gboxed.c */
2768 extern void     g_object_type_init      (void); /* sync with gobject.c */
2769 extern void     g_param_spec_types_init (void); /* sync with gparamspecs.c */
2770 extern void     g_value_transforms_init (void); /* sync with gvaluetransform.c */
2771 extern void     g_signal_init           (void); /* sync with gsignal.c */
2772
2773
2774 /* --- initialization --- */
2775 void
2776 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2777 {
2778   G_LOCK_DEFINE_STATIC (type_init_lock);
2779   static TypeNode *type0_node = NULL;
2780   const gchar *env_string;
2781   GTypeInfo info;
2782   TypeNode *node;
2783   GType type;
2784   
2785   G_LOCK (type_init_lock);
2786   
2787   G_WRITE_LOCK (&type_rw_lock);
2788   
2789   if (static_last_fundamental_id)
2790     {
2791       G_WRITE_UNLOCK (&type_rw_lock);
2792       G_UNLOCK (type_init_lock);
2793       return;
2794     }
2795
2796   /* setup GRuntime wide debugging flags */
2797   _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2798   env_string = g_getenv ("GRUNTIME_DEBUG");
2799   if (env_string != NULL)
2800     {
2801       static GDebugKey debug_keys[] = {
2802         { "objects", G_TYPE_DEBUG_OBJECTS },
2803         { "signals", G_TYPE_DEBUG_SIGNALS },
2804       };
2805
2806       _g_type_debug_flags |= g_parse_debug_string (env_string,
2807                                                    debug_keys,
2808                                                    sizeof (debug_keys) / sizeof (debug_keys[0]));
2809       env_string = NULL;
2810     }
2811   
2812   /* quarks */
2813   static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
2814   static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
2815   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
2816   
2817   /* type qname hash table */
2818   static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2819   
2820   /* invalid type G_TYPE_INVALID (0)
2821    */
2822   static_last_fundamental_id = 1;
2823   static_type_nodes = g_renew (TypeNode**, static_type_nodes, static_last_fundamental_id);
2824   static_type_nodes[0] = &type0_node;
2825   static_branch_seqnos = g_renew (GType, static_branch_seqnos, static_last_fundamental_id);
2826   static_branch_seqnos[0] = 1;
2827   
2828   /* void type G_TYPE_NONE
2829    */
2830   node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
2831   type = NODE_TYPE (node);
2832   g_assert (type == G_TYPE_NONE);
2833   
2834   /* interface fundamental type G_TYPE_INTERFACE (!classed)
2835    */
2836   memset (&info, 0, sizeof (info));
2837   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2838   type = NODE_TYPE (node);
2839   type_data_make_W (node, &info, NULL);
2840   g_assert (type == G_TYPE_INTERFACE);
2841   
2842   G_WRITE_UNLOCK (&type_rw_lock);
2843   
2844   /* G_TYPE_TYPE_PLUGIN
2845    */
2846   g_type_plugin_get_type ();
2847   
2848   /* G_TYPE_* value types
2849    */
2850   g_value_types_init ();
2851   
2852   /* G_TYPE_ENUM & G_TYPE_FLAGS
2853    */
2854   g_enum_types_init ();
2855   
2856   /* G_TYPE_BOXED
2857    */
2858   g_boxed_type_init ();
2859   
2860   /* G_TYPE_PARAM
2861    */
2862   g_param_type_init ();
2863   
2864   /* G_TYPE_OBJECT
2865    */
2866   g_object_type_init ();
2867   
2868   /* G_TYPE_PARAM_* pspec types
2869    */
2870   g_param_spec_types_init ();
2871   
2872   /* Value Transformations
2873    */
2874   g_value_transforms_init ();
2875
2876   /* Signal system
2877    */
2878   g_signal_init ();
2879   
2880   G_UNLOCK (type_init_lock);
2881 }
2882
2883 void 
2884 g_type_init (void)
2885 {
2886   g_type_init_with_debug_flags (0);
2887 }