Remove some extra memory allocations
authorArkadiy Illarionov <qarkai@gmail.com>
Fri, 18 Aug 2023 20:41:34 +0000 (23:41 +0300)
committerColin Clark <colin.clark@cclark.uk>
Sat, 19 Aug 2023 07:54:57 +0000 (08:54 +0100)
- Use g_string_free(string, FALSE). Also use len member instead of strlen(string->str).
- Use string literals instead of building string for resources' paths.

16 files changed:
src/collect-io.cc
src/collect-table.cc
src/dupe.cc
src/exif-common.cc
src/exif.cc
src/image-load-collection.cc
src/image-overlay.cc
src/layout.cc
src/misc.cc
src/osd.cc
src/pixbuf-renderer.cc
src/preferences.cc
src/search-and-run.cc
src/ui-fileops.cc
src/ui-utildlg.cc
src/utilops.cc

index a460938..fd7969c 100644 (file)
@@ -198,7 +198,7 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
                                }
                        }
 
-               if (strlen(extended_filename_buffer->str) > 0)
+               if (extended_filename_buffer->len > 0)
                        {
                        buffer2 = g_strdup(extended_filename_buffer->str);
                        g_string_erase(extended_filename_buffer, 0, -1);
index 1a79b1f..bed7239 100644 (file)
@@ -1482,7 +1482,6 @@ static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info,
        GError *error = nullptr;
        GInputStream *in_stream;
        GdkPixbuf *pb;
-       gchar *path;
 
        parent = gtk_widget_get_window(gtk_widget_get_toplevel(ct->listview));
        gdk_window_get_position(parent, &x_parent, &y_parent);
@@ -1505,10 +1504,7 @@ static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info,
                GdkWindowAttr attributes;
                gint attributes_mask;
 
-               path = g_build_filename(GQ_RESOURCE_PATH_ICONS, "gq-marker.xpm", NULL);
-               in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
-               g_free(path);
-
+               in_stream = g_resources_open_stream(GQ_RESOURCE_PATH_ICONS "/gq-marker.xpm", G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
                pb = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
                g_object_unref(in_stream);
 
index 1ce6b4f..42b050c 100644 (file)
@@ -5315,7 +5315,7 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
                work = work->next;
                }
 
-       g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, strlen(output_string->str), nullptr, &error);
+       g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, output_string->len, nullptr, &error);
 
        g_free(sep);
        g_string_free(output_string, TRUE);
index 02c4d1e..67f864b 100644 (file)
@@ -400,7 +400,6 @@ static gchar *exif_build_formatted_Flash(ExifData *exif)
 {
        /* grr, flash is a bitmask... */
        GString *string;
-       gchar *text;
        gint n;
        gint v;
 
@@ -441,10 +440,7 @@ static gchar *exif_build_formatted_Flash(ExifData *exif)
        /* red-eye (bit 6) */
        if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
 
-       text = g_strdup(string->str);
-       g_string_free(string, TRUE);
-
-       return text;
+       return g_string_free(string, FALSE);
 }
 
 static gchar *exif_build_formatted_Resolution(ExifData *exif)
@@ -530,7 +526,7 @@ static gchar *exif_build_formatted_ColorProfile(ExifData *exif)
 static gchar *exif_build_formatted_GPSPosition(ExifData *exif)
 {
        GString *string;
-       gchar *text, *ref;
+       gchar *ref;
        ExifRational *value;
        ExifItem *item;
        guint i;
@@ -575,10 +571,7 @@ static gchar *exif_build_formatted_GPSPosition(ExifData *exif)
                g_string_append_printf(string, ", %0lu° %0lu' %0.2f\" %.1s", p1, p2, p3, ref);
                } // if (item && ref)
 
-       text = g_strdup(string->str);
-       g_string_free(string, TRUE);
-
-       return text;
+       return g_string_free(string, FALSE);
 } // static gchar *exif_build_forma...
 
 static gchar *exif_build_formatted_GPSAltitude(ExifData *exif)
index 7cf15f2..71103fa 100644 (file)
@@ -1308,7 +1308,6 @@ static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat for
        const ExifMarker *marker;
        gpointer data;
        GString *string;
-       gchar *text;
        gint ne;
        gint i;
 
@@ -1433,10 +1432,7 @@ static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat for
                g_string_append(string, " ...");
                }
 
-       text = g_strdup(string->str);
-       g_string_free(string, TRUE);
-
-       return text;
+       return g_string_free(string, FALSE);
 }
 
 gchar *exif_item_get_string(ExifItem *item, gint)
index 4b831b7..9e2ced5 100644 (file)
@@ -99,15 +99,12 @@ static gboolean image_loader_collection_load(gpointer loader, const guchar *, gs
 
                                unlink(randname);
                                g_free(randname);
-                               g_string_free(file_names, TRUE);
                                g_free(cmd_line);
 
                                ret = TRUE;
                                }
