tee: Check for the removed pad flag also in the slow pushing path
[platform/upstream/gstreamer.git] / gst / gstminiobject.c
index 2454ca8..d5c28ba 100644 (file)
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 /**
  * SECTION:gstminiobject
+ * @title: GstMiniObject
  * @short_description: Lightweight base class for the GStreamer object hierarchy
  *
- * #GstMiniObject is a baseclass like #GObject, but has been stripped down of
- * features to be fast and small.
- * It offers sub-classing and ref-counting in the same way as #GObject does.
- * It has no properties and no signal-support though.
+ * #GstMiniObject is a simple structure that can be used to implement refcounted
+ * types.
  *
- * Last reviewed on 2005-11-23 (0.9.5)
+ * Subclasses will include #GstMiniObject as the first member in their structure
+ * and then call gst_mini_object_init() to initialize the #GstMiniObject fields.
+ *
+ * gst_mini_object_ref() and gst_mini_object_unref() increment and decrement the
+ * refcount respectively. When the refcount of a mini-object reaches 0, the
+ * dispose function is called first and when this returns %TRUE, the free
+ * function of the miniobject is called.
+ *
+ * A copy can be made with gst_mini_object_copy().
+ *
+ * gst_mini_object_is_writable() will return %TRUE when the refcount of the
+ * object is exactly 1 and there is no parent or a single parent exists and is
+ * writable itself, meaning the current caller has the only reference to the
+ * object. gst_mini_object_make_writable() will return a writable version of
+ * the object, which might be a new copy when the refcount was not 1.
+ *
+ * Opaque data can be associated with a #GstMiniObject with
+ * gst_mini_object_set_qdata() and gst_mini_object_get_qdata(). The data is
+ * meant to be specific to the particular object and is not automatically copied
+ * with gst_mini_object_copy() or similar methods.
+ *
+ * A weak reference can be added and remove with gst_mini_object_weak_ref()
+ * and gst_mini_object_weak_unref() respectively.
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #include "gst/gstinfo.h"
 #include <gobject/gvaluecollector.h>
 
-#ifndef GST_DISABLE_TRACE
-#include "gsttrace.h"
-static GstAllocTrace *_gst_mini_object_trace;
-#endif
-
-#define GST_MINI_OBJECT_GET_CLASS_UNCHECKED(obj) \
-    ((GstMiniObjectClass *) (((GTypeInstance*)(obj))->g_class))
-
-/* Structure used for storing weak references */
-typedef struct
-{
-  GstMiniObject *object;
-  guint n_weak_refs;
-  struct
-  {
-    GstMiniObjectWeakNotify notify;
-    gpointer data;
-  } weak_refs[1];               /* flexible array */
-} WeakRefStack;
-
-/* Structure for storing a mini object's private data */
-struct _GstMiniObjectPrivate
-{
-  WeakRefStack *wstack;
-};
-
-#if 0
-static void gst_mini_object_base_init (gpointer g_class);
-static void gst_mini_object_base_finalize (gpointer g_class);
-#endif
-static void gst_mini_object_class_init (gpointer g_class, gpointer class_data);
-static void gst_mini_object_init (GTypeInstance * instance, gpointer klass);
-
-static void gst_value_mini_object_init (GValue * value);
-static void gst_value_mini_object_free (GValue * value);
-static void weak_refs_notify (WeakRefStack * data);
-static void gst_value_mini_object_copy (const GValue * src_value,
-    GValue * dest_value);
-static gpointer gst_value_mini_object_peek_pointer (const GValue * value);
-static gchar *gst_value_mini_object_collect (GValue * value,
-    guint n_collect_values, GTypeCValue * collect_values, guint collect_flags);
-static gchar *gst_value_mini_object_lcopy (const GValue * value,
-    guint n_collect_values, GTypeCValue * collect_values, guint collect_flags);
-
-static GstMiniObject *gst_mini_object_copy_default (const GstMiniObject * obj);
-static void gst_mini_object_finalize (GstMiniObject * obj);
-
 /* Mutex used for weak referencing */
-G_LOCK_DEFINE_STATIC (weak_refs_mutex);
+G_LOCK_DEFINE_STATIC (qdata_mutex);
+static GQuark weak_ref_quark;
+
+#define SHARE_ONE (1 << 16)
+#define SHARE_TWO (2 << 16)
+#define SHARE_MASK (~(SHARE_ONE - 1))
+#define IS_SHARED(state) (state >= SHARE_TWO)
+#define LOCK_ONE (GST_LOCK_FLAG_LAST)
+#define FLAG_MASK (GST_LOCK_FLAG_LAST - 1)
+#define LOCK_MASK ((SHARE_ONE - 1) - FLAG_MASK)
+#define LOCK_FLAG_MASK (SHARE_ONE - 1)
+
+/* For backwards compatibility reasons we use the
+ * guint and gpointer in the GstMiniObject struct in
+ * a rather complicated way to store the parent(s) and qdata.
+ * Originally the were just the number of qdatas and the qdata.
+ *
+ * The guint is used as an atomic state integer with the following
+ * states:
+ * - Locked: 0, basically a spinlock
+ * - No parent, no qdata: 1 (pointer is NULL)
+ * - One parent: 2 (pointer contains the parent)
+ * - Multiple parents or qdata: 3 (pointer contains a PrivData struct)
+ *
+ * Unless we're in state 3, we always have to move to Locking state
+ * atomically and release that again later to the target state whenever
+ * accessing the pointer. When we're in state 3, we will never move to lower
+ * states again
+ *
+ * FIXME 2.0: We should store this directly inside the struct, possibly
+ * keeping space directly allocated for a couple of parents
+ */
 
-GType
-gst_mini_object_get_type (void)
+enum
 {
-  static volatile GType _gst_mini_object_type = 0;
-
-  if (g_once_init_enter (&_gst_mini_object_type)) {
-    GType _type;
-    static const GTypeValueTable value_table = {
-      gst_value_mini_object_init,
-      gst_value_mini_object_free,
-      gst_value_mini_object_copy,
-      gst_value_mini_object_peek_pointer,
-      (char *) "p",
-      gst_value_mini_object_collect,
-      (char *) "p",
-      gst_value_mini_object_lcopy
-    };
-    static const GTypeInfo mini_object_info = {
-      sizeof (GstMiniObjectClass),
-#if 0
-      gst_mini_object_base_init,
-      gst_mini_object_base_finalize,
-#else
-      NULL, NULL,
-#endif
-      gst_mini_object_class_init,
-      NULL,
-      NULL,
-      sizeof (GstMiniObject),
-      0,
-      (GInstanceInitFunc) gst_mini_object_init,
-      &value_table
-    };
-    static const GTypeFundamentalInfo mini_object_fundamental_info = {
-      (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE |
-          G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE)
-    };
-
-    _type = g_type_fundamental_next ();
-    g_type_register_fundamental (_type, "GstMiniObject",
-        &mini_object_info, &mini_object_fundamental_info, G_TYPE_FLAG_ABSTRACT);
-
-#ifndef GST_DISABLE_TRACE
-    _gst_mini_object_trace = gst_alloc_trace_register (g_type_name (_type));
-#endif
-    g_once_init_leave (&_gst_mini_object_type, _type);
-  }
-
-  return _gst_mini_object_type;
-}
+  PRIV_DATA_STATE_LOCKED = 0,
+  PRIV_DATA_STATE_NO_PARENT = 1,
+  PRIV_DATA_STATE_ONE_PARENT = 2,
+  PRIV_DATA_STATE_PARENTS_OR_QDATA = 3,
+};
 
