1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-20 04:48:06 +01:00

WINGs: Do not allocate memory for a fixed-size short-lived buffer

Allocating memory with 'malloc' has a cost and participate to memory
fragmentation, so for a temporary buffer that has a fixed size let's
prefer allocating it on the stack.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
Christophe CURIS
2013-11-10 17:41:06 +01:00
committed by Carlos R. Mafra
parent d25631016e
commit 62565b42f6

View File

@@ -693,32 +693,35 @@ static void normalizePath(char *s)
static void deleteFile(WMWidget *widget, void *p_panel) static void deleteFile(WMWidget *widget, void *p_panel)
{ {
WMFilePanel *panel = p_panel; WMFilePanel *panel = p_panel;
char *file, *buffer; char *file;
char buffer[512];
struct stat filestat; struct stat filestat;
WMScreen *scr = WMWidgetScreen(panel->win); WMScreen *scr = WMWidgetScreen(panel->win);
#define __msgbufsize__ 512
/* Parameter not used, but tell the compiler that it is ok */ /* Parameter not used, but tell the compiler that it is ok */
(void) widget; (void) widget;
buffer = wmalloc(__msgbufsize__);
file = getCurrentFileName(panel); file = getCurrentFileName(panel);
normalizePath(file); normalizePath(file);
if (stat(file, &filestat) == -1) { if (stat(file, &filestat) == -1) {
snprintf(buffer, __msgbufsize__, _("Can not find %s: %s"), file, strerror(errno)); snprintf(buffer, sizeof(buffer),
_("Can not find %s: %s"),
file, strerror(errno));
showError(scr, panel->win, buffer, NULL); showError(scr, panel->win, buffer, NULL);
goto out; goto out;
} }
snprintf(buffer, __msgbufsize__, _("Delete %s %s?"), snprintf(buffer, sizeof(buffer), _("Delete %s %s?"),
S_ISDIR(filestat.st_mode) ? _("directory") : _("file"), file); S_ISDIR(filestat.st_mode) ? _("directory") : _("file"), file);
if (!WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win, if (!WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
_("Warning"), buffer, _("OK"), _("Cancel"), NULL)) { _("Warning"), buffer, _("OK"), _("Cancel"), NULL)) {
if (remove(file) == -1) { if (remove(file) == -1) {
snprintf(buffer, __msgbufsize__, _("Removing %s failed: %s"), file, strerror(errno)); snprintf(buffer, sizeof(buffer),
_("Removing %s failed: %s"),
file, strerror(errno));
showError(scr, panel->win, buffer, NULL); showError(scr, panel->win, buffer, NULL);
} else { } else {
char *s = strrchr(file, '/'); char *s = strrchr(file, '/');
@@ -729,11 +732,8 @@ static void deleteFile(WMWidget *widget, void *p_panel)
} }
out: out:
if (buffer)
wfree(buffer);
if (file) if (file)
wfree(file); wfree(file);
#undef __msgbufsize__
} }
static void goUnmount(WMWidget *widget, void *p_panel) static void goUnmount(WMWidget *widget, void *p_panel)