Skip to content

Commit df7428e

Browse files
flyingflogitster
authored andcommitted
Add argv_array_detach and argv_array_free_detached
Allow detaching of ownership of the argv_array's contents and add a function to free those detached argv_arrays later. This makes it possible to use argv_array efficiently with the exiting struct child_process which only contains a member char **argv. Add to documentation. Signed-off-by: Florian Achleitner <[email protected]> Acked-by: David Michael Barr <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd871b9 commit df7428e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Documentation/technical/api-argv-array.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ Functions
4949
`argv_array_clear`::
5050
Free all memory associated with the array and return it to the
5151
initial, empty state.
52+
53+
`argv_array_detach`::
54+
Detach the argv array from the `struct argv_array`, transfering
55+
ownership of the allocated array and strings.
56+
57+
`argv_array_free_detached`::
58+
Free the memory allocated by a `struct argv_array` that was later
59+
detached and is now no longer needed.

argv-array.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@ void argv_array_clear(struct argv_array *array)
5959
}
6060
argv_array_init(array);
6161
}
62+
63+
const char **argv_array_detach(struct argv_array *array, int *argc)
64+
{
65+
const char **argv =
66+
array->argv == empty_argv || array->argc == 0 ? NULL : array->argv;
67+
if (argc)
68+
*argc = array->argc;
69+
argv_array_init(array);
70+
return argv;
71+
}
72+
73+
void argv_array_free_detached(const char **argv)
74+
{
75+
if (argv) {
76+
int i;
77+
for (i = 0; argv[i]; i++)
78+
free((char **)argv[i]);
79+
free(argv);
80+
}
81+
}

argv-array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ __attribute__((format (printf,2,3)))
1717
void argv_array_pushf(struct argv_array *, const char *fmt, ...);
1818
void argv_array_pushl(struct argv_array *, ...);
1919
void argv_array_clear(struct argv_array *);
20+
const char **argv_array_detach(struct argv_array *array, int *argc);
21+
void argv_array_free_detached(const char **argv);
2022

2123
#endif /* ARGV_ARRAY_H */

0 commit comments

Comments
 (0)