[Libguestfs] [libnbd PATCH 5/6] vector: Add vector_duplicate function

Laszlo Ersek lersek at redhat.com
Wed Mar 1 11:40:26 UTC 2023


From: "Richard W.M. Jones" <rjones at redhat.com>

Original commit message:

Duplicate a vector, optionally in-place.

Porting notes: this port of nbdkit commit 78b72746e547 ("vector: Add
vector_duplicate function", 2021-08-22) includes:

- The renaming of vector fields from nbdkit commit 0b0eece73f04
  ("common/utils/vector: Rename `size` to `len`", 2021-11-07); otherwise,
  the patch wouldn't compile.

- The effect of libnbd commit 8fb8ffb53477 ("build: Silence some cppcheck
  warnings", 2022-11-02), which added "__attribute__((__unused__))" to the
  other vector APIs.

- The effect of libnbd commit b5101fbc59cb ("use space consistently in
  function and function-like macro invocations", 2023-02-22), which
  inserted a space between "__attribute__" and "((__unused__))".

Signed-off-by: Laszlo Ersek <lersek at redhat.com>
(cherry picked from nbdkit commit 78b72746e547150290c2b0f2fe3ad5fe69817904)
---
 common/utils/vector.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/common/utils/vector.h b/common/utils/vector.h
index bdba02f7ebee..d0b250db87fc 100644
--- a/common/utils/vector.h
+++ b/common/utils/vector.h
@@ -181,6 +181,23 @@
                     (void *) compare);                                  \
   }                                                                     \
                                                                         \
+  /* Make a new vector with the same elements. */                       \
+  static inline int __attribute__ ((__unused__))                        \
+  name##_duplicate (name *v, name *copy)                                \
+  {                                                                     \
+    /* Note it's allowed for v and copy to be the same pointer. */      \
+    type *vptr = v->ptr;                                                \
+    type *newptr;                                                       \
+    size_t len = v->len * sizeof (type);                                \
+                                                                        \
+    newptr = malloc (len);                                              \
+    if (newptr == NULL) return -1;                                      \
+    memcpy (newptr, vptr, len);                                         \
+    copy->ptr = newptr;                                                 \
+    copy->len = copy->cap = v->len;                                     \
+    return 0;                                                           \
+  }                                                                     \
+                                                                        \
   /* End with duplicate declaration, so callers must supply ';'. */     \
   struct name
 



More information about the Libguestfs mailing list