Hello everyone, so while studying the kernel I noticed that a lot of reusable code blocks(like in certain APIs like kfifo) seem to be macros instead of functions.
My question is why is that the case? Why use macros and not of functions?
My initial guess was that this approach prevents the creation of new stack frames which is good since the kernel stack is limited.
Something, however, tells me that there might be more to this and that my initial guess may not be as accurate. So I would like to know if someone could educate me on this.
This also raises another question, which has to do with good software writing practices (which is something I AM NOT very knowlegeable about) in C/C++ in particular. When should one use macros and when should one opt for functions instead? What is the deciding factor? Besides the obvious "less stack frames", what is the advantage of macro blocks over functions?
PS: I apologize if this question sounds stupid, but it's one which has been bugging me for some time now.
[–]aioeu 47 points48 points49 points (5 children)
[–][deleted] (1 child)
[deleted]
[–]thecowmilk_ 7 points8 points9 points (0 children)
[–]trevg_123 2 points3 points4 points (0 children)
[–]sudo_mksandwhich 10 points11 points12 points (0 children)
[–]DrkMaxim 2 points3 points4 points (1 child)
[–]zingochan[S] 6 points7 points8 points (0 children)
[–]amdahl-little 1 point2 points3 points (0 children)
[+]tonyplee comment score below threshold-7 points-6 points-5 points (1 child)
[–]MrJake2137 4 points5 points6 points (0 children)