Fix a typo
[platform/upstream/glib.git] / gobject / gtype.c
index 04bcc7e..dac5e8f 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA 02111-1307, USA.
  */
-#include        <config.h>
-#include       "gtype.h"
-#include       "gobjectalias.h"
 
 /*
  * MT safe
  */
 
-#include       "gtypeplugin.h"
-#include       "gvaluecollector.h"
-#include       "gbsearcharray.h"
-#include       <string.h>
+#include "config.h"
+
+#include <string.h>
+
+#include "gtype.h"
+#include "gtype-private.h"
+#include "gtypeplugin.h"
+#include "gvaluecollector.h"
+#include "gbsearcharray.h"
+#include "gatomicarray.h"
+#include "gobject_trace.h"
+
+
+/**
+ * SECTION:gtype
+ * @short_description: The GLib Runtime type identification and
+ *     management system
+ * @title:Type Information
+ *
+ * The GType API is the foundation of the GObject system.  It provides the
+ * facilities for registering and managing all fundamental data types,
+ * user-defined object and interface types.  Before using any GType
+ * or GObject functions, g_type_init() must be called to initialize the
+ * type system.
+ *
+ * For type creation and registration purposes, all types fall into one of
+ * two categories: static or dynamic.  Static types are never loaded or
+ * unloaded at run-time as dynamic types may be.  Static types are created
+ * with g_type_register_static() that gets type specific information passed
+ * in via a #GTypeInfo structure.
+ * Dynamic types are created with g_type_register_dynamic() which takes a
+ * #GTypePlugin structure instead. The remaining type information (the
+ * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
+ * and the g_type_plugin_*() API.
+ * These registration functions are usually called only once from a
+ * function whose only purpose is to return the type identifier for a
+ * specific class.  Once the type (or class or interface) is registered,
+ * it may be instantiated, inherited, or implemented depending on exactly
+ * what sort of type it is.
+ * There is also a third registration function for registering fundamental
+ * types called g_type_register_fundamental() which requires both a #GTypeInfo
+ * structure and a #GTypeFundamentalInfo structure but it is seldom used
+ * since most fundamental types are predefined rather than user-defined.
+ *
+ * A final word about type names.
+ * Such an identifier needs to be at least three characters long. There is no
+ * upper length limit. The first character needs to be a letter (a-z or A-Z)
+ * or an underscore '_'. Subsequent characters can be letters, numbers or
+ * any of '-_+'.
+ */
 
 
 /* NOTE: some functions (some internal variants and exported ones)
  * - _Wm:      [Write-locked invocation, mutatable]
  *   like _W, but the write lock might be released and reacquired
  *   during invocation, watch your pointers
+ * - _WmREC:    [Write-locked invocation, mutatable, recursive]
+ *   like _Wm, but also acquires recursive mutex class_init_rec_mutex
  */
 
-static GStaticRWLock            type_rw_lock = G_STATIC_RW_LOCK_INIT;
 #ifdef LOCK_DEBUG
 #define G_READ_LOCK(rw_lock)    do { g_printerr (G_STRLOC ": readL++\n"); g_static_rw_lock_reader_lock (rw_lock); } while (0)
 #define G_READ_UNLOCK(rw_lock)  do { g_printerr (G_STRLOC ": readL--\n"); g_static_rw_lock_reader_unlock (rw_lock); } while (0)
@@ -81,14 +125,14 @@ static GStaticRWLock            type_rw_lock = G_STATIC_RW_LOCK_INIT;
     else \
       g_error ("%s()%s`%s'", _fname, _action, _tname); \
 }G_STMT_END
-#define        g_return_val_if_uninitialized(condition, init_function, return_value) G_STMT_START{     \
-  if (!(condition))                                                                            \
-    {                                                                                          \
-      g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,                                               \
-            "%s: initialization assertion failed, use %s() prior to this function",            \
-            G_STRLOC, G_STRINGIFY (init_function));                                            \
-      return (return_value);                                                                   \
-    }                                                                                          \
+#define g_return_val_if_type_system_uninitialized(return_value) G_STMT_START{ \
+    if (G_UNLIKELY (!static_quark_type_flags))                                \
+      {                                                                       \
+        g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,                            \
+               "%s: You forgot to call g_type_init()",                        \
+               G_STRLOC);                                                     \
+        return (return_value);                                                \
+      }                                                                       \
 }G_STMT_END
 
 #ifdef  G_ENABLE_DEBUG
@@ -125,10 +169,12 @@ static GStaticRWLock            type_rw_lock = G_STATIC_RW_LOCK_INIT;
 /* --- typedefs --- */
 typedef struct _TypeNode        TypeNode;
 typedef struct _CommonData      CommonData;
+typedef struct _BoxedData       BoxedData;
 typedef struct _IFaceData       IFaceData;
 typedef struct _ClassData       ClassData;
 typedef struct _InstanceData    InstanceData;
 typedef union  _TypeData        TypeData;
+typedef struct _IFaceEntries    IFaceEntries;
 typedef struct _IFaceEntry      IFaceEntry;
 typedef struct _IFaceHolder    IFaceHolder;
 
@@ -141,9 +187,9 @@ static            void                      type_data_make_W                (TypeNode               *node,
                                                                         const GTypeInfo        *info,
                                                                         const GTypeValueTable  *value_table);
 static inline void                     type_data_ref_Wm                (TypeNode               *node);
