Merge branch 'master' into 0.11
[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 /* Mutex used for weak referencing */
49 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
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->flags = 0;
111   mini_object->size = size;
112   mini_object->n_weak_refs = 0;
113   mini_object->weak_refs = NULL;
114 }
115
116 /**
117  * gst_mini_object_copy:
118  * @mini_object: the mini-object to copy
119  *
120  * Creates a copy of the mini-object.
121  *
122  * MT safe
123  *
124  * Returns: (transfer full): the new mini-object.
125  */
126 GstMiniObject *
127 gst_mini_object_copy (const GstMiniObject * mini_object)
128 {
129   GstMiniObject *copy;
130
131   g_return_val_if_fail (mini_object != NULL, NULL);
132
133   if (mini_object->copy)
134     copy = mini_object->copy (mini_object);
135   else
136     copy = NULL;
137
138   return copy;
139 }
140
141 /**
142  * gst_mini_object_is_writable:
143  * @mini_object: the mini-object to check
144  *
145  * Checks if a mini-object is writable.  A mini-object is writable
146  * if the reference count is one and the #GST_MINI_OBJECT_FLAG_READONLY
147  * flag is not set.  Modification of a mini-object should only be
148  * done after verifying that it is writable.
149  *
150  * MT safe
151  *
152  * Returns: TRUE if the object is writable.
153  */
154 gboolean
155 gst_mini_object_is_writable (const GstMiniObject * mini_object)
156 {
157   g_return_val_if_fail (mini_object != NULL, FALSE);
158
159   return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
160 }
161
162 /**
163  * gst_mini_object_make_writable:
164  * @mini_object: (transfer full): the mini-object to make writable
165  *
166  * Checks if a mini-object is writable.  If not, a writable copy is made and
167  * returned.  This gives away the reference to the original mini object,
168  * and returns a reference to the new object.
169  *
170  * MT safe
171  *
172  * Returns: (transfer full): a mini-object (possibly the same pointer) that
173  *     is writable.
174  */
175 GstMiniObject *
176 gst_mini_object_make_writable (GstMiniObject * mini_object)
177 {
178   GstMiniObject *ret;
179
180   g_return_val_if_fail (mini_object != NULL, NULL);
181
182   if (gst_mini_object_is_writable (mini_object)) {
183     ret = mini_object;
184   } else {
185     ret = gst_mini_object_copy (mini_object);
186     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
187         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
188     gst_mini_object_unref (mini_object);
189   }
190
191   return ret;
192 }
193
194 /**
195  * gst_mini_object_ref:
196  * @mini_object: the mini-object
197  *
198  * Increase the reference count of the mini-object.
199  *
200  * Note that the refcount affects the writeability
201  * of @mini-object, see gst_mini_object_is_writable(). It is
202  * important to note that keeping additional references to
203  * GstMiniObject instances can potentially increase the number
204  * of memcpy operations in a pipeline, especially if the miniobject
205  * is a #GstBuffer.
206  *
207  * Returns: (transfer full): the mini-object.
208  */
209 GstMiniObject *
210 gst_mini_object_ref (GstMiniObject * mini_object)
211 {
212   g_return_val_if_fail (mini_object != NULL, NULL);
213   /* we can't assert that the refcount > 0 since the _free functions
214    * increments the refcount from 0 to 1 again to allow resurecting
215    * the object
216    g_return_val_if_fail (mini_object->refcount > 0, NULL);
217    */
218
219   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
220       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
221       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
222
223   g_atomic_int_inc (&mini_object->refcount);
224
225   return mini_object;
226 }
227
228 static void
229 weak_refs_notify (GstMiniObject * obj)
230 {
231   guint i;
232
233   for (i = 0; i < obj->n_weak_refs; i++)
234     obj->weak_refs[i].notify (obj->weak_refs[i].data, obj);
235   g_free (obj->weak_refs);
236 }
237
238 /**
239  * gst_mini_object_unref:
240  * @mini_object: the mini-object
241  *
242  * Decreases the reference count of the mini-object, possibly freeing
243  * the mini-object.
244  */
245 void
246 gst_mini_object_unref (GstMiniObject * mini_object)
247 {
248   g_return_if_fail (mini_object != NULL);
249   g_return_if_fail (mini_object->refcount > 0);
250
251   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
252       mini_object,
253       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
254       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
255
256   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
257     /* At this point, the refcount of the object is 0. We increase the refcount
258      * here because if a subclass recycles the object and gives out a new
259      * reference we don't want to free the instance anymore. */
260     gst_mini_object_ref (mini_object);
261
262     if (mini_object->dispose)
263       mini_object->dispose (mini_object);
264
265     /* decrement the refcount again, if the subclass recycled the object we don't
266      * want to free the instance anymore */
267     if (G_LIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
268       /* The weak reference stack is freed in the notification function */
269       if (mini_object->n_weak_refs)
270         weak_refs_notify (mini_object);
271
272 #ifndef GST_DISABLE_TRACE
273       gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
274 #endif
275       if (mini_object->free)
276         mini_object->free (mini_object);
277     }
278   }
279 }
280
281 /**
282  * gst_mini_object_replace:
283  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
284  *     be replaced
285  * @newdata: pointer to new mini-object
286  *
287  * Atomically modifies a pointer to point to a new mini-object.
288  * The reference count of @olddata is decreased and the reference count of
289  * @newdata is increased.
290  *
291  * Either @newdata and the value pointed to by @olddata may be NULL.
292  *
293  * Returns: TRUE if @newdata was different from @olddata
294  */
295 gboolean
296 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
297 {
298   GstMiniObject *olddata_val;
299
300   g_return_val_if_fail (olddata != NULL, FALSE);
301
302   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
303       *olddata, *olddata ? (*olddata)->refcount : 0,
304       newdata, newdata ? newdata->refcount : 0);
305
306   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
307
308   if (G_UNLIKELY (olddata_val == newdata))
309     return FALSE;
310
311   if (newdata)
312     gst_mini_object_ref (newdata);
313
314   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
315               olddata, olddata_val, newdata))) {
316     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
317     if (G_UNLIKELY (olddata_val == newdata))
318       break;
319   }
320
321   if (olddata_val)
322     gst_mini_object_unref (olddata_val);
323
324   return olddata_val != newdata;
325 }
326
327 /**
328  * gst_mini_object_steal:
329  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
330  *     be stolen
331  *
332  * Replace the current #GstMiniObject pointer to by @olddata with NULL and
333  * return the old value.
334  *
335  * Returns: the #GstMiniObject at @oldata
336  */
337 GstMiniObject *
338 gst_mini_object_steal (GstMiniObject ** olddata)
339 {
340   GstMiniObject *olddata_val;
341
342   g_return_val_if_fail (olddata != NULL, NULL);
343
344   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
345       *olddata, *olddata ? (*olddata)->refcount : 0);
346
347   do {
348     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
349     if (olddata_val == NULL)
350       break;
351   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
352               olddata, olddata_val, NULL)));
353
354   return olddata_val;
355 }
356
357 /**
358  * gst_mini_object_take:
359  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
360  *     be replaced
361  *
362  * Modifies a pointer to point to a new mini-object. The modification
363  * is done atomically. This version is similar to gst_mini_object_replace()
364  * except that it does not increase the refcount of @newdata and thus
365  * takes ownership of @newdata.
366  *
367  * Either @newdata and the value pointed to by @olddata may be NULL.
368  *
369  * Returns: TRUE if @newdata was different from @olddata
370  */
371 gboolean
372 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
373 {
374   GstMiniObject *olddata_val;
375
376   g_return_val_if_fail (olddata != NULL, FALSE);
377
378   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
379       *olddata, *olddata ? (*olddata)->refcount : 0,
380       newdata, newdata ? newdata->refcount : 0);
381
382   do {
383     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
384     if (G_UNLIKELY (olddata_val == newdata))
385       break;
386   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
387               olddata, olddata_val, newdata)));
388
389   if (olddata_val)
390     gst_mini_object_unref (olddata_val);
391
392   return olddata_val != newdata;
393 }
394
395 /**
396  * gst_mini_object_weak_ref: (skip)
397  * @object: #GstMiniObject to reference weakly
398  * @notify: callback to invoke before the mini object is freed
399  * @data: extra data to pass to notify
400  *
401  * Adds a weak reference callback to a mini object. Weak references are
402  * used for notification when a mini object is finalized. They are called
403  * "weak references" because they allow you to safely hold a pointer
404  * to the mini object without calling gst_mini_object_ref()
405  * (gst_mini_object_ref() adds a strong reference, that is, forces the object
406  * to stay alive).
407  *
408  * Since: 0.10.35
409  */
410 void
411 gst_mini_object_weak_ref (GstMiniObject * object,
412     GstMiniObjectWeakNotify notify, gpointer data)
413 {
414   guint i;
415
416   g_return_if_fail (object != NULL);
417   g_return_if_fail (notify != NULL);
418   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
419
420   G_LOCK (weak_refs_mutex);
421
422   if (object->n_weak_refs) {
423     /* Don't add the weak reference if it already exists. */
424     for (i = 0; i < object->n_weak_refs; i++) {
425       if (object->weak_refs[i].notify == notify &&
426           object->weak_refs[i].data == data) {
427         g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
428             G_STRFUNC, notify, data);
429         goto found;
430       }
431     }
432
433     i = object->n_weak_refs++;
434     object->weak_refs =
435         g_realloc (object->weak_refs, sizeof (object->weak_refs[0]) * i);
436   } else {
437     object->weak_refs = g_malloc0 (sizeof (object->weak_refs[0]));
438     object->n_weak_refs = 1;
439     i = 0;
440   }
441   object->weak_refs[i].notify = notify;
442   object->weak_refs[i].data = data;
443 found:
444   G_UNLOCK (weak_refs_mutex);
445 }
446
447 /**
448  * gst_mini_object_weak_unref: (skip)
449  * @object: #GstMiniObject to remove a weak reference from
450  * @notify: callback to search for
451  * @data: data to search for
452  *
453  * Removes a weak reference callback to a mini object.
454  *
455  * Since: 0.10.35
456  */
457 void
458 gst_mini_object_weak_unref (GstMiniObject * object,
459     GstMiniObjectWeakNotify notify, gpointer data)
460 {
461   gboolean found_one = FALSE;
462
463   g_return_if_fail (object != NULL);
464   g_return_if_fail (notify != NULL);
465
466   G_LOCK (weak_refs_mutex);
467
468   if (object->n_weak_refs) {
469     guint i;
470
471     for (i = 0; i < object->n_weak_refs; i++)
472       if (object->weak_refs[i].notify == notify &&
473           object->weak_refs[i].data == data) {
474         found_one = TRUE;
475         object->n_weak_refs -= 1;
476         if (i != object->n_weak_refs)
477           object->weak_refs[i] = object->weak_refs[object->n_weak_refs];
478
479         break;
480       }
481   }
482   G_UNLOCK (weak_refs_mutex);
483   if (!found_one)
484     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
485 }