Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstminiobject.c
index 3993e02..78749dd 100644 (file)
 #include "gst/gstinfo.h"
 #include <gobject/gvaluecollector.h>
 
+#define GST_DISABLE_TRACE
+
 #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);
 
-GType
-gst_mini_object_get_type (void)
-{
-  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;
-}
-
-#if 0
-static void
-gst_mini_object_base_init (gpointer g_class)
-{
-  /* do nothing */
-}
-
-static void
-gst_mini_object_base_finalize (gpointer g_class)
-{
-  /* do nothing */
-}
-#endif
-
-static void
-gst_mini_object_class_init (gpointer g_class, gpointer class_data)
-{
-  GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (g_class);
-
-  mo_class->copy = gst_mini_object_copy_default;
-  mo_class->finalize = gst_mini_object_finalize;
-
-  /* Set the instance data type */
-  g_type_class_add_private (g_class, sizeof (GstMiniObjectPrivate));
-}
-
-static void
-gst_mini_object_init (GTypeInstance * instance, gpointer klass)
-{
-  GstMiniObject *mini_object = GST_MINI_OBJECT_CAST (instance);
-
-  mini_object->refcount = 1;
-
-  /* we delay initialising the mini object's private data until it's actually
-   * needed for now (mini_object->priv automatically inited to NULL) */
-}
-
-static GstMiniObject *
-gst_mini_object_copy_default (const GstMiniObject * obj)
-{
-  g_warning ("GstMiniObject classes must implement GstMiniObject::copy");
-  return 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_mini_object_new:
+ * gst_mini_object_init:
+ * @mini_object: a #GstMiniObject 
  * @type: the #GType of the mini-object to create
+ * @size: the size of the data
  *
- * Creates a new mini-object of the desired type.
+ * Initializes a mini-object with the desired type and size.
  *
  * MT safe
  *
  * Returns: (transfer full): the new mini-object.
  */
-GstMiniObject *
-gst_mini_object_new (GType type)
+void
+gst_mini_object_init (GstMiniObject * mini_object, GType type, gsize size)
 {
-  GstMiniObject *mini_object;
-
-  /* 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);
-
-#ifndef GST_DISABLE_TRACE
-  gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
-#endif
-
-  return mini_object;
+  mini_object->type = type;
+  mini_object->refcount = 1;
+  mini_object->flags = 0;
+  mini_object->size = size;
+  mini_object->n_weak_refs = 0;
+  mini_object->weak_refs = NULL;
 }
 
-/* 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
@@ -250,13 +84,16 @@ gst_mini_object_new (GType type)
 GstMiniObject *
 gst_mini_object_copy (const GstMiniObject * mini_object)
 {
-  GstMiniObjectClass *mo_class;
+  GstMiniObject *copy;
 
   g_return_val_if_fail (mini_object != NULL, NULL);
 
-  mo_class = GST_MINI_OBJECT_GET_CLASS (mini_object);
+  if (mini_object->copy)
+    copy = mini_object->copy (mini_object);
+  else
+    copy = NULL;
 
-  return mo_class->copy (mini_object);
+  return copy;
 }
 
 /**
@@ -277,8 +114,7 @@ gst_mini_object_is_writable (const GstMiniObject * mini_object)
 {
   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);
+  return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
 }
 
 /**
@@ -304,9 +140,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);
   }
 
@@ -337,7 +173,6 @@ gst_mini_object_ref (GstMiniObject * mini_object)
    * the object
    g_return_val_if_fail (mini_object->refcount > 0, NULL);
    */
-  g_return_val_if_fail (GST_IS_MINI_OBJECT (mini_object), NULL);
 
   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
@@ -349,44 +184,13 @@ gst_mini_object_ref (GstMiniObject * mini_object)
 }
 
 static void
-weak_refs_notify (WeakRefStack * wstack)
+weak_refs_notify (GstMiniObject * obj)
 {
   guint i;
 
-  for (i = 0; i < wstack->n_weak_refs; i++)
-    wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
-  g_free (wstack);
-}
-
-static void
-gst_mini_object_free (GstMiniObject * mini_object)
-{
-  GstMiniObjectClass *mo_class;
-
-  /* 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);
-
-  g_atomic_int_inc (&mini_object->refcount);
-
-  mo_class = GST_MINI_OBJECT_GET_CLASS_UNCHECKED (mini_object);
-  mo_class->finalize (mini_object);
-
-  /* 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);
-
-#ifndef GST_DISABLE_TRACE
-    gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
-#endif
-    g_type_free_instance ((GTypeInstance *) mini_object);
-  }
+  for (i = 0; i < obj->n_weak_refs; i++)
+    obj->weak_refs[i].notify (obj->weak_refs[i].data, obj);
+  g_free (obj->weak_refs);
 }
 
 /**
@@ -399,7 +203,7 @@ 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 != NULL);
   g_return_if_fail (mini_object->refcount > 0);
 
   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
@@ -408,111 +212,27 @@ gst_mini_object_unref (GstMiniObject * mini_object)
       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
 
   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
-    gst_mini_object_free (mini_object);
-  }
-}
-
-/**
- * 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.35
- */
-void
-gst_mini_object_weak_ref (GstMiniObject * object,
-    GstMiniObjectWeakNotify notify, gpointer data)
-{
-  guint i;
-
-  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);
+    gboolean do_free;
 
-  G_LOCK (weak_refs_mutex);
+    if (mini_object->dispose)
+      do_free = mini_object->dispose (mini_object);
+    else
+      do_free = TRUE;
 
-  if (object->priv == NULL) {
-    object->priv = G_TYPE_INSTANCE_GET_PRIVATE (object, GST_TYPE_MINI_OBJECT,
-        GstMiniObjectPrivate);
+    /* if the subclass recycled the object (and returned FALSE) we don't
+     * want to free the instance anymore */
+    if (G_LIKELY (do_free)) {
+      /* The weak reference stack is freed in the notification function */
+      if (mini_object->n_weak_refs)
+        weak_refs_notify (mini_object);
 
-    /* object->priv->wstack will have been inited to NULL automatically */
-  }
-
-  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;
-      }
+#ifndef GST_DISABLE_TRACE
+      gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
+#endif
+      if (mini_object->free)
+        mini_object->free (mini_object);
     }
-
-    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;
   }
