diff options
author | André Goddard Rosa <andre.goddard@gmail.com> | 2009-12-14 21:01:01 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-15 11:53:32 -0500 |
commit | 922ac25c9f4b5dc4c48ff12bfd14a98bdeb6ff0a (patch) | |
tree | 8c467994a89e65b231a3905a5e9d309ff1833139 /lib | |
parent | c5484d7c0a533de6198cb474097e33b174f9c565 (diff) |
vsprintf: reuse almost identical simple_strtoulX() functions
The difference between simple_strtoul() and simple_strtoull() is just
the size of the variable used to keep track of the sum of characters
converted to numbers:
unsigned long simple_strtoul() {...}
unsigned long long simple_strtoull(){...}
Both are same size on my Core 2/gcc 4.4.1.
Overflow condition is not checked on both functions, so an extremely large
string can break these functions so that they don't even notice it.
As we do not care for overflowing on these functions, always keep the sum
using the larger variable around (unsigned long long) on simple_strtoull()
and cast it to (unsigned long) on simple_strtoul(), which then becomes
just a wrapper around simple_strtoull().
Code size decreases by 304 bytes:
text data bss dec hex filename
15534 0 8 15542 3cb6 vsprintf.o (ex lib/lib.a-BEFORE)
15230 0 8 15238 3b86 vsprintf.o (ex lib/lib.a-AFTER)
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/vsprintf.c | 48 |
1 files changed, 14 insertions, 34 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index a315138ece55..c50733a690f0 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c | |||
@@ -47,14 +47,14 @@ static unsigned int simple_guess_base(const char *cp) | |||
47 | } | 47 | } |
48 | 48 | ||
49 | /** | 49 | /** |
50 | * simple_strtoul - convert a string to an unsigned long | 50 | * simple_strtoull - convert a string to an unsigned long long |
51 | * @cp: The start of the string | 51 | * @cp: The start of the string |
52 | * @endp: A pointer to the end of the parsed string will be placed here | 52 | * @endp: A pointer to the end of the parsed string will be placed here |
53 | * @base: The number base to use | 53 | * @base: The number base to use |
54 | */ | 54 | */ |
55 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | 55 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
56 | { | 56 | { |
57 | unsigned long result = 0; | 57 | unsigned long long result = 0; |
58 | 58 | ||
59 | if (!base) | 59 | if (!base) |
60 | base = simple_guess_base(cp); | 60 | base = simple_guess_base(cp); |
@@ -76,54 +76,34 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | |||
76 | 76 | ||
77 | return result; | 77 | return result; |
78 | } | 78 | } |
79 | EXPORT_SYMBOL(simple_strtoul); | 79 | EXPORT_SYMBOL(simple_strtoull); |
80 | 80 | ||
81 | /** | 81 | /** |
82 | * simple_strtol - convert a string to a signed long | 82 | * simple_strtoul - convert a string to an unsigned long |
83 | * @cp: The start of the string | 83 | * @cp: The start of the string |
84 | * @endp: A pointer to the end of the parsed string will be placed here | 84 | * @endp: A pointer to the end of the parsed string will be placed here |
85 | * @base: The number base to use | 85 | * @base: The number base to use |
86 | */ | 86 | */ |
87 | long simple_strtol(const char *cp, char **endp, unsigned int base) | 87 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
88 | { | 88 | { |
89 | if (*cp == '-') | 89 | return simple_strtoull(cp, endp, base); |
90 | return -simple_strtoul(cp + 1, endp, base); | ||
91 | |||
92 | return simple_strtoul(cp, endp, base); | ||
93 | } | 90 | } |
94 | EXPORT_SYMBOL(simple_strtol); | 91 | EXPORT_SYMBOL(simple_strtoul); |
95 | 92 | ||
96 | /** | 93 | /** |
97 | * simple_strtoull - convert a string to an unsigned long long | 94 | * simple_strtol - convert a string to a signed long |
98 | * @cp: The start of the string | 95 | * @cp: The start of the string |
99 | * @endp: A pointer to the end of the parsed string will be placed here | 96 | * @endp: A pointer to the end of the parsed string will be placed here |
100 | * @base: The number base to use | 97 | * @base: The number base to use |
101 | */ | 98 | */ |
102 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) | 99 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
103 | { | 100 | { |
104 | unsigned long long result = 0; | 101 | if (*cp == '-') |
105 | 102 | return -simple_strtoul(cp + 1, endp, base); | |
106 | if (!base) | ||
107 | base = simple_guess_base(cp); | ||
108 | |||
109 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') | ||
110 | cp += 2; | ||
111 | |||
112 | while (isxdigit(*cp)) { | ||
113 | unsigned int value; | ||
114 | |||
115 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; | ||
116 | if (value >= base) | ||
117 | break; | ||
118 | result = result * base + value; | ||
119 | cp++; | ||
120 | } | ||
121 | if (endp) | ||
122 | *endp = (char *)cp; | ||
123 | 103 | ||
124 | return result; | 104 | return simple_strtoul(cp, endp, base); |
125 | } | 105 | } |
126 | EXPORT_SYMBOL(simple_strtoull); | 106 | EXPORT_SYMBOL(simple_strtol); |
127 | 107 | ||
128 | /** | 108 | /** |
129 | * simple_strtoll - convert a string to a signed long long | 109 | * simple_strtoll - convert a string to a signed long long |