2014-04-01 10:01:40

by Gui Hecheng

[permalink] [raw]
Subject: [PATCH v2] lib: add size unit t/p/e to memparse

For modern filesystems such as btrfs, t/p/e size level operations
are common.
add size unit t/p/e parsing to memparse

Signed-off-by: Gui Hecheng <[email protected]>
---
changelog
v1->v2: replace kilobyte with kibibyte, and others
---
lib/cmdline.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index eb67911..7cf5cb8 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -119,11 +119,17 @@ char *get_options(const char *str, int nints, int *ints)
* @retptr: (output) Optional pointer to next char after parse completes
*
* Parses a string into a number. The number stored at @ptr is
- * potentially suffixed with %K (for kilobytes, or 1024 bytes),
- * %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
- * 1073741824). If the number is suffixed with K, M, or G, then
- * the return value is the number multiplied by one kilobyte, one
- * megabyte, or one gigabyte, respectively.
+ * potentially suffixed with
+ * %K (for kibibytes, or 1024 bytes),
+ * %M (for mebibytes, or 1048576 bytes),
+ * %G (for gibibytes, or 1073741824),
+ * %T (for tebibytes, or 1099511627776),
+ * %P (for pebibytes, or 1125899906842624 bytes),
+ * %E (for exbibytes, or 1152921504606846976 bytes).
+ * If the number is suffixed with K, M, G, T, P, E, then
+ * the return value is the number multiplied by one kibibyte, one
+ * mebibyte, one gibibyte, one tebibyte, one pebibyte, one exbibyte,
+ * respectively.
*/

unsigned long long memparse(const char *ptr, char **retptr)
@@ -133,6 +139,15 @@ unsigned long long memparse(const char *ptr, char **retptr)
unsigned long long ret = simple_strtoull(ptr, &endptr, 0);

switch (*endptr) {
+ case 'E':
+ case 'e':
+ ret <<= 10;
+ case 'P':
+ case 'p':
+ ret <<= 10;
+ case 'T':
+ case 't':
+ ret <<= 10;
case 'G':
case 'g':
ret <<= 10;
--
1.8.1.4


2014-04-01 10:56:16

by Duncan

[permalink] [raw]
Subject: Re: [PATCH v2] lib: add size unit t/p/e to memparse

Gui Hecheng posted on Tue, 01 Apr 2014 17:57:31 +0800 as excerpted:

> + * potentially suffixed with
> + * %K (for kibibytes, or 1024 bytes),
> + * %M (for mebibytes, or 1048576 bytes),
> + * %G (for gibibytes, or 1073741824),
> + * %T (for tebibytes, or 1099511627776),
> + * %P (for pebibytes, or 1125899906842624 bytes),
> + * %E (for exbibytes, or 1152921504606846976 bytes).

Why do the first two and the last two end in bytes, but the the middle
two don't? I'd suggest either only specifying bytes on the first one
(a reasonably common pattern in English, with the carry down to the
others implied) or on all of them.

--
Duncan - List replies preferred. No HTML msgs.
"Every nonfree program has a lord, a master --
and if you use the program, he is your master." Richard Stallman