From e32376aaed01ed44981386e348ac35b58da23495 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 18 Mar 2004 11:12:34 +0000 Subject: Reduce memory used by static buffers and allocate that memory dynamicly instead. Based on an initial patch from Tobias Anderberg, but reworked. I asked Tobias to look into doing something more like what is done in busybox, but that proved to be a pain. One possible concern is that these buffers will probably show up as memory leaks i.e. with valgrind. Perhaps we should add in an atexit call to free this memory right after we allocate it? --- libc/inet/getservice.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'libc/inet/getservice.c') diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c index fb22ff95a..606def8e4 100644 --- a/libc/inet/getservice.c +++ b/libc/inet/getservice.c @@ -81,12 +81,22 @@ static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; #define MAXALIASES 35 +#define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES)) static FILE *servf = NULL; static struct servent serv; -static char buf[BUFSIZ+1 + sizeof(char *)*MAXALIASES]; +static char *buf = NULL; static int serv_stayopen; +static void __initbuf(void) +{ + if (!buf) { + buf = malloc(SBUFSIZE); + if (!buf) + abort(); + } +} + void setservent(int f) { LOCK; @@ -112,7 +122,9 @@ void endservent(void) struct servent * getservent(void) { struct servent *result; - getservent_r(&serv, buf, sizeof(buf), &result); + + __initbuf(); + getservent_r(&serv, buf, SBUFSIZE, &result); return result; } @@ -120,7 +132,9 @@ struct servent * getservent(void) struct servent *getservbyname(const char *name, const char *proto) { struct servent *result; - getservbyname_r(name, proto, &serv, buf, sizeof(buf), &result); + + __initbuf(); + getservbyname_r(name, proto, &serv, buf, SBUFSIZE, &result); return result; } @@ -128,7 +142,9 @@ struct servent *getservbyname(const char *name, const char *proto) struct servent * getservbyport(int port, const char *proto) { struct servent *result; - getservbyport_r(port, proto, &serv, buf, sizeof(buf), &result); + + __initbuf(); + getservbyport_r(port, proto, &serv, buf, SBUFSIZE, &result); return result; } -- cgit v1.2.3