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