[<<][c][>>][..]
Thu Apr 28 12:28:44 EDT 2011

static inline: (pseudo-) separate interface from inplementation

I tend to use "static inline" functions a lot for simple accessors to
allow the compiler to perform more aggressive optimization.  The
problem is that this always needs to combine interface and
implementation for two reasons:

  - data structures need to be fully specified.

  - function bodies are visible.

The former you can't do anything about: compiler needs to know the
data layout so it's technically possible for the API user to abuse
this.  The latter makes it hard to read headers.

It seems a good idea to use the following: use forward declarations
for data structures and inline functions and stash the implementation
of both in a _priv.h file that is not supposed to be looked at by
human eyes, but is there for the compiler:


// foo.h
struct foo_data;
static inlien int foo_data_size(struct foo_data *x);
#include "foo_priv.h"

// foo_priv.h
struct foo_data {
       int a, b, c;
};
static inline int foo_data_size(struct foo_data *x) {
       return sizeof(foo_data);
}

Another issue whether one allows struct members as:

struct highlevel {
       struct lowlevel_foo foo;
       struct lowlevel_bar bar;
}

or needs to abstract using pointers and possible constructors such as
lowlevel_foo_new() and lowlevel_bar_new() to instantiate the objects.

struct lowlevel {
       struct lowlevel_foo *foo;
       struct lowlevel_bar *bar;
}




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