seed r611 - trunk/libseed



Author: racarr
Date: Wed Dec 31 09:37:39 2008
New Revision: 611
URL: http://svn.gnome.org/viewvc/seed?rev=611&view=rev

Log:
Use stack allocations for small strings, and slice allocations for 
structs/private objects.


Modified:
   trunk/libseed/seed-engine.c
   trunk/libseed/seed-signals.c
   trunk/libseed/seed-structs.c

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Wed Dec 31 09:37:39 2008
@@ -138,7 +138,7 @@
 		jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);
 
 		length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
-		prop_name = g_malloc(length * sizeof(gchar));
+		prop_name = g_alloca(length * sizeof(gchar));
 		JSStringGetUTF8CString(jsprop_name, prop_name, length);
 
 		param_spec = g_object_class_find_property(oclass, prop_name);
@@ -170,8 +170,6 @@
 
 		if (*exception)
 		{
-
-			g_free(prop_name);
 			g_free(params);
 			JSPropertyNameArrayRelease(jsprops);
 			return 0;
@@ -197,7 +195,6 @@
 	for (i = 0; i < nparams; i++)
 	{
 		g_value_unset(&params[i].value);
-		g_free((gchar *) params[i].name);
 	}
 
 	if (G_IS_INITIALLY_UNOWNED(gobject))
@@ -679,7 +676,7 @@
 		return 0;
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = g_malloc(length * sizeof(gchar));
+	cproperty_name = g_alloca(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	spec = g_object_class_find_property(G_OBJECT_GET_CLASS(b), cproperty_name);
@@ -713,7 +710,6 @@
 
 			if (!info)
 			{
-				g_free(cproperty_name);
 				return 0;
 			}
 
@@ -736,12 +732,10 @@
 			{
 				ret = seed_field_get_value(context, b, field, exception);
 				g_base_info_unref((GIBaseInfo *) info);
-				g_free(cproperty_name);
 				return ret;
 			}
 			g_base_info_unref((GIBaseInfo *) info);
 		}
-		g_free(cproperty_name);
 		return 0;
 	}
  found:
@@ -751,7 +745,6 @@
 	ret = seed_value_from_gvalue(context, &gval, exception);
 	g_value_unset(&gval);
 
-	g_free(cproperty_name);
 	return (JSValueRef) ret;
 }
 
@@ -774,7 +767,7 @@
 	obj = seed_value_to_object(context, object, 0);
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = g_malloc(length * sizeof(gchar));
+	cproperty_name = g_alloca(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	spec = g_object_class_find_property(G_OBJECT_GET_CLASS(obj),
@@ -793,7 +786,6 @@
 											cproperty_name);
 		if (!spec)
 		{
-			g_free(cproperty_name);
 			return 0;
 		}
 	}
@@ -806,7 +798,6 @@
 	seed_gvalue_from_seed_value(context, value, type, &gval, exception);
 	if (*exception)
 	{
-		g_free(cproperty_name);
 		return 0;
 	}
 
@@ -823,7 +814,6 @@
 		return FALSE;
 	}
 
-	g_free(cproperty_name);
 	g_value_unset(&gval);
 
 	return TRUE;

Modified: trunk/libseed/seed-signals.c
==============================================================================
--- trunk/libseed/seed-signals.c	(original)
+++ trunk/libseed/seed-signals.c	Wed Dec 31 09:37:39 2008
@@ -33,7 +33,7 @@
 {
 	signal_privates *sig_priv = JSObjectGetPrivate(object);
 
-	g_free(sig_priv);
+	g_slice_free1(sizeof(signal_privates), sig_priv);
 }
 
 static void
@@ -42,7 +42,7 @@
 {
 	guint k;
 	JSObjectRef signal_ref;
-	signal_privates *priv = g_malloc(sizeof(signal_privates));
+	signal_privates *priv = g_slice_alloc(sizeof(signal_privates));
 	gchar *js_signal_name = g_strdup(signal->signal_name);
 	g_assert(signal);
 

Modified: trunk/libseed/seed-structs.c
==============================================================================
--- trunk/libseed/seed-structs.c	(original)
+++ trunk/libseed/seed-structs.c	Wed Dec 31 09:37:39 2008
@@ -29,6 +29,8 @@
 	GIBaseInfo *info;
 
 	gboolean free_pointer;
+	gboolean slice_alloc;
+	gsize size;
 } seed_struct_privates;
 
 static void seed_pointer_finalize(JSObjectRef object)
@@ -43,11 +45,16 @@
 			  priv->info ? g_base_info_get_name(priv->info) : "[generic]");
 
 	if (priv->free_pointer)
-		g_free(priv->pointer);
+	{
+		if (priv->slice_alloc)
+			g_slice_free1(priv->size, priv);
+		else
+			g_free(priv->pointer);
+	}
 	if (priv->info)
 		g_base_info_unref(priv->info);
 
