Only access node->data->instance when the node is instantiable.
[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
1589       if (node->is_instantiatable)
1590         {
1591           /* We need to initialize the private_size here rather than in
1592            * type_data_make_W() since the class init for the parent
1593            * class may have changed pnode->data->instance.private_size.
1594            */
1595           node->data->instance.private_size = pnode->data->instance.private_size;
1596         }
1597     }
1598   class->g_type = NODE_TYPE (node);
1599   
1600   G_WRITE_UNLOCK (&type_rw_lock);
1601   
1602   /* stack all base class initialization functions, so we
1603    * call them in ascending order.
1604    */
1605   for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1606     if (bnode->data->class.class_init_base)
1607       init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1608   for (slist = init_slist; slist; slist = slist->next)
1609     {
1610       GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1611       
1612       class_init_base (class);
1613     }
1614   g_slist_free (init_slist);
1615   
1616   if (node->data->class.class_init)
1617     node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1618   
1619   G_WRITE_LOCK (&type_rw_lock);
1620   
1621   /* ok, we got the class done, now initialize all interfaces, either
1622    * from parent, or through our holder info
1623    */
1624   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1625   entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1626   while (entry)
1627     {
1628       g_assert (entry->vtable == NULL);
1629       
1630       if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1631         {
1632           guint j;
1633           
1634           /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1635            * need to get this interface from parent
1636            */
1637           g_assert (pnode != NULL);
1638           
1639           for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1640             {
1641               IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1642               
1643               if (pentry->iface_type == entry->iface_type)
1644                 {
1645                   entry->vtable = pentry->vtable;
1646                   break;
1647                 }
1648             }
1649           g_assert (entry->vtable != NULL);
1650         }
1651       
1652       /* refetch entry, IFACES_ENTRIES might be modified */
1653       for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1654         if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1655           entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1656     }
1657 }
1658
1659 static void
1660 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1661 {
1662   guint i;
1663
1664   g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1665
1666  reiterate:
1667   for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1668     {
1669       IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1670       if (entry->vtable)
1671         {
1672           if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1673             {
1674               /* refetch entries, IFACES_ENTRIES might be modified */
1675               goto reiterate;
1676             }
1677           else
1678             {
1679               /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1680                * iface vtable came from parent
1681                */
1682               entry->vtable = NULL;
1683             }
1684         }
1685     }
1686 }
1687
1688 static void
1689 type_data_finalize_class_U (TypeNode  *node,
1690                             ClassData *cdata)
1691 {
1692   GTypeClass *class = cdata->class;
1693   TypeNode *bnode;
1694   
1695   g_assert (cdata->class && cdata->common.ref_count == 0);
1696   
1697   if (cdata->class_finalize)
1698     cdata->class_finalize (class, (gpointer) cdata->class_data);
1699   
1700   /* call all base class destruction functions in descending order
1701    */
1702   if (cdata->class_finalize_base)
1703     cdata->class_finalize_base (class);
1704   for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1705     if (bnode->data->class.class_finalize_base)
1706       bnode->data->class.class_finalize_base (class);
1707   
1708   class->g_type = 0;
1709   g_free (cdata->class);
1710 }
1711
1712 static void
1713 type_data_last_unref_Wm (GType    type,
1714                          gboolean uncached)
1715 {
1716   TypeNode *node = lookup_type_node_I (type);
1717   
1718   g_return_if_fail (node != NULL && node->plugin != NULL);
1719   
1720   if (!node->data || node->data->common.ref_count == 0)
1721     {
1722       g_warning ("cannot drop last reference to unreferenced type `%s'",
1723                  type_descriptive_name_I (type));
1724       return;
1725     }
1726   
1727   if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1728     {
1729       guint i;
1730       
1731       G_WRITE_UNLOCK (&type_rw_lock);
1732       G_READ_LOCK (&type_rw_lock);
1733       for (i = 0; i < static_n_class_cache_funcs; i++)
1734         {
1735           GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1736           gpointer cache_data = static_class_cache_funcs[i].cache_data;
1737           gboolean need_break;
1738           
1739           G_READ_UNLOCK (&type_rw_lock);
1740           need_break = cache_func (cache_data, node->data->class.class);
1741           G_READ_LOCK (&type_rw_lock);
1742           if (!node->data || node->data->common.ref_count == 0)
1743             INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1744           if (need_break)
1745             break;
1746         }
1747       G_READ_UNLOCK (&type_rw_lock);
1748       G_WRITE_LOCK (&type_rw_lock);
1749     }
1750   
1751   if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1752     node->data->common.ref_count -= 1;
1753   else
1754     {
1755       GType ptype = NODE_PARENT_TYPE (node);
1756       TypeData *tdata;
1757       
1758       node->data->common.ref_count = 0;
1759       
1760       if (node->is_instantiatable && node->data->instance.mem_chunk)
1761         {
1762           g_mem_chunk_destroy (node->data->instance.mem_chunk);
1763           node->data->instance.mem_chunk = NULL;
1764         }
1765       
1766       tdata = node->data;
1767       if (node->is_classed && tdata->class.class)
1768         {
1769           if (CLASSED_NODE_N_IFACES (node))
1770             type_data_finalize_class_ifaces_Wm (node);
1771           node->mutatable_check_cache = FALSE;
1772           node->data = NULL;
1773           G_WRITE_UNLOCK (&type_rw_lock);
1774           type_data_finalize_class_U (node, &tdata->class);
1775           G_WRITE_LOCK (&type_rw_lock);
1776         }
1777       else
1778         {
1779           node->mutatable_check_cache = FALSE;
1780           node->data = NULL;
1781         }
1782       
1783       /* freeing tdata->common.value_table and its contents is taking care of
1784        * by allocating it in one chunk with tdata
1785        */
1786       g_free (tdata);
1787       
1788       if (ptype)
1789         type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1790       G_WRITE_UNLOCK (&type_rw_lock);
1791       g_type_plugin_unuse (node->plugin);
1792       G_WRITE_LOCK (&type_rw_lock);
1793     }
1794 }
1795
1796 void
1797 g_type_add_class_cache_func (gpointer            cache_data,
1798                              GTypeClassCacheFunc cache_func)
1799 {
1800   guint i;
1801   
1802   g_return_if_fail (cache_func != NULL);
1803   
1804   G_WRITE_LOCK (&type_rw_lock);
1805   i = static_n_class_cache_funcs++;
1806   static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1807   static_class_cache_funcs[i].cache_data = cache_data;
1808   static_class_cache_funcs[i].cache_func = cache_func;
1809   G_WRITE_UNLOCK (&type_rw_lock);
1810 }
1811
1812 void
1813 g_type_remove_class_cache_func (gpointer            cache_data,
1814                                 GTypeClassCacheFunc cache_func)
1815 {
1816   gboolean found_it = FALSE;
1817   guint i;
1818   
1819   g_return_if_fail (cache_func != NULL);
1820   
1821   G_WRITE_LOCK (&type_rw_lock);
1822   for (i = 0; i < static_n_class_cache_funcs; i++)
1823     if (static_class_cache_funcs[i].cache_data == cache_data &&
1824         static_class_cache_funcs[i].cache_func == cache_func)
1825       {
1826         static_n_class_cache_funcs--;
1827         g_memmove (static_class_cache_funcs + i,
1828                    static_class_cache_funcs + i + 1,
1829                    sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1830         static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1831         found_it = TRUE;
1832         break;
1833       }
1834   G_WRITE_UNLOCK (&type_rw_lock);
1835   
1836   if (!found_it)
1837     g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1838                cache_func, cache_data);
1839 }
1840
1841
1842 /* --- type registration --- */
1843 GType
1844 g_type_register_fundamental (GType                       type_id,
1845                              const gchar                *type_name,
1846                              const GTypeInfo            *info,
1847                              const GTypeFundamentalInfo *finfo,
1848                              GTypeFlags                  flags)
1849 {
1850   GTypeFundamentalInfo *node_finfo;
1851   TypeNode *node;
1852   
1853   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1854   g_return_val_if_fail (type_id > 0, 0);
1855   g_return_val_if_fail (type_name != NULL, 0);
1856   g_return_val_if_fail (info != NULL, 0);
1857   g_return_val_if_fail (finfo != NULL, 0);
1858   
1859   if (!check_type_name_I (type_name))
1860     return 0;
1861   if ((type_id & TYPE_ID_MASK) ||
1862       type_id > G_TYPE_FUNDAMENTAL_MAX)
1863     {
1864       g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
1865                  type_name,
1866                  type_id);
1867       return 0;
1868     }
1869   if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1870       !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1871     {
1872       g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1873                  type_name);
1874       return 0;
1875     }
1876   if (lookup_type_node_I (type_id))
1877     {
1878       g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1879                  type_descriptive_name_I (type_id),
1880                  type_name);
1881       return 0;
1882     }
1883   
1884   G_WRITE_LOCK (&type_rw_lock);
1885   node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1886   node_finfo = type_node_fundamental_info_I (node);
1887   type_add_flags_W (node, flags);
1888   
1889   if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1890     type_data_make_W (node, info,
1891                       check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1892   G_WRITE_UNLOCK (&type_rw_lock);
1893   
1894   return NODE_TYPE (node);
1895 }
1896
1897 GType
1898 g_type_register_static (GType            parent_type,
1899                         const gchar     *type_name,
1900                         const GTypeInfo *info,
1901                         GTypeFlags       flags)
1902 {
1903   TypeNode *pnode, *node;
1904   GType type = 0;
1905   
1906   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1907   g_return_val_if_fail (parent_type > 0, 0);
1908   g_return_val_if_fail (type_name != NULL, 0);
1909   g_return_val_if_fail (info != NULL, 0);
1910   
1911   if (!check_type_name_I (type_name) ||
1912       !check_derivation_I (parent_type, type_name))
1913     return 0;
1914   if (info->class_finalize)
1915     {
1916       g_warning ("class finalizer specified for static type `%s'",
1917                  type_name);
1918       return 0;
1919     }
1920   
1921   pnode = lookup_type_node_I (parent_type);
1922   G_WRITE_LOCK (&type_rw_lock);
1923   type_data_ref_Wm (pnode);
1924   if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1925     {
1926       node = type_node_new_W (pnode, type_name, NULL);
1927       type_add_flags_W (node, flags);
1928       type = NODE_TYPE (node);
1929       type_data_make_W (node, info,
1930                         check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1931     }
1932   G_WRITE_UNLOCK (&type_rw_lock);
1933   
1934   return type;
1935 }
1936
1937 GType
1938 g_type_register_dynamic (GType        parent_type,
1939                          const gchar *type_name,
1940                          GTypePlugin *plugin,
1941                          GTypeFlags   flags)
1942 {
1943   TypeNode *pnode, *node;
1944   GType type;
1945   
1946   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1947   g_return_val_if_fail (parent_type > 0, 0);
1948   g_return_val_if_fail (type_name != NULL, 0);
1949   g_return_val_if_fail (plugin != NULL, 0);
1950   
1951   if (!check_type_name_I (type_name) ||
1952       !check_derivation_I (parent_type, type_name) ||
1953       !check_plugin_U (plugin, TRUE, FALSE, type_name))
1954     return 0;
1955   
1956   G_WRITE_LOCK (&type_rw_lock);
1957   pnode = lookup_type_node_I (parent_type);
1958   node = type_node_new_W (pnode, type_name, plugin);
1959   type_add_flags_W (node, flags);
1960   type = NODE_TYPE (node);
1961   G_WRITE_UNLOCK (&type_rw_lock);
1962   
1963   return type;
1964 }
1965
1966 void
1967 g_type_add_interface_static (GType                 instance_type,
1968                              GType                 interface_type,
1969                              const GInterfaceInfo *info)
1970 {
1971   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1972   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1973   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1974   
1975   G_WRITE_LOCK (&type_rw_lock);
1976   if (check_add_interface_L (instance_type, interface_type))
1977     {
1978       TypeNode *node = lookup_type_node_I (instance_type);
1979       TypeNode *iface = lookup_type_node_I (interface_type);
1980       
1981       if (check_interface_info_I (iface, NODE_TYPE (node), info))
1982         {
1983           type_add_interface_W (node, iface, info, NULL);
1984           /* if we have a class already, the interface vtable needs to
1985            * be initialized as well
1986            */
1987           if (node->data && node->data->class.class)
1988             type_iface_vtable_init_Wm (iface, node);
1989         }
1990     }
1991   G_WRITE_UNLOCK (&type_rw_lock);
1992 }
1993
1994 void
1995 g_type_add_interface_dynamic (GType        instance_type,
1996                               GType        interface_type,
1997                               GTypePlugin *plugin)
1998 {
1999   TypeNode *node;
2000   
2001   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2002   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2003   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2004   
2005   node = lookup_type_node_I (instance_type);
2006   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2007     return;
2008   
2009   G_WRITE_LOCK (&type_rw_lock);
2010   if (check_add_interface_L (instance_type, interface_type))
2011     {
2012       TypeNode *iface = lookup_type_node_I (interface_type);
2013       
2014       type_add_interface_W (node, iface, NULL, plugin);
2015       /* if we have a class already, the interface vtable needs to
2016        * be initialized as well
2017        */
2018       if (node->data && node->data->class.class)
2019         type_iface_vtable_init_Wm (iface, node);
2020     }
2021   G_WRITE_UNLOCK (&type_rw_lock);
2022 }
2023
2024
2025 /* --- public API functions --- */
2026 gpointer
2027 g_type_class_ref (GType type)
2028 {
2029   TypeNode *node;
2030   
2031   /* optimize for common code path
2032    */
2033   G_WRITE_LOCK (&type_rw_lock);
2034   node = lookup_type_node_I (type);
2035   if (node && node->is_classed && node->data &&
2036       node->data->class.class && node->data->common.ref_count > 0)
2037     {
2038       type_data_ref_Wm (node);
2039       G_WRITE_UNLOCK (&type_rw_lock);
2040       
2041       return node->data->class.class;
2042     }
2043   
2044   if (!node || !node->is_classed ||
2045       (node->data && node->data->common.ref_count < 1))
2046     {
2047       G_WRITE_UNLOCK (&type_rw_lock);
2048       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2049                  type_descriptive_name_I (type));
2050       return NULL;
2051     }
2052   
2053   type_data_ref_Wm (node);
2054   
2055   if (!node->data->class.class)
2056     {
2057       GType ptype = NODE_PARENT_TYPE (node);
2058       GTypeClass *pclass = NULL;
2059       
2060       if (ptype)
2061         {
2062           G_WRITE_UNLOCK (&type_rw_lock);
2063           pclass = g_type_class_ref (ptype);
2064           if (node->data->class.class)
2065             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2066           G_WRITE_LOCK (&type_rw_lock);
2067         }
2068       
2069       type_class_init_Wm (node, pclass);
2070     }
2071   G_WRITE_UNLOCK (&type_rw_lock);
2072   
2073   return node->data->class.class;
2074 }
2075
2076 void
2077 g_type_class_unref (gpointer g_class)
2078 {
2079   TypeNode *node;
2080   GTypeClass *class = g_class;
2081   
2082   g_return_if_fail (g_class != NULL);
2083   
2084   node = lookup_type_node_I (class->g_type);
2085   G_WRITE_LOCK (&type_rw_lock);
2086   if (node && node->is_classed && node->data &&
2087       node->data->class.class == class && node->data->common.ref_count > 0)
2088     type_data_unref_Wm (node, FALSE);
2089   else
2090     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2091                type_descriptive_name_I (class->g_type));
2092   G_WRITE_UNLOCK (&type_rw_lock);
2093 }
2094
2095 void
2096 g_type_class_unref_uncached (gpointer g_class)
2097 {
2098   TypeNode *node;
2099   GTypeClass *class = g_class;
2100   
2101   g_return_if_fail (g_class != NULL);
2102   
2103   G_WRITE_LOCK (&type_rw_lock);
2104   node = lookup_type_node_I (class->g_type);
2105   if (node && node->is_classed && node->data &&
2106       node->data->class.class == class && node->data->common.ref_count > 0)
2107     type_data_unref_Wm (node, TRUE);
2108   else
2109     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2110                type_descriptive_name_I (class->g_type));
2111   G_WRITE_UNLOCK (&type_rw_lock);
2112 }
2113
2114 gpointer
2115 g_type_class_peek (GType type)
2116 {
2117   TypeNode *node;
2118   gpointer class;
2119   
2120   node = lookup_type_node_I (type);
2121   G_READ_LOCK (&type_rw_lock);
2122   if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2123     class = node->data->class.class;
2124   else
2125     class = NULL;
2126   G_READ_UNLOCK (&type_rw_lock);
2127   
2128   return class;
2129 }
2130
2131 gpointer
2132 g_type_class_peek_parent (gpointer g_class)
2133 {
2134   TypeNode *node;
2135   gpointer class = NULL;
2136   
2137   g_return_val_if_fail (g_class != NULL, NULL);
2138   
2139   node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2140   G_READ_LOCK (&type_rw_lock);
2141   if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2142     {
2143       node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2144       class = node->data->class.class;
2145     }
2146   else if (NODE_PARENT_TYPE (node))
2147     g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2148   G_READ_UNLOCK (&type_rw_lock);
2149   
2150   return class;
2151 }
2152
2153 gpointer
2154 g_type_interface_peek (gpointer instance_class,
2155                        GType    iface_type)
2156 {
2157   TypeNode *node;
2158   TypeNode *iface;
2159   gpointer vtable = NULL;
2160   GTypeClass *class = instance_class;
2161   
2162   g_return_val_if_fail (instance_class != NULL, NULL);
2163   
2164   node = lookup_type_node_I (class->g_type);
2165   iface = lookup_type_node_I (iface_type);
2166   if (node && node->is_instantiatable && iface)
2167     {
2168       IFaceEntry *entry;
2169       
2170       G_READ_LOCK (&type_rw_lock);
2171       
2172       entry = type_lookup_iface_entry_L (node, iface);
2173       if (entry && entry->vtable)       /* entry is relocatable */
2174         vtable = entry->vtable;
2175       
2176       G_READ_UNLOCK (&type_rw_lock);
2177     }
2178   else
2179     g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2180   
2181   return vtable;
2182 }
2183
2184 gpointer
2185 g_type_interface_peek_parent (gpointer g_iface)
2186 {
2187   TypeNode *node;
2188   TypeNode *iface;
2189   gpointer vtable = NULL;
2190   GTypeInterface *iface_class = g_iface;
2191   
2192   g_return_val_if_fail (g_iface != NULL, NULL);
2193   
2194   iface = lookup_type_node_I (iface_class->g_type);
2195   node = lookup_type_node_I (iface_class->g_instance_type);
2196   if (node)
2197     node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2198   if (node && node->is_instantiatable && iface)
2199     {
2200       IFaceEntry *entry;
2201       
2202       G_READ_LOCK (&type_rw_lock);
2203       
2204       entry = type_lookup_iface_entry_L (node, iface);
2205       if (entry && entry->vtable)       /* entry is relocatable */
2206         vtable = entry->vtable;
2207       
2208       G_READ_UNLOCK (&type_rw_lock);
2209     }
2210   else if (node)
2211     g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2212   
2213   return vtable;
2214 }
2215
2216 G_CONST_RETURN gchar*
2217 g_type_name (GType type)
2218 {
2219   TypeNode *node;
2220   
2221   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2222   
2223   node = lookup_type_node_I (type);
2224   
2225   return node ? NODE_NAME (node) : NULL;
2226 }
2227
2228 GQuark
2229 g_type_qname (GType type)
2230 {
2231   TypeNode *node;
2232   
2233   node = lookup_type_node_I (type);
2234   
2235   return node ? node->qname : 0;
2236 }
2237
2238 GType
2239 g_type_from_name (const gchar *name)
2240 {
2241   GType type = 0;
2242   GQuark quark;
2243   
2244   g_return_val_if_fail (name != NULL, 0);
2245   
2246   quark = g_quark_try_string (name);
2247   if (quark)
2248     {
2249       G_READ_LOCK (&type_rw_lock);
2250       type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2251       G_READ_UNLOCK (&type_rw_lock);
2252     }
2253   
2254   return type;
2255 }
2256
2257 GType
2258 g_type_parent (GType type)
2259 {
2260   TypeNode *node;
2261   
2262   node = lookup_type_node_I (type);
2263   
2264   return node ? NODE_PARENT_TYPE (node) : 0;
2265 }
2266
2267 guint
2268 g_type_depth (GType type)
2269 {
2270   TypeNode *node;
2271   
2272   node = lookup_type_node_I (type);
2273   
2274   return node ? node->n_supers + 1 : 0;
2275 }
2276
2277 GType
2278 g_type_next_base (GType type,
2279                   GType base_type)
2280 {
2281   GType atype = 0;
2282   TypeNode *node;
2283   
2284   node = lookup_type_node_I (type);
2285   if (node)
2286     {
2287       TypeNode *base_node = lookup_type_node_I (base_type);
2288       
2289       if (base_node && base_node->n_supers < node->n_supers)
2290         {
2291           guint n = node->n_supers - base_node->n_supers;
2292           
2293           if (node->supers[n] == base_type)
2294             atype = node->supers[n - 1];
2295         }
2296     }
2297   
2298   return atype;
2299 }
2300
2301 static inline gboolean
2302 type_node_check_conformities_UorL (TypeNode *node,
2303                                    TypeNode *iface_node,
2304                                    /*        support_inheritance */
2305                                    gboolean  support_interfaces,
2306                                    gboolean  support_prerequisites,
2307                                    gboolean  have_lock)
2308 {
2309   gboolean match;
2310   
2311   if (/* support_inheritance && */
2312       NODE_IS_ANCESTOR (iface_node, node))
2313     return TRUE;
2314   
2315   support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2316   support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2317   match = FALSE;
2318   if (support_interfaces || support_prerequisites)
2319     {
2320       if (!have_lock)
2321         G_READ_LOCK (&type_rw_lock);
2322       if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2323         match = TRUE;
2324       else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2325         match = TRUE;
2326       if (!have_lock)
2327         G_READ_UNLOCK (&type_rw_lock);
2328     }
2329   return match;
2330 }
2331
2332 static gboolean
2333 type_node_is_a_L (TypeNode *node,
2334                   TypeNode *iface_node)
2335 {
2336   return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2337 }
2338
2339 static inline gboolean
2340 type_node_conforms_to_U (TypeNode *node,
2341                          TypeNode *iface_node,
2342                          gboolean  support_interfaces,
2343                          gboolean  support_prerequisites)
2344 {
2345   return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2346 }
2347
2348 gboolean
2349 g_type_is_a (GType type,
2350              GType iface_type)
2351 {
2352   TypeNode *node, *iface_node;
2353   gboolean is_a;
2354   
2355   node = lookup_type_node_I (type);
2356   iface_node = lookup_type_node_I (iface_type);
2357   is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2358   
2359   return is_a;
2360 }
2361
2362 GType* /* free result */
2363 g_type_children (GType  type,
2364                  guint *n_children)
2365 {
2366   TypeNode *node;
2367   
2368   node = lookup_type_node_I (type);
2369   if (node)
2370     {
2371       GType *children;
2372       
2373       G_READ_LOCK (&type_rw_lock);      /* ->children is relocatable */
2374       children = g_new (GType, node->n_children + 1);
2375       memcpy (children, node->children, sizeof (GType) * node->n_children);
2376       children[node->n_children] = 0;
2377       
2378       if (n_children)
2379         *n_children = node->n_children;
2380       G_READ_UNLOCK (&type_rw_lock);
2381       
2382       return children;
2383     }
2384   else
2385     {
2386       if (n_children)
2387         *n_children = 0;
2388       
2389       return NULL;
2390     }
2391 }
2392
2393 GType* /* free result */
2394 g_type_interfaces (GType  type,
2395                    guint *n_interfaces)
2396 {
2397   TypeNode *node;
2398   
2399   node = lookup_type_node_I (type);
2400   if (node && node->is_instantiatable)
2401     {
2402       GType *ifaces;
2403       guint i;
2404       
2405       G_READ_LOCK (&type_rw_lock);
2406       ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2407       for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2408         ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2409       ifaces[i] = 0;
2410       
2411       if (n_interfaces)
2412         *n_interfaces = CLASSED_NODE_N_IFACES (node);
2413       G_READ_UNLOCK (&type_rw_lock);
2414       
2415       return ifaces;
2416     }
2417   else
2418     {
2419       if (n_interfaces)
2420         *n_interfaces = 0;
2421       
2422       return NULL;
2423     }
2424 }
2425
2426 typedef struct _QData QData;
2427 struct _GData
2428 {
2429   guint  n_qdatas;
2430   QData *qdatas;
2431 };
2432 struct _QData
2433 {
2434   GQuark   quark;
2435   gpointer data;
2436 };
2437
2438 static inline gpointer
2439 type_get_qdata_L (TypeNode *node,
2440                   GQuark    quark)
2441 {
2442   GData *gdata = node->global_gdata;
2443   
2444   if (quark && gdata && gdata->n_qdatas)
2445     {
2446       QData *qdatas = gdata->qdatas - 1;
2447       guint n_qdatas = gdata->n_qdatas;
2448       
2449       do
2450         {
2451           guint i;
2452           QData *check;
2453           
2454           i = (n_qdatas + 1) / 2;
2455           check = qdatas + i;
2456           if (quark == check->quark)
2457             return check->data;
2458           else if (quark > check->quark)
2459             {
2460               n_qdatas -= i;
2461               qdatas = check;
2462             }
2463           else /* if (quark < check->quark) */
2464             n_qdatas = i - 1;
2465         }
2466       while (n_qdatas);
2467     }
2468   return NULL;
2469 }
2470
2471 gpointer
2472 g_type_get_qdata (GType  type,
2473                   GQuark quark)
2474 {
2475   TypeNode *node;
2476   gpointer data;
2477   
2478   node = lookup_type_node_I (type);
2479   if (node)
2480     {
2481       G_READ_LOCK (&type_rw_lock);
2482       data = type_get_qdata_L (node, quark);
2483       G_READ_UNLOCK (&type_rw_lock);
2484     }
2485   else
2486     {
2487       g_return_val_if_fail (node != NULL, NULL);
2488       data = NULL;
2489     }
2490   return data;
2491 }
2492
2493 static inline void
2494 type_set_qdata_W (TypeNode *node,
2495                   GQuark    quark,
2496                   gpointer  data)
2497 {
2498   GData *gdata;
2499   QData *qdata;
2500   guint i;
2501   
2502   /* setup qdata list if necessary */
2503   if (!node->global_gdata)
2504     node->global_gdata = g_new0 (GData, 1);
2505   gdata = node->global_gdata;
2506   
2507   /* try resetting old data */
2508   qdata = gdata->qdatas;
2509   for (i = 0; i < gdata->n_qdatas; i++)
2510     if (qdata[i].quark == quark)
2511       {
2512         qdata[i].data = data;
2513         return;
2514       }
2515   
2516   /* add new entry */
2517   gdata->n_qdatas++;
2518   gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2519   qdata = gdata->qdatas;
2520   for (i = 0; i < gdata->n_qdatas - 1; i++)
2521     if (qdata[i].quark > quark)
2522       break;
2523   g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2524   qdata[i].quark = quark;
2525   qdata[i].data = data;
2526 }
2527
2528 void
2529 g_type_set_qdata (GType    type,
2530                   GQuark   quark,
2531                   gpointer data)
2532 {
2533   TypeNode *node;
2534   
2535   g_return_if_fail (quark != 0);
2536   
2537   node = lookup_type_node_I (type);
2538   if (node)
2539     {
2540       G_WRITE_LOCK (&type_rw_lock);
2541       type_set_qdata_W (node, quark, data);
2542       G_WRITE_UNLOCK (&type_rw_lock);
2543     }
2544   else
2545     g_return_if_fail (node != NULL);
2546 }
2547
2548 static void
2549 type_add_flags_W (TypeNode  *node,
2550                   GTypeFlags flags)
2551 {
2552   guint dflags;
2553   
2554   g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2555   g_return_if_fail (node != NULL);
2556   
2557   if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2558     g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2559   dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2560   dflags |= flags;
2561   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2562 }
2563
2564 void
2565 g_type_query (GType       type,
2566               GTypeQuery *query)
2567 {
2568   TypeNode *node;
2569   
2570   g_return_if_fail (query != NULL);
2571   
2572   /* if node is not static and classed, we won't allow query */
2573   query->type = 0;
2574   node = lookup_type_node_I (type);
2575   if (node && node->is_classed && !node->plugin)
2576     {
2577       /* type is classed and probably even instantiatable */
2578       G_READ_LOCK (&type_rw_lock);
2579       if (node->data)   /* type is static or referenced */
2580         {
2581           query->type = NODE_TYPE (node);
2582           query->type_name = NODE_NAME (node);
2583           query->class_size = node->data->class.class_size;
2584           query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2585         }
2586       G_READ_UNLOCK (&type_rw_lock);
2587     }
2588 }
2589
2590
2591 /* --- implementation details --- */
2592 gboolean
2593 g_type_test_flags (GType type,
2594                    guint flags)
2595 {
2596   TypeNode *node;
2597   gboolean result = FALSE;
2598   
2599   node = lookup_type_node_I (type);
2600   if (node)
2601     {
2602       guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2603       guint tflags = flags & TYPE_FLAG_MASK;
2604       
2605       if (fflags)
2606         {
2607           GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2608           
2609           fflags = (finfo->type_flags & fflags) == fflags;
2610         }
2611       else
2612         fflags = TRUE;
2613       
2614       if (tflags)
2615         {
2616           G_READ_LOCK (&type_rw_lock);
2617           tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2618           G_READ_UNLOCK (&type_rw_lock);
2619         }
2620       else
2621         tflags = TRUE;
2622       
2623       result = tflags && fflags;
2624     }
2625   
2626   return result;
2627 }
2628
2629 GTypePlugin*
2630 g_type_get_plugin (GType type)
2631 {
2632   TypeNode *node;
2633   
2634   node = lookup_type_node_I (type);
2635   
2636   return node ? node->plugin : NULL;
2637 }
2638
2639 GTypePlugin*
2640 g_type_interface_get_plugin (GType instance_type,
2641                              GType interface_type)
2642 {
2643   TypeNode *node;
2644   TypeNode *iface;
2645   
2646   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);    /* G_TYPE_IS_INTERFACE() is an external call: _U */
2647   
2648   node = lookup_type_node_I (instance_type);  
2649   iface = lookup_type_node_I (interface_type);
2650   if (node && iface)
2651     {
2652       IFaceHolder *iholder;
2653       GTypePlugin *plugin;
2654       
2655       G_READ_LOCK (&type_rw_lock);
2656       
2657       iholder = iface_node_get_holders_L (iface);
2658       while (iholder && iholder->instance_type != instance_type)
2659         iholder = iholder->next;
2660       plugin = iholder ? iholder->plugin : NULL;
2661       
2662       G_READ_UNLOCK (&type_rw_lock);
2663       
2664       return plugin;
2665     }
2666   
2667   g_return_val_if_fail (node == NULL, NULL);
2668   g_return_val_if_fail (iface == NULL, NULL);
2669   
2670   g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2671   
2672   return NULL;
2673 }
2674
2675 GType
2676 g_type_fundamental_next (void)
2677 {
2678   GType type;
2679   
2680   G_READ_LOCK (&type_rw_lock);
2681   type = static_fundamental_next;
2682   G_READ_UNLOCK (&type_rw_lock);
2683   type = G_TYPE_MAKE_FUNDAMENTAL (type);
2684   return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2685 }
2686
2687 GType
2688 g_type_fundamental (GType type_id)
2689 {
2690   TypeNode *node = lookup_type_node_I (type_id);
2691   
2692   return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2693 }
2694
2695 gboolean
2696 g_type_check_instance_is_a (GTypeInstance *type_instance,
2697                             GType          iface_type)
2698 {
2699   TypeNode *node, *iface;
2700   gboolean check;
2701   
2702   if (!type_instance || !type_instance->g_class)
2703     return FALSE;
2704   
2705   node = lookup_type_node_I (type_instance->g_class->g_type);
2706   iface = lookup_type_node_I (iface_type);
2707   check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2708   
2709   return check;
2710 }
2711
2712 gboolean
2713 g_type_check_class_is_a (GTypeClass *type_class,
2714                          GType       is_a_type)
2715 {
2716   TypeNode *node, *iface;
2717   gboolean check;
2718   
2719   if (!type_class)
2720     return FALSE;
2721   
2722   node = lookup_type_node_I (type_class->g_type);
2723   iface = lookup_type_node_I (is_a_type);
2724   check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2725   
2726   return check;
2727 }
2728
2729 GTypeInstance*
2730 g_type_check_instance_cast (GTypeInstance *type_instance,
2731                             GType          iface_type)
2732 {
2733   if (type_instance)
2734     {
2735       if (type_instance->g_class)
2736         {
2737           TypeNode *node, *iface;
2738           gboolean is_instantiatable, check;
2739           
2740           node = lookup_type_node_I (type_instance->g_class->g_type);
2741           is_instantiatable = node && node->is_instantiatable;
2742           iface = lookup_type_node_I (iface_type);
2743           check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2744           if (check)
2745             return type_instance;
2746           
2747           if (is_instantiatable)
2748             g_warning ("invalid cast from `%s' to `%s'",
2749                        type_descriptive_name_I (type_instance->g_class->g_type),
2750                        type_descriptive_name_I (iface_type));
2751           else
2752             g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2753                        type_descriptive_name_I (type_instance->g_class->g_type),
2754                        type_descriptive_name_I (iface_type));
2755         }
2756       else
2757         g_warning ("invalid unclassed pointer in cast to `%s'",
2758                    type_descriptive_name_I (iface_type));
2759     }
2760   
2761   return type_instance;
2762 }
2763
2764 GTypeClass*
2765 g_type_check_class_cast (GTypeClass *type_class,
2766                          GType       is_a_type)
2767 {
2768   if (type_class)
2769     {
2770       TypeNode *node, *iface;
2771       gboolean is_classed, check;
2772       
2773       node = lookup_type_node_I (type_class->g_type);
2774       is_classed = node && node->is_classed;
2775       iface = lookup_type_node_I (is_a_type);
2776       check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2777       if (check)
2778         return type_class;
2779       
2780       if (is_classed)
2781         g_warning ("invalid class cast from `%s' to `%s'",
2782                    type_descriptive_name_I (type_class->g_type),
2783                    type_descriptive_name_I (is_a_type));
2784       else
2785         g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2786                    type_descriptive_name_I (type_class->g_type),
2787                    type_descriptive_name_I (is_a_type));
2788     }
2789   else
2790     g_warning ("invalid class cast from (NULL) pointer to `%s'",
2791                type_descriptive_name_I (is_a_type));
2792   return type_class;
2793 }
2794
2795 gboolean
2796 g_type_check_instance (GTypeInstance *type_instance)
2797 {
2798   /* this function is just here to make the signal system
2799    * conveniently elaborated on instance checks
2800    */
2801   if (type_instance)
2802     {
2803       if (type_instance->g_class)
2804         {
2805           TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2806           
2807           if (node && node->is_instantiatable)
2808             return TRUE;
2809           
2810           g_warning ("instance of invalid non-instantiatable type `%s'",
2811                      type_descriptive_name_I (type_instance->g_class->g_type));
2812         }
2813       else
2814         g_warning ("instance with invalid (NULL) class pointer");
2815     }
2816   else
2817     g_warning ("invalid (NULL) pointer instance");
2818   
2819   return FALSE;
2820 }
2821
2822 static inline gboolean
2823 type_check_is_value_type_U (GType type)
2824 {
2825   GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2826   TypeNode *node;
2827   
2828   /* common path speed up */
2829   node = lookup_type_node_I (type);
2830   if (node && node->mutatable_check_cache)
2831     return TRUE;
2832   
2833   G_READ_LOCK (&type_rw_lock);
2834  restart_check:
2835   if (node)
2836     {
2837       if (node->data && node->data->common.ref_count > 0 &&
2838           node->data->common.value_table->value_init)
2839         tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2840       else if (NODE_IS_IFACE (node))
2841         {
2842           guint i;
2843           
2844           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2845             {
2846               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2847               TypeNode *prnode = lookup_type_node_I (prtype);
2848               
2849               if (prnode->is_instantiatable)
2850                 {
2851                   type = prtype;
2852                   node = lookup_type_node_I (type);
2853                   goto restart_check;
2854                 }
2855             }
2856         }
2857     }
2858   G_READ_UNLOCK (&type_rw_lock);
2859   
2860   return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2861 }
2862
2863 gboolean
2864 g_type_check_is_value_type (GType type)
2865 {
2866   return type_check_is_value_type_U (type);
2867 }
2868
2869 gboolean
2870 g_type_check_value (GValue *value)
2871 {
2872   return value && type_check_is_value_type_U (value->g_type);
2873 }
2874
2875 gboolean
2876 g_type_check_value_holds (GValue *value,
2877                           GType   type)
2878 {
2879   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2880 }
2881
2882 GTypeValueTable*
2883 g_type_value_table_peek (GType type)
2884 {
2885   GTypeValueTable *vtable = NULL;
2886   TypeNode *node = lookup_type_node_I (type);
2887   gboolean has_refed_data, has_table;
2888   TypeData *data;
2889
2890   /* speed up common code path, we're not 100% safe here,
2891    * but we should only get called with referenced types anyway
2892    */
2893   data = node ? node->data : NULL;
2894   if (node && node->mutatable_check_cache)
2895     return data->common.value_table;
2896
2897   G_READ_LOCK (&type_rw_lock);
2898   
2899  restart_table_peek:
2900   has_refed_data = node && node->data && node->data->common.ref_count;
2901   has_table = has_refed_data && node->data->common.value_table->value_init;
2902   if (has_refed_data)
2903     {
2904       if (has_table)
2905         vtable = node->data->common.value_table;
2906       else if (NODE_IS_IFACE (node))
2907         {
2908           guint i;
2909           
2910           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2911             {
2912               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2913               TypeNode *prnode = lookup_type_node_I (prtype);
2914               
2915               if (prnode->is_instantiatable)
2916                 {
2917                   type = prtype;
2918                   node = lookup_type_node_I (type);
2919                   goto restart_table_peek;
2920                 }
2921             }
2922         }
2923     }
2924   
2925   G_READ_UNLOCK (&type_rw_lock);
2926   
2927   if (vtable)
2928     return vtable;
2929   
2930   if (!node)
2931     g_warning (G_STRLOC ": type id `%lu' is invalid", type);
2932   if (!has_refed_data)
2933     g_warning ("can't peek value table for type `%s' which is not currently referenced",
2934                type_descriptive_name_I (type));
2935   
2936   return NULL;
2937 }
2938
2939 G_CONST_RETURN gchar*
2940 g_type_name_from_instance (GTypeInstance *instance)
2941 {
2942   if (!instance)
2943     return "<NULL-instance>";
2944   else
2945     return g_type_name_from_class (instance->g_class);
2946 }
2947
2948 G_CONST_RETURN gchar*
2949 g_type_name_from_class (GTypeClass *g_class)
2950 {
2951   if (!g_class)
2952     return "<NULL-class>";
2953   else
2954     return g_type_name (g_class->g_type);
2955 }
2956
2957
2958 /* --- foreign prototypes --- */
2959 extern void     g_value_c_init          (void); /* sync with gvalue.c */
2960 extern void     g_value_types_init      (void); /* sync with gvaluetypes.c */
2961 extern void     g_enum_types_init       (void); /* sync with genums.c */
2962 extern void     g_param_type_init       (void); /* sync with gparam.c */
2963 extern void     g_boxed_type_init       (void); /* sync with gboxed.c */
2964 extern void     g_object_type_init      (void); /* sync with gobject.c */
2965 extern void     g_param_spec_types_init (void); /* sync with gparamspecs.c */
2966 extern void     g_value_transforms_init (void); /* sync with gvaluetransform.c */
2967 extern void     g_signal_init           (void); /* sync with gsignal.c */
2968
2969
2970 /* --- initialization --- */
2971 void
2972 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2973 {
2974   G_LOCK_DEFINE_STATIC (type_init_lock);
2975   const gchar *env_string;
2976   GTypeInfo info;
2977   TypeNode *node;
2978   GType type;
2979   
2980   G_LOCK (type_init_lock);
2981   
2982   G_WRITE_LOCK (&type_rw_lock);
2983   
2984   if (static_quark_type_flags)
2985     {
2986       G_WRITE_UNLOCK (&type_rw_lock);
2987       G_UNLOCK (type_init_lock);
2988       return;
2989     }
2990   
2991   /* setup GObject library wide debugging flags */
2992   _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2993   env_string = g_getenv ("GOBJECT_DEBUG");
2994   if (env_string != NULL)
2995     {
2996       static GDebugKey debug_keys[] = {
2997         { "objects", G_TYPE_DEBUG_OBJECTS },
2998         { "signals", G_TYPE_DEBUG_SIGNALS },
2999       };
3000       
3001       _g_type_debug_flags |= g_parse_debug_string (env_string,
3002                                                    debug_keys,
3003                                                    sizeof (debug_keys) / sizeof (debug_keys[0]));
3004       env_string = NULL;
3005     }
3006   
3007   /* quarks */
3008   static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3009   static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3010   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3011   
3012   /* type qname hash table */
3013   static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3014   
3015   /* invalid type G_TYPE_INVALID (0)
3016    */
3017   static_fundamental_type_nodes[0] = NULL;
3018   
3019   /* void type G_TYPE_NONE
3020    */
3021   node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
3022   type = NODE_TYPE (node);
3023   g_assert (type == G_TYPE_NONE);
3024   
3025   /* interface fundamental type G_TYPE_INTERFACE (!classed)
3026    */
3027   memset (&info, 0, sizeof (info));
3028   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
3029   type = NODE_TYPE (node);
3030   type_data_make_W (node, &info, NULL);
3031   g_assert (type == G_TYPE_INTERFACE);
3032   
3033   G_WRITE_UNLOCK (&type_rw_lock);
3034   
3035   g_value_c_init ();
3036
3037   /* G_TYPE_TYPE_PLUGIN
3038    */
3039   g_type_plugin_get_type ();
3040   
3041   /* G_TYPE_* value types
3042    */
3043   g_value_types_init ();
3044   
3045   /* G_TYPE_ENUM & G_TYPE_FLAGS
3046    */
3047   g_enum_types_init ();
3048   
3049   /* G_TYPE_BOXED
3050    */
3051   g_boxed_type_init ();
3052   
3053   /* G_TYPE_PARAM
3054    */
3055   g_param_type_init ();
3056   
3057   /* G_TYPE_OBJECT
3058    */
3059   g_object_type_init ();
3060   
3061   /* G_TYPE_PARAM_* pspec types
3062    */
3063   g_param_spec_types_init ();
3064   
3065   /* Value Transformations
3066    */
3067   g_value_transforms_init ();
3068   
3069   /* Signal system
3070    */
3071   g_signal_init ();
3072   
3073   G_UNLOCK (type_init_lock);
3074 }
3075
3076 void 
3077 g_type_init (void)
3078 {
3079   g_type_init_with_debug_flags (0);
3080 }
3081
3082 void
3083 g_type_class_add_private (gpointer g_class,
3084                           gsize    private_size)
3085 {
3086   GType instance_type = ((GTypeClass *)g_class)->g_type;
3087   TypeNode *node = lookup_type_node_I (instance_type);
3088   gsize offset;
3089
3090   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3091     {
3092       g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3093                  type_descriptive_name_I (instance_type));
3094       return;
3095     }
3096
3097   if (NODE_PARENT_TYPE (node))
3098     {
3099       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3100       if (node->data->instance.private_size != pnode->data->instance.private_size)
3101         {
3102           g_warning ("g_type_add_private() called multiple times for the same type");
3103           return;
3104         }
3105     }
3106   
3107   G_WRITE_LOCK (&type_rw_lock);
3108
3109   offset = ALIGN_STRUCT (node->data->instance.private_size);
3110   node->data->instance.private_size = offset + private_size;
3111   
3112   G_WRITE_UNLOCK (&type_rw_lock);
3113 }
3114
3115 gpointer
3116 g_type_instance_get_private (GTypeInstance *instance,
3117                              GType          private_type)
3118 {
3119   TypeNode *instance_node;
3120   TypeNode *private_node;
3121   TypeNode *parent_node;
3122   gsize offset;
3123
3124   g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3125   
3126   instance_node = lookup_type_node_I (instance->g_class->g_type);
3127   if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3128     {
3129       g_warning ("instance of invalid non-instantiatable type `%s'",
3130                  type_descriptive_name_I (instance->g_class->g_type));
3131       return NULL;
3132     }
3133
3134   private_node = lookup_type_node_I (private_type);
3135   if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3136     {
3137       g_warning ("attempt to retrieve private data for invalid type '%s'",
3138                  type_descriptive_name_I (private_type));
3139       return NULL;
3140     }
3141
3142   /* Note that we don't need a read lock, since instance existing
3143    * means that the instance class and all parent classes
3144    * exist, so the node->data, node->data->instance.instance_size,
3145    * and node->data->instance.private_size are not going to be changed.
3146    * for any of the relevant types.
3147    */
3148
3149   offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3150
3151   if (NODE_PARENT_TYPE (private_node))
3152     {
3153       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3154       g_assert (parent_node->data && parent_node->data->common.ref_count);
3155
3156       offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3157     }
3158
3159   return G_STRUCT_MEMBER_P (instance, offset);
3160 }