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 (weak_refs_mutex);
63 _priv_gst_mini_object_initialize (void)
65 #ifndef GST_DISABLE_TRACE
66 _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
71 * gst_mini_object_init:
72 * @mini_object: a #GstMiniObject
73 * @type: the #GType of the mini-object to create
74 * @size: the size of the data
76 * Initializes a mini-object with the desired type and size.
80 * Returns: (transfer full): the new mini-object.
83 gst_mini_object_init (GstMiniObject * mini_object, GType type)
85 mini_object->type = type;
86 mini_object->refcount = 1;
87 mini_object->flags = 0;
88 mini_object->n_weak_refs = 0;
89 mini_object->weak_refs = NULL;
91 #ifndef GST_DISABLE_TRACE
92 _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
97 * gst_mini_object_copy:
98 * @mini_object: the mini-object to copy
100 * Creates a copy of the mini-object.
104 * Returns: (transfer full): the new mini-object.
107 gst_mini_object_copy (const GstMiniObject * mini_object)
111 g_return_val_if_fail (mini_object != NULL, NULL);
113 if (mini_object->copy)
114 copy = mini_object->copy (mini_object);
122 * gst_mini_object_is_writable:
123 * @mini_object: the mini-object to check
125 * Checks if a mini-object is writable. A mini-object is writable
126 * if the reference count is one. Modification of a mini-object should
127 * only be done after verifying that it is writable.
131 * Returns: TRUE if the object is writable.
134 gst_mini_object_is_writable (const GstMiniObject * mini_object)
136 g_return_val_if_fail (mini_object != NULL, FALSE);
138 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
142 * gst_mini_object_make_writable:
143 * @mini_object: (transfer full): the mini-object to make writable
145 * Checks if a mini-object is writable. If not, a writable copy is made and
146 * returned. This gives away the reference to the original mini object,
147 * and returns a reference to the new object.
151 * Returns: (transfer full): a mini-object (possibly the same pointer) that
155 gst_mini_object_make_writable (GstMiniObject * mini_object)
159 g_return_val_if_fail (mini_object != NULL, NULL);
161 if (gst_mini_object_is_writable (mini_object)) {
164 ret = gst_mini_object_copy (mini_object);
165 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
166 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
167 gst_mini_object_unref (mini_object);
174 * gst_mini_object_ref:
175 * @mini_object: the mini-object
177 * Increase the reference count of the mini-object.
179 * Note that the refcount affects the writeability
180 * of @mini-object, see gst_mini_object_is_writable(). It is
181 * important to note that keeping additional references to
182 * GstMiniObject instances can potentially increase the number
183 * of memcpy operations in a pipeline, especially if the miniobject
186 * Returns: (transfer full): the mini-object.
189 gst_mini_object_ref (GstMiniObject * mini_object)
191 g_return_val_if_fail (mini_object != NULL, NULL);
192 /* we can't assert that the refcount > 0 since the _free functions
193 * increments the refcount from 0 to 1 again to allow resurecting
195 g_return_val_if_fail (mini_object->refcount > 0, NULL);
198 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
199 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
200 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
202 g_atomic_int_inc (&mini_object->refcount);
208 weak_refs_notify (GstMiniObject * obj)
212 for (i = 0; i < obj->n_weak_refs; i++)
213 obj->weak_refs[i].notify (obj->weak_refs[i].data, obj);
214 g_free (obj->weak_refs);
218 * gst_mini_object_unref:
219 * @mini_object: the mini-object
221 * Decreases the reference count of the mini-object, possibly freeing
225 gst_mini_object_unref (GstMiniObject * mini_object)
227 g_return_if_fail (mini_object != NULL);
229 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
231 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
232 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
234 g_return_if_fail (mini_object->refcount > 0);
236 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
239 if (mini_object->dispose)
240 do_free = mini_object->dispose (mini_object);
244 /* if the subclass recycled the object (and returned FALSE) we don't
245 * want to free the instance anymore */
246 if (G_LIKELY (do_free)) {
247 /* The weak reference stack is freed in the notification function */
248 if (mini_object->n_weak_refs)
249 weak_refs_notify (mini_object);
251 #ifndef GST_DISABLE_TRACE
252 _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
254 if (mini_object->free)
255 mini_object->free (mini_object);
261 * gst_mini_object_replace:
262 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
264 * @newdata: pointer to new mini-object
266 * Atomically modifies a pointer to point to a new mini-object.
267 * The reference count of @olddata is decreased and the reference count of
268 * @newdata is increased.
270 * Either @newdata and the value pointed to by @olddata may be NULL.
272 * Returns: TRUE if @newdata was different from @olddata
275 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
277 GstMiniObject *olddata_val;
279 g_return_val_if_fail (olddata != NULL, FALSE);
281 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
282 *olddata, *olddata ? (*olddata)->refcount : 0,
283 newdata, newdata ? newdata->refcount : 0);
285 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
287 if (G_UNLIKELY (olddata_val == newdata))
291 gst_mini_object_ref (newdata);
293 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
294 olddata, olddata_val, newdata))) {
295 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
296 if (G_UNLIKELY (olddata_val == newdata))
301 gst_mini_object_unref (olddata_val);
303 return olddata_val != newdata;
307 * gst_mini_object_steal:
308 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
311 * Replace the current #GstMiniObject pointer to by @olddata with NULL and
312 * return the old value.
314 * Returns: the #GstMiniObject at @oldata
317 gst_mini_object_steal (GstMiniObject ** olddata)
319 GstMiniObject *olddata_val;
321 g_return_val_if_fail (olddata != NULL, NULL);
323 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
324 *olddata, *olddata ? (*olddata)->refcount : 0);
327 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
328 if (olddata_val == NULL)
330 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
331 olddata, olddata_val, NULL)));
337 * gst_mini_object_take:
338 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
340 * @newdata: pointer to new mini-object
342 * Modifies a pointer to point to a new mini-object. The modification
343 * is done atomically. This version is similar to gst_mini_object_replace()
344 * except that it does not increase the refcount of @newdata and thus
345 * takes ownership of @newdata.
347 * Either @newdata and the value pointed to by @olddata may be NULL.
349 * Returns: TRUE if @newdata was different from @olddata
352 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
354 GstMiniObject *olddata_val;
356 g_return_val_if_fail (olddata != NULL, FALSE);
358 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
359 *olddata, *olddata ? (*olddata)->refcount : 0,
360 newdata, newdata ? newdata->refcount : 0);
363 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
364 if (G_UNLIKELY (olddata_val == newdata))
366 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
367 olddata, olddata_val, newdata)));
370 gst_mini_object_unref (olddata_val);
372 return olddata_val != newdata;
376 * gst_mini_object_weak_ref: (skip)
377 * @object: #GstMiniObject to reference weakly
378 * @notify: callback to invoke before the mini object is freed
379 * @data: extra data to pass to notify
381 * Adds a weak reference callback to a mini object. Weak references are
382 * used for notification when a mini object is finalized. They are called
383 * "weak references" because they allow you to safely hold a pointer
384 * to the mini object without calling gst_mini_object_ref()
385 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
391 gst_mini_object_weak_ref (GstMiniObject * object,
392 GstMiniObjectWeakNotify notify, gpointer data)
396 g_return_if_fail (object != NULL);
397 g_return_if_fail (notify != NULL);
398 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
400 G_LOCK (weak_refs_mutex);
402 if (object->n_weak_refs) {
403 /* Don't add the weak reference if it already exists. */
404 for (i = 0; i < object->n_weak_refs; i++) {
405 if (object->weak_refs[i].notify == notify &&
406 object->weak_refs[i].data == data) {
407 g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
408 G_STRFUNC, notify, data);
413 i = object->n_weak_refs++;
415 g_realloc (object->weak_refs, sizeof (object->weak_refs[0]) * i);
417 object->weak_refs = g_malloc0 (sizeof (object->weak_refs[0]));
418 object->n_weak_refs = 1;
421 object->weak_refs[i].notify = notify;
422 object->weak_refs[i].data = data;
424 G_UNLOCK (weak_refs_mutex);
428 * gst_mini_object_weak_unref: (skip)
429 * @object: #GstMiniObject to remove a weak reference from
430 * @notify: callback to search for
431 * @data: data to search for
433 * Removes a weak reference callback to a mini object.
438 gst_mini_object_weak_unref (GstMiniObject * object,
439 GstMiniObjectWeakNotify notify, gpointer data)
441 gboolean found_one = FALSE;
443 g_return_if_fail (object != NULL);
444 g_return_if_fail (notify != NULL);
446 G_LOCK (weak_refs_mutex);
448 if (object->n_weak_refs) {
451 for (i = 0; i < object->n_weak_refs; i++)
452 if (object->weak_refs[i].notify == notify &&
453 object->weak_refs[i].data == data) {
455 object->n_weak_refs -= 1;
456 if (i != object->n_weak_refs)
457 object->weak_refs[i] = object->weak_refs[object->n_weak_refs];
462 G_UNLOCK (weak_refs_mutex);
464 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);