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