What is the adventage of macros against function
Currently I am trying to learn linux kernel while looking around I saw that a lot of function likes macros are defined.What is the adventage of macros over functions. Thanks -- İsmail Baydan
2011/4/27 İsmail Baydan <ibaydan@gmail.com>:
Currently I am trying to learn linux kernel while looking around I saw that a lot of function likes macros are defined.What is the adventage of macros over functions. If the size of the function is very small, then the macro is better when compared to function. The macros are replaced at the time of preprocessing. However, if there is a function, then there may be overhead in calling the function. When there is a call, the return address needs to be stored on the stack. This makes some overhead. So, if the size of a fucction is big, then define it as function otherwise make it as a macro.
I guess, this helps you a bit. Regards, Ramya.
Thanks
-- İsmail Baydan
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
2011/4/27 Ramya Desai <ramya.desai@gmail.com>
2011/4/27 İsmail Baydan <ibaydan@gmail.com>:
Currently I am trying to learn linux kernel while looking around I saw that a lot of function likes macros are defined.What is the adventage of macros over functions. If the size of the function is very small, then the macro is better when compared to function. The macros are replaced at the time of preprocessing. However, if there is a function, then there may be overhead in calling the function. When there is a call, the return address needs to be stored on the stack. This makes some overhead. So, if the size of a fucction is big, then define it as function otherwise make it as a macro.
I guess, this helps you a bit.
Regards, Ramya.
Thanks
-- İsmail Baydan
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
thanks -- İsmail Baydan
On Wed, Apr 27, 2011 at 02:16:08PM +0530, Ramya Desai wrote:
2011/4/27 İsmail Baydan <ibaydan@gmail.com>:
Currently I am trying to learn linux kernel while looking around I saw that a lot of function likes macros are defined.What is the adventage of macros over functions. If the size of the function is very small, then the macro is better when compared to function. The macros are replaced at the time of preprocessing. However, if there is a function, then there may be overhead in calling the function. When there is a call, the return address needs to be stored on the stack. This makes some overhead. So, if the size of a fucction is big, then define it as function otherwise make it as a macro.
That's why GCC (and probably other compilers, too) has the the inline keyword. One important use case of macros is when the needed functionality cannot be implemented in a function. include/linux/kernel.h offers some good examples (e.g. ARRAY_SIZE, container_of, swap). Hope this helps, Jonathan Neuschäfer
On Wed, 27 Apr 2011, Jonathan Neuschäfer wrote:
That's why GCC (and probably other compilers, too) has the the inline keyword.
inline keyword is widely supported by c compilers. Modern compiles even now support optimizations based on automatic inline to speed up programs etc. (OPTIMIZE_INLINING=y. It uses several flags to use that optimization). -- Ozan, BSc, BEng
Actually you will see lot of macros defined to achieve small and modular functionality. this makes your code faster to execute but the binary size will be bigger. You will see this in kernel code more because we have very low stack area sometime may be one page(4096 bytes) of memory. automatic variable declare and defined in function will occupy space in stack. In application side C expert suggest to have functions because we have very big stack area and function adds readability with easy debugging. I hope this will help you in your kernel context. Thanks, Naveen 2011/4/27 İsmail Baydan <ibaydan@gmail.com>
Currently I am trying to learn linux kernel while looking around I saw that a lot of function likes macros are defined.What is the adventage of macros over functions. Thanks
-- İsmail Baydan
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
participants (5)
-
İsmail Baydan -
Jonathan Neuschäfer -
Naveen Kumar -
Ozan Türkyılmaz -
Ramya Desai