[Patch] fix unsigned long fast_strtoul_10(char **endptr) for Linux 2.4
Ralf Friedl
Ralf.Friedl at online.de
Tue Feb 28 12:27:48 UTC 2012
walter harms wrote:
> Enabling the fast proc scan code for linux2.4 causes top
> to display strange numbers [see:possible bug in proc fast scan code]
>
> the reason for this is that the last char in proc/$$/stat is "\n".
> This is no problem for > 2.4 since the stats line contains additional chars.
>
> Please be aware that the current code does NOT stop at \0 ! adding more
> tokens to read will cause the code to read beyond \0 !
>
> i also killed the variable c what saves 1 byte.
>
This variable would live on the stack if it would ever leave the
register (which it doesn't, even on a x86 with very few registers).
Independent of this patch, you do realize that initializing n to
*str-'0' is some kind of manual loop unrolling? Both of the following
versions are smaller, at the cost of one more pass through the loop. I
don't know whether it is called often enough for this to matter.
The first version is smaller with -O9 -m32, -Os -m32 and -Os -m64 while
it is larger with -O9 -m64 with GCC 4.6.2. In each case both versions
are smaller than the original.
unsigned long fast_strtoul_10(char **endptr)
{
char c;
char *str = *endptr;
unsigned long n = 0;
while ((c = *str++) > ' ')
n = n*10 + (c - '0');
*endptr = str; /* We skip trailing space! */
return n;
}
unsigned long fast_strtoul_10(char **endptr)
{
char c;
char *str = *endptr;
unsigned long n = 0;
while (*str > ' ')
n = n*10 + (*str++ - '0');
*endptr = str + 1; /* We skip trailing space! */
return n;
}
More information about the busybox
mailing list