From 033e3eaa5417cdbae232d91a8b0835b5339ecb16 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 4 May 2013 15:43:31 +0200 Subject: [PATCH] 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. --- WINGs/userdefaults.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/WINGs/userdefaults.c b/WINGs/userdefaults.c index c1b4652d..4cfd55ab 100644 --- a/WINGs/userdefaults.c +++ b/WINGs/userdefaults.c @@ -48,35 +48,34 @@ 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; - } - wfree(gspath); - } - } else { - char *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; + path = gspath; + return path; } + wwarning(_("variable GNUSTEP_USER_ROOT defined with invalid path, not used")); } + h = wgethomedir(); + if (!h) + return NULL; + + pathlen = strlen(h); + path = wmalloc(pathlen + sizeof(subdir)); + strcpy(path, h); + strcpy(path + pathlen, subdir); + return path; }