From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00C20C43217 for ; Sat, 6 Nov 2021 07:35:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D2DCC611C0 for ; Sat, 6 Nov 2021 07:35:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233786AbhKFHhm (ORCPT ); Sat, 6 Nov 2021 03:37:42 -0400 Received: from smtprelay0121.hostedemail.com ([216.40.44.121]:33614 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232099AbhKFHhi (ORCPT ); Sat, 6 Nov 2021 03:37:38 -0400 Received: from omf20.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay07.hostedemail.com (Postfix) with ESMTP id CD4F7181D2090; Sat, 6 Nov 2021 07:34:56 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf20.hostedemail.com (Postfix) with ESMTPA id EE66118A5F8; Sat, 6 Nov 2021 07:34:52 +0000 (UTC) Message-ID: <6f27043dc7c50a7447b8fdcb6126f4c67a5936a5.camel@perches.com> Subject: Re: [PATCH v1 01/19] lib/string_helpers: Introduce kasprintf_strarray() From: Joe Perches To: Andy Shevchenko , Bartosz Golaszewski , Jianqun Xu , Linus Walleij , Sai Krishna Potthuri , Andrew Morton , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org Cc: Bamvor Jian Zhang , Andrew Lunn , Gregory Clement , Sebastian Hesselbarth , Heiko Stuebner , Patrice Chotard , Michal Simek , Andy Shevchenko Date: Sat, 06 Nov 2021 00:34:51 -0700 In-Reply-To: <20211105124242.27288-1-andriy.shevchenko@linux.intel.com> References: <20211105124242.27288-1-andriy.shevchenko@linux.intel.com> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.40.4-1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Stat-Signature: 1ifh16fok5zodoyg17ub7y551gue9gq8 X-Rspamd-Server: rspamout03 X-Rspamd-Queue-Id: EE66118A5F8 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1/zHRdlX+53TMDzXto5pNOnstJ0U8Gbcpk= X-HE-Tag: 1636184092-287531 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2021-11-05 at 14:42 +0200, Andy Shevchenko wrote: > We have a few users already that basically want to have array of > sequential strings to be allocated and filled. > > Provide a helper for them (basically adjusted version from gpio-mockup.c). I think this is overkill and unnecessary bloat for the number of actual or possible in-tree uses. The devm_ variant too. And it'd be useful to have an 0/n patch cover letter describing why the patchset is useful so I could reply to that instead of the 1/n. > diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h [] > @@ -100,6 +100,7 @@ char *kstrdup_quotable(const char *src, gfp_t gfp); > char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); > char *kstrdup_quotable_file(struct file *file, gfp_t gfp); > > +char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n); > void kfree_strarray(char **array, size_t n); > > #endif > diff --git a/lib/string_helpers.c b/lib/string_helpers.c [] > @@ -674,6 +674,39 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp) > } > EXPORT_SYMBOL_GPL(kstrdup_quotable_file); > > +/** > + * kasprintf_strarray - allocate and fill array of sequential strings > + * @gfp: flags for the slab allocator > + * @prefix: prefix to be used > + * @n: amount of lines to be allocated and filled > + * > + * Allocates and fills @n strings using pattern "%s-%zu", where prefix > + * is provided by caller. The caller is responsible to free them with > + * kfree_strarray() after use. > + * > + * Returns array of strings or NULL when memory can't be allocated. > + */ > +char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n) > +{ > + char **names; > + size_t i; > + > + names = kcalloc(n + 1, sizeof(char *), gfp); > + if (!names) > + return NULL; > + > + for (i = 0; i < n; i++) { > + names[i] = kasprintf(gfp, "%s-%zu", prefix, i); > + if (!names[i]) { > + kfree_strarray(names, i); > + return NULL; > + } > + } > + > + return names; > +} > +EXPORT_SYMBOL_GPL(kasprintf_strarray); > + > /** > * kfree_strarray - free a number of dynamically allocated strings contained > * in an array and the array itself