Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp239822pxk; Thu, 24 Sep 2020 04:40:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSCkigAV7/djHuROOynhiQo4iCxDUzi6t0rcOt7x31ZigScuGPmEAo+UeSPhFMRbjSbing X-Received: by 2002:a50:d591:: with SMTP id v17mr578249edi.92.1600947617501; Thu, 24 Sep 2020 04:40:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600947617; cv=none; d=google.com; s=arc-20160816; b=oDVKwMqWUMSkbNLbKlAViIaXVW2S33fCptj3FJuk9B5uywa7SuOTJtuKq4fWv/WXGu d2KQNivtQCI7PdnSEvaAf6wQLSJiXrCkEjUJBa+8XJtOqm8WCezLwoZHPfMMD5e/jXvr B7N1n3l3zyeEydsydDrkej4KUnNy4uABRAVh6qitO/JfQqRvM3YR4RxZ5QPKZALyFUOf vghltrYFRPeOjfzyyu0sqXBvcmqe0Eu0jk/Wx0fWRY21iDOyJaIgup6pmvF48qummEkA MxhhBQNyivun0SSds9SIgqYOu8yEyKFwyNrcTJPdOBMJtgiYgtuAOq4j6vTwP+0YAkrG NmcA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bCsr6AFFHdHhAugFVMGJohC8JN0hd4nL8k90M/6i7xA=; b=S6QWArC+mTy+2614aJD8D6k3tRkEXTEGUUbyU/v2R/X/HadfwF54jNNWIags8Lw93Y EL+KQaPB3GbQ/qXjjb3J1p5jew4zNmbj6WbH7yimKsFWCywtrrkJuNpGXe5Eml8Ut3cd Fntvz+gxkPSljtB2/yCxq/PfdOs3eZL5UHYsRJf7WO0AC2SNRwH9sgtIqJxGWwAvB/qi gNQO6BZMdN6cmqEBc4LGlRBG8VcVLJNtL2Zib0AvsbTmR5bTcq9bJ38yHIbyn+yxSb+x EZEgAEoHwOVUq+fYt53udzaNUXrXtuaCMRkqmoRDj166jWw9ocDXDIKGYYl2ewO9yNHo p9Sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=a7KuNLrw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id yc15si2189249ejb.23.2020.09.24.04.39.54; Thu, 24 Sep 2020 04:40:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=a7KuNLrw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727527AbgIXLiy (ORCPT + 99 others); Thu, 24 Sep 2020 07:38:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727485AbgIXLiv (ORCPT ); Thu, 24 Sep 2020 07:38:51 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0974FC0613D4 for ; Thu, 24 Sep 2020 04:38:51 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id s13so3172571wmh.4 for ; Thu, 24 Sep 2020 04:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bCsr6AFFHdHhAugFVMGJohC8JN0hd4nL8k90M/6i7xA=; b=a7KuNLrwlM7cvptfMtEqtc5CpObOWJJFH9henQqs6idqfzL5qx7a9wLt2qGL4rT1Qz HutCQ1XrFdOs4pQA/nDIfdY6FMVAIEQsMDZlU5mc/CuxvUbDh/zIlSkAGsP6NFHhhnNd 4p6qhRDO0u8+AeeHYDV1nzUhpue9ar8pyt3JB0SRwKUyE3yfGvNtDXvkGzKkIQnMjJmI BXn0xVBLk0iwWyTAtoRm2eP+V4REcHuv7huXbBX3HW4qyd0PxDCNQAo6QX1K6rz6SS8y TFsyKVS7ba824hLFuu3AlRdnQMNyxc8FI06nsImBXaOtWSfuZWrgZvoGNTJFCAk3LEzO 8tRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bCsr6AFFHdHhAugFVMGJohC8JN0hd4nL8k90M/6i7xA=; b=h5J0CYLwrvi78750IUXioPYF6+ATSGvtnl92QEQoXoe6DrB9AwIRpNtWUrN+yezucw Jfdyn9yH8qRRR++R9tiEi/WnRsg7/YZnACY9iQUJpcME7BBLnDCAoSlbvriNfHrMZAoA DqYy0RArwtTgy50yHxNIX+xJA1ety/EwFYFh/ugXXHmgfyyQvXzOOejGhZ+okBkfkHTZ vjb4zCJ9eYfjai0JOY+dcEVAWRBZ0LrRFdfVhJEOBY9sDSNzO74x3ufgD8cvRTigk1j0 ZPgKsCIy3ykHdfW5dSIdVpesprNfCMSLpJAeH5hajS7/B9ItMLd40ugmKTn/2itOBurS /hTQ== X-Gm-Message-State: AOAM533atYB0dM1pncPUkUdPVhaGmkaX1ymaHLt79PRIuR/yC599R30K +4Ul4IJDxYcMpqtXXS7oN2qSGg== X-Received: by 2002:a1c:b608:: with SMTP id g8mr4552385wmf.106.1600947529507; Thu, 24 Sep 2020 04:38:49 -0700 (PDT) Received: from debian-brgl.home (lfbn-nic-1-68-20.w2-15.abo.wanadoo.fr. [2.15.159.20]) by smtp.gmail.com with ESMTPSA id y207sm3390133wmc.17.2020.09.24.04.38.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 04:38:48 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Jonathan Corbet , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 1/9] lib: string_helpers: provide kfree_strarray() Date: Thu, 24 Sep 2020 13:38:34 +0200 Message-Id: <20200924113842.11670-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200924113842.11670-1-brgl@bgdev.pl> References: <20200924113842.11670-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski There's a common pattern of dynamically allocating an array of char pointers and then also dynamically allocating each string in this array. Provide a helper for freeing such a string array with one call. Signed-off-by: Bartosz Golaszewski --- include/linux/string_helpers.h | 2 ++ lib/string_helpers.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 86f150c2a6b6..55b25120a1c6 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h @@ -94,4 +94,6 @@ 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); +void kfree_strarray(char **str_array, size_t num_str); + #endif diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 963050c0283e..56c01ec8a076 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -649,3 +649,25 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp) return pathname; } EXPORT_SYMBOL_GPL(kstrdup_quotable_file); + +/** + * kfree_strarray - free a number of dynamically allocated strings contained + * in an array and the array itself + * + * @str_array: Dynamically allocated array of strings to free. If NULL - the + * function does nothing. + * @num_str: Number of strings (starting from the beginning of the array) to + * free. + * + * Passing a non-null str_array and num_str == 0 as well as NULL str_array and + * num_str == 0 are valid use-cases. + */ +void kfree_strarray(char **str_array, size_t num_str) +{ + unsigned int i; + + for (i = 0; i < num_str; i++) + kfree(str_array[i]); + kfree(str_array); +} +EXPORT_SYMBOL_GPL(kfree_strarray); -- 2.26.1