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