-#if 0
-static void
-gst_mini_object_base_init (gpointer g_class)
+typedef struct
 {
-  /* do nothing */
-}
+  GQuark quark;
+  GstMiniObjectNotify notify;
+  gpointer data;
+  GDestroyNotify destroy;
+} GstQData;
 
-static void
-gst_mini_object_base_finalize (gpointer g_class)
+typedef struct
 {
-  /* do nothing */
-}
-#endif
+  /* Atomic spinlock: 1 if locked, 0 otherwise */
+  gint parent_lock;
+  guint n_parents, n_parents_len;
+  GstMiniObject **parents;
 
-static void
-gst_mini_object_class_init (gpointer g_class, gpointer class_data)
-{
-  GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (g_class);
+  guint n_qdata, n_qdata_len;
+  GstQData *qdata;
+} PrivData;
 
-  mo_class->copy = gst_mini_object_copy_default;
-  mo_class->finalize = gst_mini_object_finalize;
+#define QDATA(q,i)          (q->qdata)[(i)]
+#define QDATA_QUARK(o,i)    (QDATA(o,i).quark)
+#define QDATA_NOTIFY(o,i)   (QDATA(o,i).notify)
+#define QDATA_DATA(o,i)     (QDATA(o,i).data)
+#define QDATA_DESTROY(o,i)  (QDATA(o,i).destroy)
 
-  /* Set the instance data type */
-  g_type_class_add_private (g_class, sizeof (GstMiniObjectPrivate));
+void
+_priv_gst_mini_object_initialize (void)
+{
+  weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
 }
 
-static void
-gst_mini_object_init (GTypeInstance * instance, gpointer klass)
+/**
+ * gst_mini_object_init: (skip)
+ * @mini_object: a #GstMiniObject
+ * @flags: initial #GstMiniObjectFlags
+ * @type: the #GType of the mini-object to create
+ * @copy_func: (allow-none): the copy function, or %NULL
+ * @dispose_func: (allow-none): the dispose function, or %NULL
+ * @free_func: (allow-none): the free function or %NULL
+ *
+ * Initializes a mini-object with the desired type and copy/dispose/free
+ * functions.
+ */
+void
+gst_mini_object_init (GstMiniObject * mini_object, guint flags, GType type,
+    GstMiniObjectCopyFunction copy_func,
+    GstMiniObjectDisposeFunction dispose_func,
+    GstMiniObjectFreeFunction free_func)
 {
-  GstMiniObject *mini_object = GST_MINI_OBJECT_CAST (instance);
-
+  mini_object->type = type;
   mini_object->refcount = 1;
+  mini_object->lockstate = 0;
+  mini_object->flags = flags;
 
-  /* we delay initialising the mini object's private data until it's actually
-   * needed for now (mini_object->priv automatically inited to NULL) */
-}
+  mini_object->copy = copy_func;
+  mini_object->dispose = dispose_func;
+  mini_object->free = free_func;
 
-static GstMiniObject *
-gst_mini_object_copy_default (const GstMiniObject * obj)
-{
-  g_warning ("GstMiniObject classes must implement GstMiniObject::copy");
-  return NULL;
-}
+  g_atomic_int_set ((gint *) & mini_object->priv_uint,
+      PRIV_DATA_STATE_NO_PARENT);
+  mini_object->priv_pointer = NULL;
 
-static void
-gst_mini_object_finalize (GstMiniObject * obj)
-{
-  /* do nothing */
-
-  /* WARNING: if anything is ever put in this method, make sure that the
-   * following sub-classes' finalize method chains up to this one:
-   * gstbuffer
-   * gstevent
-   * gstmessage
-   * gstquery
-   */
+  GST_TRACER_MINI_OBJECT_CREATED (mini_object);
 }
 
 /**
- * gst_mini_object_new:
- * @type: the #GType of the mini-object to create
+ * gst_mini_object_copy: (skip)
+ * @mini_object: the mini-object to copy
  *
- * Creates a new mini-object of the desired type.
+ * Creates a copy of the mini-object.
  *
  * MT safe
  *
- * Returns: (transfer full): the new mini-object.
+ * Returns: (transfer full) (nullable): the new mini-object if copying is
+ * possible, %NULL otherwise.
  */
 GstMiniObject *
-gst_mini_object_new (GType type)
+gst_mini_object_copy (const GstMiniObject * mini_object)
 {
-  GstMiniObject *mini_object;
+  GstMiniObject *copy;
 
-  /* we don't support dynamic types because they really aren't useful,
-   * and could cause refcount problems */
-  mini_object = (GstMiniObject *) g_type_create_instance (type);
+  g_return_val_if_fail (mini_object != NULL, NULL);
 
-#ifndef GST_DISABLE_TRACE
-  gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
-#endif
+  if (mini_object->copy)
+    copy = mini_object->copy (mini_object);
+  else
+    copy = NULL;
 
-  return mini_object;
+  return copy;
 }
 
-/* FIXME 0.11: Current way of doing the copy makes it impossible
- * to currectly chain to the parent classes and do a copy in a
- * subclass without knowing all internals of the parent classes.
- *
- * For 0.11 we should do something like the following:
- *  - The GstMiniObjectClass::copy() implementation of GstMiniObject
- *    should call g_type_create_instance() with the type of the source
- *    object.
- *  - All GstMiniObjectClass::copy() implementations should as first
- *    thing chain up to the parent class and then do whatever they need
- *    to do to copy their type specific data. Note that this way the
- *    instance_init() functions are called!
- */
-
 /**
- * gst_mini_object_copy:
- * @mini_object: the mini-object to copy
+ * gst_mini_object_lock:
+ * @object: the mini-object to lock
+ * @flags: #GstLockFlags
  *
- * Creates a copy of the mini-object.
+ * Lock the mini-object with the specified access mode in @flags.
  *
- * MT safe
+ * Returns: %TRUE if @object could be locked.
+ */
+gboolean
+gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
+{
+  gint access_mode, state, newstate;
+
+  g_return_val_if_fail (object != NULL, FALSE);
+  g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
+
+  if (G_UNLIKELY (object->flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY &&
+          flags & GST_LOCK_FLAG_WRITE))
+    return FALSE;
+
+  do {
+    access_mode = flags & FLAG_MASK;
+    newstate = state = g_atomic_int_get (&object->lockstate);
+
+    GST_CAT_TRACE (GST_CAT_LOCKING, "lock %p: state %08x, access_mode %d",
+        object, state, access_mode);
+
+    if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
+      /* shared ref */
+      newstate += SHARE_ONE;
+      access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
+    }
+
+    /* shared counter > 1 and write access is not allowed */
+    if (((state & GST_LOCK_FLAG_WRITE) != 0
+            || (access_mode & GST_LOCK_FLAG_WRITE) != 0)
+        && IS_SHARED (newstate))
+      goto lock_failed;
+
+    if (access_mode) {
+      if ((state & LOCK_FLAG_MASK) == 0) {
+        /* nothing mapped, set access_mode */
+        newstate |= access_mode;
+      } else {
+        /* access_mode must match */
+        if ((state & access_mode) != access_mode)
+          goto lock_failed;
+      }
+      /* increase refcount */
+      newstate += LOCK_ONE;
+    }
+  } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
+          newstate));
+
+  return TRUE;
+
+lock_failed:
+  {
+    GST_CAT_DEBUG (GST_CAT_LOCKING,
+        "lock failed %p: state %08x, access_mode %d", object, state,
+        access_mode);
+    return FALSE;
+  }
+}
+
+/**
+ * gst_mini_object_unlock:
+ * @object: the mini-object to unlock
+ * @flags: #GstLockFlags
  *
- * Returns: (transfer full): the new mini-object.
+ * Unlock the mini-object with the specified access mode in @flags.
  */