-                       else
-                               {
-                               g_string_free(file_names, TRUE);
-                               }
+
+                       g_string_free(file_names, TRUE);
                        }
                }
        return ret;
index 6f190ee..d63976e 100644 (file)
@@ -365,20 +365,20 @@ static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
 
                if (active_marks > 0)
                        {
-                       GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
+                       GString *buf = g_string_sized_new(strlen(text) + 1 + FILEDATA_MARKS_SIZE * 2);
+
+                       if (*text)
+                               {
+                               g_string_append_printf(buf, "%s\n", text);
+                               }
 
                        for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
                                {
                                g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + (mark < 9 ? mark : -1) );
                                }
 
-                       if (*text)
-                               text2 = g_strdup_printf("%s\n%s", text, buf->str);
-                       else
-                               text2 = g_strdup(buf->str);
-                       g_string_free(buf, TRUE);
                        g_free(text);
-                       text = text2;
+                       text = g_string_free(buf, FALSE);
                        }
 
                if (with_hist)
index e419d07..9966828 100644 (file)
@@ -673,9 +673,7 @@ void layout_status_update_info(LayoutWindow *lw, const gchar *text)
                                        }
                                g_string_append_printf(delay, "%.1fs]", sec);
 
-                               ss = g_strdup(delay->str);
-
-                               g_string_free(delay, TRUE);
+                               ss = g_string_free(delay, FALSE);
                                }
                        else
                                {
index baae564..8ad2e56 100644 (file)
@@ -319,31 +319,24 @@ gchar *date_get_abbreviated_day_name(gint day)
 
 gchar *convert_rating_to_stars(gint rating)
 {
-       gchar *ret;
        GString *str = g_string_new(nullptr);
 
        if (rating == -1)
                {
                str = g_string_append_unichar(str, options->star_rating.rejected);
-               ret = g_strdup(str->str);
-               g_string_free(str, TRUE);
+               return g_string_free(str, FALSE);
                }
-       else if (rating > 0 && rating < 6)
+
+       if (rating > 0 && rating < 6)
                {
-               while (rating > 0)
+               for (; rating > 0; --rating)
                        {
                        str = g_string_append_unichar(str, options->star_rating.star);
-                       rating = rating - 1;
                        }
-               ret = g_strdup(str->str);
-               g_string_free(str, TRUE);
-               }
-       else
-               {
-               ret = g_strdup("");
+               return g_string_free(str, FALSE);
                }
 
-       return ret;
+       return g_strdup("");
 }
 
 gchar *get_symbolic_link(const gchar *path_utf8)
index d322e3f..6adb7ed 100644 (file)
@@ -201,7 +201,6 @@ static gchar *keywords_to_string(FileData *fd)
 {
        GList *keywords;
        GString *kwstr = nullptr;
-       gchar *ret = nullptr;
 
        g_assert(fd);
 
@@ -229,11 +228,10 @@ static gchar *keywords_to_string(FileData *fd)
 
        if (kwstr)
                {
-               ret = g_strdup(kwstr->str);
-               g_string_free(kwstr, TRUE);
+               return g_string_free(kwstr, FALSE);
                }
 
-       return ret;
+       return nullptr;
 }
 
 gchar *image_osd_mkinfo(const gchar *str, FileData *fd, GHashTable *vars)
@@ -443,12 +441,9 @@ gchar *image_osd_mkinfo(const gchar *str, FileData *fd, GHashTable *vars)
                g_string_erase(osd_info, start-osd_info->str, end-start-1);
                }
 
-       g_strchomp(osd_info->str);
+       ret = g_string_free(osd_info, FALSE);
 
-       ret = g_strdup(osd_info->str);
-       g_string_free(osd_info, TRUE);
-
-       return ret;
+       return g_strchomp(ret);
 }
 
 void osd_template_insert(GHashTable *vars, const gchar *keyword, const gchar *value, OsdTemplateFlags flags)
index 0b4f44b..05dc434 100644 (file)
@@ -882,12 +882,7 @@ static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
                gint w, h;
 
 #ifdef GQ_BUILD
-               gchar *resource_path;
-
-               resource_path = g_build_filename(GQ_RESOURCE_PATH_ICONS, PIXBUF_INLINE_SCROLLER ".png", NULL);
-
-               pixbuf = gdk_pixbuf_new_from_resource(resource_path, nullptr);
-               g_free(resource_path);
+               pixbuf = gdk_pixbuf_new_from_resource(GQ_RESOURCE_PATH_ICONS "/" PIXBUF_INLINE_SCROLLER ".png", nullptr);
 #else
                pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
                gdk_pixbuf_fill(pixbuf, 0x000000ff);
index 322ea97..a8589a2 100644 (file)
@@ -4051,11 +4051,9 @@ void show_about_window(LayoutWindow *lw)
        gchar *artists[2];
        gchar *author_line;
        gchar *authors[1000];
