diff options
author | bbraun <bbraun@FreeBSD.org> | 2002-12-21 22:04:50 +0000 |
---|---|---|
committer | bbraun <bbraun@FreeBSD.org> | 2002-12-21 22:04:50 +0000 |
commit | d025aceb5b4ca22bb13f5c0229336bfdbd3052c8 (patch) | |
tree | 5420e832bd6279f20924f90b1a6f469d83df37ec | |
parent | 63da09d1e62bd16665dbeae5bd1bc3b67e6d5b98 (diff) | |
download | FreeBSD-src-d025aceb5b4ca22bb13f5c0229336bfdbd3052c8.zip FreeBSD-src-d025aceb5b4ca22bb13f5c0229336bfdbd3052c8.tar.gz |
Reduce libc's memory footprint by lazily allocating memory used internally
by setproctitle().
Reviewed by: jkh
-rw-r--r-- | lib/libc/gen/setproctitle.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/lib/libc/gen/setproctitle.c b/lib/libc/gen/setproctitle.c index d6101e1..332453c 100644 --- a/lib/libc/gen/setproctitle.c +++ b/lib/libc/gen/setproctitle.c @@ -62,11 +62,11 @@ void setproctitle(const char *fmt, ...) { static struct ps_strings *ps_strings; - static char buf[SPT_BUFSIZE]; - static char obuf[SPT_BUFSIZE]; + static char *buf = NULL; + static char *obuf = NULL; static char **oargv, *kbuf; static int oargc = -1; - static char *nargv[2] = { buf, NULL }; + static char *nargv[2] = { NULL, NULL }; char **nargvp; int nargc; int i; @@ -75,10 +75,23 @@ setproctitle(const char *fmt, ...) unsigned long ul_ps_strings; int oid[4]; + if (buf == NULL) { + buf = malloc(SPT_BUFSIZE); + if (buf == NULL) + return; + nargv[0] = buf; + } + + if (obuf == NULL ) { + obuf = malloc(SPT_BUFSIZE); + if (obuf == NULL) + return; + } + va_start(ap, fmt); if (fmt) { - buf[sizeof(buf) - 1] = '\0'; + buf[SPT_BUFSIZE - 1] = '\0'; if (fmt[0] == '-') { /* skip program name prefix */ @@ -86,12 +99,12 @@ setproctitle(const char *fmt, ...) len = 0; } else { /* print program name heading for grep */ - (void)snprintf(buf, sizeof(buf), "%s: ", _getprogname()); + (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname()); len = strlen(buf); } /* print the argument string */ - (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); + (void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap); nargvp = nargv; nargc = 1; @@ -140,12 +153,12 @@ setproctitle(const char *fmt, ...) oargc = i; break; } - snprintf(obuf + len, sizeof(obuf) - len, "%s%s", + snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", len ? " " : "", oargv[i]); if (len) len++; len += strlen(oargv[i]); - if (len >= sizeof(obuf)) + if (len >= SPT_BUFSIZE) break; } } @@ -155,7 +168,7 @@ setproctitle(const char *fmt, ...) /* style #2 - we can only restore our first arg :-( */ if (*obuf == '\0') strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr, - sizeof(obuf) - 1); + SPT_BUFSIZE - 1); OLD_PS_STRINGS->old_ps_nargvstr = 1; OLD_PS_STRINGS->old_ps_argvstr = nargvp[0]; } |