-GstMiniObject *
-gst_mini_object_copy (const GstMiniObject * mini_object)
+void
+gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
 {
-  GstMiniObjectClass *mo_class;
+  gint access_mode, state, newstate;
 
-  g_return_val_if_fail (mini_object != NULL, NULL);
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
+
+  do {
+    access_mode = flags & FLAG_MASK;
+    newstate = state = g_atomic_int_get (&object->lockstate);
 
-  mo_class = GST_MINI_OBJECT_GET_CLASS (mini_object);
+    GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
+        object, state, access_mode);
 
-  return mo_class->copy (mini_object);
+    if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
+      /* shared counter */
+      g_return_if_fail (state >= SHARE_ONE);
+      newstate -= SHARE_ONE;
+      access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
+    }
+
+    if (access_mode) {
+      g_return_if_fail ((state & access_mode) == access_mode);
+      /* decrease the refcount */
+      newstate -= LOCK_ONE;
+      /* last refcount, unset access_mode */
+      if ((newstate & LOCK_FLAG_MASK) == access_mode)
+        newstate &= ~LOCK_FLAG_MASK;
+    }
+  } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
+          newstate));
+}
+
+/* Locks the priv pointer and sets the priv uint to PRIV_DATA_STATE_LOCKED,
+ * unless the full struct was already stored in the priv pointer.
+ *
+ * Returns the previous state of the priv uint
+ */
+static guint
+lock_priv_pointer (GstMiniObject * object)
+{
+  gint priv_state = g_atomic_int_get ((gint *) & object->priv_uint);
+
+  if (priv_state != PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    /* As long as the struct was not allocated yet and either someone else
+     * locked it or our priv_state is out of date, try to lock it */
+    while (priv_state != PRIV_DATA_STATE_PARENTS_OR_QDATA &&
+        (priv_state == PRIV_DATA_STATE_LOCKED ||
+            !g_atomic_int_compare_and_exchange ((gint *) & object->priv_uint,
+                priv_state, PRIV_DATA_STATE_LOCKED)))
+      priv_state = g_atomic_int_get ((gint *) & object->priv_uint);
+
+    /* Note that if we got the full struct, we did not store
+     * PRIV_DATA_STATE_LOCKED and did not actually lock the priv pointer */
+  }
+
+  return priv_state;
 }
 
 /**
  * gst_mini_object_is_writable:
  * @mini_object: the mini-object to check
  *
- * Checks if a mini-object is writable.  A mini-object is writable
- * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
- * flag is not set.  Modification of a mini-object should only be
- * done after verifying that it is writable.
+ * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
+ * lock on @object is the only one, this means that changes to the object will
+ * not be visible to any other object.
  *
- * MT safe
+ * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
+ * exactly 1, meaning that no other reference exists to the object and that the
+ * object is therefore writable.
+ *
+ * Modification of a mini-object should only be done after verifying that it
+ * is writable.
  *
- * Returns: TRUE if the object is writable.
+ * Returns: %TRUE if the object is writable.
  */
 gboolean
 gst_mini_object_is_writable (const GstMiniObject * mini_object)
 {
+  gboolean result;
+  gint priv_state;
+
   g_return_val_if_fail (mini_object != NULL, FALSE);
 
-  return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1) &&
-      ((mini_object->flags & GST_MINI_OBJECT_FLAG_READONLY) == 0);
+  /* Let's first check our own writability. If this already fails there's
+   * no point in checking anything else */
+  if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
+    result = !IS_SHARED (g_atomic_int_get (&mini_object->lockstate));
+  } else {
+    result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
+  }
+  if (!result)
+    return result;
+
+  /* We are writable ourselves, but are there parents and are they all
+   * writable too? */
+  priv_state = lock_priv_pointer (GST_MINI_OBJECT_CAST (mini_object));
+
+  /* Now we either have to check the full struct and all the
+   * parents in there, or if there is exactly one parent we
+   * can check that one */
+  if (priv_state == PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    PrivData *priv_data = mini_object->priv_pointer;
+
+    /* Lock parents */
+    while (!g_atomic_int_compare_and_exchange (&priv_data->parent_lock, 0, 1));
+
+    /* If we have one parent, we're only writable if that parent is writable.
+     * Otherwise if we have multiple parents we are not writable, and if
+     * we have no parent, we are writable */
+    if (priv_data->n_parents == 1)
+      result = gst_mini_object_is_writable (priv_data->parents[0]);
+    else if (priv_data->n_parents == 0)
+      result = TRUE;
+    else
+      result = FALSE;
+
+    /* Unlock again */
+    g_atomic_int_set (&priv_data->parent_lock, 0);
+  } else {
+    if (priv_state == PRIV_DATA_STATE_ONE_PARENT) {
+      result = gst_mini_object_is_writable (mini_object->priv_pointer);
+    } else {
+      g_assert (priv_state == PRIV_DATA_STATE_NO_PARENT);
+      result = TRUE;
+    }
+
+    /* Unlock again */
+    g_atomic_int_set ((gint *) & mini_object->priv_uint, priv_state);
+  }
+
+  return result;
 }
 
 /**
- * gst_mini_object_make_writable:
+ * gst_mini_object_make_writable: (skip)
  * @mini_object: (transfer full): the mini-object to make writable
  *
  * Checks if a mini-object is writable.  If not, a writable copy is made and
@@ -304,9 +423,9 @@ gst_mini_object_make_writable (GstMiniObject * mini_object)
   if (gst_mini_object_is_writable (mini_object)) {
     ret = mini_object;
   } else {
-    GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject",
-        g_type_name (G_TYPE_FROM_INSTANCE (mini_object)));
     ret = gst_mini_object_copy (mini_object);
+    GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
+        g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
     gst_mini_object_unref (mini_object);
   }
 
@@ -314,12 +433,12 @@ gst_mini_object_make_writable (GstMiniObject * mini_object)
 }
 
 /**
- * gst_mini_object_ref:
+ * gst_mini_object_ref: (skip)
  * @mini_object: the mini-object
  *
  * Increase the reference count of the mini-object.
  *
- * Note that the refcount affects the writeability
+ * Note that the refcount affects the writability
  * of @mini-object, see gst_mini_object_is_writable(). It is
  * important to note that keeping additional references to
  * GstMiniObject instances can potentially increase the number
@@ -331,66 +450,182 @@ gst_mini_object_make_writable (GstMiniObject * mini_object)
 GstMiniObject *
 gst_mini_object_ref (GstMiniObject * mini_object)
 {
+  gint old_refcount, new_refcount;
+
   g_return_val_if_fail (mini_object != NULL, NULL);
   /* we can't assert that the refcount > 0 since the _free functions
-   * increments the refcount from 0 to 1 again to allow resurecting
+   * increments the refcount from 0 to 1 again to allow resurrecting
    * the object
    g_return_val_if_fail (mini_object->refcount > 0, NULL);
    */