-	g_free(priv);
+	g_slice_free1(sizeof(seed_struct_privates), priv);
 }
 
 static void seed_boxed_finalize(JSObjectRef object)
@@ -173,7 +180,7 @@
 	JSValueRef ret;
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = g_malloc(length * sizeof(gchar));
+	cproperty_name = g_alloca(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	SEED_NOTE(STRUCTS, "Getting property on union of type: %s  "
@@ -183,7 +190,6 @@
 	field = seed_union_find_field((GIUnionInfo *) priv->info, cproperty_name);
 	if (!field)
 	{
-		g_free(cproperty_name);
 		return 0;
 	}
 
@@ -191,8 +197,6 @@
 
 	g_base_info_unref((GIBaseInfo *) field);
 
-	g_free(cproperty_name);
-
 	return ret;
 }
 
@@ -212,7 +216,7 @@
 	gboolean ret;
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = g_malloc(length * sizeof(gchar));
+	cproperty_name = g_alloca(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	SEED_NOTE(STRUCTS, "Setting property on struct of type: %s  "
@@ -223,7 +227,6 @@
 
 	if (!field)
 	{
-		g_free(cproperty_name);
 		return 0;
 	}
 
@@ -232,7 +235,6 @@
 	seed_gi_make_argument(context, value, field_type, &field_value, exception);
 	ret = g_field_info_set_field(field, priv->pointer, &field_value);
 
-	g_free(cproperty_name);
 	g_base_info_unref((GIBaseInfo *) field_type);
 	g_base_info_unref((GIBaseInfo *) field);
 }
@@ -253,7 +255,7 @@
 	JSValueRef ret;
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = g_malloc(length * sizeof(gchar));
+	cproperty_name = g_alloca(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	SEED_NOTE(STRUCTS, "Getting property on struct of type: %s  "
@@ -264,14 +266,12 @@
 
 	if (!field)
 	{
-		g_free(cproperty_name);
 		return 0;
 	}
 
 	ret = seed_field_get_value(context, priv->pointer, field, exception);
 
 	g_base_info_unref((GIBaseInfo *) field);
-	g_free(cproperty_name);
 
 	return ret;
 }
@@ -422,9 +422,16 @@
 	}
 }
 
+static void seed_pointer_set_slice(JSContextRef ctx,
+						   JSValueRef pointer, gboolean free_pointer)
+{
+	seed_struct_privates *priv = JSObjectGetPrivate((JSObjectRef) pointer);
+	priv->slice_alloc = free_pointer;
+}
+
 JSObjectRef seed_make_pointer(JSContextRef ctx, gpointer pointer)
 {
-	seed_struct_privates *priv = g_malloc(sizeof(seed_struct_privates));
+	seed_struct_privates *priv = g_slice_alloc(sizeof(seed_struct_privates));
 	priv->pointer = pointer;
 	priv->info = 0;
 	priv->free_pointer = FALSE;
@@ -437,7 +444,7 @@
 {
 	JSObjectRef object;
 	gint i, n_methods;
-	seed_struct_privates *priv = g_malloc(sizeof(seed_struct_privates));
+	seed_struct_privates *priv = g_slice_alloc(sizeof(seed_struct_privates));
 
 	priv->pointer = younion;
 	priv->info = info ? g_base_info_ref(info) : 0;
@@ -470,7 +477,7 @@
 {
 	JSObjectRef object;
 	gint i, n_methods;
-	seed_struct_privates *priv = g_malloc(sizeof(seed_struct_privates));
+	seed_struct_privates *priv = g_slice_alloc(sizeof(seed_struct_privates));
 
 	priv->info = info ? g_base_info_ref(info) : 0;
 	priv->pointer = boxed;
@@ -489,7 +496,7 @@
 {
 	JSObjectRef object;
 	gint i, n_methods;
-	seed_struct_privates *priv = g_malloc(sizeof(seed_struct_privates));
+	seed_struct_privates *priv = g_slice_alloc(sizeof(seed_struct_privates));
 
 	priv->info = info ? g_base_info_ref(info) : 0;
 	priv->pointer = strukt;
@@ -559,14 +566,15 @@
 		size = g_union_info_get_size((GIUnionInfo *) info);
 	}
 	g_assert(size);
-	object = g_malloc0(size);
-
+	object = g_slice_alloc0(size);
+	
 	if (type == GI_INFO_TYPE_STRUCT)
 		ret = seed_make_struct(ctx, object, info);
 	else
 		ret = seed_make_union(ctx, object, info);
 
 	seed_pointer_set_free(ctx, ret, TRUE);
+	seed_pointer_set_slice(ctx, ret, TRUE);
 
 	if (!parameters)
 		return ret;
@@ -580,7 +588,7 @@
 		jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);
 
 		length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
-		prop_name = g_malloc(length * sizeof(gchar));
+		prop_name = g_alloca(length * sizeof(gchar));
 		JSStringGetUTF8CString(jsprop_name, prop_name, length);
 
 		if (type == GI_INFO_TYPE_STRUCT)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]