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