-  g_return_val_if_fail (GST_IS_MINI_OBJECT (mini_object), NULL);
+
+  old_refcount = g_atomic_int_add (&mini_object->refcount, 1);
+  new_refcount = old_refcount + 1;
 
   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
+      old_refcount, new_refcount);
 
-  g_atomic_int_inc (&mini_object->refcount);
+  GST_TRACER_MINI_OBJECT_REFFED (mini_object, new_refcount);
 
   return mini_object;
 }
 
-static void
-weak_refs_notify (WeakRefStack * wstack)
+/* Called with global qdata lock */
+static gint
+find_notify (GstMiniObject * object, GQuark quark, gboolean match_notify,
+    GstMiniObjectNotify notify, gpointer data)
 {
   guint i;
+  gint priv_state = g_atomic_int_get ((gint *) & object->priv_uint);
+  PrivData *priv_data;
+
+  if (priv_state != PRIV_DATA_STATE_PARENTS_OR_QDATA)
+    return -1;
 
-  for (i = 0; i < wstack->n_weak_refs; i++)
-    wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
-  g_free (wstack);
+  priv_data = object->priv_pointer;
+
+  for (i = 0; i < priv_data->n_qdata; i++) {
+    if (QDATA_QUARK (priv_data, i) == quark) {
+      /* check if we need to match the callback too */
+      if (!match_notify || (QDATA_NOTIFY (priv_data, i) == notify &&
+              QDATA_DATA (priv_data, i) == data))
+        return i;
+    }
+  }
+  return -1;
 }
 
 static void
-gst_mini_object_free (GstMiniObject * mini_object)
+remove_notify (GstMiniObject * object, gint index)
 {
-  GstMiniObjectClass *mo_class;
+  gint priv_state = g_atomic_int_get ((gint *) & object->priv_uint);
+  PrivData *priv_data;
+
+  g_assert (priv_state == PRIV_DATA_STATE_PARENTS_OR_QDATA);
+  priv_data = object->priv_pointer;
+
+  /* remove item */
+  priv_data->n_qdata--;
+  if (priv_data->n_qdata == 0) {
+    /* we don't shrink but free when everything is gone */
+    g_free (priv_data->qdata);
+    priv_data->qdata = NULL;
+    priv_data->n_qdata_len = 0;
+  } else if (index != priv_data->n_qdata) {
+    QDATA (priv_data, index) = QDATA (priv_data, priv_data->n_qdata);
+  }
+}
 
-  /* At this point, the refcount of the object is 0. We increase the refcount
-   * here because if a subclass recycles the object and gives out a new
-   * reference we don't want to free the instance anymore. */
-  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
+/* Make sure we allocate the PrivData of this object if not happened yet */
+static void
+ensure_priv_data (GstMiniObject * object)
+{
+  gint priv_state;
+  PrivData *priv_data;
+  GstMiniObject *parent = NULL;
 
-  g_atomic_int_inc (&mini_object->refcount);
+  GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
+      "allocating private data %s miniobject %p",
+      g_type_name (GST_MINI_OBJECT_TYPE (object)), object);
 
-  mo_class = GST_MINI_OBJECT_GET_CLASS_UNCHECKED (mini_object);
-  mo_class->finalize (mini_object);
+  priv_state = lock_priv_pointer (object);
+  if (priv_state == PRIV_DATA_STATE_PARENTS_OR_QDATA)
+    return;
 
