Skip to content

Commit 770feda

Browse files
avargitster
authored andcommitted
string-list.[ch]: add a string_list_init_{nodup,dup}()
In order to use the new "memcpy() a 'blank' struct on the stack" pattern for string_list_init(), and to make the macro initialization consistent with the function initialization introduce two new string_list_init_{nodup,dup}() functions. These are like the old string_list_init() when called with a false and true second argument, respectively. I think this not only makes things more consistent, but also easier to read. I often had to lookup what the ", 0)" or ", 1)" in these invocations meant, now it's right there in the function name, and corresponds to the macros. A subsequent commit will convert existing API users to this pattern, but as this is a very common API let's leave a compatibility function in place for later removal. This intermediate state also proves that the compatibility function works. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce93a4c commit 770feda

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

string-list.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#include "cache.h"
22
#include "string-list.h"
33

4+
void string_list_init_nodup(struct string_list *list)
5+
{
6+
struct string_list blank = STRING_LIST_INIT_NODUP;
7+
memcpy(list, &blank, sizeof(*list));
8+
}
9+
10+
void string_list_init_dup(struct string_list *list)
11+
{
12+
struct string_list blank = STRING_LIST_INIT_DUP;
13+
memcpy(list, &blank, sizeof(*list));
14+
}
15+
416
void string_list_init(struct string_list *list, int strdup_strings)
517
{
6-
memset(list, 0, sizeof(*list));
7-
list->strdup_strings = strdup_strings;
18+
if (strdup_strings)
19+
string_list_init_dup(list);
20+
else
21+
string_list_init_nodup(list);
822
}
923

1024
/* if there is no exact match, point to the index where the entry could be

string-list.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,15 @@ struct string_list {
9797
/* General functions which work with both sorted and unsorted lists. */
9898

9999
/**
100-
* Initialize the members of the string_list, set `strdup_strings`
101-
* member according to the value of the second parameter.
100+
* Initialize the members of a string_list pointer in the same way as
101+
* the corresponding `STRING_LIST_INIT_NODUP` and
102+
* `STRING_LIST_INIT_DUP` macros.
103+
*/
104+
void string_list_init_nodup(struct string_list *list);
105+
void string_list_init_dup(struct string_list *list);
106+
107+
/**
108+
* TODO remove: For compatibility with any in-flight older API users
102109
*/
103110
void string_list_init(struct string_list *list, int strdup_strings);
104111

0 commit comments

Comments
 (0)