From mboxrd@z Thu Jan 1 00:00:00 1970 From: j.neuschaefer@gmx.net (Jonathan =?utf-8?Q?Neusch=C3=A4fer?=) Date: Wed, 27 Apr 2011 14:25:43 +0200 Subject: What is the adventage of macros against function In-Reply-To: References: Message-ID: <20110427122543.GA1290@debian.debian> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org On Wed, Apr 27, 2011 at 02:16:08PM +0530, Ramya Desai wrote: > 2011/4/27 ?smail Baydan : > > 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