From patchwork Sun Sep 24 07:36:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 144066 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp602143vqu; Sun, 24 Sep 2023 03:57:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7gW5kV4ElpudUGRelooU9+gcSTOFDhBJphL011T9ZXzLX/BEE6muInF3qjUCo4liiMzfJ X-Received: by 2002:a17:90a:6486:b0:274:1bc9:b72 with SMTP id h6-20020a17090a648600b002741bc90b72mr3452178pjj.24.1695553021742; Sun, 24 Sep 2023 03:57:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695553021; cv=none; d=google.com; s=arc-20160816; b=J4sLq0JhTY8M3esq+uLWRgHGSDNKuB5TRtGL6Ass1jP0qaIc7Aj9FJh5WBA3o2aarC A81lSMwf2uP5f6adczomOgJeezCDmXD7PmlTetmDV8TUrZle5+MoRYTh0cusoT1DaUhq MqFq6VDC2hVTt8cOl0lM6kW/a7H0VHny4Bt9twnYW7DKUUa1aYLDLamuqD+p9b8P1fue 6n5HUOfRVR3ATLkQAeOnfd6gjWI1zC9YNUhGtB2kXMAtOtnfCE8Mmz3ow/7lR6E//M1X 7o3jfZ3bw4TDsOuBzdqMo7gE/pQ1aWUDGiaPM0q5J3IL5GJeNENb9P2T3tk+HxCIloUr /91g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=018xaLEDvxggoMddYpgeo/uvtqjMWAwwaPEHKHKhtvA=; fh=b0yL+LwIHgfZpId/ZkI9nzpOkBAQDmeGaGCrtgYhfik=; b=0rFb7BfmuTS/gyJdvfyjjH88mMXd7LeMCxbNx/voMy3cTvZnkmEXy4L1b+tDMRmo3G ivSkzDszQkm57ezJkuoeP5iGTWd1/RKNV2DjOF9RIqgZkFJ2UeEXJlM0Pcoj+y6x2mWd gz/ksA5TkOORm9k42lkChefMwQ2varIhJXvQffQzAC+GbWvQ7HMpLnmGGvkC2I9q7NlY apfRGgqRWtsYHZ2YKWVZwGixsGvYRSR6fajdEPqUA2ZnbsXL2jVnOTmzWFvGbBY4/Bcb Lf/CikI6vpogsBLUiJVG3uOC1TEhPWADnfEEmFJflcqIrTFm0BMup24glQOcUP12kNgT y+9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=pQy7OeR8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t6-20020a17090a024600b0026390b4a4e0si9911333pje.124.2023.09.24.03.57.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 03:57:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=pQy7OeR8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E66698092DB2; Sun, 24 Sep 2023 00:49:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229437AbjIXHgP (ORCPT + 30 others); Sun, 24 Sep 2023 03:36:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229699AbjIXHgO (ORCPT ); Sun, 24 Sep 2023 03:36:14 -0400 Received: from smtp.smtpout.orange.fr (smtp-13.smtpout.orange.fr [80.12.242.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A443197 for ; Sun, 24 Sep 2023 00:36:05 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id kJesqk7nIGc65kJesqQUu3; Sun, 24 Sep 2023 09:36:03 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1695540963; bh=018xaLEDvxggoMddYpgeo/uvtqjMWAwwaPEHKHKhtvA=; h=From:To:Cc:Subject:Date; b=pQy7OeR8/JRokKoalfqARW7ycJBOE9XFchZQxVNYhEXztu4UfdU1+F0kj2pmV28aO avdVOG0lO5QJB3GNl3bBX6xGCB2HsPjwCPGC7ZqDlHUFdlzbmmvmofoZQq2OofMt9g zl975c9qmr6bq2Ji42XXpKuAK1ig5oC5yVccIcVpOOYiEBRfXSZAmwF1uA43icMWrh mmlPixtAbDWeRPqP7MHo3XDf6exTjL5hhPh0Jj86aRb/YFxqXekWxjXu7y0/iYnUU+ /R1zrZGk5lKkqODxeXGwJv1SG2Ja2xHQcrjd2mCUOXlK3zLH7MsnQCVsRnV9LjF8N4 mYyvzM4ZCl5dg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 24 Sep 2023 09:36:03 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , alsa-devel@alsa-project.org Subject: [PATCH] ASoC: audio-iio-aux: Use flex array to simplify code Date: Sun, 24 Sep 2023 09:36:01 +0200 Message-Id: <1c0090aaf49504eaeaff5e7dd119fd37173290b5.1695540940.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 24 Sep 2023 00:49:36 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777916205521922736 X-GMAIL-MSGID: 1777916205521922736 "io-channel-names" is expected to have few values, so there is no real point to allocate audio_iio_aux_chan structure with a dedicate memory allocation. Using a flexible array for struct audio_iio_aux->chans avoids the overhead of an additional, managed, memory allocation. This also saves an indirection when the array is accessed. Finally, __counted_by() can be used for run-time bounds checking if configured and supported by the compiler. Signed-off-by: Christophe JAILLET --- sound/soc/codecs/audio-iio-aux.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/sound/soc/codecs/audio-iio-aux.c b/sound/soc/codecs/audio-iio-aux.c index a8bf14239bd7..1e8e1effc2af 100644 --- a/sound/soc/codecs/audio-iio-aux.c +++ b/sound/soc/codecs/audio-iio-aux.c @@ -26,8 +26,8 @@ struct audio_iio_aux_chan { struct audio_iio_aux { struct device *dev; - struct audio_iio_aux_chan *chans; unsigned int num_chans; + struct audio_iio_aux_chan chans[] __counted_by(num_chans); }; static int audio_iio_aux_info_volsw(struct snd_kcontrol *kcontrol, @@ -250,23 +250,18 @@ static int audio_iio_aux_probe(struct platform_device *pdev) int ret; int i; - iio_aux = devm_kzalloc(dev, sizeof(*iio_aux), GFP_KERNEL); + count = device_property_string_array_count(dev, "io-channel-names"); + if (count < 0) + return dev_err_probe(dev, count, "failed to count io-channel-names\n"); + + iio_aux = devm_kzalloc(dev, struct_size(iio_aux, chans, count), GFP_KERNEL); if (!iio_aux) return -ENOMEM; iio_aux->dev = dev; - count = device_property_string_array_count(dev, "io-channel-names"); - if (count < 0) - return dev_err_probe(dev, count, "failed to count io-channel-names\n"); - iio_aux->num_chans = count; - iio_aux->chans = devm_kmalloc_array(dev, iio_aux->num_chans, - sizeof(*iio_aux->chans), GFP_KERNEL); - if (!iio_aux->chans) - return -ENOMEM; - names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL); if (!names) return -ENOMEM;