Work on refcounting and proper object destruction.

Original commit message from CVS:
Work on refcounting and proper object destruction.
This commit is contained in:
Wim Taymans 2001-05-11 18:41:13 +00:00
parent 24a3b52549
commit fa8e54e450
6 changed files with 156 additions and 43 deletions

View file

@ -29,7 +29,7 @@
#include <sys/mman.h>
/* we make too much noise for normal debugging... */
#define GST_DEBUG_FORCE_DISABLE
//#define GST_DEBUG_FORCE_DISABLE
#include "gst_private.h"
#include "cothreads.h"

View file

@ -510,6 +510,9 @@ gst_bin_real_destroy (GtkObject *object)
}
g_list_free (bin->children);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
/**
@ -915,7 +918,7 @@ static gboolean
gst_bin_iterate_func (GstBin *bin)
{
// only iterate if this is the manager bin
if (GST_ELEMENT_SCHED(bin)->parent == (GstElement *)bin) {
if (GST_ELEMENT_SCHED(bin)->parent == GST_ELEMENT (bin)) {
return GST_SCHEDULE_ITERATE(GST_ELEMENT_SCHED(bin));
} else {
GST_DEBUG (GST_CAT_SCHEDULING, "this bin can't be iterated on!\n");

View file

@ -29,7 +29,6 @@
#include "gstscheduler.h"
#include "gstutils.h"
/* Element signals and args */
enum {
STATE_CHANGE,
@ -52,7 +51,7 @@ static void gst_element_init (GstElement *element);
static void gst_element_set_arg (GtkObject *object, GtkArg *arg, guint id);
static void gst_element_get_arg (GtkObject *object, GtkArg *arg, guint id);
static void gst_element_real_destroy (GtkObject *object);
static void gst_element_finalize (GtkObject *object);
static GstElementStateReturn gst_element_change_state (GstElement *element);
@ -121,7 +120,7 @@ gst_element_class_init (GstElementClass *klass)
gtkobject_class->set_arg = GST_DEBUG_FUNCPTR(gst_element_set_arg);
gtkobject_class->get_arg = GST_DEBUG_FUNCPTR(gst_element_get_arg);
gtkobject_class->destroy = GST_DEBUG_FUNCPTR(gst_element_real_destroy);
gtkobject_class->finalize = GST_DEBUG_FUNCPTR(gst_element_finalize);
gstobject_class->save_thyself = GST_DEBUG_FUNCPTR(gst_element_save_thyself);
gstobject_class->restore_thyself = GST_DEBUG_FUNCPTR(gst_element_restore_thyself);
@ -837,13 +836,13 @@ GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT_PARENT(element)),GST_ELEM
}
static void
gst_element_real_destroy (GtkObject *object)
gst_element_finalize (GtkObject *object)
{
GstElement *element = GST_ELEMENT (object);
GList *pads;
GstPad *pad;
// g_print("in gst_element_real_destroy()\n");
//g_print("element_finalize()\n");
pads = element->pads;
while (pads) {
@ -853,6 +852,8 @@ gst_element_real_destroy (GtkObject *object)
}
g_list_free (element->pads);
GTK_OBJECT_CLASS (parent_class)->finalize (object);
}
/*

View file

@ -53,6 +53,10 @@ static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
static void gst_object_class_init (GstObjectClass *klass);
static void gst_object_init (GstObject *object);
static void gst_object_real_destroy (GtkObject *gtk_object);
static void gst_object_shutdown (GtkObject *gtk_object);
static void gst_object_finalize (GtkObject *gtk_object);
static GtkObjectClass *parent_class = NULL;
static guint gst_object_signals[LAST_SIGNAL] = { 0 };
@ -105,6 +109,10 @@ gst_object_class_init (GstObjectClass *klass)
klass->path_string_separator = "/";
klass->signal_object = gtk_type_new (gst_signal_object_get_type ());
gtkobject_class->shutdown = gst_object_shutdown;
gtkobject_class->destroy = gst_object_real_destroy;
gtkobject_class->finalize = gst_object_finalize;
}
static void
@ -118,9 +126,12 @@ gst_object_init (GstObject *object)
#ifdef HAVE_ATOMIC_H
atomic_set(&(object->refcount),1);
#else
object->refcount++;
object->refcount = 1;
#endif
object->parent = NULL;
object->flags = 0;
GST_FLAG_SET (object, GST_FLOATING);
}
/**
@ -136,6 +147,113 @@ gst_object_new (void)
return GST_OBJECT (gtk_type_new (gst_object_get_type ()));
}
/**
* gst_object_ref:
* @object: GstObject to reference
*
* Increments the refence count on the object.
*/
GstObject*
gst_object_ref (GstObject *object)
{
g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
//g_print ("object_ref\n");
gtk_object_ref (GTK_OBJECT (object));
return object;
}
#define gst_object_ref gst_object_ref
/**
* gst_object_unref:
* @object: GstObject to unreference
*
* Decrements the refence count on the object. If reference count hits
* zero, destroy the object.
*/
void
gst_object_unref (GstObject *object)
{
g_return_if_fail (GST_IS_OBJECT (object));
//g_print ("object_unref\n");
gtk_object_unref (GTK_OBJECT (object));
}
#define gst_object_unref gst_object_unref
/**
* gst_object_sink:
* @object: GstObject to sink
*
* Removes floating reference on an object. Any newly created object has
* a refcount of 1 and is FLOATING. This function should be used when
* creating a new object to symbolically 'take ownership of' the object.
*/
void
gst_object_sink (GstObject *object)
{
g_return_if_fail (object != NULL);
g_return_if_fail (GST_IS_OBJECT (object));
//g_print ("object_sink\n");
if (GST_OBJECT_FLOATING (object))
{
GST_FLAG_UNSET (object, GST_FLOATING);
gst_object_unref (object);
}
}
void
gst_object_destroy (GstObject *object)
{
g_return_if_fail (object != NULL);
g_return_if_fail (GST_IS_OBJECT (object));
//g_print ("object_destroy\n");
if (!GST_OBJECT_DESTROYED (object))
{
/* need to hold a reference count around all class method
* invocations.
*/
gst_object_ref (object);
GTK_OBJECT (object)->klass->shutdown (GTK_OBJECT (object));
gst_object_unref (object);
}
}
static void
gst_object_shutdown (GtkObject *object)
{
//g_print ("object_shutdown\n");
GST_FLAG_SET (GST_OBJECT (object), GST_DESTROYED);
parent_class->shutdown (GTK_OBJECT (object));
}
/* finilize is called when the object has to free its resources */
static void
gst_object_real_destroy (GtkObject *gtk_object)
{
parent_class->destroy (gtk_object);
}
/* finilize is called when the object has to free its resources */
static void
gst_object_finalize (GtkObject *gtk_object)
{
GstObject *object;
object = GST_OBJECT (gtk_object);
//g_print ("object_finalize\n");
if (object->name != NULL)
g_free (object->name);
g_mutex_free (object->lock);
parent_class->finalize (gtk_object);
}
/**
* gst_object_set_name:
* @object: GstObject to set the name of
@ -314,28 +432,6 @@ gst_object_unref (GstObject *object)
}
#endif /* gst_object_unref */
/**
* gst_object_sink:
* @object: GstObject to sink
*
* Removes floating reference on an object. Any newly created object has
* a refcount of 1 and is FLOATING. This function should be used when
* creating a new object to symbolically 'take ownership of' the object.
*/
#ifndef gst_object_sink
void
gst_object_sink (GstObject *object)
{
g_return_if_fail (object != NULL);
g_return_if_fail (GST_IS_OBJECT (object));
if (GTK_OBJECT_FLOATING (object)) {
GTK_OBJECT_UNSET_FLAGS (object, GTK_FLOATING);
gst_object_unref (object);
}
}
#endif /* gst_object_sink */
/**
* gst_object_check_uniqueness:
* @list: a list of #GstObject to check through

View file

@ -59,8 +59,14 @@ extern "C" {
//typedef struct _GstObject GstObject;
//typedef struct _GstObjectClass GstObjectClass;
//
typedef enum
{
GST_DESTROYED = 0,
GST_FLOATING,
#define GST_OBJECT_FLAG_LAST 4
GST_OBJECT_FLAG_LAST = 4,
} GstObjectFlags;
struct _GstObject {
GtkObject object;
@ -78,6 +84,8 @@ struct _GstObject {
/* this objects parent */
GstObject *parent;
guint32 flags;
};
struct _GstObjectClass {
@ -91,19 +99,23 @@ struct _GstObjectClass {
void (*object_saved) (GstObject *object, xmlNodePtr parent);
/* functions go here */
void (*destroy) (GstObject *object);
xmlNodePtr (*save_thyself) (GstObject *object, xmlNodePtr parent);
void (*restore_thyself) (GstObject *object, xmlNodePtr self);
};
#define GST_OBJECT_NAME(obj) (const gchar*)(((GstObject *)(obj))->name)
#define GST_OBJECT_PARENT(obj) (((GstObject *)(obj))->parent)
#define GST_FLAGS(obj) GTK_OBJECT_FLAGS(obj)
#define GST_FLAGS(obj) (GST_OBJECT (obj)->flags)
#define GST_FLAG_IS_SET(obj,flag) (GST_FLAGS (obj) & (1<<(flag)))
#define GST_FLAG_SET(obj,flag) G_STMT_START{ (GST_FLAGS (obj) |= (1<<(flag))); }G_STMT_END
#define GST_FLAG_UNSET(obj,flag) G_STMT_START{ (GST_FLAGS (obj) &= ~(1<<(flag))); }G_STMT_END
#define GST_OBJECT_NAME(obj) (const gchar*)(((GstObject *)(obj))->name)
#define GST_OBJECT_PARENT(obj) (((GstObject *)(obj))->parent)
#define GST_OBJECT_DESTROYED(obj) (GST_FLAG_IS_SET (obj, GST_DESTROYED))
#define GST_OBJECT_FLOATING(obj) (GST_FLAG_IS_SET (obj, GST_FLOATING))
/* object locking */
#define GST_LOCK(obj) (g_mutex_lock(GST_OBJECT(obj)->lock))
#define GST_TRYLOCK(obj) (g_mutex_trylock(GST_OBJECT(obj)->lock))
@ -129,12 +141,12 @@ gboolean gst_object_check_uniqueness (GList *list, const gchar *name);
xmlNodePtr gst_object_save_thyself (GstObject *object, xmlNodePtr parent);
/* refcounting */
#define gst_object_ref(object) gtk_object_ref(GTK_OBJECT(object));
#define gst_object_unref(object) gtk_object_unref(GTK_OBJECT(object));
#define gst_object_sink(object) gtk_object_sink(GTK_OBJECT(object));
GstObject * gst_object_ref (GstObject *object);
void gst_object_unref (GstObject *object);
void gst_object_sink (GstObject *object);
/* destroying an object */
#define gst_object_destroy(object) gtk_object_destroy(GTK_OBJECT(object))
void gst_object_destroy (GstObject *object);
/* printing out the 'path' of the object */
gchar * gst_object_get_path_string (GstObject *object);

View file

@ -180,7 +180,8 @@ gst_typefind_chain (GstPad *pad, GstBuffer *buf)
GST_DEBUG (0,"try type :%d \"%s\"\n", type->id, type->mime);
if (typefindfunc && (caps = typefindfunc (buf, type))) {
GST_DEBUG (0,"found type :%d \"%s\"\n", caps->id, type->mime);
GST_DEBUG (0,"found type :%d \"%s\" \"%s\"\n", caps->id, type->mime,
gst_caps_get_name (caps));
typefind->caps = caps;
gtk_signal_emit (GTK_OBJECT (typefind), gst_typefind_signals[HAVE_TYPE],
typefind->caps);