diff options
author | Eric Andersen <andersen@codepoet.org> | 2004-03-18 11:12:34 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2004-03-18 11:12:34 +0000 |
commit | e32376aaed01ed44981386e348ac35b58da23495 (patch) | |
tree | 3626e95d9c5c905b74085288d6e83ff7bd8dfa78 /libc/inet/getproto.c | |
parent | 38cd8e780fc6375117e8bc7db2eca17764779bba (diff) |
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?
Diffstat (limited to 'libc/inet/getproto.c')
-rw-r--r-- | libc/inet/getproto.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c index 1d3a5eff5..c9f35f149 100644 --- a/libc/inet/getproto.c +++ b/libc/inet/getproto.c @@ -75,12 +75,22 @@ static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; #define MAXALIASES 35 +#define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES)) static FILE *protof = NULL; static struct protoent proto; -static char static_aliases[BUFSIZ+1 + sizeof(char *)*MAXALIASES]; +static char *static_aliases = NULL; static int proto_stayopen; +static void __initbuf(void) +{ + if (!static_aliases) { + static_aliases = malloc(SBUFSIZE); + if (!static_aliases) + abort(); + } +} + void setprotoent(int f) { LOCK; @@ -183,7 +193,9 @@ again: struct protoent * getprotoent(void) { struct protoent *result; - getprotoent_r(&proto, static_aliases, sizeof(static_aliases), &result); + + __initbuf(); + getprotoent_r(&proto, static_aliases, SBUFSIZE, &result); return result; } @@ -216,7 +228,9 @@ found: struct protoent * getprotobyname(const char *name) { struct protoent *result; - getprotobyname_r(name, &proto, static_aliases, sizeof(static_aliases), &result); + + __initbuf(); + getprotobyname_r(name, &proto, static_aliases, SBUFSIZE, &result); return result; } @@ -242,7 +256,10 @@ int getprotobynumber_r (int proto_num, struct protoent * getprotobynumber(int proto_num) { struct protoent *result; - getprotobynumber_r(proto_num, &proto, static_aliases, sizeof(static_aliases), &result); + + __initbuf(); + getprotobynumber_r(proto_num, &proto, static_aliases, + SBUFSIZE, &result); return result; } |