Serval_StructCtx.h File Reference

Struct context moduleThis header provides the macro GET_CONTEXT_OF_MEMBER which calculates the address of a struct from a given struct member. More...

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


#define GET_CONTEXT_OF_MEMBER(ContextTypeName, ContextMemberName, MemberPtr)   SERVAL_CONTAINER_OF(MemberPtr, ContextTypeName, ContextMemberName)
#define SERVAL_CONTAINER_OF(ptr, containerType, member)   ((containerType *)((char *)ptr - offsetof(containerType, member)))

Macro Definition Documentation

#define GET_CONTEXT_OF_MEMBER (   ContextTypeName,
)    SERVAL_CONTAINER_OF(MemberPtr, ContextTypeName, ContextMemberName)

This macro computes the address of the context struct of a given struct member. It can be used as in the following example.

use SERVAL_CONTAINER_OF instead typedef struct Context_S { int member; } Context_T;

Context_T context; int *p = &context.member;

Context_T *ctx_ptr = GET_CONTEXT_OF_MEMBER(Context_T, member, p); In this example it holds that ctx_ptr is equal &context.

[in]ContextTypeNamethe name of the expected data type of the containing structure
[in]ContextMemberNamethe member name of the callable object inside the enclosing structure
[in]MemberPtra pointer to the struct member
a pointer to the enclosing structure.
#define SERVAL_CONTAINER_OF (   ptr,
)    ((containerType *)((char *)ptr - offsetof(containerType, member)))

This macro computes the address of the container of a given pointer to a field in the container. It can be used as in the following example. typedef struct Container_S { int field1; int field2; } Container_T;

Context_T container; int *p = &(container.field2);

Container_T *container_ptr = GET_CONTAINER_OF(p, Container_T, field2); assert(container_ptr == &container)

[in]ptrThe pointer for which to get the container
[in]containerTypeThe type of the container
[in]memberThe name of the member that ptr points to
a pointer to the container.

This documentation file has been automatically generated on Fri Oct 4 2019 03:47:21 by doxygen 1.8.8