Loading...
Searching...
No Matches
container.h File Reference

Common macros and compiler attributes/pragmas configuration. More...

Detailed Description

Common macros and compiler attributes/pragmas configuration.

Author
René Kijewski rene..nosp@m.kije.nosp@m.wski@.nosp@m.fu-b.nosp@m.erlin.nosp@m..de
Michel Rottleuthner miche.nosp@m.l.ro.nosp@m.ttleu.nosp@m.thne.nosp@m.r@haw.nosp@m.-ham.nosp@m.burg..nosp@m.de

Definition in file container.h.

#include <stddef.h>
#include <stdint.h>
+ Include dependency graph for container.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define container_of(PTR, TYPE, MEMBER)    ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
 Returns the container of a pointer to a member.
 
#define index_of(ARRAY, ELEMENT)   (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0]))
 Returns the index of a pointer to an array element.
 
#define ARRAY_SIZE(a)   (sizeof((a)) / sizeof((a)[0]))
 Calculate the number of elements in a static array.
 

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   a)    (sizeof((a)) / sizeof((a)[0]))

Calculate the number of elements in a static array.

Parameters
[in]aArray to examine
Returns
The number of elements in the array a.

Definition at line 83 of file container.h.

◆ container_of

#define container_of (   PTR,
  TYPE,
  MEMBER 
)     ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))

Returns the container of a pointer to a member.

For a struct TYPE with a member MEMBER, given a pointer PTR to TYPE::MEMBER this function returns a pointer to the instance of TYPE.

E.g. for struct my_struct_t { ...; something_t n; ... } my_struct;, &my_struct == container_of(&my_struct.n, struct my_struct_t, n).

Parameters
[in]PTRpointer to a member
[in]TYPEa type name (a struct or union), container of PTR
[in]MEMBERname of the member of TYPE which PTR points to
Returns
Pointer to the container of PTR.

Definition at line 62 of file container.h.

◆ index_of

#define index_of (   ARRAY,
  ELEMENT 
)    (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0]))

Returns the index of a pointer to an array element.

Parameters
[in]ARRAYan array
[in]ELEMENTpointer to an array element
Returns
Index of the element in the array

Definition at line 74 of file container.h.