mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-19 12:28:22 +01:00
WUtil: Rewrote 'wusergnusteppath' to be more efficient
The first optimisation is to compute only once the path, and then always re-use the value which did not change anyway. The second optimisation is to avoid a lot of excessive function calls, including alloc+free that are not necessary and participate in memory fragmentation.
This commit is contained in:
committed by
Carlos R. Mafra
parent
a18fd7cd69
commit
033e3eaa54
@@ -48,34 +48,33 @@ extern char *WMGetApplicationName();
|
||||
|
||||
const char *wusergnusteppath()
|
||||
{
|
||||
static const char subdir[] = "/GNUstep";
|
||||
static char *path = NULL;
|
||||
char *gspath;
|
||||
char *gspath, *h;
|
||||
int pathlen;
|
||||
|
||||
if (path)
|
||||
/* Value have been already computed, re-use it */
|
||||
return path;
|
||||
|
||||
gspath = getenv("GNUSTEP_USER_ROOT");
|
||||
if (gspath) {
|
||||
gspath = wexpandpath(gspath);
|
||||
if (gspath) {
|
||||
pathlen = strlen(gspath) + 4;
|
||||
path = wmalloc(pathlen);
|
||||
if (wstrlcpy(path, gspath, pathlen) >= pathlen) {
|
||||
wfree(gspath);
|
||||
return NULL;
|
||||
path = gspath;
|
||||
return path;
|
||||
}
|
||||
wfree(gspath);
|
||||
wwarning(_("variable GNUSTEP_USER_ROOT defined with invalid path, not used"));
|
||||
}
|
||||
} else {
|
||||
char *h = wgethomedir();
|
||||
|
||||
h = wgethomedir();
|
||||
if (!h)
|
||||
return NULL;
|
||||
pathlen = strlen(h) + 8 /* /GNUstep */ + 1;
|
||||
path = wmalloc(pathlen);
|
||||
if (wstrlcpy(path, h, pathlen) >= pathlen ||
|
||||
wstrlcat(path, "/GNUstep", pathlen) >= pathlen) {
|
||||
wfree(path);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
pathlen = strlen(h);
|
||||
path = wmalloc(pathlen + sizeof(subdir));
|
||||
strcpy(path, h);
|
||||
strcpy(path + pathlen, subdir);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user