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