Blame SOURCES/0021-common-utils-vector-Rename-alloc-to-cap.patch

3beab2
From 24e2694b302f6602e0fc7808a53a766cb983dfb4 Mon Sep 17 00:00:00 2001
7084e2
From: Nir Soffer <nsoffer@redhat.com>
7084e2
Date: Fri, 5 Nov 2021 22:59:38 +0200
7084e2
Subject: [PATCH] common/utils/vector: Rename `alloc` to `cap`
7084e2
7084e2
The `alloc` field is the maximum number of items you can append to a
7084e2
vector before it need to be resized. This may confuse users with the
7084e2
size of the `ptr` array which is `alloc * itemsize`. Rename to "cap",
7084e2
common term for this property in many languages (e.g C++, Rust, Go).
7084e2
7084e2
Tested with "make check". Tests requiring root or external libraries
7084e2
(vddk) not tested.
7084e2
7084e2
Ported from libnbd commit e3c7f02a2a844295564c832108d36c939c4e4ecf.
7084e2
7084e2
(cherry picked from commit 75a44237c4463524dbf7951bb62b59c373c85865)
7084e2
---
7084e2
 common/allocators/malloc.c | 24 ++++++++++++------------
7084e2
 common/utils/vector.c      | 16 ++++++++--------
7084e2
 common/utils/vector.h      | 12 ++++++------
7084e2
 plugins/vddk/reexec.c      |  2 +-
7084e2
 4 files changed, 27 insertions(+), 27 deletions(-)
7084e2
7084e2
diff --git a/common/allocators/malloc.c b/common/allocators/malloc.c
7084e2
index 59409c24..f7474465 100644
7084e2
--- a/common/allocators/malloc.c
7084e2
+++ b/common/allocators/malloc.c
7084e2
@@ -88,16 +88,16 @@ extend (struct m_alloc *ma, uint64_t new_size)
7084e2
   ACQUIRE_WRLOCK_FOR_CURRENT_SCOPE (&ma->lock);
7084e2
   size_t old_size, n;
7084e2
 