-       gchar *authors_path;
        gchar *comment;
        gchar *timezone_path;
        gchar *translators;
-       gchar *translators_path;
        gint i_authors = 0;
        gint n = 0;
        gsize bytes_read;
@@ -4082,9 +4080,7 @@ void show_about_window(LayoutWindow *lw)
                }
        g_free(timezone_path);
 
-       authors_path = g_build_filename(GQ_RESOURCE_PATH_CREDITS, "authors", NULL);
-
-       in_stream_authors = g_resources_open_stream(authors_path, G_RESOURCE_LOOKUP_FLAGS_NONE, nullptr);
+       in_stream_authors = g_resources_open_stream(GQ_RESOURCE_PATH_CREDITS "/authors", G_RESOURCE_LOOKUP_FLAGS_NONE, nullptr);
 
        data_stream = g_data_input_stream_new(in_stream_authors);
 
@@ -4099,7 +4095,7 @@ void show_about_window(LayoutWindow *lw)
 
        g_input_stream_close(in_stream_authors, nullptr, nullptr);
 
-       translators_path = g_build_filename(GQ_RESOURCE_PATH_CREDITS, "translators", NULL);
+       constexpr auto translators_path = GQ_RESOURCE_PATH_CREDITS "/translators";
 
        g_resources_get_info(translators_path, G_RESOURCE_LOOKUP_FLAGS_NONE, &size, &flags, nullptr);
 
@@ -4141,10 +4137,8 @@ void show_about_window(LayoutWindow *lw)
                }
 
        g_free(artists[0]);
-       g_free(authors_path);
        g_free(comment);
        g_free(translators);
-       g_free(translators_path);
        g_object_unref(data_stream);
        g_object_unref(in_stream_authors);
        g_object_unref(in_stream_translators);
index d4c5ed6..df7bb0f 100644 (file)
@@ -280,14 +280,11 @@ static gboolean match_func(GtkEntryCompletion *completion, const gchar *key, Gtk
 GtkWidget *search_and_run_new(LayoutWindow *lw)
 {
        SarData *sar;
-       gchar *ui_path;
 
        sar = g_new0(SarData, 1);
        sar->lw = lw;
 
-       ui_path = g_build_filename(GQ_RESOURCE_PATH_UI, "search-and-run.ui", nullptr);
-
-       sar->builder = gtk_builder_new_from_resource(ui_path);
+       sar->builder = gtk_builder_new_from_resource(GQ_RESOURCE_PATH_UI "/search-and-run.ui");
        command_store_populate(sar);
 
        sar->window = GTK_WIDGET(gtk_builder_get_object(sar->builder, "search_and_run"));
@@ -300,7 +297,6 @@ GtkWidget *search_and_run_new(LayoutWindow *lw)
        g_signal_connect(G_OBJECT(gtk_builder_get_object(sar->builder, "entry")), "activate", G_CALLBACK(entry_box_activate_cb), sar);
 
        gtk_widget_show(sar->window);
-       g_free(ui_path);
 
        return sar->window;
 }
index 719d58f..fcdd69e 100644 (file)
@@ -76,8 +76,7 @@ static void encoding_dialog(const gchar *path)
        lc = getenv("LANG");
        bf = getenv("G_BROKEN_FILENAMES");
 
-       string = g_string_new("");
-       g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
+       string = g_string_new(_("One or more filenames are not encoded with the preferred locale character set.\n"));
        g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
        g_string_append(string, "\n");
        g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
index 7078e9f..8d7075f 100644 (file)
@@ -571,10 +571,8 @@ static gboolean user_close_cb(GtkWidget *, GdkEvent *, gpointer data)
 static void show_notification_message(AppImageData *appimage_data)
 {
        GtkBuilder *builder;
-       gchar *ui_path;
 
-       ui_path = g_build_filename(GQ_RESOURCE_PATH_UI, "appimage-notification.ui", nullptr);
-       builder = gtk_builder_new_from_resource(ui_path);
+       builder = gtk_builder_new_from_resource(GQ_RESOURCE_PATH_UI "/appimage-notification.ui");
 
        appimage_data->window = GTK_WIDGET(gtk_builder_get_object(builder, "appimage_notification"));
 
@@ -584,8 +582,6 @@ static void show_notification_message(AppImageData *appimage_data)
 
        g_object_unref(builder);
        gtk_widget_show(appimage_data->window);
-
-       g_free(ui_path);
 }
 
 void appimage_notification_func(gpointer data, gpointer)
index 14dd3eb..d2851ed 100644 (file)
@@ -3154,7 +3154,7 @@ static void clipboard_get_func(GtkClipboard *clipboard, GtkSelectionData *select
                        }
                }
 
-       gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data), 8, reinterpret_cast<guchar *>(path_list_str->str), strlen(path_list_str->str));
+       gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data), 8, reinterpret_cast<guchar *>(path_list_str->str), path_list_str->len);
 
        g_string_free(path_list_str, TRUE);
 }