I was poking around the definitions for things like CGPoint for hints on how to create my own functions but I don’t know the purpose of CG_INLINE. What is happening behind the scenes here?
CG_INLINE CGPoint
CGPointMake(CGFloat x, CGFloat y)
{
CGPoint p; p.x = x; p.y = y; return p;
}
CG_INLINE CGSize
CGSizeMake(CGFloat width, CGFloat height)
{
CGSize size; size.width = width; size.height = height; return size;
}
Inline functions are compiled into the call site, rather than being compiled as a single block of function code and call instructions issued when the function is used. With care, this provides a little more speed and greater numbers of cache hits. However, the history of
inlinein C and C++ is rocky, so this macro effectively provides a compiler independentstatic inlinebehaviour. Looking at the definition:So…
__STDC_VERSION__of an appropriate version (in this case >= C99), this meansstatic inline(as C99 allows this natively)inlinenatively.static __inline__. The use of__inline__is the GCC specific inline specifier for previous C standards where inline is unsupported : http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Alternate-Keywords.html.static.Why bother with all of these definitions? Because Apple, in their history, have been through a fair few compilers. In the days of yore, the Codewarrior C compiler was the tool of choice for users. Since OS X, Apple have been using Objective C and C++ via an (originally modifier) GCC. Recently, they’re transitioning to clang. This macro covers all the cases (and, given how new Core Graphics is, I suspect is a modified version of an older macro).
However, many compilers will ignore inline annotations these days, as their optimisers are better than the hints provided by the programmer. In your own code, don’t bother with it (in native form, or via this macro) unless you’re really sure you need it (and have proven it useful via profiling). Of course, you may still want
static– the above advice covers theinlinebehaviour.