ssm  0.0.2
Runtime Library for the Sparse Synchronous Model
Utilities

A collection of helpful definitions which are not specific to SSM. More...

Discussion

A collection of helpful definitions which are not specific to SSM.

Macros

#define member_type(type, member)   __typeof__(((type *)0)->member)
 Obtain the type of a struct member. More...
 
#define container_of(ptr, type, member)
 Obtain the pointer to an outer, enclosing struct. More...
 

Macro Definition Documentation

◆ member_type

#define member_type (   type,
  member 
)    __typeof__(((type *)0)->member)

Obtain the type of a struct member.

Intended for use in container_of, for type safety.

When GNU C is not available, falls back to ISO C99 and returns const void (see https://stackoverflow.com/a/10269925/10497710).

For instance, given the following struct definition:

struct foo { int bar; float baz; };

member_type(struct foo, baz) expands to float.

Parameters
typethe struct type.
memberthe name of the member in type.
Returns
the type of member in type.

Definition at line 1196 of file ssm.h.

◆ container_of

#define container_of (   ptr,
  type,
  member 
)
Value:
((type *)((char *)(member_type(type, member) *){ptr} - \
offsetof(type, member)))
#define member_type(type, member)
Obtain the type of a struct member.
Definition: ssm.h:1196

Obtain the pointer to an outer, enclosing struct.

For example, with the following struct definition:

struct foo { int bar; float baz; };

Given some float *p, container_of(p, struct foo, baz) will return a pointer to the enclosing struct foo. The caller is responsible for ensuring that such an enclosing pointer actually exists.

Use this macro instead of pointer casting or pointer arithmetic; this macro is more principled in what it does and, with GNU C support, will trigger compiler warnings when ptr and the member type do not agree.

Adapted from the Linux kernel source (see https://stackoverflow.com/a/10269925/10497710).

Parameters
ptrpointer to member.
typetype of enclosing struct.
membername of member in enclosing struct.
Returns
pointer to enclosing struct.

Definition at line 1226 of file ssm.h.