[<<][c][>>][..]
Sun May 8 23:27:35 EDT 2016

Inline functions and instantiation (fast abstract code).

The pattern is this:

- generic:
  struct config { int param; }
  static inline int fn(struct config c, int arg) { return c.param + arg; }

- specific instantiation:
  static const struct config c0 = { .param = 123 };
  int fn0(int arg) { fn(c0, arg); }


It seems that most compilers know how to fold the constants even if
it's a big struct.  This makes it useful for deeply embedded code
where you want to avoid the abstraction overhead.  This can be
significant for peripheral access that in most cases simplifies to one
or two inline bus accesses, as opposed to a function that inspects the
struct at run time).

A way to use this is to put all instantiations in a main.c file, and
have library code refer to instantiated versions of the generic inline
code.

Note that pointers work equally well as long as the struct referred to
is const.  If it is not, it is possible to externally modifiy it so
compiler can't fold it.  If struct is also static, no remnant will be
left in the file.

It might be better practice to use pointers.  In case of non-optimized
code this would be not horribily inefficient (call by value).




[Reply][About]
[<<][c][>>][..]