miniobject: remove the size field
[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  * Last reviewed on 2012-03-28 (0.11.3)
44  */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "gst/gst_private.h"
50 #include "gst/gstminiobject.h"
51 #include "gst/gstinfo.h"
52 #include <gobject/gvaluecollector.h>
53
54 #ifndef GST_DISABLE_TRACE
55 #include "gsttrace.h"
56 static GstAllocTrace *_gst_mini_object_trace;
57 #endif
58
59 /* Mutex used for weak referencing */
60 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
61
62 void
63 _priv_gst_mini_object_initialize (void)
64 {
65 #ifndef GST_DISABLE_TRACE
66   _gst_mini_object_trace = _gst_alloc_trace_register ("GstMiniObject", 0);
67 #endif
68 }
69
70 /**
71  * gst_mini_object_init:
72  * @mini_object: a #GstMiniObject 
73  * @type: the #GType of the mini-object to create
74  * @size: the size of the data
75  *
76  * Initializes a mini-object with the desired type and size.
77  *
78  * MT safe
79  *
80  * Returns: (transfer full): the new mini-object.
81  */
82 void
83 gst_mini_object_init (GstMiniObject * mini_object, GType type)
84 {
85   mini_object->type = type;
86   mini_object->refcount = 1;
87   mini_object->flags = 0;
88   mini_object->n_weak_refs = 0;
89   mini_object->weak_refs = NULL;
90
91 #ifndef GST_DISABLE_TRACE
92   _gst_alloc_trace_new (_gst_mini_object_trace, mini_object);
93 #endif
94 }
95
96 /**
97  * gst_mini_object_copy:
98  * @mini_object: the mini-object to copy
99  *
100  * Creates a copy of the mini-object.
101  *
102  * MT safe
103  *
104  * Returns: (transfer full): the new mini-object.
105  */
106 GstMiniObject *
107 gst_mini_object_copy (const GstMiniObject * mini_object)
108 {
109   GstMiniObject *copy;
110
111   g_return_val_if_fail (mini_object != NULL, NULL);
112
113   if (mini_object->copy)
114     copy = mini_object->copy (mini_object);
115   else
116     copy = NULL;
117
118   return copy;
119 }
120
121 /**
122  * gst_mini_object_is_writable:
123  * @mini_object: the mini-object to check
124  *
125  * Checks if a mini-object is writable.  A mini-object is writable
126  * if the reference count is one. Modification of a mini-object should
127  * only be done after verifying that it is writable.
128  *
129  * MT safe
130  *
131  * Returns: TRUE if the object is writable.
132  */
133 gboolean
134 gst_mini_object_is_writable (const GstMiniObject * mini_object)
135 {
136   g_return_val_if_fail (mini_object != NULL, FALSE);
137
138   return (GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) == 1);
139 }
140
141 /**
142  * gst_mini_object_make_writable:
143  * @mini_object: (transfer full): the mini-object to make writable
144  *
145  * Checks if a mini-object is writable.  If not, a writable copy is made and
146  * returned.  This gives away the reference to the original mini object,
147  * and returns a reference to the new object.
148  *
149  * MT safe
150  *
151  * Returns: (transfer full): a mini-object (possibly the same pointer) that
152  *     is writable.
153  */
154 GstMiniObject *
155 gst_mini_object_make_writable (GstMiniObject * mini_object)
156 {
157   GstMiniObject *ret;
158
159   g_return_val_if_fail (mini_object != NULL, NULL);
160
161   if (gst_mini_object_is_writable (mini_object)) {
162     ret = mini_object;
163   } else {
164     ret = gst_mini_object_copy (mini_object);
165     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy %s miniobject %p -> %p",
166         g_type_name (GST_MINI_OBJECT_TYPE (mini_object)), mini_object, ret);
167     gst_mini_object_unref (mini_object);
168   }
169
170   return ret;
171 }
172
173 /**
174  * gst_mini_object_ref:
175  * @mini_object: the mini-object
176  *
177  * Increase the reference count of the mini-object.
178  *
179  * Note that the refcount affects the writeability
180  * of @mini-object, see gst_mini_object_is_writable(). It is
181  * important to note that keeping additional references to
182  * GstMiniObject instances can potentially increase the number
183  * of memcpy operations in a pipeline, especially if the miniobject
184  * is a #GstBuffer.
185  *
186  * Returns: (transfer full): the mini-object.
187  */
188 GstMiniObject *
189 gst_mini_object_ref (GstMiniObject * mini_object)
190 {
191   g_return_val_if_fail (mini_object != NULL, NULL);
192   /* we can't assert that the refcount > 0 since the _free functions
193    * increments the refcount from 0 to 1 again to allow resurecting
194    * the object
195    g_return_val_if_fail (mini_object->refcount > 0, NULL);
196    */
197
198   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p ref %d->%d", mini_object,
199       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
200       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) + 1);
201
202   g_atomic_int_inc (&mini_object->refcount);
203
204   return mini_object;
205 }
206
207 static void
208 weak_refs_notify (GstMiniObject * obj)
209 {
210   guint i;
211
212   for (i = 0; i < obj->n_weak_refs; i++)
213     obj->weak_refs[i].notify (obj->weak_refs[i].data, obj);
214   g_free (obj->weak_refs);
215 }
216
217 /**
218  * gst_mini_object_unref:
219  * @mini_object: the mini-object
220  *
221  * Decreases the reference count of the mini-object, possibly freeing
222  * the mini-object.
223  */
224 void
225 gst_mini_object_unref (GstMiniObject * mini_object)
226 {
227   g_return_if_fail (mini_object != NULL);
228
229   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "%p unref %d->%d",
230       mini_object,
231       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object),
232       GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) - 1);
233
234   g_return_if_fail (mini_object->refcount > 0);
235
236   if (G_UNLIKELY (g_atomic_int_dec_and_test (&mini_object->refcount))) {
237     gboolean do_free;
238
239     if (mini_object->dispose)
240       do_free = mini_object->dispose (mini_object);
241     else
242       do_free = TRUE;
243
244     /* if the subclass recycled the object (and returned FALSE) we don't
245      * want to free the instance anymore */
246     if (G_LIKELY (do_free)) {
247       /* The weak reference stack is freed in the notification function */
248       if (mini_object->n_weak_refs)
249         weak_refs_notify (mini_object);
250
251 #ifndef GST_DISABLE_TRACE
252       _gst_alloc_trace_free (_gst_mini_object_trace, mini_object);
253 #endif
254       if (mini_object->free)
255         mini_object->free (mini_object);
256     }
257   }
258 }
259
260 /**
261  * gst_mini_object_replace:
262  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
263  *     be replaced
264  * @newdata: pointer to new mini-object
265  *
266  * Atomically modifies a pointer to point to a new mini-object.
267  * The reference count of @olddata is decreased and the reference count of
268  * @newdata is increased.
269  *
270  * Either @newdata and the value pointed to by @olddata may be NULL.
271  *
272  * Returns: TRUE if @newdata was different from @olddata
273  */
274 gboolean
275 gst_mini_object_replace (GstMiniObject ** olddata, GstMiniObject * newdata)
276 {
277   GstMiniObject *olddata_val;
278
279   g_return_val_if_fail (olddata != NULL, FALSE);
280
281   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p (%d) with %p (%d)",
282       *olddata, *olddata ? (*olddata)->refcount : 0,
283       newdata, newdata ? newdata->refcount : 0);
284
285   olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
286
287   if (G_UNLIKELY (olddata_val == newdata))
288     return FALSE;
289
290   if (newdata)
291     gst_mini_object_ref (newdata);
292
293   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
294               olddata, olddata_val, newdata))) {
295     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
296     if (G_UNLIKELY (olddata_val == newdata))
297       break;
298   }
299
300   if (olddata_val)
301     gst_mini_object_unref (olddata_val);
302
303   return olddata_val != newdata;
304 }
305
306 /**
307  * gst_mini_object_steal:
308  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
309  *     be stolen
310  *
311  * Replace the current #GstMiniObject pointer to by @olddata with NULL and
312  * return the old value.
313  *
314  * Returns: the #GstMiniObject at @oldata
315  */
316 GstMiniObject *
317 gst_mini_object_steal (GstMiniObject ** olddata)
318 {
319   GstMiniObject *olddata_val;
320
321   g_return_val_if_fail (olddata != NULL, NULL);
322
323   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "steal %p (%d)",
324       *olddata, *olddata ? (*olddata)->refcount : 0);
325
326   do {
327     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
328     if (olddata_val == NULL)
329       break;
330   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
331               olddata, olddata_val, NULL)));
332
333   return olddata_val;
334 }
335
336 /**
337  * gst_mini_object_take:
338  * @olddata: (inout) (transfer full): pointer to a pointer to a mini-object to
339  *     be replaced
340  * @newdata: pointer to new mini-object
341  *
342  * Modifies a pointer to point to a new mini-object. The modification
343  * is done atomically. This version is similar to gst_mini_object_replace()
344  * except that it does not increase the refcount of @newdata and thus
345  * takes ownership of @newdata.
346  *
347  * Either @newdata and the value pointed to by @olddata may be NULL.
348  *
349  * Returns: TRUE if @newdata was different from @olddata
350  */
351 gboolean
352 gst_mini_object_take (GstMiniObject ** olddata, GstMiniObject * newdata)
353 {
354   GstMiniObject *olddata_val;
355
356   g_return_val_if_fail (olddata != NULL, FALSE);
357
358   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "take %p (%d) with %p (%d)",
359       *olddata, *olddata ? (*olddata)->refcount : 0,
360       newdata, newdata ? newdata->refcount : 0);
361
362   do {
363     olddata_val = g_atomic_pointer_get ((gpointer *) olddata);
364     if (G_UNLIKELY (olddata_val == newdata))
365       break;
366   } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
367               olddata, olddata_val, newdata)));
368
369   if (olddata_val)
370     gst_mini_object_unref (olddata_val);
371
372   return olddata_val != newdata;
373 }
374
375 /**
376  * gst_mini_object_weak_ref: (skip)
377  * @object: #GstMiniObject to reference weakly
378  * @notify: callback to invoke before the mini object is freed
379  * @data: extra data to pass to notify
380  *
381  * Adds a weak reference callback to a mini object. Weak references are
382  * used for notification when a mini object is finalized. They are called
383  * "weak references" because they allow you to safely hold a pointer
384  * to the mini object without calling gst_mini_object_ref()
385  * (gst_mini_object_ref() adds a strong reference, that is, forces the object
386  * to stay alive).
387  *
388  * Since: 0.10.35
389  */
390 void
391 gst_mini_object_weak_ref (GstMiniObject * object,
392     GstMiniObjectWeakNotify notify, gpointer data)
393 {
394   guint i;
395
396   g_return_if_fail (object != NULL);
397   g_return_if_fail (notify != NULL);
398   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (object) >= 1);
399
400   G_LOCK (weak_refs_mutex);
401
402   if (object->n_weak_refs) {
403     /* Don't add the weak reference if it already exists. */
404     for (i = 0; i < object->n_weak_refs; i++) {
405       if (object->weak_refs[i].notify == notify &&
406           object->weak_refs[i].data == data) {
407         g_warning ("%s: Attempt to re-add existing weak ref %p(%p) failed.",
408             G_STRFUNC, notify, data);
409         goto found;
410       }
411     }
412
413     i = object->n_weak_refs++;
414     object->weak_refs =
415         g_realloc (object->weak_refs, sizeof (object->weak_refs[0]) * i);
416   } else {
417     object->weak_refs = g_malloc0 (sizeof (object->weak_refs[0]));
418     object->n_weak_refs = 1;
419     i = 0;
420   }
421   object->weak_refs[i].notify = notify;
422   object->weak_refs[i].data = data;
423 found:
424   G_UNLOCK (weak_refs_mutex);
425 }
426
427 /**
428  * gst_mini_object_weak_unref: (skip)
429  * @object: #GstMiniObject to remove a weak reference from
430  * @notify: callback to search for
431  * @data: data to search for
432  *
433  * Removes a weak reference callback to a mini object.
434  *
435  * Since: 0.10.35
436  */
437 void
438 gst_mini_object_weak_unref (GstMiniObject * object,
439     GstMiniObjectWeakNotify notify, gpointer data)
440 {
441   gboolean found_one = FALSE;
442
443   g_return_if_fail (object != NULL);
444   g_return_if_fail (notify != NULL);
445
446   G_LOCK (weak_refs_mutex);
447
448   if (object->n_weak_refs) {
449     guint i;
450
451     for (i = 0; i < object->n_weak_refs; i++)
452       if (object->weak_refs[i].notify == notify &&
453           object->weak_refs[i].data == data) {
454         found_one = TRUE;
455         object->n_weak_refs -= 1;
456         if (i != object->n_weak_refs)
457           object->weak_refs[i] = object->weak_refs[object->n_weak_refs];
458
459         break;
460       }
461   }
462   G_UNLOCK (weak_refs_mutex);
463   if (!found_one)
464     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
465 }