-  /* decrement the refcount again, if the subclass recycled the object we don't
-   * want to free the instance anymore */
-  if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
-    /* The weak reference stack is freed in the notification function */
-    if (mini_object->priv != NULL && mini_object->priv->wstack != NULL)
-      weak_refs_notify (mini_object->priv->wstack);
+  /* Now we're either locked, or someone has already allocated the struct
+   * before us and we can just go ahead
+   *
+   * Note: if someone else allocated it in the meantime, we don't have to
+   * unlock as we didn't lock! */
+  if (priv_state != PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    if (priv_state == PRIV_DATA_STATE_ONE_PARENT)
+      parent = object->priv_pointer;
+
+    object->priv_pointer = priv_data = g_new0 (PrivData, 1);
+
+    if (parent) {
+      priv_data->parents = g_new (GstMiniObject *, 16);
+      priv_data->n_parents_len = 16;
+      priv_data->n_parents = 1;
+      priv_data->parents[0] = parent;
+    }
 
-#ifndef GST_DISABLE_TRACE
-    gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
-#endif
-    g_type_free_instance ((GTypeInstance *) mini_object);
+    /* Unlock */
+    g_atomic_int_set ((gint *) & object->priv_uint,
+        PRIV_DATA_STATE_PARENTS_OR_QDATA);
+  }
+}
+
+static void
+set_notify (GstMiniObject * object, gint index, GQuark quark,
+    GstMiniObjectNotify notify, gpointer data, GDestroyNotify destroy)
+{
+  PrivData *priv_data;
+
+  ensure_priv_data (object);
+  priv_data = object->priv_pointer;
+
+  if (index == -1) {
+    /* add item */
+    index = priv_data->n_qdata++;
+    if (index >= priv_data->n_qdata_len) {
+      priv_data->n_qdata_len *= 2;
+      if (priv_data->n_qdata_len == 0)
+        priv_data->n_qdata_len = 16;
+
+      priv_data->qdata =
+          g_realloc (priv_data->qdata,
+          sizeof (GstQData) * priv_data->n_qdata_len);
+    }
+  }
+
+  QDATA_QUARK (priv_data, index) = quark;
+  QDATA_NOTIFY (priv_data, index) = notify;
+  QDATA_DATA (priv_data, index) = data;
+  QDATA_DESTROY (priv_data, index) = destroy;
+}
+
+static void
+free_priv_data (GstMiniObject * obj)
+{
+  guint i;
+  gint priv_state = g_atomic_int_get ((gint *) & obj->priv_uint);
+  PrivData *priv_data;
+
+  if (priv_state != PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    if (priv_state == PRIV_DATA_STATE_LOCKED) {
+      g_warning
+          ("%s: object finalizing but has locked private data (object:%p)",
+          G_STRFUNC, obj);
+    } else if (priv_state == PRIV_DATA_STATE_ONE_PARENT) {
+      g_warning
+          ("%s: object finalizing but still has parent (object:%p, parent:%p)",
+          G_STRFUNC, obj, obj->priv_pointer);
+    }
+
+    return;
+  }
+
+  priv_data = obj->priv_pointer;
+
+  for (i = 0; i < priv_data->n_qdata; i++) {
+    if (QDATA_QUARK (priv_data, i) == weak_ref_quark)
+      QDATA_NOTIFY (priv_data, i) (QDATA_DATA (priv_data, i), obj);
+    if (QDATA_DESTROY (priv_data, i))
+      QDATA_DESTROY (priv_data, i) (QDATA_DATA (priv_data, i));
   }
+  g_free (priv_data->qdata);
+
+  if (priv_data->n_parents)
+    g_warning ("%s: object finalizing but still has %d parents (object:%p)",
+        G_STRFUNC, priv_data->n_parents, obj);
+  g_free (priv_data->parents);
+
+  g_free (priv_data);
 }
 
 /**
- * gst_mini_object_unref:
+ * gst_mini_object_unref: (skip)
  * @mini_object: the mini-object
  *
  * Decreases the reference count of the mini-object, possibly freeing
@@ -399,138 +634,89 @@ gst_mini_object_free (GstMiniObject * mini_object)
 void
 gst_mini_object_unref (GstMiniObject * mini_object)
 {
-  g_return_if_fail (GST_IS_MINI_OBJECT (mini_object));
-  g_return_if_fail (mini_object->refcount > 0);
+  gint old_refcount, new_refcount;
 
-  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
-      mini_object,
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
-      GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
+  g_return_if_fail (mini_object != NULL);
+  g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) > 0);
 
-  if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
-    gst_mini_object_free (mini_object);
-  }
-}
+  old_refcount = g_atomic_int_add (&mini_object->refcount, -1);
+  new_refcount = old_refcount - 1;
 
-/**
- * gst_mini_object_weak_ref: (skip)
- * @object: #GstMiniObject to reference weakly
- * @notify: callback to invoke before the mini object is freed
- * @data: extra data to pass to notify
- *
- * Adds a weak reference callback to a mini object. Weak references are
- * used for notification when a mini object is finalized. They are called
- * "weak references" because they allow you to safely hold a pointer
- * to the mini object without calling gst_mini_object_ref()
- * (gst_mini_object_ref() adds a strong reference, that is, forces the object
- * to stay alive).
- *
- * Since: 0.10.36
- */
-void
-gst_mini_object_weak_ref (GstMiniObject * object,
-    GstMiniObjectWeakNotify notify, gpointer data)
-{
-  guint i;
+  g_return_if_fail (old_refcount > 0);
 
-  g_return_if_fail (GST_IS_MINI_OBJECT (object));
-  g_return_if_fail (notify != NULL);
-  g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
+      mini_object, old_refcount, new_refcount);
 
-  G_LOCK (weak_refs_mutex);
+  GST_TRACER_MINI_OBJECT_UNREFFED (mini_object, new_refcount);
 
-  if (object->priv == NULL) {
-    object->priv = G_TYPE_INSTANCE_GET_PRIVATE (object, GST_TYPE_MINI_OBJECT,
-        GstMiniObjectPrivate);
+  if (new_refcount == 0) {
+    gboolean do_free;
 
-    /* object->priv->wstack will have been inited to NULL automatically */
-  }
+    if (mini_object->dispose)
+      do_free = mini_object->dispose (mini_object);
+    else
+      do_free = TRUE;
 
-  if (object->priv->wstack) {
-    /* Don't add the weak reference if it already exists. */
-    for (i = 0; i < object->priv->wstack->n_weak_refs; i++) {
-      if (object->priv->wstack->weak_refs[i].notify == notify &&
-          object->priv->wstack->weak_refs[i].data == data) {
-        g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
-            G_STRFUNC, notify, data);
-        goto found;
-      }
-    }
+    /* if the subclass recycled the object (and returned FALSE) we don't
+     * want to free the instance anymore */
+    if (G_LIKELY (do_free)) {
+      /* there should be no outstanding locks */
+      g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
+          < 4);
 
-    i = object->priv->wstack->n_weak_refs++;
-    object->priv->wstack =
-        g_realloc (object->priv->wstack, sizeof (*(object->priv->wstack)) +
-        sizeof (object->priv->wstack->weak_refs[0]) * i);
-  } else {
-    object->priv->wstack = g_renew (WeakRefStack, NULL, 1);
-    object->priv->wstack->object = object;
-    object->priv->wstack->n_weak_refs = 1;
-    i = 0;
+      free_priv_data (mini_object);
+
+      GST_TRACER_MINI_OBJECT_DESTROYED (mini_object);
+      if (mini_object->free)
+        mini_object->free (mini_object);
+    }
   }
-  object->priv->wstack->weak_refs[i].notify = notify;
-  object->priv->wstack->weak_refs[i].data = data;
-found:
-  G_UNLOCK (weak_refs_mutex);
 }
 
 /**
- * gst_mini_object_weak_unref: (skip)
- * @object: #GstMiniObject to remove a weak reference from
- * @notify: callback to search for
- * @data: data to search for
+ * gst_clear_mini_object: (skip)
+ * @object_ptr: a pointer to a #GstMiniObject reference
  *
- * Removes a weak reference callback to a mini object.
+ * Clears a reference to a #GstMiniObject.
  *
- * Since: 0.10.36
- */
+ * @object_ptr must not be %NULL.
+ *
+ * If the reference is %NULL then this function does nothing.
+ * Otherwise, the reference count of the object is decreased using
+ * gst_mini_object_unref() and the pointer is set to %NULL.
+ *
+ * A macro is also included that allows this function to be used without
+ * pointer casts.
+ *
+ * Since: 1.16
+ **/
+#undef gst_clear_mini_object
 void
