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