]> Savannah Git Hosting - gnulib.git/commitdiff
canonicalize: simplify via scratch_buffer_dupfree
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 28 Dec 2020 19:38:58 +0000 (11:38 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 28 Dec 2020 20:56:47 +0000 (12:56 -0800)
* config/srclist.txt: Adjust accordingly.
* lib/canonicalize-lgpl.c (realpath_stk):
* lib/canonicalize.c (canonicalize_filename_mode_stk):
Simplify by using scratch_buffer_dupfree.
* lib/malloc/scratch_buffer.h (scratch_buffer_dupfree): New function.
* lib/malloc/scratch_buffer_dupfree.c: New file.
* modules/scratch_buffer (Files, Depends-on):
Add malloc/scratch_buffer_dupfree.c.

ChangeLog
config/srclist.txt
lib/canonicalize-lgpl.c
lib/canonicalize.c
lib/malloc/scratch_buffer.h
lib/malloc/scratch_buffer_dupfree.c [new file with mode: 0644]
modules/scratch_buffer

index 97966b427f9db06f8a6b983a8cdceeca2d4a540b..0428619c179fa6fa8e8e5dbf51459cc84fb2f724 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2020-12-28  Paul Eggert  <eggert@cs.ucla.edu>
+
+       canonicalize: simplify via scratch_buffer_dupfree
+       * config/srclist.txt: Adjust accordingly.
+       * lib/canonicalize-lgpl.c (realpath_stk):
+       * lib/canonicalize.c (canonicalize_filename_mode_stk):
+       Simplify by using scratch_buffer_dupfree.
+       * lib/malloc/scratch_buffer.h (scratch_buffer_dupfree): New function.
+       * lib/malloc/scratch_buffer_dupfree.c: New file.
+       * modules/scratch_buffer (Files, Depends-on):
+       Add malloc/scratch_buffer_dupfree.c.
+
 2020-12-27  Paul Eggert  <eggert@cs.ucla.edu>
 
        regex: remove glibc21.m4
index f33b1353f86eef52f14444699f051aa63742d0d2..3956082c842f51f148670397e883fa542c5de74c 100644 (file)
@@ -49,7 +49,8 @@ $GNUORG Copyright/request-assign.future               doc/Copyright
 $GNUORG Copyright/request-assign.program       doc/Copyright
 $GNUORG Copyright/request-disclaim.changes     doc/Copyright
 
-$LIBCSRC include/scratch_buffer.h      lib/malloc
+#$LIBCSRC include/scratch_buffer.h     lib/malloc
+#$LIBCSRC malloc/scratch_buffer_dupfree.c      lib/malloc
 $LIBCSRC malloc/scratch_buffer_grow.c  lib/malloc
 $LIBCSRC malloc/scratch_buffer_grow_preserve.c lib/malloc
 $LIBCSRC malloc/scratch_buffer_set_array_size.c        lib/malloc
index 68b6fdf6edcc49781931767a441562a2201898fd..7ac5d412dae824fec3fa79902fcf1aecc4e4a661 100644 (file)
@@ -413,24 +413,14 @@ error:
 error_nomem:
   scratch_buffer_free (&extra_buffer);
   scratch_buffer_free (&link_buffer);
-  if (failed || rname == resolved)
-    scratch_buffer_free (rname_buf);
-
-  if (failed)
-    return NULL;
 
-  if (rname == resolved)
-    return rname;
-  idx_t rname_size = dest - rname;
-  if (rname == rname_on_stack)
+  if (failed || rname == resolved)
     {
-      rname = malloc (rname_size);
-      if (rname == NULL)
-        return NULL;
-      return memcpy (rname, rname_on_stack, rname_size);
+      scratch_buffer_free (rname_buf);
+      return failed ? NULL : resolved;
     }
-  char *result = realloc (rname, rname_size);
-  return result != NULL ? result : rname;
+
+  return scratch_buffer_dupfree (rname_buf, dest - rname);
 }
 
 /* Return the canonical absolute name of file NAME.  A canonical name
index e8b74d855abd3373e24544f93a1f08a9d32532c0..48a49e41242a608589bf5a400b2de3c549a379f1 100644 (file)
@@ -469,11 +469,10 @@ error:
       return NULL;
     }
 
-  idx_t rname_size = dest - rname;
-  if (rname == rname_on_stack)
-    return xmemdup (rname, rname_size);
-  char *result = realloc (rname, rname_size);
-  return result != NULL ? result : rname;
+  char *result = scratch_buffer_dupfree (rname_buf, dest - rname);
+  if (!result)
+    xalloc_die ();
+  return result;
 }
 
 /* Return the canonical absolute name of file NAME, while treating
index c39da78629a6ed5d27dd173df62a0f8c73c51800..48d651b41aaf2892de0f1b6c34091194ff9d134d 100644 (file)
@@ -132,4 +132,20 @@ scratch_buffer_set_array_size (struct scratch_buffer *buffer,
                         (buffer, nelem, size));
 }
 
+/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block,
+   deallocating *BUFFER if it was heap-allocated.  SIZE must be at
+   most *BUFFER's size.  Return NULL (setting errno) on memory
+   exhaustion.  */
+void *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer,
+                                     size_t size);
+libc_hidden_proto (__libc_scratch_buffer_dupfree)
+
+/* Alias for __libc_scratch_dupfree.  */
+static __always_inline void *
+scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size)
+{
+  void *r = __libc_scratch_buffer_dupfree (buffer, size);
+  return __glibc_likely (r != NULL) ? r : NULL;
+}
+
 #endif /* _SCRATCH_BUFFER_H */
diff --git a/lib/malloc/scratch_buffer_dupfree.c b/lib/malloc/scratch_buffer_dupfree.c
new file mode 100644 (file)
index 0000000..5561e99
--- /dev/null
@@ -0,0 +1,41 @@
+/* Variable-sized buffer with on-stack default allocation.
+   Copyright (C) 2020 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
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _LIBC
+# include <libc-config.h>
+#endif
+
+#include <scratch_buffer.h>
+#include <string.h>
+
+void *
+__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size)
+{
+  void *data = buffer->data;
+  if (data == buffer->__space.__c)
+    {
+      void *copy = malloc (size);
+      return copy != NULL ? memcpy (copy, data, size) : NULL;
+    }
+  else
+    {
+      void *copy = realloc (data, size);
+      return copy != NULL ? copy : data;
+    }
+}
+libc_hidden_def (__libc_scratch_buffer_dupfree)
index 4f9a72581f1aa61b9d4b71487018d1e343f86aa5..7eedae7cc65fc54eb34b1f2dc06c98e7b0c6c81f 100644 (file)
@@ -4,6 +4,7 @@ Variable-sized buffer with on-stack default allocation.
 Files:
 lib/scratch_buffer.h
 lib/malloc/scratch_buffer.h
+lib/malloc/scratch_buffer_dupfree.c
 lib/malloc/scratch_buffer_grow.c
 lib/malloc/scratch_buffer_grow_preserve.c
 lib/malloc/scratch_buffer_set_array_size.c
@@ -17,7 +18,8 @@ stddef
 configure.ac:
 
 Makefile.am:
-lib_SOURCES += malloc/scratch_buffer_grow.c \
+lib_SOURCES += malloc/scratch_buffer_dupfree.c \
+               malloc/scratch_buffer_grow.c \
                malloc/scratch_buffer_grow_preserve.c \
                malloc/scratch_buffer_set_array_size.c