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 /* Initialize the mini object's private data */
177 mini_object->priv = (GstMiniObjectPrivate *)
178 G_TYPE_INSTANCE_GET_PRIVATE (instance, GST_TYPE_MINI_OBJECT,
179 GstMiniObjectPrivate);
181 mini_object->priv->wstack = NULL;
184 static GstMiniObject *
185 gst_mini_object_copy_default (const GstMiniObject * obj)
187 g_warning ("GstMiniObject classes must implement GstMiniObject::copy");
192 gst_mini_object_finalize (GstMiniObject * obj)
196 /* WARNING: if anything is ever put in this method, make sure that the
197 * following sub-classes' finalize method chains up to this one:
206 * gst_mini_object_new:
207 * @type: the #GType of the mini-object to create
209 * Creates a new mini-object of the desired type.
213 * Returns: (transfer full): the new mini-object.
216 gst_mini_object_new (GType type)
218 GstMiniObject *mini_object;
220 /* we don't support dynamic types because they really aren't useful,
221 * and could cause refcount problems */
222 mini_object = (GstMiniObject *) g_type_create_instance (type);
224 #ifndef GST_DISABLE_TRACE
225 gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
231 /* FIXME 0.11: Current way of doing the copy makes it impossible
232 * to currectly chain to the parent classes and do a copy in a
233 * subclass without knowing all internals of the parent classes.
235 * For 0.11 we should do something like the following:
236 * - The GstMiniObjectClass::copy() implementation of GstMiniObject
237 * should call g_type_create_instance() with the type of the source
239 * - All GstMiniObjectClass::copy() implementations should as first
240 * thing chain up to the parent class and then do whatever they need
241 * to do to copy their type specific data. Note that this way the
242 * instance_init() functions are called!
246 * gst_mini_object_copy:
247 * @mini_object: the mini-object to copy
249 * Creates a copy of the mini-object.
253 * Returns: (transfer full): the new mini-object.
256 gst_mini_object_copy (const GstMiniObject * mini_object)
258 GstMiniObjectClass *mo_class;
260 g_return_val_if_fail (mini_object != NULL, NULL);
262 mo_class = GST_MINI_OBJECT_GET_CLASS (mini_object);
264 return mo_class->copy (mini_object);
268 * gst_mini_object_is_writable:
269 * @mini_object: the mini-object to check
271 * Checks if a mini-object is writable. A mini-object is writable
272 * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
273 * flag is not set. Modification of a mini-object should only be
274 * done after verifying that it is writable.
278 * Returns: TRUE if the object is writable.
281 gst_mini_object_is_writable (const GstMiniObject * mini_object)
283 g_return_val_if_fail (mini_object != NULL, FALSE);
285 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1) &&
286 ((mini_object->flags & GST_MINI_OBJECT_FLAG_READONLY) == 0);
290 * gst_mini_object_make_writable:
291 * @mini_object: (transfer full): the mini-object to make writable
293 * Checks if a mini-object is writable. If not, a writable copy is made and
294 * returned. This gives away the reference to the original mini object,
295 * and returns a reference to the new object.
299 * Returns: (transfer full): a mini-object (possibly the same pointer) that
303 gst_mini_object_make_writable (GstMiniObject * mini_object)
307 g_return_val_if_fail (mini_object != NULL, NULL);
309 if (gst_mini_object_is_writable (mini_object)) {
312 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject",
313 g_type_name (G_TYPE_FROM_INSTANCE (mini_object)));
314 ret = gst_mini_object_copy (mini_object);
315 gst_mini_object_unref (mini_object);
322 * gst_mini_object_ref:
323 * @mini_object: the mini-object
325 * Increase the reference count of the mini-object.
327 * Note that the refcount affects the writeability
328 * of @mini-object, see gst_mini_object_is_writable(). It is
329 * important to note that keeping additional references to
330 * GstMiniObject instances can potentially increase the number
331 * of memcpy operations in a pipeline, especially if the miniobject
334 * Returns: (transfer full): the mini-object.
337 gst_mini_object_ref (GstMiniObject * mini_object)
339 g_return_val_if_fail (mini_object != NULL, NULL);
340 /* we can't assert that the refcount > 0 since the _free functions
341 * increments the refcount from 0 to 1 again to allow resurecting
343 g_return_val_if_fail (mini_object->refcount > 0, NULL);
345 g_return_val_if_fail (GST_IS_MINI_OBJECT (mini_object), NULL);
347 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
348 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
349 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
351 g_atomic_int_inc (&mini_object->refcount);
357 weak_refs_notify (WeakRefStack * wstack)
361 for (i = 0; i < wstack->n_weak_refs; i++)
362 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
367 gst_mini_object_free (GstMiniObject * mini_object)
369 GstMiniObjectClass *mo_class;
371 /* At this point, the refcount of the object is 0. We increase the refcount
372 * here because if a subclass recycles the object and gives out a new
373 * reference we don't want to free the instance anymore. */
374 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
375 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
376 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
378 g_atomic_int_inc (&mini_object->refcount);
380 mo_class = GST_MINI_OBJECT_GET_CLASS_UNCHECKED (mini_object);
381 mo_class->finalize (mini_object);
383 /* decrement the refcount again, if the subclass recycled the object we don't
384 * want to free the instance anymore */
385 if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
386 /* The weak reference stack is freed in the notification function */
387 if (mini_object->priv->wstack)
388 weak_refs_notify (mini_object->priv->wstack);
390 #ifndef GST_DISABLE_TRACE
391 gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
393 g_type_free_instance ((GTypeInstance *) mini_object);
398 * gst_mini_object_unref:
399 * @mini_object: the mini-object
401 * Decreases the reference count of the mini-object, possibly freeing
405 gst_mini_object_unref (GstMiniObject * mini_object)
407 g_return_if_fail (GST_IS_MINI_OBJECT (mini_object));
408 g_return_if_fail (mini_object->refcount > 0);
410 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
412 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
413 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
415 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
416 gst_mini_object_free (mini_object);
421 * gst_mini_object_weak_ref: (skip)
422 * @mini_object: #GstMiniObject to reference weakly
423 * @notify: callback to invoke before the mini object is freed
424 * @data: extra data to pass to notify
426 * Adds a weak reference callback to a mini object. Weak references are
427 * used for notification when a mini object is finalized. They are called
428 * "weak references" because they allow you to safely hold a pointer
429 * to the mini object without calling gst_mini_object_ref()
430 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
436 gst_mini_object_weak_ref (GstMiniObject * object,
437 GstMiniObjectWeakNotify notify, gpointer data)
441 g_return_if_fail (GST_IS_MINI_OBJECT (object));
442 g_return_if_fail (notify != NULL);
443 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
445 G_LOCK (weak_refs_mutex);
447 if (object->priv->wstack) {
448 /* Don't add the weak reference if it already exists. */
449 for (i = 0; i < object->priv->wstack->n_weak_refs; i++) {
450 if (object->priv->wstack->weak_refs[i].notify == notify &&
451 object->priv->wstack->weak_refs[i].data == data) {
452 g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
453 G_STRFUNC, notify, data);
458 i = object->priv->wstack->n_weak_refs++;
459 object->priv->wstack =
460 g_realloc (object->priv->wstack, sizeof (*(object->priv->wstack)) +
461 sizeof (object->priv->wstack->weak_refs[0]) * i);
463 object->priv->wstack = g_renew (WeakRefStack, NULL, 1);
464 object->priv->wstack->object = object;
465 object->priv->wstack->n_weak_refs = 1;
468 object->priv->wstack->weak_refs[i].notify = notify;
469 object->priv->wstack->weak_refs[i].data = data;
471 G_UNLOCK (weak_refs_mutex);
475 * gst_mini_object_weak_unref: (skip)
476 * @mini_object: #GstMiniObject to remove a weak reference from
477 * @notify: callback to search for
478 * @data: data to search for
480 * Removes a weak reference callback to a mini object.
485 gst_mini_object_weak_unref (GstMiniObject * object,
486 GstMiniObjectWeakNotify notify, gpointer data)
488 gboolean found_one = FALSE;
490 g_return_if_fail (GST_IS_MINI_OBJECT (object));
491 g_return_if_fail (notify != NULL);
493 G_LOCK (weak_refs_mutex);
495 if (object->priv->wstack) {
498 for (i = 0; i < object->priv->wstack->n_weak_refs; i++)
499 if (object->priv->wstack->weak_refs[i].notify == notify &&
500 object->priv->wstack->weak_refs[i].data == data) {
502 object->priv->wstack->n_weak_refs -= 1;
503 if (i != object->priv->wstack->n_weak_refs)
504 object->priv->wstack->weak_refs[i] =
505 object->priv->wstack->weak_refs[object->priv->wstack->
511 G_UNLOCK (weak_refs_mutex);
513 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
517 * gst_mini_object_replace:
518 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
520 * @newdata: pointer to new mini-object
522 * Modifies a pointer to point to a new mini-object. The modification
523 * is done atomically, and the reference counts are updated correctly.
524 * Either @newdata and the value pointed to by @olddata may be NULL.
527 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
529 GstMiniObject *olddata_val;
531 g_return_if_fail (olddata != NULL);
533 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
534 *olddata, *olddata ? (*olddata)->refcount : 0,
535 newdata, newdata ? newdata->refcount : 0);
537 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
539 if (olddata_val == newdata)
543 gst_mini_object_ref (newdata);
545 while (!g_atomic_pointer_compare_and_exchange ((gpointer *) olddata,
546 olddata_val, newdata)) {
547 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
551 gst_mini_object_unref (olddata_val);
555 gst_value_mini_object_init (GValue * value)
557 value->data[0].v_pointer = NULL;
561 gst_value_mini_object_free (GValue * value)
563 if (value->data[0].v_pointer) {
564 gst_mini_object_unref (GST_MINI_OBJECT_CAST (value->data[0].v_pointer));
569 gst_value_mini_object_copy (const GValue * src_value, GValue * dest_value)
571 if (src_value->data[0].v_pointer) {
572 dest_value->data[0].v_pointer =
573 gst_mini_object_ref (GST_MINI_OBJECT_CAST (src_value->data[0].
576 dest_value->data[0].v_pointer = NULL;
581 gst_value_mini_object_peek_pointer (const GValue * value)
583 return value->data[0].v_pointer;
587 gst_value_mini_object_collect (GValue * value, guint n_collect_values,
588 GTypeCValue * collect_values, guint collect_flags)
590 if (collect_values[0].v_pointer) {
591 value->data[0].v_pointer =
592 gst_mini_object_ref (collect_values[0].v_pointer);
594 value->data[0].v_pointer = NULL;
601 gst_value_mini_object_lcopy (const GValue * value, guint n_collect_values,
602 GTypeCValue * collect_values, guint collect_flags)
604 gpointer *mini_object_p = collect_values[0].v_pointer;
606 if (!mini_object_p) {
607 return g_strdup_printf ("value location for '%s' passed as NULL",
608 G_VALUE_TYPE_NAME (value));
611 if (!value->data[0].v_pointer)
612 *mini_object_p = NULL;
613 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
614 *mini_object_p = value->data[0].v_pointer;
616 *mini_object_p = gst_mini_object_ref (value->data[0].v_pointer);
622 * gst_value_set_mini_object:
623 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
624 * @mini_object: (transfer none): mini object value to set
626 * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
628 * The caller retains ownership of the reference.
631 gst_value_set_mini_object (GValue * value, GstMiniObject * mini_object)
635 g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
636 g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
638 pointer_p = &value->data[0].v_pointer;
639 gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
643 * gst_value_take_mini_object:
644 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
645 * @mini_object: (transfer full): mini object value to take
647 * Set the contents of a %GST_TYPE_MINI_OBJECT derived #GValue to
649 * Takes over the ownership of the caller's reference to @mini_object;
650 * the caller doesn't have to unref it any more.
653 gst_value_take_mini_object (GValue * value, GstMiniObject * mini_object)
657 g_return_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value));
658 g_return_if_fail (mini_object == NULL || GST_IS_MINI_OBJECT (mini_object));
660 pointer_p = &value->data[0].v_pointer;
661 /* takes additional refcount */
662 gst_mini_object_replace ((GstMiniObject **) pointer_p, mini_object);
663 /* remove additional refcount */
665 gst_mini_object_unref (mini_object);
669 * gst_value_get_mini_object:
670 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
672 * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue.
673 * Does not increase the refcount of the returned object.
675 * Returns: (transfer none): mini object contents of @value
678 gst_value_get_mini_object (const GValue * value)
680 g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
682 return value->data[0].v_pointer;
686 * gst_value_dup_mini_object:
687 * @value: a valid #GValue of %GST_TYPE_MINI_OBJECT derived type
689 * Get the contents of a %GST_TYPE_MINI_OBJECT derived #GValue,
690 * increasing its reference count. If the contents of the #GValue
691 * are %NULL, %NULL will be returned.
693 * Returns: (transfer full): mini object contents of @value
698 gst_value_dup_mini_object (const GValue * value)
700 g_return_val_if_fail (GST_VALUE_HOLDS_MINI_OBJECT (value), NULL);
702 return value->data[0].v_pointer ? gst_mini_object_ref (value->
703 data[0].v_pointer) : NULL;
710 param_mini_object_init (GParamSpec * pspec)
712 /* GParamSpecMiniObject *ospec = G_PARAM_SPEC_MINI_OBJECT (pspec); */
716 param_mini_object_set_default (GParamSpec * pspec, GValue * value)
718 value->data[0].v_pointer = NULL;
722 param_mini_object_validate (GParamSpec * pspec, GValue * value)
724 GstMiniObject *mini_object = value->data[0].v_pointer;
725 gboolean changed = FALSE;
728 && !g_value_type_compatible (G_OBJECT_TYPE (mini_object),
729 pspec->value_type)) {
730 gst_mini_object_unref (mini_object);
731 value->data[0].v_pointer = NULL;
739 param_mini_object_values_cmp (GParamSpec * pspec,
740 const GValue * value1, const GValue * value2)
742 guint8 *p1 = value1->data[0].v_pointer;
743 guint8 *p2 = value2->data[0].v_pointer;
745 /* not much to compare here, try to at least provide stable lesser/greater result */
747 return p1 < p2 ? -1 : p1 > p2;
751 gst_param_spec_mini_object_get_type (void)
755 if (G_UNLIKELY (type) == 0) {
756 static const GParamSpecTypeInfo pspec_info = {
757 sizeof (GstParamSpecMiniObject), /* instance_size */
758 16, /* n_preallocs */
759 param_mini_object_init, /* instance_init */
760 G_TYPE_OBJECT, /* value_type */
762 param_mini_object_set_default, /* value_set_default */
763 param_mini_object_validate, /* value_validate */
764 param_mini_object_values_cmp, /* values_cmp */
766 /* FIXME 0.11: Should really be GstParamSpecMiniObject */
767 type = g_param_type_register_static ("GParamSpecMiniObject", &pspec_info);
774 * gst_param_spec_mini_object:
775 * @name: the canonical name of the property
776 * @nick: the nickname of the property
777 * @blurb: a short description of the property
778 * @object_type: the #GstMiniObject #GType for the property
779 * @flags: a combination of #GParamFlags
781 * Creates a new #GParamSpec instance that hold #GstMiniObject references.
783 * Returns: (transfer full): a newly allocated #GParamSpec instance
786 gst_param_spec_mini_object (const char *name, const char *nick,
787 const char *blurb, GType object_type, GParamFlags flags)
789 GstParamSpecMiniObject *ospec;
791 g_return_val_if_fail (g_type_is_a (object_type, GST_TYPE_MINI_OBJECT), NULL);
793 ospec = g_param_spec_internal (GST_TYPE_PARAM_MINI_OBJECT,
794 name, nick, blurb, flags);
795 G_PARAM_SPEC (ospec)->value_type = object_type;
797 return G_PARAM_SPEC (ospec);