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