From: Taylor Blau Date: Wed, 25 Feb 2026 00:21:01 GMT Subject: [RFC PATCH 02/14] strvec: introduce `strvec_init_alloc()` Message-ID: <50efbbb0fe8d897d7c4cd51489af4cb4c4c49d02.1771978829.git.me@ttaylorr.com> In-Reply-To: When the caller knows upfront how many elements will be pushed onto a `strvec`, it is useful to pre-allocate enough space in the array to fit that many elements (and one additional slot to store NULL, indicating the end of the list.) Introduce `strvec_init_alloc()`, which allocates the backing array large enough to hold `alloc` elements and the termination marker without further reallocation. Signed-off-by: Taylor Blau --- strvec.c | 7 +++++++ strvec.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/strvec.c b/strvec.c index f8de79f5579..f7f32a53b56 100644 --- a/strvec.c +++ b/strvec.c @@ -10,6 +10,13 @@ void strvec_init(struct strvec *array) memcpy(array, &blank, sizeof(*array)); } +void strvec_init_alloc(struct strvec *array, size_t alloc) +{ + CALLOC_ARRAY(array->v, st_add(alloc, 1)); + array->nr = 0; + array->alloc = alloc + 1; +} + void strvec_push_nodup(struct strvec *array, char *value) { if (array->v == empty_strvec) diff --git a/strvec.h b/strvec.h index f74e061e141..34cb1f939f0 100644 --- a/strvec.h +++ b/strvec.h @@ -43,6 +43,11 @@ struct strvec { */ void strvec_init(struct strvec *); +/* + * Initializes an array large enough to store `alloc` elements. + */ +void strvec_init_alloc(struct strvec *, size_t alloc); + /* Push a copy of a string onto the end of the array. */ const char *strvec_push(struct strvec *, const char *); -- 2.53.0.185.g29bc4dff628