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;
74 GstMiniObjectWeakNotify notify;
78 #define QDATA(o,i) ((GstQData *)(o)->qdata)[(i)]
79 #define QDATA_QUARK(o,i) (QDATA(o,i).quark)
80 #define QDATA_NOTIFY(o,i) (QDATA(o,i).notify)
81 #define QDATA_DATA(o,i) (QDATA(o,i).data)
84 _priv_gst_mini_object_initialize (void)
86 weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
88 #ifndef GST_DISABLE_TRACE
89 _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
94 * gst_mini_object_init:
95 * @mini_object: a #GstMiniObject
96 * @type: the #GType of the mini-object to create
98 * Initializes a mini-object with the desired type and size.
102 * Returns: (transfer full): the new mini-object.
105 gst_mini_object_init (GstMiniObject * mini_object, GType type)
107 mini_object->type = type;
108 mini_object->refcount = 1;
109 mini_object->flags = 0;
110 mini_object->n_qdata = 0;
111 mini_object->qdata = NULL;
113 #ifndef GST_DISABLE_TRACE
114 _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
119 * gst_mini_object_copy:
120 * @mini_object: the mini-object to copy
122 * Creates a copy of the mini-object.
126 * Returns: (transfer full): the new mini-object.
129 gst_mini_object_copy (const GstMiniObject * mini_object)
133 g_return_val_if_fail (mini_object != NULL, NULL);
135 if (mini_object->copy)
136 copy = mini_object->copy (mini_object);
144 * gst_mini_object_is_writable:
145 * @mini_object: the mini-object to check
147 * Checks if a mini-object is writable. A mini-object is writable
148 * if the reference count is one. Modification of a mini-object should
149 * only be done after verifying that it is writable.
153 * Returns: TRUE if the object is writable.
156 gst_mini_object_is_writable (const GstMiniObject * mini_object)
158 g_return_val_if_fail (mini_object != NULL, FALSE);
160 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
164 * gst_mini_object_make_writable:
165 * @mini_object: (transfer full): the mini-object to make writable
167 * Checks if a mini-object is writable. If not, a writable copy is made and
168 * returned. This gives away the reference to the original mini object,
169 * and returns a reference to the new object.
173 * Returns: (transfer full): a mini-object (possibly the same pointer) that
177 gst_mini_object_make_writable (GstMiniObject * mini_object)
181 g_return_val_if_fail (mini_object != NULL, NULL);
183 if (gst_mini_object_is_writable (mini_object)) {
186 ret = gst_mini_object_copy (mini_object);
187 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
188 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
189 gst_mini_object_unref (mini_object);
196 * gst_mini_object_ref:
197 * @mini_object: the mini-object
199 * Increase the reference count of the mini-object.
201 * Note that the refcount affects the writeability
202 * of @mini-object, see gst_mini_object_is_writable(). It is
203 * important to note that keeping additional references to
204 * GstMiniObject instances can potentially increase the number
205 * of memcpy operations in a pipeline, especially if the miniobject
208 * Returns: (transfer full): the mini-object.
211 gst_mini_object_ref (GstMiniObject * mini_object)
213 g_return_val_if_fail (mini_object != NULL, NULL);
214 /* we can't assert that the refcount > 0 since the _free functions
215 * increments the refcount from 0 to 1 again to allow resurecting
217 g_return_val_if_fail (mini_object->refcount > 0, NULL);
220 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
221 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
222 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
224 g_atomic_int_inc (&mini_object->refcount);
230 qdata_notify (GstMiniObject * obj)
234 for (i = 0; i < obj->n_qdata; i++)
235 QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
240 * gst_mini_object_unref:
241 * @mini_object: the mini-object
243 * Decreases the reference count of the mini-object, possibly freeing
247 gst_mini_object_unref (GstMiniObject * mini_object)
249 g_return_if_fail (mini_object != NULL);
251 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
253 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
254 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
256 g_return_if_fail (mini_object->refcount > 0);
258 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
261 if (mini_object->dispose)
262 do_free = mini_object->dispose (mini_object);
266 /* if the subclass recycled the object (and returned FALSE) we don't
267 * want to free the instance anymore */
268 if (G_LIKELY (do_free)) {
269 /* The weak reference stack is freed in the notification function */
270 if (mini_object->n_qdata)
271 qdata_notify (mini_object);
273 #ifndef GST_DISABLE_TRACE
274 _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
276 if (mini_object->free)
277 mini_object->free (mini_object);
283 * gst_mini_object_replace:
284 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
286 * @newdata: pointer to new mini-object
288 * Atomically modifies a pointer to point to a new mini-object.
289 * The reference count of @olddata is decreased and the reference count of
290 * @newdata is increased.
292 * Either @newdata and the value pointed to by @olddata may be NULL.
294 * Returns: TRUE if @newdata was different from @olddata
297 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
299 GstMiniObject *olddata_val;
301 g_return_val_if_fail (olddata != NULL, FALSE);
303 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
304 *olddata, *olddata ? (*olddata)->refcount : 0,
305 newdata, newdata ? newdata->refcount : 0);
307 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
309 if (G_UNLIKELY (olddata_val == newdata))
313 gst_mini_object_ref (newdata);
315 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
316 olddata, olddata_val, newdata))) {
317 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
318 if (G_UNLIKELY (olddata_val == newdata))
323 gst_mini_object_unref (olddata_val);
325 return olddata_val != newdata;
329 * gst_mini_object_steal:
330 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
333 * Replace the current #GstMiniObject pointer to by @olddata with NULL and
334 * return the old value.
336 * Returns: the #GstMiniObject at @oldata
339 gst_mini_object_steal (GstMiniObject ** olddata)
341 GstMiniObject *olddata_val;
343 g_return_val_if_fail (olddata != NULL, NULL);
345 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
346 *olddata, *olddata ? (*olddata)->refcount : 0);
349 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
350 if (olddata_val == NULL)
352 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
353 olddata, olddata_val, NULL)));
359 * gst_mini_object_take:
360 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
362 * @newdata: pointer to new mini-object
364 * Modifies a pointer to point to a new mini-object. The modification
365 * is done atomically. This version is similar to gst_mini_object_replace()
366 * except that it does not increase the refcount of @newdata and thus
367 * takes ownership of @newdata.
369 * Either @newdata and the value pointed to by @olddata may be NULL.
371 * Returns: TRUE if @newdata was different from @olddata
374 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
376 GstMiniObject *olddata_val;
378 g_return_val_if_fail (olddata != NULL, FALSE);
380 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
381 *olddata, *olddata ? (*olddata)->refcount : 0,
382 newdata, newdata ? newdata->refcount : 0);
385 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
386 if (G_UNLIKELY (olddata_val == newdata))
388 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
389 olddata, olddata_val, newdata)));
392 gst_mini_object_unref (olddata_val);
394 return olddata_val != newdata;
398 * gst_mini_object_weak_ref: (skip)
399 * @object: #GstMiniObject to reference weakly
400 * @notify: callback to invoke before the mini object is freed
401 * @data: extra data to pass to notify
403 * Adds a weak reference callback to a mini object. Weak references are
404 * used for notification when a mini object is finalized. They are called
405 * "weak references" because they allow you to safely hold a pointer
406 * to the mini object without calling gst_mini_object_ref()
407 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
413 gst_mini_object_weak_ref (GstMiniObject * object,
414 GstMiniObjectWeakNotify notify, gpointer data)
418 g_return_if_fail (object != NULL);
419 g_return_if_fail (notify != NULL);
420 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
422 G_LOCK (qdata_mutex);
423 i = object->n_qdata++;
425 g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
426 QDATA_QUARK (object, i) = weak_ref_quark;
427 QDATA_NOTIFY (object, i) = notify;
428 QDATA_DATA (object, i) = data;
429 G_UNLOCK (qdata_mutex);
433 * gst_mini_object_weak_unref: (skip)
434 * @object: #GstMiniObject to remove a weak reference from
435 * @notify: callback to search for
436 * @data: data to search for
438 * Removes a weak reference callback to a mini object.
443 gst_mini_object_weak_unref (GstMiniObject * object,
444 GstMiniObjectWeakNotify notify, gpointer data)
447 gboolean found_one = FALSE;
449 g_return_if_fail (object != NULL);
450 g_return_if_fail (notify != NULL);
452 G_LOCK (qdata_mutex);
453 for (i = 0; i < object->n_qdata; i++) {
454 if (QDATA_QUARK (object, i) == weak_ref_quark &&
455 QDATA_NOTIFY (object, i) == notify && QDATA_DATA (object, i) == data) {
457 if (--object->n_qdata == 0) {
458 /* we don't shrink but free when everything is gone */
459 g_free (object->qdata);
460 object->qdata = NULL;
461 } else if (i != object->n_qdata)
462 QDATA (object, i) = QDATA (object, object->n_qdata);
466 G_UNLOCK (qdata_mutex);
469 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
473 * gst_mini_object_set_qdata:
474 * @object: a #GstMiniObject
475 * @quark: A #GQuark, naming the user data pointer
476 * @data: An opaque user data pointer
477 * @destroy: Function to invoke with @data as argument, when @data
480 * This sets an opaque, named pointer on a miniobject.
481 * The name is specified through a #GQuark (retrived e.g. via
482 * g_quark_from_static_string()), and the pointer
483 * can be gotten back from the @object with gst_mini_object_get_qdata()
484 * until the @object is disposed.
485 * Setting a previously set user data pointer, overrides (frees)
486 * the old pointer set, using #NULL as pointer essentially
487 * removes the data stored.
489 * @destroy may be specified which is called with @data as argument
490 * when the @object is disposed, or the data is being overwritten by
491 * a call to gst_mini_object_set_qdata() with the same @quark.
494 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
495 gpointer data, GDestroyNotify destroy)
498 gpointer old_data = NULL;
499 GDestroyNotify old_notify = NULL;
501 g_return_if_fail (object != NULL);
502 g_return_if_fail (quark > 0);
504 G_LOCK (qdata_mutex);
505 for (i = 0; i < object->n_qdata; i++) {
506 if (QDATA_QUARK (object, i) == quark) {
507 old_data = QDATA_DATA (object, i);
508 old_notify = (GDestroyNotify) QDATA_NOTIFY (object, i);
512 if (--object->n_qdata == 0) {
513 /* we don't shrink but free when everything is gone */
514 g_free (object->qdata);
515 object->qdata = NULL;
516 } else if (i != object->n_qdata)
517 QDATA (object, i) = QDATA (object, object->n_qdata);
524 i = object->n_qdata++;
526 g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
528 QDATA_QUARK (object, i) = quark;
529 QDATA_DATA (object, i) = data;
530 QDATA_NOTIFY (object, i) = (GstMiniObjectWeakNotify) destroy;
531 G_UNLOCK (qdata_mutex);
534 old_notify (old_data);
538 * gst_mini_object_get_qdata:
539 * @object: The GstMiniObject to get a stored user data pointer from
540 * @quark: A #GQuark, naming the user data pointer
542 * This function gets back user data pointers stored via
543 * gst_mini_object_set_qdata().
545 * Returns: (transfer none): The user data pointer set, or %NULL
548 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
551 gpointer result = NULL;
553 g_return_val_if_fail (object != NULL, NULL);
554 g_return_val_if_fail (quark > 0, NULL);
556 G_LOCK (qdata_mutex);
557 for (i = 0; i < object->n_qdata; i++) {
558 if (QDATA_QUARK (object, i) == quark) {
559 result = QDATA_DATA (object, i);
563 G_UNLOCK (qdata_mutex);