gst: Fix up a bunch of GIR annotations
[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) (nullable): the new mini-object if copying is
136  * possible, %NULL otherwise.
137  */
138 GstMiniObject *
139 gst_mini_object_copy (const GstMiniObject * mini_object)
140 {
141   GstMiniObject *copy;
142
143   g_return_val_if_fail (mini_object != NULL, NULL);
144
145   if (mini_object->copy)
146     copy = mini_object->copy (mini_object);
147   else
148     copy = NULL;
149
150   return copy;
151 }
152
153 /**
154  * gst_mini_object_lock:
155  * @object: the mini-object to lock
156  * @flags: #GstLockFlags
157  *
158  * Lock the mini-object with the specified access mode in @flags.
159  *
160  * Returns: %TRUE if @object could be locked.
161  */
162 gboolean
163 gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
164 {
165   gint access_mode, state, newstate;
166
167   g_return_val_if_fail (object != NULL, FALSE);
168   g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
169
170   if (G_UNLIKELY (object->flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY &&
171           flags & GST_LOCK_FLAG_WRITE))
172     return FALSE;
173
174   do {
175     access_mode = flags & FLAG_MASK;
176     newstate = state = g_atomic_int_get (&object->lockstate);
177
178     GST_CAT_TRACE (GST_CAT_LOCKING, "lock %p: state %08x, access_mode %d",
179         object, state, access_mode);
180
181     if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
182       /* shared ref */
183       newstate += SHARE_ONE;
184       access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
185     }
186
187     /* shared counter > 1 and write access is not allowed */
188     if (((state & GST_LOCK_FLAG_WRITE) != 0
189             || (access_mode & GST_LOCK_FLAG_WRITE) != 0)
190         && IS_SHARED (newstate))
191       goto lock_failed;
192
193     if (access_mode) {
194       if ((state & LOCK_FLAG_MASK) == 0) {
195         /* nothing mapped, set access_mode */
196         newstate |= access_mode;
197       } else {
198         /* access_mode must match */
199         if ((state & access_mode) != access_mode)
200           goto lock_failed;
201       }
202       /* increase refcount */
203       newstate += LOCK_ONE;
204     }
205   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
206           newstate));
207
208   return TRUE;
209
210 lock_failed:
211   {
212     GST_CAT_DEBUG (GST_CAT_LOCKING,
213         "lock failed %p: state %08x, access_mode %d", object, state,
214         access_mode);
215     return FALSE;
216   }
217 }
218
219 /**
220  * gst_mini_object_unlock:
221  * @object: the mini-object to unlock
222  * @flags: #GstLockFlags
223  *
224  * Unlock the mini-object with the specified access mode in @flags.
225  */
226 void
227 gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
228 {
229   gint access_mode, state, newstate;
230
231   g_return_if_fail (object != NULL);
232   g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
233
234   do {
235     access_mode = flags & FLAG_MASK;
236     newstate = state = g_atomic_int_get (&object->lockstate);
237
238     GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
239         object, state, access_mode);
240
241     if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
242       /* shared counter */
243       g_return_if_fail (state >= SHARE_ONE);
244       newstate -= SHARE_ONE;
245       access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
246     }
247
248     if (access_mode) {
249       g_return_if_fail ((state & access_mode) == access_mode);
250       /* decrease the refcount */
251       newstate -= LOCK_ONE;
252       /* last refcount, unset access_mode */
253       if ((newstate & LOCK_FLAG_MASK) == access_mode)
254         newstate &= ~LOCK_FLAG_MASK;
255     }
256   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
257           newstate));
258 }
259
260 /**
261  * gst_mini_object_is_writable:
262  * @mini_object: the mini-object to check
263  *
264  * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
265  * lock on @object is the only one, this means that changes to the object will
266  * not be visible to any other object.
267  *
268  * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
269  * exactly 1, meaning that no other reference exists to the object and that the
270  * object is therefore writable.
271  *
272  * Modification of a mini-object should only be done after verifying that it
273  * is writable.
274  *
275  * Returns: %TRUE if the object is writable.
276  */
277 gboolean
278 gst_mini_object_is_writable (const GstMiniObject * mini_object)
279 {
280   gboolean result;
281
282   g_return_val_if_fail (mini_object != NULL, FALSE);
283
284   if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
285     result = !IS_SHARED (g_atomic_int_get (&mini_object->lockstate));
286   } else {
287     result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
288   }
289   return result;
290 }
291
292 /**
293  * gst_mini_object_make_writable: (skip)
294  * @mini_object: (transfer full): the mini-object to make writable
295  *
296  * Checks if a mini-object is writable.  If not, a writable copy is made and
297  * returned.  This gives away the reference to the original mini object,
298  * and returns a reference to the new object.
299  *
300  * MT safe
301  *
302  * Returns: (transfer full): a mini-object (possibly the same pointer) that
303  *     is writable.
304  */
305 GstMiniObject *
306 gst_mini_object_make_writable (GstMiniObject * mini_object)
307 {
308   GstMiniObject *ret;
309
310   g_return_val_if_fail (mini_object != NULL, NULL);
311
312   if (gst_mini_object_is_writable (mini_object)) {
313     ret = mini_object;
314   } else {
315     ret = gst_mini_object_copy (mini_object);
316     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
317         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
318     gst_mini_object_unref (mini_object);
319   }
320
321   return ret;
322 }
323
324 /**
325  * gst_mini_object_ref: (skip)
326  * @mini_object: the mini-object
327  *
328  * Increase the reference count of the mini-object.
329  *
330  * Note that the refcount affects the writability
331  * of @mini-object, see gst_mini_object_is_writable(). It is
332  * important to note that keeping additional references to
333  * GstMiniObject instances can potentially increase the number
334  * of memcpy operations in a pipeline, especially if the miniobject
335  * is a #GstBuffer.
336  *
337  * Returns: (transfer full): the mini-object.
338  */
339 GstMiniObject *
340 gst_mini_object_ref (GstMiniObject * mini_object)
341 {
342   gint old_refcount, new_refcount;
343
344   g_return_val_if_fail (mini_object != NULL, NULL);
345   /* we can't assert that the refcount > 0 since the _free functions
346    * increments the refcount from 0 to 1 again to allow resurecting
347    * the object
348    g_return_val_if_fail (mini_object->refcount > 0, NULL);
349    */
350
351   old_refcount = g_atomic_int_add (&mini_object->refcount, 1);
352   new_refcount = old_refcount + 1;
353
354   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
355       old_refcount, new_refcount);
356
357   GST_TRACER_MINI_OBJECT_REFFED (mini_object, new_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: (skip)
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   gint old_refcount, new_refcount;
431
432   g_return_if_fail (mini_object != NULL);
433   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) > 0);
434
435   old_refcount = g_atomic_int_add (&mini_object->refcount, -1);
436   new_refcount = old_refcount - 1;
437
438   g_return_if_fail (old_refcount > 0);
439
440   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
441       mini_object, old_refcount, new_refcount);
442
443   GST_TRACER_MINI_OBJECT_UNREFFED (mini_object, new_refcount);
444
445   if (new_refcount == 0) {
446     gboolean do_free;
447
448     if (mini_object->dispose)
449       do_free = mini_object->dispose (mini_object);
450     else
451       do_free = TRUE;
452
453     /* if the subclass recycled the object (and returned FALSE) we don't
454      * want to free the instance anymore */
455     if (G_LIKELY (do_free)) {
456       /* there should be no outstanding locks */
457       g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
458           < 4);
459
460       if (mini_object->n_qdata) {
461         call_finalize_notify (mini_object);
462         g_free (mini_object->qdata);
463       }
464       GST_TRACER_MINI_OBJECT_DESTROYED (mini_object);
465       if (mini_object->free)
466         mini_object->free (mini_object);
467     }
468   }
469 }
470
471 /**
472  * gst_mini_object_replace:
473  * @olddata: (inout) (transfer full) (nullable): pointer to a pointer to a
474  *     mini-object to be replaced
475  * @newdata: (allow-none): pointer to new mini-object
476  *
477  * Atomically modifies a pointer to point to a new mini-object.
478  * The reference count of @olddata is decreased and the reference count of
479  * @newdata is increased.
480  *
481  * Either @newdata and the value pointed to by @olddata may be %NULL.
482  *
483  * Returns: %TRUE if @newdata was different from @olddata
484  */
485 gboolean
486 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
487 {
488   GstMiniObject *olddata_val;
489
490   g_return_val_if_fail (olddata != NULL, FALSE);
491
492   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
493       *olddata, *olddata ? (*olddata)->refcount : 0,
494       newdata, newdata ? newdata->refcount : 0);
495
496   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
497
498   if (G_UNLIKELY (olddata_val == newdata))
499     return FALSE;
500
501   if (newdata)
502     gst_mini_object_ref (newdata);
503
504   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
505               olddata, olddata_val, newdata))) {
506     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
507     if (G_UNLIKELY (olddata_val == newdata))
508       break;
509   }
510
511   if (olddata_val)
512     gst_mini_object_unref (olddata_val);
513
514   return olddata_val != newdata;
515 }
516
517 /**
518  * gst_mini_object_steal: (skip)
519  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
520  *     be stolen
521  *
522  * Replace the current #GstMiniObject pointer to by @olddata with %NULL and
523  * return the old value.
524  *
525  * Returns: (nullable): the #GstMiniObject at @oldata
526  */
527 GstMiniObject *
528 gst_mini_object_steal (GstMiniObject ** olddata)
529 {
530   GstMiniObject *olddata_val;
531
532   g_return_val_if_fail (olddata != NULL, NULL);
533
534   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
535       *olddata, *olddata ? (*olddata)->refcount : 0);
536
537   do {
538     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
539     if (olddata_val == NULL)
540       break;
541   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
542               olddata, olddata_val, NULL)));
543
544   return olddata_val;
545 }
546
547 /**
548  * gst_mini_object_take:
549  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
550  *     be replaced
551  * @newdata: pointer to new mini-object
552  *
553  * Modifies a pointer to point to a new mini-object. The modification
554  * is done atomically. This version is similar to gst_mini_object_replace()
555  * except that it does not increase the refcount of @newdata and thus
556  * takes ownership of @newdata.
557  *
558  * Either @newdata and the value pointed to by @olddata may be %NULL.
559  *
560  * Returns: %TRUE if @newdata was different from @olddata
561  */
562 gboolean
563 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
564 {
565   GstMiniObject *olddata_val;
566
567   g_return_val_if_fail (olddata != NULL, FALSE);
568
569   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
570       *olddata, *olddata ? (*olddata)->refcount : 0,
571       newdata, newdata ? newdata->refcount : 0);
572
573   do {
574     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
575     if (G_UNLIKELY (olddata_val == newdata))
576       break;
577   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
578               olddata, olddata_val, newdata)));
579
580   if (olddata_val)
581     gst_mini_object_unref (olddata_val);
582
583   return olddata_val != newdata;
584 }
585
586 /**
587  * gst_mini_object_weak_ref: (skip)
588  * @object: #GstMiniObject to reference weakly
589  * @notify: callback to invoke before the mini object is freed
590  * @data: extra data to pass to notify
591  *
592  * Adds a weak reference callback to a mini object. Weak references are
593  * used for notification when a mini object is finalized. They are called
594  * "weak references" because they allow you to safely hold a pointer
595  * to the mini object without calling gst_mini_object_ref()
596  * (gst_mini_object_ref() adds a strong reference, that is, forces the object
597  * to stay alive).
598  */
599 void
600 gst_mini_object_weak_ref (GstMiniObject * object,
601     GstMiniObjectNotify notify, gpointer data)
602 {
603   g_return_if_fail (object != NULL);
604   g_return_if_fail (notify != NULL);
605   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
606
607   G_LOCK (qdata_mutex);
608   set_notify (object, -1, weak_ref_quark, notify, data, NULL);
609   G_UNLOCK (qdata_mutex);
610 }
611
612 /**
613  * gst_mini_object_weak_unref: (skip)
614  * @object: #GstMiniObject to remove a weak reference from
615  * @notify: callback to search for
616  * @data: data to search for
617  *
618  * Removes a weak reference callback from a mini object.
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 (object:%p data:%p)", G_STRFUNC,
634         notify, object, data);
635   }
636   G_UNLOCK (qdata_mutex);
637 }
638
639 /**
640  * gst_mini_object_set_qdata:
641  * @object: a #GstMiniObject
642  * @quark: A #GQuark, naming the user data pointer
643  * @data: An opaque user data pointer
644  * @destroy: Function to invoke with @data as argument, when @data
645  *           needs to be freed
646  *
647  * This sets an opaque, named pointer on a miniobject.
648  * The name is specified through a #GQuark (retrieved e.g. via
649  * g_quark_from_static_string()), and the pointer
650  * can be gotten back from the @object with gst_mini_object_get_qdata()
651  * until the @object is disposed.
652  * Setting a previously set user data pointer, overrides (frees)
653  * the old pointer set, using %NULL as pointer essentially
654  * removes the data stored.
655  *
656  * @destroy may be specified which is called with @data as argument
657  * when the @object is disposed, or the data is being overwritten by
658  * a call to gst_mini_object_set_qdata() with the same @quark.
659  */
660 void
661 gst_mini_object_set_qdata (GstMiniObject * object, GQuark quark,
662     gpointer data, GDestroyNotify destroy)
663 {
664   gint i;
665   gpointer old_data = NULL;
666   GDestroyNotify old_notify = NULL;
667
668   g_return_if_fail (object != NULL);
669   g_return_if_fail (quark > 0);
670
671   G_LOCK (qdata_mutex);
672   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
673
674     old_data = QDATA_DATA (object, i);
675     old_notify = QDATA_DESTROY (object, i);
676
677     if (data == NULL)
678       remove_notify (object, i);
679   }
680   if (data != NULL)
681     set_notify (object, i, quark, NULL, data, destroy);
682   G_UNLOCK (qdata_mutex);
683
684   if (old_notify)
685     old_notify (old_data);
686 }
687
688 /**
689  * gst_mini_object_get_qdata:
690  * @object: The GstMiniObject to get a stored user data pointer from
691  * @quark: A #GQuark, naming the user data pointer
692  *
693  * This function gets back user data pointers stored via
694  * gst_mini_object_set_qdata().
695  *
696  * Returns: (transfer none) (nullable): The user data pointer set, or
697  * %NULL
698  */
699 gpointer
700 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
701 {
702   guint i;
703   gpointer result;
704
705   g_return_val_if_fail (object != NULL, NULL);
706   g_return_val_if_fail (quark > 0, NULL);
707
708   G_LOCK (qdata_mutex);
709   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1)
710     result = QDATA_DATA (object, i);
711   else
712     result = NULL;
713   G_UNLOCK (qdata_mutex);
714
715   return result;
716 }
717
718 /**
719  * gst_mini_object_steal_qdata:
720  * @object: The GstMiniObject to get a stored user data pointer from
721  * @quark: A #GQuark, naming the user data pointer
722  *
723  * This function gets back user data pointers stored via gst_mini_object_set_qdata()
724  * and removes the data from @object without invoking its destroy() function (if
725  * any was set).
726  *
727  * Returns: (transfer full) (nullable): The user data pointer set, or
728  * %NULL
729  */
730 gpointer
731 gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
732 {
733   guint i;
734   gpointer result;
735
736   g_return_val_if_fail (object != NULL, NULL);
737   g_return_val_if_fail (quark > 0, NULL);
738
739   G_LOCK (qdata_mutex);
740   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
741     result = QDATA_DATA (object, i);
742     remove_notify (object, i);
743   } else {
744     result = NULL;
745   }
746   G_UNLOCK (qdata_mutex);
747
748   return result;
749 }