[PATCH 01/23] virInsertElementsN: Rename 'add' argument

Peter Krempa pkrempa at redhat.com
Wed Aug 4 11:02:21 UTC 2021


The idea of @add was that the insersion/appension macros would allow
adding more than one element but this feature was never implemented.

'add' is nowadays used as a dummy variable consuming the result of the
VIR_TYPEMATCH compile time check.

Make it obvious that we don't use 'add' by renaming it to
'typematchDummy', marking it as unused and replacing all occurences
where the value was used by literal '1'.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/viralloc.c | 31 ++++++++++++++++---------------
 src/util/viralloc.h | 14 +-------------
 2 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index cd7ae9e7d1..6c76da8537 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -148,7 +148,7 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
  * @size:     the size of one element in bytes
  * @at:       index within array where new elements should be added, -1 for end
  * @countptr: variable tracking number of elements currently allocated
- * @add:      number of elements to add
+ * @typematchDummy: helper variable to consume results of compile time checks
  * @newelems: pointer to array of one or more new elements to move into
  *            place (the originals will be zeroed out if successful
  *            and if clearOriginal is true)
@@ -160,8 +160,8 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
  *            already* done that.
  *
  * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes
- * long, to be *countptr+add elements long, then appropriately move
- * the elements starting at ptrptr[at] up by add elements, copy the
+ * long, to be *countptr elements long, then appropriately move
+ * the elements starting at ptrptr[at] up by 1 elements, copy the
  * items from newelems into ptrptr[at], then store the address of
  * allocated memory in *ptrptr and the new size in *countptr.  If
  * newelems is NULL, the new elements at ptrptr[at] are instead filled
@@ -173,22 +173,23 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
 int
 virInsertElementsN(void *ptrptr, size_t size, size_t at,
                    size_t *countptr,
-                   size_t add, void *newelems,
+                   size_t typematchDummy G_GNUC_UNUSED,
+                   void *newelems,
                    bool clearOriginal, bool inPlace)
 {
     if (at == -1) {
         at = *countptr;
     } else if (at > *countptr) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("out of bounds index - count %zu at %zu add %zu"),
-                       *countptr, at, add);
+                       _("out of bounds index - count %zu at %zu"),
+                       *countptr, at);
         return -1;
     }

     if (inPlace) {
-        *countptr += add;
+        *countptr += 1;
     } else {
-        virExpandN(ptrptr, size, countptr, add);
+        virExpandN(ptrptr, size, countptr, 1);
     }

     /* memory was successfully re-allocated. Move up all elements from
@@ -197,19 +198,19 @@ virInsertElementsN(void *ptrptr, size_t size, size_t at,
      * from their original location. Remember that *countptr has
      * already been updated with new element count!
      */
-    if (at < *countptr - add) {
-        memmove(*(char**)ptrptr + (size * (at + add)),
+    if (at < *countptr - 1) {
+        memmove(*(char**)ptrptr + (size * (at + 1)),
                 *(char**)ptrptr + (size * at),
-                size * (*countptr - add - at));
+                size * (*countptr - 1 - at));
     }

     if (newelems) {
-        memcpy(*(char**)ptrptr + (size * at), newelems, size * add);
+        memcpy(*(char**)ptrptr + (size * at), newelems, size);
         if (clearOriginal)
-           memset((char*)newelems, 0, size * add);
-    } else if (inPlace || (at < *countptr - add)) {
+           memset((char*)newelems, 0, size);
+    } else if (inPlace || (at < *countptr - 1)) {
         /* NB: if inPlace, assume memory at the end wasn't initialized */
-        memset(*(char**)ptrptr + (size * at), 0, size * add);
+        memset(*(char**)ptrptr + (size * at), 0, size);
     }

     return 0;
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index 72e8f13bef..b637bc2ca4 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -43,7 +43,7 @@ void virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, size_t d
 void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
-                       size_t add, void *newelem,
+                       size_t typematchDummy, void *newelem,
                        bool clearOriginal, bool inPlace)
     G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
 int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
@@ -182,12 +182,6 @@ int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
  * VIR_INSERT_ELEMENT_INPLACE is identical, but assumes any necessary
  * memory re-allocation has already been done.
  *
- * Both functions need to send "1" as the "add" argument to
- * virInsertElementsN (which has the currently-unused capability of
- * inserting multiple items at once). We use this to our advantage by
- * replacing it with VIR_TYPECHECK(ptr, &newelem) so that we can be
- * assured ptr and &newelem are of compatible types.
- *
  * These macros are safe to use on arguments with side effects.
  *
  * Returns -1 on failure (with OOM error reported), 0 on success
@@ -224,12 +218,6 @@ int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
  * VIR_APPEND_ELEMENT_*INPLACE are identical, but assume any
  *   necessary memory re-allocation has already been done.
  *
- * VIR_APPEND_ELEMENT_* all need to send "1" as the "add" argument to
- * virInsertElementsN (which has the currently-unused capability of
- * inserting multiple items at once). We use this to our advantage by
- * replacing it with VIR_TYPECHECK(ptr, &newelem) so that we can be
- * assured ptr and &newelem are of compatible types.
- *
  * These macros are safe to use on arguments with side effects.
  *
  * Returns -1 on failure (with OOM error reported), 0 on success
-- 
2.31.1




More information about the libvir-list mailing list