avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] inline vs. #define


From: David Brown
Subject: Re: [avr-gcc-list] inline vs. #define
Date: Wed, 26 Mar 2003 08:32:08 +0100

> On 25 Mar 2003 at 13:23, Andreas Trenkwalder wrote:
>
> > Hi,
> >
> > How are inline functions handled by avr-gcc?
> >
> > if i use
> > #define i2c_wait() while (!(TWCR & _BV(TWINT)))
> >
> > avr-size says:
> > text    data     bss     dec     hex filename
> > 388       0       1     389     185 i2c.elf
> >
> >
> > instead if i use
> > inline void i2c_wait(void)
> > {
> >  while (!(TWCR & _BV(TWINT)));
> > }
> >
> > avr-size output looks like
> > text    data     bss     dec     hex filename
> > 398       0       1     399     18f i2c.elf
> >
> > Why is there a difference in size?
>
> Take a look at the mixed C and assembly listing. You'll see that
> functions incur an overhead with prolog and epilog code. Inlined
> functions don't have this overhead.
>

My guess is that if you look at the generated assembly code, you will see
the same code generated for the calling function with both the #define'd and
inlined versions of i2c_wait.  But the "inline void" function has external
linkage, and must therefore be generated in full as a stand-alone function,
just in case another C module wants to use it.  The normal way to make
always-inlinded functions is to declare them "static inline" - then the
compiler knows that they will never be used externally, and can avoid
generating the original function.  "Static inline" functions are safe to put
in headers too - they are then as small and fast as a macro, with all the
advantages of a function (improved readability, and improved compile-time
checking).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]