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