vector: introduce DEFINE_POINTER_VECTOR_TYPE()
(Commit message below adapted from libnbd commit
fda38988b86b, "vector:
introduce DEFINE_POINTER_VECTOR_TYPE()", 2023-02-28).
The "name##_iter" function is used 5 times in nbdkit as follows:
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Furthermore, in those 5 cases, the freeing of the vector's strings is
immediately followed by the release of the vector's array-of-pointers too.
(This additional step is not expressed consistently across nbdkit: it's
sometimes spelled as free(vec.ptr), sometimes as
string_vector_reset(&vec).)
Introduce "name##_empty", which performs both steps at the same time.
Expose the "name##_empty" function definition with a new, separate macro:
ADD_VECTOR_EMPTY_METHOD(). The existent DEFINE_VECTOR_TYPE() macro permits
such element types that are not pointers, or are pointers to const- and/or
volatile-qualified objects. Whereas "name##_empty" requires that the
elements be pointers to dynamically allocated, non-const, non-volatile
objects.
Add DEFINE_POINTER_VECTOR_TYPE() that expands to both DEFINE_VECTOR_TYPE()
and the additive ADD_VECTOR_EMPTY_METHOD().
(
For example, after
typedef char foobar[5];
the following compiles (as expected):
DEFINE_VECTOR_TYPE (foobar_vector, foobar);
and the following fails to build (as expected):
DEFINE_POINTER_VECTOR_TYPE (foobar_vector_bad, foobar);
with the diagnostics including
> In function ‘foobar_vector_bad_empty’:
> error: size of array ‘_vector_contains_pointers1’ is negative
)
Switch "string_vector" to DEFINE_POINTER_VECTOR_TYPE(). The 5
string_vector_iter() call sites continue working; they will be converted
to string_vector_empty() in a subsequent patch.
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from libnbd commit
fda38988b86bb133b56fc6251b2d581e809b3b67)
Message-Id: <
20230303075145.177338-9-lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
Acked-by: Eric Blake <eblake@redhat.com>