summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Documentation/technical/api-argv-array.txt8
-rw-r--r--argv-array.c20
-rw-r--r--argv-array.h2
3 files changed, 30 insertions, 0 deletions
diff --git a/Documentation/technical/api-argv-array.txt b/Documentation/technical/api-argv-array.txt
index 1b7d8f140c..6b97d6db74 100644
--- a/Documentation/technical/api-argv-array.txt
+++ b/Documentation/technical/api-argv-array.txt
@@ -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.
diff --git a/argv-array.c b/argv-array.c
index 0b5f8898a1..aab50d6a97 100644
--- a/argv-array.c
+++ b/argv-array.c
@@ -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);
+ }
+}
diff --git a/argv-array.h b/argv-array.h
index b93a69c36c..b3ef351b44 100644
--- a/argv-array.h
+++ b/argv-array.h
@@ -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 */