Commit ea60952f authored by Tim-Philipp Müller's avatar Tim-Philipp Müller 🐠
Browse files

gst/gstobject.c: Random micro-optimisation: don't use a hash table with...

gst/gstobject.c: Random micro-optimisation: don't use a hash table with strings as keys and the usual strdup/strcmp i...

Original commit message from CVS:
* gst/gstobject.c: (gst_object_set_name_default),
(gst_object_set_name):
Random micro-optimisation: don't use a hash table
with strings as keys and the usual strdup/strcmp
involved, but rather just use the GQuark of the
type name as key, since it needs to be looked up
anyway to get the type name string.
* tests/check/gst/gstobject.c: (GST_START_TEST):
Fix various leaks.
parent 99f16655
2006-07-07 Tim-Philipp Müller <tim at centricular dot net>
* gst/gstobject.c: (gst_object_set_name_default),
(gst_object_set_name):
Random micro-optimisation: don't use a hash table
with strings as keys and the usual strdup/strcmp
involved, but rather just use the GQuark of the
type name as key, since it needs to be looked up
anyway to get the type name string.
* tests/check/gst/gstobject.c: (GST_START_TEST):
Fix various leaks.
2006-07-07 Tim-Philipp Müller <tim at centricular dot net>
* gst/gstbin.c: (compare_interface), (gst_bin_get_by_interface),
......
......@@ -122,7 +122,8 @@ enum
SO_LAST_SIGNAL
};
static GHashTable *object_name_counts = NULL;
/* maps type name quark => count */
static GData *object_name_counts = NULL;
G_LOCK_DEFINE_STATIC (object_name_mutex);
......@@ -150,8 +151,7 @@ static void gst_object_dispatch_properties_changed (GObject * object,
static void gst_object_dispose (GObject * object);
static void gst_object_finalize (GObject * object);
static gboolean gst_object_set_name_default (GstObject * object,
const gchar * type_name);
static gboolean gst_object_set_name_default (GstObject * object);
#ifndef GST_DISABLE_LOADSAVE_REGISTRY
static void gst_object_real_restore_thyself (GstObject * object,
......@@ -600,28 +600,30 @@ gst_object_default_deep_notify (GObject * object, GstObject * orig,
}
static gboolean
gst_object_set_name_default (GstObject * object, const gchar * type_name)
gst_object_set_name_default (GstObject * object)
{
const gchar *type_name;
gint count;
gchar *name, *tmp;
gboolean result;
GQuark q;
/* to ensure guaranteed uniqueness across threads, only one thread
* may ever assign a name */
G_LOCK (object_name_mutex);
if (!object_name_counts) {
object_name_counts = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, NULL);
g_datalist_init (&object_name_counts);
}
count = GPOINTER_TO_INT (g_hash_table_lookup (object_name_counts, type_name));
g_hash_table_insert (object_name_counts, g_strdup (type_name),
GINT_TO_POINTER (count + 1));
q = g_type_qname (G_OBJECT_TYPE (object));
count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
G_UNLOCK (object_name_mutex);
/* GstFooSink -> foosinkN */
type_name = g_quark_to_string (q);
if (strncmp (type_name, "Gst", 3) == 0)
type_name += 3;
tmp = g_strdup_printf ("%s%d", type_name, count);
......@@ -670,7 +672,7 @@ gst_object_set_name (GstObject * object, const gchar * name)
result = TRUE;
} else {
GST_OBJECT_UNLOCK (object);
result = gst_object_set_name_default (object, G_OBJECT_TYPE_NAME (object));
result = gst_object_set_name_default (object);
}
return result;
......
......@@ -85,6 +85,7 @@ GST_START_TEST (test_fake_object_new)
fail_if (object == NULL, "Failed to create instance of GstFakeObject");
fail_unless (GST_IS_OBJECT (object),
"GstFakeObject instance is not a GstObject");
gst_object_unref (object);
}
GST_END_TEST
......@@ -101,6 +102,7 @@ GST_START_TEST (test_fake_object_name)
fail_if (name == NULL, "Newly created object has no name");
fail_if (strncmp (name, "fakeobject", 10) != 0,
"Random name %s does not start with Gst", name);
g_free (name);
/* give a random name by setting with NULL;
* GstFakeObject class -> fakeobject%d */
......@@ -123,6 +125,8 @@ GST_START_TEST (test_fake_object_name)
"Copy of object name affected actual object name");
g_free (name);
g_free (name2);
gst_object_unref (object);
}
GST_END_TEST
......@@ -185,6 +189,8 @@ GST_START_TEST (test_fake_object_name_threaded_wrong)
}
MAIN_STOP_THREADS ();
gst_object_unref (object);
fail_unless (expected_failure, "name did not get changed");
}
......@@ -220,6 +226,7 @@ GST_START_TEST (test_fake_object_name_threaded_right)
g_free (name);
}
MAIN_STOP_THREADS ();
gst_object_unref (object);
}
GST_END_TEST
......@@ -322,6 +329,7 @@ GST_START_TEST (test_fake_object_name_threaded_unique)
g_free (name1);
name1 = name2;
}
g_free (name1);
/* free stuff */
g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment