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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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.
55 #include "gst/gst_private.h"
56 #include "gst/gstminiobject.h"
57 #include "gst/gstinfo.h"
58 #include <gobject/gvaluecollector.h>
60 /* Mutex used for weak referencing */
61 G_LOCK_DEFINE_STATIC (qdata_mutex);
62 static GQuark weak_ref_quark;
64 #define SHARE_ONE (1 << 16)
65 #define SHARE_TWO (2 << 16)
66 #define SHARE_MASK (~(SHARE_ONE - 1))
67 #define IS_SHARED(state) (state >= SHARE_TWO)
68 #define LOCK_ONE (GST_LOCK_FLAG_LAST)
69 #define FLAG_MASK (GST_LOCK_FLAG_LAST - 1)
70 #define LOCK_MASK ((SHARE_ONE - 1) - FLAG_MASK)
71 #define LOCK_FLAG_MASK (SHARE_ONE - 1)
76 GstMiniObjectNotify notify;
78 GDestroyNotify destroy;
81 #define QDATA(o,i) ((GstQData *)(o)->qdata)[(i)]
82 #define QDATA_QUARK(o,i) (QDATA(o,i).quark)
83 #define QDATA_NOTIFY(o,i) (QDATA(o,i).notify)
84 #define QDATA_DATA(o,i) (QDATA(o,i).data)
85 #define QDATA_DESTROY(o,i) (QDATA(o,i).destroy)
88 _priv_gst_mini_object_initialize (void)
90 weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
94 * gst_mini_object_init: (skip)
95 * @mini_object: a #GstMiniObject
96 * @flags: initial #GstMiniObjectFlags
97 * @type: the #GType of the mini-object to create
98 * @copy_func: (allow-none): the copy function, or %NULL
99 * @dispose_func: (allow-none): the dispose function, or %NULL
100 * @free_func: (allow-none): the free function or %NULL
102 * Initializes a mini-object with the desired type and copy/dispose/free
106 gst_mini_object_init (GstMiniObject * mini_object, guint flags, GType type,
107 GstMiniObjectCopyFunction copy_func,
108 GstMiniObjectDisposeFunction dispose_func,
109 GstMiniObjectFreeFunction free_func)
111 mini_object->type = type;
112 mini_object->refcount = 1;
113 mini_object->lockstate = 0;
114 mini_object->flags = flags;
116 mini_object->copy = copy_func;
117 mini_object->dispose = dispose_func;
118 mini_object->free = free_func;
120 mini_object->n_qdata = 0;
121 mini_object->qdata = NULL;
123 GST_TRACER_MINI_OBJECT_CREATED (mini_object);
127 * gst_mini_object_copy: (skip)
128 * @mini_object: the mini-object to copy
130 * Creates a copy of the mini-object.
134 * Returns: (transfer full): the new mini-object.
137 gst_mini_object_copy (const GstMiniObject * mini_object)
141 g_return_val_if_fail (mini_object != NULL, NULL);
143 if (mini_object->copy)
144 copy = mini_object->copy (mini_object);
152 * gst_mini_object_lock:
153 * @object: the mini-object to lock
154 * @flags: #GstLockFlags
156 * Lock the mini-object with the specified access mode in @flags.
158 * Returns: %TRUE if @object could be locked.
161 gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
163 gint access_mode, state, newstate;
165 g_return_val_if_fail (object != NULL, FALSE);
166 g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
168 if (G_UNLIKELY (object->flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY &&
169 flags & GST_LOCK_FLAG_WRITE))
173 access_mode = flags & FLAG_MASK;
174 newstate = state = g_atomic_int_get (&object->lockstate);
176 GST_CAT_TRACE (GST_CAT_LOCKING, "lock %p: state %08x, access_mode %d",
177 object, state, access_mode);
179 if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
181 newstate += SHARE_ONE;
182 access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
185 /* shared counter > 1 and write access is not allowed */
186 if (((state & GST_LOCK_FLAG_WRITE) != 0
187 || (access_mode & GST_LOCK_FLAG_WRITE) != 0)
188 && IS_SHARED (newstate))
192 if ((state & LOCK_FLAG_MASK) == 0) {
193 /* nothing mapped, set access_mode */
194 newstate |= access_mode;
196 /* access_mode must match */
197 if ((state & access_mode) != access_mode)
200 /* increase refcount */
201 newstate += LOCK_ONE;
203 } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
210 GST_CAT_DEBUG (GST_CAT_LOCKING,
211 "lock failed %p: state %08x, access_mode %d", object, state,
218 * gst_mini_object_unlock:
219 * @object: the mini-object to unlock
220 * @flags: #GstLockFlags
222 * Unlock the mini-object with the specified access mode in @flags.
225 gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
227 gint access_mode, state, newstate;
229 g_return_if_fail (object != NULL);
230 g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
233 access_mode = flags & FLAG_MASK;
234 newstate = state = g_atomic_int_get (&object->lockstate);
236 GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
237 object, state, access_mode);
239 if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
241 g_return_if_fail (state >= SHARE_ONE);
242 newstate -= SHARE_ONE;
243 access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
247 g_return_if_fail ((state & access_mode) == access_mode);
248 /* decrease the refcount */
249 newstate -= LOCK_ONE;
250 /* last refcount, unset access_mode */
251 if ((newstate & LOCK_FLAG_MASK) == access_mode)
252 newstate &= ~LOCK_FLAG_MASK;
254 } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
259 * gst_mini_object_is_writable:
260 * @mini_object: the mini-object to check
262 * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
263 * lock on @object is the only one, this means that changes to the object will
264 * not be visible to any other object.
266 * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
267 * exactly 1, meaning that no other reference exists to the object and that the
268 * object is therefore writable.
270 * Modification of a mini-object should only be done after verifying that it
273 * Returns: %TRUE if the object is writable.
276 gst_mini_object_is_writable (const GstMiniObject * mini_object)
280 g_return_val_if_fail (mini_object != NULL, FALSE);
282 if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
283 result = !IS_SHARED (g_atomic_int_get (&mini_object->lockstate));
285 result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
291 * gst_mini_object_make_writable: (skip)
292 * @mini_object: (transfer full): the mini-object to make writable
294 * Checks if a mini-object is writable. If not, a writable copy is made and
295 * returned. This gives away the reference to the original mini object,
296 * and returns a reference to the new object.
300 * Returns: (transfer full): a mini-object (possibly the same pointer) that
304 gst_mini_object_make_writable (GstMiniObject * mini_object)
308 g_return_val_if_fail (mini_object != NULL, NULL);
310 if (gst_mini_object_is_writable (mini_object)) {
313 ret = gst_mini_object_copy (mini_object);
314 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
315 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
316 gst_mini_object_unref (mini_object);
323 * gst_mini_object_ref: (skip)
324 * @mini_object: the mini-object
326 * Increase the reference count of the mini-object.
328 * Note that the refcount affects the writability
329 * of @mini-object, see gst_mini_object_is_writable(). It is
330 * important to note that keeping additional references to
331 * GstMiniObject instances can potentially increase the number
332 * of memcpy operations in a pipeline, especially if the miniobject
335 * Returns: (transfer full): the mini-object.
338 gst_mini_object_ref (GstMiniObject * mini_object)
340 g_return_val_if_fail (mini_object != NULL, NULL);
341 /* we can't assert that the refcount > 0 since the _free functions
342 * increments the refcount from 0 to 1 again to allow resurecting
344 g_return_val_if_fail (mini_object->refcount > 0, NULL);
347 GST_TRACER_MINI_OBJECT_REFFED (mini_object, mini_object->refcount + 1);
348 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
349 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
350 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
352 g_atomic_int_inc (&mini_object->refcount);
358 find_notify (GstMiniObject * object, GQuark quark, gboolean match_notify,
359 GstMiniObjectNotify notify, gpointer data)
363 for (i = 0; i < object->n_qdata; i++) {
364 if (QDATA_QUARK (object, i) == quark) {
365 /* check if we need to match the callback too */
366 if (!match_notify || (QDATA_NOTIFY (object, i) == notify &&
367 QDATA_DATA (object, i) == data))
375 remove_notify (GstMiniObject * object, gint index)
378 if (--object->n_qdata == 0) {
379 /* we don't shrink but free when everything is gone */
380 g_free (object->qdata);
381 object->qdata = NULL;
382 } else if (index != object->n_qdata)
383 QDATA (object, index) = QDATA (object, object->n_qdata);
387 set_notify (GstMiniObject * object, gint index, GQuark quark,
388 GstMiniObjectNotify notify, gpointer data, GDestroyNotify destroy)
392 index = object->n_qdata++;
394 g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
396 QDATA_QUARK (object, index) = quark;
397 QDATA_NOTIFY (object, index) = notify;
398 QDATA_DATA (object, index) = data;
399 QDATA_DESTROY (object, index) = destroy;
403 call_finalize_notify (GstMiniObject * obj)
407 for (i = 0; i < obj->n_qdata; i++) {
408 if (QDATA_QUARK (obj, i) == weak_ref_quark)
409 QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
410 if (QDATA_DESTROY (obj, i))
411 QDATA_DESTROY (obj, i) (QDATA_DATA (obj, i));
416 * gst_mini_object_unref: (skip)
417 * @mini_object: the mini-object
419 * Decreases the reference count of the mini-object, possibly freeing
423 gst_mini_object_unref (GstMiniObject * mini_object)
425 g_return_if_fail (mini_object != NULL);
427 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
429 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
430 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
432 g_return_if_fail (mini_object->refcount > 0);
434 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
437 GST_TRACER_MINI_OBJECT_UNREFFED (mini_object, mini_object->refcount);
438 if (mini_object->dispose)
439 do_free = mini_object->dispose (mini_object);
443 /* if the subclass recycled the object (and returned FALSE) we don't
444 * want to free the instance anymore */
445 if (G_LIKELY (do_free)) {
446 /* there should be no outstanding locks */
447 g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
450 if (mini_object->n_qdata) {
451 call_finalize_notify (mini_object);
452 g_free (mini_object->qdata);
454 GST_TRACER_MINI_OBJECT_DESTROYED (mini_object);
455 if (mini_object->free)
456 mini_object->free (mini_object);
459 GST_TRACER_MINI_OBJECT_UNREFFED (mini_object, mini_object->refcount);
464 * gst_mini_object_replace:
465 * @olddata: (inout) (transfer full) (nullable): pointer to a pointer to a
466 * mini-object to be replaced
467 * @newdata: (allow-none): pointer to new mini-object
469 * Atomically modifies a pointer to point to a new mini-object.
470 * The reference count of @olddata is decreased and the reference count of
471 * @newdata is increased.
473 * Either @newdata and the value pointed to by @olddata may be %NULL.
475 * Returns: %TRUE if @newdata was different from @olddata
478 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
480 GstMiniObject *olddata_val;
482 g_return_val_if_fail (olddata != NULL, FALSE);
484 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
485 *olddata, *olddata ? (*olddata)->refcount : 0,
486 newdata, newdata ? newdata->refcount : 0);
488 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
490 if (G_UNLIKELY (olddata_val == newdata))
494 gst_mini_object_ref (newdata);
496 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
497 olddata, olddata_val, newdata))) {
498 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
499 if (G_UNLIKELY (olddata_val == newdata))
504 gst_mini_object_unref (olddata_val);
506 return olddata_val != newdata;
510 * gst_mini_object_steal: (skip)
511 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
514 * Replace the current #GstMiniObject pointer to by @olddata with %NULL and
515 * return the old value.
517 * Returns: the #GstMiniObject at @oldata
520 gst_mini_object_steal (GstMiniObject ** olddata)
522 GstMiniObject *olddata_val;
524 g_return_val_if_fail (olddata != NULL, NULL);
526 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
527 *olddata, *olddata ? (*olddata)->refcount : 0);
530 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
531 if (olddata_val == NULL)
533 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
534 olddata, olddata_val, NULL)));
540 * gst_mini_object_take:
541 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
543 * @newdata: pointer to new mini-object
545 * Modifies a pointer to point to a new mini-object. The modification
546 * is done atomically. This version is similar to gst_mini_object_replace()
547 * except that it does not increase the refcount of @newdata and thus
548 * takes ownership of @newdata.
550 * Either @newdata and the value pointed to by @olddata may be %NULL.
552 * Returns: %TRUE if @newdata was different from @olddata
555 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
557 GstMiniObject *olddata_val;
559 g_return_val_if_fail (olddata != NULL, FALSE);
561 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
562 *olddata, *olddata ? (*olddata)->refcount : 0,
563 newdata, newdata ? newdata->refcount : 0);
566 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
567 if (G_UNLIKELY (olddata_val == newdata))
569 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
570 olddata, olddata_val, newdata)));
573 gst_mini_object_unref (olddata_val);
575 return olddata_val != newdata;
579 * gst_mini_object_weak_ref: (skip)
580 * @object: #GstMiniObject to reference weakly
581 * @notify: callback to invoke before the mini object is freed
582 * @data: extra data to pass to notify
584 * Adds a weak reference callback to a mini object. Weak references are
585 * used for notification when a mini object is finalized. They are called
586 * "weak references" because they allow you to safely hold a pointer
587 * to the mini object without calling gst_mini_object_ref()
588 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
592 gst_mini_object_weak_ref (GstMiniObject * object,
593 GstMiniObjectNotify notify, gpointer data)
595 g_return_if_fail (object != NULL);
596 g_return_if_fail (notify != NULL);
597 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
599 G_LOCK (qdata_mutex);
600 set_notify (object, -1, weak_ref_quark, notify, data, NULL);
601 G_UNLOCK (qdata_mutex);
605 * gst_mini_object_weak_unref: (skip)
606 * @object: #GstMiniObject to remove a weak reference from
607 * @notify: callback to search for
608 * @data: data to search for
610 * Removes a weak reference callback from a mini object.
613 gst_mini_object_weak_unref (GstMiniObject * object,
614 GstMiniObjectNotify notify, gpointer data)
618 g_return_if_fail (object != NULL);
619 g_return_if_fail (notify != NULL);
621 G_LOCK (qdata_mutex);
622 if ((i = find_notify (object, weak_ref_quark, TRUE, notify, data)) != -1) {
623 remove_notify (object, i);
625 g_warning ("%s: couldn't find weak ref %p (object:%p data:%p)", G_STRFUNC,
626 notify, object, data);
628 G_UNLOCK (qdata_mutex);
632 * gst_mini_object_set_qdata:
633 * @object: a #GstMiniObject
634 * @quark: A #GQuark, naming the user data pointer
635 * @data: An opaque user data pointer
636 * @destroy: Function to invoke with @data as argument, when @data
639 * This sets an opaque, named pointer on a miniobject.
640 * The name is specified through a #GQuark (retrieved e.g. via
641 * g_quark_from_static_string()), and the pointer
642 * can be gotten back from the @object with gst_mini_object_get_qdata()
643 * until the @object is disposed.
644 * Setting a previously set user data pointer, overrides (frees)
645 * the old pointer set, using %NULL as pointer essentially
646 * removes the data stored.
648 * @destroy may be specified which is called with @data as argument
649 * when the @object is disposed, or the data is being overwritten by
650 * a call to gst_mini_object_set_qdata() with the same @quark.
653 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
654 gpointer data, GDestroyNotify destroy)
657 gpointer old_data = NULL;
658 GDestroyNotify old_notify = NULL;
660 g_return_if_fail (object != NULL);
661 g_return_if_fail (quark > 0);
663 G_LOCK (qdata_mutex);
664 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
666 old_data = QDATA_DATA (object, i);
667 old_notify = QDATA_DESTROY (object, i);
670 remove_notify (object, i);
673 set_notify (object, i, quark, NULL, data, destroy);
674 G_UNLOCK (qdata_mutex);
677 old_notify (old_data);
681 * gst_mini_object_get_qdata:
682 * @object: The GstMiniObject to get a stored user data pointer from
683 * @quark: A #GQuark, naming the user data pointer
685 * This function gets back user data pointers stored via
686 * gst_mini_object_set_qdata().
688 * Returns: (transfer none) (nullable): The user data pointer set, or
692 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
697 g_return_val_if_fail (object != NULL, NULL);
698 g_return_val_if_fail (quark > 0, NULL);
700 G_LOCK (qdata_mutex);
701 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1)
702 result = QDATA_DATA (object, i);
705 G_UNLOCK (qdata_mutex);
711 * gst_mini_object_steal_qdata:
712 * @object: The GstMiniObject to get a stored user data pointer from
713 * @quark: A #GQuark, naming the user data pointer
715 * This function gets back user data pointers stored via gst_mini_object_set_qdata()
716 * and removes the data from @object without invoking its destroy() function (if
719 * Returns: (transfer full) (nullable): The user data pointer set, or
723 gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
728 g_return_val_if_fail (object != NULL, NULL);
729 g_return_val_if_fail (quark > 0, NULL);
731 G_LOCK (qdata_mutex);
732 if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
733 result = QDATA_DATA (object, i);
734 remove_notify (object, i);
738 G_UNLOCK (qdata_mutex);