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