Optimize the common cases (init == NULL or init == "") a bit. replace uses
[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       if (NODE_PARENT_TYPE (node))
930         {
931           TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
932           data->instance.private_size = pnode->data->instance.private_size;
933         }
934       else
935         data->instance.private_size = 0;
936 #ifdef  DISABLE_MEM_POOLS
937       data->instance.n_preallocs = 0;
938 #else   /* !DISABLE_MEM_POOLS */
939       data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
940 #endif  /* !DISABLE_MEM_POOLS */
941       data->instance.instance_init = info->instance_init;
942       data->instance.mem_chunk = NULL;
943     }
944   else if (node->is_classed) /* only classed */
945     {
946       data = g_malloc0 (sizeof (ClassData) + vtable_size);
947       if (vtable_size)
948         vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
949       data->class.class_size = info->class_size;
950       data->class.class_init_base = info->base_init;
951       data->class.class_finalize_base = info->base_finalize;
952       data->class.class_init = info->class_init;
953       data->class.class_finalize = info->class_finalize;
954       data->class.class_data = info->class_data;
955       data->class.class = NULL;
956     }
957   else if (NODE_IS_IFACE (node))
958     {
959       data = g_malloc0 (sizeof (IFaceData) + vtable_size);
960       if (vtable_size)
961         vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
962       data->iface.vtable_size = info->class_size;
963       data->iface.vtable_init_base = info->base_init;
964       data->iface.vtable_finalize_base = info->base_finalize;
965     }
966   else
967     {
968       data = g_malloc0 (sizeof (CommonData) + vtable_size);
969       if (vtable_size)
970         vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
971     }
972   
973   node->data = data;
974   node->data->common.ref_count = 1;
975   
976   if (vtable_size)
977     {
978       gchar *p;
979       
980       /* we allocate the vtable and its strings together with the type data, so
981        * children can take over their parent's vtable pointer, and we don't
982        * need to worry freeing it or not when the child data is destroyed
983        */
984       *vtable = *value_table;
985       p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
986       p[0] = 0;
987       vtable->collect_format = p;
988       if (value_table->collect_format)
989         {
990           strcat (p, value_table->collect_format);
991           p += strlen (value_table->collect_format);
992         }
993       p++;
994       p[0] = 0;
995       vtable->lcopy_format = p;
996       if (value_table->lcopy_format)
997         strcat  (p, value_table->lcopy_format);
998     }
999   node->data->common.value_table = vtable;
1000   node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1001                                  !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1002                                    GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1003   
1004   g_assert (node->data->common.value_table != NULL); /* paranoid */
1005 }
1006
1007 static inline void
1008 type_data_ref_Wm (TypeNode *node)
1009 {
1010   if (!node->data)
1011     {
1012       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1013       GTypeInfo tmp_info;
1014       GTypeValueTable tmp_value_table;
1015       
1016       g_assert (node->plugin != NULL);
1017       
1018       if (pnode)
1019         {
1020           type_data_ref_Wm (pnode);
1021           if (node->data)
1022             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1023         }
1024       
1025       memset (&tmp_info, 0, sizeof (tmp_info));
1026       memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1027       
1028       G_WRITE_UNLOCK (&type_rw_lock);
1029       g_type_plugin_use (node->plugin);
1030       g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1031       G_WRITE_LOCK (&type_rw_lock);
1032       if (node->data)
1033         INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1034       
1035       check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1036       type_data_make_W (node, &tmp_info,
1037                         check_value_table_I (NODE_NAME (node),
1038                                              &tmp_value_table) ? &tmp_value_table : NULL);
1039     }
1040   else
1041     {
1042       g_assert (node->data->common.ref_count > 0);
1043       
1044       node->data->common.ref_count += 1;
1045     }
1046 }
1047
1048 static inline void
1049 type_data_unref_Wm (TypeNode *node,
1050                     gboolean  uncached)
1051 {
1052   g_assert (node->data && node->data->common.ref_count);
1053   
1054   if (node->data->common.ref_count > 1)
1055     node->data->common.ref_count -= 1;
1056   else
1057     {
1058       if (!node->plugin)
1059         {
1060           g_warning ("static type `%s' unreferenced too often",
1061                      NODE_NAME (node));
1062           return;
1063         }
1064       
1065       type_data_last_unref_Wm (NODE_TYPE (node), uncached);
1066     }
1067 }
1068
1069 static void
1070 type_node_add_iface_entry_W (TypeNode *node,
1071                              GType     iface_type)
1072 {
1073   IFaceEntry *entries;
1074   guint i;
1075   
1076   g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
1077   
1078   entries = CLASSED_NODE_IFACES_ENTRIES (node);
1079   for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1080     if (entries[i].iface_type == iface_type)
1081       {
1082         /* this can (should) only happen if our parent type already conformed
1083          * to iface_type and node got it's own holder info. here, our
1084          * children should already have entries with NULL vtables, so
1085          * we're actually done.
1086          */
1087         g_assert (entries[i].vtable == NULL);
1088         return;
1089       }
1090     else if (entries[i].iface_type > iface_type)
1091       break;
1092   CLASSED_NODE_N_IFACES (node) += 1;
1093   CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
1094                                                 CLASSED_NODE_IFACES_ENTRIES (node),
1095                                                 CLASSED_NODE_N_IFACES (node));
1096   entries = CLASSED_NODE_IFACES_ENTRIES (node);
1097   g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
1098   entries[i].iface_type = iface_type;
1099   entries[i].vtable = NULL;
1100   
1101   for (i = 0; i < node->n_children; i++)
1102     type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type);
1103 }
1104
1105 static void
1106 type_add_interface_W (TypeNode             *node,
1107                       TypeNode             *iface,
1108                       const GInterfaceInfo *info,
1109                       GTypePlugin          *plugin)
1110 {
1111   IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1112   
1113   /* we must not call any functions of GInterfaceInfo from within here, since
1114    * we got most probably called from _within_ a type registration function
1115    */
1116   g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1117   
1118   iholder->next = iface_node_get_holders_L (iface);
1119   iface_node_set_holders_W (iface, iholder);
1120   iholder->instance_type = NODE_TYPE (node);
1121   iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1122   iholder->plugin = plugin;
1123   
1124   type_node_add_iface_entry_W (node, NODE_TYPE (iface));
1125 }
1126
1127 static void
1128 type_iface_add_prerequisite_W (TypeNode *iface,
1129                                TypeNode *prerequisite_node)
1130 {
1131   GType prerequisite_type = NODE_TYPE (prerequisite_node);
1132   GType *prerequisites, *dependants;
1133   guint n_dependants, i;
1134   
1135   g_assert (NODE_IS_IFACE (iface) &&
1136             IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1137             (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1138   
1139   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1140   for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1141     if (prerequisites[i] == prerequisite_type)
1142       return;                   /* we already have that prerequisiste */
1143     else if (prerequisites[i] > prerequisite_type)
1144       break;
1145   IFACE_NODE_N_PREREQUISITES (iface) += 1;
1146   IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1147                                               IFACE_NODE_PREREQUISITES (iface),
1148                                               IFACE_NODE_N_PREREQUISITES (iface));
1149   prerequisites = IFACE_NODE_PREREQUISITES (iface);
1150   g_memmove (prerequisites + i + 1, prerequisites + i,
1151              sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1152   prerequisites[i] = prerequisite_type;
1153   
1154   /* we want to get notified when prerequisites get added to prerequisite_node */
1155   if (NODE_IS_IFACE (prerequisite_node))
1156     {
1157       dependants = iface_node_get_dependants_array_L (prerequisite_node);
1158       n_dependants = dependants ? dependants[0] : 0;
1159       n_dependants += 1;
1160       dependants = g_renew (GType, dependants, n_dependants + 1);
1161       dependants[n_dependants] = NODE_TYPE (iface);
1162       dependants[0] = n_dependants;
1163       iface_node_set_dependants_array_W (prerequisite_node, dependants);
1164     }
1165   
1166   /* we need to notify all dependants */
1167   dependants = iface_node_get_dependants_array_L (iface);
1168   n_dependants = dependants ? dependants[0] : 0;
1169   for (i = 1; i <= n_dependants; i++)
1170     type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1171 }
1172
1173 void
1174 g_type_interface_add_prerequisite (GType interface_type,
1175                                    GType prerequisite_type)
1176 {
1177   TypeNode *iface, *prerequisite_node;
1178   IFaceHolder *holders;
1179   
1180   g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type));      /* G_TYPE_IS_INTERFACE() is an external call: _U */
1181   g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1182   g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1183   
1184   iface = lookup_type_node_I (interface_type);
1185   prerequisite_node = lookup_type_node_I (prerequisite_type);
1186   if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1187     {
1188       g_warning ("interface type `%s' or prerequisite type `%s' invalid",
1189                  type_descriptive_name_I (interface_type),
1190                  type_descriptive_name_I (prerequisite_type));
1191       return;
1192     }
1193   G_WRITE_LOCK (&type_rw_lock);
1194   holders = iface_node_get_holders_L (iface);
1195   if (holders)
1196     {
1197       G_WRITE_UNLOCK (&type_rw_lock);
1198       g_warning ("unable to add prerequisite `%s' to interface `%s' which is already in use for `%s'",
1199                  type_descriptive_name_I (prerequisite_type),
1200                  type_descriptive_name_I (interface_type),
1201                  type_descriptive_name_I (holders->instance_type));
1202       return;
1203     }
1204   if (prerequisite_node->is_instantiatable)
1205     {
1206       guint i;
1207       
1208       /* can have at most one publically installable instantiatable prerequisite */
1209       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1210         {
1211           TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1212           
1213           if (prnode->is_instantiatable)
1214             {
1215               G_WRITE_UNLOCK (&type_rw_lock);
1216               g_warning ("adding prerequisite `%s' to interface `%s' conflicts with existing prerequisite `%s'",
1217                          type_descriptive_name_I (prerequisite_type),
1218                          type_descriptive_name_I (interface_type),
1219                          type_descriptive_name_I (NODE_TYPE (prnode)));
1220               return;
1221             }
1222         }
1223       
1224       for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1225         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1226       G_WRITE_UNLOCK (&type_rw_lock);
1227     }
1228   else if (NODE_IS_IFACE (prerequisite_node))
1229     {
1230       GType *prerequisites;
1231       guint i;
1232       
1233       prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1234       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1235         type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1236       type_iface_add_prerequisite_W (iface, prerequisite_node);
1237       G_WRITE_UNLOCK (&type_rw_lock);
1238     }
1239   else
1240     {
1241       G_WRITE_UNLOCK (&type_rw_lock);
1242       g_warning ("prerequisite `%s' for interface `%s' is neither instantiatable nor interface",
1243                  type_descriptive_name_I (prerequisite_type),
1244                  type_descriptive_name_I (interface_type));
1245     }
1246 }
1247
1248 GType* /* free result */
1249 g_type_interface_prerequisites (GType  interface_type,
1250                                 guint *n_prerequisites)
1251 {
1252   TypeNode *iface;
1253   
1254   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1255
1256   iface = lookup_type_node_I (interface_type);
1257   if (iface)
1258     {
1259       GType *types;
1260       TypeNode *inode = NULL;
1261       guint i, n = 0;
1262       
1263       G_READ_LOCK (&type_rw_lock);
1264       types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1265       for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1266         {
1267           GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1268           TypeNode *node = lookup_type_node_I (prerequisite);
1269           if (node->is_instantiatable &&
1270               (!inode || type_node_is_a_L (node, inode)))
1271             inode = node;
1272           else
1273             types[n++] = NODE_TYPE (node);
1274         }
1275       if (inode)
1276         types[n++] = NODE_TYPE (inode);
1277       
1278       if (n_prerequisites)
1279         *n_prerequisites = n;
1280       G_READ_UNLOCK (&type_rw_lock);
1281       
1282       return types;
1283     }
1284   else
1285     {
1286       if (n_prerequisites)
1287         *n_prerequisites = 0;
1288       
1289       return NULL;
1290     }
1291 }
1292
1293
1294 static IFaceHolder*
1295 type_iface_peek_holder_L (TypeNode *iface,
1296                           GType     instance_type)
1297 {
1298   IFaceHolder *iholder;
1299   
1300   g_assert (NODE_IS_IFACE (iface));
1301   
1302   iholder = iface_node_get_holders_L (iface);
1303   while (iholder && iholder->instance_type != instance_type)
1304     iholder = iholder->next;
1305   return iholder;
1306 }
1307
1308 static IFaceHolder*
1309 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1310                                     GType     instance_type,
1311                                     gboolean  need_info)
1312 {
1313   IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1314   
1315   if (iholder && !iholder->info && need_info)
1316     {
1317       GInterfaceInfo tmp_info;
1318       
1319       g_assert (iholder->plugin != NULL);
1320       
1321       type_data_ref_Wm (iface);
1322       if (iholder->info)
1323         INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1324       
1325       memset (&tmp_info, 0, sizeof (tmp_info));
1326       
1327       G_WRITE_UNLOCK (&type_rw_lock);
1328       g_type_plugin_use (iholder->plugin);
1329       g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1330       G_WRITE_LOCK (&type_rw_lock);
1331       if (iholder->info)
1332         INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1333       
1334       check_interface_info_I (iface, instance_type, &tmp_info);
1335       iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1336     }
1337   
1338   return iholder;       /* we don't modify write lock upon returning NULL */
1339 }
1340
1341 static void
1342 type_iface_blow_holder_info_Wm (TypeNode *iface,
1343                                 GType     instance_type)
1344 {
1345   IFaceHolder *iholder = iface_node_get_holders_L (iface);
1346   
1347   g_assert (NODE_IS_IFACE (iface));
1348   
1349   while (iholder->instance_type != instance_type)
1350     iholder = iholder->next;
1351   
1352   if (iholder->info && iholder->plugin)
1353     {
1354       g_free (iholder->info);
1355       iholder->info = NULL;
1356       
1357       G_WRITE_UNLOCK (&type_rw_lock);
1358       g_type_plugin_unuse (iholder->plugin);
1359       G_WRITE_LOCK (&type_rw_lock);
1360       
1361       type_data_unref_Wm (iface, FALSE);
1362     }
1363 }
1364
1365 /* Assumes type's class already exists
1366  */
1367 static inline size_t
1368 type_total_instance_size_I (TypeNode *node)
1369 {
1370   gsize total_instance_size;
1371
1372   total_instance_size = node->data->instance.instance_size;
1373   if (node->data->instance.private_size != 0)
1374     total_instance_size = ALIGN_STRUCT (total_instance_size) + node->data->instance.private_size;
1375
1376   return total_instance_size;
1377 }
1378
1379 /* --- type structure creation/destruction --- */
1380 GTypeInstance*
1381 g_type_create_instance (GType type)
1382 {
1383   TypeNode *node;
1384   GTypeInstance *instance;
1385   GTypeClass *class;
1386   guint i;
1387   gsize total_instance_size;
1388   
1389   node = lookup_type_node_I (type);
1390   if (!node || !node->is_instantiatable)
1391     {
1392       g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
1393                  type_descriptive_name_I (type));
1394       return NULL;
1395     }
1396   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1397   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1398     {
1399       g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
1400                  type_descriptive_name_I (type));
1401       return NULL;
1402     }
1403   
1404   class = g_type_class_ref (type);
1405
1406   total_instance_size = type_total_instance_size_I (node);
1407   
1408   if (node->data->instance.n_preallocs)
1409     {
1410       G_WRITE_LOCK (&type_rw_lock);
1411       if (!node->data->instance.mem_chunk)
1412         {
1413           /* If there isn't private data, the compiler will have already
1414            * added the necessary padding, but in the private data case, we
1415            * have to pad ourselves to ensure proper alignment of all the
1416            * atoms in the slab.
1417            */
1418           gsize atom_size = total_instance_size;
1419           if (node->data->instance.private_size)
1420             atom_size = ALIGN_STRUCT (atom_size);
1421           
1422           node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
1423                                                             atom_size,
1424                                                             (atom_size *
1425                                                              node->data->instance.n_preallocs),
1426                                                             G_ALLOC_AND_FREE);
1427         }
1428       
1429       instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
1430       G_WRITE_UNLOCK (&type_rw_lock);
1431     }
1432   else
1433     instance = g_malloc0 (total_instance_size); /* fine without read lock */
1434   for (i = node->n_supers; i > 0; i--)
1435     {
1436       TypeNode *pnode;
1437       
1438       pnode = lookup_type_node_I (node->supers[i]);
1439       if (pnode->data->instance.instance_init)
1440         {
1441           instance->g_class = pnode->data->instance.class;
1442           pnode->data->instance.instance_init (instance, class);
1443         }
1444     }
1445   instance->g_class = class;
1446   
1447   if (node->data->instance.instance_init)
1448     node->data->instance.instance_init (instance, class);
1449   
1450   return instance;
1451 }
1452
1453 void
1454 g_type_free_instance (GTypeInstance *instance)
1455 {
1456   TypeNode *node;
1457   GTypeClass *class;
1458   
1459   g_return_if_fail (instance != NULL && instance->g_class != NULL);
1460   
1461   class = instance->g_class;
1462   node = lookup_type_node_I (class->g_type);
1463   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1464     {
1465       g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1466                  type_descriptive_name_I (class->g_type));
1467       return;
1468     }
1469   /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1470   if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1471     {
1472       g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1473                  NODE_NAME (node));
1474       return;
1475     }
1476   
1477   instance->g_class = NULL;
1478 #ifdef G_ENABLE_DEBUG  
1479   memset (instance, 0xaa, type_total_instance_size_I (node));   /* debugging hack */
1480 #endif  
1481   if (node->data->instance.n_preallocs)
1482     {
1483       G_WRITE_LOCK (&type_rw_lock);
1484       g_chunk_free (instance, node->data->instance.mem_chunk);
1485       G_WRITE_UNLOCK (&type_rw_lock);
1486     }
1487   else
1488     g_free (instance);
1489   
1490   g_type_class_unref (class);
1491 }
1492
1493 static gboolean
1494 type_iface_vtable_init_Wm (TypeNode *iface,
1495                            TypeNode *node)
1496 {
1497   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1498   IFaceHolder *iholder;
1499   GTypeInterface *vtable = NULL;
1500   TypeNode *pnode;
1501   
1502   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1503   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
1504   if (!iholder)
1505     return FALSE;       /* we don't modify write lock upon FALSE */
1506   
1507   g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1508   
1509   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1510   if (pnode)    /* want to copy over parent iface contents */
1511     {
1512       IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
1513       
1514       if (pentry)
1515         vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
1516     }
1517   if (!vtable)
1518     vtable = g_malloc0 (iface->data->iface.vtable_size);
1519   entry->vtable = vtable;
1520   vtable->g_type = NODE_TYPE (iface);
1521   vtable->g_instance_type = NODE_TYPE (node);
1522   
1523   if (iface->data->iface.vtable_init_base || iholder->info->interface_init)
1524     {
1525       G_WRITE_UNLOCK (&type_rw_lock);
1526       if (iface->data->iface.vtable_init_base)
1527         iface->data->iface.vtable_init_base (vtable);
1528       if (iholder->info->interface_init)
1529         iholder->info->interface_init (vtable, iholder->info->interface_data);
1530       G_WRITE_LOCK (&type_rw_lock);
1531     }
1532   return TRUE;  /* write lock modified */
1533 }
1534
1535 static gboolean
1536 type_iface_vtable_finalize_Wm (TypeNode       *iface,
1537                                TypeNode       *node,
1538                                GTypeInterface *vtable)
1539 {
1540   IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
1541   IFaceHolder *iholder;
1542   
1543   /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
1544   iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
1545   if (!iholder)
1546     return FALSE;       /* we don't modify write lock upon FALSE */
1547   
1548   g_assert (entry && entry->vtable == vtable && iholder->info);
1549   
1550   entry->vtable = NULL;
1551   if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
1552     {
1553       G_WRITE_UNLOCK (&type_rw_lock);
1554       if (iholder->info->interface_finalize)
1555         iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1556       if (iface->data->iface.vtable_finalize_base)
1557         iface->data->iface.vtable_finalize_base (vtable);
1558       G_WRITE_LOCK (&type_rw_lock);
1559     }
1560   vtable->g_type = 0;
1561   vtable->g_instance_type = 0;
1562   g_free (vtable);
1563   
1564   type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
1565   
1566   return TRUE;  /* write lock modified */
1567 }
1568
1569 static void
1570 type_class_init_Wm (TypeNode   *node,
1571                     GTypeClass *pclass)
1572 {
1573   GSList *slist, *init_slist = NULL;
1574   GTypeClass *class;
1575   IFaceEntry *entry;
1576   TypeNode *bnode, *pnode;
1577   guint i;
1578   
1579   g_assert (node->is_classed && node->data &&
1580             node->data->class.class_size &&
1581             !node->data->class.class);
1582
1583   class = g_malloc0 (node->data->class.class_size);
1584   node->data->class.class = class;
1585   
1586   if (pclass)
1587     {
1588       TypeNode *pnode = lookup_type_node_I (pclass->g_type);
1589       
1590       memcpy (class, pclass, pnode->data->class.class_size);
1591     }
1592   class->g_type = NODE_TYPE (node);
1593   
1594   G_WRITE_UNLOCK (&type_rw_lock);
1595   
1596   /* stack all base class initialization functions, so we
1597    * call them in ascending order.
1598    */
1599   for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1600     if (bnode->data->class.class_init_base)
1601       init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1602   for (slist = init_slist; slist; slist = slist->next)
1603     {
1604       GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1605       
1606       class_init_base (class);
1607     }
1608   g_slist_free (init_slist);
1609   
1610   if (node->data->class.class_init)
1611     node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1612   
1613   G_WRITE_LOCK (&type_rw_lock);
1614   
1615   /* ok, we got the class done, now initialize all interfaces, either
1616    * from parent, or through our holder info
1617    */
1618   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1619   entry = CLASSED_NODE_IFACES_ENTRIES (node) + 0;
1620   while (entry)
1621     {
1622       g_assert (entry->vtable == NULL);
1623       
1624       if (!type_iface_vtable_init_Wm (lookup_type_node_I (entry->iface_type), node))
1625         {
1626           guint j;
1627           
1628           /* type_iface_vtable_init_Wm() doesn't modify write lock upon FALSE,
1629            * need to get this interface from parent
1630            */
1631           g_assert (pnode != NULL);
1632           
1633           for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
1634             {
1635               IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
1636               
1637               if (pentry->iface_type == entry->iface_type)
1638                 {
1639                   entry->vtable = pentry->vtable;
1640                   break;
1641                 }
1642             }
1643           g_assert (entry->vtable != NULL);
1644         }
1645       
1646       /* refetch entry, IFACES_ENTRIES might be modified */
1647       for (entry = NULL, i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1648         if (!CLASSED_NODE_IFACES_ENTRIES (node)[i].vtable)
1649           entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1650     }
1651 }
1652
1653 static void
1654 type_data_finalize_class_ifaces_Wm (TypeNode *node)
1655 {
1656   guint i;
1657
1658   g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1659
1660  reiterate:
1661   for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
1662     {
1663       IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
1664       if (entry->vtable)
1665         {
1666           if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
1667             {
1668               /* refetch entries, IFACES_ENTRIES might be modified */
1669               goto reiterate;
1670             }
1671           else
1672             {
1673               /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
1674                * iface vtable came from parent
1675                */
1676               entry->vtable = NULL;
1677             }
1678         }
1679     }
1680 }
1681
1682 static void
1683 type_data_finalize_class_U (TypeNode  *node,
1684                             ClassData *cdata)
1685 {
1686   GTypeClass *class = cdata->class;
1687   TypeNode *bnode;
1688   
1689   g_assert (cdata->class && cdata->common.ref_count == 0);
1690   
1691   if (cdata->class_finalize)
1692     cdata->class_finalize (class, (gpointer) cdata->class_data);
1693   
1694   /* call all base class destruction functions in descending order
1695    */
1696   if (cdata->class_finalize_base)
1697     cdata->class_finalize_base (class);
1698   for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
1699     if (bnode->data->class.class_finalize_base)
1700       bnode->data->class.class_finalize_base (class);
1701   
1702   class->g_type = 0;
1703   g_free (cdata->class);
1704 }
1705
1706 static void
1707 type_data_last_unref_Wm (GType    type,
1708                          gboolean uncached)
1709 {
1710   TypeNode *node = lookup_type_node_I (type);
1711   
1712   g_return_if_fail (node != NULL && node->plugin != NULL);
1713   
1714   if (!node->data || node->data->common.ref_count == 0)
1715     {
1716       g_warning ("cannot drop last reference to unreferenced type `%s'",
1717                  type_descriptive_name_I (type));
1718       return;
1719     }
1720   
1721   if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs)
1722     {
1723       guint i;
1724       
1725       G_WRITE_UNLOCK (&type_rw_lock);
1726       G_READ_LOCK (&type_rw_lock);
1727       for (i = 0; i < static_n_class_cache_funcs; i++)
1728         {
1729           GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
1730           gpointer cache_data = static_class_cache_funcs[i].cache_data;
1731           gboolean need_break;
1732           
1733           G_READ_UNLOCK (&type_rw_lock);
1734           need_break = cache_func (cache_data, node->data->class.class);
1735           G_READ_LOCK (&type_rw_lock);
1736           if (!node->data || node->data->common.ref_count == 0)
1737             INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
1738           if (need_break)
1739             break;
1740         }
1741       G_READ_UNLOCK (&type_rw_lock);
1742       G_WRITE_LOCK (&type_rw_lock);
1743     }
1744   
1745   if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1746     node->data->common.ref_count -= 1;
1747   else
1748     {
1749       GType ptype = NODE_PARENT_TYPE (node);
1750       TypeData *tdata;
1751       
1752       node->data->common.ref_count = 0;
1753       
1754       if (node->is_instantiatable && node->data->instance.mem_chunk)
1755         {
1756           g_mem_chunk_destroy (node->data->instance.mem_chunk);
1757           node->data->instance.mem_chunk = NULL;
1758         }
1759       
1760       tdata = node->data;
1761       if (node->is_classed && tdata->class.class)
1762         {
1763           if (CLASSED_NODE_N_IFACES (node))
1764             type_data_finalize_class_ifaces_Wm (node);
1765           node->mutatable_check_cache = FALSE;
1766           node->data = NULL;
1767           G_WRITE_UNLOCK (&type_rw_lock);
1768           type_data_finalize_class_U (node, &tdata->class);
1769           G_WRITE_LOCK (&type_rw_lock);
1770         }
1771       else
1772         {
1773           node->mutatable_check_cache = FALSE;
1774           node->data = NULL;
1775         }
1776       
1777       /* freeing tdata->common.value_table and its contents is taking care of
1778        * by allocating it in one chunk with tdata
1779        */
1780       g_free (tdata);
1781       
1782       if (ptype)
1783         type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
1784       G_WRITE_UNLOCK (&type_rw_lock);
1785       g_type_plugin_unuse (node->plugin);
1786       G_WRITE_LOCK (&type_rw_lock);
1787     }
1788 }
1789
1790 void
1791 g_type_add_class_cache_func (gpointer            cache_data,
1792                              GTypeClassCacheFunc cache_func)
1793 {
1794   guint i;
1795   
1796   g_return_if_fail (cache_func != NULL);
1797   
1798   G_WRITE_LOCK (&type_rw_lock);
1799   i = static_n_class_cache_funcs++;
1800   static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1801   static_class_cache_funcs[i].cache_data = cache_data;
1802   static_class_cache_funcs[i].cache_func = cache_func;
1803   G_WRITE_UNLOCK (&type_rw_lock);
1804 }
1805
1806 void
1807 g_type_remove_class_cache_func (gpointer            cache_data,
1808                                 GTypeClassCacheFunc cache_func)
1809 {
1810   gboolean found_it = FALSE;
1811   guint i;
1812   
1813   g_return_if_fail (cache_func != NULL);
1814   
1815   G_WRITE_LOCK (&type_rw_lock);
1816   for (i = 0; i < static_n_class_cache_funcs; i++)
1817     if (static_class_cache_funcs[i].cache_data == cache_data &&
1818         static_class_cache_funcs[i].cache_func == cache_func)
1819       {
1820         static_n_class_cache_funcs--;
1821         g_memmove (static_class_cache_funcs + i,
1822                    static_class_cache_funcs + i + 1,
1823                    sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
1824         static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
1825         found_it = TRUE;
1826         break;
1827       }
1828   G_WRITE_UNLOCK (&type_rw_lock);
1829   
1830   if (!found_it)
1831     g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1832                cache_func, cache_data);
1833 }
1834
1835
1836 /* --- type registration --- */
1837 GType
1838 g_type_register_fundamental (GType                       type_id,
1839                              const gchar                *type_name,
1840                              const GTypeInfo            *info,
1841                              const GTypeFundamentalInfo *finfo,
1842                              GTypeFlags                  flags)
1843 {
1844   GTypeFundamentalInfo *node_finfo;
1845   TypeNode *node;
1846   
1847   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1848   g_return_val_if_fail (type_id > 0, 0);
1849   g_return_val_if_fail (type_name != NULL, 0);
1850   g_return_val_if_fail (info != NULL, 0);
1851   g_return_val_if_fail (finfo != NULL, 0);
1852   
1853   if (!check_type_name_I (type_name))
1854     return 0;
1855   if ((type_id & TYPE_ID_MASK) ||
1856       type_id > G_TYPE_FUNDAMENTAL_MAX)
1857     {
1858       g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
1859                  type_name,
1860                  type_id);
1861       return 0;
1862     }
1863   if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1864       !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1865     {
1866       g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1867                  type_name);
1868       return 0;
1869     }
1870   if (lookup_type_node_I (type_id))
1871     {
1872       g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1873                  type_descriptive_name_I (type_id),
1874                  type_name);
1875       return 0;
1876     }
1877   
1878   G_WRITE_LOCK (&type_rw_lock);
1879   node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
1880   node_finfo = type_node_fundamental_info_I (node);
1881   type_add_flags_W (node, flags);
1882   
1883   if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
1884     type_data_make_W (node, info,
1885                       check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1886   G_WRITE_UNLOCK (&type_rw_lock);
1887   
1888   return NODE_TYPE (node);
1889 }
1890
1891 GType
1892 g_type_register_static (GType            parent_type,
1893                         const gchar     *type_name,
1894                         const GTypeInfo *info,
1895                         GTypeFlags       flags)
1896 {
1897   TypeNode *pnode, *node;
1898   GType type = 0;
1899   
1900   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1901   g_return_val_if_fail (parent_type > 0, 0);
1902   g_return_val_if_fail (type_name != NULL, 0);
1903   g_return_val_if_fail (info != NULL, 0);
1904   
1905   if (!check_type_name_I (type_name) ||
1906       !check_derivation_I (parent_type, type_name))
1907     return 0;
1908   if (info->class_finalize)
1909     {
1910       g_warning ("class finalizer specified for static type `%s'",
1911                  type_name);
1912       return 0;
1913     }
1914   
1915   pnode = lookup_type_node_I (parent_type);
1916   G_WRITE_LOCK (&type_rw_lock);
1917   type_data_ref_Wm (pnode);
1918   if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
1919     {
1920       node = type_node_new_W (pnode, type_name, NULL);
1921       type_add_flags_W (node, flags);
1922       type = NODE_TYPE (node);
1923       type_data_make_W (node, info,
1924                         check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
1925     }
1926   G_WRITE_UNLOCK (&type_rw_lock);
1927   
1928   return type;
1929 }
1930
1931 GType
1932 g_type_register_dynamic (GType        parent_type,
1933                          const gchar *type_name,
1934                          GTypePlugin *plugin,
1935                          GTypeFlags   flags)
1936 {
1937   TypeNode *pnode, *node;
1938   GType type;
1939   
1940   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
1941   g_return_val_if_fail (parent_type > 0, 0);
1942   g_return_val_if_fail (type_name != NULL, 0);
1943   g_return_val_if_fail (plugin != NULL, 0);
1944   
1945   if (!check_type_name_I (type_name) ||
1946       !check_derivation_I (parent_type, type_name) ||
1947       !check_plugin_U (plugin, TRUE, FALSE, type_name))
1948     return 0;
1949   
1950   G_WRITE_LOCK (&type_rw_lock);
1951   pnode = lookup_type_node_I (parent_type);
1952   node = type_node_new_W (pnode, type_name, plugin);
1953   type_add_flags_W (node, flags);
1954   type = NODE_TYPE (node);
1955   G_WRITE_UNLOCK (&type_rw_lock);
1956   
1957   return type;
1958 }
1959
1960 void
1961 g_type_add_interface_static (GType                 instance_type,
1962                              GType                 interface_type,
1963                              const GInterfaceInfo *info)
1964 {
1965   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1966   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1967   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1968   
1969   G_WRITE_LOCK (&type_rw_lock);
1970   if (check_add_interface_L (instance_type, interface_type))
1971     {
1972       TypeNode *node = lookup_type_node_I (instance_type);
1973       TypeNode *iface = lookup_type_node_I (interface_type);
1974       
1975       if (check_interface_info_I (iface, NODE_TYPE (node), info))
1976         {
1977           type_add_interface_W (node, iface, info, NULL);
1978           /* if we have a class already, the interface vtable needs to
1979            * be initialized as well
1980            */
1981           if (node->data && node->data->class.class)
1982             type_iface_vtable_init_Wm (iface, node);
1983         }
1984     }
1985   G_WRITE_UNLOCK (&type_rw_lock);
1986 }
1987
1988 void
1989 g_type_add_interface_dynamic (GType        instance_type,
1990                               GType        interface_type,
1991                               GTypePlugin *plugin)
1992 {
1993   TypeNode *node;
1994   
1995   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
1996   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1997   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1998   
1999   node = lookup_type_node_I (instance_type);
2000   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2001     return;
2002   
2003   G_WRITE_LOCK (&type_rw_lock);
2004   if (check_add_interface_L (instance_type, interface_type))
2005     {
2006       TypeNode *iface = lookup_type_node_I (interface_type);
2007       
2008       type_add_interface_W (node, iface, NULL, plugin);
2009       /* if we have a class already, the interface vtable needs to
2010        * be initialized as well
2011        */
2012       if (node->data && node->data->class.class)
2013         type_iface_vtable_init_Wm (iface, node);
2014     }
2015   G_WRITE_UNLOCK (&type_rw_lock);
2016 }
2017
2018
2019 /* --- public API functions --- */
2020 gpointer
2021 g_type_class_ref (GType type)
2022 {
2023   TypeNode *node;
2024   
2025   /* optimize for common code path
2026    */
2027   G_WRITE_LOCK (&type_rw_lock);
2028   node = lookup_type_node_I (type);
2029   if (node && node->is_classed && node->data &&
2030       node->data->class.class && node->data->common.ref_count > 0)
2031     {
2032       type_data_ref_Wm (node);
2033       G_WRITE_UNLOCK (&type_rw_lock);
2034       
2035       return node->data->class.class;
2036     }
2037   
2038   if (!node || !node->is_classed ||
2039       (node->data && node->data->common.ref_count < 1))
2040     {
2041       G_WRITE_UNLOCK (&type_rw_lock);
2042       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
2043                  type_descriptive_name_I (type));
2044       return NULL;
2045     }
2046   
2047   type_data_ref_Wm (node);
2048   
2049   if (!node->data->class.class)
2050     {
2051       GType ptype = NODE_PARENT_TYPE (node);
2052       GTypeClass *pclass = NULL;
2053       
2054       if (ptype)
2055         {
2056           G_WRITE_UNLOCK (&type_rw_lock);
2057           pclass = g_type_class_ref (ptype);
2058           if (node->data->class.class)
2059             INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
2060           G_WRITE_LOCK (&type_rw_lock);
2061         }
2062       
2063       type_class_init_Wm (node, pclass);
2064     }
2065   G_WRITE_UNLOCK (&type_rw_lock);
2066   
2067   return node->data->class.class;
2068 }
2069
2070 void
2071 g_type_class_unref (gpointer g_class)
2072 {
2073   TypeNode *node;
2074   GTypeClass *class = g_class;
2075   
2076   g_return_if_fail (g_class != NULL);
2077   
2078   node = lookup_type_node_I (class->g_type);
2079   G_WRITE_LOCK (&type_rw_lock);
2080   if (node && node->is_classed && node->data &&
2081       node->data->class.class == class && node->data->common.ref_count > 0)
2082     type_data_unref_Wm (node, FALSE);
2083   else
2084     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2085                type_descriptive_name_I (class->g_type));
2086   G_WRITE_UNLOCK (&type_rw_lock);
2087 }
2088
2089 void
2090 g_type_class_unref_uncached (gpointer g_class)
2091 {
2092   TypeNode *node;
2093   GTypeClass *class = g_class;
2094   
2095   g_return_if_fail (g_class != NULL);
2096   
2097   G_WRITE_LOCK (&type_rw_lock);
2098   node = lookup_type_node_I (class->g_type);
2099   if (node && node->is_classed && node->data &&
2100       node->data->class.class == class && node->data->common.ref_count > 0)
2101     type_data_unref_Wm (node, TRUE);
2102   else
2103     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
2104                type_descriptive_name_I (class->g_type));
2105   G_WRITE_UNLOCK (&type_rw_lock);
2106 }
2107
2108 gpointer
2109 g_type_class_peek (GType type)
2110 {
2111   TypeNode *node;
2112   gpointer class;
2113   
2114   node = lookup_type_node_I (type);
2115   G_READ_LOCK (&type_rw_lock);
2116   if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
2117     class = node->data->class.class;
2118   else
2119     class = NULL;
2120   G_READ_UNLOCK (&type_rw_lock);
2121   
2122   return class;
2123 }
2124
2125 gpointer
2126 g_type_class_peek_parent (gpointer g_class)
2127 {
2128   TypeNode *node;
2129   gpointer class = NULL;
2130   
2131   g_return_val_if_fail (g_class != NULL, NULL);
2132   
2133   node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
2134   G_READ_LOCK (&type_rw_lock);
2135   if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
2136     {
2137       node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2138       class = node->data->class.class;
2139     }
2140   else if (NODE_PARENT_TYPE (node))
2141     g_warning (G_STRLOC ": invalid class pointer `%p'", g_class);
2142   G_READ_UNLOCK (&type_rw_lock);
2143   
2144   return class;
2145 }
2146
2147 gpointer
2148 g_type_interface_peek (gpointer instance_class,
2149                        GType    iface_type)
2150 {
2151   TypeNode *node;
2152   TypeNode *iface;
2153   gpointer vtable = NULL;
2154   GTypeClass *class = instance_class;
2155   
2156   g_return_val_if_fail (instance_class != NULL, NULL);
2157   
2158   node = lookup_type_node_I (class->g_type);
2159   iface = lookup_type_node_I (iface_type);
2160   if (node && node->is_instantiatable && iface)
2161     {
2162       IFaceEntry *entry;
2163       
2164       G_READ_LOCK (&type_rw_lock);
2165       
2166       entry = type_lookup_iface_entry_L (node, iface);
2167       if (entry && entry->vtable)       /* entry is relocatable */
2168         vtable = entry->vtable;
2169       
2170       G_READ_UNLOCK (&type_rw_lock);
2171     }
2172   else
2173     g_warning (G_STRLOC ": invalid class pointer `%p'", class);
2174   
2175   return vtable;
2176 }
2177
2178 gpointer
2179 g_type_interface_peek_parent (gpointer g_iface)
2180 {
2181   TypeNode *node;
2182   TypeNode *iface;
2183   gpointer vtable = NULL;
2184   GTypeInterface *iface_class = g_iface;
2185   
2186   g_return_val_if_fail (g_iface != NULL, NULL);
2187   
2188   iface = lookup_type_node_I (iface_class->g_type);
2189   node = lookup_type_node_I (iface_class->g_instance_type);
2190   if (node)
2191     node = lookup_type_node_I (NODE_PARENT_TYPE (node));
2192   if (node && node->is_instantiatable && iface)
2193     {
2194       IFaceEntry *entry;
2195       
2196       G_READ_LOCK (&type_rw_lock);
2197       
2198       entry = type_lookup_iface_entry_L (node, iface);
2199       if (entry && entry->vtable)       /* entry is relocatable */
2200         vtable = entry->vtable;
2201       
2202       G_READ_UNLOCK (&type_rw_lock);
2203     }
2204   else if (node)
2205     g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
2206   
2207   return vtable;
2208 }
2209
2210 G_CONST_RETURN gchar*
2211 g_type_name (GType type)
2212 {
2213   TypeNode *node;
2214   
2215   g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
2216   
2217   node = lookup_type_node_I (type);
2218   
2219   return node ? NODE_NAME (node) : NULL;
2220 }
2221
2222 GQuark
2223 g_type_qname (GType type)
2224 {
2225   TypeNode *node;
2226   
2227   node = lookup_type_node_I (type);
2228   
2229   return node ? node->qname : 0;
2230 }
2231
2232 GType
2233 g_type_from_name (const gchar *name)
2234 {
2235   GType type = 0;
2236   GQuark quark;
2237   
2238   g_return_val_if_fail (name != NULL, 0);
2239   
2240   quark = g_quark_try_string (name);
2241   if (quark)
2242     {
2243       G_READ_LOCK (&type_rw_lock);
2244       type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
2245       G_READ_UNLOCK (&type_rw_lock);
2246     }
2247   
2248   return type;
2249 }
2250
2251 GType
2252 g_type_parent (GType type)
2253 {
2254   TypeNode *node;
2255   
2256   node = lookup_type_node_I (type);
2257   
2258   return node ? NODE_PARENT_TYPE (node) : 0;
2259 }
2260
2261 guint
2262 g_type_depth (GType type)
2263 {
2264   TypeNode *node;
2265   
2266   node = lookup_type_node_I (type);
2267   
2268   return node ? node->n_supers + 1 : 0;
2269 }
2270
2271 GType
2272 g_type_next_base (GType type,
2273                   GType base_type)
2274 {
2275   GType atype = 0;
2276   TypeNode *node;
2277   
2278   node = lookup_type_node_I (type);
2279   if (node)
2280     {
2281       TypeNode *base_node = lookup_type_node_I (base_type);
2282       
2283       if (base_node && base_node->n_supers < node->n_supers)
2284         {
2285           guint n = node->n_supers - base_node->n_supers;
2286           
2287           if (node->supers[n] == base_type)
2288             atype = node->supers[n - 1];
2289         }
2290     }
2291   
2292   return atype;
2293 }
2294
2295 static inline gboolean
2296 type_node_check_conformities_UorL (TypeNode *node,
2297                                    TypeNode *iface_node,
2298                                    /*        support_inheritance */
2299                                    gboolean  support_interfaces,
2300                                    gboolean  support_prerequisites,
2301                                    gboolean  have_lock)
2302 {
2303   gboolean match;
2304   
2305   if (/* support_inheritance && */
2306       NODE_IS_ANCESTOR (iface_node, node))
2307     return TRUE;
2308   
2309   support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
2310   support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
2311   match = FALSE;
2312   if (support_interfaces || support_prerequisites)
2313     {
2314       if (!have_lock)
2315         G_READ_LOCK (&type_rw_lock);
2316       if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
2317         match = TRUE;
2318       else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
2319         match = TRUE;
2320       if (!have_lock)
2321         G_READ_UNLOCK (&type_rw_lock);
2322     }
2323   return match;
2324 }
2325
2326 static gboolean
2327 type_node_is_a_L (TypeNode *node,
2328                   TypeNode *iface_node)
2329 {
2330   return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
2331 }
2332
2333 static inline gboolean
2334 type_node_conforms_to_U (TypeNode *node,
2335                          TypeNode *iface_node,
2336                          gboolean  support_interfaces,
2337                          gboolean  support_prerequisites)
2338 {
2339   return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
2340 }
2341
2342 gboolean
2343 g_type_is_a (GType type,
2344              GType iface_type)
2345 {
2346   TypeNode *node, *iface_node;
2347   gboolean is_a;
2348   
2349   node = lookup_type_node_I (type);
2350   iface_node = lookup_type_node_I (iface_type);
2351   is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
2352   
2353   return is_a;
2354 }
2355
2356 GType* /* free result */
2357 g_type_children (GType  type,
2358                  guint *n_children)
2359 {
2360   TypeNode *node;
2361   
2362   node = lookup_type_node_I (type);
2363   if (node)
2364     {
2365       GType *children;
2366       
2367       G_READ_LOCK (&type_rw_lock);      /* ->children is relocatable */
2368       children = g_new (GType, node->n_children + 1);
2369       memcpy (children, node->children, sizeof (GType) * node->n_children);
2370       children[node->n_children] = 0;
2371       
2372       if (n_children)
2373         *n_children = node->n_children;
2374       G_READ_UNLOCK (&type_rw_lock);
2375       
2376       return children;
2377     }
2378   else
2379     {
2380       if (n_children)
2381         *n_children = 0;
2382       
2383       return NULL;
2384     }
2385 }
2386
2387 GType* /* free result */
2388 g_type_interfaces (GType  type,
2389                    guint *n_interfaces)
2390 {
2391   TypeNode *node;
2392   
2393   node = lookup_type_node_I (type);
2394   if (node && node->is_instantiatable)
2395     {
2396       GType *ifaces;
2397       guint i;
2398       
2399       G_READ_LOCK (&type_rw_lock);
2400       ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
2401       for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
2402         ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
2403       ifaces[i] = 0;
2404       
2405       if (n_interfaces)
2406         *n_interfaces = CLASSED_NODE_N_IFACES (node);
2407       G_READ_UNLOCK (&type_rw_lock);
2408       
2409       return ifaces;
2410     }
2411   else
2412     {
2413       if (n_interfaces)
2414         *n_interfaces = 0;
2415       
2416       return NULL;
2417     }
2418 }
2419
2420 typedef struct _QData QData;
2421 struct _GData
2422 {
2423   guint  n_qdatas;
2424   QData *qdatas;
2425 };
2426 struct _QData
2427 {
2428   GQuark   quark;
2429   gpointer data;
2430 };
2431
2432 static inline gpointer
2433 type_get_qdata_L (TypeNode *node,
2434                   GQuark    quark)
2435 {
2436   GData *gdata = node->global_gdata;
2437   
2438   if (quark && gdata && gdata->n_qdatas)
2439     {
2440       QData *qdatas = gdata->qdatas - 1;
2441       guint n_qdatas = gdata->n_qdatas;
2442       
2443       do
2444         {
2445           guint i;
2446           QData *check;
2447           
2448           i = (n_qdatas + 1) / 2;
2449           check = qdatas + i;
2450           if (quark == check->quark)
2451             return check->data;
2452           else if (quark > check->quark)
2453             {
2454               n_qdatas -= i;
2455               qdatas = check;
2456             }
2457           else /* if (quark < check->quark) */
2458             n_qdatas = i - 1;
2459         }
2460       while (n_qdatas);
2461     }
2462   return NULL;
2463 }
2464
2465 gpointer
2466 g_type_get_qdata (GType  type,
2467                   GQuark quark)
2468 {
2469   TypeNode *node;
2470   gpointer data;
2471   
2472   node = lookup_type_node_I (type);
2473   if (node)
2474     {
2475       G_READ_LOCK (&type_rw_lock);
2476       data = type_get_qdata_L (node, quark);
2477       G_READ_UNLOCK (&type_rw_lock);
2478     }
2479   else
2480     {
2481       g_return_val_if_fail (node != NULL, NULL);
2482       data = NULL;
2483     }
2484   return data;
2485 }
2486
2487 static inline void
2488 type_set_qdata_W (TypeNode *node,
2489                   GQuark    quark,
2490                   gpointer  data)
2491 {
2492   GData *gdata;
2493   QData *qdata;
2494   guint i;
2495   
2496   /* setup qdata list if necessary */
2497   if (!node->global_gdata)
2498     node->global_gdata = g_new0 (GData, 1);
2499   gdata = node->global_gdata;
2500   
2501   /* try resetting old data */
2502   qdata = gdata->qdatas;
2503   for (i = 0; i < gdata->n_qdatas; i++)
2504     if (qdata[i].quark == quark)
2505       {
2506         qdata[i].data = data;
2507         return;
2508       }
2509   
2510   /* add new entry */
2511   gdata->n_qdatas++;
2512   gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
2513   qdata = gdata->qdatas;
2514   for (i = 0; i < gdata->n_qdatas - 1; i++)
2515     if (qdata[i].quark > quark)
2516       break;
2517   g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
2518   qdata[i].quark = quark;
2519   qdata[i].data = data;
2520 }
2521
2522 void
2523 g_type_set_qdata (GType    type,
2524                   GQuark   quark,
2525                   gpointer data)
2526 {
2527   TypeNode *node;
2528   
2529   g_return_if_fail (quark != 0);
2530   
2531   node = lookup_type_node_I (type);
2532   if (node)
2533     {
2534       G_WRITE_LOCK (&type_rw_lock);
2535       type_set_qdata_W (node, quark, data);
2536       G_WRITE_UNLOCK (&type_rw_lock);
2537     }
2538   else
2539     g_return_if_fail (node != NULL);
2540 }
2541
2542 static void
2543 type_add_flags_W (TypeNode  *node,
2544                   GTypeFlags flags)
2545 {
2546   guint dflags;
2547   
2548   g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
2549   g_return_if_fail (node != NULL);
2550   
2551   if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
2552     g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
2553   dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2554   dflags |= flags;
2555   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
2556 }
2557
2558 void
2559 g_type_query (GType       type,
2560               GTypeQuery *query)
2561 {
2562   TypeNode *node;
2563   
2564   g_return_if_fail (query != NULL);
2565   
2566   /* if node is not static and classed, we won't allow query */
2567   query->type = 0;
2568   node = lookup_type_node_I (type);
2569   if (node && node->is_classed && !node->plugin)
2570     {
2571       /* type is classed and probably even instantiatable */
2572       G_READ_LOCK (&type_rw_lock);
2573       if (node->data)   /* type is static or referenced */
2574         {
2575           query->type = NODE_TYPE (node);
2576           query->type_name = NODE_NAME (node);
2577           query->class_size = node->data->class.class_size;
2578           query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
2579         }
2580       G_READ_UNLOCK (&type_rw_lock);
2581     }
2582 }
2583
2584
2585 /* --- implementation details --- */
2586 gboolean
2587 g_type_test_flags (GType type,
2588                    guint flags)
2589 {
2590   TypeNode *node;
2591   gboolean result = FALSE;
2592   
2593   node = lookup_type_node_I (type);
2594   if (node)
2595     {
2596       guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
2597       guint tflags = flags & TYPE_FLAG_MASK;
2598       
2599       if (fflags)
2600         {
2601           GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
2602           
2603           fflags = (finfo->type_flags & fflags) == fflags;
2604         }
2605       else
2606         fflags = TRUE;
2607       
2608       if (tflags)
2609         {
2610           G_READ_LOCK (&type_rw_lock);
2611           tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
2612           G_READ_UNLOCK (&type_rw_lock);
2613         }
2614       else
2615         tflags = TRUE;
2616       
2617       result = tflags && fflags;
2618     }
2619   
2620   return result;
2621 }
2622
2623 GTypePlugin*
2624 g_type_get_plugin (GType type)
2625 {
2626   TypeNode *node;
2627   
2628   node = lookup_type_node_I (type);
2629   
2630   return node ? node->plugin : NULL;
2631 }
2632
2633 GTypePlugin*
2634 g_type_interface_get_plugin (GType instance_type,
2635                              GType interface_type)
2636 {
2637   TypeNode *node;
2638   TypeNode *iface;
2639   
2640   g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);    /* G_TYPE_IS_INTERFACE() is an external call: _U */
2641   
2642   node = lookup_type_node_I (instance_type);  
2643   iface = lookup_type_node_I (interface_type);
2644   if (node && iface)
2645     {
2646       IFaceHolder *iholder;
2647       GTypePlugin *plugin;
2648       
2649       G_READ_LOCK (&type_rw_lock);
2650       
2651       iholder = iface_node_get_holders_L (iface);
2652       while (iholder && iholder->instance_type != instance_type)
2653         iholder = iholder->next;
2654       plugin = iholder ? iholder->plugin : NULL;
2655       
2656       G_READ_UNLOCK (&type_rw_lock);
2657       
2658       return plugin;
2659     }
2660   
2661   g_return_val_if_fail (node == NULL, NULL);
2662   g_return_val_if_fail (iface == NULL, NULL);
2663   
2664   g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
2665   
2666   return NULL;
2667 }
2668
2669 GType
2670 g_type_fundamental_next (void)
2671 {
2672   GType type;
2673   
2674   G_READ_LOCK (&type_rw_lock);
2675   type = static_fundamental_next;
2676   G_READ_UNLOCK (&type_rw_lock);
2677   type = G_TYPE_MAKE_FUNDAMENTAL (type);
2678   return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
2679 }
2680
2681 GType
2682 g_type_fundamental (GType type_id)
2683 {
2684   TypeNode *node = lookup_type_node_I (type_id);
2685   
2686   return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
2687 }
2688
2689 gboolean
2690 g_type_check_instance_is_a (GTypeInstance *type_instance,
2691                             GType          iface_type)
2692 {
2693   TypeNode *node, *iface;
2694   gboolean check;
2695   
2696   if (!type_instance || !type_instance->g_class)
2697     return FALSE;
2698   
2699   node = lookup_type_node_I (type_instance->g_class->g_type);
2700   iface = lookup_type_node_I (iface_type);
2701   check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2702   
2703   return check;
2704 }
2705
2706 gboolean
2707 g_type_check_class_is_a (GTypeClass *type_class,
2708                          GType       is_a_type)
2709 {
2710   TypeNode *node, *iface;
2711   gboolean check;
2712   
2713   if (!type_class)
2714     return FALSE;
2715   
2716   node = lookup_type_node_I (type_class->g_type);
2717   iface = lookup_type_node_I (is_a_type);
2718   check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2719   
2720   return check;
2721 }
2722
2723 GTypeInstance*
2724 g_type_check_instance_cast (GTypeInstance *type_instance,
2725                             GType          iface_type)
2726 {
2727   if (type_instance)
2728     {
2729       if (type_instance->g_class)
2730         {
2731           TypeNode *node, *iface;
2732           gboolean is_instantiatable, check;
2733           
2734           node = lookup_type_node_I (type_instance->g_class->g_type);
2735           is_instantiatable = node && node->is_instantiatable;
2736           iface = lookup_type_node_I (iface_type);
2737           check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
2738           if (check)
2739             return type_instance;
2740           
2741           if (is_instantiatable)
2742             g_warning ("invalid cast from `%s' to `%s'",
2743                        type_descriptive_name_I (type_instance->g_class->g_type),
2744                        type_descriptive_name_I (iface_type));
2745           else
2746             g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
2747                        type_descriptive_name_I (type_instance->g_class->g_type),
2748                        type_descriptive_name_I (iface_type));
2749         }
2750       else
2751         g_warning ("invalid unclassed pointer in cast to `%s'",
2752                    type_descriptive_name_I (iface_type));
2753     }
2754   
2755   return type_instance;
2756 }
2757
2758 GTypeClass*
2759 g_type_check_class_cast (GTypeClass *type_class,
2760                          GType       is_a_type)
2761 {
2762   if (type_class)
2763     {
2764       TypeNode *node, *iface;
2765       gboolean is_classed, check;
2766       
2767       node = lookup_type_node_I (type_class->g_type);
2768       is_classed = node && node->is_classed;
2769       iface = lookup_type_node_I (is_a_type);
2770       check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
2771       if (check)
2772         return type_class;
2773       
2774       if (is_classed)
2775         g_warning ("invalid class cast from `%s' to `%s'",
2776                    type_descriptive_name_I (type_class->g_type),
2777                    type_descriptive_name_I (is_a_type));
2778       else
2779         g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2780                    type_descriptive_name_I (type_class->g_type),
2781                    type_descriptive_name_I (is_a_type));
2782     }
2783   else
2784     g_warning ("invalid class cast from (NULL) pointer to `%s'",
2785                type_descriptive_name_I (is_a_type));
2786   return type_class;
2787 }
2788
2789 gboolean
2790 g_type_check_instance (GTypeInstance *type_instance)
2791 {
2792   /* this function is just here to make the signal system
2793    * conveniently elaborated on instance checks
2794    */
2795   if (type_instance)
2796     {
2797       if (type_instance->g_class)
2798         {
2799           TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
2800           
2801           if (node && node->is_instantiatable)
2802             return TRUE;
2803           
2804           g_warning ("instance of invalid non-instantiatable type `%s'",
2805                      type_descriptive_name_I (type_instance->g_class->g_type));
2806         }
2807       else
2808         g_warning ("instance with invalid (NULL) class pointer");
2809     }
2810   else
2811     g_warning ("invalid (NULL) pointer instance");
2812   
2813   return FALSE;
2814 }
2815
2816 static inline gboolean
2817 type_check_is_value_type_U (GType type)
2818 {
2819   GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
2820   TypeNode *node;
2821   
2822   /* common path speed up */
2823   node = lookup_type_node_I (type);
2824   if (node && node->mutatable_check_cache)
2825     return TRUE;
2826   
2827   G_READ_LOCK (&type_rw_lock);
2828  restart_check:
2829   if (node)
2830     {
2831       if (node->data && node->data->common.ref_count > 0 &&
2832           node->data->common.value_table->value_init)
2833         tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
2834       else if (NODE_IS_IFACE (node))
2835         {
2836           guint i;
2837           
2838           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2839             {
2840               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2841               TypeNode *prnode = lookup_type_node_I (prtype);
2842               
2843               if (prnode->is_instantiatable)
2844                 {
2845                   type = prtype;
2846                   node = lookup_type_node_I (type);
2847                   goto restart_check;
2848                 }
2849             }
2850         }
2851     }
2852   G_READ_UNLOCK (&type_rw_lock);
2853   
2854   return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
2855 }
2856
2857 gboolean
2858 g_type_check_is_value_type (GType type)
2859 {
2860   return type_check_is_value_type_U (type);
2861 }
2862
2863 gboolean
2864 g_type_check_value (GValue *value)
2865 {
2866   return value && type_check_is_value_type_U (value->g_type);
2867 }
2868
2869 gboolean
2870 g_type_check_value_holds (GValue *value,
2871                           GType   type)
2872 {
2873   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
2874 }
2875
2876 GTypeValueTable*
2877 g_type_value_table_peek (GType type)
2878 {
2879   GTypeValueTable *vtable = NULL;
2880   TypeNode *node = lookup_type_node_I (type);
2881   gboolean has_refed_data, has_table;
2882   TypeData *data;
2883
2884   /* speed up common code path, we're not 100% safe here,
2885    * but we should only get called with referenced types anyway
2886    */
2887   data = node ? node->data : NULL;
2888   if (node && node->mutatable_check_cache)
2889     return data->common.value_table;
2890
2891   G_READ_LOCK (&type_rw_lock);
2892   
2893  restart_table_peek:
2894   has_refed_data = node && node->data && node->data->common.ref_count;
2895   has_table = has_refed_data && node->data->common.value_table->value_init;
2896   if (has_refed_data)
2897     {
2898       if (has_table)
2899         vtable = node->data->common.value_table;
2900       else if (NODE_IS_IFACE (node))
2901         {
2902           guint i;
2903           
2904           for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
2905             {
2906               GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
2907               TypeNode *prnode = lookup_type_node_I (prtype);
2908               
2909               if (prnode->is_instantiatable)
2910                 {
2911                   type = prtype;
2912                   node = lookup_type_node_I (type);
2913                   goto restart_table_peek;
2914                 }
2915             }
2916         }
2917     }
2918   
2919   G_READ_UNLOCK (&type_rw_lock);
2920   
2921   if (vtable)
2922     return vtable;
2923   
2924   if (!node)
2925     g_warning (G_STRLOC ": type id `%lu' is invalid", type);
2926   if (!has_refed_data)
2927     g_warning ("can't peek value table for type `%s' which is not currently referenced",
2928                type_descriptive_name_I (type));
2929   
2930   return NULL;
2931 }
2932
2933 G_CONST_RETURN gchar*
2934 g_type_name_from_instance (GTypeInstance *instance)
2935 {
2936   if (!instance)
2937     return "<NULL-instance>";
2938   else
2939     return g_type_name_from_class (instance->g_class);
2940 }
2941
2942 G_CONST_RETURN gchar*
2943 g_type_name_from_class (GTypeClass *g_class)
2944 {
2945   if (!g_class)
2946     return "<NULL-class>";
2947   else
2948     return g_type_name (g_class->g_type);
2949 }
2950
2951
2952 /* --- foreign prototypes --- */
2953 extern void     g_value_c_init          (void); /* sync with gvalue.c */
2954 extern void     g_value_types_init      (void); /* sync with gvaluetypes.c */
2955 extern void     g_enum_types_init       (void); /* sync with genums.c */
2956 extern void     g_param_type_init       (void); /* sync with gparam.c */
2957 extern void     g_boxed_type_init       (void); /* sync with gboxed.c */
2958 extern void     g_object_type_init      (void); /* sync with gobject.c */
2959 extern void     g_param_spec_types_init (void); /* sync with gparamspecs.c */
2960 extern void     g_value_transforms_init (void); /* sync with gvaluetransform.c */
2961 extern void     g_signal_init           (void); /* sync with gsignal.c */
2962
2963
2964 /* --- initialization --- */
2965 void
2966 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
2967 {
2968   G_LOCK_DEFINE_STATIC (type_init_lock);
2969   const gchar *env_string;
2970   GTypeInfo info;
2971   TypeNode *node;
2972   GType type;
2973   
2974   G_LOCK (type_init_lock);
2975   
2976   G_WRITE_LOCK (&type_rw_lock);
2977   
2978   if (static_quark_type_flags)
2979     {
2980       G_WRITE_UNLOCK (&type_rw_lock);
2981       G_UNLOCK (type_init_lock);
2982       return;
2983     }
2984   
2985   /* setup GObject library wide debugging flags */
2986   _g_type_debug_flags = debug_flags & G_TYPE_DEBUG_MASK;
2987   env_string = g_getenv ("GOBJECT_DEBUG");
2988   if (env_string != NULL)
2989     {
2990       static GDebugKey debug_keys[] = {
2991         { "objects", G_TYPE_DEBUG_OBJECTS },
2992         { "signals", G_TYPE_DEBUG_SIGNALS },
2993       };
2994       
2995       _g_type_debug_flags |= g_parse_debug_string (env_string,
2996                                                    debug_keys,
2997                                                    sizeof (debug_keys) / sizeof (debug_keys[0]));
2998       env_string = NULL;
2999     }
3000   
3001   /* quarks */
3002   static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
3003   static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
3004   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
3005   
3006   /* type qname hash table */
3007   static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
3008   
3009   /* invalid type G_TYPE_INVALID (0)
3010    */
3011   static_fundamental_type_nodes[0] = NULL;
3012   
3013   /* void type G_TYPE_NONE
3014    */
3015   node = type_node_fundamental_new_W (G_TYPE_NONE, "void", 0);
3016   type = NODE_TYPE (node);
3017   g_assert (type == G_TYPE_NONE);
3018   
3019   /* interface fundamental type G_TYPE_INTERFACE (!classed)
3020    */
3021   memset (&info, 0, sizeof (info));
3022   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
3023   type = NODE_TYPE (node);
3024   type_data_make_W (node, &info, NULL);
3025   g_assert (type == G_TYPE_INTERFACE);
3026   
3027   G_WRITE_UNLOCK (&type_rw_lock);
3028   
3029   g_value_c_init ();
3030
3031   /* G_TYPE_TYPE_PLUGIN
3032    */
3033   g_type_plugin_get_type ();
3034   
3035   /* G_TYPE_* value types
3036    */
3037   g_value_types_init ();
3038   
3039   /* G_TYPE_ENUM & G_TYPE_FLAGS
3040    */
3041   g_enum_types_init ();
3042   
3043   /* G_TYPE_BOXED
3044    */
3045   g_boxed_type_init ();
3046   
3047   /* G_TYPE_PARAM
3048    */
3049   g_param_type_init ();
3050   
3051   /* G_TYPE_OBJECT
3052    */
3053   g_object_type_init ();
3054   
3055   /* G_TYPE_PARAM_* pspec types
3056    */
3057   g_param_spec_types_init ();
3058   
3059   /* Value Transformations
3060    */
3061   g_value_transforms_init ();
3062   
3063   /* Signal system
3064    */
3065   g_signal_init ();
3066   
3067   G_UNLOCK (type_init_lock);
3068 }
3069
3070 void 
3071 g_type_init (void)
3072 {
3073   g_type_init_with_debug_flags (0);
3074 }
3075
3076 void
3077 g_type_class_add_private (gpointer g_class,
3078                           gsize    private_size)
3079 {
3080   GType instance_type = ((GTypeClass *)g_class)->g_type;
3081   TypeNode *node = lookup_type_node_I (instance_type);
3082   gsize offset;
3083
3084   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
3085     {
3086       g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
3087                  type_descriptive_name_I (instance_type));
3088       return;
3089     }
3090
3091   if (NODE_PARENT_TYPE (node))
3092     {
3093       TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
3094       if (node->data->instance.private_size != pnode->data->instance.private_size)
3095         {
3096           g_warning ("g_type_add_private() called multiple times for the same type");
3097           return;
3098         }
3099     }
3100   
3101   G_WRITE_LOCK (&type_rw_lock);
3102
3103   offset = ALIGN_STRUCT (node->data->instance.private_size);
3104   node->data->instance.private_size = offset + private_size;
3105   
3106   G_WRITE_UNLOCK (&type_rw_lock);
3107 }
3108
3109 gpointer
3110 g_type_instance_get_private (GTypeInstance *instance,
3111                              GType          private_type)
3112 {
3113   TypeNode *instance_node;
3114   TypeNode *private_node;
3115   TypeNode *parent_node;
3116   gsize offset;
3117
3118   g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
3119   
3120   instance_node = lookup_type_node_I (instance->g_class->g_type);
3121   if (G_UNLIKELY (!instance_node || !instance_node->is_instantiatable))
3122     {
3123       g_warning ("instance of invalid non-instantiatable type `%s'",
3124                  type_descriptive_name_I (instance->g_class->g_type));
3125       return NULL;
3126     }
3127
3128   private_node = lookup_type_node_I (private_type);
3129   if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, instance_node)))
3130     {
3131       g_warning ("attempt to retrieve private data for invalid type '%s'",
3132                  type_descriptive_name_I (private_type));
3133       return NULL;
3134     }
3135
3136   /* Note that we don't need a read lock, since instance existing
3137    * means that the instance class and all parent classes
3138    * exist, so the node->data, node->data->instance.instance_size,
3139    * and node->data->instance.private_size are not going to be changed.
3140    * for any of the relevant types.
3141    */
3142
3143   offset = ALIGN_STRUCT (instance_node->data->instance.instance_size);
3144
3145   if (NODE_PARENT_TYPE (private_node))
3146     {
3147       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
3148       g_assert (parent_node->data && parent_node->data->common.ref_count);
3149
3150       offset += ALIGN_STRUCT (parent_node->data->instance.private_size);
3151     }
3152
3153   return G_STRUCT_MEMBER_P (instance, offset);
3154 }