[PATCH 2/6] app/testpmd: register driver specific commands
Ferruh Yigit
ferruh.yigit at amd.com
Mon May 23 20:10:23 CEST 2022
On 5/23/2022 8:10 AM, David Marchand wrote:
> +int
> +init_cmdline(void)
> +{
> + struct testpmd_commands *c;
> + cmdline_parse_ctx_t *ctx;
> + unsigned int count = 0;
> + unsigned int i;
> +
> + /* initialize non-constant commands */
> + cmd_set_fwd_mode_init();
> + cmd_set_fwd_retry_mode_init();
> +
> + main_ctx = NULL;
> + for (i = 0; builtin_ctx[i] != NULL; i++) {
> + ctx = realloc(main_ctx, (count + i + 1) * sizeof(*ctx));
> + if (ctx == NULL)
> + goto err;
> + main_ctx = ctx;
Instead of 'realloc()', check and assign pointer in each entry, what do
you think about first calculate the size and alloc once, both for
'builtin_ctx' & driver specific command?
But of course trade off is to loop twice in that case.
> + main_ctx[count + i] = builtin_ctx[i];
> + }
no need to use 'count' in this loop, since it is always '0'.
> + count += i;
> +
> + TAILQ_FOREACH(c, &commands_head, next) {
> + for (i = 0; c->commands[i].ctx != NULL; i++) {
> + ctx = realloc(main_ctx, (count + i + 1) * sizeof(*ctx));
> + if (ctx == NULL)
> + goto err;
> + main_ctx = ctx;
> + main_ctx[count + i] = c->commands[i].ctx;
> + }
> + count += i;
> + }
> +
> + /* cmdline expects a NULL terminated array */
> + ctx = realloc(main_ctx, (count + 1) * sizeof(*ctx));
> + if (ctx == NULL)
> + goto err;
> + main_ctx = ctx;
> + main_ctx[count] = NULL;
> + count += 1;
Above block also can be avoided if size calculated in advance.
> +
> + return 0;
> +err:
> + free(main_ctx);
> + return -1;
> +}
> +
More information about the dev
mailing list