From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4B704A04FF; Tue, 24 May 2022 12:53:22 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E164440140; Tue, 24 May 2022 12:53:21 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id A4B80400D6 for ; Tue, 24 May 2022 12:53:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653389600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wv5rW3lqWNDCj8wfPh9lTCfU0qSJFazWG/nmv+42PaI=; b=bwc0gnSmU2DZ8QIfPD6JE87HWfsLVEVn2ZS0wQmlUbhdbiG4bdDk5tw6OkdB8iOPJzmQe6 ovxcMgh6kYsun3JjfJJDWqCFIqZifk4wcRbzbC826ZXF/wI+50ORhtA6OJe32GjF/dy4TF a+ac4GvcYTWegqA+sRZbUcCJEH/XRow= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-364-s2peiR-7Oh67F1g7TY1Ddg-1; Tue, 24 May 2022 06:53:19 -0400 X-MC-Unique: s2peiR-7Oh67F1g7TY1Ddg-1 Received: by mail-lf1-f70.google.com with SMTP id br24-20020a056512401800b00477cb7c9a9dso6917730lfb.1 for ; Tue, 24 May 2022 03:53:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wv5rW3lqWNDCj8wfPh9lTCfU0qSJFazWG/nmv+42PaI=; b=BVomND7Xg9jowe0vqptdrC0IqRvPQqNqVo9U8ZPxwFYFzfHy9pCGVhbIIwpbhEODgt +xXqJ4JDaTu/7wnG17CRSSfYZV42B+zdrQdHn+tuanCKvZCYy7TkfkoR6wJQAomfuwHx s5wOtOo4t1W8ASbnN+TlVCJUmFuhGRZgZnOPKAUxtCtHEpS+5KXHoy6syk9cF3P0R1FE sJWRP9VdA78N+/e89dkkFDFnGds94oB49bUciKXioEIT/xToJbj9i5B1MD+jIwJzwUYl QnCAZfRg8XreoAcHsx4BIFNkqC+B4RhVPqAF81y7ZzFIU0HfLgFfGU3yZ/e0KFYFRtO1 IyIg== X-Gm-Message-State: AOAM531wJUFYtAMd77VDc8k66+ifPQ5/MPTqqX/MpHHIDVjRGgxRa28N UKdggNtGDQSNh19/PV2Ft3OfUMs3Zv+KXw24CA2cmP+Rq44eKVMQLs5HcKB3nL2JQp9T8z8Lnuu pWxkYqSzeFyadpW547l4= X-Received: by 2002:a2e:a36f:0:b0:253:d948:731c with SMTP id i15-20020a2ea36f000000b00253d948731cmr13677877ljn.159.1653389597525; Tue, 24 May 2022 03:53:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjrEjvDjGZiQqrHunjO3VgQtAVsWsp8nC7Ujs8lg4pCRUmp16MkG8Xd0q5EtUBN9LmDoYz8v/6TglC7EAnZAo= X-Received: by 2002:a2e:a36f:0:b0:253:d948:731c with SMTP id i15-20020a2ea36f000000b00253d948731cmr13677871ljn.159.1653389597343; Tue, 24 May 2022 03:53:17 -0700 (PDT) MIME-Version: 1.0 References: <20220513075718.18674-1-david.marchand@redhat.com> <20220523071031.1868862-1-david.marchand@redhat.com> <20220523071031.1868862-3-david.marchand@redhat.com> In-Reply-To: From: David Marchand Date: Tue, 24 May 2022 12:53:06 +0200 Message-ID: Subject: Re: [PATCH 2/6] app/testpmd: register driver specific commands To: Ferruh Yigit Cc: dev , Thomas Monjalon , Andrew Rybchenko , Xiaoyun Li , Aman Singh , Yuying Zhang , Bruce Richardson Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dmarchan@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org On Mon, May 23, 2022 at 8:10 PM Ferruh Yigit wrote: > > 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. Yes, I hesitated because of the duplicated loop (and I found no "elegant" macro). I'm ok with the suggestion, this makes the code simpler. int init_cmdline(void) { struct testpmd_commands *c; unsigned int count; unsigned int i; /* initialize non-constant commands */ cmd_set_fwd_mode_init(); cmd_set_fwd_retry_mode_init(); count = 0; for (i = 0; builtin_ctx[i] != NULL; i++, count++) ; TAILQ_FOREACH(c, &commands_head, next) { for (i = 0; c->commands[i].ctx != NULL; i++, count++) ; } /* cmdline expects a NULL terminated array */ main_ctx = calloc(count + 1, sizeof(main_ctx[0])); if (main_ctx == NULL) return -1; count = 0; for (i = 0; builtin_ctx[i] != NULL; i++, count++) main_ctx[count] = builtin_ctx[i]; TAILQ_FOREACH(c, &commands_head, next) { for (i = 0; c->commands[i].ctx != NULL; i++, count++) main_ctx[count] = c->commands[i].ctx; } return 0; } -- David Marchand