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()
|
const char *wusergnusteppath()
|
||||||
{
|
{
|
||||||
|
static const char subdir[] = "/GNUstep";
|
||||||
static char *path = NULL;
|
static char *path = NULL;
|
||||||
char *gspath;
|
char *gspath, *h;
|
||||||
int pathlen;
|
int pathlen;
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
/* Value have been already computed, re-use it */
|
||||||
|
return path;
|
||||||
|
|
||||||
gspath = getenv("GNUSTEP_USER_ROOT");
|
gspath = getenv("GNUSTEP_USER_ROOT");
|
||||||
if (gspath) {
|
if (gspath) {
|
||||||
gspath = wexpandpath(gspath);
|
gspath = wexpandpath(gspath);
|
||||||
if (gspath) {
|
if (gspath) {
|
||||||
pathlen = strlen(gspath) + 4;
|
path = gspath;
|
||||||
path = wmalloc(pathlen);
|
return path;
|
||||||
if (wstrlcpy(path, gspath, pathlen) >= pathlen) {
|
|
||||||
wfree(gspath);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
wfree(gspath);
|
wwarning(_("variable GNUSTEP_USER_ROOT defined with invalid path, not used"));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
char *h = wgethomedir();
|
h = wgethomedir();
|
||||||
if (!h)
|
if (!h)
|
||||||
return NULL;
|
return NULL;
|
||||||
pathlen = strlen(h) + 8 /* /GNUstep */ + 1;
|
|
||||||
path = wmalloc(pathlen);
|
pathlen = strlen(h);
|
||||||
if (wstrlcpy(path, h, pathlen) >= pathlen ||
|
path = wmalloc(pathlen + sizeof(subdir));
|
||||||
wstrlcat(path, "/GNUstep", pathlen) >= pathlen) {
|
strcpy(path, h);
|
||||||
wfree(path);
|
strcpy(path + pathlen, subdir);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user