diff options
author | Florian Weimer <fweimer@redhat.com> | 2019-08-28 12:01:48 +0200 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2019-08-28 12:01:48 +0200 |
commit | 35ffd20dbd76d3cb6b478c7a69bb40d8c827ed81 (patch) | |
tree | 448240f1180109e25feed63b50fddb464a441fe0 /misc | |
parent | nptl: Move pthread_attr_setdetachstate implementation into libc (diff) | |
download | glibc-35ffd20dbd76d3cb6b478c7a69bb40d8c827ed81.tar.gz glibc-35ffd20dbd76d3cb6b478c7a69bb40d8c827ed81.tar.bz2 glibc-35ffd20dbd76d3cb6b478c7a69bb40d8c827ed81.zip |
misc: Use allocate_once in getmntent
Both the buffer and struct mntent are now allocated on the heap.
This results in a slight reduction of RSS usage.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'misc')
-rw-r--r-- | misc/mntent.c | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/misc/mntent.c b/misc/mntent.c index 980ea40967..8fae6064c6 100644 --- a/misc/mntent.c +++ b/misc/mntent.c @@ -18,36 +18,41 @@ #include <mntent.h> #include <stdlib.h> -#include <libc-lock.h> +#include <allocate_once.h> -/* We don't want to allocate the static buffer all the time since it - is not always used (in fact, rather infrequently). Accept the - extra cost of a `malloc'. */ -libc_freeres_ptr (static char *getmntent_buffer); +struct mntent_buffer +{ + struct mntent m; + char buffer[4096]; +}; -/* This is the size of the buffer. This is really big. */ -#define BUFFER_SIZE 4096 +/* We don't want to allocate the static buffer all the time since it + is not always used (in fact, rather infrequently). */ +libc_freeres_ptr (static void *mntent_buffer); +static void * +allocate (void *closure) +{ + return malloc (sizeof (struct mntent_buffer)); +} static void -allocate (void) +deallocate (void *closure, void *ptr) { - getmntent_buffer = (char *) malloc (BUFFER_SIZE); + free (ptr); } - struct mntent * getmntent (FILE *stream) { - static struct mntent m; - __libc_once_define (static, once); - __libc_once (once, allocate); - - if (getmntent_buffer == NULL) + struct mntent_buffer *buffer = allocate_once (&mntent_buffer, + allocate, deallocate, NULL); + if (buffer == NULL) /* If no core is available we don't have a chance to run the program successfully and so returning NULL is an acceptable result. */ return NULL; - return __getmntent_r (stream, &m, getmntent_buffer, BUFFER_SIZE); + return __getmntent_r (stream, &buffer->m, + buffer->buffer, sizeof (buffer->buffer)); } |