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 simple structure that can be used to implement refcounted
28 * Subclasses will include #GstMiniObject as the first member in their structure
29 * and then call gst_mini_object_init() to initialize the #GstMiniObject fields.
31 * gst_mini_object_ref() and gst_mini_object_unref() increment and decrement the
32 * refcount respectively. When the refcount of a mini-object reaches 0, the
33 * dispose function is called first and when this returns %TRUE, the free
34 * function of the miniobject is called.
36 * A copy can be made with gst_mini_object_copy().
38 * gst_mini_object_is_writable() will return %TRUE when the refcount of the
39 * object is exactly 1, meaning the current caller has the only reference to the
40 * object. gst_mini_object_make_writable() will return a writable version of the
41 * object, which might be a new copy when the refcount was not 1.
43 * Opaque data can be associated with a #GstMiniObject with
44 * gst_mini_object_set_qdata() and gst_mini_object_get_qdata(). The data is
45 * meant to be specific to the particular object and is not automatically copied
46 * with gst_mini_object_copy() or similar methods.
48 * A weak reference can be added and remove with gst_mini_object_weak_ref()
49 * and gst_mini_object_weak_unref() respectively.
51 * Last reviewed on 2012-06-15 (0.11.93)
57 #include "gst/gst_private.h"
58 #include "gst/gstminiobject.h"
59 #include "gst/gstinfo.h"
60 #include <gobject/gvaluecollector.h>
62 #ifndef GST_DISABLE_TRACE
64 static GstAllocTrace *_gst_mini_object_trace;
67 /* Mutex used for weak referencing */
68 G_LOCK_DEFINE_STATIC (qdata_mutex);
69 static GQuark weak_ref_quark;
71 #define SHARE_ONE (1 << 16)
72 #define SHARE_TWO (2 << 16)
73 #define SHARE_MASK (~(SHARE_ONE - 1))
74 #define IS_SHARED(state) (state >= SHARE_TWO)
75 #define LOCK_ONE (GST_LOCK_FLAG_LAST)
76 #define FLAG_MASK (GST_LOCK_FLAG_LAST - 1)
77 #define LOCK_MASK ((SHARE_ONE - 1) - FLAG_MASK)
78 #define LOCK_FLAG_MASK (SHARE_ONE - 1)
83 GstMiniObjectNotify notify;
85 GDestroyNotify destroy;
88 #define QDATA(o,i) ((GstQData *)(o)->qdata)[(i)]
89 #define QDATA_QUARK(o,i) (QDATA(o,i).quark)
90 #define QDATA_NOTIFY(o,i) (QDATA(o,i).notify)
91 #define QDATA_DATA(o,i) (QDATA(o,i).data)
92 #define QDATA_DESTROY(o,i) (QDATA(o,i).destroy)
95 _priv_gst_mini_object_initialize (void)
97 weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
99 #ifndef GST_DISABLE_TRACE
100 _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
105 * gst_mini_object_init: (skip)
106 * @mini_object: a #GstMiniObject
107 * @type: the #GType of the mini-object to create
108 * @copy_func: the copy function, or NULL
109 * @dispose_func: the dispose function, or NULL
110 * @free_func: the free function or NULL
112 * Initializes a mini-object with the desired type and copy/dispose/free
116 gst_mini_object_init (GstMiniObject * mini_object, guint flags, GType type,
117 GstMiniObjectCopyFunction copy_func,
118 GstMiniObjectDisposeFunction dispose_func,
119 GstMiniObjectFreeFunction free_func)
121 mini_object->type = type;
122 mini_object->refcount = 1;
123 mini_object->lockstate =
124 (flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY ? GST_LOCK_FLAG_READ : 0);
125 mini_object->flags = flags;
127 mini_object->copy = copy_func;
128 mini_object->dispose = dispose_func;
129 mini_object->free = free_func;
131 mini_object->n_qdata = 0;
132 mini_object->qdata = NULL;
134 #ifndef GST_DISABLE_TRACE
135 _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
140 * gst_mini_object_copy:
141 * @mini_object: the mini-object to copy
143 * Creates a copy of the mini-object.
147 * Returns: (transfer full): the new mini-object.
150 gst_mini_object_copy (const GstMiniObject * mini_object)
154 g_return_val_if_fail (mini_object != NULL, NULL);
156 if (mini_object->copy)
157 copy = mini_object->copy (mini_object);
165 * gst_mini_object_lock:
166 * @object: the mini-object to lock
167 * @flags: #GstLockFlags
169 * Lock the mini-object with the specified access mode in @flags.
171 * Returns: %TRUE if @object could be locked.
174 gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
176 gint access_mode, state, newstate;
178 g_return_val_if_fail (object != NULL, FALSE);
179 g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
182 access_mode = flags & FLAG_MASK;
183 newstate = state = g_atomic_int_get (&object->lockstate);
185 GST_CAT_TRACE (GST_CAT_LOCKING, "lock %p: state %08x, access_mode %d",
186 object, state, access_mode);
188 if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
190 newstate += SHARE_ONE;
191 access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
195 /* shared counter > 1 and write access is not allowed */
196 if (access_mode & GST_LOCK_FLAG_WRITE && IS_SHARED (state))
199 if ((state & LOCK_FLAG_MASK) == 0) {
200 /* nothing mapped, set access_mode */
201 newstate |= access_mode;
203 /* access_mode must match */
204 if ((state & access_mode) != access_mode)
207 /* increase refcount */
208 newstate += LOCK_ONE;
210 } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
217 GST_CAT_DEBUG (GST_CAT_LOCKING,
218 "lock failed %p: state %08x, access_mode %d", object, state,
225 * gst_mini_object_unlock:
226 * @object: the mini-object to unlock
227 * @flags: #GstLockFlags
229 * Unlock the mini-object with the specified access mode in @flags.
232 gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
234 gint access_mode, state, newstate;
236 g_return_if_fail (object != NULL);
237 g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
240 access_mode = flags & FLAG_MASK;
241 newstate = state = g_atomic_int_get (&object->lockstate);
243 GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
244 object, state, access_mode);
246 if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
248 g_return_if_fail (state >= SHARE_ONE);
249 newstate -= SHARE_ONE;
250 access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
254 g_return_if_fail ((state & access_mode) == access_mode);
255 /* decrease the refcount */
256 newstate -= LOCK_ONE;
257 /* last refcount, unset access_mode */
258 if ((newstate & LOCK_FLAG_MASK) == access_mode)
259 newstate &= ~LOCK_FLAG_MASK;
261 } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
266 * gst_mini_object_is_writable:
267 * @mini_object: the mini-object to check
269 * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
270 * lock on @object is the only one, this means that changes to the object will
271 * not be visible to any other object.
273 * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
274 * exactly 1, meaning that no other reference exists to the object and that the
275 * object is therefore writable.
277 * Modification of a mini-object should only be done after verifying that it
280 * Returns: TRUE if the object is writable.
283 gst_mini_object_is_writable (const GstMiniObject * mini_object)
287 g_return_val_if_fail (mini_object != NULL, FALSE);
289 if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
290 result = (g_atomic_int_get (&mini_object->lockstate) & SHARE_MASK) < 2;
292 result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
298 * gst_mini_object_make_writable:
299 * @mini_object: (transfer full): the mini-object to make writable
301 * Checks if a mini-object is writable. If not, a writable copy is made and
302 * returned. This gives away the reference to the original mini object,
303 * and returns a reference to the new object.
307 * Returns: (transfer full): a mini-object (possibly the same pointer) that
311 gst_mini_object_make_writable (GstMiniObject * mini_object)
315 g_return_val_if_fail (mini_object != NULL, NULL);
317 if (gst_mini_object_is_writable (mini_object)) {
320 ret = gst_mini_object_copy (mini_object);
321 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
322 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
323 gst_mini_object_unref (mini_object);
330 * gst_mini_object_ref:
331 * @mini_object: the mini-object
333 * Increase the reference count of the mini-object.
335 * Note that the refcount affects the writeability
336 * of @mini-object, see gst_mini_object_is_writable(). It is
337 * important to note that keeping additional references to
338 * GstMiniObject instances can potentially increase the number
339 * of memcpy operations in a pipeline, especially if the miniobject
342 * Returns: (transfer full): the mini-object.
345 gst_mini_object_ref (GstMiniObject * mini_object)
347 g_return_val_if_fail (mini_object != NULL, NULL);
348 /* we can't assert that the refcount > 0 since the _free functions
349 * increments the refcount from 0 to 1 again to allow resurecting
351 g_return_val_if_fail (mini_object->refcount > 0, NULL);
354 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
355 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
356 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
358 g_atomic_int_inc (&mini_object->refcount);
364 find_notify (GstMiniObject * object, GQuark quark, gboolean match_notify,
365 GstMiniObjectNotify notify, gpointer data)
369 for (i = 0; i < object->n_qdata; i++) {
370 if (QDATA_QUARK (object, i) == quark) {
371 /* check if we need to match the callback too */
372 if (!match_notify || (QDATA_NOTIFY (object, i) == notify &&
373 QDATA_DATA (object, i) == data))
381 remove_notify (GstMiniObject * object, gint index)
384 if (--object->n_qdata == 0) {
385 /* we don't shrink but free when everything is gone */
386 g_free (object->qdata);
387 object->qdata = NULL;
388 } else if (index != object->n_qdata)
389 QDATA (object, index) = QDATA (object, object->n_qdata);
393 set_notify (GstMiniObject * object, gint index, GQuark quark,
394 GstMiniObjectNotify notify, gpointer data, GDestroyNotify destroy)
398 index = object->n_qdata++;
400 g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
402 QDATA_QUARK (object, index) = quark;
403 QDATA_NOTIFY (object, index) = notify;
404 QDATA_DATA (object, index) = data;
405 QDATA_DESTROY (object, index) = destroy;
409 call_finalize_notify (GstMiniObject * obj)
413 for (i = 0; i < obj->n_qdata; i++) {
414 if (QDATA_QUARK (obj, i) == weak_ref_quark)
415 QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
416 if (QDATA_DESTROY (obj, i))
417 QDATA_DESTROY (obj, i) (QDATA_DATA (obj, i));
422 * gst_mini_object_unref:
423 * @mini_object: the mini-object
425 * Decreases the reference count of the mini-object, possibly freeing
429 gst_mini_object_unref (GstMiniObject * mini_object)
431 g_return_if_fail (mini_object != NULL);
433 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
435 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
436 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
438 g_return_if_fail (mini_object->refcount > 0);
440 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
443 if (mini_object->dispose)
444 do_free = mini_object->dispose (mini_object);
448 /* if the subclass recycled the object (and returned FALSE) we don't
449 * want to free the instance anymore */
450 if (G_LIKELY (do_free)) {
451 /* there should be no outstanding locks */
452 g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
455 if (mini_object->n_qdata) {
456 call_finalize_notify (mini_object);
457 g_free (mini_object->qdata);
459 #ifndef GST_DISABLE_TRACE
460 _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
462 if (mini_object->free)
463 mini_object->free (mini_object);
469 * gst_mini_object_replace:
470 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
472 * @newdata: pointer to new mini-object
474 * Atomically modifies a pointer to point to a new mini-object.
475 * The reference count of @olddata is decreased and the reference count of
476 * @newdata is increased.
478 * Either @newdata and the value pointed to by @olddata may be NULL.
480 * Returns: TRUE if @newdata was different from @olddata
483 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
485 GstMiniObject *olddata_val;
487 g_return_val_if_fail (olddata != NULL, FALSE);
489 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
490 *olddata, *olddata ? (*olddata)->refcount : 0,
491 newdata, newdata ? newdata->refcount : 0);
493 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
495 if (G_UNLIKELY (olddata_val == newdata))
499 gst_mini_object_ref (newdata);
501 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
502 olddata, olddata_val, newdata))) {
503 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
504 if (G_UNLIKELY (olddata_val == newdata))
509 gst_mini_object_unref (olddata_val);
511 return olddata_val != newdata;
515 * gst_mini_object_steal:
516 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
519 * Replace the current #GstMiniObject pointer to by @olddata with NULL and
520 * return the old value.
522 * Returns: the #GstMiniObject at @oldata
525 gst_mini_object_steal (GstMiniObject ** olddata)
527 GstMiniObject *olddata_val;
529 g_return_val_if_fail (olddata != NULL, NULL);
531 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
532 *olddata, *olddata ? (*olddata)->refcount : 0);
535 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
536 if (olddata_val == NULL)
538 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
539 olddata, olddata_val, NULL)));
545 * gst_mini_object_take:
546 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
548 * @newdata: pointer to new mini-object
550 * Modifies a pointer to point to a new mini-object. The modification
551 * is done atomically. This version is similar to gst_mini_object_replace()
552 * except that it does not increase the refcount of @newdata and thus
553 * takes ownership of @newdata.
555 * Either @newdata and the value pointed to by @olddata may be NULL.
557 * Returns: TRUE if @newdata was different from @olddata
560 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
562 GstMiniObject *olddata_val;
564 g_return_val_if_fail (olddata != NULL, FALSE);
566 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
567 *olddata, *olddata ? (*olddata)->refcount : 0,
568 newdata, newdata ? newdata->refcount : 0);
571 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
572 if (G_UNLIKELY (olddata_val == newdata))
574 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
575 olddata, olddata_val, newdata)));
578 gst_mini_object_unref (olddata_val);
580 return olddata_val != newdata;
584 * gst_mini_object_weak_ref: (skip)
585 * @object: #GstMiniObject to reference weakly
586 * @notify: callback to invoke before the mini object is freed
587 * @data: extra data to pass to notify
589 * Adds a weak reference callback to a mini object. Weak references are
590 * used for notification when a mini object is finalized. They are called
591 * "weak references" because they allow you to safely hold a pointer
592 * to the mini object without calling gst_mini_object_ref()
593 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
597 gst_mini_object_weak_ref (GstMiniObject * object,
598 GstMiniObjectNotify notify, gpointer data)
600 g_return_if_fail (object != NULL);
601 g_return_if_fail (notify != NULL);
602 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
604 G_LOCK (qdata_mutex);
605 set_notify (object, -1, weak_ref_quark, notify, data, NULL);
606 G_UNLOCK (qdata_mutex);
610 * gst_mini_object_weak_unref: (skip)
611 * @object: #GstMiniObject to remove a weak reference from
612 * @notify: callback to search for
613 * @data: data to search for
615 * Removes a weak reference callback from a mini object.
618 gst_mini_object_weak_unref (GstMiniObject * object,
619 GstMiniObjectNotify notify, gpointer data)
623 g_return_if_fail (object != NULL);
624 g_return_if_fail (notify != NULL);
626 G_LOCK (qdata_mutex);
627 if ((i = find_notify (object, weak_ref_quark, TRUE, notify, data)) != -1) {
628 remove_notify (object, i);
630 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
632 G_UNLOCK (qdata_mutex);
636 * gst_mini_object_set_qdata:
637 * @object: a #GstMiniObject
638 * @quark: A #GQuark, naming the user data pointer
639 * @data: An opaque user data pointer
640 * @destroy: Function to invoke with @data as argument, when @data
643 * This sets an opaque, named pointer on a miniobject.
644 * The name is specified through a #GQuark (retrived e.g. via
645 * g_quark_from_static_string()), and the pointer
646 * can be gotten back from the @object with gst_mini_object_get_qdata()
647 * until the @object is disposed.
648 * Setting a previously set user data pointer, overrides (frees)
649 * the old pointer set, using #NULL as pointer essentially
650 * removes the data stored.
652 * @destroy may be specified which is called with @data as argument
653 * when the @object is disposed, or the data is being overwritten by
654 * a call to gst_mini_object_set_qdata() with the same @quark.
657 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
658 gpointer data, GDestroyNotify destroy)
661 gpointer old_data = NULL;
662 GDestroyNotify old_notify = NULL;
664 g_return_if_fail (object != NULL);
665 g_return_if_fail (quark > 0);
667 G_LOCK (qdata_mutex);
668 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
670 old_data = QDATA_DATA (object, i);
671 old_notify = QDATA_DESTROY (object, i);
674 remove_notify (object, i);
677 set_notify (object, i, quark, NULL, data, destroy);
678 G_UNLOCK (qdata_mutex);
681 old_notify (old_data);
685 * gst_mini_object_get_qdata:
686 * @object: The GstMiniObject to get a stored user data pointer from
687 * @quark: A #GQuark, naming the user data pointer
689 * This function gets back user data pointers stored via
690 * gst_mini_object_set_qdata().
692 * Returns: (transfer none): The user data pointer set, or %NULL
695 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
700 g_return_val_if_fail (object != NULL, NULL);
701 g_return_val_if_fail (quark > 0, NULL);
703 G_LOCK (qdata_mutex);
704 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1)
705 result = QDATA_DATA (object, i);
708 G_UNLOCK (qdata_mutex);
714 * gst_mini_object_steal_qdata:
715 * @object: The GstMiniObject to get a stored user data pointer from
716 * @quark: A #GQuark, naming the user data pointer
718 * This function gets back user data pointers stored via gst_mini_object_set_qdata()
719 * and removes the data from @object without invoking its destroy() function (if
722 * Returns: (transfer full): The user data pointer set, or %NULL
725 gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
730 g_return_val_if_fail (object != NULL, NULL);
731 g_return_val_if_fail (quark > 0, NULL);
733 G_LOCK (qdata_mutex);
734 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
735 result = QDATA_DATA (object, i);
736 remove_notify (object, i);
740 G_UNLOCK (qdata_mutex);