Skip to contentSkip to conversation

add u64 number parser

PATCH J. R. Okajima 1 participant 1 message Jan 30, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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 <[email protected]>
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.

lib/parser.c +9 -5
index 3278958..cfbc6ec 100644
@@ -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;
}
=