From: Cezary Rojewski <cezary.rojewski@intel.com>
To: "Andy Shevchenko" <andy.shevchenko@gmail.com>,
"Péter Ujfalusi" <peter.ujfalusi@linux.intel.com>
Cc: Andy Shevchenko <andy@kernel.org>,
Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>,
ALSA Development Mailing List <alsa-devel@alsa-project.org>,
Kai Vehmanen <kai.vehmanen@linux.intel.com>,
Liam Girdwood <lgirdwood@gmail.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Takashi Iwai <tiwai@suse.com>,
Hans de Goede <hdegoede@redhat.com>,
Mark Brown <broonie@kernel.org>,
Ranjani Sridharan <ranjani.sridharan@linux.intel.com>,
amadeuszx.slawinski@linux.intel.com,
Bard Liao <yung-chuan.liao@linux.intel.com>
Subject: Re: [PATCH 1/2] lib/string_helpers: Introduce strsplit_u32()
Date: Fri, 8 Jul 2022 18:32:38 +0200 [thread overview]
Message-ID: <e2fe6351-f9ee-48eb-ad7f-280249f7f3f7@intel.com> (raw)
In-Reply-To: <CAHp75Vd4D0KF7ik+aMOwv-+bofWja_tDe4YUmihQBF+RiHZTmA@mail.gmail.com>
On 2022-07-08 5:25 PM, Andy Shevchenko wrote:
> On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
> <peter.ujfalusi@linux.intel.com> wrote:
...
>>> It seems you are missing the (1). The code has checks for the case where you
>>> can do get number upfront, it would just require two passes, but it's nothing
>>> in comparison of heave realloc().
>>>
>>> unsigned int *tokens;
>>> char *p;
>>> int num;
>>>
>>> p = get_options(str, 0, &num);
>>> if (num == 0)
>>> // No numbers in the string!
>>>
>>> tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL);
>>> if (!tokens)
>>> return -ENOMEM;
>>>
>>> p = get_oprions(str, num, &tokens);
>>> if (*p)
>>> // String was parsed only partially!
>>> // assuming it's not a fatal error
>>>
>>> return tokens;
>
>> This diff is tested and works:
>
> Thanks, Peter!
>
> But at least you can memove() to avoid second allocation.
> ideally to refactor that the result of get_options is consumed as is
> (it may be casted to struct tokens { int n; u32 v[]; })
A long shot, but what if we were to modify get_options() so it takes
additional element-size parameter instead? Something like below - I've
ignored get_range() though. Will re-visit if this option is viable.
diff --git a/lib/cmdline.c b/lib/cmdline.c
index 5546bf588780..272f892b71df 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -53,7 +53,7 @@ static int get_range(char **str, int *pint, int n)
* for the sake of simplification.
*/
-int get_option(char **str, int *pint)
+int get_num_option(char **str, void *pint, size_t nsize)
{
char *cur = *str;
int value;
@@ -65,7 +65,7 @@ int get_option(char **str, int *pint)
else
value = simple_strtoull(cur, str, 0);
if (pint)
- *pint = value;
+ memcpy(pint, &value, min(nsize, sizeof(value)));
if (cur == *str)
return 0;
if (**str == ',') {
@@ -77,6 +77,12 @@ int get_option(char **str, int *pint)
return 1;
}
+EXPORT_SYMBOL(get_num_option);
+
+int get_option(char **str, int *pint)
+{
+ return get_num_option(str, pint, sizeof(*pint));
+}
EXPORT_SYMBOL(get_option);
/**
@@ -104,15 +110,15 @@ EXPORT_SYMBOL(get_option);
* completely parseable).
*/
-char *get_options(const char *str, int nints, int *ints)
+char *get_num_options(const char *str, int nints, void *ints, size_t nsize)
{
bool validate = (nints == 0);
int res, i = 1;
while (i < nints || validate) {
- int *pint = validate ? ints : ints + i;
+ int *pint = validate ? ints : ints + (i * nsize);
- res = get_option((char **)&str, pint);
+ res = get_num_option((char **)&str, pint, nsize);
if (res == 0)
break;
if (res == 3) {
@@ -133,9 +139,17 @@ char *get_options(const char *str, int nints, int
*ints)
if (res == 1)
break;
}
- ints[0] = i - 1;
+ --i;
+ memcpy(ints, &i, min(nsize, sizeof(i)));
return (char *)str;
}
+EXPORT_SYMBOL(get_num_options);
+
+char *get_options(const char *str, int nints, int *ints)
+{
+ return get_num_options(str, nints, ints, sizeof(*ints));
+}
+
EXPORT_SYMBOL(get_options);
WARNING: multiple messages have this Message-ID (diff)
From: Cezary Rojewski <cezary.rojewski@intel.com>
To: "Andy Shevchenko" <andy.shevchenko@gmail.com>,
"Péter Ujfalusi" <peter.ujfalusi@linux.intel.com>
Cc: Andy Shevchenko <andy@kernel.org>,
Mark Brown <broonie@kernel.org>,
"ALSA Development Mailing List" <alsa-devel@alsa-project.org>,
Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>,
<amadeuszx.slawinski@linux.intel.com>,
Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>,
Hans de Goede <hdegoede@redhat.com>,
Ranjani Sridharan <ranjani.sridharan@linux.intel.com>,
"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
Liam Girdwood <lgirdwood@gmail.com>,
"Kai Vehmanen" <kai.vehmanen@linux.intel.com>,
Bard Liao <yung-chuan.liao@linux.intel.com>
Subject: Re: [PATCH 1/2] lib/string_helpers: Introduce strsplit_u32()
Date: Fri, 8 Jul 2022 18:32:38 +0200 [thread overview]
Message-ID: <e2fe6351-f9ee-48eb-ad7f-280249f7f3f7@intel.com> (raw)
In-Reply-To: <CAHp75Vd4D0KF7ik+aMOwv-+bofWja_tDe4YUmihQBF+RiHZTmA@mail.gmail.com>
On 2022-07-08 5:25 PM, Andy Shevchenko wrote:
> On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi
> <peter.ujfalusi@linux.intel.com> wrote:
...
>>> It seems you are missing the (1). The code has checks for the case where you
>>> can do get number upfront, it would just require two passes, but it's nothing
>>> in comparison of heave realloc().
>>>
>>> unsigned int *tokens;
>>> char *p;
>>> int num;
>>>
>>> p = get_options(str, 0, &num);
>>> if (num == 0)
>>> // No numbers in the string!
>>>
>>> tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL);
>>> if (!tokens)
>>> return -ENOMEM;
>>>
>>> p = get_oprions(str, num, &tokens);
>>> if (*p)
>>> // String was parsed only partially!
>>> // assuming it's not a fatal error
>>>
>>> return tokens;
>
>> This diff is tested and works:
>
> Thanks, Peter!
>
> But at least you can memove() to avoid second allocation.
> ideally to refactor that the result of get_options is consumed as is
> (it may be casted to struct tokens { int n; u32 v[]; })
A long shot, but what if we were to modify get_options() so it takes
additional element-size parameter instead? Something like below - I've
ignored get_range() though. Will re-visit if this option is viable.
diff --git a/lib/cmdline.c b/lib/cmdline.c
index 5546bf588780..272f892b71df 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -53,7 +53,7 @@ static int get_range(char **str, int *pint, int n)
* for the sake of simplification.
*/
-int get_option(char **str, int *pint)
+int get_num_option(char **str, void *pint, size_t nsize)
{
char *cur = *str;
int value;
@@ -65,7 +65,7 @@ int get_option(char **str, int *pint)
else
value = simple_strtoull(cur, str, 0);
if (pint)
- *pint = value;
+ memcpy(pint, &value, min(nsize, sizeof(value)));
if (cur == *str)
return 0;
if (**str == ',') {
@@ -77,6 +77,12 @@ int get_option(char **str, int *pint)
return 1;
}
+EXPORT_SYMBOL(get_num_option);
+
+int get_option(char **str, int *pint)
+{
+ return get_num_option(str, pint, sizeof(*pint));
+}
EXPORT_SYMBOL(get_option);
/**
@@ -104,15 +110,15 @@ EXPORT_SYMBOL(get_option);
* completely parseable).
*/
-char *get_options(const char *str, int nints, int *ints)
+char *get_num_options(const char *str, int nints, void *ints, size_t nsize)
{
bool validate = (nints == 0);
int res, i = 1;
while (i < nints || validate) {
- int *pint = validate ? ints : ints + i;
+ int *pint = validate ? ints : ints + (i * nsize);
- res = get_option((char **)&str, pint);
+ res = get_num_option((char **)&str, pint, nsize);
if (res == 0)
break;
if (res == 3) {
@@ -133,9 +139,17 @@ char *get_options(const char *str, int nints, int
*ints)
if (res == 1)
break;
}
- ints[0] = i - 1;
+ --i;
+ memcpy(ints, &i, min(nsize, sizeof(i)));
return (char *)str;
}
+EXPORT_SYMBOL(get_num_options);
+
+char *get_options(const char *str, int nints, int *ints)
+{
+ return get_num_options(str, nints, ints, sizeof(*ints));
+}
+
EXPORT_SYMBOL(get_options);
next prev parent reply other threads:[~2022-07-08 16:33 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-07 9:13 [PATCH 1/2] lib/string_helpers: Introduce strsplit_u32() Cezary Rojewski
2022-07-07 9:13 ` Cezary Rojewski
2022-07-07 9:13 ` [PATCH 2/2] ASoC: SOF: Remove tokenize_input() Cezary Rojewski
2022-07-07 9:13 ` Cezary Rojewski
2022-07-07 13:51 ` [PATCH 1/2] lib/string_helpers: Introduce strsplit_u32() Péter Ujfalusi
2022-07-07 13:51 ` Péter Ujfalusi
2022-07-08 11:33 ` Cezary Rojewski
2022-07-08 11:33 ` Cezary Rojewski
2022-07-08 10:22 ` Andy Shevchenko
2022-07-08 10:22 ` Andy Shevchenko
2022-07-08 11:29 ` Andy Shevchenko
2022-07-08 11:29 ` Andy Shevchenko
2022-07-08 11:32 ` Cezary Rojewski
2022-07-08 11:32 ` Cezary Rojewski
2022-07-08 11:46 ` Andy Shevchenko
2022-07-08 11:46 ` Andy Shevchenko
2022-07-08 11:51 ` Andy Shevchenko
2022-07-08 11:51 ` Andy Shevchenko
2022-07-08 12:13 ` Cezary Rojewski
2022-07-08 12:13 ` Cezary Rojewski
2022-07-08 12:30 ` Andy Shevchenko
2022-07-08 12:30 ` Andy Shevchenko
2022-07-08 12:35 ` Péter Ujfalusi
2022-07-08 12:35 ` Péter Ujfalusi
2022-07-08 15:25 ` Andy Shevchenko
2022-07-08 15:25 ` Andy Shevchenko
2022-07-08 15:28 ` Andy Shevchenko
2022-07-08 15:28 ` Andy Shevchenko
2022-07-08 16:32 ` Cezary Rojewski [this message]
2022-07-08 16:32 ` Cezary Rojewski
2022-07-08 16:49 ` Andy Shevchenko
2022-07-08 16:49 ` Andy Shevchenko
2022-07-09 8:45 ` Cezary Rojewski
2022-07-09 8:45 ` Cezary Rojewski
2022-07-09 20:42 ` Andy Shevchenko
2022-07-09 20:42 ` Andy Shevchenko
2022-07-12 13:51 ` Cezary Rojewski
2022-07-12 13:51 ` Cezary Rojewski
2022-07-12 13:59 ` Andy Shevchenko
2022-07-12 13:59 ` Andy Shevchenko
2022-07-12 14:02 ` Mark Brown
2022-07-12 14:02 ` Mark Brown
2022-07-12 14:24 ` Andy Shevchenko
2022-07-12 14:24 ` Andy Shevchenko
2022-07-19 11:40 ` Cezary Rojewski
2022-07-19 11:40 ` Cezary Rojewski
2022-08-09 9:55 ` Cezary Rojewski
2022-08-09 9:55 ` Cezary Rojewski
2022-08-09 15:23 ` Andy Shevchenko
2022-08-09 15:23 ` Andy Shevchenko
2022-08-16 9:28 ` Cezary Rojewski
2022-08-16 9:28 ` Cezary Rojewski
2022-08-25 15:09 ` Andy Shevchenko
2022-08-25 15:09 ` Andy Shevchenko
2022-08-25 16:44 ` Cezary Rojewski
2022-08-25 16:44 ` Cezary Rojewski
2022-07-13 9:14 ` David Laight
2022-07-13 9:14 ` David Laight
2022-07-13 9:38 ` Andy Shevchenko
2022-07-13 9:38 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e2fe6351-f9ee-48eb-ad7f-280249f7f3f7@intel.com \
--to=cezary.rojewski@intel.com \
--cc=alsa-devel@alsa-project.org \
--cc=amadeuszx.slawinski@linux.intel.com \
--cc=andy.shevchenko@gmail.com \
--cc=andy@kernel.org \
--cc=broonie@kernel.org \
--cc=hdegoede@redhat.com \
--cc=kai.vehmanen@linux.intel.com \
--cc=lgirdwood@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peter.ujfalusi@linux.intel.com \
--cc=pierre-louis.bossart@linux.intel.com \
--cc=ranjani.sridharan@linux.intel.com \
--cc=tiwai@suse.com \
--cc=yung-chuan.liao@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.