2 * Copyright (C) 2005 David Schleef <ds@schleef.org>
4 * gstminiobject.h: Header for GstMiniObject
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:gstminiobject
23 * @short_description: Lightweight base class for the GStreamer object hierarchy
25 * #GstMiniObject is a baseclass like #GObject, but has been stripped down of
26 * features to be fast and small.
27 * It offers sub-classing and ref-counting in the same way as #GObject does.
28 * It has no properties and no signal-support though.
30 * Last reviewed on 2005-11-23 (0.9.5)
36 #include "gst/gst_private.h"
37 #include "gst/gstminiobject.h"
38 #include "gst/gstinfo.h"
39 #include <gobject/gvaluecollector.h>
41 #ifndef GST_DISABLE_TRACE
43 static GstAllocTrace *_gst_mini_object_trace;
46 #define GST_MINI_OBJECT_GET_CLASS_UNCHECKED(obj) \
47 ((GstMiniObjectClass *) (((GTypeInstance*)(obj))->g_class))
49 /* Structure used for storing weak references */
52 GstMiniObject *object;
56 GstMiniObjectWeakNotify notify;
58 } weak_refs[1]; /* flexible array */
61 /* Structure for storing a mini object's private data */
62 struct _GstMiniObjectPrivate
68 static void gst_mini_object_base_init (gpointer g_class);
69 static void gst_mini_object_base_finalize (gpointer g_class);
71 static void gst_mini_object_class_init (gpointer g_class, gpointer class_data);
72 static void gst_mini_object_init (GTypeInstance * instance, gpointer klass);
74 static void gst_value_mini_object_init (GValue * value);
75 static void gst_value_mini_object_free (GValue * value);
76 static void weak_refs_notify (WeakRefStack * data);
77 static void gst_value_mini_object_copy (const GValue * src_value,
79 static gpointer gst_value_mini_object_peek_pointer (const GValue * value);
80 static gchar *gst_value_mini_object_collect (GValue * value,
81 guint n_collect_values, GTypeCValue * collect_values, guint collect_flags);
82 static gchar *gst_value_mini_object_lcopy (const GValue * value,
83 guint n_collect_values, GTypeCValue * collect_values, guint collect_flags);
85 static GstMiniObject *gst_mini_object_copy_default (const GstMiniObject * obj);
86 static void gst_mini_object_finalize (GstMiniObject * obj);
88 /* Mutex used for weak referencing */
89 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
92 gst_mini_object_get_type (void)
94 static volatile GType _gst_mini_object_type = 0;
96 if (g_once_init_enter (&_gst_mini_object_type)) {
98 static const GTypeValueTable value_table = {
99 gst_value_mini_object_init,
100 gst_value_mini_object_free,
101 gst_value_mini_object_copy,
102 gst_value_mini_object_peek_pointer,
104 gst_value_mini_object_collect,
106 gst_value_mini_object_lcopy
108 static const GTypeInfo mini_object_info = {
109 sizeof (GstMiniObjectClass),
111 gst_mini_object_base_init,
112 gst_mini_object_base_finalize,
116 gst_mini_object_class_init,
119 sizeof (GstMiniObject),
121 (GInstanceInitFunc) gst_mini_object_init,
124 static const GTypeFundamentalInfo mini_object_fundamental_info = {
125 (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE |
126 G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE)
129 _type = g_type_fundamental_next ();
130 g_type_register_fundamental (_type, "GstMiniObject",
131 &mini_object_info, &mini_object_fundamental_info, G_TYPE_FLAG_ABSTRACT);
133 #ifndef GST_DISABLE_TRACE
134 _gst_mini_object_trace = gst_alloc_trace_register (g_type_name (_type));
136 g_once_init_leave (&_gst_mini_object_type, _type);
139 return _gst_mini_object_type;
144 gst_mini_object_base_init (gpointer g_class)
150 gst_mini_object_base_finalize (gpointer g_class)
157 gst_mini_object_class_init (gpointer g_class, gpointer class_data)
159 GstMiniObjectClass *mo_class = GST_MINI_OBJECT_CLASS (g_class);
161 mo_class->copy = gst_mini_object_copy_default;
162 mo_class->finalize = gst_mini_object_finalize;
164 /* Set the instance data type */
165 g_type_class_add_private (g_class, sizeof (GstMiniObjectPrivate));
169 gst_mini_object_init (GTypeInstance * instance, gpointer klass)
171 GstMiniObject *mini_object = GST_MINI_OBJECT_CAST (instance);
173 mini_object->refcount = 1;
175 /* we delay initialising the mini object's private data until it's actually
176 * needed for now (mini_object->priv automatically inited to NULL) */
179 static GstMiniObject *
180 gst_mini_object_copy_default (const GstMiniObject * obj)
182 g_warning ("GstMiniObject classes must implement GstMiniObject::copy");
187 gst_mini_object_finalize (GstMiniObject * obj)
191 /* WARNING: if anything is ever put in this method, make sure that the
192 * following sub-classes' finalize method chains up to this one:
201 * gst_mini_object_new:
202 * @type: the #GType of the mini-object to create
204 * Creates a new mini-object of the desired type.
208 * Returns: (transfer full): the new mini-object.
211 gst_mini_object_new (GType type)
213 GstMiniObject *mini_object;
215 /* we don't support dynamic types because they really aren't useful,
216 * and could cause refcount problems */
217 mini_object = (GstMiniObject *) g_type_create_instance (type);
219 #ifndef GST_DISABLE_TRACE
220 gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
226 /* FIXME 0.11: Current way of doing the copy makes it impossible
227 * to currectly chain to the parent classes and do a copy in a
228 * subclass without knowing all internals of the parent classes.
230 * For 0.11 we should do something like the following:
231 * - The GstMiniObjectClass::copy() implementation of GstMiniObject
232 * should call g_type_create_instance() with the type of the source
234 * - All GstMiniObjectClass::copy() implementations should as first
235 * thing chain up to the parent class and then do whatever they need
236 * to do to copy their type specific data. Note that this way the
237 * instance_init() functions are called!
241 * gst_mini_object_copy:
242 * @mini_object: the mini-object to copy
244 * Creates a copy of the mini-object.
248 * Returns: (transfer full): the new mini-object.
251 gst_mini_object_copy (const GstMiniObject * mini_object)
253 GstMiniObjectClass *mo_class;
255 g_return_val_if_fail (mini_object != NULL, NULL);
257 mo_class = GST_MINI_OBJECT_GET_CLASS (mini_object);
259 return mo_class->copy (mini_object);
263 * gst_mini_object_is_writable:
264 * @mini_object: the mini-object to check
266 * Checks if a mini-object is writable. A mini-object is writable
267 * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
268 * flag is not set. Modification of a mini-object should only be
269 * done after verifying that it is writable.
273 * Returns: TRUE if the object is writable.
276 gst_mini_object_is_writable (const GstMiniObject * mini_object)
278 g_return_val_if_fail (mini_object != NULL, FALSE);
280 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1) &&
281 ((mini_object->flags & GST_MINI_OBJECT_FLAG_READONLY) == 0);
285 * gst_mini_object_make_writable:
286 * @mini_object: (transfer full): the mini-object to make writable
288 * Checks if a mini-object is writable. If not, a writable copy is made and
289 * returned. This gives away the reference to the original mini object,
290 * and returns a reference to the new object.
294 * Returns: (transfer full): a mini-object (possibly the same pointer) that
298 gst_mini_object_make_writable (GstMiniObject * mini_object)
302 g_return_val_if_fail (mini_object != NULL, NULL);
304 if (gst_mini_object_is_writable (mini_object)) {
307 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject",
308 g_type_name (G_TYPE_FROM_INSTANCE (mini_object)));
309 ret = gst_mini_object_copy (mini_object);
310 gst_mini_object_unref (mini_object);
317 * gst_mini_object_ref:
318 * @mini_object: the mini-object
320 * Increase the reference count of the mini-object.
322 * Note that the refcount affects the writeability
323 * of @mini-object, see gst_mini_object_is_writable(). It is
324 * important to note that keeping additional references to
325 * GstMiniObject instances can potentially increase the number
326 * of memcpy operations in a pipeline, especially if the miniobject
329 * Returns: (transfer full): the mini-object.
332 gst_mini_object_ref (GstMiniObject * mini_object)
334 g_return_val_if_fail (mini_object != NULL, NULL);
335 /* we can't assert that the refcount > 0 since the _free functions
336 * increments the refcount from 0 to 1 again to allow resurecting
338 g_return_val_if_fail (mini_object->refcount > 0, NULL);
340 g_return_val_if_fail (GST_IS_MINI_OBJECT (mini_object), NULL);
342 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
343 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
344 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
346 g_atomic_int_inc (&mini_object->refcount);
352 weak_refs_notify (WeakRefStack * wstack)
356 for (i = 0; i < wstack->n_weak_refs; i++)
357 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
362 gst_mini_object_free (GstMiniObject * mini_object)
364 GstMiniObjectClass *mo_class;
366 /* At this point, the refcount of the object is 0. We increase the refcount
367 * here because if a subclass recycles the object and gives out a new
368 * reference we don't want to free the instance anymore. */
369 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
370 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
371 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
373 g_atomic_int_inc (&mini_object->refcount);
375 mo_class = GST_MINI_OBJECT_GET_CLASS_UNCHECKED (mini_object);
376 mo_class->finalize (mini_object);
378 /* decrement the refcount again, if the subclass recycled the object we don't
379 * want to free the instance anymore */
380 if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
381 /* The weak reference stack is freed in the notification function */
382 if (mini_object->priv != NULL && mini_object->priv->wstack != NULL)
383 weak_refs_notify (mini_object->priv->wstack);
385 #ifndef GST_DISABLE_TRACE
386 gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
388 g_type_free_instance ((GTypeInstance *) mini_object);
393 * gst_mini_object_unref:
394 * @mini_object: the mini-object
396 * Decreases the reference count of the mini-object, possibly freeing
400 gst_mini_object_unref (GstMiniObject * mini_object)
402 g_return_if_fail (GST_IS_MINI_OBJECT (mini_object));
403 g_return_if_fail (mini_object->refcount > 0);
405 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
407 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
408 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
410 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
411 gst_mini_object_free (mini_object);
416 * gst_mini_object_weak_ref: (skip)
417 * @object: #GstMiniObject to reference weakly
418 * @notify: callback to invoke before the mini object is freed
419 * @data: extra data to pass to notify
421 * Adds a weak reference callback to a mini object. Weak references are
422 * used for notification when a mini object is finalized. They are called
423 * "weak references" because they allow you to safely hold a pointer
424 * to the mini object without calling gst_mini_object_ref()
425 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
431 gst_mini_object_weak_ref (GstMiniObject * object,
432 GstMiniObjectWeakNotify notify, gpointer data)
436 g_return_if_fail (GST_IS_MINI_OBJECT (object));
437 g_return_if_fail (notify != NULL);
438 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
440 G_LOCK (weak_refs_mutex);
442 if (object->priv == NULL) {
443 object->priv = G_TYPE_INSTANCE_GET_PRIVATE (object, GST_TYPE_MINI_OBJECT,
444 GstMiniObjectPrivate);
446 /* object->priv->wstack will have been inited to NULL automatically */
449 if (object->priv->wstack) {
450 /* Don't add the weak reference if it already exists. */
451 for (i = 0; i < object->priv->wstack->n_weak_refs; i++) {
452 if (object->priv->wstack->weak_refs[i].notify == notify &&
453 object->priv->wstack->weak_refs[i].data == data) {
454 g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
455 G_STRFUNC, notify, data);
460 i = object->priv->wstack->n_weak_refs++;
461 object->priv->wstack =
462 g_realloc (object->priv->wstack, sizeof (*(object->priv->wstack)) +
463 sizeof (object->priv->wstack->weak_refs[0]) * i);
465 object->priv->wstack = g_renew (WeakRefStack, NULL, 1);
466 object->priv->wstack->object = object;
467 object->priv->wstack->n_weak_refs = 1;
470 object->priv->wstack->weak_refs[i].notify = notify;
471 object->priv->wstack->weak_refs[i].data = data;
473 G_UNLOCK (weak_refs_mutex);
477 * gst_mini_object_weak_unref: (skip)
478 * @object: #GstMiniObject to remove a weak reference from
479 * @notify: callback to search for
480 * @data: data to search for
482 * Removes a weak reference callback to a mini object.
487 gst_mini_object_weak_unref (GstMiniObject * object,
488 GstMiniObjectWeakNotify notify, gpointer data)
490 gboolean found_one = FALSE;
492 g_return_if_fail (GST_IS_MINI_OBJECT (object));
493 g_return_if_fail (notify != NULL);
495 G_LOCK (weak_refs_mutex);
497 if (object->priv != NULL && object->priv->wstack != NULL) {
500 for (i = 0; i < object->priv->wstack->n_weak_refs; i++)
501 if (object->priv->wstack->weak_refs[i].notify == notify &&
502 object->priv->wstack->weak_refs[i].data == data) {
504 object->priv->wstack->n_weak_refs -= 1;
505 if (i != object->priv->wstack->n_weak_refs)
506 object->priv->wstack->weak_refs[i] =
507 object->priv->wstack->weak_refs[object->priv->wstack->
513 G_UNLOCK (weak_refs_mutex);
515 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
519 * gst_mini_object_replace:
520 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
522 * @newdata: pointer to new mini-object
524 * Modifies a pointer to point to a new mini-object. The modification
525 * is done atomically, and the reference counts are updated correctly.
526 * Either @newdata and the value pointed to by @olddata may be NULL.
529 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
531 GstMiniObject *olddata_val;
533 g_return_if_fail (olddata != NULL);
535 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
536 *olddata, *olddata ? (*olddata)->refcount : 0,
537 newdata, newdata ? newdata->refcount : 0);
539 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
541 if (olddata_val == newdata)
545 gst_mini_object_ref (newdata);
547 while (!g_atomic_pointer_compare_and_exchange ((gpointer *) olddata,
548 olddata_val, newdata)) {
549 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
553 gst_mini_object_unref (olddata_val);
557 gst_value_mini_object_init (GValue * value)
559 value->data[0].v_pointer = NULL;
563 gst_value_mini_object_free (GValue * value)
565 if (value->data[0].v_pointer) {
566 gst_mini_object_unref (GST_MINI_OBJECT_CAST (value->data[0].v_pointer));
571 gst_value_mini_object_copy (const GValue * src_value, GValue * dest_value)
573 if (src_value->data[0].v_pointer) {
574 dest_value->data[0].v_pointer =
575 gst_mini_object_ref (GST_MINI_OBJECT_CAST (src_value->data[0].
578 dest_value->data[0].v_pointer = NULL;
583 gst_value_mini_object_peek_pointer (const GValue * value)
585 return value->data[0].v_pointer;
589 gst_value_mini_object_collect (GValue * value, guint n_collect_values,
590 GTypeCValue * collect_values, guint collect_flags)
592 if (collect_values[0].v_pointer) {
593 value->data[0].v_pointer =
594 gst_mini_object_ref (collect_values[0].v_pointer);
596 value->data[0].v_pointer = NULL;
603 gst_value_mini_object_lcopy (const GValue * value, guint n_collect_values,
604 GTypeCValue * collect_values, guint collect_flags)
606 gpointer *mini_object_p = collect_values[0].v_pointer;
608 if (!mini_object_p) {
609 return g_strdup_printf ("value location for '%s' passed as NULL",
610 G_VALUE_TYPE_NAME (value));
613 if (!value->data[0].v_pointer)
614 *mini_object_p = NULL;
615 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
616 *mini_object_p = value->data[0].v_pointer;
618 *mini_object_p = gst_mini_object_ref (value->data[0].v_pointer);
624 * gst_value_set_mini_object:
625 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
626 * @mini_object: (transfer none): mini object value to set
628 * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
630 * The caller retains ownership of the reference.
633 gst_value_set_mini_object (GValue * value, GstMiniObject * mini_object)
637 g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
638 g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
640 pointer_p = &value->data[0].v_pointer;
641 gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
645 * gst_value_take_mini_object:
646 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
647 * @mini_object: (transfer full): mini object value to take
649 * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
651 * Takes over the ownership of the caller's reference to @mini_object;
652 * the caller doesn't have to unref it any more.
655 gst_value_take_mini_object (GValue * value, GstMiniObject * mini_object)
659 g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
660 g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
662 pointer_p = &value->data[0].v_pointer;
663 /* takes additional refcount */
664 gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
665 /* remove additional refcount */
667 gst_mini_object_unref (mini_object);
671 * gst_value_get_mini_object:
672 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
674 * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue.
675 * Does not increase the refcount of the returned object.
677 * Returns: (transfer none): mini object contents of @value
680 gst_value_get_mini_object (const GValue * value)
682 g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
684 return value->data[0].v_pointer;
688 * gst_value_dup_mini_object:
689 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
691 * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue,
692 * increasing its reference count. If the contents of the #GValue
693 * are %NULL, %NULL will be returned.
695 * Returns: (transfer full): mini object contents of @value
700 gst_value_dup_mini_object (const GValue * value)
702 g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
704 return value->data[0].v_pointer ? gst_mini_object_ref (value->
705 data[0].v_pointer) : NULL;
712 param_mini_object_init (GParamSpec * pspec)
714 /* GParamSpecMiniObject *ospec = G_PARAM_SPEC_MINI_OBJECT (pspec); */
718 param_mini_object_set_default (GParamSpec * pspec, GValue * value)
720 value->data[0].v_pointer = NULL;
724 param_mini_object_validate (GParamSpec * pspec, GValue * value)
726 GstMiniObject *mini_object = value->data[0].v_pointer;
727 gboolean changed = FALSE;
730 && !g_value_type_compatible (G_OBJECT_TYPE (mini_object),
731 pspec->value_type)) {
732 gst_mini_object_unref (mini_object);
733 value->data[0].v_pointer = NULL;
741 param_mini_object_values_cmp (GParamSpec * pspec,
742 const GValue * value1, const GValue * value2)
744 guint8 *p1 = value1->data[0].v_pointer;
745 guint8 *p2 = value2->data[0].v_pointer;
747 /* not much to compare here, try to at least provide stable lesser/greater result */
749 return p1 < p2 ? -1 : p1 > p2;
753 gst_param_spec_mini_object_get_type (void)
757 if (G_UNLIKELY (type) == 0) {
758 static const GParamSpecTypeInfo pspec_info = {
759 sizeof (GstParamSpecMiniObject), /* instance_size */
760 16, /* n_preallocs */
761 param_mini_object_init, /* instance_init */
762 G_TYPE_OBJECT, /* value_type */
764 param_mini_object_set_default, /* value_set_default */
765 param_mini_object_validate, /* value_validate */
766 param_mini_object_values_cmp, /* values_cmp */
768 /* FIXME 0.11: Should really be GstParamSpecMiniObject */
769 type = g_param_type_register_static ("GParamSpecMiniObject", &pspec_info);
776 * gst_param_spec_mini_object:
777 * @name: the canonical name of the property
778 * @nick: the nickname of the property
779 * @blurb: a short description of the property
780 * @object_type: the #GstMiniObject #GType for the property
781 * @flags: a combination of #GParamFlags
783 * Creates a new #GParamSpec instance that hold #GstMiniObject references.
785 * Returns: (transfer full): a newly allocated #GParamSpec instance
788 gst_param_spec_mini_object (const char *name, const char *nick,
789 const char *blurb, GType object_type, GParamFlags flags)
791 GstParamSpecMiniObject *ospec;
793 g_return_val_if_fail (g_type_is_a (object_type, GST_TYPE_MINI_OBJECT), NULL);
795 ospec = g_param_spec_internal (GST_TYPE_PARAM_MINI_OBJECT,
796 name, nick, blurb, flags);
797 G_PARAM_SPEC (ospec)->value_type = object_type;
799 return G_PARAM_SPEC (ospec);