* 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-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
$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
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
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
(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 */
--- /dev/null
+/* 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)
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
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