Add helper to parse common memory prefixes on strings. Signed-off-by: Daniel Wagner <dwagner@xxxxxxx> --- src/include/rt-utils.h | 1 + src/lib/rt-utils.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/include/rt-utils.h b/src/include/rt-utils.h index c6fb220cdb0a..39ddbd9eae29 100644 --- a/src/include/rt-utils.h +++ b/src/include/rt-utils.h @@ -28,6 +28,7 @@ uint32_t string_to_policy(const char *str); pid_t gettid(void); int parse_time_string(char *val); +int parse_mem_string(char *str, uint64_t *val); void enable_trace_mark(void); void tracemark(char *fmt, ...) __attribute__((format(printf, 1, 2))); diff --git a/src/lib/rt-utils.c b/src/lib/rt-utils.c index a30e1e4ce436..2d68d62cd875 100644 --- a/src/lib/rt-utils.c +++ b/src/lib/rt-utils.c @@ -362,6 +362,36 @@ int parse_time_string(char *val) return t; } +int parse_mem_string(char *str, uint64_t *val) +{ + char *endptr; + int v = strtol(str, &endptr, 10); + + if (!*endptr) + return v; + + switch (*endptr) { + case 'g': + case 'G': + v *= 1024; + case 'm': + case 'M': + v *= 1024; + case 'k': + case 'K': + v *= 1024; + case 'b': + case 'B': + break; + default: + return -1; + } + + *val = v; + + return 0; +} + static void open_tracemark_fd(void) { char path[MAX_PATH]; -- 2.29.2