aboutsummaryrefslogtreecommitdiff
path: root/malloc/scratch_buffer_dupfree.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2020-12-28 09:50:23 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-01-05 11:33:16 -0300
commit448a256359e951fd2e81ccb2926e3f2b1d7a09de (patch)
treea0eb770538013c697f378a35cc035f033381d44f /malloc/scratch_buffer_dupfree.c
parent47f43160953677faf33853359ee7b973dc487139 (diff)
downloadglibc-448a256359e951fd2e81ccb2926e3f2b1d7a09de.zip
glibc-448a256359e951fd2e81ccb2926e3f2b1d7a09de.tar.gz
glibc-448a256359e951fd2e81ccb2926e3f2b1d7a09de.tar.bz2
malloc: Add scratch_buffer_dupfree
It returns a copy of the buffer up to a defined size. It will be used on realpath sync with gnulib.
Diffstat (limited to 'malloc/scratch_buffer_dupfree.c')
-rw-r--r--malloc/scratch_buffer_dupfree.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/malloc/scratch_buffer_dupfree.c b/malloc/scratch_buffer_dupfree.c
new file mode 100644
index 0000000..07363b9
--- /dev/null
+++ b/malloc/scratch_buffer_dupfree.c
@@ -0,0 +1,41 @@
+/* Variable-sized buffer with on-stack default allocation.
+ Copyright (C) 2020-2021 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)