-static inline void                     type_data_unref_Wm              (TypeNode               *node,
+static inline void                     type_data_unref_              (TypeNode               *node,
                                                                         gboolean                uncached);
-static void                            type_data_last_unref_Wm         (GType                   type,
+static void                            type_data_last_unref_Wm         (TypeNode *              node,
                                                                         gboolean                uncached);
 static inline gpointer                 type_get_qdata_L                (TypeNode               *node,
                                                                         GQuark                  quark);
@@ -180,37 +226,42 @@ typedef enum
 /* --- structures --- */
 struct _TypeNode
 {
+  guint volatile ref_count;
   GTypePlugin *plugin;
-  guint        n_children : 12;
+  guint        n_children; /* writable with lock */
   guint        n_supers : 8;
-  guint        _prot_n_ifaces_prerequisites : 9;
+  guint        n_prerequisites : 9;
   guint        is_classed : 1;
   guint        is_instantiatable : 1;
   guint        mutatable_check_cache : 1;      /* combines some common path checks */
-  GType       *children;
+  GType       *children; /* writable with lock */
   TypeData * volatile data;
   GQuark       qname;
   GData       *global_gdata;
   union {
-    IFaceEntry  *iface_entries;                /* for !iface types */
-    GType       *prerequisistes;
+    GAtomicArray iface_entries;                /* for !iface types */
+    GAtomicArray offsets;
   } _prot;
+  GType       *prerequisites;
   GType        supers[1]; /* flexible array */
 };
+
 #define SIZEOF_BASE_TYPE_NODE()                        (G_STRUCT_OFFSET (TypeNode, supers))
 #define MAX_N_SUPERS                           (255)
 #define MAX_N_CHILDREN                         (4095)
-#define MAX_N_IFACES                           (511)
-#define        MAX_N_PREREQUISITES                     (MAX_N_IFACES)
+#define        MAX_N_INTERFACES                        (255) /* Limited by offsets being 8 bits */
+#define        MAX_N_PREREQUISITES                     (511)
 #define NODE_TYPE(node)                                (node->supers[0])
 #define NODE_PARENT_TYPE(node)                 (node->supers[1])
 #define NODE_FUNDAMENTAL_TYPE(node)            (node->supers[node->n_supers])
 #define NODE_NAME(node)                                (g_quark_to_string (node->qname))
+#define NODE_REFCOUNT(node)                     ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
+#define        NODE_IS_BOXED(node)                     (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
 #define        NODE_IS_IFACE(node)                     (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
-#define        CLASSED_NODE_N_IFACES(node)             ((node)->_prot_n_ifaces_prerequisites)
-#define        CLASSED_NODE_IFACES_ENTRIES(node)       ((node)->_prot.iface_entries)
-#define        IFACE_NODE_N_PREREQUISITES(node)        ((node)->_prot_n_ifaces_prerequisites)
-#define        IFACE_NODE_PREREQUISITES(node)          ((node)->_prot.prerequisistes)
+#define        CLASSED_NODE_IFACES_ENTRIES(node)       (&(node)->_prot.iface_entries)
+#define        CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
+#define        IFACE_NODE_N_PREREQUISITES(node)        ((node)->n_prerequisites)
+#define        IFACE_NODE_PREREQUISITES(node)          ((node)->prerequisites)
 #define        iface_node_get_holders_L(node)          ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
 #define        iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
 #define        iface_node_get_dependants_array_L(n)    ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
@@ -221,7 +272,6 @@ struct _TypeNode
         ((ancestor)->n_supers <= (node)->n_supers &&                                        \
         (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
 
-
 struct _IFaceHolder
 {
   GType           instance_type;
@@ -229,17 +279,34 @@ struct _IFaceHolder
   GTypePlugin    *plugin;
   IFaceHolder    *next;
 };
+
 struct _IFaceEntry
 {
   GType           iface_type;
   GTypeInterface *vtable;
   InitState       init_state;
 };
+
+struct _IFaceEntries {
+  guint offset_index;
+  IFaceEntry entry[1];
+};
+
+#define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
+#define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
+
 struct _CommonData
 {
-  guint             ref_count;
   GTypeValueTable  *value_table;
 };
+
+struct _BoxedData
+{
+  CommonData         data;
+  GBoxedCopyFunc     copy_func;
+  GBoxedFreeFunc     free_func;
+};
+
 struct _IFaceData
 {
   CommonData         common;
@@ -251,11 +318,13 @@ struct _IFaceData
   gconstpointer      dflt_data;
   gpointer           dflt_vtable;
 };
+
 struct _ClassData
 {
   CommonData         common;
   guint16            class_size;
-  guint              init_state : 4;
+  guint16            class_private_size;
+  int volatile       init_state; /* atomic - g_type_class_ref reads it unlocked */
   GBaseInitFunc      class_init_base;
   GBaseFinalizeFunc  class_finalize_base;
   GClassInitFunc     class_init;
@@ -263,11 +332,13 @@ struct _ClassData
   gconstpointer      class_data;
   gpointer           class;
 };
+
 struct _InstanceData
 {
   CommonData         common;
   guint16            class_size;
-  guint              init_state : 4;
+  guint16            class_private_size;
+  int volatile       init_state; /* atomic - g_type_class_ref reads it unlocked */
   GBaseInitFunc      class_init_base;
   GBaseFinalizeFunc  class_finalize_base;
   GClassInitFunc     class_init;
@@ -279,17 +350,21 @@ struct _InstanceData
   guint16            n_preallocs;
   GInstanceInitFunc  instance_init;
 };
+
 union _TypeData
 {
   CommonData         common;
+  BoxedData          boxed;
   IFaceData          iface;
   ClassData          class;
   InstanceData       instance;
 };
+
 typedef struct {
   gpointer            cache_data;
   GTypeClassCacheFunc cache_func;
 } ClassCacheFunc;
+
 typedef struct {
   gpointer                check_data;
   GTypeInterfaceCheckFunc check_func;
@@ -297,6 +372,8 @@ typedef struct {
 
 
 /* --- variables --- */
+static GStaticRWLock   type_rw_lock = G_STATIC_RW_LOCK_INIT;
+static GStaticRecMutex class_init_rec_mutex = G_STATIC_REC_MUTEX_INIT;
 static guint           static_n_class_cache_funcs = 0;
 static ClassCacheFunc *static_class_cache_funcs = NULL;
 static guint           static_n_iface_check_funcs = 0;
@@ -306,7 +383,6 @@ static GQuark          static_quark_iface_holder = 0;
 static GQuark          static_quark_dependants_array = 0;
 GTypeDebugFlags               _g_type_debug_flags = 0;
 
-
 /* --- type nodes --- */
 static GHashTable       *static_type_nodes_ht = NULL;
 static TypeNode                *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
@@ -332,7 +408,7 @@ type_node_any_new_W (TypeNode             *pnode,
   GType type;
   TypeNode *node;
   guint i, node_size = 0;
-  
+
   n_supers = pnode ? pnode->n_supers + 1 : 0;
   
   if (!pnode)
@@ -366,10 +442,7 @@ type_node_any_new_W (TypeNode             *pnode,
          IFACE_NODE_PREREQUISITES (node) = NULL;
        }
       else
-       {
-         CLASSED_NODE_N_IFACES (node) = 0;
-         CLASSED_NODE_IFACES_ENTRIES (node) = NULL;
-       }
+       _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
     }
   else
     {
@@ -387,23 +460,30 @@ type_node_any_new_W (TypeNode             *pnode,
       else
        {
          guint j;
-         
-         CLASSED_NODE_N_IFACES (node) = CLASSED_NODE_N_IFACES (pnode);
-         CLASSED_NODE_IFACES_ENTRIES (node) = g_memdup (CLASSED_NODE_IFACES_ENTRIES (pnode),
-                                                        sizeof (CLASSED_NODE_IFACES_ENTRIES (pnode)[0]) *
-                                                        CLASSED_NODE_N_IFACES (node));
-         for (j = 0; j < CLASSED_NODE_N_IFACES (node); j++)
+         IFaceEntries *entries;
+
+         entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
+                                         IFACE_ENTRIES_HEADER_SIZE,
+                                         0);
+         if (entries)
            {
-             CLASSED_NODE_IFACES_ENTRIES (node)[j].vtable = NULL;
-             CLASSED_NODE_IFACES_ENTRIES (node)[j].init_state = UNINITIALIZED;
+             for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
+               {
+                 entries->entry[j].vtable = NULL;
+                 entries->entry[j].init_state = UNINITIALIZED;
+               }
+             _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
+                                     entries);
            }
        }
-      
+
       i = pnode->n_children++;
       pnode->children = g_renew (GType, pnode->children, pnode->n_children);
       pnode->children[i] = type;
     }
-  
+
+  TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
+
   node->plugin = plugin;
   node->n_children = 0;
   node->children = NULL;
@@ -466,36 +546,88 @@ type_node_new_W (TypeNode    *pnode,
 }
 
 static inline IFaceEntry*
+lookup_iface_entry_I (volatile IFaceEntries *entries,
+                     TypeNode *iface_node)
+{
+  guint8 *offsets;
+  guint offset_index;
+  IFaceEntry *check;
+  int index;
+  IFaceEntry *entry;
+
+  if (entries == NULL)
+    return NULL;
+
+  G_ATOMIC_ARRAY_DO_TRANSACTION
+    (&iface_node->_prot.offsets, guint8,
+
+     entry = NULL;
+     offsets = transaction_data;
+     offset_index = entries->offset_index;
+     if (offsets != NULL &&
+        offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
+       {
+        index = offsets[offset_index];
+        if (index > 0)
+          {
+            /* zero means unset, subtract one to get real index */
+            index -= 1;
+
+            if (index < IFACE_ENTRIES_N_ENTRIES (entries))
+              {
+                check = (IFaceEntry *)&entries->entry[index];
+                if (check->iface_type == NODE_TYPE (iface_node))
+                  entry = check;
+              }
+          }
+       }
+     );
+
+ return entry;
+}
+
+static inline IFaceEntry*
 type_lookup_iface_entry_L (TypeNode *node,
                           TypeNode *iface_node)
 {
-  if (NODE_IS_IFACE (iface_node) && CLASSED_NODE_N_IFACES (node))
+  if (!NODE_IS_IFACE (iface_node))
+    return NULL;
+
+  return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
+                              iface_node);
+}
+
+
+static inline gboolean
+type_lookup_iface_vtable_I (TypeNode *node,
+                           TypeNode *iface_node,
+                           gpointer *vtable_ptr)
+{
+  IFaceEntry *entry;
+  gboolean res;
+
+  if (!NODE_IS_IFACE (iface_node))
     {
-      IFaceEntry *ifaces = CLASSED_NODE_IFACES_ENTRIES (node) - 1;
-      guint n_ifaces = CLASSED_NODE_N_IFACES (node);
-      GType iface_type = NODE_TYPE (iface_node);
-      
-      do
-       {
-         guint i;
-         IFaceEntry *check;
-         
-         i = (n_ifaces + 1) >> 1;
-         check = ifaces + i;
-         if (iface_type == check->iface_type)
-           return check;
-         else if (iface_type > check->iface_type)
-           {
-             n_ifaces -= i;
-             ifaces = check;
-           }
-         else /* if (iface_type < check->iface_type) */
-           n_ifaces = i - 1;
-       }
-      while (n_ifaces);
+      if (vtable_ptr)
+       *vtable_ptr = NULL;
+      return FALSE;
     }
-  
-  return NULL;
+
+  G_ATOMIC_ARRAY_DO_TRANSACTION
+    (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
+
+     entry = lookup_iface_entry_I (transaction_data, iface_node);
+     res = entry != NULL;
+     if (vtable_ptr)
+       {
+        if (entry)
+          *vtable_ptr = entry->vtable;
+        else
+          *vtable_ptr = NULL;
+       }
+     );
+
+  return res;
 }
 
 static inline gboolean
@@ -529,7 +661,7 @@ type_lookup_prerequisite_L (TypeNode *iface,
   return FALSE;
 }
 
-static gchar*
+static const gchar*
 type_descriptive_name_I (GType type)
 {
   if (type)
@@ -953,6 +1085,8 @@ type_data_make_W (TypeNode              *node,
    
   if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
     {
+      TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
+
       data = g_malloc0 (sizeof (InstanceData) + vtable_size);
       if (vtable_size)
        vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
@@ -969,6 +1103,9 @@ type_data_make_W (TypeNode              *node,
        * after the parent class has been initialized
        */
       data->instance.private_size = 0;
+      data->instance.class_private_size = 0;
+      if (pnode)
+        data->instance.class_private_size = pnode->data->instance.class_private_size;
 #ifdef DISABLE_MEM_POOLS
       data->instance.n_preallocs = 0;
 #else  /* !DISABLE_MEM_POOLS */
@@ -978,6 +1115,8 @@ type_data_make_W (TypeNode              *node,
     }
   else if (node->is_classed) /* only classed */
     {
+      TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
+
       data = g_malloc0 (sizeof (ClassData) + vtable_size);
       if (vtable_size)
        vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
@@ -988,6 +1127,9 @@ type_data_make_W (TypeNode              *node,
       data->class.class_finalize = info->class_finalize;
       data->class.class_data = info->class_data;
       data->class.class = NULL;
+      data->class.class_private_size = 0;
+      if (pnode)
+        data->class.class_private_size = pnode->data->class.class_private_size;
       data->class.init_state = UNINITIALIZED;
     }
   else if (NODE_IS_IFACE (node))
@@ -1003,6 +1145,12 @@ type_data_make_W (TypeNode              *node,
       data->iface.dflt_data = info->class_data;
       data->iface.dflt_vtable = NULL;
     }
+  else if (NODE_IS_BOXED (node))
+    {
+      data = g_malloc0 (sizeof (BoxedData) + vtable_size);
+      if (vtable_size)
+       vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
+    }
   else
     {
       data = g_malloc0 (sizeof (CommonData) + vtable_size);
@@ -1011,7 +1159,6 @@ type_data_make_W (TypeNode              *node,
     }
   
   node->data = data;
-  node->data->common.ref_count = 1;
   
   if (vtable_size)
     {
@@ -1042,6 +1189,8 @@ type_data_make_W (TypeNode              *node,
                                   GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
   
   g_assert (node->data->common.value_table != NULL); /* paranoid */
+
+  g_atomic_int_set ((int *) &node->ref_count, 1);
 }
 
 static inline void
@@ -1079,31 +1228,107 @@ type_data_ref_Wm (TypeNode *node)
     }
   else
     {
-      g_assert (node->data->common.ref_count > 0);
+      g_assert (NODE_REFCOUNT (node) > 0);
       
-      node->data->common.ref_count += 1;
+      g_atomic_int_inc ((int *) &node->ref_count);
     }
 }
 
-static inline void
-type_data_unref_Wm (TypeNode *node,
-                   gboolean  uncached)
+static inline gboolean
+type_data_ref_U (TypeNode *node)
 {
-  g_assert (node->data && node->data->common.ref_count);
-  
-  if (node->data->common.ref_count > 1)
-    node->data->common.ref_count -= 1;
-  else
+  guint current;
+
+  do {
+    current = NODE_REFCOUNT (node);
+
+    if (current < 1)
+      return FALSE;
+  } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
+
+  return TRUE;
+}
+
+static gboolean
+iface_node_has_available_offset_L (TypeNode *iface_node,
+                                  int offset,
+                                  int for_index)
+{
+  guint8 *offsets;
+
+  offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
+  if (offsets == NULL)
+    return TRUE;
+
+  if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
+    return TRUE;
+
+  if (offsets[offset] == 0 ||
+      offsets[offset] == for_index+1)
+    return TRUE;
+
+  return FALSE;
+}
+
+static int
+find_free_iface_offset_L (IFaceEntries *entries)
+{
+  IFaceEntry *entry;
+  TypeNode *iface_node;
+  int offset;
+  int i;
+  int n_entries;
+
+  n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
+  offset = -1;
+  do
     {
-      if (!node->plugin)
+      offset++;
+      for (i = 0; i < n_entries; i++)
        {
-         g_warning ("static type `%s' unreferenced too often",
-                    NODE_NAME (node));
-         return;
+         entry = &entries->entry[i];
+         iface_node = lookup_type_node_I (entry->iface_type);
+
+         if (!iface_node_has_available_offset_L (iface_node, offset, i))
+           break;
        }
-      
-      type_data_last_unref_Wm (NODE_TYPE (node), uncached);
     }
+  while (i != n_entries);
+
+  return offset;
+}
+
+static void
+iface_node_set_offset_L (TypeNode *iface_node,
+                        int offset,
+                        int index)
+{
+  guint8 *offsets, *old_offsets;
+  int new_size, old_size;
+  int i;
+
+  old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
+  if (old_offsets == NULL)
+    old_size = 0;
+  else
+    {
+      old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
+      if (offset < old_size &&
+         old_offsets[offset] == index + 1)
+       return; /* Already set to this index, return */
+    }
+  new_size = MAX (old_size, offset + 1);
+
+  offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
+                                 0, new_size - old_size);
+
+  /* Mark new area as unused */
+  for (i = old_size; i < new_size; i++)
+    offsets[i] = 0;
+
+  offsets[offset] = index + 1;
+
+  _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
 }
 
 static void
@@ -1111,54 +1336,97 @@ type_node_add_iface_entry_W (TypeNode   *node,
                             GType       iface_type,
                              IFaceEntry *parent_entry)
 {
-  IFaceEntry *entries;
-  guint i;
-  
-  g_assert (node->is_instantiatable && CLASSED_NODE_N_IFACES (node) < MAX_N_IFACES);
-  
-  entries = CLASSED_NODE_IFACES_ENTRIES (node);
-  for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
-    if (entries[i].iface_type == iface_type)
-      {
-       /* this can happen in two cases:
-         * - our parent type already conformed to iface_type and node
-         *   got it's own holder info. here, our children already have
-         *   entries and NULL vtables, since this will only work for
-         *   uninitialized classes.
-        * - an interface type is added to an ancestor after it was
-         *   added to a child type.
-        */
-        if (!parent_entry)
-          g_assert (entries[i].vtable == NULL && entries[i].init_state == UNINITIALIZED);
-        else
-          {
-            /* sick, interface is added to ancestor *after* child type;
-             * nothing todo, the entry and our children were already setup correctly
-             */
-          }
-        return;
-      }
-    else if (entries[i].iface_type > iface_type)
-      break;
-  CLASSED_NODE_N_IFACES (node) += 1;
-  CLASSED_NODE_IFACES_ENTRIES (node) = g_renew (IFaceEntry,
-                                               CLASSED_NODE_IFACES_ENTRIES (node),
-                                               CLASSED_NODE_N_IFACES (node));
-  entries = CLASSED_NODE_IFACES_ENTRIES (node);
-  g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (CLASSED_NODE_N_IFACES (node) - i - 1));
-  entries[i].iface_type = iface_type;
-  entries[i].vtable = NULL;
-  entries[i].init_state = UNINITIALIZED;
+  IFaceEntries *entries;
+  IFaceEntry *entry;
+  TypeNode *iface_node;
+  guint i, j;
+  int num_entries;
+
+  g_assert (node->is_instantiatable);
+
+  entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
+  if (entries != NULL)
+    {
+      num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
+
+      g_assert (num_entries < MAX_N_INTERFACES);
+
+      for (i = 0; i < num_entries; i++)
+       {
+         entry = &entries->entry[i];
+         if (entry->iface_type == iface_type)
+           {
+             /* this can happen in two cases:
+              * - our parent type already conformed to iface_type and node
+              *   got its own holder info. here, our children already have
+              *   entries and NULL vtables, since this will only work for
+              *   uninitialized classes.
+              * - an interface type is added to an ancestor after it was
+              *   added to a child type.
+              */
+             if (!parent_entry)
+               g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
+             else
+               {
+                 /* sick, interface is added to ancestor *after* child type;
+                  * nothing todo, the entry and our children were already setup correctly
+                  */
+               }
+             return;
+           }
+       }
+    }
+
+  entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
+                                 IFACE_ENTRIES_HEADER_SIZE,
+                                 sizeof (IFaceEntry));
+  num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
+  i = num_entries - 1;
+  if (i == 0)
+    entries->offset_index = 0;
+  entries->entry[i].iface_type = iface_type;
+  entries->entry[i].vtable = NULL;
+  entries->entry[i].init_state = UNINITIALIZED;
 
   if (parent_entry)
     {
       if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
         {
-          entries[i].init_state = INITIALIZED;
-          entries[i].vtable = parent_entry->vtable;
+          entries->entry[i].init_state = INITIALIZED;
+          entries->entry[i].vtable = parent_entry->vtable;
         }
+    }
+
+  /* Update offsets in iface */
+  iface_node = lookup_type_node_I (iface_type);
+
+  if (iface_node_has_available_offset_L (iface_node,
+                                        entries->offset_index,
+                                        i))
+    {
+      iface_node_set_offset_L (iface_node,
+                              entries->offset_index, i);
+    }
+  else
+   {
+      entries->offset_index =
+       find_free_iface_offset_L (entries);
+      for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
+       {
+         entry = &entries->entry[j];
+         iface_node =
+           lookup_type_node_I (entry->iface_type);
+         iface_node_set_offset_L (iface_node,
+                                  entries->offset_index, j);
+       }
+    }
+
+  _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
+
+  if (parent_entry)
+    {
       for (i = 0; i < node->n_children; i++)
-        type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries[i]);
+        type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
     }
 }
 
@@ -1171,7 +1439,7 @@ type_add_interface_Wm (TypeNode             *node,
   IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
   IFaceEntry *entry;
   guint i;
-  
+
   g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
   
   iholder->next = iface_node_get_holders_L (iface);
@@ -1249,6 +1517,17 @@ type_iface_add_prerequisite_W (TypeNode *iface,
     type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
 }
 
+/**
+ * g_type_interface_add_prerequisite:
+ * @interface_type: #GType value of an interface type.
+ * @prerequisite_type: #GType value of an interface or instantiatable type.
+ *
+ * Adds @prerequisite_type to the list of prerequisites of @interface_type.
+ * This means that any type implementing @interface_type must also implement
+ * @prerequisite_type. Prerequisites can be thought of as an alternative to
+ * interface derivation (which GType doesn't support). An interface can have
+ * at most one instantiatable prerequisite type.
+ */
 void
 g_type_interface_add_prerequisite (GType interface_type,
                                   GType prerequisite_type)
@@ -1324,7 +1603,21 @@ g_type_interface_add_prerequisite (GType interface_type,
     }
 }
 
-GType* /* free result */
+/**
+ * g_type_interface_prerequisites:
+ * @interface_type: an interface type
+ * @n_prerequisites: (out) (allow-none): location to return the number
+ *                   of prerequisites, or %NULL
+ *
+ * Returns the prerequisites of an interfaces type.
+ *
+ * Since: 2.2
+ *
+ * Returns: (array length=n_prerequisites) (transfer full): a
+ *          newly-allocated zero-terminated array of #GType containing
+ *          the prerequisites of @interface_type
+ */
+GType*
 g_type_interface_prerequisites (GType  interface_type,
                                guint *n_prerequisites)
 {
@@ -1345,9 +1638,11 @@ g_type_interface_prerequisites (GType  interface_type,
        {
          GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
          TypeNode *node = lookup_type_node_I (prerequisite);
-         if (node->is_instantiatable &&
-             (!inode || type_node_is_a_L (node, inode)))
-           inode = node;
+         if (node->is_instantiatable)
+            {
+              if (!inode || type_node_is_a_L (node, inode))
+               inode = node;
+            }
          else
            types[n++] = NODE_TYPE (node);
        }
@@ -1435,9 +1730,8 @@ type_iface_blow_holder_info_Wm (TypeNode *iface,
       
       G_WRITE_UNLOCK (&type_rw_lock);
       g_type_plugin_unuse (iholder->plugin);
+      type_data_unref_U (iface, FALSE);
       G_WRITE_LOCK (&type_rw_lock);
-      
-      type_data_unref_Wm (iface, FALSE);
     }
 }
 
@@ -1460,6 +1754,7 @@ typedef struct {
   gpointer instance;
   gpointer class;
 } InstanceRealClass;
+
 static gint
 instance_real_class_cmp (gconstpointer p1,
                          gconstpointer p2)
@@ -1470,6 +1765,7 @@ instance_real_class_cmp (gconstpointer p1,
   guint8 *i2 = irc2->instance;
   return G_BSEARCH_ARRAY_CMP (i1, i2);
 }
+
 G_LOCK_DEFINE_STATIC (instance_real_class);
 static GBSearchArray *instance_real_class_bsa = NULL;
 static GBSearchConfig instance_real_class_bconfig = {
@@ -1477,6 +1773,7 @@ static GBSearchConfig instance_real_class_bconfig = {
   instance_real_class_cmp,
   0,
 };
+
 static inline void
 instance_real_class_set (gpointer    instance,
                          GTypeClass *class)
@@ -1490,6 +1787,7 @@ instance_real_class_set (gpointer    instance,
   instance_real_class_bsa = g_bsearch_array_replace (instance_real_class_bsa, &instance_real_class_bconfig, &key);
   G_UNLOCK (instance_real_class);
 }
+
 static inline void
 instance_real_class_remove (gpointer instance)
 {
@@ -1507,17 +1805,41 @@ instance_real_class_remove (gpointer instance)
     }
   G_UNLOCK (instance_real_class);
 }
+
 static inline GTypeClass*
 instance_real_class_get (gpointer instance)
 {
   InstanceRealClass key, *node;
+  GTypeClass *class;
   key.instance = instance;
   G_LOCK (instance_real_class);
   node = instance_real_class_bsa ? g_bsearch_array_lookup (instance_real_class_bsa, &instance_real_class_bconfig, &key) : NULL;
+  class = node ? node->class : NULL;
   G_UNLOCK (instance_real_class);
-  return node ? node->class : NULL;
+  return class;
 }
 
+/**
+ * g_type_create_instance: (skip)
+ * @type: An instantiatable type to create an instance for.
+ *
+ * Creates and initializes an instance of @type if @type is valid and
+ * can be instantiated. The type system only performs basic allocation
+ * and structure setups for instances: actual instance creation should
+ * happen through functions supplied by the type's fundamental type
+ * implementation.  So use of g_type_create_instance() is reserved for
+ * implementators of fundamental types only. E.g. instances of the
+ * #GObject hierarchy should be created via g_object_new() and
+ * <emphasis>never</emphasis> directly through
+ * g_type_create_instance() which doesn't handle things like singleton
+ * objects or object construction.  Note: Do <emphasis>not</emphasis>
+ * use this function, unless you're implementing a fundamental
+ * type. Also language bindings should <emphasis>not</emphasis> use
+ * this function but g_object_new() instead.
+ *
+ * Returns: An allocated and initialized instance, subject to further
+ *  treatment by the fundamental type implementation.
+ */
 GTypeInstance*
 g_type_create_instance (GType type)
 {
@@ -1565,10 +1887,22 @@ g_type_create_instance (GType type)
   instance->g_class = class;
   if (node->data->instance.instance_init)
     node->data->instance.instance_init (instance, class);
-  
+
+  TRACE(GOBJECT_OBJECT_NEW(instance, type));
+
   return instance;
 }
 
+/**
+ * g_type_free_instance:
+ * @instance: an instance of a type.
+ *
+ * Frees an instance of a type, returning it to the instance pool for
+ * the type, if there is one.
+ *
+ * Like g_type_create_instance(), this function is reserved for
+ * implementors of fundamental types.
+ */
 void
 g_type_free_instance (GTypeInstance *instance)
 {
@@ -1763,24 +2097,31 @@ type_class_init_Wm (TypeNode   *node,
 {
   GSList *slist, *init_slist = NULL;
   GTypeClass *class;
+  IFaceEntries *entries;
   IFaceEntry *entry;
   TypeNode *bnode, *pnode;
   guint i;
   
+  /* Accessing data->class will work for instantiable types
+   * too because ClassData is a subset of InstanceData
+   */
   g_assert (node->is_classed && node->data &&
            node->data->class.class_size &&
            !node->data->class.class &&
            node->data->class.init_state == UNINITIALIZED);
-
-  class = g_malloc0 (node->data->class.class_size);
+  if (node->data->class.class_private_size)
+    class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
+  else
+    class = g_malloc0 (node->data->class.class_size);
   node->data->class.class = class;
-  node->data->class.init_state = BASE_CLASS_INIT;
+  g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
   
   if (pclass)
     {
       TypeNode *pnode = lookup_type_node_I (pclass->g_type);
       
       memcpy (class, pclass, pnode->data->class.class_size);
+      memcpy (G_STRUCT_MEMBER_P (class, ALIGN_STRUCT (node->data->class.class_size)), G_STRUCT_MEMBER_P (pclass, ALIGN_STRUCT (pnode->data->class.class_size)), pnode->data->class.class_private_size);
 
       if (node->is_instantiatable)
        {
@@ -1811,7 +2152,7 @@ type_class_init_Wm (TypeNode   *node,
   
   G_WRITE_LOCK (&type_rw_lock);
 
-  node->data->class.init_state = BASE_IFACE_INIT;
+  g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
   
   /* Before we initialize the class, base initialize all interfaces, either
    * from parent, or through our holder info
@@ -1819,39 +2160,42 @@ type_class_init_Wm (TypeNode   *node,
   pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
 
   i = 0;
-  while (i < CLASSED_NODE_N_IFACES (node))
+  while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
+         i < IFACE_ENTRIES_N_ENTRIES (entries))
     {
-      entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
-      while (i < CLASSED_NODE_N_IFACES (node) &&
+      entry = &entries->entry[i];
+      while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
             entry->init_state == IFACE_INIT)
        {
          entry++;
          i++;
        }
 
-      if (i == CLASSED_NODE_N_IFACES (node))
+      if (i == IFACE_ENTRIES_N_ENTRIES (entries))
        break;
 
       if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
        {
          guint j;
+         IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
          
          /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
           * doesn't modify write lock upon FALSE, so entry is still valid; 
           */
          g_assert (pnode != NULL);
-         
-         for (j = 0; j < CLASSED_NODE_N_IFACES (pnode); j++)
-           {
-             IFaceEntry *pentry = CLASSED_NODE_IFACES_ENTRIES (pnode) + j;
-             
-             if (pentry->iface_type == entry->iface_type)
-               {
-                 entry->vtable = pentry->vtable;
-                 entry->init_state = INITIALIZED;
-                 break;
-               }
-           }
+
+         if (pentries)
+           for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
+             {
+               IFaceEntry *pentry = &pentries->entry[j];
+
+               if (pentry->iface_type == entry->iface_type)
+                 {
+                   entry->vtable = pentry->vtable;
+                   entry->init_state = INITIALIZED;
+                   break;
+                 }
+             }
          g_assert (entry->vtable != NULL);
        }
 
@@ -1864,7 +2208,7 @@ type_class_init_Wm (TypeNode   *node,
       i++;
     }
   
-  node->data->class.init_state = CLASS_INIT;
+  g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
   
   G_WRITE_UNLOCK (&type_rw_lock);
 
@@ -1873,7 +2217,7 @@ type_class_init_Wm (TypeNode   *node,
   
   G_WRITE_LOCK (&type_rw_lock);
   
-  node->data->class.init_state = IFACE_INIT;
+  g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
   
   /* finish initializing the interfaces through our holder info.
    * inherited interfaces are already init_state == INITIALIZED, because
@@ -1882,17 +2226,17 @@ type_class_init_Wm (TypeNode   *node,
    * an anchestor type.
    */
   i = 0;
-  while (TRUE)
+  while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
     {
-      entry = &CLASSED_NODE_IFACES_ENTRIES (node)[i];
-      while (i < CLASSED_NODE_N_IFACES (node) &&
+      entry = &entries->entry[i];
+      while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
             entry->init_state == INITIALIZED)
        {
          entry++;
          i++;
        }
 
-      if (i == CLASSED_NODE_N_IFACES (node))
+      if (i == IFACE_ENTRIES_N_ENTRIES (entries))
        break;
 
       type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
@@ -1904,20 +2248,22 @@ type_class_init_Wm (TypeNode   *node,
       i++;
     }
   
-  node->data->class.init_state = INITIALIZED;
+  g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
 }
 
 static void
 type_data_finalize_class_ifaces_Wm (TypeNode *node)
 {
   guint i;
+  IFaceEntries *entries;
 
-  g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
+  g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
 
  reiterate:
-  for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
+  entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
+  for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
     {
-      IFaceEntry *entry = CLASSED_NODE_IFACES_ENTRIES (node) + i;
+      IFaceEntry *entry = &entries->entry[i];
       if (entry->vtable)
        {
           if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
@@ -1944,7 +2290,7 @@ type_data_finalize_class_U (TypeNode  *node,
   GTypeClass *class = cdata->class;
   TypeNode *bnode;
   
-  g_assert (cdata->class && cdata->common.ref_count == 0);
+  g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
   
   if (cdata->class_finalize)
     cdata->class_finalize (class, (gpointer) cdata->class_data);
@@ -1961,17 +2307,15 @@ type_data_finalize_class_U (TypeNode  *node,
 }
 
 static void
-type_data_last_unref_Wm (GType    type,
-                        gboolean uncached)
+type_data_last_unref_Wm (TypeNode *node,
+                        gboolean  uncached)
 {
-  TypeNode *node = lookup_type_node_I (type);
-  
   g_return_if_fail (node != NULL && node->plugin != NULL);
   
-  if (!node->data || node->data->common.ref_count == 0)
+  if (!node->data || NODE_REFCOUNT (node) == 0)
     {
       g_warning ("cannot drop last reference to unreferenced type `%s'",
-                type_descriptive_name_I (type));
+                NODE_NAME (node));
       return;
     }
 
@@ -1991,7 +2335,7 @@ type_data_last_unref_Wm (GType    type,
          G_READ_UNLOCK (&type_rw_lock);
          need_break = cache_func (cache_data, node->data->class.class);
          G_READ_LOCK (&type_rw_lock);
-         if (!node->data || node->data->common.ref_count == 0)
+         if (!node->data || NODE_REFCOUNT (node) == 0)
            INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
          if (need_break)
            break;
@@ -2000,15 +2344,12 @@ type_data_last_unref_Wm (GType    type,
       G_WRITE_LOCK (&type_rw_lock);
     }
   
-  if (node->data->common.ref_count > 1)        /* may have been re-referenced meanwhile */
-    node->data->common.ref_count -= 1;
-  else
+  /* may have been re-referenced meanwhile */
+  if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
     {
       GType ptype = NODE_PARENT_TYPE (node);
       TypeData *tdata;
       
-      node->data->common.ref_count = 0;
-      
       if (node->is_instantiatable)
        {
          /* destroy node->data->instance.mem_chunk */
@@ -2017,7 +2358,7 @@ type_data_last_unref_Wm (GType    type,
       tdata = node->data;
       if (node->is_classed && tdata->class.class)
        {
-         if (CLASSED_NODE_N_IFACES (node))
+         if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
            type_data_finalize_class_ifaces_Wm (node);
          node->mutatable_check_cache = FALSE;
          node->data = NULL;
@@ -2053,12 +2394,55 @@ type_data_last_unref_Wm (GType    type,
       
       G_WRITE_UNLOCK (&type_rw_lock);
       g_type_plugin_unuse (node->plugin);
-      G_WRITE_LOCK (&type_rw_lock);
       if (ptype)
-       type_data_unref_Wm (lookup_type_node_I (ptype), FALSE);
+       type_data_unref_U (lookup_type_node_I (ptype), FALSE);
+      G_WRITE_LOCK (&type_rw_lock);
+    }
+}
+
+static inline void
+type_data_unref_U (TypeNode *node,
+                   gboolean  uncached)
+{
+  guint current;
+
+  do {
+    current = NODE_REFCOUNT (node);
+
+    if (current <= 1)
+    {
+      if (!node->plugin)
+       {
+         g_warning ("static type `%s' unreferenced too often",
+                    NODE_NAME (node));
+         return;
+       }
+
+      g_assert (current > 0);
+
+      g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
+      G_WRITE_LOCK (&type_rw_lock);
+      type_data_last_unref_Wm (node, uncached);
+      G_WRITE_UNLOCK (&type_rw_lock);
+      g_static_rec_mutex_unlock (&class_init_rec_mutex);
+      return;
     }
+  } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
 }
 
+/**
+ * g_type_add_class_cache_func: (skip)
+ * @cache_data: data to be passed to @cache_func
+ * @cache_func: a #GTypeClassCacheFunc
+ *
+ * Adds a #GTypeClassCacheFunc to be called before the reference count of a
+ * class goes from one to zero. This can be used to prevent premature class
+ * destruction. All installed #GTypeClassCacheFunc functions will be chained
+ * until one of them returns %TRUE. The functions have to check the class id
+ * passed in to figure whether they actually want to cache the class of this
+ * type, since all classes are routed through the same #GTypeClassCacheFunc
+ * chain.
+ */
 void
 g_type_add_class_cache_func (gpointer            cache_data,
                             GTypeClassCacheFunc cache_func)
@@ -2075,6 +2459,15 @@ g_type_add_class_cache_func (gpointer            cache_data,
   G_WRITE_UNLOCK (&type_rw_lock);
 }
 
+/**
+ * g_type_remove_class_cache_func: (skip)
+ * @cache_data: data that was given when adding @cache_func
+ * @cache_func: a #GTypeClassCacheFunc
+ *
+ * Removes a previously installed #GTypeClassCacheFunc. The cache
+ * maintained by @cache_func has to be empty when calling
+ * g_type_remove_class_cache_func() to avoid leaks.
+ */
 void
 g_type_remove_class_cache_func (gpointer            cache_data,
                                GTypeClassCacheFunc cache_func)
@@ -2105,6 +2498,24 @@ g_type_remove_class_cache_func (gpointer            cache_data,
 }
 
 
+/**
+ * g_type_add_interface_check: (skip)
+ * @check_data: data to pass to @check_func
+ * @check_func: function to be called after each interface
+ *              is initialized.
+ *
+ * Adds a function to be called after an interface vtable is
+ * initialized for any class (i.e. after the @interface_init member of
+ * #GInterfaceInfo has been called).
+ *
+ * This function is useful when you want to check an invariant that
+ * depends on the interfaces of a class. For instance, the
+ * implementation of #GObject uses this facility to check that an
+ * object implements all of the properties that are defined on its
+ * interfaces.
+ *
+ * Since: 2.4
+ */
 void
 g_type_add_interface_check (gpointer               check_data,
                            GTypeInterfaceCheckFunc check_func)
@@ -2121,6 +2532,16 @@ g_type_add_interface_check (gpointer                 check_data,
   G_WRITE_UNLOCK (&type_rw_lock);
 }
 
+/**
+ * g_type_remove_interface_check: (skip)
+ * @check_data: callback data passed to g_type_add_interface_check()
+ * @check_func: callback function passed to g_type_add_interface_check()
+ *
+ * Removes an interface check function added with
+ * g_type_add_interface_check().
+ *
+ * Since: 2.4
+ */
 void
 g_type_remove_interface_check (gpointer                check_data,
                               GTypeInterfaceCheckFunc check_func)
@@ -2151,6 +2572,23 @@ g_type_remove_interface_check (gpointer                check_data,
 }
 
 /* --- type registration --- */
+/**
+ * g_type_register_fundamental:
+ * @type_id: A predefined type identifier.
+ * @type_name: 0-terminated string used as the name of the new type.
+ * @info: The #GTypeInfo structure for this type.
+ * @finfo: The #GTypeFundamentalInfo structure for this type.
+ * @flags: Bitwise combination of #GTypeFlags values.
+ *
+ * Registers @type_id as the predefined identifier and @type_name as the
+ * name of a fundamental type.  The type system uses the information
+ * contained in the #GTypeInfo structure pointed to by @info and the
+ * #GTypeFundamentalInfo structure pointed to by @finfo to manage the
+ * type and its instances.  The value of @flags determines additional
+ * characteristics of the fundamental type.
+ *
+ * Returns: The predefined type identifier.
+ */
 GType
 g_type_register_fundamental (GType                       type_id,
                             const gchar                *type_name,
@@ -2160,7 +2598,7 @@ g_type_register_fundamental (GType                       type_id,
 {
   TypeNode *node;
   
-  g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
+  g_return_val_if_type_system_uninitialized (0);
   g_return_val_if_fail (type_id > 0, 0);
   g_return_val_if_fail (type_name != NULL, 0);
   g_return_val_if_fail (info != NULL, 0);
@@ -2171,7 +2609,7 @@ g_type_register_fundamental (GType                       type_id,
   if ((type_id & TYPE_ID_MASK) ||
       type_id > G_TYPE_FUNDAMENTAL_MAX)
     {
-      g_warning ("attempt to register fundamental type `%s' with invalid type id (%lu)",
+      g_warning ("attempt to register fundamental type `%s' with invalid type id (%" G_GSIZE_FORMAT ")",
                 type_name,
                 type_id);
       return 0;
@@ -2203,6 +2641,25 @@ g_type_register_fundamental (GType                       type_id,
   return NODE_TYPE (node);
 }
 
+/**
+ * g_type_register_static_simple: (skip)
+ * @parent_type: Type from which this type will be derived.
+ * @type_name: 0-terminated string used as the name of the new type.
+ * @class_size: Size of the class structure (see #GTypeInfo)
+ * @class_init: Location of the class initialization function (see #GTypeInfo)
+ * @instance_size: Size of the instance structure (see #GTypeInfo)
+ * @instance_init: Location of the instance initialization function (see #GTypeInfo)
+ * @flags: Bitwise combination of #GTypeFlags values.
+ *
+ * Registers @type_name as the name of a new static type derived from
+ * @parent_type.  The value of @flags determines the nature (e.g.
+ * abstract or not) of the type. It works by filling a #GTypeInfo
+ * struct and calling g_type_register_static().
+ *
+ * Since: 2.12
+ *
+ * Returns: The new type identifier.
+ */
 GType
 g_type_register_static_simple (GType             parent_type,
                               const gchar      *type_name,
@@ -2228,6 +2685,21 @@ g_type_register_static_simple (GType             parent_type,
   return g_type_register_static (parent_type, type_name, &info, flags);
 }
 
+/**
+ * g_type_register_static:
+ * @parent_type: Type from which this type will be derived.
+ * @type_name: 0-terminated string used as the name of the new type.
+ * @info: The #GTypeInfo structure for this type.
+ * @flags: Bitwise combination of #GTypeFlags values.
+ *
+ * Registers @type_name as the name of a new static type derived from
+ * @parent_type.  The type system uses the information contained in the
+ * #GTypeInfo structure pointed to by @info to manage the type and its
+ * instances (if not abstract).  The value of @flags determines the nature
+ * (e.g. abstract or not) of the type.
+ *
+ * Returns: The new type identifier.
+ */
 GType
 g_type_register_static (GType            parent_type,
                        const gchar     *type_name,
@@ -2237,7 +2709,7 @@ g_type_register_static (GType            parent_type,
   TypeNode *pnode, *node;
   GType type = 0;
   
-  g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
+  g_return_val_if_type_system_uninitialized (0);
   g_return_val_if_fail (parent_type > 0, 0);
   g_return_val_if_fail (type_name != NULL, 0);
   g_return_val_if_fail (info != NULL, 0);
@@ -2268,6 +2740,21 @@ g_type_register_static (GType            parent_type,
   return type;
 }
 
+/**
+ * g_type_register_dynamic:
+ * @parent_type: Type from which this type will be derived.
+ * @type_name: 0-terminated string used as the name of the new type.
+ * @plugin: The #GTypePlugin structure to retrieve the #GTypeInfo from.
+ * @flags: Bitwise combination of #GTypeFlags values.
+ *
+ * Registers @type_name as the name of a new dynamic type derived from
+ * @parent_type.  The type system uses the information contained in the
+ * #GTypePlugin structure pointed to by @plugin to manage the type and its
+ * instances (if not abstract).  The value of @flags determines the nature
+ * (e.g. abstract or not) of the type.
+ *
+ * Returns: The new type identifier or #G_TYPE_INVALID if registration failed.
+ */
 GType
 g_type_register_dynamic (GType        parent_type,
                         const gchar *type_name,
@@ -2277,7 +2764,7 @@ g_type_register_dynamic (GType        parent_type,
   TypeNode *pnode, *node;
   GType type;
   
-  g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, 0);
+  g_return_val_if_type_system_uninitialized (0);
   g_return_val_if_fail (parent_type > 0, 0);
   g_return_val_if_fail (type_name != NULL, 0);
   g_return_val_if_fail (plugin != NULL, 0);
@@ -2297,6 +2784,17 @@ g_type_register_dynamic (GType        parent_type,
   return type;
 }
 
+/**
+ * g_type_add_interface_static:
+ * @instance_type: #GType value of an instantiable type.
+ * @interface_type: #GType value of an interface type.
+ * @info: The #GInterfaceInfo structure for this
+ *        (@instance_type, @interface_type) combination.
+ *
+ * Adds the static @interface_type to @instantiable_type.  The information
+ * contained in the #GTypeInterfaceInfo structure pointed to by @info
+ * is used to manage the relationship.
+ */
 void
 g_type_add_interface_static (GType                 instance_type,
                             GType                 interface_type,
@@ -2305,96 +2803,138 @@ g_type_add_interface_static (GType                 instance_type,
   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
-  
+
+  /* we only need to lock class_init_rec_mutex if instance_type already has its
+   * class initialized, however this function is rarely enough called to take
+   * the simple route and always acquire class_init_rec_mutex.
+   */
+  g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
   G_WRITE_LOCK (&type_rw_lock);
   if (check_add_interface_L (instance_type, interface_type))
     {
       TypeNode *node = lookup_type_node_I (instance_type);
       TypeNode *iface = lookup_type_node_I (interface_type);
-      
       if (check_interface_info_I (iface, NODE_TYPE (node), info))
         type_add_interface_Wm (node, iface, info, NULL);
     }
   G_WRITE_UNLOCK (&type_rw_lock);
+  g_static_rec_mutex_unlock (&class_init_rec_mutex);
 }
 
+/**
+ * g_type_add_interface_dynamic:
+ * @instance_type: the #GType value of an instantiable type.
+ * @interface_type: the #GType value of an interface type.
+ * @plugin: the #GTypePlugin structure to retrieve the #GInterfaceInfo from.
+ *
+ * Adds the dynamic @interface_type to @instantiable_type. The information
+ * contained in the #GTypePlugin structure pointed to by @plugin
+ * is used to manage the relationship.
+ */
 void
 g_type_add_interface_dynamic (GType        instance_type,
                              GType        interface_type,
                              GTypePlugin *plugin)
 {
   TypeNode *node;
-  
   /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
   g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
   g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
-  
+
   node = lookup_type_node_I (instance_type);
   if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
     return;
-  
+
+  /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
+  g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
   G_WRITE_LOCK (&type_rw_lock);
   if (check_add_interface_L (instance_type, interface_type))
     {
       TypeNode *iface = lookup_type_node_I (interface_type);
-      
       type_add_interface_Wm (node, iface, NULL, plugin);
     }
   G_WRITE_UNLOCK (&type_rw_lock);
+  g_static_rec_mutex_unlock (&class_init_rec_mutex);
 }
 
 
 /* --- public API functions --- */
+/**
+ * g_type_class_ref:
+ * @type: Type ID of a classed type.
+ *
+ * Increments the reference count of the class structure belonging to
+ * @type. This function will demand-create the class if it doesn't
+ * exist already.
+ *
+ * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
+ *  structure for the given type ID.
+ */
 gpointer
 g_type_class_ref (GType type)
 {
   TypeNode *node;
-  
-  /* optimize for common code path
-   */
-  G_WRITE_LOCK (&type_rw_lock);
+  GType ptype;
+  gboolean holds_ref;
+  GTypeClass *pclass;
+
+  /* optimize for common code path */
   node = lookup_type_node_I (type);
-  if (node && node->is_classed && node->data &&
-      node->data->class.class && node->data->common.ref_count > 0)
-    {
-      type_data_ref_Wm (node);
-      G_WRITE_UNLOCK (&type_rw_lock);
-      
-      return node->data->class.class;
-    }
-  
-  if (!node || !node->is_classed ||
-      (node->data && node->data->common.ref_count < 1))
+  if (!node || !node->is_classed)
     {
-      G_WRITE_UNLOCK (&type_rw_lock);
       g_warning ("cannot retrieve class for invalid (unclassed) type `%s'",
                 type_descriptive_name_I (type));
       return NULL;
     }
-  
-  type_data_ref_Wm (node);
-  
-  if (!node->data->class.class)
+
+  if (G_LIKELY (type_data_ref_U (node)))
     {
-      GType ptype = NODE_PARENT_TYPE (node);
-      GTypeClass *pclass = NULL;
-      
-      if (ptype)
-       {
-         G_WRITE_UNLOCK (&type_rw_lock);
-         pclass = g_type_class_ref (ptype);
-         if (node->data->class.class)
-            INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
-         G_WRITE_LOCK (&type_rw_lock);
-       }
-      
-      type_class_init_Wm (node, pclass);
+      if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
+        return node->data->class.class;
+      holds_ref = TRUE;
     }
-  G_WRITE_UNLOCK (&type_rw_lock);
+  else
+    holds_ref = FALSE;
   
+  /* here, we either have node->data->class.class == NULL, or a recursive
+   * call to g_type_class_ref() with a partly initialized class, or
+   * node->data->class.init_state == INITIALIZED, because any
+   * concurrently running initialization was guarded by class_init_rec_mutex.
+   */
+  g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
+
+  /* we need an initialized parent class for initializing derived classes */
+  ptype = NODE_PARENT_TYPE (node);
+  pclass = ptype ? g_type_class_ref (ptype) : NULL;
+
+  G_WRITE_LOCK (&type_rw_lock);
+
+  if (!holds_ref)
+    type_data_ref_Wm (node);
+
+  if (!node->data->class.class) /* class uninitialized */
+    type_class_init_Wm (node, pclass);
+
+  G_WRITE_UNLOCK (&type_rw_lock);
+
+  if (pclass)
+    g_type_class_unref (pclass);
+
+  g_static_rec_mutex_unlock (&class_init_rec_mutex);
+
   return node->data->class.class;
 }
 
+/**
+ * g_type_class_unref:
+ * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
+ *  unreference.
+ *
+ * Decrements the reference count of the class structure being passed in.
+ * Once the last reference count of a class has been released, classes
+ * may be finalized by the type system, so further dereferencing of a
+ * class pointer after g_type_class_unref() are invalid.
+ */
 void
 g_type_class_unref (gpointer g_class)
 {
@@ -2404,16 +2944,23 @@ g_type_class_unref (gpointer g_class)
   g_return_if_fail (g_class != NULL);
   
   node = lookup_type_node_I (class->g_type);
-  G_WRITE_LOCK (&type_rw_lock);
-  if (node && node->is_classed && node->data &&
-      node->data->class.class == class && node->data->common.ref_count > 0)
-    type_data_unref_Wm (node, FALSE);
+  if (node && node->is_classed && NODE_REFCOUNT (node))
+    type_data_unref_U (node, FALSE);
   else
     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
               type_descriptive_name_I (class->g_type));
-  G_WRITE_UNLOCK (&type_rw_lock);
 }
 
+/**
+ * g_type_class_unref_uncached: (skip)
+ * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
+ *  unreference.
+ *
+ * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
+ * implementations. It unreferences a class without consulting the chain
+ * of #GTypeClassCacheFunc<!-- -->s, avoiding the recursion which would occur
+ * otherwise.
+ */
 void
 g_type_class_unref_uncached (gpointer g_class)
 {
@@ -2422,17 +2969,27 @@ g_type_class_unref_uncached (gpointer g_class)
   
   g_return_if_fail (g_class != NULL);
   
-  G_WRITE_LOCK (&type_rw_lock);
   node = lookup_type_node_I (class->g_type);
-  if (node && node->is_classed && node->data &&
-      node->data->class.class == class && node->data->common.ref_count > 0)
-    type_data_unref_Wm (node, TRUE);
+  if (node && node->is_classed && NODE_REFCOUNT (node))
+    type_data_unref_U (node, TRUE);
   else
     g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
               type_descriptive_name_I (class->g_type));
-  G_WRITE_UNLOCK (&type_rw_lock);
 }
 
+/**
+ * g_type_class_peek:
+ * @type: Type ID of a classed type.
+ *
+ * This function is essentially the same as g_type_class_ref(), except that
+ * the classes reference count isn't incremented. As a consequence, this function
+ * may return %NULL if the class of the type passed in does not currently
+ * exist (hasn't been referenced before).
+ *
+ * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
+ *  structure for the given type ID or %NULL if the class does not
+ *  currently exist.
+ */
 gpointer
 g_type_class_peek (GType type)
 {
@@ -2440,16 +2997,28 @@ g_type_class_peek (GType type)
   gpointer class;
   
   node = lookup_type_node_I (type);
-  G_READ_LOCK (&type_rw_lock);
-  if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
+  if (node && node->is_classed && NODE_REFCOUNT (node) &&
+      g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
+    /* ref_count _may_ be 0 */
     class = node->data->class.class;
   else
     class = NULL;
-  G_READ_UNLOCK (&type_rw_lock);
   
   return class;
 }
 
+/**
+ * g_type_class_peek_static:
+ * @type: Type ID of a classed type.
+ *
+ * A more efficient version of g_type_class_peek() which works only for
+ * static types.
+ * 
+ * Since: 2.4
+ * Returns: (type GObject.TypeClass) (transfer none): The #GTypeClass
+ *  structure for the given type ID or %NULL if the class does not
+ *  currently exist or is dynamically loaded.
+ */
 gpointer
 g_type_class_peek_static (GType type)
 {
@@ -2457,18 +3026,36 @@ g_type_class_peek_static (GType type)
   gpointer class;
   
   node = lookup_type_node_I (type);
-  G_READ_LOCK (&type_rw_lock);
-  if (node && node->is_classed && node->data &&
+  if (node && node->is_classed && NODE_REFCOUNT (node) &&
       /* peek only static types: */ node->plugin == NULL &&
-      node->data->class.class) /* common.ref_count _may_ be 0 */
+      g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
+    /* ref_count _may_ be 0 */
     class = node->data->class.class;
   else
     class = NULL;
-  G_READ_UNLOCK (&type_rw_lock);
   
   return class;
 }
 
+/**
+ * g_type_class_peek_parent:
+ * @g_class: (type GObject.TypeClass): The #GTypeClass structure to
+ *  retrieve the parent class for.
+ *
+ * This is a convenience function often needed in class initializers.
+ * It returns the class structure of the immediate parent type of the
+ * class passed in.  Since derived classes hold a reference count on
+ * their parent classes as long as they are instantiated, the returned
+ * class will always exist. This function is essentially equivalent
+ * to:
+ *
+ * <programlisting>
+ * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)));
+ * </programlisting>
+ *
+ * Returns: (type GObject.TypeClass) (transfer none): The parent class
+ *  of @g_class.
+ */
 gpointer
 g_type_class_peek_parent (gpointer g_class)
 {
@@ -2493,6 +3080,18 @@ g_type_class_peek_parent (gpointer g_class)
   return class;
 }
 
+/**
+ * g_type_interface_peek:
+ * @instance_class: (type GObject.TypeClass): A #GTypeClass structure.
+ * @iface_type: An interface ID which this class conforms to.
+ *
+ * Returns the #GTypeInterface structure of an interface to which the
+ * passed in class conforms.
+ *
+ * Returns: (type GObject.TypeInterface) (transfer none): The GTypeInterface
+ *  structure of iface_type if implemented by @instance_class, %NULL
+ *  otherwise
+ */
 gpointer
 g_type_interface_peek (gpointer instance_class,
                       GType    iface_type)
@@ -2507,23 +3106,27 @@ g_type_interface_peek (gpointer instance_class,
   node = lookup_type_node_I (class->g_type);
   iface = lookup_type_node_I (iface_type);
   if (node && node->is_instantiatable && iface)
-    {
-      IFaceEntry *entry;
-      
-      G_READ_LOCK (&type_rw_lock);
-      
-      entry = type_lookup_iface_entry_L (node, iface);
-      if (entry && entry->vtable)      /* entry is relocatable */
-       vtable = entry->vtable;
-      
-      G_READ_UNLOCK (&type_rw_lock);
-    }
+    type_lookup_iface_vtable_I (node, iface, &vtable);
   else
     g_warning (G_STRLOC ": invalid class pointer `%p'", class);
   
   return vtable;
 }
 
+/**
+ * g_type_interface_peek_parent:
+ * @g_iface: (type GObject.TypeInterface): A #GTypeInterface structure.
+ *
+ * Returns the corresponding #GTypeInterface structure of the parent type
+ * of the instance type to which @g_iface belongs. This is useful when
+ * deriving the implementation of an interface from the parent type and
+ * then possibly overriding some methods.
+ *
+ * Returns: (transfer none) (type GObject.TypeInterface): The
+ *  corresponding #GTypeInterface structure of the parent type of the
+ *  instance type to which @g_iface belongs, or %NULL if the parent
+ *  type doesn't conform to the interface.
+ */
 gpointer
 g_type_interface_peek_parent (gpointer g_iface)
 {
@@ -2539,49 +3142,85 @@ g_type_interface_peek_parent (gpointer g_iface)
   if (node)
     node = lookup_type_node_I (NODE_PARENT_TYPE (node));
   if (node && node->is_instantiatable && iface)
-    {
-      IFaceEntry *entry;
-      
-      G_READ_LOCK (&type_rw_lock);
-      
-      entry = type_lookup_iface_entry_L (node, iface);
-      if (entry && entry->vtable)      /* entry is relocatable */
-       vtable = entry->vtable;
-      
-      G_READ_UNLOCK (&type_rw_lock);
-    }
+    type_lookup_iface_vtable_I (node, iface, &vtable);
   else if (node)
     g_warning (G_STRLOC ": invalid interface pointer `%p'", g_iface);
   
   return vtable;
 }
 
+/**
+ * g_type_default_interface_ref:
+ * @g_type: an interface type
+ *
+ * Increments the reference count for the interface type @g_type,
+ * and returns the default interface vtable for the type.
+ *
+ * If the type is not currently in use, then the default vtable
+ * for the type will be created and initalized by calling
+ * the base interface init and default vtable init functions for
+ * the type (the @<structfield>base_init</structfield>
+ * and <structfield>class_init</structfield> members of #GTypeInfo).
+ * Calling g_type_default_interface_ref() is useful when you
+ * want to make sure that signals and properties for an interface
+ * have been installed.
+ *
+ * Since: 2.4
+ *
+ * Returns: (type GObject.TypeInterface) (transfer none): the default
+ *  vtable for the interface; call g_type_default_interface_unref()
+ *  when you are done using the interface.
+ */
 gpointer
 g_type_default_interface_ref (GType g_type)
 {
   TypeNode *node;
-  
+  gpointer dflt_vtable;
+
   G_WRITE_LOCK (&type_rw_lock);
-  
+
   node = lookup_type_node_I (g_type);
   if (!node || !NODE_IS_IFACE (node) ||
-      (node->data && node->data->common.ref_count < 1))
+      (node->data && NODE_REFCOUNT (node) == 0))
     {
       G_WRITE_UNLOCK (&type_rw_lock);
       g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
                 type_descriptive_name_I (g_type));
       return NULL;
     }
-  
-  type_data_ref_Wm (node);
 
-  type_iface_ensure_dflt_vtable_Wm (node);
+  if (!node->data || !node->data->iface.dflt_vtable)
+    {
+      G_WRITE_UNLOCK (&type_rw_lock);
+      g_static_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
+      G_WRITE_LOCK (&type_rw_lock);
+      node = lookup_type_node_I (g_type);
+      type_data_ref_Wm (node);
+      type_iface_ensure_dflt_vtable_Wm (node);
+      g_static_rec_mutex_unlock (&class_init_rec_mutex);
+    }
+  else
+    type_data_ref_Wm (node); /* ref_count >= 1 already */
 
+  dflt_vtable = node->data->iface.dflt_vtable;
   G_WRITE_UNLOCK (&type_rw_lock);
-  
-  return node->data->iface.dflt_vtable;
+
+  return dflt_vtable;
 }
 
+/**
+ * g_type_default_interface_peek:
+ * @g_type: an interface type
+ *
+ * If the interface type @g_type is currently in use, returns its
+ * default interface vtable.
+ *
+ * Since: 2.4
+ *
+ * Returns: (type GObject.TypeInterface) (transfer none): the default
+ *  vtable for the interface, or %NULL if the type is not currently in
+ *  use.
+ */
 gpointer
 g_type_default_interface_peek (GType g_type)
 {
@@ -2589,16 +3228,29 @@ g_type_default_interface_peek (GType g_type)
   gpointer vtable;
   
   node = lookup_type_node_I (g_type);
-  G_READ_LOCK (&type_rw_lock);
-  if (node && NODE_IS_IFACE (node) && node->data && node->data->iface.dflt_vtable)
+  if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
     vtable = node->data->iface.dflt_vtable;
   else
     vtable = NULL;
-  G_READ_UNLOCK (&type_rw_lock);
   
   return vtable;
 }
 
+/**
+ * g_type_default_interface_unref:
+ * @g_iface: (type GObject.TypeInterface): the default vtable
+ *  structure for a interface, as returned by
+ *  g_type_default_interface_ref()
+ *
+ * Decrements the reference count for the type corresponding to the
+ * interface default vtable @g_iface. If the type is dynamic, then
+ * when no one is using the interface and all references have
+ * been released, the finalize function for the interface's default
+ * vtable (the <structfield>class_finalize</structfield> member of
+ * #GTypeInfo) will be called.
+ *
+ * Since: 2.4
+ */
 void
 g_type_default_interface_unref (gpointer g_iface)
 {
@@ -2608,29 +3260,45 @@ g_type_default_interface_unref (gpointer g_iface)
   g_return_if_fail (g_iface != NULL);
   
   node = lookup_type_node_I (vtable->g_type);
-  G_WRITE_LOCK (&type_rw_lock);
-  if (node && NODE_IS_IFACE (node) &&
-      node->data->iface.dflt_vtable == g_iface &&
-      node->data->common.ref_count > 0)
-    type_data_unref_Wm (node, FALSE);
+  if (node && NODE_IS_IFACE (node))
+    type_data_unref_U (node, FALSE);
   else
     g_warning ("cannot unreference invalid interface default vtable for '%s'",
               type_descriptive_name_I (vtable->g_type));
-  G_WRITE_UNLOCK (&type_rw_lock);
 }
 
+/**
+ * g_type_name:
+ * @type: Type to return name for.
+ *
+ * Get the unique name that is assigned to a type ID.  Note that this
+ * function (like all other GType API) cannot cope with invalid type
+ * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
+ * other validly registered type ID, but randomized type IDs should
+ * not be passed in and will most likely lead to a crash.
+ *
+ * Returns: Static type name or %NULL.
+ */
 G_CONST_RETURN gchar*
 g_type_name (GType type)
 {
   TypeNode *node;
   
-  g_return_val_if_uninitialized (static_quark_type_flags, g_type_init, NULL);
+  g_return_val_if_type_system_uninitialized (NULL);
   
   node = lookup_type_node_I (type);
   
   return node ? NODE_NAME (node) : NULL;
 }
 
+/**
+ * g_type_qname:
+ * @type: Type to return quark of type name for.
+ *
+ * Get the corresponding quark of the type IDs name.
+ *
+ * Returns: The type names quark or 0.
+ */
 GQuark
 g_type_qname (GType type)
 {
@@ -2641,6 +3309,17 @@ g_type_qname (GType type)
   return node ? node->qname : 0;
 }
 
+/**
+ * g_type_from_name:
+ * @name: Type name to lookup.
+ *
+ * Lookup the type ID from a given type name, returning 0 if no type
+ * has been registered under this name (this is the preferred method
+ * to find out by name whether a specific type has been registered
+ * yet).
+ *
+ * Returns: Corresponding type ID or 0.
+ */
 GType
 g_type_from_name (const gchar *name)
 {
@@ -2660,6 +3339,15 @@ g_type_from_name (const gchar *name)
   return type;
 }
 
+/**
+ * g_type_parent:
+ * @type: The derived type.
+ *
+ * Return the direct parent type of the passed in type.  If the passed
+ * in type has no parent, i.e. is a fundamental type, 0 is returned.
+ *
+ * Returns: The parent type.
+ */
 GType
 g_type_parent (GType type)
 {
@@ -2670,6 +3358,15 @@ g_type_parent (GType type)
   return node ? NODE_PARENT_TYPE (node) : 0;
 }
 
+/**
+ * g_type_depth:
+ * @type: A #GType value.
+ *
+ * Returns the length of the ancestry of the passed in type. This
+ * includes the type itself, so that e.g. a fundamental type has depth 1.
+ *
+ * Returns: The depth of @type.
+ */
 guint
 g_type_depth (GType type)
 {
@@ -2680,6 +3377,21 @@ g_type_depth (GType type)
   return node ? node->n_supers + 1 : 0;
 }
 
+/**
+ * g_type_next_base:
+ * @leaf_type: Descendant of @root_type and the type to be returned.
+ * @root_type: Immediate parent of the returned type.
+ *
+ * Given a @leaf_type and a @root_type which is contained in its
+ * anchestry, return the type that @root_type is the immediate parent
+ * of.  In other words, this function determines the type that is
+ * derived directly from @root_type which is also a base class of
+ * @leaf_type.  Given a root type and a leaf type, this function can
+ * be used to determine the types and order in which the leaf type is
+ * descended from the root type.
+ *
+ * Returns: Immediate child of @root_type and anchestor of @leaf_type.
+ */
 GType
 g_type_next_base (GType type,
                  GType base_type)
@@ -2713,21 +3425,33 @@ type_node_check_conformities_UorL (TypeNode *node,
                                   gboolean  have_lock)
 {
   gboolean match;
-  
+
   if (/* support_inheritance && */
       NODE_IS_ANCESTOR (iface_node, node))
     return TRUE;
-  
+
   support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
   support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
   match = FALSE;
-  if (support_interfaces || support_prerequisites)
+  if (support_interfaces)
+    {
+      if (have_lock)
+       {
+         if (type_lookup_iface_entry_L (node, iface_node))
+           match = TRUE;
+       }
+      else
+       {
+         if (type_lookup_iface_vtable_I (node, iface_node, NULL))
+           match = TRUE;
+       }
+    }
+  if (!match &&
+      support_prerequisites)
     {
       if (!have_lock)
        G_READ_LOCK (&type_rw_lock);
-      if (support_interfaces && type_lookup_iface_entry_L (node, iface_node))
-       match = TRUE;
-      else if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
+      if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
        match = TRUE;
       if (!have_lock)
        G_READ_UNLOCK (&type_rw_lock);
@@ -2751,6 +3475,17 @@ type_node_conforms_to_U (TypeNode *node,
   return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
 }
 
+/**
+ * g_type_is_a:
+ * @type: Type to check anchestry for.
+ * @is_a_type: Possible anchestor of @type or interface @type could conform to.
+ *
+ * If @is_a_type is a derivable type, check whether @type is a
+ * descendant of @is_a_type.  If @is_a_type is an interface, check
+ * whether @type conforms to it.
+ *
+ * Returns: %TRUE if @type is_a @is_a_type holds true.
+ */
 gboolean
 g_type_is_a (GType type,
             GType iface_type)
@@ -2765,7 +3500,19 @@ g_type_is_a (GType type,
   return is_a;
 }
 
-GType* /* free result */
+/**
+ * g_type_children:
+ * @type: The parent type.
+ * @n_children: (out) (allow-none): Optional #guint pointer to contain
+ *              the number of child types.
+ *
+ * Return a newly allocated and 0-terminated array of type IDs, listing the
+ * child types of @type. The return value has to be g_free()ed after use.
+ *
+ * Returns: (array length=n_children) (transfer full): Newly allocated
+ *          and 0-terminated array of child types.
+ */
+GType*
 g_type_children (GType  type,
                 guint *n_children)
 {
@@ -2796,7 +3543,20 @@ g_type_children (GType  type,
     }
 }
 
-GType* /* free result */
+/**
+ * g_type_interfaces:
+ * @type: The type to list interface types for.
+ * @n_interfaces: (out) (allow-none): Optional #guint pointer to
+ *                contain the number of interface types.
+ *
+ * Return a newly allocated and 0-terminated array of type IDs, listing the
+ * interface types that @type conforms to. The return value has to be
+ * g_free()ed after use.
+ *
+ * Returns: (array length=n_interfaces) (transfer full): Newly
+ *          allocated and 0-terminated array of interface types.
+ */
+GType*
 g_type_interfaces (GType  type,
                   guint *n_interfaces)
 {
@@ -2805,17 +3565,27 @@ g_type_interfaces (GType  type,
   node = lookup_type_node_I (type);
   if (node && node->is_instantiatable)
     {
+      IFaceEntries *entries;
       GType *ifaces;
       guint i;
       
       G_READ_LOCK (&type_rw_lock);
-      ifaces = g_new (GType, CLASSED_NODE_N_IFACES (node) + 1);
-      for (i = 0; i < CLASSED_NODE_N_IFACES (node); i++)
-       ifaces[i] = CLASSED_NODE_IFACES_ENTRIES (node)[i].iface_type;
+      entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
+      if (entries)
+       {
+         ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
+         for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
+           ifaces[i] = entries->entry[i].iface_type;
+       }
+      else
+       {
+         ifaces = g_new (GType, 1);
+         i = 0;
+       }
       ifaces[i] = 0;
       
       if (n_interfaces)
-       *n_interfaces = CLASSED_NODE_N_IFACES (node);
+       *n_interfaces = i;
       G_READ_UNLOCK (&type_rw_lock);
       
       return ifaces;
@@ -2874,6 +3644,16 @@ type_get_qdata_L (TypeNode *node,
   return NULL;
 }
 
+/**
+ * g_type_get_qdata:
+ * @type: a #GType
+ * @quark: a #GQuark id to identify the data
+ *
+ * Obtains data which has previously been attached to @type
+ * with g_type_set_qdata().
+ *
+ * Returns: (transfer none): the data, or %NULL if no data was found
+ */
 gpointer
 g_type_get_qdata (GType  type,
                  GQuark quark)
@@ -2931,6 +3711,14 @@ type_set_qdata_W (TypeNode *node,
   qdata[i].data = data;
 }
 
+/**
+ * g_type_set_qdata:
+ * @type: a #GType
+ * @quark: a #GQuark id to identify the data
+ * @data: the data
+ *
+ * Attaches arbitrary data to a type.
+ */
 void
 g_type_set_qdata (GType    type,
                  GQuark   quark,
@@ -2967,6 +3755,19 @@ type_add_flags_W (TypeNode  *node,
   type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
 }
 
+/**
+ * g_type_query:
+ * @type: the #GType value of a static, classed type.
+ * @query: (out caller-allocates): A user provided structure that is
+ *         filled in with constant values upon success.
+ *
+ * Queries the type system for information about a specific type.
+ * This function will fill in a user-provided structure to hold
+ * type-specific information. If an invalid #GType is passed in, the
+ * @type member of the #GTypeQuery is 0. All members filled into the
+ * #GTypeQuery structure should be considered constant and have to be
+ * left untouched.
+ */
 void
 g_type_query (GType       type,
              GTypeQuery *query)
@@ -3032,6 +3833,16 @@ g_type_test_flags (GType type,
   return result;
 }
 
+/**
+ * g_type_get_plugin:
+ * @type: The #GType to retrieve the plugin for.
+ *
+ * Returns the #GTypePlugin structure for @type or
+ * %NULL if @type does not have a #GTypePlugin structure.
+ *
+ * Returns: (transfer none): The corresponding plugin if @type is a
+ *          dynamic type, %NULL otherwise.
+ */
 GTypePlugin*
 g_type_get_plugin (GType type)
 {
@@ -3042,6 +3853,19 @@ g_type_get_plugin (GType type)
   return node ? node->plugin : NULL;
 }
 
+/**
+ * g_type_interface_get_plugin:
+ * @instance_type: the #GType value of an instantiatable type.
+ * @interface_type: the #GType value of an interface type.
+ *
+ * Returns the #GTypePlugin structure for the dynamic interface
+ * @interface_type which has been added to @instance_type, or %NULL if
+ * @interface_type has not been added to @instance_type or does not
+ * have a #GTypePlugin structure. See g_type_add_interface_dynamic().
+ *
+ * Returns: (transfer none): the #GTypePlugin for the dynamic
+ *          interface @interface_type of @instance_type.
+ */
 GTypePlugin*
 g_type_interface_get_plugin (GType instance_type,
                             GType interface_type)
@@ -3078,6 +3902,17 @@ g_type_interface_get_plugin (GType instance_type,
   return NULL;
 }
 
+/**
+ * g_type_fundamental_next:
+ *
+ * Returns the next free fundamental type id which can be used to
+ * register a new fundamental type with g_type_register_fundamental().
+ * The returned type ID represents the highest currently registered
+ * fundamental type identifier.
+ *
+ * Returns: The nextmost fundamental type ID to be registered,
+ *          or 0 if the type system ran out of fundamental type IDs.
+ */
 GType
 g_type_fundamental_next (void)
 {
@@ -3090,6 +3925,15 @@ g_type_fundamental_next (void)
   return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
 }
 
+/**
+ * g_type_fundamental:
+ * @type_id: valid type ID
+ * 
+ * Internal function, used to extract the fundamental type ID portion.
+ * use G_TYPE_FUNDAMENTAL() instead.
+ * 
+ * Returns: fundamental type ID
+ */
 GType
 g_type_fundamental (GType type_id)
 {
@@ -3198,6 +4042,15 @@ g_type_check_class_cast (GTypeClass *type_class,
   return type_class;
 }
 
+/**
+ * g_type_check_instance:
+ * @instance: A valid #GTypeInstance structure.
+ *
+ * Private helper function to aid implementation of the G_TYPE_CHECK_INSTANCE()
+ * macro.
+ *
+ * @Returns:  #TRUE if @instance is valid, #FALSE otherwise.
+ */
 gboolean
 g_type_check_instance (GTypeInstance *type_instance)
 {
@@ -3240,7 +4093,7 @@ type_check_is_value_type_U (GType type)
  restart_check:
   if (node)
     {
-      if (node->data && node->data->common.ref_count > 0 &&
+      if (node->data && NODE_REFCOUNT (node) > 0 &&
          node->data->common.value_table->value_init)
        tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
       else if (NODE_IS_IFACE (node))
@@ -3285,25 +4138,32 @@ g_type_check_value_holds (GValue *value,
   return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
 }
 
+/**
+ * g_type_value_table_peek: (skip)
+ * @type: A #GType value.
+ *
+ * Returns the location of the #GTypeValueTable associated with @type.
+ * <emphasis>Note that this function should only be used from source code
+ * that implements or has internal knowledge of the implementation of
+ * @type.</emphasis>
+ *
+ * Returns: Location of the #GTypeValueTable associated with @type or
+ *  %NULL if there is no #GTypeValueTable associated with @type.
+ */
 GTypeValueTable*
 g_type_value_table_peek (GType type)
 {
   GTypeValueTable *vtable = NULL;
   TypeNode *node = lookup_type_node_I (type);
   gboolean has_refed_data, has_table;
-  TypeData *data;
 
-  /* speed up common code path, we're not 100% safe here,
-   * but we should only get called with referenced types anyway
-   */
-  data = node ? node->data : NULL;
-  if (node && node->mutatable_check_cache)
-    return data->common.value_table;
+  if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
+    return node->data->common.value_table;
 
   G_READ_LOCK (&type_rw_lock);
   
  restart_table_peek:
-  has_refed_data = node && node->data && node->data->common.ref_count;
+  has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
   has_table = has_refed_data && node->data->common.value_table->value_init;
   if (has_refed_data)
     {
@@ -3334,7 +4194,7 @@ g_type_value_table_peek (GType type)
     return vtable;
   
   if (!node)
-    g_warning (G_STRLOC ": type id `%lu' is invalid", type);
+    g_warning (G_STRLOC ": type id `%" G_GSIZE_FORMAT "' is invalid", type);
   if (!has_refed_data)
     g_warning ("can't peek value table for type `%s' which is not currently referenced",
               type_descriptive_name_I (type));
@@ -3361,7 +4221,42 @@ g_type_name_from_class (GTypeClass *g_class)
 }
 
 
+/* --- private api for gboxed.c --- */
+gpointer
+_g_type_boxed_copy (GType type, gpointer value)
+{
+  TypeNode *node = lookup_type_node_I (type);
+
+  return node->data->boxed.copy_func (value);
+}
+
+void
+_g_type_boxed_free (GType type, gpointer value)
+{
+  TypeNode *node = lookup_type_node_I (type);
+
+  node->data->boxed.free_func (value);
+}
+
+void
+_g_type_boxed_init (GType          type,
+                    GBoxedCopyFunc copy_func,
+                    GBoxedFreeFunc free_func)
+{
+  TypeNode *node = lookup_type_node_I (type);
+
+  node->data->boxed.copy_func = copy_func;
+  node->data->boxed.free_func = free_func;
+}
+
 /* --- initialization --- */
+/**
+ * g_type_init_with_debug_flags:
+ * @debug_flags: Bitwise combination of #GTypeDebugFlags values for
+ *               debugging purposes.
+ *
+ * Similar to g_type_init(), but additionally sets debug flags.
+ */
 void
 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
 {
@@ -3369,8 +4264,13 @@ g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
   const gchar *env_string;
   GTypeInfo info;
   TypeNode *node;
-  GType type;
-  
+  volatile GType votype;
+
+#ifdef G_THREADS_ENABLED
+  if (!g_thread_get_initialized())
+    g_thread_init (NULL);
+#endif
+
   G_LOCK (type_init_lock);
   
   G_WRITE_LOCK (&type_rw_lock);
@@ -3413,16 +4313,16 @@ g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
   /* void type G_TYPE_NONE
    */
   node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
-  type = NODE_TYPE (node);
-  g_assert (type == G_TYPE_NONE);
+  votype = NODE_TYPE (node);
+  g_assert (votype == G_TYPE_NONE);
   
   /* interface fundamental type G_TYPE_INTERFACE (!classed)
    */
   memset (&info, 0, sizeof (info));
   node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
-  type = NODE_TYPE (node);
+  votype = NODE_TYPE (node);
   type_data_make_W (node, &info, NULL);
-  g_assert (type == G_TYPE_INTERFACE);
+  g_assert (votype == G_TYPE_INTERFACE);
   
   G_WRITE_UNLOCK (&type_rw_lock);
   
@@ -3430,7 +4330,7 @@ g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
 
   /* G_TYPE_TYPE_PLUGIN
    */
-  g_type_plugin_get_type ();
+  votype = g_type_plugin_get_type ();
   
   /* G_TYPE_* value types
    */
@@ -3467,12 +4367,86 @@ g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
   G_UNLOCK (type_init_lock);
 }
 
-void 
+/**
+ * g_type_init:
+ *
+ * Prior to any use of the type system, g_type_init() has to be called
+ * to initialize the type system and assorted other code portions
+ * (such as the various fundamental type implementations or the signal
+ * system).
+ *
+ * Since version 2.24 this also initializes the thread system
+ */
+void
 g_type_init (void)
 {
   g_type_init_with_debug_flags (0);
 }
 
+/**
+ * g_type_class_add_private:
+ * @g_class: class structure for an instantiatable type
+ * @private_size: size of private structure.
+ *
+ * Registers a private structure for an instantiatable type.
+ *
+ * When an object is allocated, the private structures for
+ * the type and all of its parent types are allocated
+ * sequentially in the same memory block as the public
+ * structures.
+ *
+ * Note that the accumulated size of the private structures of
+ * a type and all its parent types cannot excced 64kB.
+ *
+ * This function should be called in the type's class_init() function.
+ * The private structure can be retrieved using the
+ * G_TYPE_INSTANCE_GET_PRIVATE() macro.
+ *
+ * The following example shows attaching a private structure
+ * <structname>MyObjectPrivate</structname> to an object
+ * <structname>MyObject</structname> defined in the standard GObject
+ * fashion.
+ * type's class_init() function.
+ *
+ * |[
+ * typedef struct _MyObject        MyObject;
+ * typedef struct _MyObjectPrivate MyObjectPrivate;
+ *
+ * struct _MyObject {
+ *  GObject parent;
+ *
+ *  MyObjectPrivate *priv;
+ * };
+ *
+ * struct _MyObjectPrivate {
+ *   int some_field;
+ * };
+ *
+ * static void
+ * my_object_class_init (MyObjectClass *klass)
+ * {
+ *   g_type_class_add_private (klass, sizeof (MyObjectPrivate));
+ * }
+ *
+ * static void
+ * my_object_init (MyObject *my_object)
+ * {
+ *   my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
+ *                                                  MY_TYPE_OBJECT,
+ *                                                  MyObjectPrivate);
+ * }
+ *
+ * static int
+ * my_object_get_some_field (MyObject *my_object)
+ * {
+ *   MyObjectPrivate *priv = my_object->priv;
+ *
+ *   return priv->some_field;
+ * }
+ * ]|
+ *
+ * Since: 2.4
+ */
 void
 g_type_class_add_private (gpointer g_class,
                          gsize    private_size)
@@ -3481,6 +4455,9 @@ g_type_class_add_private (gpointer g_class,
   TypeNode *node = lookup_type_node_I (instance_type);
   gsize offset;
 
+  g_return_if_fail (private_size > 0);
+  g_return_if_fail (private_size <= 0xffff);
+
   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
     {
       g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
@@ -3501,6 +4478,9 @@ g_type_class_add_private (gpointer g_class,
   G_WRITE_LOCK (&type_rw_lock);
 
   offset = ALIGN_STRUCT (node->data->instance.private_size);
+
+  g_assert (offset + private_size <= 0xffff);
+
   node->data->instance.private_size = offset + private_size;
   
   G_WRITE_UNLOCK (&type_rw_lock);
@@ -3553,7 +4533,7 @@ g_type_instance_get_private (GTypeInstance *instance,
   if (NODE_PARENT_TYPE (private_node))
     {
       parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
-      g_assert (parent_node->data && parent_node->data->common.ref_count);
+      g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
 
       if (G_UNLIKELY (private_node->data->instance.private_size == parent_node->data->instance.private_size))
        {
@@ -3567,5 +4547,98 @@ g_type_instance_get_private (GTypeInstance *instance,
   return G_STRUCT_MEMBER_P (instance, offset);
 }
 
-#define __G_TYPE_C__
-#include "gobjectaliasdef.c"
+/**
+ * g_type_add_class_private:
+ * @class_type: GType of an classed type.
+ * @private_size: size of private structure.
+ *
+ * Registers a private class structure for a classed type;
+ * when the class is allocated, the private structures for
+ * the class and all of its parent types are allocated
+ * sequentially in the same memory block as the public
+ * structures. This function should be called in the
+ * type's get_type() function after the type is registered.
+ * The private structure can be retrieved using the
+ * G_TYPE_CLASS_GET_PRIVATE() macro.
+ *
+ * Since: 2.24
+ */
+void
+g_type_add_class_private (GType    class_type,
+                         gsize    private_size)
+{
+  TypeNode *node = lookup_type_node_I (class_type);
+  gsize offset;
+
+  g_return_if_fail (private_size > 0);
+
+  if (!node || !node->is_classed || !node->data)
+    {
+      g_warning ("cannot add class private field to invalid type '%s'",
+                type_descriptive_name_I (class_type));
+      return;
+    }
+
+  if (NODE_PARENT_TYPE (node))
+    {
+      TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
+      if (node->data->class.class_private_size != pnode->data->class.class_private_size)
+       {
+         g_warning ("g_type_add_class_private() called multiple times for the same type");
+         return;
+       }
+    }
+  
+  G_WRITE_LOCK (&type_rw_lock);
+
+  offset = ALIGN_STRUCT (node->data->class.class_private_size);
+  node->data->class.class_private_size = offset + private_size;
+
+  G_WRITE_UNLOCK (&type_rw_lock);
+}
+
+gpointer
+g_type_class_get_private (GTypeClass *klass,
+                         GType       private_type)
+{
+  TypeNode *class_node;
+  TypeNode *private_node;
+  TypeNode *parent_node;
+  gsize offset;
+
+  g_return_val_if_fail (klass != NULL, NULL);
+
+  class_node = lookup_type_node_I (klass->g_type);
+  if (G_UNLIKELY (!class_node || !class_node->is_classed))
+    {
+      g_warning ("class of invalid type `%s'",
+                type_descriptive_name_I (klass->g_type));
+      return NULL;
+    }
+
+  private_node = lookup_type_node_I (private_type);
+  if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
+    {
+      g_warning ("attempt to retrieve private data for invalid type '%s'",
+                type_descriptive_name_I (private_type));
+      return NULL;
+    }
+
+  offset = ALIGN_STRUCT (class_node->data->class.class_size);
+
+  if (NODE_PARENT_TYPE (private_node))
+    {
+      parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
+      g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
+
+      if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
+       {
+         g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_class_add_class_private()");
+         return NULL;
+       }
+
+      offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
+    }
+
+  return G_STRUCT_MEMBER_P (klass, offset);
+}