Skip to content

Commit

Permalink
Add argv_array_detach and argv_array_free_detached
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
flyingflo authored and gitster committed Oct 7, 2012
1 parent fd871b9 commit df7428e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Documentation/technical/api-argv-array.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ Functions
`argv_array_clear`::
Free all memory associated with the array and return it to the
initial, empty state.

`argv_array_detach`::
Detach the argv array from the `struct argv_array`, transfering
ownership of the allocated array and strings.

`argv_array_free_detached`::
Free the memory allocated by a `struct argv_array` that was later
detached and is now no longer needed.
20 changes: 20 additions & 0 deletions argv-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,23 @@ void argv_array_clear(struct argv_array *array)
}
argv_array_init(array);
}

const char **argv_array_detach(struct argv_array *array, int *argc)
{
const char **argv =
array->argv == empty_argv || array->argc == 0 ? NULL : array->argv;
if (argc)
*argc = array->argc;
argv_array_init(array);
return argv;
}

void argv_array_free_detached(const char **argv)
{
if (argv) {
int i;
for (i = 0; argv[i]; i++)
free((char **)argv[i]);
free(argv);
}
}
2 changes: 2 additions & 0 deletions argv-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ __attribute__((format (printf,2,3)))
void argv_array_pushf(struct argv_array *, const char *fmt, ...);
void argv_array_pushl(struct argv_array *, ...);
void argv_array_clear(struct argv_array *);
const char **argv_array_detach(struct argv_array *array, int *argc);
void argv_array_free_detached(const char **argv);

#endif /* ARGV_ARRAY_H */

0 comments on commit df7428e

Please sign in to comment.