]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - misc/mntent.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / misc / mntent.c
index 7dedc62387a602d42b6acdbb91739b44a6f87656..326c9cd6419750ba34ee8dde5e20930d61f4e661 100644 (file)
@@ -1,5 +1,5 @@
 /* Utilities for reading/writing fstab, mtab, etc.
-   Copyright (C) 1995-2015 Free Software Foundation, Inc.
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <https://www.gnu.org/licenses/>.  */
 
 #include <mntent.h>
 #include <stdlib.h>
-#include <bits/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));
 }