Avoid copying of strings and making tiny allocations

This commit is contained in:
Christian Dywan 2011-12-11 02:57:04 +01:00
parent 622628b9c3
commit c8b19b2eeb
6 changed files with 42 additions and 50 deletions

View file

@ -216,12 +216,10 @@ settings_save_to_file (MidoriWebSettings* settings,
} }
g_object_get (settings, property, &string, NULL); g_object_get (settings, property, &string, NULL);
if (!string)
string = g_strdup ("");
if (!def_string) if (!def_string)
def_string = ""; def_string = "";
if (strcmp (string, def_string)) if (strcmp (string ? string : "", def_string))
g_key_file_set_string (key_file, "settings", property, string); g_key_file_set_string (key_file, "settings", property, string ? string : "");
g_free (string); g_free (string);
} }
else if (type == G_TYPE_PARAM_INT) else if (type == G_TYPE_PARAM_INT)
@ -1405,8 +1403,7 @@ midori_load_session (gpointer data)
{ {
katze_item_set_meta_integer (item, "append", 1); katze_item_set_meta_integer (item, "append", 1);
katze_item_set_meta_integer (item, "dont-write-history", 1); katze_item_set_meta_integer (item, "dont-write-history", 1);
if (load_on_startup == MIDORI_STARTUP_DELAYED_PAGES if (load_on_startup == MIDORI_STARTUP_DELAYED_PAGES)
&& katze_item_get_meta_integer (item, "delay") == -1)
katze_item_set_meta_integer (item, "delay", 1); katze_item_set_meta_integer (item, "delay", 1);
midori_browser_add_item (browser, item); midori_browser_add_item (browser, item);
} }

View file

@ -796,7 +796,7 @@ katze_item_metadata_to_xbel (KatzeItem* item)
const gchar* value; const gchar* value;
if (!keys) if (!keys)
return g_strdup (""); return NULL;
markup = g_string_new ("<info>\n<metadata"); markup = g_string_new ("<info>\n<metadata");
markdown = g_string_new (NULL); markdown = g_string_new (NULL);
@ -856,7 +856,7 @@ katze_array_to_xbel (KatzeArray* array,
">\n"); ">\n");
string_append_xml_element (markup, "title", katze_item_get_name (KATZE_ITEM (array))); string_append_xml_element (markup, "title", katze_item_get_name (KATZE_ITEM (array)));
string_append_xml_element (markup, "desc", katze_item_get_text (KATZE_ITEM (array))); string_append_xml_element (markup, "desc", katze_item_get_text (KATZE_ITEM (array)));
g_string_append (markup, metadata); g_string_append (markup, metadata ? metadata : "");
KATZE_ARRAY_FOREACH_ITEM_L (item, array, list) KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
string_append_item (markup, item); string_append_item (markup, item);
g_string_append (markup, "</xbel>\n"); g_string_append (markup, "</xbel>\n");

View file

