From 714814f97e6c078073a81dd819fb1c0d02800641 Mon Sep 17 00:00:00 2001 From: Shiqing Gao Date: Thu, 20 Dec 2018 11:08:27 +0800 Subject: [PATCH] hv: move `atoi` and `strtol_dec` to debug directory This patch moves `atoi` and `strtol_dec` to debug directory since they are only used by code under debug directory. Tracked-On: #861 Signed-off-by: Shiqing Gao Acked-by: Anthony Xu --- hypervisor/debug/string.c | 91 ++++++++++++++++++++++++++++++++++ hypervisor/include/lib/rtl.h | 10 ++++ hypervisor/lib/string.c | 95 +----------------------------------- 3 files changed, 102 insertions(+), 94 deletions(-) create mode 100644 hypervisor/debug/string.c diff --git a/hypervisor/debug/string.c b/hypervisor/debug/string.c new file mode 100644 index 000000000..fdd58be4c --- /dev/null +++ b/hypervisor/debug/string.c @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +/* + * Convert a string to a long integer - decimal support only. + */ +int64_t strtol_deci(const char *nptr) +{ + const char *s = nptr; + char c; + uint64_t acc, cutoff, cutlim; + int32_t neg = 0, any; + uint64_t base = 10UL; + + /* + * Skip white space and pick up leading +/- sign if any. + */ + do { + c = *s; + s++; + } while (is_space(c)); + + if (c == '-') { + neg = 1; + c = *s; + s++; + } else if (c == '+') { + c = *s; + s++; + } else { + /* No sign character. */ + } + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, or equal but the next digit is > 7 (or 8), + * the number is too big, and we will return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + cutoff = (neg != 0) ? LONG_MIN : LONG_MAX; + cutlim = cutoff % base; + cutoff /= base; + acc = 0UL; + any = 0; + + while ((c >= '0') && (c <= '9')) { + c -= '0'; + if ((acc > cutoff) || + ((acc == cutoff) && ((uint64_t)c > cutlim))) { + any = -1; + break; + } else { + acc *= base; + acc += (uint64_t)c; + } + + c = *s; + s++; + } + + if (any < 0) { + acc = (neg != 0) ? LONG_MIN : LONG_MAX; + } else if (neg != 0) { + acc = ~acc + 1UL; + } else { + /* There is no overflow and no leading '-' exists. In such case + * acc already holds the right number. No action required. */ + } + return (long)acc; +} + +int32_t atoi(const char *str) +{ + return (int32_t)strtol_deci(str); +} diff --git a/hypervisor/include/lib/rtl.h b/hypervisor/include/lib/rtl.h index 2c0cef6b5..8fe521e71 100644 --- a/hypervisor/include/lib/rtl.h +++ b/hypervisor/include/lib/rtl.h @@ -19,6 +19,16 @@ union u_qword { }; +/* MACRO related to string */ +#define ULONG_MAX ((uint64_t)(~0UL)) /* 0xFFFFFFFF */ +#define LONG_MAX (ULONG_MAX >> 1U) /* 0x7FFFFFFF */ +#define LONG_MIN (~LONG_MAX) /* 0x80000000 */ + +static inline bool is_space(char c) +{ + return ((c == ' ') || (c == '\t')); +} + /* Function prototypes */ void udelay(uint32_t us); int32_t strcmp(const char *s1_arg, const char *s2_arg); diff --git a/hypervisor/lib/string.c b/hypervisor/lib/string.c index 37b69a007..7bb212833 100644 --- a/hypervisor/lib/string.c +++ b/hypervisor/lib/string.c @@ -5,16 +5,7 @@ #include -#define ULONG_MAX ((uint64_t)(~0UL)) /* 0xFFFFFFFF */ -#define LONG_MAX (ULONG_MAX >> 1U) /* 0x7FFFFFFF */ -#define LONG_MIN (~LONG_MAX) /* 0x80000000 */ - -static inline bool is_space(char c) -{ - return ((c == ' ') || (c == '\t')); -} - -static inline char hex_digit_value (char ch) +static inline char hex_digit_value(char ch) { char c; if (('0' <= ch) && (ch <= '9')) { @@ -29,85 +20,6 @@ static inline char hex_digit_value (char ch) return c; } -/* - * Convert a string to a long integer - decimal support only. - */ -int64_t strtol_deci(const char *nptr) -{ - const char *s = nptr; - char c; - uint64_t acc, cutoff, cutlim; - int32_t neg = 0, any; - uint64_t base = 10UL; - - /* - * Skip white space and pick up leading +/- sign if any. - */ - do { - c = *s; - s++; - } while (is_space(c)); - - if (c == '-') { - neg = 1; - c = *s; - s++; - } else if (c == '+') { - c = *s; - s++; - } else { - /* No sign character. */ - } - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for longs is - * [-2147483648..2147483647] and the input base is 10, - * cutoff will be set to 214748364 and cutlim to either - * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated - * a value > 214748364, or equal but the next digit is > 7 (or 8), - * the number is too big, and we will return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - cutoff = (neg != 0) ? LONG_MIN : LONG_MAX; - cutlim = cutoff % base; - cutoff /= base; - acc = 0UL; - any = 0; - - while ((c >= '0') && (c <= '9')) { - c -= '0'; - if ((acc > cutoff) || - ((acc == cutoff) && ((uint64_t)c > cutlim))) { - any = -1; - break; - } else { - acc *= base; - acc += (uint64_t)c; - } - - c = *s; - s++; - } - - if (any < 0) { - acc = (neg != 0) ? LONG_MIN : LONG_MAX; - } else if (neg != 0) { - acc = ~acc + 1UL; - } else { - /* There is no overflow and no leading '-' exists. In such case - * acc already holds the right number. No action required. */ - } - return (int64_t)acc; -} - /* * Convert a string to an uint64_t integer - hexadecimal support only. */ @@ -157,11 +69,6 @@ uint64_t strtoul_hex(const char *nptr) return acc; } -int32_t atoi(const char *str) -{ - return (int32_t)strtol_deci(str); -} - char *strchr(char *s_arg, char ch) { char *s = s_arg;