Skip to contentSkip to conversation

statistics infrastructure - prerequisite: parser enhancement

RFC PATCH In review Martin Peschke 3 participants 4 messages May 16, 2006 – May 17, 2006 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/8 statistics infrastructure - prerequisite: parser enhancement

This patch adds two match_* derivates for 64 bit operands to the parser library.

---

 include/linux/parser.h |    2 +
 lib/parser.c           |   60 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
include/linux/parser.h +2 -0
diff -Nurp a/include/linux/parser.h b/include/linux/parser.h
@@ -31,3 +31,5 @@ int match_octal(substring_t *, int *resu
 int match_hex(substring_t *, int *result);
 void match_strcpy(char *, substring_t *);
 char *match_strdup(substring_t *);
+int match_u64(substring_t *, u64 *result, int);
+int match_s64(substring_t *, s64 *result, int);
lib/parser.c +60 -0

69 lines not on this page · raw message

#1 lore raw

include/linux/parser.h int match_octal(substring_t *, int *resu · line 35

Quoted 8 lines · Martin Peschke

This patch adds two match_* derivates for 64 bit operands to the parser library.

  void match_strcpy(char *, substring_t *);
  char *match_strdup(substring_t *);
include/linux/parser.h +2 -0
+int match_u64(substring_t *, u64 *result, int);
+int match_s64(substring_t *, s64 *result, int);

Your email client does space-stuffing, so this won't apply (ok, it might apply if my email client knew how to space-unstuff, but it doesn't).

Quoted 53 lines

diff -Nurp a/lib/parser.c b/lib/parser.c

lib/parser.c +46 -0
@@ -140,6 +140,64 @@ static int match_number(substring_t *s,
  }
 
  /**
+ * match_u64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 u64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_u64(substring_t *s, u64 *result, int base)
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';
+	*result = simple_strtoull(buf, &endp, base);
+	ret = 0;
+	if (endp == buf)
+		ret = -EINVAL;
+	kfree(buf);
+	return ret;
+}
+
+/**
+ * match_s64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 s64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_s64(substring_t *s, s64 *result, int base)
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)

lib/parser.c static int match_number(substring_t *s, · line 198

Quoted 10 lines
lib/parser.c +10 -0
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';
+	*result = simple_strtoll(buf, &endp, base);
+	ret = 0;
+	if (endp == buf)
+		ret = -EINVAL;
+	kfree(buf);
+	return ret;
+}

These are identical. If we _really_ need one for signed and one for unsigned then we could at least do

match_s64(..., s64 *result, ...)

{
	return match_u64(..., (u64 *)result, ...);
}

That's if we do need one for signed and one for unsigned..

#3 lore raw
In reply to Martin Peschke
Quoted 39 lines · Martin Peschke

This patch adds two match_* derivates for 64 bit operands to the parser library.

---

 include/linux/parser.h |    2 +
 lib/parser.c           |   60
 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff -Nurp a/include/linux/parser.h b/include/linux/parser.h

include/linux/parser.h +2 -0
@@ -31,3 +31,5 @@ int match_octal(substring_t *, int *resu
 int match_hex(substring_t *, int *result);
 void match_strcpy(char *, substring_t *);
 char *match_strdup(substring_t *);
+int match_u64(substring_t *, u64 *result, int);
+int match_s64(substring_t *, s64 *result, int);

diff -Nurp a/lib/parser.c b/lib/parser.c

lib/parser.c +6 -0
@@ -140,6 +140,64 @@ static int match_number(substring_t *s,
 }
 
 /**
+ * match_u64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 u64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */

lib/parser.c +1 -0
+int match_u64(substring_t *s, u64 *result, int base)

lib/parser.c static int match_number(substring_t *s, · line 162

Quoted 10 lines
lib/parser.c +10 -0
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';

Why not match_strdup:

	buf = match_strdup(s);
	if (unlikely(!buf))
		return -ENOMEM;
Quoted 21 lines
lib/parser.c +15 -0
+	*result = simple_strtoull(buf, &endp, base);
+	ret = 0;
+	if (endp == buf)
+		ret = -EINVAL;
+	kfree(buf);
+	return ret;
+}
+
+/**
+ * match_s64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 s64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */

lib/parser.c +1 -0
+int match_s64(substring_t *s, s64 *result, int base)

lib/parser.c static int match_number(substring_t *s, · line 191

Quoted 10 lines
lib/parser.c +10 -0
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';

Same story here.

Quoted 14 lines
lib/parser.c +9 -0
+	*result = simple_strtoll(buf, &endp, base);
+	ret = 0;
+	if (endp == buf)
+		ret = -EINVAL;
+	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

lib/parser.c EXPORT_SYMBOL(match_octal); · line 280

Quoted 5 lines
lib/parser.c +2 -0
@@ -218,3 +276,5 @@ EXPORT_SYMBOL(match_octal);
 EXPORT_SYMBOL(match_hex);
 EXPORT_SYMBOL(match_strcpy);
 EXPORT_SYMBOL(match_strdup);
+EXPORT_SYMBOL(match_u64);
+EXPORT_SYMBOL(match_s64);

Thanks,
Chase

#2 lore raw
In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

Your email client does space-stuffing, so this won't apply (ok, it might apply if my email client knew how to space-unstuff, but it doesn't).

Okay, I got rid of my email client's space-stuffing (and of my former email client, for that matter). I will resend my patches.

Quoted 58 lines
Quoted 58 lines

diff -Nurp a/lib/parser.c b/lib/parser.c

lib/parser.c +51 -0
@@ -140,6 +140,64 @@ static int match_number(substring_t *s,
  }
 
  /**
+ * match_u64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 u64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_u64(substring_t *s, u64 *result, int base)
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';
+	*result = simple_strtoull(buf, &endp, base);
+	ret = 0;
+	if (endp == buf)
+		ret = -EINVAL;
+	kfree(buf);
+	return ret;
+}
+
+/**
+ * match_s64: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer 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 s64 represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_s64(substring_t *s, s64 *result, int base)
+{
+	char *endp;
+	char *buf;
+	int ret;
+
+	buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	memcpy(buf, s->from, s->to - s->from);
+	buf[s->to - s->from] = '\0';
+	*result = simple_strtoll(buf, &endp, base);
+	ret = 0;
Quoted 8 lines

lib/parser.c static int match_number(substring_t *s, · line 169

Quoted 5 lines
lib/parser.c +5 -0
+	if (endp == buf)
+		ret = -EINVAL;
+	kfree(buf);
+	return ret;
+}

These are identical. If we _really_ need one for signed and one for unsigned then we could at least do

I am going to remove match_u64. My code doesn't use it anymore.