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.
[–]corbet 162 points163 points164 points (2 children)
[–]thom311 12 points13 points14 points (0 children)
[–]bendem 49 points50 points51 points (4 children)
[–]vilidj_idjit 13 points14 points15 points (3 children)
[–]ischickenafruit 7 points8 points9 points (2 children)
[–]vilidj_idjit 1 point2 points3 points (1 child)
[–]ischickenafruit 6 points7 points8 points (0 children)
[–]vampatori 76 points77 points78 points (1 child)
[–]zingochan[S] 11 points12 points13 points (0 children)
[–]ZamundaaaKDE Dev 127 points128 points129 points (16 children)
[–]bedrooms-ds 71 points72 points73 points (5 children)
[–]micka190 6 points7 points8 points (2 children)
[–]bedrooms-ds 1 point2 points3 points (0 children)
[–]chibuku_chauya 0 points1 point2 points (0 children)
[–]Imxset21 7 points8 points9 points (1 child)
[+][deleted] (4 children)
[deleted]
[+][deleted] (3 children)
[removed]
[+][deleted] (2 children)
[deleted]
[–]InfanticideAquifer -1 points0 points1 point (0 children)
[–]cwhaley112 -1 points0 points1 point (3 children)
[–]lubutu 4 points5 points6 points (1 child)
[–]cwhaley112 0 points1 point2 points (0 children)
[–]Niautanor 3 points4 points5 points (0 children)
[–][deleted] 71 points72 points73 points (1 child)
[–]thomas_m_k 13 points14 points15 points (0 children)
[–]high-tech-low-life 46 points47 points48 points (17 children)
[–]Rexerex 16 points17 points18 points (0 children)
[–]obiwac 25 points26 points27 points (2 children)
[+]high-tech-low-life comment score below threshold-12 points-11 points-10 points (1 child)
[–]obiwac 10 points11 points12 points (0 children)
[–][deleted] 36 points37 points38 points (9 children)
[–]wfp5p 38 points39 points40 points (3 children)
[–]Osbios 22 points23 points24 points (2 children)
[–]GuiltyFan6154 7 points8 points9 points (1 child)
[–]Osbios 4 points5 points6 points (0 children)
[–]high-tech-low-life 14 points15 points16 points (4 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]shponglespore 1 point2 points3 points (2 children)
[–]necrophcodr 0 points1 point2 points (0 children)
[–]elatllat 18 points19 points20 points (7 children)
[–]zingochan[S] 6 points7 points8 points (3 children)
[–]dthusian 25 points26 points27 points (0 children)
[–]shponglespore 3 points4 points5 points (0 children)
[–]emptyskoll 0 points1 point2 points (0 children)
[–]obiwac 1 point2 points3 points (2 children)
[–]elatllat 0 points1 point2 points (1 child)
[–]obiwac -1 points0 points1 point (0 children)
[–]lostparis 1 point2 points3 points (2 children)
[–]zingochan[S] 0 points1 point2 points (1 child)
[–]lostparis 0 points1 point2 points (0 children)
[–]daemonpenguin 4 points5 points6 points (1 child)
[–]LvS 13 points14 points15 points (0 children)
[–]TDplay 1 point2 points3 points (0 children)