include/linux/slab.h: use for() and left shift to calculate
Subsystems (2)
PATCH 1/1 include/linux/slab.h: use for() and left shift to calculate
use for() and left shift to calculate the value that compared with size.
--- include/linux/slab.h | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-)
include/linux/slab.h +8 -24
@@ -347,6 +347,8 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)*/ static __always_inline unsigned int kmalloc_index(size_t size) { +int i; +if (!size)return 0 ;
@@ -357,30 +359,12 @@ static __always_inline unsigned int kmalloc_index(size_t size)return 1 ;if (KMALLOC_MIN_SIZE <=64 && size >128 && size <=192 )return 2 ; -if (size <=8 )return 3 ; -if (size <=16 )return 4 ; -if (size <=32 )return 5 ; -if (size <=64 )return 6 ; -if (size <=128 )return 7 ; -if (size <=256 )return 8 ; -if (size <=512 )return 9 ; -if (size <=1024 )return 10 ; -if (size <=2 *1024 )return 11 ; -if (size <=4 *1024 )return 12 ; -if (size <=8 *1024 )return 13 ; -if (size <=16 *1024 )return 14 ; -if (size <=32 *1024 )return 15 ; -if (size <=64 *1024 )return 16 ; -if (size <=128 *1024 )return 17 ; -if (size <=256 *1024 )return 18 ; -if (size <=512 *1024 )return 19 ; -if (size <=1024 *1024 )return 20 ; -if (size <=2 *1024 *1024 )return 21 ; -if (size <=4 *1024 *1024 )return 22 ; -if (size <=8 *1024 *1024 )return 23 ; -if (size <=16 *1024 *1024 )return 24 ; -if (size <=32 *1024 *1024 )return 25 ; -if (size <=64 *1024 *1024 )return 26 ; + +/* The maximum of size is 2^26 = 64 * 1024 * 1024 */ +for (i =3 ; i <=26 ; i++) { +if (size <= (1 << i)) +return i; + }BUG ();/* Will never be reached. Needed because the compiler may complain */
Signature · 1 line
2.29.0
Quoted 1 line · Yejune Deng
use
for()and left shift to calculate the value that compared with size.
Quoted 1 line · Yejune Deng
use
for()and left shift to calculate the value that compared with size.
There is a reason for the madness...
The current code was written so compilers can do proper constant folding and eliminate the whole function entirely.
If you want this change then please verify that all compilers currently in use with this code do proper constant folding and never generate code for the for loop.
Thank you. I hadn't thought of that。
Quoted 13 lines · Christoph Lameter
Quoted 1 line · Yejune Deng
use
for()and left shift to calculate the value that compared with size.There is a reason for the madness...
The current code was written so compilers can do proper constant folding and eliminate the whole function entirely.
If you want this change then please verify that all compilers currently in use with this code do proper constant folding and never generate code for the for loop.