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