Re: [PATCH v4] add u64 number parser

2017-01-30 Thread J. R. Okajima
Hello James,

Your commit
a317178 2016-12-06 parser: add u64 number parser
was merged into v4.10-rc1. Good.
I have a very similar function and used for a long time. Here I'd
suggest you a tiny optimization based on my version.

If you think another value is more apropriate as the size of an internal
array, you can change it freely.


J. R. Okajima

commit 030361e78d327d9d89254dc3b320c092221c7cd0
Author: J. R. Okajima 
Date:   Tue Jan 31 00:01:16 2017 +0900

parser: match_u64, short string optimization
 =

When the given string is short enough, we can skip kmalloc/kfree.

Signed-off-by: J. R. Okajima 

diff --git a/lib/parser.c b/lib/parser.c
index 3278958..cfbc6ec 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -163,21 +163,25 @@ static int match_number(substring_t *s, int *result,=
 int base)
  */
 static int match_u64int(substring_t *s, u64 *result, int base)
 {
-   char *buf;
+   char *buf, a[32];
int ret;
u64 val;
size_t len =3D s->to - s->from;
 =

-   buf =3D kmalloc(len + 1, GFP_KERNEL);
-   if (!buf)
-   return -ENOMEM;
+   buf =3D a;
+   if (len + 1 > sizeof(a)) {
+   buf =3D kmalloc(len + 1, GFP_KERNEL);
+   if (unlikely(!buf))
+   return -ENOMEM;
+   }
memcpy(buf, s->from, len);
buf[len] =3D '\0';
 =

ret =3D kstrtoull(buf, base, &val);
if (!ret)
*result =3D val;
-   kfree(buf);
+   if (buf !=3D a)
+   kfree(buf);
return ret;
 }
 =



Re: [PATCH v4] add u64 number parser

2016-09-26 Thread J Freyensee
On Sun, 2016-09-25 at 09:14 -0700, James Smart wrote:
> add u64 number parser
> 
> Reverted back to version 2 of the patch.  This adds the interface
> using existing logic. Comments from the prior reviewers to move to
> kasprintf were rejected by Linus.
> 
> Signed-off-by: James Smart 

Acked-by: Jay Freyensee 



[PATCH v4] add u64 number parser

2016-09-25 Thread James Smart
add u64 number parser

Reverted back to version 2 of the patch.  This adds the interface
using existing logic. Comments from the prior reviewers to move to
kasprintf were rejected by Linus.

Signed-off-by: James Smart 
---
 include/linux/parser.h |  1 +
 lib/parser.c   | 47 +++
 2 files changed, 48 insertions(+)

diff --git a/include/linux/parser.h b/include/linux/parser.h
index 39d5b79..884c1e6 100644
--- a/include/linux/parser.h
+++ b/include/linux/parser.h
@@ -27,6 +27,7 @@ typedef struct {
 
 int match_token(char *, const match_table_t table, substring_t args[]);
 int match_int(substring_t *, int *result);
+int match_u64(substring_t *, u64 *result);
 int match_octal(substring_t *, int *result);
 int match_hex(substring_t *, int *result);
 bool match_wildcard(const char *pattern, const char *str);
diff --git a/lib/parser.c b/lib/parser.c
index b6d1163..3278958 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -152,6 +152,36 @@ static int match_number(substring_t *s, int *result, int 
base)
 }
 
 /**
+ * match_u64int: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting u64 on success
+ * @base: base to use when converting string
+ *
+ * Description: Given a &substring_t and a base, attempts to parse the 
substring
+ * as a number in that base. On success, sets @result to the integer 
represented
+ * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on 
failure.
+ */
+static int match_u64int(substring_t *s, u64 *result, int base)
+{
+   char *buf;
+   int ret;
+   u64 val;
+   size_t len = s->to - s->from;
+
+   buf = kmalloc(len + 1, GFP_KERNEL);
+   if (!buf)
+   return -ENOMEM;
+   memcpy(buf, s->from, len);
+   buf[len] = '\0';
+
+   ret = kstrtoull(buf, base, &val);
+   if (!ret)
+   *result = val;
+   kfree(buf);
+   return ret;
+}
+
+/**
  * match_int: - scan a decimal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
@@ -167,6 +197,23 @@ int match_int(substring_t *s, int *result)
 EXPORT_SYMBOL(match_int);
 
 /**
+ * match_u64: - scan a decimal representation of a u64 from
+ *  a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting unsigned long long on success
+ *
+ * Description: Attempts to parse the &substring_t @s as a long decimal
+ * integer. On success, sets @result to the integer represented by the
+ * string and returns 0.
+ * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
+ */
+int match_u64(substring_t *s, u64 *result)
+{
+   return match_u64int(s, result, 0);
+}
+EXPORT_SYMBOL(match_u64);
+
+/**
  * match_octal: - scan an octal representation of an integer from a substring_t
  * @s: substring_t to be scanned
  * @result: resulting integer on success
-- 
2.5.0