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