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 #define GST_DISABLE_TRACE
43 #ifndef GST_DISABLE_TRACE
45 static GstAllocTrace *_gst_mini_object_trace;
48 #define GST_MINI_OBJECT_GET_CLASS_UNCHECKED(obj) \
49 ((GstMiniObjectClass *) (((GTypeInstance*)(obj))->g_class))
51 /* boxed copy and free functions. Don't real copy or free but simply
52 * change the refcount */
53 static GstMiniObject *
54 _gst_mini_object_boxed_copy (GstMiniObject * mini_object)
57 return gst_mini_object_ref (mini_object);
63 _gst_mini_object_boxed_free (GstMiniObject * mini_object)
66 gst_mini_object_unref (mini_object);
70 * gst_mini_object_register:
71 * @name: name of the new boxed type
73 * This function creates a new G_TYPE_BOXED derived type id for a new boxed type
74 * with name @name. The default miniobject refcounting copy and free function
75 * are used for the boxed type.
77 * Returns: a new G_TYPE_BOXED derived type id for @name.
80 gst_mini_object_register (const gchar * name)
84 g_return_val_if_fail (name != NULL, 0);
86 type = g_boxed_type_register_static (name,
87 (GBoxedCopyFunc) _gst_mini_object_boxed_copy,
88 (GBoxedFreeFunc) _gst_mini_object_boxed_free);
94 * gst_mini_object_init:
95 * @mini_object: a #GstMiniObject
96 * @type: the #GType of the mini-object to create
97 * @size: the size of the data
99 * Initializes a mini-object with the desired type and size.
103 * Returns: (transfer full): the new mini-object.
106 gst_mini_object_init (GstMiniObject * mini_object, GType type, gsize size)
108 mini_object->type = type;
109 mini_object->refcount = 1;
110 mini_object->flags = 0;
111 mini_object->size = size;
115 * gst_mini_object_copy:
116 * @mini_object: the mini-object to copy
118 * Creates a copy of the mini-object.
122 * Returns: (transfer full): the new mini-object.
125 gst_mini_object_copy (const GstMiniObject * mini_object)
129 g_return_val_if_fail (mini_object != NULL, NULL);
131 if (mini_object->copy)
132 copy = mini_object->copy (mini_object);
140 * gst_mini_object_is_writable:
141 * @mini_object: the mini-object to check
143 * Checks if a mini-object is writable. A mini-object is writable
144 * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
145 * flag is not set. Modification of a mini-object should only be
146 * done after verifying that it is writable.
150 * Returns: TRUE if the object is writable.
153 gst_mini_object_is_writable (const GstMiniObject * mini_object)
155 g_return_val_if_fail (mini_object != NULL, FALSE);
157 return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
161 * gst_mini_object_make_writable:
162 * @mini_object: (transfer full): the mini-object to make writable
164 * Checks if a mini-object is writable. If not, a writable copy is made and
165 * returned. This gives away the reference to the original mini object,
166 * and returns a reference to the new object.
170 * Returns: (transfer full): a mini-object (possibly the same pointer) that
174 gst_mini_object_make_writable (GstMiniObject * mini_object)
178 g_return_val_if_fail (mini_object != NULL, NULL);
180 if (gst_mini_object_is_writable (mini_object)) {
183 ret = gst_mini_object_copy (mini_object);
184 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
185 g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
186 gst_mini_object_unref (mini_object);
193 * gst_mini_object_ref:
194 * @mini_object: the mini-object
196 * Increase the reference count of the mini-object.
198 * Note that the refcount affects the writeability
199 * of @mini-object, see gst_mini_object_is_writable(). It is
200 * important to note that keeping additional references to
201 * GstMiniObject instances can potentially increase the number
202 * of memcpy operations in a pipeline, especially if the miniobject
205 * Returns: the mini-object.
208 gst_mini_object_ref (GstMiniObject * mini_object)
210 g_return_val_if_fail (mini_object != NULL, NULL);
211 /* we can't assert that the refcount > 0 since the _free functions
212 * increments the refcount from 0 to 1 again to allow resurecting
214 g_return_val_if_fail (mini_object->refcount > 0, NULL);
217 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
218 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
219 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
221 g_atomic_int_inc (&mini_object->refcount);
227 * gst_mini_object_unref:
228 * @mini_object: the mini-object
230 * Decreases the reference count of the mini-object, possibly freeing
234 gst_mini_object_unref (GstMiniObject * mini_object)
236 g_return_if_fail (mini_object != NULL);
237 g_return_if_fail (mini_object->refcount > 0);
239 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
241 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
242 GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
244 if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
245 /* At this point, the refcount of the object is 0. We increase the refcount
246 * here because if a subclass recycles the object and gives out a new
247 * reference we don't want to free the instance anymore. */
248 gst_mini_object_ref (mini_object);
250 if (mini_object->dispose)
251 mini_object->dispose (mini_object);
253 /* decrement the refcount again, if the subclass recycled the object we don't
254 * want to free the instance anymore */
255 if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
256 #ifndef GST_DISABLE_TRACE
257 gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
259 if (mini_object->free)
260 mini_object->free (mini_object);
266 * gst_mini_object_replace:
267 * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
269 * @newdata: pointer to new mini-object
271 * Modifies a pointer to point to a new mini-object. The modification
272 * is done atomically, and the reference counts are updated correctly.
273 * Either @newdata and the value pointed to by @olddata may be NULL.
276 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
278 GstMiniObject *olddata_val;
280 g_return_if_fail (olddata != NULL);
282 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
283 *olddata, *olddata ? (*olddata)->refcount : 0,
284 newdata, newdata ? newdata->refcount : 0);
286 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
288 if (olddata_val == newdata)
292 gst_mini_object_ref (newdata);
294 while (!g_atomic_pointer_compare_and_exchange ((gpointer *) olddata,
295 olddata_val, newdata)) {
296 olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
300 gst_mini_object_unref (olddata_val);