7084e2
-  if (ma->ba.alloc < new_size) {
7084e2
-    old_size = ma->ba.alloc;
7084e2
-    n = new_size - ma->ba.alloc;
7084e2
+  if (ma->ba.cap < new_size) {
7084e2
+    old_size = ma->ba.cap;
7084e2
+    n = new_size - ma->ba.cap;
7084e2
 
7084e2
 #ifdef HAVE_MUNLOCK
7084e2
     /* Since the memory might be moved by realloc, we must unlock the
7084e2
      * original array.
7084e2
      */
7084e2
     if (ma->use_mlock)
7084e2
-      munlock (ma->ba.ptr, ma->ba.alloc);
7084e2
+      munlock (ma->ba.ptr, ma->ba.cap);
7084e2
 #endif
7084e2
 
7084e2
     if (bytearray_reserve (&ma->ba, n) == -1) {
7084e2
@@ -110,7 +110,7 @@ extend (struct m_alloc *ma, uint64_t new_size)
7084e2
 
7084e2
 #ifdef HAVE_MLOCK
7084e2
     if (ma->use_mlock) {
7084e2
-      if (mlock (ma->ba.ptr, ma->ba.alloc) == -1) {
7084e2
+      if (mlock (ma->ba.ptr, ma->ba.cap) == -1) {
7084e2
         nbdkit_error ("allocator=malloc: mlock: %m");
7084e2
         return -1;
7084e2
       }
7084e2
@@ -138,11 +138,11 @@ m_alloc_read (struct allocator *a, void *buf,
7084e2
   /* Avoid reading beyond the end of the allocated array.  Return
7084e2
    * zeroes for that part.
7084e2
    */
7084e2
-  if (offset >= ma->ba.alloc)
7084e2
+  if (offset >= ma->ba.cap)
7084e2
     memset (buf, 0, count);
7084e2
-  else if (offset + count > ma->ba.alloc) {
7084e2
-    memcpy (buf, ma->ba.ptr + offset, ma->ba.alloc - offset);
7084e2
-    memset (buf + ma->ba.alloc - offset, 0, offset + count - ma->ba.alloc);
7084e2
+  else if (offset + count > ma->ba.cap) {
7084e2
+    memcpy (buf, ma->ba.ptr + offset, ma->ba.cap - offset);
7084e2
+    memset (buf + ma->ba.cap - offset, 0, offset + count - ma->ba.cap);
7084e2
   }
7084e2
   else
7084e2
     memcpy (buf, ma->ba.ptr + offset, count);
7084e2
@@ -191,9 +191,9 @@ m_alloc_zero (struct allocator *a, uint64_t count, uint64_t offset)
7084e2
   /* Try to avoid extending the array, since the unallocated part
7084e2
    * always reads as zero.
7084e2
    */
7084e2
-  if (offset < ma->ba.alloc) {
7084e2
-    if (offset + count > ma->ba.alloc)
7084e2
-      memset (ma->ba.ptr + offset, 0, ma->ba.alloc - offset);
7084e2
+  if (offset < ma->ba.cap) {
7084e2
+    if (offset + count > ma->ba.cap)
7084e2
+      memset (ma->ba.ptr + offset, 0, ma->ba.cap - offset);
7084e2
     else
7084e2
       memset (ma->ba.ptr + offset, 0, count);
7084e2
   }
7084e2
diff --git a/common/utils/vector.c b/common/utils/vector.c
7084e2
index 7df17e1b..a4b43ce7 100644
7084e2
--- a/common/utils/vector.c
7084e2
+++ b/common/utils/vector.c
7084e2
@@ -41,21 +41,21 @@ int
7084e2
 generic_vector_reserve (struct generic_vector *v, size_t n, size_t itemsize)
7084e2
 {
7084e2
   void *newptr;
7084e2
-  size_t reqalloc, newalloc;
7084e2
+  size_t reqcap, newcap;
7084e2
 
7084e2
-  reqalloc = v->alloc + n;
7084e2
-  if (reqalloc < v->alloc)
7084e2
+  reqcap = v->cap + n;
7084e2
+  if (reqcap < v->cap)
7084e2
     return -1; /* overflow */
7084e2
 
7084e2
-  newalloc = (v->alloc * 3 + 1) / 2;
7084e2
+  newcap = (v->cap * 3 + 1) / 2;
7084e2
 
7084e2
-  if (newalloc < reqalloc)
7084e2
-    newalloc = reqalloc;
7084e2
+  if (newcap < reqcap)
7084e2
+    newcap = reqcap;
7084e2
 
7084e2
-  newptr = realloc (v->ptr, newalloc * itemsize);
7084e2
+  newptr = realloc (v->ptr, newcap * itemsize);
7084e2
   if (newptr == NULL)
7084e2
     return -1;
7084e2
   v->ptr = newptr;
7084e2
-  v->alloc = newalloc;
7084e2
+  v->cap = newcap;
7084e2
   return 0;
7084e2
 }
7084e2
diff --git a/common/utils/vector.h b/common/utils/vector.h
7084e2
index f6a0af78..782dcba6 100644
7084e2
--- a/common/utils/vector.h
7084e2
+++ b/common/utils/vector.h
7084e2
@@ -86,7 +86,7 @@
7084e2
   struct name {                                                         \
7084e2
     type *ptr;                 /* Pointer to array of items. */         \
7084e2
     size_t size;               /* Number of valid items in the array. */ \
7084e2
-    size_t alloc;              /* Number of items allocated. */         \
7084e2
+    size_t cap;                /* Maximum number of items. */           \
7084e2
   };                                                                    \
7084e2
   typedef struct name name;                                             \
7084e2
                                                                         \
7084e2
@@ -106,7 +106,7 @@
7084e2
   name##_insert (name *v, type elem, size_t i)                          \
7084e2
   {                                                                     \
7084e2
     assert (i <= v->size);                                              \
7084e2
-    if (v->size >= v->alloc) {                                          \
7084e2
+    if (v->size >= v->cap) {                                            \
7084e2
       if (name##_reserve (v, 1) == -1) return -1;                       \
7084e2
     }                                                                   \
7084e2
     memmove (&v->ptr[i+1], &v->ptr[i], (v->size-i) * sizeof (elem));    \
7084e2
@@ -137,7 +137,7 @@
7084e2
   {                                                                     \
7084e2
     free (v->ptr);                                                      \
7084e2
     v->ptr = NULL;                                                      \
7084e2
-    v->size = v->alloc = 0;                                             \
7084e2
+    v->size = v->cap = 0;                                               \
7084e2
   }                                                                     \
7084e2
                                                                         \
7084e2
   /* Iterate over the vector, calling f() on each element. */           \
7084e2
@@ -181,17 +181,17 @@
7084e2
     if (newptr == NULL) return -1;                                      \
7084e2
     memcpy (newptr, vptr, len);                                         \
7084e2
     copy->ptr = newptr;                                                 \
7084e2
-    copy->size = copy->alloc = v->size;                                 \
7084e2
+    copy->size = copy->cap = v->size;                                   \
7084e2
     return 0;                                                           \
7084e2
   }                                                                     \
7084e2
                                                                         \
7084e2
 
7084e2
-#define empty_vector { .ptr = NULL, .size = 0, .alloc = 0 }
7084e2
+#define empty_vector { .ptr = NULL, .size = 0, .cap = 0 }
7084e2
 
7084e2
 struct generic_vector {
7084e2
   void *ptr;
7084e2
   size_t size;
7084e2
-  size_t alloc;
7084e2
+  size_t cap;
7084e2
 };
7084e2
 
7084e2
 extern int generic_vector_reserve (struct generic_vector *v,
7084e2
diff --git a/plugins/vddk/reexec.c b/plugins/vddk/reexec.c
7084e2
index 46acdb62..9e87025e 100644
7084e2
--- a/plugins/vddk/reexec.c
7084e2
+++ b/plugins/vddk/reexec.c
7084e2
@@ -116,7 +116,7 @@ perform_reexec (const char *env, const char *prepend)
7084e2
       nbdkit_error ("realloc: %m");
7084e2
       exit (EXIT_FAILURE);
7084e2
     }
7084e2
-    r = read (fd, buf.ptr + buf.size, buf.alloc - buf.size);
7084e2
+    r = read (fd, buf.ptr + buf.size, buf.cap - buf.size);
7084e2
     if (r == -1) {
7084e2
       nbdkit_error ("read: %s: %m", cmdline_file);
7084e2
       exit (EXIT_FAILURE);
7084e2
-- 
7084e2
2.31.1
7084e2