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