fix compilation
[platform/upstream/gstreamer.git] / gst / gstminiobject.c
1 /* GStreamer
2  * Copyright (C) 2005 David Schleef <ds@schleef.org>
3  *
4  * gstminiobject.h: Header for GstMiniObject
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:gstminiobject
23  * @short_description: Lightweight base class for the GStreamer object hierarchy
24  *
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.
29  *
30  * Last reviewed on 2005-11-23 (0.9.5)
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "gst/gst_private.h"
37 #include "gst/gstminiobject.h"
38 #include "gst/gstinfo.h"
39 #include <gobject/gvaluecollector.h>
40
41 #define GST_DISABLE_TRACE
42
43 #ifndef GST_DISABLE_TRACE
44 #include "gsttrace.h"
45 static GstAllocTrace *_gst_mini_object_trace;
46 #endif
47
48 #define GST_MINI_OBJECT_GET_CLASS_UNCHECKED(obj) \
49     ((GstMiniObjectClass *) (((GTypeInstance*)(obj))->g_class))
50
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)
55 {
56   if (mini_object)
57     return gst_mini_object_ref (mini_object);
58   else
59     return NULL;
60 }
61
62 static void
63 _gst_mini_object_boxed_free (GstMiniObject * mini_object)
64 {
65   if (mini_object)
66     gst_mini_object_unref (mini_object);
67 }
68
69 /**
70  * gst_mini_object_register:
71  * @name: name of the new boxed type
72  *
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.
76  *
77  * Returns: a new G_TYPE_BOXED derived type id for @name.
78  */
79 GType
80 gst_mini_object_register (const gchar * name)
81 {
82   GType type;
83
84   g_return_val_if_fail (name != NULL, 0);
85
86   type = g_boxed_type_register_static (name,
87       (GBoxedCopyFunc) _gst_mini_object_boxed_copy,
88       (GBoxedFreeFunc) _gst_mini_object_boxed_free);
89
90   return type;
91 }
92
93 /**
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
98  *
99  * Initializes a mini-object with the desired type and size.
100  *
101  * MT safe
102  *
103  * Returns: (transfer full): the new mini-object.
104  */
105 void
106 gst_mini_object_init (GstMiniObject * mini_object, GType type, gsize size)
107 {
108   mini_object->type = type;
109   mini_object->refcount = 1;
110   mini_object->size = size;
111 }
112
113 /* FIXME 0.11: Current way of doing the copy makes it impossible
114  * to currectly chain to the parent classes and do a copy in a
115  * subclass without knowing all internals of the parent classes.
116  *
117  * For 0.11 we should do something like the following:
118  *  - The GstMiniObjectClass::copy() implementation of GstMiniObject
119  *    should call g_type_create_instance() with the type of the source
120  *    object.
121  *  - All GstMiniObjectClass::copy() implementations should as first
122  *    thing chain up to the parent class and then do whatever they need
123  *    to do to copy their type specific data. Note that this way the
124  *    instance_init() functions are called!
125  */
126
127 /**
128  * gst_mini_object_copy:
129  * @mini_object: the mini-object to copy
130  *
131  * Creates a copy of the mini-object.
132  *
133  * MT safe
134  *
135  * Returns: (transfer full): the new mini-object.
136  */
137 GstMiniObject *
138 gst_mini_object_copy (const GstMiniObject * mini_object)
139 {
140   GstMiniObject *copy;
141
142   g_return_val_if_fail (mini_object != NULL, NULL);
143
144   if (mini_object->copy)
145     copy = mini_object->copy (mini_object);
146   else
147     copy = NULL;
148
149   return copy;
150 }
151
152 /**
153  * gst_mini_object_is_writable:
154  * @mini_object: the mini-object to check
155  *
156  * Checks if a mini-object is writable.  A mini-object is writable
157  * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
158  * flag is not set.  Modification of a mini-object should only be
159  * done after verifying that it is writable.
160  *
161  * MT safe
162  *
163  * Returns: TRUE if the object is writable.
164  */
165 gboolean
166 gst_mini_object_is_writable (const GstMiniObject * mini_object)
167 {
168   g_return_val_if_fail (mini_object != NULL, FALSE);
169
170   return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1) &&
171       ((mini_object->flags & GST_MINI_OBJECT_FLAG_READONLY) == 0);
172 }
173
174 /**
175  * gst_mini_object_make_writable:
176  * @mini_object: (transfer full): the mini-object to make writable
177  *
178  * Checks if a mini-object is writable.  If not, a writable copy is made and
179  * returned.  This gives away the reference to the original mini object,
180  * and returns a reference to the new object.
181  *
182  * MT safe
183  *
184  * Returns: (transfer full): a mini-object (possibly the same pointer) that
185  *     is writable.
186  */
187 GstMiniObject *
188 gst_mini_object_make_writable (GstMiniObject * mini_object)
189 {
190   GstMiniObject *ret;
191
192   g_return_val_if_fail (mini_object != NULL, NULL);
193
194   if (gst_mini_object_is_writable (mini_object)) {
195     ret = mini_object;
196   } else {
197     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject",
198         g_type_name (G_TYPE_FROM_INSTANCE (mini_object)));
199     ret = gst_mini_object_copy (mini_object);
200     gst_mini_object_unref (mini_object);
201   }
202
203   return ret;
204 }
205
206 /**
207  * gst_mini_object_ref:
208  * @mini_object: the mini-object
209  *
210  * Increase the reference count of the mini-object.
211  *
212  * Note that the refcount affects the writeability
213  * of @mini-object, see gst_mini_object_is_writable(). It is
214  * important to note that keeping additional references to
215  * GstMiniObject instances can potentially increase the number
216  * of memcpy operations in a pipeline, especially if the miniobject
217  * is a #GstBuffer.
218  *
219  * Returns: the mini-object.
220  */
221 GstMiniObject *
222 gst_mini_object_ref (GstMiniObject * mini_object)
223 {
224   g_return_val_if_fail (mini_object != NULL, NULL);
225   /* we can't assert that the refcount > 0 since the _free functions
226    * increments the refcount from 0 to 1 again to allow resurecting
227    * the object
228    g_return_val_if_fail (mini_object->refcount > 0, NULL);
229    */
230
231   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
232       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
233       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
234
235   g_atomic_int_inc (&mini_object->refcount);
236
237   return mini_object;
238 }
239
240 /**
241  * gst_mini_object_unref:
242  * @mini_object: the mini-object
243  *
244  * Decreases the reference count of the mini-object, possibly freeing
245  * the mini-object.
246  */
247 void
248 gst_mini_object_unref (GstMiniObject * mini_object)
249 {
250   g_return_if_fail (mini_object != NULL);
251   g_return_if_fail (mini_object->refcount > 0);
252
253   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
254       mini_object,
255       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
256       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
257
258   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
259     /* At this point, the refcount of the object is 0. We increase the refcount
260      * here because if a subclass recycles the object and gives out a new
261      * reference we don't want to free the instance anymore. */
262     gst_mini_object_ref (mini_object);
263
264     if (mini_object->dispose)
265       mini_object->dispose (mini_object);
266
267     /* decrement the refcount again, if the subclass recycled the object we don't
268      * want to free the instance anymore */
269     if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
270 #ifndef GST_DISABLE_TRACE
271       gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
272 #endif
273       if (mini_object->free)
274         mini_object->free (mini_object);
275     }
276   }
277 }
278
279 /**
280  * gst_mini_object_replace:
281  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
282  *     be replaced
283  * @newdata: pointer to new mini-object
284  *
285  * Modifies a pointer to point to a new mini-object.  The modification
286  * is done atomically, and the reference counts are updated correctly.
287  * Either @newdata and the value pointed to by @olddata may be NULL.
288  */
289 void
290 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
291 {
292   GstMiniObject *olddata_val;
293
294   g_return_if_fail (olddata != NULL);
295
296   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
297       *olddata, *olddata ? (*olddata)->refcount : 0,
298       newdata, newdata ? newdata->refcount : 0);
299
300   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
301
302   if (olddata_val == newdata)
303     return;
304
305   if (newdata)
306     gst_mini_object_ref (newdata);
307
308   while (!g_atomic_pointer_compare_and_exchange ((gpointer *) olddata,
309           olddata_val, newdata)) {
310     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
311   }
312
313   if (olddata_val)
314     gst_mini_object_unref (olddata_val);
315 }