From 15dfafdfb1f994e204561a7572ad66985e0d5898 Mon Sep 17 00:00:00 2001 From: phillbush Date: Fri, 29 May 2020 22:52:55 -0300 Subject: [PATCH] Saving users memory. When the item->label and item->output are the same, XMenu now only strdup() the item->label and make item->output equal to item->label. This saves memory when the user do not write a output specification in the input. --- xmenu.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/xmenu.c b/xmenu.c index 17aa3fb..7127914 100644 --- a/xmenu.c +++ b/xmenu.c @@ -86,7 +86,7 @@ static void drawmenu(struct Menu *currmenu); static struct Item *itemcycle(struct Menu *currmenu, int direction); static void run(struct Menu *currmenu); static void freemenu(struct Menu *menu); -static void cleanup(struct Menu *rootmenu); +static void cleanup(void); static void usage(void); /* global variables (X stuff and geometries) */ @@ -145,7 +145,10 @@ main(int argc, char *argv[]) /* run event loop */ run(rootmenu); - cleanup(rootmenu); + /* freeing stuff */ + freemenu(rootmenu); + cleanup(); + return 0; } @@ -254,8 +257,12 @@ allocitem(const char *label, const char *output) } else { if ((item->label = strdup(label)) == NULL) err(1, "strdup"); - if ((item->output = strdup(output)) == NULL) - err(1, "strdup"); + if (label == output) { + item->output = item->label; + } else { + if ((item->output = strdup(output)) == NULL) + err(1, "strdup"); + } } item->y = 0; item->h = item->label ? geom.itemh : geom.separator; @@ -818,7 +825,8 @@ freemenu(struct Menu *menu) freemenu(item->submenu); tmp = item; item = item->next; - free(tmp->label); + if (tmp->label != tmp->output) + free(tmp->label); free(tmp->output); free(tmp); } @@ -831,13 +839,11 @@ freemenu(struct Menu *menu) /* cleanup and exit */ static void -cleanup(struct Menu *rootmenu) +cleanup(void) { XUngrabPointer(dpy, CurrentTime); XUngrabKeyboard(dpy, CurrentTime); - freemenu(rootmenu); - XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); -- 2.20.1