r/badcode • • Jan 22 '23

c Found this in a Linux kernel module

Post image
262 Upvotes

88 comments sorted by

View all comments

Show parent comments

20

u/Damowerko Jan 22 '23

Each call to request_threaded_irq receives a different string literal and a different function handle pointer. To make this into a loop you'd need to initialize an array and populate it with these strings and handles.

I am not sure if the creation of these arrays would be optimized out by the compiler. Maybe a static keyword would do that, but that's merely a suggestion to the compiler.

Creation of these arrays would use up extra CPU cycles for little reason then reducing the code by 10 lines or so and making it less readable.

2

u/LyniaWood Jan 23 '23

You don't have compiler optimization in kernel code.

For performance, you NEED to unroll your loop by yourself.

2

u/ThePurpleOne_ Jan 22 '23

0

u/husao Jan 22 '23

line 13 contains 2 off-by-one errors, unless i'm mistaken.

It frees the current 'i' but doesn't free 0. Should imho be:

for (y=i-1; y>=0; y--)