@ -3341,17 +3341,17 @@ midori_browser_get_uri_extension (const gchar* uri)
slash = strrchr (uri, '/'); slash = strrchr (uri, '/');
/* Huh, URI without slashes? */ /* Huh, URI without slashes? */
if (!slash) if (!slash)
return g_strdup (""); return NULL;
ext_end = period = strrchr (slash, '.'); ext_end = period = strrchr (slash, '.');
if (!period) if (!period)
return g_strdup (""); return NULL;
/* Skip the period */ /* Skip the period */
ext_end++; ext_end++;
/* If *ext_end is 0 here, the URI ended with a period, so skip */ /* If *ext_end is 0 here, the URI ended with a period, so skip */
if (!*ext_end) if (!*ext_end)
return g_strdup (""); return NULL;
/* Find the end of the extension */ /* Find the end of the extension */
while (*ext_end && g_ascii_isalnum (*ext_end)) while (*ext_end && g_ascii_isalnum (*ext_end))
@ -5489,7 +5489,7 @@ midori_browser_size_allocate_cb (MidoriBrowser* browser,
{ {
GtkWidget* widget = GTK_WIDGET (browser); GtkWidget* widget = GTK_WIDGET (browser);
if (gtk_widget_get_realized (widget) && !browser->alloc_timeout) if (!browser->alloc_timeout && gtk_widget_get_realized (widget))
{ {
gpointer last_page; gpointer last_page;

View file

@ -1951,8 +1951,8 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view,
if (!webkit_web_view_can_cut_clipboard (web_view) if (!webkit_web_view_can_cut_clipboard (web_view)
&& !webkit_web_view_can_paste_clipboard (web_view)) && !webkit_web_view_can_paste_clipboard (web_view))
{ {
gchar* text = character ? g_strdup_printf ("%c", character) : g_strdup (""); gchar* text = character ? g_strdup_printf ("%c", character) : NULL;
g_signal_emit (view, signals[SEARCH_TEXT], 0, TRUE, text); g_signal_emit (view, signals[SEARCH_TEXT], 0, TRUE, text ? text : "");
g_free (text); g_free (text);
return TRUE; return TRUE;
} }
@ -3720,7 +3720,7 @@ prepare_speed_dial_html (MidoriView* view,
else else
{ {
g_free (file_path); g_free (file_path);
return g_strdup (""); return NULL;
} }
groups = g_key_file_get_groups (key_file, NULL); groups = g_key_file_get_groups (key_file, NULL);
@ -3774,7 +3774,7 @@ prepare_speed_dial_html (MidoriView* view,
} }
else else
{ {
encoded = g_strdup (""); encoded = NULL;
if (load_missing) if (load_missing)
midori_view_speed_dial_get_thumb (view, groups[i], uri); midori_view_speed_dial_get_thumb (view, groups[i], uri);
} }
@ -3785,7 +3785,7 @@ prepare_speed_dial_html (MidoriView* view,
"<a class=\"cross\" href=\"#\" onclick='clearShortcut(\"s%d\");'></a>" "<a class=\"cross\" href=\"#\" onclick='clearShortcut(\"s%d\");'></a>"
"<a href=\"%s\"><img src=\"data:image/png;base64,%s\"></a>" "<a href=\"%s\"><img src=\"data:image/png;base64,%s\"></a>"
"</div><div class=\"title\" onclick='renameShortcut(\"s%d\");'>%s</div></div>\n", "</div><div class=\"title\" onclick='renameShortcut(\"s%d\");'>%s</div></div>\n",
slot, slot, uri, encoded, slot, title ? title : ""); slot, slot, uri, encoded ? encoded : "", slot, title ? title : "");
g_free (title); g_free (title);
g_free (encoded); g_free (encoded);
@ -3848,7 +3848,7 @@ midori_view_set_uri (MidoriView* view,
speeddial_markup = prepare_speed_dial_html (view, TRUE); speeddial_markup = prepare_speed_dial_html (view, TRUE);
midori_view_load_alternate_string (view, midori_view_load_alternate_string (view,
speeddial_markup, "about:blank", NULL); speeddial_markup ? speeddial_markup : "", "about:blank", NULL);
#ifdef G_ENABLE_DEBUG #ifdef G_ENABLE_DEBUG
if (g_getenv ("MIDORI_STARTTIME") != NULL) if (g_getenv ("MIDORI_STARTTIME") != NULL)
@ -3960,7 +3960,7 @@ midori_view_set_uri (MidoriView* view,
katze_item_set_uri (view->item, view->uri); katze_item_set_uri (view->item, view->uri);
g_object_notify (G_OBJECT (view), "uri"); g_object_notify (G_OBJECT (view), "uri");
} }
else if (katze_item_get_meta_integer (view->item, "delay") > 0) else if (katze_item_get_meta_boolean (view->item, "delay"))
{ {
katze_assign (view->uri, g_strdup (uri)); katze_assign (view->uri, g_strdup (uri));
katze_item_set_meta_integer (view->item, "delay", -1); katze_item_set_meta_integer (view->item, "delay", -1);

View file

@ -1701,7 +1701,7 @@ midori_download_prepare_tooltip_text (WebKitDownload* download)
if (time_estimated > 0) if (time_estimated > 0)
{ {
gchar* eta; gchar* eta = NULL;
if (hours_left > 0) if (hours_left > 0)
eta = g_strdup_printf ("%s, %s", hours_str, minutes_str); eta = g_strdup_printf ("%s, %s", hours_str, minutes_str);
else if (minutes_left >= 10) else if (minutes_left >= 10)
@ -1710,12 +1710,13 @@ midori_download_prepare_tooltip_text (WebKitDownload* download)
eta = g_strdup_printf ("%s, %s", minutes_str, seconds_str); eta = g_strdup_printf ("%s, %s", minutes_str, seconds_str);
else if (seconds_left > 0) else if (seconds_left > 0)
eta = g_strdup_printf ("%s", seconds_str); eta = g_strdup_printf ("%s", seconds_str);
else if (eta != NULL)
eta = g_strdup (""); {
/* i18n: Download tooltip (estimated time) : - 1 hour, 5 minutes remaning */ /* i18n: Download tooltip (estimated time) : - 1 hour, 5 minutes remaning */
g_string_append_printf (tooltip, _(" - %s remaining"), eta); g_string_append_printf (tooltip, _(" - %s remaining"), eta);
g_free (eta); g_free (eta);
} }
}
g_free (hours_str); g_free (hours_str);
g_free (seconds_str); g_free (seconds_str);

View file

@ -216,9 +216,9 @@ midori_bookmarks_insert_item_db (sqlite3* db,
gchar* sqlcmd; gchar* sqlcmd;
char* errmsg = NULL; char* errmsg = NULL;
KatzeItem* old_parent; KatzeItem* old_parent;
gchar* parent; const gchar* parent;
gchar* uri; const gchar* uri = NULL;
gchar* desc; const gchar* desc = NULL;
/* Bookmarks must have a name, import may produce invalid items */ /* Bookmarks must have a name, import may produce invalid items */
g_return_if_fail (katze_item_get_name (item)); g_return_if_fail (katze_item_get_name (item));
@ -227,30 +227,26 @@ midori_bookmarks_insert_item_db (sqlite3* db,
return; return;
if (KATZE_ITEM_IS_BOOKMARK (item)) if (KATZE_ITEM_IS_BOOKMARK (item))
uri = g_strdup (katze_item_get_uri (item)); uri = katze_item_get_uri (item);
else
uri = g_strdup ("");
if (katze_item_get_text (item)) if (katze_item_get_text (item))
desc = g_strdup (katze_item_get_text (item)); desc = katze_item_get_text (item);
else
desc = g_strdup ("");
/* Use folder, otherwise fallback to parent folder */ /* Use folder, otherwise fallback to parent folder */
old_parent = katze_item_get_parent (item); old_parent = katze_item_get_parent (item);
if (folder && *folder) if (folder && *folder)
parent = g_strdup (folder); parent = folder;
else if (old_parent && katze_item_get_name (old_parent)) else if (old_parent && katze_item_get_name (old_parent))
parent = g_strdup (katze_item_get_name (old_parent)); parent = katze_item_get_name (old_parent);
else else
parent = g_strdup (""); parent = "";
sqlcmd = sqlite3_mprintf ( sqlcmd = sqlite3_mprintf (
"INSERT into bookmarks (uri, title, desc, folder, toolbar, app) values" "INSERT into bookmarks (uri, title, desc, folder, toolbar, app) values"
" ('%q', '%q', '%q', '%q', %d, %d)", " ('%q', '%q', '%q', '%q', %d, %d)",
uri, uri ? uri : "",
katze_item_get_name (item), katze_item_get_name (item),
desc, desc ? desc : "",
parent, parent,
katze_item_get_meta_boolean (item, "toolbar"), katze_item_get_meta_boolean (item, "toolbar"),
katze_item_get_meta_boolean (item, "app")); katze_item_get_meta_boolean (item, "app"));
@ -261,9 +257,6 @@ midori_bookmarks_insert_item_db (sqlite3* db,
sqlite3_free (errmsg); sqlite3_free (errmsg);
} }
g_free (uri);
g_free (parent);
g_free (desc);
sqlite3_free (sqlcmd); sqlite3_free (sqlcmd);
} }
@ -315,30 +308,31 @@ midori_bookmarks_row_changed_cb (GtkTreeModel* model,
{ {
KatzeItem* item; KatzeItem* item;
GtkTreeIter parent; GtkTreeIter parent;
gchar* parent_name; KatzeItem* new_parent = NULL;
const gchar* parent_name;
gtk_tree_model_get (model, iter, 0, &item, -1); gtk_tree_model_get (model, iter, 0, &item, -1);
if (gtk_tree_model_iter_parent (model, &parent, iter)) if (gtk_tree_model_iter_parent (model, &parent, iter))
{ {
KatzeItem* new_parent;
gtk_tree_model_get (model, &parent, 0, &new_parent, -1); gtk_tree_model_get (model, &parent, 0, &new_parent, -1);
/* Bookmarks must not be moved into non-folder items */ /* Bookmarks must not be moved into non-folder items */
if (!KATZE_ITEM_IS_FOLDER (new_parent)) if (!KATZE_ITEM_IS_FOLDER (new_parent))
parent_name = g_strdup (""); parent_name = "";
else else
parent_name = g_strdup (katze_item_get_name (new_parent)); parent_name = katze_item_get_name (new_parent);
g_object_unref (new_parent);
} }
else else
parent_name = g_strdup (""); parent_name = "";
katze_array_remove_item (bookmarks->array, item); katze_array_remove_item (bookmarks->array, item);
katze_item_set_meta_string (item, "folder", parent_name); katze_item_set_meta_string (item, "folder", parent_name);
katze_array_add_item (bookmarks->array, item); katze_array_add_item (bookmarks->array, item);
g_object_unref (item);
if (new_parent)
g_object_unref (new_parent);
} }
static void static void