strvec: declare the `strvec_push_nodup()` function globally
This function differs from `strvec_push()` in that it takes ownership of the allocated string that is passed as second argument. This is useful when appending elements to the string array that have been freshly allocated and serve no further other purpose after that. Without declaring this function globally, call sites would allocate the memory, only to have `strvec_push()` duplicate the string, and then the first copy would need to be released. Having this function globally avoids that kind of unnecessary work. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jul 13, 2024 at 21:08 UTC
ce68178a0ae222b71fe5f89f05dc7113267c079b
2 files changed
+4
-1
strvec.c
+1
-1
@@ -10,7 +10,7 @@ void strvec_init(struct strvec *array)
10
memcpy(array, &blank, sizeof(*array));
11
}
12
13
-static void strvec_push_nodup(struct strvec *array, const char *value)
13
+void strvec_push_nodup(struct strvec *array, char *value)
14
{
15
if (array->v == empty_strvec)
16
array->v = NULL;
strvec.h
+3
@@ -46,6 +46,9 @@ void strvec_init(struct strvec *);
46
/* Push a copy of a string onto the end of the array. */
47
const char *strvec_push(struct strvec *, const char *);
48
49
+/* Push an allocated string onto the end of the array, taking ownership. */
50
+void strvec_push_nodup(struct strvec *array, char *value);
51
+
52
/**
53
* Format a string and push it onto the end of the array. This is a
54
* convenience wrapper combining `strbuf_addf` and `strvec_push`.