Skip to content

Commit ce68178

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0593c1e commit ce68178

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

strvec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void strvec_init(struct strvec *array)
1010
memcpy(array, &blank, sizeof(*array));
1111
}
1212

13-
static void strvec_push_nodup(struct strvec *array, const char *value)
13+
void strvec_push_nodup(struct strvec *array, char *value)
1414
{
1515
if (array->v == empty_strvec)
1616
array->v = NULL;

strvec.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ void strvec_init(struct strvec *);
4646
/* Push a copy of a string onto the end of the array. */
4747
const char *strvec_push(struct strvec *, const char *);
4848

49+
/* Push an allocated string onto the end of the array, taking ownership. */
50+
void strvec_push_nodup(struct strvec *array, char *value);
51+
4952
/**
5053
* Format a string and push it onto the end of the array. This is a
5154
* convenience wrapper combining `strbuf_addf` and `strvec_push`.

0 commit comments

Comments
 (0)