]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Add scratch_buffer_dupfree
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 28 Dec 2020 12:50:23 +0000 (09:50 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 5 Jan 2021 14:33:16 +0000 (11:33 -0300)
It returns a copy of the buffer up to a defined size.  It will be used
on realpath sync with gnulib.

include/scratch_buffer.h
malloc/Makefile
malloc/Versions
malloc/scratch_buffer_dupfree.c [new file with mode: 0644]
malloc/tst-scratch_buffer.c

index f51456444f0711b8f37fc3d94d90f2e504391e9f..36d0bef4bb1691f8cda746e3c002cc00a69877c2 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 */
index 7f7ce633e3386f39eb7996b176473e25c33b1ffb..583bbefb0d1bc948411c93cdd3885452d55be633 100644 (file)
@@ -74,6 +74,7 @@ tests-exclude-mcheck = tst-mcheck tst-malloc-usable \
 tests-mcheck = $(filter-out $(tests-exclude-mcheck),$(tests))
 
 routines = malloc morecore mcheck mtrace obstack reallocarray \
+  scratch_buffer_dupfree \
   scratch_buffer_grow scratch_buffer_grow_preserve \
   scratch_buffer_set_array_size \
   dynarray_at_failure \
index 94c8ba804048aa44f15aa30ba6b4fe78802f34c6..6693c46ee231e22fbb415d094e3d9ab8d19bff36 100644 (file)
@@ -75,6 +75,7 @@ libc {
     __libc_thread_freeres;
 
     # struct scratch_buffer support
+    __libc_scratch_buffer_dupfree;
     __libc_scratch_buffer_grow;
     __libc_scratch_buffer_grow_preserve;
     __libc_scratch_buffer_set_array_size;
diff --git a/malloc/scratch_buffer_dupfree.c b/malloc/scratch_buffer_dupfree.c
new file mode 100644 (file)
index 0000000..07363b9
--- /dev/null
@@ -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)
index 8ea07e8d5e698d4aa6b3c42da15716569fc03b65..6e521c78bfeee28f742cf9447195806826d79e91 100644 (file)
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <array_length.h>
 #include <scratch_buffer.h>
+#include <support/check.h>
+#include <support/support.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
@@ -148,8 +151,27 @@ do_test (void)
          && array_size_must_fail (4, ((size_t)-1) / 4)))
        return 1;
   }
+  {
+    struct scratch_buffer buf;
+    scratch_buffer_init (&buf);
+    memset (buf.data, '@', buf.length);
+
+    size_t sizes[] = { 16, buf.length, buf.length + 16 };
+    for (int i = 0; i < array_length (sizes); i++)
+      {
+        /* The extra size is unitialized through realloc.  */
+        size_t l = sizes[i] > buf.length ? sizes[i] : buf.length;
+        void *r = scratch_buffer_dupfree (&buf, l);
+        void *c = xmalloc (l);
+        memset (c, '@', l);
+        TEST_COMPARE_BLOB (r, l, buf.data, l);
+        free (r);
+        free (c);
+      }
+
+    scratch_buffer_free (&buf);
+  }
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>