miniobject: add lock functionality to GstMiniObject
[platform/upstream/gstreamer.git] / gst / gstminiobject.c
1 /* GStreamer
2  * Copyright (C) 2005 David Schleef <ds@schleef.org>
3  *
4  * gstminiobject.h: Header for GstMiniObject
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 /**
22  * SECTION:gstminiobject
23  * @short_description: Lightweight base class for the GStreamer object hierarchy
24  *
25  * #GstMiniObject is a simple structure that can be used to implement refcounted
26  * types.
27  *
28  * Subclasses will include #GstMiniObject as the first member in their structure
29  * and then call gst_mini_object_init() to initialize the #GstMiniObject fields.
30  *
31  * gst_mini_object_ref() and gst_mini_object_unref() increment and decrement the
32  * refcount respectively. When the refcount of a mini-object reaches 0, the
33  * dispose function is called first and when this returns %TRUE, the free
34  * function of the miniobject is called.
35  *
36  * A copy can be made with gst_mini_object_copy().
37  *
38  * gst_mini_object_is_writable() will return %TRUE when the refcount of the
39  * object is exactly 1, meaning the current caller has the only reference to the
40  * object. gst_mini_object_make_writable() will return a writable version of the
41  * object, which might be a new copy when the refcount was not 1.
42  *
43  * Opaque data can be associated with a #GstMiniObject with
44  * gst_mini_object_set_qdata() and gst_mini_object_get_qdata(). The data is
45  * meant to be specific to the particular object and is not automatically copied
46  * with gst_mini_object_copy() or similar methods.
47  *
48  * A weak reference can be added and remove with gst_mini_object_weak_ref()
49  * and gst_mini_object_weak_unref() respectively.
50  *
51  * Last reviewed on 2012-06-15 (0.11.93)
52  */
53 #ifdef HAVE_CONFIG_H
54 #include "config.h"
55 #endif
56
57 #include "gst/gst_private.h"
58 #include "gst/gstminiobject.h"
59 #include "gst/gstinfo.h"
60 #include <gobject/gvaluecollector.h>
61
62 #ifndef GST_DISABLE_TRACE
63 #include "gsttrace.h"
64 static GstAllocTrace *_gst_mini_object_trace;
65 #endif
66
67 /* Mutex used for weak referencing */
68 G_LOCK_DEFINE_STATIC (qdata_mutex);
69 static GQuark weak_ref_quark;
70
71 #define SHARE_ONE (1 << 16)
72 #define SHARE_MASK (~(SHARE_ONE - 1))
73 #define LOCK_ONE (GST_LOCK_FLAG_LAST)
74 #define FLAG_MASK (GST_LOCK_FLAG_LAST - 1)
75 #define LOCK_MASK ((SHARE_ONE - 1) - FLAG_MASK)
76 #define LOCK_FLAG_MASK (SHARE_ONE - 1)
77
78 typedef struct
79 {
80   GQuark quark;
81   GstMiniObjectNotify notify;
82   gpointer data;
83   GDestroyNotify destroy;
84 } GstQData;
85
86 #define QDATA(o,i)          ((GstQData *)(o)->qdata)[(i)]
87 #define QDATA_QUARK(o,i)    (QDATA(o,i).quark)
88 #define QDATA_NOTIFY(o,i)   (QDATA(o,i).notify)
89 #define QDATA_DATA(o,i)     (QDATA(o,i).data)
90 #define QDATA_DESTROY(o,i)  (QDATA(o,i).destroy)
91
92 void
93 _priv_gst_mini_object_initialize (void)
94 {
95   weak_ref_quark = g_quark_from_static_string ("GstMiniObjectWeakRefQuark");
96
97 #ifndef GST_DISABLE_TRACE
98   _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
99 #endif
100 }
101
102 /**
103  * gst_mini_object_init: (skip)
104  * @mini_object: a #GstMiniObject
105  * @type: the #GType of the mini-object to create
106  * @copy_func: the copy function, or NULL
107  * @dispose_func: the dispose function, or NULL
108  * @free_func: the free function or NULL
109  *
110  * Initializes a mini-object with the desired type and copy/dispose/free
111  * functions.
112  *
113  * MT safe
114  *
115  * Returns: (transfer full): the new mini-object.
116  */
117 void
118 gst_mini_object_init (GstMiniObject * mini_object, guint flags, GType type,
119     GstMiniObjectCopyFunction copy_func,
120     GstMiniObjectDisposeFunction dispose_func,
121     GstMiniObjectFreeFunction free_func)
122 {
123   mini_object->type = type;
124   mini_object->refcount = 1;
125   mini_object->lockstate =
126       (flags & GST_MINI_OBJECT_FLAG_LOCK_READONLY ? GST_LOCK_FLAG_READ : 0);
127   mini_object->flags = flags;
128
129   mini_object->copy = copy_func;
130   mini_object->dispose = dispose_func;
131   mini_object->free = free_func;
132
133   mini_object->n_qdata = 0;
134   mini_object->qdata = NULL;
135
136 #ifndef GST_DISABLE_TRACE
137   _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
138 #endif
139 }
140
141 /**
142  * gst_mini_object_copy:
143  * @mini_object: the mini-object to copy
144  *
145  * Creates a copy of the mini-object.
146  *
147  * MT safe
148  *
149  * Returns: (transfer full): the new mini-object.
150  */
151 GstMiniObject *
152 gst_mini_object_copy (const GstMiniObject * mini_object)
153 {
154   GstMiniObject *copy;
155
156   g_return_val_if_fail (mini_object != NULL, NULL);
157
158   if (mini_object->copy)
159     copy = mini_object->copy (mini_object);
160   else
161     copy = NULL;
162
163   return copy;
164 }
165
166 /**
167  * gst_mini_object_lock:
168  * @object: the mini-object to lock
169  * @flags: #GstLockFlags
170  *
171  * Lock the mini-object with the specified access mode in @flags.
172  *
173  * Returns: %TRUE if @object could be locked.
174  */
175 gboolean
176 gst_mini_object_lock (GstMiniObject * object, GstLockFlags flags)
177 {
178   gint access_mode, state, newstate;
179
180   g_return_val_if_fail (object != NULL, FALSE);
181   g_return_val_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object), FALSE);
182
183   access_mode = flags & FLAG_MASK;
184
185   do {
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     if (access_mode) {
198       if ((state & LOCK_FLAG_MASK) == 0) {
199         /* shared counter > 1 and write access */
200         if (state > SHARE_ONE && access_mode & GST_LOCK_FLAG_WRITE)
201           goto lock_failed;
202         /* nothing mapped, set access_mode */
203         newstate |= access_mode;
204       } else {
205         /* access_mode must match */
206         if ((state & access_mode) != access_mode)
207           goto lock_failed;
208       }
209       /* increase refcount */
210       newstate += LOCK_ONE;
211     }
212   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
213           newstate));
214
215   return TRUE;
216
217 lock_failed:
218   {
219     GST_CAT_DEBUG (GST_CAT_LOCKING,
220         "lock failed %p: state %08x, access_mode %d", object, state,
221         access_mode);
222     return FALSE;
223   }
224 }
225
226 /**
227  * gst_mini_object_unlock:
228  * @object: the mini-object to unlock
229  * @flags: #GstLockFlags
230  *
231  * Unlock the mini-object with the specified access mode in @flags.
232  */
233 void
234 gst_mini_object_unlock (GstMiniObject * object, GstLockFlags flags)
235 {
236   gint access_mode, state, newstate;
237
238   g_return_if_fail (object != NULL);
239   g_return_if_fail (GST_MINI_OBJECT_IS_LOCKABLE (object));
240
241   access_mode = flags & FLAG_MASK;
242
243   do {
244     newstate = state = g_atomic_int_get (&object->lockstate);
245
246     GST_CAT_TRACE (GST_CAT_LOCKING, "unlock %p: state %08x, access_mode %d",
247         object, state, access_mode);
248
249     if (access_mode & GST_LOCK_FLAG_EXCLUSIVE) {
250       /* shared counter */
251       g_return_if_fail (state >= SHARE_ONE);
252       newstate -= SHARE_ONE;
253       access_mode &= ~GST_LOCK_FLAG_EXCLUSIVE;
254     }
255
256     if (access_mode) {
257       g_return_if_fail ((state & access_mode) == access_mode);
258       /* decrease the refcount */
259       newstate -= LOCK_ONE;
260       /* last refcount, unset access_mode */
261       if ((newstate & LOCK_FLAG_MASK) == access_mode)
262         newstate &= ~LOCK_FLAG_MASK;
263     }
264   } while (!g_atomic_int_compare_and_exchange (&object->lockstate, state,
265           newstate));
266 }
267
268 /**
269  * gst_mini_object_is_writable:
270  * @mini_object: the mini-object to check
271  *
272  * If @mini_object has the LOCKABLE flag set, check if the current EXCLUSIVE
273  * lock on @object is the only one, this means that changes to the object will
274  * not be visible to any other object.
275  *
276  * If the LOCKABLE flag is not set, check if the refcount of @mini_object is
277  * exactly 1, meaning that no other reference exists to the object and that the
278  * object is therefore writable.
279  *
280  * Modification of a mini-object should only be done after verifying that it
281  * is writable.
282  *
283  * Returns: TRUE if the object is writable.
284  */
285 gboolean
286 gst_mini_object_is_writable (const GstMiniObject * mini_object)
287 {
288   gboolean result;
289
290   g_return_val_if_fail (mini_object != NULL, FALSE);
291
292   if (GST_MINI_OBJECT_IS_LOCKABLE (mini_object)) {
293     result = (g_atomic_int_get (&mini_object->lockstate) & SHARE_MASK) < 2;
294   } else {
295     result = (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
296   }
297   return result;
298 }
299
300 /**
301  * gst_mini_object_make_writable:
302  * @mini_object: (transfer full): the mini-object to make writable
303  *
304  * Checks if a mini-object is writable.  If not, a writable copy is made and
305  * returned.  This gives away the reference to the original mini object,
306  * and returns a reference to the new object.
307  *
308  * MT safe
309  *
310  * Returns: (transfer full): a mini-object (possibly the same pointer) that
311  *     is writable.
312  */
313 GstMiniObject *
314 gst_mini_object_make_writable (GstMiniObject * mini_object)
315 {
316   GstMiniObject *ret;
317
318   g_return_val_if_fail (mini_object != NULL, NULL);
319
320   if (gst_mini_object_is_writable (mini_object)) {
321     ret = mini_object;
322   } else {
323     ret = gst_mini_object_copy (mini_object);
324     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
325         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
326     gst_mini_object_unref (mini_object);
327   }
328
329   return ret;
330 }
331
332 /**
333  * gst_mini_object_ref:
334  * @mini_object: the mini-object
335  *
336  * Increase the reference count of the mini-object.
337  *
338  * Note that the refcount affects the writeability
339  * of @mini-object, see gst_mini_object_is_writable(). It is
340  * important to note that keeping additional references to
341  * GstMiniObject instances can potentially increase the number
342  * of memcpy operations in a pipeline, especially if the miniobject
343  * is a #GstBuffer.
344  *
345  * Returns: (transfer full): the mini-object.
346  */
347 GstMiniObject *
348 gst_mini_object_ref (GstMiniObject * mini_object)
349 {
350   g_return_val_if_fail (mini_object != NULL, NULL);
351   /* we can't assert that the refcount > 0 since the _free functions
352    * increments the refcount from 0 to 1 again to allow resurecting
353    * the object
354    g_return_val_if_fail (mini_object->refcount > 0, NULL);
355    */
356
357   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
358       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
359       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
360
361   g_atomic_int_inc (&mini_object->refcount);
362
363   return mini_object;
364 }
365
366 static gint
367 find_notify (GstMiniObject * object, GQuark quark, gboolean match_notify,
368     GstMiniObjectNotify notify, gpointer data)
369 {
370   guint i;
371
372   for (i = 0; i < object->n_qdata; i++) {
373     if (QDATA_QUARK (object, i) == quark) {
374       /* check if we need to match the callback too */
375       if (!match_notify || (QDATA_NOTIFY (object, i) == notify &&
376               QDATA_DATA (object, i) == data))
377         return i;
378     }
379   }
380   return -1;
381 }
382
383 static void
384 remove_notify (GstMiniObject * object, gint index)
385 {
386   /* remove item */
387   if (--object->n_qdata == 0) {
388     /* we don't shrink but free when everything is gone */
389     g_free (object->qdata);
390     object->qdata = NULL;
391   } else if (index != object->n_qdata)
392     QDATA (object, index) = QDATA (object, object->n_qdata);
393 }
394
395 static void
396 set_notify (GstMiniObject * object, gint index, GQuark quark,
397     GstMiniObjectNotify notify, gpointer data, GDestroyNotify destroy)
398 {
399   if (index == -1) {
400     /* add item */
401     index = object->n_qdata++;
402     object->qdata =
403         g_realloc (object->qdata, sizeof (GstQData) * object->n_qdata);
404   }
405   QDATA_QUARK (object, index) = quark;
406   QDATA_NOTIFY (object, index) = notify;
407   QDATA_DATA (object, index) = data;
408   QDATA_DESTROY (object, index) = destroy;
409 }
410
411 static void
412 call_finalize_notify (GstMiniObject * obj)
413 {
414   guint i;
415
416   for (i = 0; i < obj->n_qdata; i++) {
417     if (QDATA_QUARK (obj, i) == weak_ref_quark)
418       QDATA_NOTIFY (obj, i) (QDATA_DATA (obj, i), obj);
419     if (QDATA_DESTROY (obj, i))
420       QDATA_DESTROY (obj, i) (QDATA_DATA (obj, i));
421   }
422 }
423
424 /**
425  * gst_mini_object_unref:
426  * @mini_object: the mini-object
427  *
428  * Decreases the reference count of the mini-object, possibly freeing
429  * the mini-object.
430  */
431 void
432 gst_mini_object_unref (GstMiniObject * mini_object)
433 {
434   g_return_if_fail (mini_object != NULL);
435
436   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
437       mini_object,
438       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
439       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
440
441   g_return_if_fail (mini_object->refcount > 0);
442
443   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
444     gboolean do_free;
445
446     if (mini_object->dispose)
447       do_free = mini_object->dispose (mini_object);
448     else
449       do_free = TRUE;
450
451     /* if the subclass recycled the object (and returned FALSE) we don't
452      * want to free the instance anymore */
453     if (G_LIKELY (do_free)) {
454       /* there should be no outstanding locks */
455       g_return_if_fail ((g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK)
456           < 4);
457
458       if (mini_object->n_qdata) {
459         call_finalize_notify (mini_object);
460         g_free (mini_object->qdata);
461       }
462 #ifndef GST_DISABLE_TRACE
463       _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
464 #endif
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): pointer to a pointer to a mini-object to
474  *     be replaced
475  * @newdata: 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:
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: 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  * Since: 0.10.35
600  */
601 void
602 gst_mini_object_weak_ref (GstMiniObject * object,
603     GstMiniObjectNotify notify, gpointer data)
604 {
605   g_return_if_fail (object != NULL);
606   g_return_if_fail (notify != NULL);
607   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
608
609   G_LOCK (qdata_mutex);
610   set_notify (object, -1, weak_ref_quark, notify, data, NULL);
611   G_UNLOCK (qdata_mutex);
612 }
613
614 /**
615  * gst_mini_object_weak_unref: (skip)
616  * @object: #GstMiniObject to remove a weak reference from
617  * @notify: callback to search for
618  * @data: data to search for
619  *
620  * Removes a weak reference callback from a mini object.
621  *
622  * Since: 0.10.35
623  */
624 void
625 gst_mini_object_weak_unref (GstMiniObject * object,
626     GstMiniObjectNotify notify, gpointer data)
627 {
628   gint i;
629
630   g_return_if_fail (object != NULL);
631   g_return_if_fail (notify != NULL);
632
633   G_LOCK (qdata_mutex);
634   if ((i = find_notify (object, weak_ref_quark, TRUE, notify, data)) != -1) {
635     remove_notify (object, i);
636   } else {
637     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, 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 (retrived 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): The user data pointer set, or %NULL
700  */
701 gpointer
702 gst_mini_object_get_qdata (GstMiniObject * object, GQuark quark)
703 {
704   guint i;
705   gpointer result;
706
707   g_return_val_if_fail (object != NULL, NULL);
708   g_return_val_if_fail (quark > 0, NULL);
709
710   G_LOCK (qdata_mutex);
711   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1)
712     result = QDATA_DATA (object, i);
713   else
714     result = NULL;
715   G_UNLOCK (qdata_mutex);
716
717   return result;
718 }
719
720 /**
721  * gst_mini_object_steal_qdata:
722  * @object: The GstMiniObject to get a stored user data pointer from
723  * @quark: A #GQuark, naming the user data pointer
724  *
725  * This function gets back user data pointers stored via gst_mini_object_set_qdata()
726  * and removes the data from @object without invoking its destroy() function (if
727  * any was set).
728  *
729  * Returns: (transfer full): The user data pointer set, or %NULL
730  */
731 gpointer
732 gst_mini_object_steal_qdata (GstMiniObject * object, GQuark quark)
733 {
734   guint i;
735   gpointer result;
736
737   g_return_val_if_fail (object != NULL, NULL);
738   g_return_val_if_fail (quark > 0, NULL);
739
740   G_LOCK (qdata_mutex);
741   if ((i = find_notify (object, quark, FALSE, NULL, NULL)) != -1) {
742     result = QDATA_DATA (object, i);
743     remove_notify (object, i);
744   } else {
745     result = NULL;
746   }
747   G_UNLOCK (qdata_mutex);
748
749   return result;
750 }