Update calls to malloc/free, etc., to use wmalloc/wfree/...
This fixes a lot of memory bugs which arose as a result of doing something different from the system malloc/free when allocators were rewritten in Rust. These changes originate from a different approach to writing the allocator in Rust: https://git.sdf.org/vitrine/wmaker/pulls/1/files#diff-04a0fd2319b9969373b75377716e45c836d22869 There are other function calls (to XFree) that need to be fixed, but that can be done in another commit. This one is already getting large.
This commit is contained in:
+1
-30
@@ -39,10 +39,6 @@
|
||||
#include <time.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#ifdef HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#include <signal.h>
|
||||
#ifdef __FreeBSD__
|
||||
#include <sys/signal.h>
|
||||
@@ -1361,7 +1357,7 @@ void wShowInfoPanel(WScreen *scr)
|
||||
char *posn = getPrettyOSName();
|
||||
if (posn) {
|
||||
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), posn, uts.machine);
|
||||
free(posn);
|
||||
wfree(posn);
|
||||
}
|
||||
else
|
||||
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), uts.sysname, uts.machine);
|
||||
@@ -1393,31 +1389,6 @@ void wShowInfoPanel(WScreen *scr)
|
||||
break;
|
||||
}
|
||||
|
||||
#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO2)
|
||||
{
|
||||
struct mallinfo2 ma = mallinfo2();
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
#ifdef DEBUG
|
||||
_("Total memory allocated: %lu kB (in use: %lu kB, %lu free chunks)\n"),
|
||||
#else
|
||||
_("Total memory allocated: %lu kB (in use: %lu kB)\n"),
|
||||
#endif
|
||||
(ma.arena + ma.hblkhd) / 1024,
|
||||
(ma.uordblks + ma.hblkhd) / 1024
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
* This information is representative of the memory
|
||||
* fragmentation. In ideal case it should be 1, but
|
||||
* that is never possible
|
||||
*/
|
||||
, ma.ordblks
|
||||
#endif
|
||||
);
|
||||
|
||||
strbuf = wstrappend(strbuf, buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
strbuf = wstrappend(strbuf, _("Image formats: "));
|
||||
strl = RSupportedFileFormats();
|
||||
separator = NULL;
|
||||
|
||||
+5
-7
@@ -3196,7 +3196,7 @@ static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
|
||||
setsid();
|
||||
#endif
|
||||
|
||||
args = malloc(sizeof(char *) * (argc + 1));
|
||||
args = wmalloc(sizeof(char *) * (argc + 1));
|
||||
if (!args)
|
||||
exit(111);
|
||||
|
||||
@@ -3340,8 +3340,8 @@ void wDockTrackWindowLaunch(WDock *dock, Window window)
|
||||
char *command = NULL;
|
||||
|
||||
if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
|
||||
free(wm_class);
|
||||
free(wm_instance);
|
||||
wfree(wm_class);
|
||||
wfree(wm_instance);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3421,10 +3421,8 @@ void wDockTrackWindowLaunch(WDock *dock, Window window)
|
||||
if (command)
|
||||
wfree(command);
|
||||
|
||||
if (wm_class)
|
||||
free(wm_class);
|
||||
if (wm_instance)
|
||||
free(wm_instance);
|
||||
wfree(wm_class);
|
||||
wfree(wm_instance);
|
||||
}
|
||||
|
||||
void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
|
||||
|
||||
+3
-3
@@ -141,7 +141,7 @@ WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
|
||||
{
|
||||
DeathHandler *handler;
|
||||
|
||||
handler = malloc(sizeof(DeathHandler));
|
||||
handler = wmalloc(sizeof(DeathHandler));
|
||||
if (!handler)
|
||||
return 0;
|
||||
|
||||
@@ -150,7 +150,7 @@ WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
|
||||
handler->client_data = cdata;
|
||||
|
||||
if (!deathHandlers)
|
||||
deathHandlers = WMCreateArrayWithDestructor(8, free);
|
||||
deathHandlers = WMCreateArrayWithDestructor(8, wfree);
|
||||
|
||||
WMAddToArray(deathHandlers, handler);
|
||||
|
||||
@@ -164,7 +164,7 @@ static void wdelete_death_handler(WMagicNumber id)
|
||||
if (!handler || !deathHandlers)
|
||||
return;
|
||||
|
||||
/* array destructor will call free(handler) */
|
||||
/* array destructor will call wfree(handler) */
|
||||
WMRemoveFromArray(deathHandlers, handler);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ WGeometryView *WCreateGeometryView(WMScreen * scr)
|
||||
widgetClass = W_RegisterUserWidget();
|
||||
}
|
||||
|
||||
gview = malloc(sizeof(WGeometryView));
|
||||
gview = wmalloc(sizeof(WGeometryView));
|
||||
if (!gview) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
+4
-4
@@ -131,7 +131,7 @@ static void setWVisualID(int screen, int val)
|
||||
/* no array at all, alloc space for screen + 1 entries
|
||||
* and init with default value */
|
||||
wVisualID_len = screen + 1;
|
||||
wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
|
||||
wVisualID = (int *)wmalloc(wVisualID_len * sizeof(int));
|
||||
for (i = 0; i < wVisualID_len; i++) {
|
||||
wVisualID[i] = -1;
|
||||
}
|
||||
@@ -156,7 +156,7 @@ static void setWVisualID(int screen, int val)
|
||||
*/
|
||||
static int initWVisualID(const char *user_str)
|
||||
{
|
||||
char *mystr = strdup(user_str);
|
||||
char *mystr = wstrdup(user_str);
|
||||
int cur_in_pos = 0;
|
||||
int cur_out_pos = 0;
|
||||
int cur_screen = 0;
|
||||
@@ -191,7 +191,7 @@ static int initWVisualID(const char *user_str)
|
||||
cur_in_pos++;
|
||||
}
|
||||
|
||||
free(mystr);
|
||||
wfree(mystr);
|
||||
|
||||
if (cur_screen == 0||error_found != 0)
|
||||
return 1;
|
||||
@@ -383,7 +383,7 @@ Bool RelaunchWindow(WWindow *wwin)
|
||||
setsid();
|
||||
#endif
|
||||
/* argv is not null-terminated */
|
||||
char **a = (char **) malloc(argc + 1);
|
||||
char **a = (char **) wmalloc(argc + 1);
|
||||
if (! a) {
|
||||
werror("out of memory trying to relaunch the application");
|
||||
Exit(-1);
|
||||
|
||||
+4
-4
@@ -54,13 +54,13 @@ int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
|
||||
|
||||
class_hint = XAllocClassHint();
|
||||
if (XGetClassHint(dpy, window, class_hint) == 0) {
|
||||
*wm_class = strdup("default");
|
||||
*wm_instance = strdup("default");
|
||||
*wm_class = wstrdup("default");
|
||||
*wm_instance = wstrdup("default");
|
||||
XFree(class_hint);
|
||||
return False;
|
||||
}
|
||||
*wm_instance = strdup(class_hint->res_name);
|
||||
*wm_class = strdup(class_hint->res_class);
|
||||
*wm_instance = wstrdup(class_hint->res_name);
|
||||
*wm_class = wstrdup(class_hint->res_class);
|
||||
|
||||
XFree(class_hint->res_name);
|
||||
XFree(class_hint->res_class);
|
||||
|
||||
+4
-4
@@ -1243,7 +1243,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
if (dentry->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
buffer = malloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
|
||||
buffer = wmalloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[i]);
|
||||
break;
|
||||
@@ -1288,7 +1288,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
@@ -1315,7 +1315,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
length += 7;
|
||||
if (command)
|
||||
length += strlen(command) + 6;
|
||||
buffer = malloc(length);
|
||||
buffer = wmalloc(length);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[data->index]);
|
||||
break;
|
||||
@@ -1353,7 +1353,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
if (command)
|
||||
length += strlen(command);
|
||||
|
||||
buffer = malloc(length);
|
||||
buffer = wmalloc(length);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[data->index]);
|
||||
break;
|
||||
|
||||
+3
-3
@@ -290,9 +290,9 @@ static WMPropList *makeWindowState(WWindow * wwin, WApplication * wapp)
|
||||
}
|
||||
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
if (command)
|
||||
wfree(command);
|
||||
|
||||
@@ -383,7 +383,7 @@ static pid_t execCommand(WScreen *scr, char *command)
|
||||
|
||||
SetupEnvironment(scr);
|
||||
|
||||
args = malloc(sizeof(char *) * (argc + 1));
|
||||
args = wmalloc(sizeof(char *) * (argc + 1));
|
||||
if (!args)
|
||||
exit(111);
|
||||
for (i = 0; i < argc; i++) {
|
||||
|
||||
+2
-2
@@ -244,7 +244,7 @@ reinit:
|
||||
wApplicationSetBouncing(data->wapp, 0);
|
||||
WMDeleteTimerHandler(data->timer);
|
||||
wApplicationDestroy(data->wapp);
|
||||
free(data);
|
||||
wfree(data);
|
||||
}
|
||||
|
||||
static int bounceDirection(WAppIcon *aicon)
|
||||
@@ -324,7 +324,7 @@ void wAppBounce(WApplication *wapp)
|
||||
wApplicationIncrementRefcount(wapp);
|
||||
wApplicationSetBouncing(wapp, 1);
|
||||
|
||||
AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
|
||||
AppBouncerData *data = (AppBouncerData *)wmalloc(sizeof(AppBouncerData));
|
||||
data->wapp = wapp;
|
||||
data->count = data->pow = 0;
|
||||
data->dir = bounceDirection(wApplicationGetAppIcon(wapp));
|
||||
|
||||
+1
-2
@@ -357,8 +357,7 @@ static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
|
||||
WMSetLabelText(panel->label, ntitle);
|
||||
}
|
||||
|
||||
if (ntitle)
|
||||
free(ntitle);
|
||||
wfree(ntitle);
|
||||
}
|
||||
|
||||
static WMArray *makeWindowListArray(WScreen *scr, int include_unmapped, Bool class_only)
|
||||
|
||||
+6
-6
@@ -950,10 +950,10 @@ WWindow *wManageWindow(WScreen *scr, Window window)
|
||||
}
|
||||
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
#undef ADEQUATE
|
||||
}
|
||||
|
||||
@@ -2602,7 +2602,7 @@ void wWindowSetShape(WWindow * wwin)
|
||||
if (!rects)
|
||||
goto alt_code;
|
||||
|
||||
urec = malloc(sizeof(XRectangle) * (count + 2));
|
||||
urec = wmalloc(sizeof(XRectangle) * (count + 2));
|
||||
if (!urec) {
|
||||
XFree(rects);
|
||||
goto alt_code;
|
||||
@@ -2779,7 +2779,7 @@ WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
|
||||
{
|
||||
WWindowState *wstate;
|
||||
|
||||
wstate = malloc(sizeof(WWindowState));
|
||||
wstate = wmalloc(sizeof(WWindowState));
|
||||
if (!wstate)
|
||||
return NULL;
|
||||
|
||||
@@ -2841,9 +2841,9 @@ WMagicNumber wWindowGetSavedState(Window win)
|
||||
if (command)
|
||||
wfree(command);
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
|
||||
return wstate;
|
||||
}
|
||||
|
||||
+2
-2
@@ -256,7 +256,7 @@ static void wXDNDGetTypeList(Display *dpy, Window window)
|
||||
return;
|
||||
}
|
||||
|
||||
typelist = malloc((count + 1) * sizeof(Atom));
|
||||
typelist = wmalloc((count + 1) * sizeof(Atom));
|
||||
a = (Atom *) data;
|
||||
for (i = 0; i < count; i++) {
|
||||
typelist[i] = a[i];
|
||||
@@ -267,7 +267,7 @@ static void wXDNDGetTypeList(Display *dpy, Window window)
|
||||
}
|
||||
typelist[count] = 0;
|
||||
XFree(data);
|
||||
free(typelist);
|
||||
wfree(typelist);
|
||||
}
|
||||
|
||||
Bool wXDNDProcessClientMessage(XClientMessageEvent *event)
|
||||
|
||||
Reference in New Issue
Block a user