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)
 Returns the container of a pointer to a member.
 
#define index_of(ARRAY, ELEMENT)
 Returns the index of a pointer to an array element.
 
#define ARRAY_SIZE(a)
 Calculate the number of elements in a static array.
 
#define STATIC_STRLEN(string)
 Length of C string excluding the null terminator as an integer constant expression.
 

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE ( a)
Value:
(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 79 of file container.h.

◆ container_of

#define container_of ( PTR,
TYPE,
MEMBER )
Value:
((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 58 of file container.h.

◆ index_of

#define index_of ( ARRAY,
ELEMENT )
Value:
(((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 70 of file container.h.

◆ STATIC_STRLEN

#define STATIC_STRLEN ( string)
Value:
(sizeof(string) - 1)

Length of C string excluding the null terminator as an integer constant expression.

Parameters
[in]stringString literal expression or char array sized by initializer
Returns
Number of bytes occupied by string characters, excluding the null byte at the end, as an integer compile-time expression

Use this macro to determine the length of a C string at compile-time, without resorting to strlen.

Warning
Do not use this macro on char * pointer variables.
Note
Modern compilers may be able to fold calls to strlen given string literals or visible char buffers into integers at compile time, too. If the string contains additional null bytes, you should use strlen instead.

Examples

const char string[] = "Hello, World!";
char characters[STATIC_STRLEN(string)] = { 0 };
memcpy(characters, string, STATIC_STRLEN(string));
#define STATIC_STRLEN(string)
Length of C string excluding the null terminator as an integer constant expression.
Definition container.h:116
static_assert(STATIC_STRLEN("x") == 1, "");
#define MESSAGE(audience) "Hello, " audience "!"
static_assert(STATIC_STRLEN(MESSAGE("World")) < 42, "Message too long!");

Definition at line 116 of file container.h.