public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH] misc: Use allocate_once in getmntent
Date: Wed, 21 Aug 2019 14:41:00 -0000	[thread overview]
Message-ID: <8736huh98l.fsf@oldenburg2.str.redhat.com> (raw)

Both the buffer and struct mntent are now allocated on the heap.
This results in a slight reduction of RSS usage.

2019-08-21  Florian Weimer  <fweimer@redhat.com>

	* misc/mntent.c (struct mntent_buffer): Define.
	(mntent_buffer): Adjust type to void *.
	(allocate): Adjust for allocate_once.
	(deallocate): New function.
	(getmntent): Call allocate_once.

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>
+
+struct mntent_buffer
+{
+  struct mntent m;
+  char buffer[4096];
+};
 
 /* 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);
-
-/* This is the size of the buffer.  This is really big.  */
-#define BUFFER_SIZE	4096
+   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));
 }

             reply	other threads:[~2019-08-21 14:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-21 14:41 Florian Weimer [this message]
2019-08-21 20:19 ` Adhemerval Zanella
2019-08-22  9:06   ` Florian Weimer
2019-08-27 13:29     ` Adhemerval Zanella
2019-08-27 13:32       ` Florian Weimer
2019-08-27 13:33 ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8736huh98l.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).