From cc3c79065c7db7d52384ff4b5ec0ace09ad5f35e Mon Sep 17 00:00:00 2001 From: Pawel Wodkowski Date: Mon, 16 Oct 2017 20:16:38 +0200 Subject: [PATCH] env: don't free DPDK eal arguments Documentation of rte_eal_init() lie. At least two parameters (huge-dir and file-prefix) are held and used internally trought the lifetime of application. Change-Id: Ie418b5f2b0d7fbea5c217e47a555a520d3256360 Signed-off-by: Pawel Wodkowski Reviewed-on: https://review.gerrithub.io/382677 Reviewed-by: Dariusz Stojaczyk Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp --- lib/env_dpdk/init.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/env_dpdk/init.c b/lib/env_dpdk/init.c index c33d6aa5d6..f2e8fee5c5 100644 --- a/lib/env_dpdk/init.c +++ b/lib/env_dpdk/init.c @@ -45,6 +45,9 @@ #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL -1 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK "0x1" +static char **eal_cmdline; +static int eal_cmdline_argcount; + static char * _sprintf_alloc(const char *format, ...) { @@ -133,10 +136,7 @@ spdk_free_args(char **args, int argcount) { int i; - assert(args != NULL); - for (i = 0; i < argcount; i++) { - assert(args[i] != NULL); free(args[i]); } @@ -164,17 +164,19 @@ spdk_push_arg(char *args[], int *argcount, char *arg) return tmp; } +static void +spdk_destruct_eal_cmdline(void) +{ + spdk_free_args(eal_cmdline, eal_cmdline_argcount); +} + + static int -spdk_build_eal_cmdline(const struct spdk_env_opts *opts, char **out[]) +spdk_build_eal_cmdline(const struct spdk_env_opts *opts) { int argcount = 0; char **args; - if (out == NULL) { - return -1; - } - - *out = NULL; args = NULL; /* set the program name */ @@ -250,28 +252,31 @@ spdk_build_eal_cmdline(const struct spdk_env_opts *opts, char **out[]) } #endif - *out = args; + eal_cmdline = args; + eal_cmdline_argcount = argcount; + if (atexit(spdk_destruct_eal_cmdline) != 0) { + fprintf(stderr, "Failed to register cleanup handler\n"); + } return argcount; } void spdk_env_init(const struct spdk_env_opts *opts) { - char **args = NULL; char **dpdk_args = NULL; - int argcount, i, rc; + int i, rc; int orig_optind; - argcount = spdk_build_eal_cmdline(opts, &args); - if (argcount <= 0) { + rc = spdk_build_eal_cmdline(opts); + if (rc < 0) { fprintf(stderr, "Invalid arguments to initialize DPDK\n"); exit(-1); } printf("Starting %s initialization...\n", rte_version()); printf("[ DPDK EAL parameters: "); - for (i = 0; i < argcount; i++) { - printf("%s ", args[i]); + for (i = 0; i < eal_cmdline_argcount; i++) { + printf("%s ", eal_cmdline[i]); } printf("]\n"); @@ -279,20 +284,19 @@ void spdk_env_init(const struct spdk_env_opts *opts) * before passing so we can still free the individual strings * correctly. */ - dpdk_args = calloc(argcount, sizeof(char *)); + dpdk_args = calloc(eal_cmdline_argcount, sizeof(char *)); if (dpdk_args == NULL) { fprintf(stderr, "Failed to allocate dpdk_args\n"); exit(-1); } - memcpy(dpdk_args, args, sizeof(char *) * argcount); + memcpy(dpdk_args, eal_cmdline, sizeof(char *) * eal_cmdline_argcount); fflush(stdout); orig_optind = optind; optind = 1; - rc = rte_eal_init(argcount, dpdk_args); + rc = rte_eal_init(eal_cmdline_argcount, dpdk_args); optind = orig_optind; - spdk_free_args(args, argcount); free(dpdk_args); if (rc < 0) {