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 * Last reviewed on 2012-03-28 (0.11.3)
49 #include "gst/gst_private.h"
50 #include "gst/gstminiobject.h"
51 #include "gst/gstinfo.h"
52 #include <gobject/gvaluecollector.h>
54 #ifndef GST_DISABLE_TRACE
56 static GstAllocTrace *_gst_mini_object_trace;
59 /* Mutex used for weak referencing */
60 G_LOCK_DEFINE_STATIC (qdata_mutex);
61 static GQuark weak_ref_quark;
64 _priv_gst_mini_object_initialize (void)
66 weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
68 #ifndef GST_DISABLE_TRACE
69 _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
74 * gst_mini_object_init:
75 * @mini_object: a #GstMiniObject
76 * @type: the #GType of the mini-object to create
77 * @size: the size of the data
79 * Initializes a mini-object with the desired type and size.
83 * Returns: (transfer full): the new mini-object.
86 gst_mini_object_init (GstMiniObject * mini_object, GType type)
88 mini_object->type = type;
89 mini_object->refcount = 1;
90 mini_object->flags = 0;
91 mini_object->n_qdata = 0;
92 mini_object->qdata = NULL;
94 #ifndef GST_DISABLE_TRACE
95 _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
100 * gst_mini_object_copy:
101 * @mini_object: the mini-object to copy
103 * Creates a copy of the mini-object.
107 * Returns: (transfer full): the new mini-object.
110 gst_mini_object_copy (const GstMiniObject * mini_object)
114 g_return_val_if_fail (mini_object != NULL, NULL);
116 if (mini_object->copy)
117 copy = mini_object->copy (mini_object);
125 * gst_mini_object_is_writable:
126 * @mini_object: the mini-object to check
128 * Checks if a mini-object is writable. A mini-object is writable
129 * if the reference count is one. Modification of a mini-object should
130 * only be done after verifying that it is writable.
134 * Returns: TRUE if the object is writable.
137 gst_mini_object_is_writable (const GstMiniObject * mini_object)
139 g_return_val_if_fail (mini_object != NULL, FALSE);
141 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
145 * gst_mini_object_make_writable:
146 * @mini_object: (transfer full): the mini-object to make writable
148 * Checks if a mini-object is writable. If not, a writable copy is made and
149 * returned. This gives away the reference to the original mini object,
150 * and returns a reference to the new object.
154 * Returns: (transfer full): a mini-object (possibly the same pointer) that
158 gst_mini_object_make_writable (GstMiniObject * mini_object)
162 g_return_val_if_fail (mini_object != NULL, NULL);
164 if (gst_mini_object_is_writable (mini_object)) {
167 ret = gst_mini_object_copy (mini_object);
168 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
169 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
170 gst_mini_object_unref (mini_object);
177 * gst_mini_object_ref:
178 * @mini_object: the mini-object
180 * Increase the reference count of the mini-object.
182 * Note that the refcount affects the writeability
183 * of @mini-object, see gst_mini_object_is_writable(). It is
184 * important to note that keeping additional references to
185 * GstMiniObject instances can potentially increase the number
186 * of memcpy operations in a pipeline, especially if the miniobject
189 * Returns: (transfer full): the mini-object.
192 gst_mini_object_ref (GstMiniObject * mini_object)
194 g_return_val_if_fail (mini_object != NULL, NULL);
195 /* we can't assert that the refcount > 0 since the _free functions
196 * increments the refcount from 0 to 1 again to allow resurecting
198 g_return_val_if_fail (mini_object->refcount > 0, NULL);
201 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
202 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
203 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
205 g_atomic_int_inc (&mini_object->refcount);
211 qdata_notify (GstMiniObject * obj)
215 for (i = 0; i < obj->n_qdata; i++)
216 obj->qdata[i].notify (obj->qdata[i].data, obj);
221 * gst_mini_object_unref:
222 * @mini_object: the mini-object
224 * Decreases the reference count of the mini-object, possibly freeing
228 gst_mini_object_unref (GstMiniObject * mini_object)
230 g_return_if_fail (mini_object != NULL);
232 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
234 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
235 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
237 g_return_if_fail (mini_object->refcount > 0);
239 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
242 if (mini_object->dispose)
243 do_free = mini_object->dispose (mini_object);
247 /* if the subclass recycled the object (and returned FALSE) we don't
248 * want to free the instance anymore */
249 if (G_LIKELY (do_free)) {
250 /* The weak reference stack is freed in the notification function */
251 if (mini_object->n_qdata)
252 qdata_notify (mini_object);
254 #ifndef GST_DISABLE_TRACE
255 _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
257 if (mini_object->free)
258 mini_object->free (mini_object);
264 * gst_mini_object_replace:
265 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
267 * @newdata: pointer to new mini-object
269 * Atomically modifies a pointer to point to a new mini-object.
270 * The reference count of @olddata is decreased and the reference count of
271 * @newdata is increased.
273 * Either @newdata and the value pointed to by @olddata may be NULL.
275 * Returns: TRUE if @newdata was different from @olddata
278 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
280 GstMiniObject *olddata_val;
282 g_return_val_if_fail (olddata != NULL, FALSE);
284 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
285 *olddata, *olddata ? (*olddata)->refcount : 0,
286 newdata, newdata ? newdata->refcount : 0);
288 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
290 if (G_UNLIKELY (olddata_val == newdata))
294 gst_mini_object_ref (newdata);
296 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
297 olddata, olddata_val, newdata))) {
298 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
299 if (G_UNLIKELY (olddata_val == newdata))
304 gst_mini_object_unref (olddata_val);
306 return olddata_val != newdata;
310 * gst_mini_object_steal:
311 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
314 * Replace the current #GstMiniObject pointer to by @olddata with NULL and
315 * return the old value.
317 * Returns: the #GstMiniObject at @oldata
320 gst_mini_object_steal (GstMiniObject ** olddata)
322 GstMiniObject *olddata_val;
324 g_return_val_if_fail (olddata != NULL, NULL);
326 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
327 *olddata, *olddata ? (*olddata)->refcount : 0);
330 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
331 if (olddata_val == NULL)
333 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
334 olddata, olddata_val, NULL)));
340 * gst_mini_object_take:
341 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
343 * @newdata: pointer to new mini-object
345 * Modifies a pointer to point to a new mini-object. The modification
346 * is done atomically. This version is similar to gst_mini_object_replace()
347 * except that it does not increase the refcount of @newdata and thus
348 * takes ownership of @newdata.
350 * Either @newdata and the value pointed to by @olddata may be NULL.
352 * Returns: TRUE if @newdata was different from @olddata
355 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
357 GstMiniObject *olddata_val;
359 g_return_val_if_fail (olddata != NULL, FALSE);
361 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
362 *olddata, *olddata ? (*olddata)->refcount : 0,
363 newdata, newdata ? newdata->refcount : 0);
366 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
367 if (G_UNLIKELY (olddata_val == newdata))
369 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
370 olddata, olddata_val, newdata)));
373 gst_mini_object_unref (olddata_val);
375 return olddata_val != newdata;
379 * gst_mini_object_weak_ref: (skip)
380 * @object: #GstMiniObject to reference weakly
381 * @notify: callback to invoke before the mini object is freed
382 * @data: extra data to pass to notify
384 * Adds a weak reference callback to a mini object. Weak references are
385 * used for notification when a mini object is finalized. They are called
386 * "weak references" because they allow you to safely hold a pointer
387 * to the mini object without calling gst_mini_object_ref()
388 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
394 gst_mini_object_weak_ref (GstMiniObject * object,
395 GstMiniObjectWeakNotify notify, gpointer data)
399 g_return_if_fail (object != NULL);
400 g_return_if_fail (notify != NULL);
401 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
403 G_LOCK (qdata_mutex);
404 i = object->n_qdata++;
406 g_realloc (object->qdata, sizeof (object->qdata[0]) * object->n_qdata);
407 object->qdata[i].quark = weak_ref_quark;
408 object->qdata[i].notify = notify;
409 object->qdata[i].data = data;
410 G_UNLOCK (qdata_mutex);
414 * gst_mini_object_weak_unref: (skip)
415 * @object: #GstMiniObject to remove a weak reference from
416 * @notify: callback to search for
417 * @data: data to search for
419 * Removes a weak reference callback to a mini object.
424 gst_mini_object_weak_unref (GstMiniObject * object,
425 GstMiniObjectWeakNotify notify, gpointer data)
428 gboolean found_one = FALSE;
430 g_return_if_fail (object != NULL);
431 g_return_if_fail (notify != NULL);
433 G_LOCK (qdata_mutex);
434 for (i = 0; i < object->n_qdata; i++) {
435 if (object->qdata[i].quark == weak_ref_quark &&
436 object->qdata[i].notify == notify && object->qdata[i].data == data) {
438 if (--object->n_qdata == 0) {
439 /* we don't shrink but free when everything is gone */
440 g_free (object->qdata);
441 object->qdata = NULL;
442 } else if (i != object->n_qdata)
443 object->qdata[i] = object->qdata[object->n_qdata];
447 G_UNLOCK (qdata_mutex);
450 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
454 * gst_mini_object_set_qdata:
455 * @object: a #GstMiniObject
456 * @quark: A #GQuark, naming the user data pointer
457 * @data: An opaque user data pointer
458 * @destroy: Function to invoke with @data as argument, when @data
461 * This sets an opaque, named pointer on a miniobject.
462 * The name is specified through a #GQuark (retrived e.g. via
463 * g_quark_from_static_string()), and the pointer
464 * can be gotten back from the @object with gst_mini_object_get_qdata()
465 * until the @object is disposed.
466 * Setting a previously set user data pointer, overrides (frees)
467 * the old pointer set, using #NULL as pointer essentially
468 * removes the data stored.
470 * @destroy may be specified which is called with @data as argument
471 * when the @object is disposed, or the data is being overwritten by
472 * a call to gst_mini_object_set_qdata() with the same @quark.
475 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
476 gpointer data, GDestroyNotify destroy)
479 gpointer old_data = NULL;
480 GDestroyNotify old_notify = NULL;
482 g_return_if_fail (object != NULL);
483 g_return_if_fail (quark > 0);
485 G_LOCK (qdata_mutex);
486 for (i = 0; i < object->n_qdata; i++) {
487 if (object->qdata[i].quark == quark) {
488 old_data = object->qdata[i].data;
489 old_notify = (GDestroyNotify) object->qdata[i].notify;
493 if (--object->n_qdata == 0) {
494 /* we don't shrink but free when everything is gone */
495 g_free (object->qdata);
496 object->qdata = NULL;
497 } else if (i != object->n_qdata)
498 object->qdata[i] = object->qdata[object->n_qdata];
505 i = object->n_qdata++;
507 g_realloc (object->qdata, sizeof (object->qdata[0]) * object->n_qdata);
509 object->qdata[i].quark = quark;
510 object->qdata[i].data = data;
511 object->qdata[i].notify = (GstMiniObjectWeakNotify) destroy;
512 G_UNLOCK (qdata_mutex);
515 old_notify (old_data);
519 * gst_mini_object_get_qdata:
520 * @object: The GstMiniObject to get a stored user data pointer from
521 * @quark: A #GQuark, naming the user data pointer
523 * This function gets back user data pointers stored via
524 * gst_mini_object_set_qdata().
526 * Returns: (transfer none): The user data pointer set, or %NULL
529 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
532 gpointer result = NULL;
534 g_return_val_if_fail (object != NULL, NULL);
535 g_return_val_if_fail (quark > 0, NULL);
537 G_LOCK (qdata_mutex);
538 for (i = 0; i < object->n_qdata; i++) {
539 if (object->qdata[i].quark == quark) {
540 result = object->qdata[i].data;
544 G_UNLOCK (qdata_mutex);