-  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
- *
- * Removes a weak reference callback to a mini object.
- *
- * Since: 0.10.35
- */
-void
-gst_mini_object_weak_unref (GstMiniObject * object,
-    GstMiniObjectWeakNotify notify, gpointer data)
-{
-  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);
 }
 
 /**
@@ -521,16 +241,20 @@ gst_mini_object_weak_unref (GstMiniObject * object,
  *     be replaced
  * @newdata: pointer to new mini-object
  *
- * Modifies a pointer to point to a new mini-object.  The modification
- * is done atomically, and the reference counts are updated correctly.
+ * 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.
+ *
+ * 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 +262,182 @@ 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;
-}
-
-static void
-gst_value_mini_object_free (GValue * value)
-{
-  if (value->data[0].v_pointer) {
-    gst_mini_object_unref (GST_MINI_OBJECT_CAST (value->data[0].v_pointer));
-  }
-}
 
-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;
-  }
-}
-
-static gpointer
-gst_value_mini_object_peek_pointer (const GValue * value)
-{
-  return value->data[0].v_pointer;
-}
-
-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;
-  }
-
-  return NULL;
-}
-
-static gchar *
-gst_value_mini_object_lcopy (const GValue * value, guint n_collect_values,
-    GTypeCValue * collect_values, guint collect_flags)
-{
-  gpointer *mini_object_p = collect_values[0].v_pointer;
-
-  if (!mini_object_p) {
-    return g_strdup_printf ("value location for '%s' passed as NULL",
-        G_VALUE_TYPE_NAME (value));
-  }
-
-  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);
-
-  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_steal:
+ * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
+ *     be stolen
  *
- * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
- * @mini_object.
- * The caller retains ownership of the reference.
- */
-void
-gst_value_set_mini_object (GValue * value, GstMiniObject * mini_object)
-{
-  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));
-
-  pointer_p = &value->data[0].v_pointer;
-  gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
-}
-
-/**
- * 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.
+ * Replace the current #GstMiniObject pointer to by @olddata with NULL and
+ * return the old value.
+ *
+ * Returns: the #GstMiniObject at @oldata
  */
-void
-gst_value_take_mini_object (GValue * value, GstMiniObject * mini_object)
+GstMiniObject *
+gst_mini_object_steal (GstMiniObject ** olddata)
 {
-  gpointer *pointer_p;
+  GstMiniObject *olddata_val;
 
-  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_val_if_fail (olddata != NULL, 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);
-}
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
+      *olddata, *olddata ? (*olddata)->refcount : 0);
 
-/**
- * gst_value_get_mini_object:
- * @value:   a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
- *
- * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue.
- * Does not increase the refcount of the returned object.
- *
- * Returns: (transfer none): mini object contents of @value
- */
-GstMiniObject *
-gst_value_get_mini_object (const GValue * value)
-{
-  g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), 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 value->data[0].v_pointer;
+  return olddata_val;
 }
 
 /**
- * gst_value_dup_mini_object:
- * @value:   a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
+ * 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
  *
- * 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.
+ * 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.
  *
- * Returns: (transfer full): mini object contents of @value
+ * Either @newdata and the value pointed to by @olddata may be NULL.
  *
- * Since: 0.10.20
+ * Returns: TRUE if @newdata was different from @olddata
  */