-gst_mini_object_weak_unref (GstMiniObject * object,
-    GstMiniObjectWeakNotify notify, gpointer data)
+gst_clear_mini_object (GstMiniObject ** object_ptr)
 {
-  gboolean found_one = FALSE;
-
-  g_return_if_fail (GST_IS_MINI_OBJECT (object));
-  g_return_if_fail (notify != NULL);
-
-  G_LOCK (weak_refs_mutex);
-
-  if (object->priv != NULL && object->priv->wstack != NULL) {
-    guint i;
-
-    for (i = 0; i < object->priv->wstack->n_weak_refs; i++)
-      if (object->priv->wstack->weak_refs[i].notify == notify &&
-          object->priv->wstack->weak_refs[i].data == data) {
-        found_one = TRUE;
-        object->priv->wstack->n_weak_refs -= 1;
-        if (i != object->priv->wstack->n_weak_refs)
-          object->priv->wstack->weak_refs[i] =
-              object->priv->wstack->weak_refs[object->priv->wstack->
-              n_weak_refs];
-
-        break;
-      }
-  }
-  G_UNLOCK (weak_refs_mutex);
-  if (!found_one)
-    g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
+  g_clear_pointer (object_ptr, gst_mini_object_unref);
 }
 
 /**
  * gst_mini_object_replace:
- * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
- *     be replaced
- * @newdata: pointer to new mini-object
+ * @olddata: (inout) (transfer full) (nullable): pointer to a pointer to a
+ *     mini-object to be replaced
+ * @newdata: (allow-none): pointer to new mini-object
+ *
+ * Atomically modifies a pointer to point to a new mini-object.
+ * The reference count of @olddata is decreased and the reference count of
+ * @newdata is increased.
+ *
+ * Either @newdata and the value pointed to by @olddata may be %NULL.
  *
- * Modifies a pointer to point to a new mini-object.  The modification
- * is done atomically, and the reference counts are updated correctly.
- * Either @newdata and the value pointed to by @olddata may be NULL.
+ * Returns: %TRUE if @newdata was different from @olddata
  */
-void
+gboolean
 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
 {
   GstMiniObject *olddata_val;
 
-  g_return_if_fail (olddata != NULL);
+  g_return_val_if_fail (olddata != NULL, FALSE);
 
   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
       *olddata, *olddata ? (*olddata)->refcount : 0,
@@ -538,263 +724,391 @@ gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
 
   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
 
-  if (olddata_val == newdata)
-    return;
+  if (G_UNLIKELY (olddata_val == newdata))
+    return FALSE;
 
   if (newdata)
     gst_mini_object_ref (newdata);
 
-  while (!g_atomic_pointer_compare_and_exchange ((gpointer *) olddata,
-          olddata_val, newdata)) {
+  while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
+              olddata, olddata_val, newdata))) {
     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
+    if (G_UNLIKELY (olddata_val == newdata))
+      break;
   }
 
   if (olddata_val)
     gst_mini_object_unref (olddata_val);
-}
 
-static void
-gst_value_mini_object_init (GValue * value)
-{
-  value->data[0].v_pointer = NULL;
+  return olddata_val != newdata;
 }
 
-static void
-gst_value_mini_object_free (GValue * value)
+/**
+ * gst_mini_object_steal: (skip)
+ * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
+ *     be stolen
+ *
+ * Replace the current #GstMiniObject pointer to by @olddata with %NULL and
+ * return the old value.
+ *
+ * Returns: (nullable): the #GstMiniObject at @oldata
+ */
+GstMiniObject *
+gst_mini_object_steal (GstMiniObject ** olddata)
 {
-  if (value->data[0].v_pointer) {
-    gst_mini_object_unref (GST_MINI_OBJECT_CAST (value->data[0].v_pointer));
-  }
-}
+  GstMiniObject *olddata_val;
 
-static void
-gst_value_mini_object_copy (const GValue * src_value, GValue * dest_value)
-{
-  if (src_value->data[0].v_pointer) {
-    dest_value->data[0].v_pointer =
-        gst_mini_object_ref (GST_MINI_OBJECT_CAST (src_value->data[0].
-            v_pointer));
-  } else {
-    dest_value->data[0].v_pointer = NULL;
-  }
-}
+  g_return_val_if_fail (olddata != NULL, NULL);
 
-static gpointer
-gst_value_mini_object_peek_pointer (const GValue * value)
-{
-  return value->data[0].v_pointer;
-}
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
+      *olddata, *olddata ? (*olddata)->refcount : 0);
 
-static gchar *
-gst_value_mini_object_collect (GValue * value, guint n_collect_values,
-    GTypeCValue * collect_values, guint collect_flags)
-{
-  if (collect_values[0].v_pointer) {
-    value->data[0].v_pointer =
-        gst_mini_object_ref (collect_values[0].v_pointer);
-  } else {
-    value->data[0].v_pointer = NULL;
-  }
+  do {
+    olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
+    if (olddata_val == NULL)
+      break;
+  } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
+              olddata, olddata_val, NULL)));
 
-  return NULL;
+  return olddata_val;
 }
 
-static gchar *
-gst_value_mini_object_lcopy (const GValue * value, guint n_collect_values,
-    GTypeCValue * collect_values, guint collect_flags)
+/**
+ * gst_mini_object_take:
+ * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
+ *     be replaced
+ * @newdata: pointer to new mini-object
+ *
+ * Modifies a pointer to point to a new mini-object. The modification
+ * is done atomically. This version is similar to gst_mini_object_replace()
+ * except that it does not increase the refcount of @newdata and thus
+ * takes ownership of @newdata.
+ *
+ * Either @newdata and the value pointed to by @olddata may be %NULL.
+ *
+ * Returns: %TRUE if @newdata was different from @olddata
+ */
+gboolean
+gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
 {
-  gpointer *mini_object_p = collect_values[0].v_pointer;
+  GstMiniObject *olddata_val;
 
-  if (!mini_object_p) {
-    return g_strdup_printf ("value location for '%s' passed as NULL",
-        G_VALUE_TYPE_NAME (value));
-  }
+  g_return_val_if_fail (olddata != NULL, FALSE);
 
-  if (!value->data[0].v_pointer)
-    *mini_object_p = NULL;
-  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
-    *mini_object_p = value->data[0].v_pointer;
-  else
-    *mini_object_p = gst_mini_object_ref (value->data[0].v_pointer);
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
+      *olddata, *olddata ? (*olddata)->refcount : 0,
+      newdata, newdata ? newdata->refcount : 0);
+
+  do {
+    olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
+    if (G_UNLIKELY (olddata_val == newdata))
+      break;
+  } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
+              olddata, olddata_val, newdata)));
+
+  if (olddata_val)
+    gst_mini_object_unref (olddata_val);
 
-  return NULL;
+  return olddata_val != newdata;
 }
 
 /**
- * gst_value_set_mini_object:
- * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
- * @mini_object: (transfer none): mini object value to set
+ * gst_mini_object_weak_ref: (skip)
+ * @object: #GstMiniObject to reference weakly
+ * @notify: callback to invoke before the mini object is freed
+ * @data: extra data to pass to notify
  *
- * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
- * @mini_object.
- * The caller retains ownership of the reference.
+ * Adds a weak reference callback to a mini object. Weak references are
+ * used for notification when a mini object is finalized. They are called
+ * "weak references" because they allow you to safely hold a pointer
+ * to the mini object without calling gst_mini_object_ref()
+ * (gst_mini_object_ref() adds a strong reference, that is, forces the object
+ * to stay alive).
  */
 void
