Commit 6e27a58a authored by Christian Persch's avatar Christian Persch

Avoid unnecessary strdups

In all cases except one, the property bag does not need to store copies
of the attributes and values. So make the property bag store them by
reference, and add rsvg_property_bag_dup for the one case where we do
need to make a bag that owns the attributes and values.
parent 593ecfbb
......@@ -121,7 +121,7 @@ rsvg_style_handler_end (RsvgSaxHandler * self, const char *name)
}
static void
rsvg_start_style (RsvgHandle * ctx, RsvgPropertyBag * atts)
rsvg_start_style (RsvgHandle * ctx)
{
RsvgSaxHandlerStyle *handler = g_new0 (RsvgSaxHandlerStyle, 1);
......@@ -324,7 +324,7 @@ rsvg_desc_handler_end (RsvgSaxHandler * self, const char *name)
}
static void
rsvg_start_desc (RsvgHandle * ctx, RsvgPropertyBag * atts)
rsvg_start_desc (RsvgHandle * ctx)
{
RsvgSaxHandlerDesc *handler = g_new0 (RsvgSaxHandlerDesc, 1);
......@@ -390,7 +390,7 @@ rsvg_title_handler_end (RsvgSaxHandler * self, const char *name)
}
static void
rsvg_start_title (RsvgHandle * ctx, RsvgPropertyBag * atts)
rsvg_start_title (RsvgHandle * ctx)
{
RsvgSaxHandlerTitle *handler = g_new0 (RsvgSaxHandlerTitle, 1);
......@@ -470,7 +470,7 @@ rsvg_metadata_handler_end (RsvgSaxHandler * self, const char *name)
}
static void
rsvg_start_metadata (RsvgHandle * ctx, RsvgPropertyBag * atts)
rsvg_start_metadata (RsvgHandle * ctx)
{
RsvgSaxHandlerMetadata *handler = g_new0 (RsvgSaxHandlerMetadata, 1);
......@@ -646,13 +646,13 @@ rsvg_start_element (void *data, const xmlChar * name, const xmlChar ** atts)
name = (const xmlChar *) (tempname + 1);
if (!strcmp ((const char *) name, "style"))
rsvg_start_style (ctx, bag);
rsvg_start_style (ctx);
else if (!strcmp ((const char *) name, "title"))
rsvg_start_title (ctx, bag);
rsvg_start_title (ctx);
else if (!strcmp ((const char *) name, "desc"))
rsvg_start_desc (ctx, bag);
rsvg_start_desc (ctx);
else if (!strcmp ((const char *) name, "metadata"))
rsvg_start_metadata (ctx, bag);
rsvg_start_metadata (ctx);
else if (!strcmp ((const char *) name, "include")) /* xi:include */
rsvg_start_xinclude (ctx, bag);
else
......
......@@ -351,7 +351,7 @@ struct _RsvgNodeChars {
typedef void (*RsvgPropertyBagEnumFunc) (const char *key, const char *value, gpointer user_data);
RsvgPropertyBag *rsvg_property_bag_new (const char **atts);
RsvgPropertyBag *rsvg_property_bag_ref (RsvgPropertyBag * bag);
RsvgPropertyBag *rsvg_property_bag_dup (RsvgPropertyBag * bag);
void rsvg_property_bag_free (RsvgPropertyBag * bag);
const char *rsvg_property_bag_lookup (RsvgPropertyBag * bag, const char *key);
guint rsvg_property_bag_size (RsvgPropertyBag * bag);
......
......@@ -359,7 +359,7 @@ rsvg_node_svg_set_atts (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * att
* style element is not loaded yet here, so we need to store those attribues
* to be applied later.
*/
svg->atts = rsvg_property_bag_ref(atts);
svg->atts = rsvg_property_bag_dup(atts);
}
}
......
......@@ -1478,26 +1478,55 @@ rsvg_state_free_all (RsvgState * state)
}
}
/**
* rsvg_property_bag_new:
* @atts:
*
* The property bag will NOT copy the attributes and values. If you need
* to store them for later, use rsvg_property_bag_dup().
*
* Returns: (transfer full): a new property bag
*/
RsvgPropertyBag *
rsvg_property_bag_new (const char **atts)
{
RsvgPropertyBag *bag;
int i;
bag = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
bag = g_hash_table_new (g_str_hash, g_str_equal);
if (atts != NULL) {
for (i = 0; atts[i] != NULL; i += 2)
g_hash_table_insert (bag, (gpointer) g_strdup(atts[i]), (gpointer) g_strdup(atts[i + 1]));
g_hash_table_insert (bag, (gpointer) atts[i], (gpointer) atts[i + 1]);
}
return bag;
}
/**
* rsvg_property_bag_dup:
* @bag:
*
* Returns a copy of @bag that owns the attributes and values.
*
* Returns: (transfer full): a new property bag
*/
RsvgPropertyBag *
rsvg_property_bag_ref (RsvgPropertyBag * bag)
rsvg_property_bag_dup (RsvgPropertyBag * bag)
{
return g_hash_table_ref (bag);
RsvgPropertyBag *dup;
GHashTableIter iter;
gpointer key, value;
dup = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_iter_init (&iter, bag);
while (g_hash_table_iter_next (&iter, &key, &value))
g_hash_table_insert (dup,
(gpointer) g_strdup ((char *) key),
(gpointer) g_strdup ((char *) value));
return dup;
}
void
......
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