bf488d5d4fa63c8f6bcedd8d84907fb33778251f
[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 simple structure that can be used to implement refcounted
26  * types.
27  *
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.
30  *
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.
35  *
36  * A copy can be made with gst_mini_object_copy().
37  *
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.
42  *
43  * Opaque data can be associated with a #GstMiniObject with
44  * gst_mini_object_set_qdata() and gst_mini_object_get_qdata(). The data is
45  * meant to be specific to the particular object and is not automatically copied
46  * with gst_mini_object_copy() or similar methods.
47  *
48  * A weak reference can be added and remove with gst_mini_object_weak_ref()
49  * and gst_mini_object_weak_unref() respectively.
50  *
51  * Last reviewed on 2012-06-15 (0.11.93)
52  */
53 #ifdef HAVE_CONFIG_H
54 #include "config.h"
55 #endif
56
57 #include "gst/gst_private.h"
58 #include "gst/gstminiobject.h"
59 #include "gst/gstinfo.h"
60 #include <gobject/gvaluecollector.h>
61
62 #ifndef GST_DISABLE_TRACE
63 #include "gsttrace.h"
64 static GstAllocTrace *_gst_mini_object_trace;
65 #endif
66
67 /* Mutex used for weak referencing */
68 G_LOCK_DEFINE_STATIC (qdata_mutex);
69 static GQuark weak_ref_quark;
70
71 typedef struct
72 {
73   GQuark quark;
74   GstMiniObjectWeakNotify notify;
75   gpointer data;
76 } GstQData;
77
78 #define QDATA(o,i)        ((GstQData *)(o)->qdata)[(i)]
79 #define QDATA_QUARK(o,i)  (QDATA(o,i).quark)
80 #define QDATA_NOTIFY(o,i) (QDATA(o,i).notify)
81 #define QDATA_DATA(o,i)   (QDATA(o,i).data)
82
83 void
84 _priv_gst_mini_object_initialize (void)
85 {
86   weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
87
88 #ifndef GST_DISABLE_TRACE
89   _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
90 #endif
91 }
92
93 /**
94  * gst_mini_object_init:
95  * @mini_object: a #GstMiniObject 
96  * @type: the #GType of the mini-object to create
97  *
98  * Initializes a mini-object with the desired type and size.
99  *
100  * MT safe
101  *
102  * Returns: (transfer full): the new mini-object.
103  */
104 void
105 gst_mini_object_init (GstMiniObject * mini_object, GType type)
106 {
107   mini_object->type = type;
108   mini_object->refcount = 1;
109   mini_object->flags = 0;
110   mini_object->n_qdata = 0;
111   mini_object->qdata = NULL;
112
113 #ifndef GST_DISABLE_TRACE
114   _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
115 #endif
116 }
117
118 /**
119  * gst_mini_object_copy:
120  * @mini_object: the mini-object to copy
121  *
122  * Creates a copy of the mini-object.
123  *
124  * MT safe
125  *
126  * Returns: (transfer full): the new mini-object.
127  */
128 GstMiniObject *
129 gst_mini_object_copy (const GstMiniObject * mini_object)
130 {
131   GstMiniObject *copy;
132
133   g_return_val_if_fail (mini_object != NULL, NULL);
134
135   if (mini_object->copy)
136     copy = mini_object->copy (mini_object);
137   else
138     copy = NULL;
139
140   return copy;
141 }
142
143 /**
144  * gst_mini_object_is_writable:
145  * @mini_object: the mini-object to check
146  *
147  * Checks if a mini-object is writable.  A mini-object is writable
148  * if the reference count is one. Modification of a mini-object should
149  * only be done after verifying that it is writable.
150  *
151  * MT safe
152  *
153  * Returns: TRUE if the object is writable.
154  */
155 gboolean
156 gst_mini_object_is_writable (const GstMiniObject * mini_object)
157 {
158   g_return_val_if_fail (mini_object != NULL, FALSE);
159
160   return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
161 }
162
163 /**
164  * gst_mini_object_make_writable:
165  * @mini_object: (transfer full): the mini-object to make writable
166  *
167  * Checks if a mini-object is writable.  If not, a writable copy is made and
168  * returned.  This gives away the reference to the original mini object,
169  * and returns a reference to the new object.
170  *
171  * MT safe
172  *
173  * Returns: (transfer full): a mini-object (possibly the same pointer) that
174  *     is writable.
175  */
176 GstMiniObject *
177 gst_mini_object_make_writable (GstMiniObject * mini_object)
178 {
179   GstMiniObject *ret;
180
181   g_return_val_if_fail (mini_object != NULL, NULL);
182
183   if (gst_mini_object_is_writable (mini_object)) {
184     ret = mini_object;
185   } else {
186     ret = gst_mini_object_copy (mini_object);
187     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
188         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
189     gst_mini_object_unref (mini_object);
190   }
191
192   return ret;
193 }
194
195 /**
196  * gst_mini_object_ref:
197  * @mini_object: the mini-object
198  *
199  * Increase the reference count of the mini-object.
200  *
201  * Note that the refcount affects the writeability
202  * of @mini-object, see gst_mini_object_is_writable(). It is
203  * important to note that keeping additional references to
204  * GstMiniObject instances can potentially increase the number
205  * of memcpy operations in a pipeline, especially if the miniobject
206  * is a #GstBuffer.
207  *
208  * Returns: (transfer full): the mini-object.
209  */
210 GstMiniObject *
211 gst_mini_object_ref (GstMiniObject * mini_object)
212 {
213   g_return_val_if_fail (mini_object != NULL, NULL);
214   /* we can't assert that the refcount > 0 since the _free functions
215    * increments the refcount from 0 to 1 again to allow resurecting
216    * the object
217    g_return_val_if_fail (mini_object->refcount > 0, NULL);
218    */
219
220   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
221       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
222       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
223
224   g_atomic_int_inc (&mini_object->refcount);
225
226   return mini_object;
227 }
228
229 static void
230 qdata_notify (GstMiniObject * obj)
231 {
232   guint i;
233
234   for (i = 0; i < obj->n_qdata; i++)
235     QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
236   g_free (obj->qdata);
237 }
238
239 /**
240  * gst_mini_object_unref:
241  * @mini_object: the mini-object
242  *
243  * Decreases the reference count of the mini-object, possibly freeing
244  * the mini-object.
245  */
246 void
247 gst_mini_object_unref (GstMiniObject * mini_object)
248 {
249   g_return_if_fail (mini_object != NULL);
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   g_return_if_fail (mini_object->refcount > 0);
257
258   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
259     gboolean do_free;
260
261     if (mini_object->dispose)
262       do_free = mini_object->dispose (mini_object);
263     else
264       do_free = TRUE;
265
266     /* if the subclass recycled the object (and returned FALSE) we don't
267      * want to free the instance anymore */
268     if (G_LIKELY (do_free)) {
269       /* The weak reference stack is freed in the notification function */
270       if (mini_object->n_qdata)
271         qdata_notify (mini_object);
272
273 #ifndef GST_DISABLE_TRACE
274       _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
275 #endif
276       if (mini_object->free)
277         mini_object->free (mini_object);
278     }
279   }
280 }
281
282 /**
283  * gst_mini_object_replace:
284  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
285  *     be replaced
286  * @newdata: pointer to new mini-object
287  *
288  * Atomically modifies a pointer to point to a new mini-object.
289  * The reference count of @olddata is decreased and the reference count of
290  * @newdata is increased.
291  *
292  * Either @newdata and the value pointed to by @olddata may be NULL.
293  *
294  * Returns: TRUE if @newdata was different from @olddata
295  */
296 gboolean
297 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
298 {
299   GstMiniObject *olddata_val;
300
301   g_return_val_if_fail (olddata != NULL, FALSE);
302
303   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
304       *olddata, *olddata ? (*olddata)->refcount : 0,
305       newdata, newdata ? newdata->refcount : 0);
306
307   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
308
309   if (G_UNLIKELY (olddata_val == newdata))
310     return FALSE;
311
312   if (newdata)
313     gst_mini_object_ref (newdata);
314
315   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
316               olddata, olddata_val, newdata))) {
317     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
318     if (G_UNLIKELY (olddata_val == newdata))
319       break;
320   }
321
322   if (olddata_val)
323     gst_mini_object_unref (olddata_val);
324
325   return olddata_val != newdata;
326 }
327
328 /**
329  * gst_mini_object_steal:
330  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
331  *     be stolen
332  *
333  * Replace the current #GstMiniObject pointer to by @olddata with NULL and
334  * return the old value.
335  *
336  * Returns: the #GstMiniObject at @oldata
337  */
338 GstMiniObject *
339 gst_mini_object_steal (GstMiniObject ** olddata)
340 {
341   GstMiniObject *olddata_val;
342
343   g_return_val_if_fail (olddata != NULL, NULL);
344
345   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
346       *olddata, *olddata ? (*olddata)->refcount : 0);
347
348   do {
349     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
350     if (olddata_val == NULL)
351       break;
352   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
353               olddata, olddata_val, NULL)));
354
355   return olddata_val;
356 }
357
358 /**
359  * gst_mini_object_take:
360  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
361  *     be replaced
362  * @newdata: pointer to new mini-object
363  *
364  * Modifies a pointer to point to a new mini-object. The modification
365  * is done atomically. This version is similar to gst_mini_object_replace()
366  * except that it does not increase the refcount of @newdata and thus
367  * takes ownership of @newdata.
368  *
369  * Either @newdata and the value pointed to by @olddata may be NULL.
370  *
371  * Returns: TRUE if @newdata was different from @olddata
372  */
373 gboolean
374 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
375 {
376   GstMiniObject *olddata_val;
377
378   g_return_val_if_fail (olddata != NULL, FALSE);
379
380   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
381       *olddata, *olddata ? (*olddata)->refcount : 0,
382       newdata, newdata ? newdata->refcount : 0);
383
384   do {
385     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
386     if (G_UNLIKELY (olddata_val == newdata))
387       break;
388   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
389               olddata, olddata_val, newdata)));
390
391   if (olddata_val)
392     gst_mini_object_unref (olddata_val);
393
394   return olddata_val != newdata;
395 }
396
397 /**
398  * gst_mini_object_weak_ref: (skip)
399  * @object: #GstMiniObject to reference weakly
400  * @notify: callback to invoke before the mini object is freed
401  * @data: extra data to pass to notify
402  *
403  * Adds a weak reference callback to a mini object. Weak references are
404  * used for notification when a mini object is finalized. They are called
405  * "weak references" because they allow you to safely hold a pointer
406  * to the mini object without calling gst_mini_object_ref()
407  * (gst_mini_object_ref() adds a strong reference, that is, forces the object
408  * to stay alive).
409  *
410  * Since: 0.10.35
411  */
412 void
413 gst_mini_object_weak_ref (GstMiniObject * object,
414     GstMiniObjectWeakNotify notify, gpointer data)
415 {
416   guint i;
417
418   g_return_if_fail (object != NULL);
419   g_return_if_fail (notify != NULL);
420   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
421
422   G_LOCK (qdata_mutex);
423   i = object->n_qdata++;
424   object->qdata =
425       g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
426   QDATA_QUARK (object, i) = weak_ref_quark;
427   QDATA_NOTIFY (object, i) = notify;
428   QDATA_DATA (object, i) = data;
429   G_UNLOCK (qdata_mutex);
430 }
431
432 /**
433  * gst_mini_object_weak_unref: (skip)
434  * @object: #GstMiniObject to remove a weak reference from
435  * @notify: callback to search for
436  * @data: data to search for
437  *
438  * Removes a weak reference callback to a mini object.
439  *
440  * Since: 0.10.35
441  */
442 void
443 gst_mini_object_weak_unref (GstMiniObject * object,
444     GstMiniObjectWeakNotify notify, gpointer data)
445 {
446   guint i;
447   gboolean found_one = FALSE;
448
449   g_return_if_fail (object != NULL);
450   g_return_if_fail (notify != NULL);
451
452   G_LOCK (qdata_mutex);
453   for (i = 0; i < object->n_qdata; i++) {
454     if (QDATA_QUARK (object, i) == weak_ref_quark &&
455         QDATA_NOTIFY (object, i) == notify && QDATA_DATA (object, i) == data) {
456       found_one = TRUE;
457       if (--object->n_qdata == 0) {
458         /* we don't shrink but free when everything is gone */
459         g_free (object->qdata);
460         object->qdata = NULL;
461       } else if (i != object->n_qdata)
462         QDATA (object, i) = QDATA (object, object->n_qdata);
463       break;
464     }
465   }
466   G_UNLOCK (qdata_mutex);
467
468   if (!found_one)
469     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
470 }
471
472 /**
473  * gst_mini_object_set_qdata:
474  * @object: a #GstMiniObject
475  * @quark: A #GQuark, naming the user data pointer
476  * @data: An opaque user data pointer
477  * @destroy: Function to invoke with @data as argument, when @data
478  *           needs to be freed
479  *
480  * This sets an opaque, named pointer on a miniobject.
481  * The name is specified through a #GQuark (retrived e.g. via
482  * g_quark_from_static_string()), and the pointer
483  * can be gotten back from the @object with gst_mini_object_get_qdata()
484  * until the @object is disposed.
485  * Setting a previously set user data pointer, overrides (frees)
486  * the old pointer set, using #NULL as pointer essentially
487  * removes the data stored.
488  *
489  * @destroy may be specified which is called with @data as argument
490  * when the @object is disposed, or the data is being overwritten by
491  * a call to gst_mini_object_set_qdata() with the same @quark.
492  */
493 void
494 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
495     gpointer data, GDestroyNotify destroy)
496 {
497   guint i;
498   gpointer old_data = NULL;
499   GDestroyNotify old_notify = NULL;
500
501   g_return_if_fail (object != NULL);
502   g_return_if_fail (quark > 0);
503
504   G_LOCK (qdata_mutex);
505   for (i = 0; i < object->n_qdata; i++) {
506     if (QDATA_QUARK (object, i) == quark) {
507       old_data = QDATA_DATA (object, i);
508       old_notify = (GDestroyNotify) QDATA_NOTIFY (object, i);
509
510       if (data == NULL) {
511         /* remove item */
512         if (--object->n_qdata == 0) {
513           /* we don't shrink but free when everything is gone */
514           g_free (object->qdata);
515           object->qdata = NULL;
516         } else if (i != object->n_qdata)
517           QDATA (object, i) = QDATA (object, object->n_qdata);
518       }
519       break;
520     }
521   }
522   if (!old_data) {
523     /* add item */
524     i = object->n_qdata++;
525     object->qdata =
526         g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
527   }
528   QDATA_QUARK (object, i) = quark;
529   QDATA_DATA (object, i) = data;
530   QDATA_NOTIFY (object, i) = (GstMiniObjectWeakNotify) destroy;
531   G_UNLOCK (qdata_mutex);
532
533   if (old_notify)
534     old_notify (old_data);
535 }
536
537 /**
538  * gst_mini_object_get_qdata:
539  * @object: The GstMiniObject to get a stored user data pointer from
540  * @quark: A #GQuark, naming the user data pointer
541  *
542  * This function gets back user data pointers stored via
543  * gst_mini_object_set_qdata().
544  *
545  * Returns: (transfer none): The user data pointer set, or %NULL
546  */
547 gpointer
548 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
549 {
550   guint i;
551   gpointer result = NULL;
552
553   g_return_val_if_fail (object != NULL, NULL);
554   g_return_val_if_fail (quark > 0, NULL);
555
556   G_LOCK (qdata_mutex);
557   for (i = 0; i < object->n_qdata; i++) {
558     if (QDATA_QUARK (object, i) == quark) {
559       result = QDATA_DATA (object, i);
560       break;
561     }
562   }
563   G_UNLOCK (qdata_mutex);
564
565   return result;
566 }