summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-03-18 11:12:34 +0000
committerEric Andersen <andersen@codepoet.org>2004-03-18 11:12:34 +0000
commite32376aaed01ed44981386e348ac35b58da23495 (patch)
tree3626e95d9c5c905b74085288d6e83ff7bd8dfa78
parent38cd8e780fc6375117e8bc7db2eca17764779bba (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?
-rw-r--r--libc/inet/getnetent.c11
-rw-r--r--libc/inet/getproto.c25
-rw-r--r--libc/inet/getservice.c24
-rw-r--r--libc/misc/mntent/mntent.c11
-rw-r--r--libc/misc/ttyent/getttyent.c10
5 files changed, 68 insertions, 13 deletions
diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c
index 9ade1f6b2..8c22c0a00 100644
--- a/libc/inet/getnetent.c
+++ b/libc/inet/getnetent.c
@@ -37,7 +37,7 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
#define MAXALIASES 35
static const char NETDB[] = _PATH_NETWORKS;
static FILE *netf = NULL;
-static char line[BUFSIZ+1];
+static char *line = NULL;
static struct netent net;
static char *net_aliases[MAXALIASES];
@@ -90,6 +90,13 @@ struct netent * getnetent(void)
return (NULL);
}
again:
+
+ if (!line) {
+ line = malloc(BUFSIZ + 1);
+ if (!line)
+ abort();
+ }
+
p = fgets(line, BUFSIZ, netf);
if (p == NULL) {
UNLOCK;
@@ -114,7 +121,7 @@ again:
net.n_net = inet_network(cp);
net.n_addrtype = AF_INET;
q = net.n_aliases = net_aliases;
- if (p != NULL)
+ if (p != NULL)
cp = p;
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
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;
}
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;
}
diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c
index 93d591812..d98a6870f 100644
--- a/libc/misc/mntent/mntent.c
+++ b/libc/misc/mntent/mntent.c
@@ -65,10 +65,17 @@ struct mntent *getmntent_r (FILE *filep,
struct mntent *getmntent(FILE * filep)
{
struct mntent *tmp;
- static char buff[BUFSIZ];
+ static char *buff = NULL;
static struct mntent mnt;
LOCK;
- tmp = getmntent_r(filep, &mnt, buff, sizeof buff);
+
+ if (!buff) {
+ buff = malloc(BUFSIZ);
+ if (!buff)
+ abort();
+ }
+
+ tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
UNLOCK;
return(tmp);
}
diff --git a/libc/misc/ttyent/getttyent.c b/libc/misc/ttyent/getttyent.c
index 18b9109af..0b2c9a3cc 100644
--- a/libc/misc/ttyent/getttyent.c
+++ b/libc/misc/ttyent/getttyent.c
@@ -99,11 +99,19 @@ struct ttyent * getttyent(void)
{
register int c;
register char *p;
- static char line[BUFSIZ];
+ static char *line = NULL;
if (!tf && !setttyent())
return (NULL);
+
+ if (!line) {
+ line = malloc(BUFSIZ);
+ if (!line)
+ abort();
+ }
+
flockfile (tf);
+
for (;;) {
if (!fgets_unlocked(p = line, sizeof(line), tf)) {
funlockfile (tf);