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