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