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 baseclass like #GObject, but has been stripped down of
26 * features to be fast and small.
27 * It offers sub-classing and ref-counting in the same way as #GObject does.
28 * It has no properties and no signal-support though.
30 * Last reviewed on 2005-11-23 (0.9.5)
36 #include "gst/gst_private.h"
37 #include "gst/gstminiobject.h"
38 #include "gst/gstinfo.h"
39 #include <gobject/gvaluecollector.h>
41 #ifndef GST_DISABLE_TRACE
43 static GstAllocTrace *_gst_mini_object_trace;
46 /* Mutex used for weak referencing */
47 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
50 _priv_gst_mini_object_initialize (void)
52 #ifndef GST_DISABLE_TRACE
53 _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
58 * gst_mini_object_init:
59 * @mini_object: a #GstMiniObject
60 * @type: the #GType of the mini-object to create
61 * @size: the size of the data
63 * Initializes a mini-object with the desired type and size.
67 * Returns: (transfer full): the new mini-object.
70 gst_mini_object_init (GstMiniObject * mini_object, GType type, gsize size)
72 mini_object->type = type;
73 mini_object->refcount = 1;
74 mini_object->flags = 0;
75 mini_object->size = size;
76 mini_object->n_weak_refs = 0;
77 mini_object->weak_refs = NULL;
79 #ifndef GST_DISABLE_TRACE
80 _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
85 * gst_mini_object_copy:
86 * @mini_object: the mini-object to copy
88 * Creates a copy of the mini-object.
92 * Returns: (transfer full): the new mini-object.
95 gst_mini_object_copy (const GstMiniObject * mini_object)
99 g_return_val_if_fail (mini_object != NULL, NULL);
101 if (mini_object->copy)
102 copy = mini_object->copy (mini_object);
110 * gst_mini_object_is_writable:
111 * @mini_object: the mini-object to check
113 * Checks if a mini-object is writable. A mini-object is writable
114 * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
115 * flag is not set. Modification of a mini-object should only be
116 * done after verifying that it is writable.
120 * Returns: TRUE if the object is writable.
123 gst_mini_object_is_writable (const GstMiniObject * mini_object)
125 g_return_val_if_fail (mini_object != NULL, FALSE);
127 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
131 * gst_mini_object_make_writable:
132 * @mini_object: (transfer full): the mini-object to make writable
134 * Checks if a mini-object is writable. If not, a writable copy is made and
135 * returned. This gives away the reference to the original mini object,
136 * and returns a reference to the new object.
140 * Returns: (transfer full): a mini-object (possibly the same pointer) that
144 gst_mini_object_make_writable (GstMiniObject * mini_object)
148 g_return_val_if_fail (mini_object != NULL, NULL);
150 if (gst_mini_object_is_writable (mini_object)) {
153 ret = gst_mini_object_copy (mini_object);
154 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
155 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
156 gst_mini_object_unref (mini_object);
163 * gst_mini_object_ref:
164 * @mini_object: the mini-object
166 * Increase the reference count of the mini-object.
168 * Note that the refcount affects the writeability
169 * of @mini-object, see gst_mini_object_is_writable(). It is
170 * important to note that keeping additional references to
171 * GstMiniObject instances can potentially increase the number
172 * of memcpy operations in a pipeline, especially if the miniobject
175 * Returns: (transfer full): the mini-object.
178 gst_mini_object_ref (GstMiniObject * mini_object)
180 g_return_val_if_fail (mini_object != NULL, NULL);
181 /* we can't assert that the refcount > 0 since the _free functions
182 * increments the refcount from 0 to 1 again to allow resurecting
184 g_return_val_if_fail (mini_object->refcount > 0, NULL);
187 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
188 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
189 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
191 g_atomic_int_inc (&mini_object->refcount);
197 weak_refs_notify (GstMiniObject * obj)
201 for (i = 0; i < obj->n_weak_refs; i++)
202 obj->weak_refs[i].notify (obj->weak_refs[i].data, obj);
203 g_free (obj->weak_refs);
207 * gst_mini_object_unref:
208 * @mini_object: the mini-object
210 * Decreases the reference count of the mini-object, possibly freeing
214 gst_mini_object_unref (GstMiniObject * mini_object)
216 g_return_if_fail (mini_object != NULL);
218 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
220 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
221 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
223 g_return_if_fail (mini_object->refcount > 0);
225 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
228 if (mini_object->dispose)
229 do_free = mini_object->dispose (mini_object);
233 /* if the subclass recycled the object (and returned FALSE) we don't
234 * want to free the instance anymore */
235 if (G_LIKELY (do_free)) {
236 /* The weak reference stack is freed in the notification function */
237 if (mini_object->n_weak_refs)
238 weak_refs_notify (mini_object);
240 #ifndef GST_DISABLE_TRACE
241 _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
243 if (mini_object->free)
244 mini_object->free (mini_object);
250 * gst_mini_object_replace:
251 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
253 * @newdata: pointer to new mini-object
255 * Atomically modifies a pointer to point to a new mini-object.
256 * The reference count of @olddata is decreased and the reference count of
257 * @newdata is increased.
259 * Either @newdata and the value pointed to by @olddata may be NULL.
261 * Returns: TRUE if @newdata was different from @olddata
264 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
266 GstMiniObject *olddata_val;
268 g_return_val_if_fail (olddata != NULL, FALSE);
270 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
271 *olddata, *olddata ? (*olddata)->refcount : 0,
272 newdata, newdata ? newdata->refcount : 0);
274 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
276 if (G_UNLIKELY (olddata_val == newdata))
280 gst_mini_object_ref (newdata);
282 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
283 olddata, olddata_val, newdata))) {
284 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
285 if (G_UNLIKELY (olddata_val == newdata))
290 gst_mini_object_unref (olddata_val);
292 return olddata_val != newdata;
296 * gst_mini_object_steal:
297 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
300 * Replace the current #GstMiniObject pointer to by @olddata with NULL and
301 * return the old value.
303 * Returns: the #GstMiniObject at @oldata
306 gst_mini_object_steal (GstMiniObject ** olddata)
308 GstMiniObject *olddata_val;
310 g_return_val_if_fail (olddata != NULL, NULL);
312 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
313 *olddata, *olddata ? (*olddata)->refcount : 0);
316 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
317 if (olddata_val == NULL)
319 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
320 olddata, olddata_val, NULL)));
326 * gst_mini_object_take:
327 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
329 * @newdata: pointer to new mini-object
331 * Modifies a pointer to point to a new mini-object. The modification
332 * is done atomically. This version is similar to gst_mini_object_replace()
333 * except that it does not increase the refcount of @newdata and thus
334 * takes ownership of @newdata.
336 * Either @newdata and the value pointed to by @olddata may be NULL.
338 * Returns: TRUE if @newdata was different from @olddata
341 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
343 GstMiniObject *olddata_val;
345 g_return_val_if_fail (olddata != NULL, FALSE);
347 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
348 *olddata, *olddata ? (*olddata)->refcount : 0,
349 newdata, newdata ? newdata->refcount : 0);
352 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
353 if (G_UNLIKELY (olddata_val == newdata))
355 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
356 olddata, olddata_val, newdata)));
359 gst_mini_object_unref (olddata_val);
361 return olddata_val != newdata;
365 * gst_mini_object_weak_ref: (skip)
366 * @object: #GstMiniObject to reference weakly
367 * @notify: callback to invoke before the mini object is freed
368 * @data: extra data to pass to notify
370 * Adds a weak reference callback to a mini object. Weak references are
371 * used for notification when a mini object is finalized. They are called
372 * "weak references" because they allow you to safely hold a pointer
373 * to the mini object without calling gst_mini_object_ref()
374 * (gst_mini_object_ref() adds a strong reference, that is, forces the object
380 gst_mini_object_weak_ref (GstMiniObject * object,
381 GstMiniObjectWeakNotify notify, gpointer data)
385 g_return_if_fail (object != NULL);
386 g_return_if_fail (notify != NULL);
387 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
389 G_LOCK (weak_refs_mutex);
391 if (object->n_weak_refs) {
392 /* Don't add the weak reference if it already exists. */
393 for (i = 0; i < object->n_weak_refs; i++) {
394 if (object->weak_refs[i].notify == notify &&
395 object->weak_refs[i].data == data) {
396 g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
397 G_STRFUNC, notify, data);
402 i = object->n_weak_refs++;
404 g_realloc (object->weak_refs, sizeof (object->weak_refs[0]) * i);
406 object->weak_refs = g_malloc0 (sizeof (object->weak_refs[0]));
407 object->n_weak_refs = 1;
410 object->weak_refs[i].notify = notify;
411 object->weak_refs[i].data = data;
413 G_UNLOCK (weak_refs_mutex);
417 * gst_mini_object_weak_unref: (skip)
418 * @object: #GstMiniObject to remove a weak reference from
419 * @notify: callback to search for
420 * @data: data to search for
422 * Removes a weak reference callback to a mini object.
427 gst_mini_object_weak_unref (GstMiniObject * object,
428 GstMiniObjectWeakNotify notify, gpointer data)
430 gboolean found_one = FALSE;
432 g_return_if_fail (object != NULL);
433 g_return_if_fail (notify != NULL);
435 G_LOCK (weak_refs_mutex);
437 if (object->n_weak_refs) {
440 for (i = 0; i < object->n_weak_refs; i++)
441 if (object->weak_refs[i].notify == notify &&
442 object->weak_refs[i].data == data) {
444 object->n_weak_refs -= 1;
445 if (i != object->n_weak_refs)
446 object->weak_refs[i] = object->weak_refs[object->n_weak_refs];
451 G_UNLOCK (weak_refs_mutex);
453 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);