miniobject: fix some miniobject docs
[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 #define SHARE_ONE (1 << 16)
72 #define SHARE_MASK (~(SHARE_ONE - 1))
73 #define LOCK_ONE (GST_LOCK_FLAG_LAST)
74 #define FLAG_MASK (GST_LOCK_FLAG_LAST - 1)
75 #define LOCK_MASK ((SHARE_ONE - 1) - FLAG_MASK)
76 #define LOCK_FLAG_MASK (SHARE_ONE - 1)
77
78 typedef struct
79 {
80   GQuark quark;
81   GstMiniObjectNotify notify;
82   gpointer data;
83   GDestroyNotify destroy;
84 } GstQData;
85
86 #define QDATA(o,i)          ((GstQData *)(o)->qdata)[(i)]
87 #define QDATA_QUARK(o,i)    (QDATA(o,i).quark)
88 #define QDATA_NOTIFY(o,i)   (QDATA(o,i).notify)
89 #define QDATA_DATA(o,i)     (QDATA(o,i).data)
90 #define QDATA_DESTROY(o,i)  (QDATA(o,i).destroy)
91
92 void
93 _priv_gst_mini_object_initialize (void)
94 {
95   weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
96
97 #ifndef GST_DISABLE_TRACE
98   _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
99 #endif
100 }
101
102 /**
103  * gst_mini_object_init: (skip)
104  * @mini_object: a #GstMiniObject
105  * @type: the #GType of the mini-object to create
106  * @copy_func: the copy function, or NULL
107  * @dispose_func: the dispose function, or NULL
108  * @free_func: the free function or NULL
109  *
110  * Initializes a mini-object with the desired type and copy/dispose/free
111  * functions.
112  */
113 void
114 gst_mini_object_init (GstMiniObject * mini_object, guint flags, GType type,
115     GstMiniObjectCopyFunction copy_func,
116     GstMiniObjectDisposeFunction dispose_func,
117     GstMiniObjectFreeFunction free_func)
118 {
119   mini_object->type = type;
120   mini_object->refcount = 1;
121   mini_object->lockstate =
122       (flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY ? GST_LOCK_FLAG_READ : 0);
123   mini_object->flags = flags;
124
125   mini_object->copy = copy_func;
126   mini_object->dispose = dispose_func;
127   mini_object->free = free_func;
128
129   mini_object->n_qdata = 0;
130   mini_object->qdata = NULL;
131
132 #ifndef GST_DISABLE_TRACE
133   _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
134 #endif
135 }
136
137 /**
138  * gst_mini_object_copy:
139  * @mini_object: the mini-object to copy
140  *
141  * Creates a copy of the mini-object.
142  *
143  * MT safe
144  *
145  * Returns: (transfer full): the new mini-object.
146  */
147 GstMiniObject *
148 gst_mini_object_copy (const GstMiniObject * mini_object)
149 {
150   GstMiniObject *copy;
151
152   g_return_val_if_fail (mini_object != NULL, NULL);
153
154   if (mini_object->copy)
155     copy = mini_object->copy (mini_object);
156   else
157     copy = NULL;
158
159   return copy;
160 }
161
162 /**
163  * gst_mini_object_lock:
164  * @object: the mini-object to lock
165  * @flags: #GstLockFlags
166  *
167  * Lock the mini-object with the specified access mode in @flags.
168  *
169  * Returns: %TRUE if @object could be locked.
170  */
171 gboolean
172 gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
173 {
174   gint access_mode, state, newstate;
175
176   g_return_val_if_fail (object != NULL, FALSE);
177   g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
178
179   access_mode = flags & FLAG_MASK;
180
181   do {
182     newstate = state = g_atomic_int_get (&object->lockstate);
183
184     GST_CAT_TRACE (GST_CAT_LOCKING, "lock %p: state %08x, access_mode %d",
185         object, state, access_mode);
186
187     if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
188       /* shared ref */
189       newstate += SHARE_ONE;
190       access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
191     }
192
193     if (access_mode) {
194       if ((state & LOCK_FLAG_MASK) == 0) {
195         /* shared counter > 1 and write access */
196         if (state > SHARE_ONE && access_mode & GST_LOCK_FLAG_WRITE)
197           goto lock_failed;
198         /* nothing mapped, set access_mode */
199         newstate |= access_mode;
200       } else {
201         /* access_mode must match */
202         if ((state & access_mode) != access_mode)
203           goto lock_failed;
204       }
205       /* increase refcount */
206       newstate += LOCK_ONE;
207     }
208   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
209           newstate));
210
211   return TRUE;
212
213 lock_failed:
214   {
215     GST_CAT_DEBUG (GST_CAT_LOCKING,
216         "lock failed %p: state %08x, access_mode %d", object, state,
217         access_mode);
218     return FALSE;
219   }
220 }
221
222 /**
223  * gst_mini_object_unlock:
224  * @object: the mini-object to unlock
225  * @flags: #GstLockFlags
226  *
227  * Unlock the mini-object with the specified access mode in @flags.
228  */
229 void
230 gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
231 {
232   gint access_mode, state, newstate;
233
234   g_return_if_fail (object != NULL);
235   g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
236
237   access_mode = flags & FLAG_MASK;
238
239   do {
240     newstate = state = g_atomic_int_get (&object->lockstate);
241
242     GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
243         object, state, access_mode);
244
245     if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
246       /* shared counter */
247       g_return_if_fail (state >= SHARE_ONE);
248       newstate -= SHARE_ONE;
249       access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
250     }
251
252     if (access_mode) {
253       g_return_if_fail ((state & access_mode) == access_mode);
254       /* decrease the refcount */
255       newstate -= LOCK_ONE;
256       /* last refcount, unset access_mode */
257       if ((newstate & LOCK_FLAG_MASK) == access_mode)
258         newstate &= ~LOCK_FLAG_MASK;
259     }
260   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
261           newstate));
262 }
263
264 /**
265  * gst_mini_object_is_writable:
266  * @mini_object: the mini-object to check
267  *
268  * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
269  * lock on @object is the only one, this means that changes to the object will
270  * not be visible to any other object.
271  *
272  * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
273  * exactly 1, meaning that no other reference exists to the object and that the
274  * object is therefore writable.
275  *
276  * Modification of a mini-object should only be done after verifying that it
277  * is writable.
278  *
279  * Returns: TRUE if the object is writable.
280  */
281 gboolean
282 gst_mini_object_is_writable (const GstMiniObject * mini_object)
283 {
284   gboolean result;
285
286   g_return_val_if_fail (mini_object != NULL, FALSE);
287
288   if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
289     result = (g_atomic_int_get (&mini_object->lockstate) & SHARE_MASK) < 2;
290   } else {
291     result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
292   }
293   return result;
294 }
295
296 /**
297  * gst_mini_object_make_writable:
298  * @mini_object: (transfer full): the mini-object to make writable
299  *
300  * Checks if a mini-object is writable.  If not, a writable copy is made and
301  * returned.  This gives away the reference to the original mini object,
302  * and returns a reference to the new object.
303  *
304  * MT safe
305  *
306  * Returns: (transfer full): a mini-object (possibly the same pointer) that
307  *     is writable.
308  */
309 GstMiniObject *
310 gst_mini_object_make_writable (GstMiniObject * mini_object)
311 {
312   GstMiniObject *ret;
313
314   g_return_val_if_fail (mini_object != NULL, NULL);
315
316   if (gst_mini_object_is_writable (mini_object)) {
317     ret = mini_object;
318   } else {
319     ret = gst_mini_object_copy (mini_object);
320     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
321         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
322     gst_mini_object_unref (mini_object);
323   }
324
325   return ret;
326 }
327
328 /**
329  * gst_mini_object_ref:
330  * @mini_object: the mini-object
331  *
332  * Increase the reference count of the mini-object.
333  *
334  * Note that the refcount affects the writeability
335  * of @mini-object, see gst_mini_object_is_writable(). It is
336  * important to note that keeping additional references to
337  * GstMiniObject instances can potentially increase the number
338  * of memcpy operations in a pipeline, especially if the miniobject
339  * is a #GstBuffer.
340  *
341  * Returns: (transfer full): the mini-object.
342  */
343 GstMiniObject *
344 gst_mini_object_ref (GstMiniObject * mini_object)
345 {
346   g_return_val_if_fail (mini_object != NULL, NULL);
347   /* we can't assert that the refcount > 0 since the _free functions
348    * increments the refcount from 0 to 1 again to allow resurecting
349    * the object
350    g_return_val_if_fail (mini_object->refcount > 0, NULL);
351    */
352
353   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
354       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
355       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
356
357   g_atomic_int_inc (&mini_object->refcount);
358
359   return mini_object;
360 }
361
362 static gint
363 find_notify (GstMiniObject * object, GQuark quark, gboolean match_notify,
364     GstMiniObjectNotify notify, gpointer data)
365 {
366   guint i;
367
368   for (i = 0; i < object->n_qdata; i++) {
369     if (QDATA_QUARK (object, i) == quark) {
370       /* check if we need to match the callback too */
371       if (!match_notify || (QDATA_NOTIFY (object, i) == notify &&
372               QDATA_DATA (object, i) == data))
373         return i;
374     }
375   }
376   return -1;
377 }
378
379 static void
380 remove_notify (GstMiniObject * object, gint index)
381 {
382   /* remove item */
383   if (--object->n_qdata == 0) {
384     /* we don't shrink but free when everything is gone */
385     g_free (object->qdata);
386     object->qdata = NULL;
387   } else if (index != object->n_qdata)
388     QDATA (object, index) = QDATA (object, object->n_qdata);
389 }
390
391 static void
392 set_notify (GstMiniObject * object, gint index, GQuark quark,
393     GstMiniObjectNotify notify, gpointer data, GDestroyNotify destroy)
394 {
395   if (index == -1) {
396     /* add item */
397     index = object->n_qdata++;
398     object->qdata =
399         g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
400   }
401   QDATA_QUARK (object, index) = quark;
402   QDATA_NOTIFY (object, index) = notify;
403   QDATA_DATA (object, index) = data;
404   QDATA_DESTROY (object, index) = destroy;
405 }
406
407 static void
408 call_finalize_notify (GstMiniObject * obj)
409 {
410   guint i;
411
412   for (i = 0; i < obj->n_qdata; i++) {
413     if (QDATA_QUARK (obj, i) == weak_ref_quark)
414       QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
415     if (QDATA_DESTROY (obj, i))
416       QDATA_DESTROY (obj, i) (QDATA_DATA (obj, i));
417   }
418 }
419
420 /**
421  * gst_mini_object_unref:
422  * @mini_object: the mini-object
423  *
424  * Decreases the reference count of the mini-object, possibly freeing
425  * the mini-object.
426  */
427 void
428 gst_mini_object_unref (GstMiniObject * mini_object)
429 {
430   g_return_if_fail (mini_object != NULL);
431
432   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
433       mini_object,
434       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
435       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
436
437   g_return_if_fail (mini_object->refcount > 0);
438
439   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
440     gboolean do_free;
441
442     if (mini_object->dispose)
443       do_free = mini_object->dispose (mini_object);
444     else
445       do_free = TRUE;
446
447     /* if the subclass recycled the object (and returned FALSE) we don't
448      * want to free the instance anymore */
449     if (G_LIKELY (do_free)) {
450       /* there should be no outstanding locks */
451       g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
452           < 4);
453
454       if (mini_object->n_qdata) {
455         call_finalize_notify (mini_object);
456         g_free (mini_object->qdata);
457       }
458 #ifndef GST_DISABLE_TRACE
459       _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
460 #endif
461       if (mini_object->free)
462         mini_object->free (mini_object);
463     }
464   }
465 }
466
467 /**
468  * gst_mini_object_replace:
469  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
470  *     be replaced
471  * @newdata: pointer to new mini-object
472  *
473  * Atomically modifies a pointer to point to a new mini-object.
474  * The reference count of @olddata is decreased and the reference count of
475  * @newdata is increased.
476  *
477  * Either @newdata and the value pointed to by @olddata may be NULL.
478  *
479  * Returns: TRUE if @newdata was different from @olddata
480  */
481 gboolean
482 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
483 {
484   GstMiniObject *olddata_val;
485
486   g_return_val_if_fail (olddata != NULL, FALSE);
487
488   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
489       *olddata, *olddata ? (*olddata)->refcount : 0,
490       newdata, newdata ? newdata->refcount : 0);
491
492   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
493
494   if (G_UNLIKELY (olddata_val == newdata))
495     return FALSE;
496
497   if (newdata)
498     gst_mini_object_ref (newdata);
499
500   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
501               olddata, olddata_val, newdata))) {
502     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
503     if (G_UNLIKELY (olddata_val == newdata))
504       break;
505   }
506
507   if (olddata_val)
508     gst_mini_object_unref (olddata_val);
509
510   return olddata_val != newdata;
511 }
512
513 /**
514  * gst_mini_object_steal:
515  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
516  *     be stolen
517  *
518  * Replace the current #GstMiniObject pointer to by @olddata with NULL and
519  * return the old value.
520  *
521  * Returns: the #GstMiniObject at @oldata
522  */
523 GstMiniObject *
524 gst_mini_object_steal (GstMiniObject ** olddata)
525 {
526   GstMiniObject *olddata_val;
527
528   g_return_val_if_fail (olddata != NULL, NULL);
529
530   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
531       *olddata, *olddata ? (*olddata)->refcount : 0);
532
533   do {
534     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
535     if (olddata_val == NULL)
536       break;
537   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
538               olddata, olddata_val, NULL)));
539
540   return olddata_val;
541 }
542
543 /**
544  * gst_mini_object_take:
545  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
546  *     be replaced
547  * @newdata: pointer to new mini-object
548  *
549  * Modifies a pointer to point to a new mini-object. The modification
550  * is done atomically. This version is similar to gst_mini_object_replace()
551  * except that it does not increase the refcount of @newdata and thus
552  * takes ownership of @newdata.
553  *
554  * Either @newdata and the value pointed to by @olddata may be NULL.
555  *
556  * Returns: TRUE if @newdata was different from @olddata
557  */
558 gboolean
559 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
560 {
561   GstMiniObject *olddata_val;
562
563   g_return_val_if_fail (olddata != NULL, FALSE);
564
565   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
566       *olddata, *olddata ? (*olddata)->refcount : 0,
567       newdata, newdata ? newdata->refcount : 0);
568
569   do {
570     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
571     if (G_UNLIKELY (olddata_val == newdata))
572       break;
573   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
574               olddata, olddata_val, newdata)));
575
576   if (olddata_val)
577     gst_mini_object_unref (olddata_val);
578
579   return olddata_val != newdata;
580 }
581
582 /**
583  * gst_mini_object_weak_ref: (skip)
584  * @object: #GstMiniObject to reference weakly
585  * @notify: callback to invoke before the mini object is freed
586  * @data: extra data to pass to notify
587  *
588  * Adds a weak reference callback to a mini object. Weak references are
589  * used for notification when a mini object is finalized. They are called
590  * "weak references" because they allow you to safely hold a pointer
591  * to the mini object without calling gst_mini_object_ref()
592  * (gst_mini_object_ref() adds a strong reference, that is, forces the object
593  * to stay alive).
594  *
595  * Since: 0.10.35
596  */
597 void
598 gst_mini_object_weak_ref (GstMiniObject * object,
599     GstMiniObjectNotify notify, gpointer data)
600 {
601   g_return_if_fail (object != NULL);
602   g_return_if_fail (notify != NULL);
603   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
604
605   G_LOCK (qdata_mutex);
606   set_notify (object, -1, weak_ref_quark, notify, data, NULL);
607   G_UNLOCK (qdata_mutex);
608 }
609
610 /**
611  * gst_mini_object_weak_unref: (skip)
612  * @object: #GstMiniObject to remove a weak reference from
613  * @notify: callback to search for
614  * @data: data to search for
615  *
616  * Removes a weak reference callback from a mini object.
617  *
618  * Since: 0.10.35
619  */
620 void
621 gst_mini_object_weak_unref (GstMiniObject * object,
622     GstMiniObjectNotify notify, gpointer data)
623 {
624   gint i;
625
626   g_return_if_fail (object != NULL);
627   g_return_if_fail (notify != NULL);
628
629   G_LOCK (qdata_mutex);
630   if ((i = find_notify (object, weak_ref_quark, TRUE, notify, data)) != -1) {
631     remove_notify (object, i);
632   } else {
633     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
634   }
635   G_UNLOCK (qdata_mutex);
636 }
637
638 /**
639  * gst_mini_object_set_qdata:
640  * @object: a #GstMiniObject
641  * @quark: A #GQuark, naming the user data pointer
642  * @data: An opaque user data pointer
643  * @destroy: Function to invoke with @data as argument, when @data
644  *           needs to be freed
645  *
646  * This sets an opaque, named pointer on a miniobject.
647  * The name is specified through a #GQuark (retrived e.g. via
648  * g_quark_from_static_string()), and the pointer
649  * can be gotten back from the @object with gst_mini_object_get_qdata()
650  * until the @object is disposed.
651  * Setting a previously set user data pointer, overrides (frees)
652  * the old pointer set, using #NULL as pointer essentially
653  * removes the data stored.
654  *
655  * @destroy may be specified which is called with @data as argument
656  * when the @object is disposed, or the data is being overwritten by
657  * a call to gst_mini_object_set_qdata() with the same @quark.
658  */
659 void
660 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
661     gpointer data, GDestroyNotify destroy)
662 {
663   gint i;
664   gpointer old_data = NULL;
665   GDestroyNotify old_notify = NULL;
666
667   g_return_if_fail (object != NULL);
668   g_return_if_fail (quark > 0);
669
670   G_LOCK (qdata_mutex);
671   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
672
673     old_data = QDATA_DATA (object, i);
674     old_notify = QDATA_DESTROY (object, i);
675
676     if (data == NULL)
677       remove_notify (object, i);
678   }
679   if (data != NULL)
680     set_notify (object, i, quark, NULL, data, destroy);
681   G_UNLOCK (qdata_mutex);
682
683   if (old_notify)
684     old_notify (old_data);
685 }
686
687 /**
688  * gst_mini_object_get_qdata:
689  * @object: The GstMiniObject to get a stored user data pointer from
690  * @quark: A #GQuark, naming the user data pointer
691  *
692  * This function gets back user data pointers stored via
693  * gst_mini_object_set_qdata().
694  *
695  * Returns: (transfer none): The user data pointer set, or %NULL
696  */
697 gpointer
698 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
699 {
700   guint i;
701   gpointer result;
702
703   g_return_val_if_fail (object != NULL, NULL);
704   g_return_val_if_fail (quark > 0, NULL);
705
706   G_LOCK (qdata_mutex);
707   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1)
708     result = QDATA_DATA (object, i);
709   else
710     result = NULL;
711   G_UNLOCK (qdata_mutex);
712
713   return result;
714 }
715
716 /**
717  * gst_mini_object_steal_qdata:
718  * @object: The GstMiniObject to get a stored user data pointer from
719  * @quark: A #GQuark, naming the user data pointer
720  *
721  * This function gets back user data pointers stored via gst_mini_object_set_qdata()
722  * and removes the data from @object without invoking its destroy() function (if
723  * any was set).
724  *
725  * Returns: (transfer full): The user data pointer set, or %NULL
726  */
727 gpointer
728 gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
729 {
730   guint i;
731   gpointer result;
732
733   g_return_val_if_fail (object != NULL, NULL);
734   g_return_val_if_fail (quark > 0, NULL);
735
736   G_LOCK (qdata_mutex);
737   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
738     result = QDATA_DATA (object, i);
739     remove_notify (object, i);
740   } else {
741     result = NULL;
742   }
743   G_UNLOCK (qdata_mutex);
744
745   return result;
746 }