-gst_value_set_mini_object (GValue * value, GstMiniObject * mini_object)
+gst_mini_object_weak_ref (GstMiniObject * object,
+    GstMiniObjectNotify notify, gpointer data)
 {
-  gpointer *pointer_p;
-
-  g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
-  g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (notify != NULL);
+  g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
 
-  pointer_p = &value->data[0].v_pointer;
-  gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
+  G_LOCK (qdata_mutex);
+  set_notify (object, -1, weak_ref_quark, notify, data, NULL);
+  G_UNLOCK (qdata_mutex);
 }
 
 /**
- * gst_value_take_mini_object:
- * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
- * @mini_object: (transfer full): mini object value to take
- *
- * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
- * @mini_object.
- * Takes over the ownership of the caller's reference to @mini_object;
- * the caller doesn't have to unref it any more.
+ * gst_mini_object_weak_unref: (skip)
+ * @object: #GstMiniObject to remove a weak reference from
+ * @notify: callback to search for
+ * @data: data to search for
+ *
+ * Removes a weak reference callback from a mini object.
  */
 void
-gst_value_take_mini_object (GValue * value, GstMiniObject * mini_object)
+gst_mini_object_weak_unref (GstMiniObject * object,
+    GstMiniObjectNotify notify, gpointer data)
 {
-  gpointer *pointer_p;
+  gint i;
 
-  g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
-  g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (notify != NULL);
 
-  pointer_p = &value->data[0].v_pointer;
-  /* takes additional refcount */
-  gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
-  /* remove additional refcount */
-  if (mini_object)
-    gst_mini_object_unref (mini_object);
+  G_LOCK (qdata_mutex);
+  if ((i = find_notify (object, weak_ref_quark, TRUE, notify, data)) != -1) {
+    remove_notify (object, i);
+  } else {
+    g_warning ("%s: couldn't find weak ref %p (object:%p data:%p)", G_STRFUNC,
+        notify, object, data);
+  }
+  G_UNLOCK (qdata_mutex);
 }
 
 /**
- * gst_value_get_mini_object:
- * @value:   a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
+ * gst_mini_object_set_qdata:
+ * @object: a #GstMiniObject
+ * @quark: A #GQuark, naming the user data pointer
+ * @data: An opaque user data pointer
+ * @destroy: Function to invoke with @data as argument, when @data
+ *           needs to be freed
  *
- * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue.
- * Does not increase the refcount of the returned object.
+ * This sets an opaque, named pointer on a miniobject.
+ * The name is specified through a #GQuark (retrieved e.g. via
+ * g_quark_from_static_string()), and the pointer
+ * can be gotten back from the @object with gst_mini_object_get_qdata()
+ * until the @object is disposed.
+ * Setting a previously set user data pointer, overrides (frees)
+ * the old pointer set, using %NULL as pointer essentially
+ * removes the data stored.
  *
- * Returns: (transfer none): mini object contents of @value
+ * @destroy may be specified which is called with @data as argument
+ * when the @object is disposed, or the data is being overwritten by
+ * a call to gst_mini_object_set_qdata() with the same @quark.
  */
-GstMiniObject *
-gst_value_get_mini_object (const GValue * value)
+void
+gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
+    gpointer data, GDestroyNotify destroy)
 {
-  g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
+  gint i;
+  gpointer old_data = NULL;
+  GDestroyNotify old_notify = NULL;
+
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (quark > 0);
+
+  G_LOCK (qdata_mutex);
+  if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
+    PrivData *priv_data = object->priv_pointer;
+
+    old_data = QDATA_DATA (priv_data, i);
+    old_notify = QDATA_DESTROY (priv_data, i);
+
+    if (data == NULL)
+      remove_notify (object, i);
+  }
+  if (data != NULL)
+    set_notify (object, i, quark, NULL, data, destroy);
+  G_UNLOCK (qdata_mutex);
 
-  return value->data[0].v_pointer;
+  if (old_notify)
+    old_notify (old_data);
 }
 
 /**
- * gst_value_dup_mini_object:
- * @value:   a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
- *
- * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue,
- * increasing its reference count. If the contents of the #GValue
- * are %NULL, %NULL will be returned.
+ * gst_mini_object_get_qdata:
+ * @object: The GstMiniObject to get a stored user data pointer from
+ * @quark: A #GQuark, naming the user data pointer
  *
- * Returns: (transfer full): mini object contents of @value
+ * This function gets back user data pointers stored via
+ * gst_mini_object_set_qdata().
  *
- * Since: 0.10.20
+ * Returns: (transfer none) (nullable): The user data pointer set, or
+ * %NULL
  */
-GstMiniObject *
-gst_value_dup_mini_object (const GValue * value)
+gpointer
+gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
 {
-  g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
-
-  return value->data[0].v_pointer ? gst_mini_object_ref (value->
-      data[0].v_pointer) : NULL;
-}
+  guint i;
+  gpointer result;
 
+  g_return_val_if_fail (object != NULL, NULL);
+  g_return_val_if_fail (quark > 0, NULL);
 
-/* param spec */
+  G_LOCK (qdata_mutex);
+  if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
+    PrivData *priv_data = object->priv_pointer;
+    result = QDATA_DATA (priv_data, i);
+  } else {
+    result = NULL;
+  }
+  G_UNLOCK (qdata_mutex);
 
-static void
-param_mini_object_init (GParamSpec * pspec)
-{
-  /* GParamSpecMiniObject *ospec = G_PARAM_SPEC_MINI_OBJECT (pspec); */
+  return result;
 }
 
-static void
-param_mini_object_set_default (GParamSpec * pspec, GValue * value)
+/**
+ * gst_mini_object_steal_qdata:
+ * @object: The GstMiniObject to get a stored user data pointer from
+ * @quark: A #GQuark, naming the user data pointer
+ *
+ * This function gets back user data pointers stored via gst_mini_object_set_qdata()
+ * and removes the data from @object without invoking its destroy() function (if
+ * any was set).
+ *
+ * Returns: (transfer full) (nullable): The user data pointer set, or
+ * %NULL
+ */
+gpointer
+gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
 {
-  value->data[0].v_pointer = NULL;
-}
+  guint i;
+  gpointer result;
 
-static gboolean
-param_mini_object_validate (GParamSpec * pspec, GValue * value)
-{
-  GstMiniObject *mini_object = value->data[0].v_pointer;
-  gboolean changed = FALSE;
+  g_return_val_if_fail (object != NULL, NULL);
+  g_return_val_if_fail (quark > 0, NULL);
 
-  if (mini_object
-      && !g_value_type_compatible (G_OBJECT_TYPE (mini_object),
-          pspec->value_type)) {
-    gst_mini_object_unref (mini_object);
-    value->data[0].v_pointer = NULL;
-    changed = TRUE;
+  G_LOCK (qdata_mutex);
+  if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
+    PrivData *priv_data = object->priv_pointer;
+    result = QDATA_DATA (priv_data, i);
+    remove_notify (object, i);
+  } else {
+    result = NULL;
   }
+  G_UNLOCK (qdata_mutex);
 
-  return changed;
+  return result;
 }
 
