On Sat, Oct 30, 2021 at 07:56:07PM +0300, Nir Soffer wrote: > The `alloc` field is the maximum number of items you can append to a > vector before it need to be resized. This may confuse users with the > size of the `ptr` array which is `alloc * itemsize`. Rename to "cap", > common term for this property in many languages (e.g C++, Rust, Go).
cap is short for "capacity"? I didn't know about this convention but I see that C++ std::vector does indeed call this field "__end_cap". (Also weirdly it stores the vector as a pointer to the beginning and end - I guess that's to make C++ iterators easier?) I suspect this (and the next patch) is going to break callers since they can touch these fields directly ... > Signed-off-by: Nir Soffer <[email protected]> > --- > common/utils/vector.c | 16 ++++++++-------- > common/utils/vector.h | 10 +++++----- > lib/crypto.c | 4 ++-- > 3 files changed, 15 insertions(+), 15 deletions(-) > > diff --git a/common/utils/vector.c b/common/utils/vector.c > index 7df17e1..a4b43ce 100644 > --- a/common/utils/vector.c > +++ b/common/utils/vector.c > @@ -41,21 +41,21 @@ int > generic_vector_reserve (struct generic_vector *v, size_t n, size_t itemsize) > { > void *newptr; > - size_t reqalloc, newalloc; > + size_t reqcap, newcap; > > - reqalloc = v->alloc + n; > - if (reqalloc < v->alloc) > + reqcap = v->cap + n; > + if (reqcap < v->cap) > return -1; /* overflow */ > > - newalloc = (v->alloc * 3 + 1) / 2; > + newcap = (v->cap * 3 + 1) / 2; > > - if (newalloc < reqalloc) > - newalloc = reqalloc; > + if (newcap < reqcap) > + newcap = reqcap; > > - newptr = realloc (v->ptr, newalloc * itemsize); > + newptr = realloc (v->ptr, newcap * itemsize); > if (newptr == NULL) > return -1; > v->ptr = newptr; > - v->alloc = newalloc; > + v->cap = newcap; > return 0; > } > diff --git a/common/utils/vector.h b/common/utils/vector.h > index 69aad8d..3fc8b68 100644 > --- a/common/utils/vector.h > +++ b/common/utils/vector.h > @@ -86,7 +86,7 @@ > struct name { \ > type *ptr; /* Pointer to array of items. */ \ > size_t size; /* Number of valid items in the array. */ \ > - size_t alloc; /* Number of items allocated. */ \ > + size_t cap; /* Maximum number of items. */ \ > }; \ > typedef struct name name; \ > \ > @@ -106,7 +106,7 @@ > name##_insert (name *v, type elem, size_t i) \ > { \ > assert (i <= v->size); \ > - if (v->size >= v->alloc) { \ > + if (v->size >= v->cap) { \ > if (name##_reserve (v, 1) == -1) return -1; \ > } \ > memmove (&v->ptr[i+1], &v->ptr[i], (v->size-i) * sizeof (elem)); \ > @@ -137,7 +137,7 @@ > { \ > free (v->ptr); \ > v->ptr = NULL; \ > - v->size = v->alloc = 0; \ > + v->size = v->cap = 0; \ > } \ > \ > /* Iterate over the vector, calling f() on each element. */ \ > @@ -168,12 +168,12 @@ > (void *) compare); \ > } > > -#define empty_vector { .ptr = NULL, .size = 0, .alloc = 0 } > +#define empty_vector { .ptr = NULL, .size = 0, .cap = 0 } > > struct generic_vector { > void *ptr; > size_t size; > - size_t alloc; > + size_t cap; > }; and indeed ... > extern int generic_vector_reserve (struct generic_vector *v, > diff --git a/lib/crypto.c b/lib/crypto.c > index a9b3789..f570db3 100644 > --- a/lib/crypto.c > +++ b/lib/crypto.c > @@ -141,7 +141,7 @@ nbd_unlocked_get_tls_username (struct nbd_handle *h) > } > > for (;;) { > - if (getlogin_r (str.ptr, str.alloc) == 0) { > + if (getlogin_r (str.ptr, str.cap) == 0) { > return str.ptr; > } > else if (errno != ERANGE) { > @@ -150,7 +150,7 @@ nbd_unlocked_get_tls_username (struct nbd_handle *h) > return NULL; > } > /* Try again with a larger buffer. */ > - if (string_reserve (&str, str.alloc == 0 ? 16 : str.alloc * 2) == -1) { > + if (string_reserve (&str, str.cap == 0 ? 16 : str.cap * 2) == -1) { > set_error (errno, "realloc"); > free (str.ptr); > return NULL; Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v _______________________________________________ Libguestfs mailing list [email protected] https://listman.redhat.com/mailman/listinfo/libguestfs