-GstMiniObject *
-gst_value_dup_mini_object (const GValue * value)
+gboolean
+gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
 {
-  g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
+  GstMiniObject *olddata_val;
 
-  return value->data[0].v_pointer ? gst_mini_object_ref (value->
-      data[0].v_pointer) : NULL;
-}
+  g_return_val_if_fail (olddata != NULL, FALSE);
 
+  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
+      *olddata, *olddata ? (*olddata)->refcount : 0,
+      newdata, newdata ? newdata->refcount : 0);
 
-/* param spec */
+  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)));
 
-static void
-param_mini_object_init (GParamSpec * pspec)
-{
-  /* GParamSpecMiniObject *ospec = G_PARAM_SPEC_MINI_OBJECT (pspec); */
-}
+  if (olddata_val)
+    gst_mini_object_unref (olddata_val);
 
-static void
-param_mini_object_set_default (GParamSpec * pspec, GValue * value)
-{
-  value->data[0].v_pointer = NULL;
+  return olddata_val != newdata;
 }
 
-static gboolean
-param_mini_object_validate (GParamSpec * pspec, GValue * value)
+/**
+ * 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.35
+ */
+void
+gst_mini_object_weak_ref (GstMiniObject * object,
+    GstMiniObjectWeakNotify notify, gpointer data)
 {
-  GstMiniObject *mini_object = value->data[0].v_pointer;
-  gboolean changed = FALSE;
-
-  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;
-  }
-
-  return changed;
-}
+  guint i;
 
-static gint
-param_mini_object_values_cmp (GParamSpec * pspec,
-    const GValue * value1, const GValue * value2)
-{
-  guint8 *p1 = value1->data[0].v_pointer;
-  guint8 *p2 = value2->data[0].v_pointer;
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (notify != NULL);
+  g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
 
-  /* not much to compare here, try to at least provide stable lesser/greater result */
+  G_LOCK (weak_refs_mutex);
 
-  return p1 < p2 ? -1 : p1 > p2;
-}
+  if (object->n_weak_refs) {
+    /* Don't add the weak reference if it already exists. */
+    for (i = 0; i < object->n_weak_refs; i++) {
+      if (object->weak_refs[i].notify == notify &&
+          object->weak_refs[i].data == data) {
+        g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
+            G_STRFUNC, notify, data);
+        goto found;
+      }
+    }
 
-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);
+    i = object->n_weak_refs++;
+    object->weak_refs =
+        g_realloc (object->weak_refs, sizeof (object->weak_refs[0]) * i);
+  } else {
+    object->weak_refs = g_malloc0 (sizeof (object->weak_refs[0]));
+    object->n_weak_refs = 1;
+    i = 0;
   }
-
-  return type;
+  object->weak_refs[i].notify = notify;
+  object->weak_refs[i].data = data;
+found:
+  G_UNLOCK (weak_refs_mutex);
 }
 
 /**
- * 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_weak_unref: (skip)
+ * @object: #GstMiniObject to remove a weak reference from
+ * @notify: callback to search for
+ * @data: data to search for
  *
- * Creates a new #GParamSpec instance that hold #GstMiniObject references.
+ * Removes a weak reference callback to a mini object.
  *
- * Returns: (transfer full): a newly allocated #GParamSpec instance
+ * Since: 0.10.35
  */
-GParamSpec *
-gst_param_spec_mini_object (const char *name, const char *nick,
-    const char *blurb, GType object_type, GParamFlags flags)
+void
+gst_mini_object_weak_unref (GstMiniObject * object,
+    GstMiniObjectWeakNotify notify, gpointer data)
 {
-  GstParamSpecMiniObject *ospec;
+  gboolean found_one = FALSE;
 
-  g_return_val_if_fail (g_type_is_a (object_type, GST_TYPE_MINI_OBJECT), NULL);
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (notify != NULL);
+
+  G_LOCK (weak_refs_mutex);
 
-  ospec = g_param_spec_internal (GST_TYPE_PARAM_MINI_OBJECT,
-      name, nick, blurb, flags);
-  G_PARAM_SPEC (ospec)->value_type = object_type;
+  if (object->n_weak_refs) {
+    guint i;
 
-  return G_PARAM_SPEC (ospec);
+    for (i = 0; i < object->n_weak_refs; i++)
+      if (object->weak_refs[i].notify == notify &&
+          object->weak_refs[i].data == data) {
+        found_one = TRUE;
+        object->n_weak_refs -= 1;
+        if (i != object->n_weak_refs)
+          object->weak_refs[i] = object->weak_refs[object->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);
 }