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.
This commit is contained in:
phillbush 2020-05-29 22:52:55 -03:00
parent 4f898cc3af
commit 15dfafdfb1

16
xmenu.c
View File

@ -86,7 +86,7 @@ static void drawmenu(struct Menu *currmenu);
static struct Item *itemcycle(struct Menu *currmenu, int direction); static struct Item *itemcycle(struct Menu *currmenu, int direction);
static void run(struct Menu *currmenu); static void run(struct Menu *currmenu);
static void freemenu(struct Menu *menu); static void freemenu(struct Menu *menu);
static void cleanup(struct Menu *rootmenu); static void cleanup(void);
static void usage(void); static void usage(void);
/* global variables (X stuff and geometries) */ /* global variables (X stuff and geometries) */
@ -145,7 +145,10 @@ main(int argc, char *argv[])
/* run event loop */ /* run event loop */
run(rootmenu); run(rootmenu);
cleanup(rootmenu); /* freeing stuff */
freemenu(rootmenu);
cleanup();
return 0; return 0;
} }
@ -254,9 +257,13 @@ allocitem(const char *label, const char *output)
} else { } else {
if ((item->label = strdup(label)) == NULL) if ((item->label = strdup(label)) == NULL)
err(1, "strdup"); err(1, "strdup");
if (label == output) {
item->output = item->label;
} else {
if ((item->output = strdup(output)) == NULL) if ((item->output = strdup(output)) == NULL)
err(1, "strdup"); err(1, "strdup");
} }
}
item->y = 0; item->y = 0;
item->h = item->label ? geom.itemh : geom.separator; item->h = item->label ? geom.itemh : geom.separator;
if (item->label == NULL) if (item->label == NULL)
@ -818,6 +825,7 @@ freemenu(struct Menu *menu)
freemenu(item->submenu); freemenu(item->submenu);
tmp = item; tmp = item;
item = item->next; item = item->next;
if (tmp->label != tmp->output)
free(tmp->label); free(tmp->label);
free(tmp->output); free(tmp->output);
free(tmp); free(tmp);
@ -831,13 +839,11 @@ freemenu(struct Menu *menu)
/* cleanup and exit */ /* cleanup and exit */
static void static void
cleanup(struct Menu *rootmenu) cleanup(void)
{ {
XUngrabPointer(dpy, CurrentTime); XUngrabPointer(dpy, CurrentTime);
XUngrabKeyboard(dpy, CurrentTime); XUngrabKeyboard(dpy, CurrentTime);
freemenu(rootmenu);
XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]); XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]); XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]); XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);