-static gint
-param_mini_object_values_cmp (GParamSpec * pspec,
-    const GValue * value1, const GValue * value2)
+/**
+ * gst_mini_object_add_parent:
+ * @object: a #GstMiniObject
+ * @parent: a parent #GstMiniObject
+ *
+ * This adds @parent as a parent for @object. Having one ore more parents affects the
+ * writability of @object: if a @parent is not writable, @object is also not
+ * writable, regardless of its refcount. @object is only writable if all
+ * the parents are writable and its own refcount is exactly 1.
+ *
+ * Note: This function does not take ownership of @parent and also does not
+ * take an additional reference. It is the responsibility of the caller to
+ * remove the parent again at a later time.
+ *
+ * Since: 1.16
+ */
+void
+gst_mini_object_add_parent (GstMiniObject * object, GstMiniObject * parent)
 {
-  guint8 *p1 = value1->data[0].v_pointer;
-  guint8 *p2 = value2->data[0].v_pointer;
+  gint priv_state;
 
-  /* not much to compare here, try to at least provide stable lesser/greater result */
+  g_return_if_fail (object != NULL);
 
-  return p1 < p2 ? -1 : p1 > p2;
-}
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "adding parent %p to object %p", parent,
+      object);
 
-GType
-gst_param_spec_mini_object_get_type (void)
-{
-  static GType type;
-
-  if (G_UNLIKELY (type) == 0) {
-    static const GParamSpecTypeInfo pspec_info = {
-      sizeof (GstParamSpecMiniObject),  /* instance_size */
-      16,                       /* n_preallocs */
-      param_mini_object_init,   /* instance_init */
-      G_TYPE_OBJECT,            /* value_type */
-      NULL,                     /* finalize */
-      param_mini_object_set_default,    /* value_set_default */
-      param_mini_object_validate,       /* value_validate */
-      param_mini_object_values_cmp,     /* values_cmp */
-    };
-    /* FIXME 0.11: Should really be GstParamSpecMiniObject */
-    type = g_param_type_register_static ("GParamSpecMiniObject", &pspec_info);
+  priv_state = lock_priv_pointer (object);
+  /* If we already had one parent, we need to allocate the full struct now */
+  if (priv_state == PRIV_DATA_STATE_ONE_PARENT) {
+    /* Unlock again */
+    g_atomic_int_set ((gint *) & object->priv_uint, priv_state);
+
+    ensure_priv_data (object);
+    priv_state = PRIV_DATA_STATE_PARENTS_OR_QDATA;
   }
 
-  return type;
+  /* Now we either have to add the new parent to the full struct, or add
+   * our one and only parent to the pointer field */
+  if (priv_state == PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    PrivData *priv_data = object->priv_pointer;
+
+    /* Lock parents */
+    while (!g_atomic_int_compare_and_exchange (&priv_data->parent_lock, 0, 1));
+
+    if (priv_data->n_parents >= priv_data->n_parents_len) {
+      priv_data->n_parents_len *= 2;
+      if (priv_data->n_parents_len == 0)
+        priv_data->n_parents_len = 16;
+
+      priv_data->parents =
+          g_realloc (priv_data->parents,
+          priv_data->n_parents_len * sizeof (GstMiniObject *));
+    }
+    priv_data->parents[priv_data->n_parents] = parent;
+    priv_data->n_parents++;
+
+    /* Unlock again */
+    g_atomic_int_set (&priv_data->parent_lock, 0);
+  } else if (priv_state == PRIV_DATA_STATE_NO_PARENT) {
+    object->priv_pointer = parent;
+
+    /* Unlock again */
+    g_atomic_int_set ((gint *) & object->priv_uint, PRIV_DATA_STATE_ONE_PARENT);
+  } else {
+    g_assert_not_reached ();
+  }
 }
 
 /**
- * gst_param_spec_mini_object:
- * @name: the canonical name of the property
- * @nick: the nickname of the property
- * @blurb: a short description of the property
- * @object_type: the #GstMiniObject #GType for the property
- * @flags: a combination of #GParamFlags
+ * gst_mini_object_remove_parent:
+ * @object: a #GstMiniObject
+ * @parent: a parent #GstMiniObject
  *
- * Creates a new #GParamSpec instance that hold #GstMiniObject references.
+ * This removes @parent as a parent for @object. See
+ * gst_mini_object_add_parent().
  *
- * Returns: (transfer full): a newly allocated #GParamSpec instance
+ * Since: 1.16
  */
-GParamSpec *
-gst_param_spec_mini_object (const char *name, const char *nick,
-    const char *blurb, GType object_type, GParamFlags flags)
+void
+gst_mini_object_remove_parent (GstMiniObject * object, GstMiniObject * parent)
 {
-  GstParamSpecMiniObject *ospec;
+  gint priv_state;
+
+  g_return_if_fail (object != NULL);
 
-  g_return_val_if_fail (g_type_is_a (object_type, GST_TYPE_MINI_OBJECT), NULL);
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "removing parent %p from object %p",
+      parent, object);
 
-  ospec = g_param_spec_internal (GST_TYPE_PARAM_MINI_OBJECT,
-      name, nick, blurb, flags);
-  G_PARAM_SPEC (ospec)->value_type = object_type;
+  priv_state = lock_priv_pointer (object);
 
-  return G_PARAM_SPEC (ospec);
+  /* Now we either have to add the new parent to the full struct, or add
+   * our one and only parent to the pointer field */
+  if (priv_state == PRIV_DATA_STATE_PARENTS_OR_QDATA) {
+    PrivData *priv_data = object->priv_pointer;
+    guint i;
+
+    /* Lock parents */
+    while (!g_atomic_int_compare_and_exchange (&priv_data->parent_lock, 0, 1));
+
+    for (i = 0; i < priv_data->n_parents; i++)
+      if (parent == priv_data->parents[i])
+        break;
+
+    if (i != priv_data->n_parents) {
+      priv_data->n_parents--;
+      if (priv_data->n_parents != i)
+        priv_data->parents[i] = priv_data->parents[priv_data->n_parents];
+    } else {
+      g_warning ("%s: couldn't find parent %p (object:%p)", G_STRFUNC,
+          object, parent);
+    }
+
+    /* Unlock again */
+    g_atomic_int_set (&priv_data->parent_lock, 0);
+  } else if (priv_state == PRIV_DATA_STATE_ONE_PARENT) {
+    if (object->priv_pointer != parent) {
+      g_warning ("%s: couldn't find parent %p (object:%p)", G_STRFUNC,
+          object, parent);
+      /* Unlock again */
+      g_atomic_int_set ((gint *) & object->priv_uint, priv_state);
+    } else {
+      object->priv_pointer = NULL;
+      /* Unlock again */
+      g_atomic_int_set ((gint *) & object->priv_uint,
+          PRIV_DATA_STATE_NO_PARENT);
+    }
+  } else {
+    /* Unlock again */
+    g_atomic_int_set ((gint *) & object->priv_uint, PRIV_DATA_STATE_NO_PARENT);
+  }
 }