Solaris Software
Loading...
Searching...
No Matches
structof.h
Go to the documentation of this file.
1/**
2 * @file structof.h
3 * @brief Container-of macro for intrusive data structures.
4 *
5 * Allows recovering a pointer to the enclosing struct from a pointer to
6 * one of its members — enabling zero-overhead intrusive lists and trees
7 * without extra allocations, as used in lely-core.
8 *
9 * Naming conventions used in this file:
10 * - Macros: SPP_STRUCTOF
11 */
12
13#ifndef SPP_STRUCTOF_H
14#define SPP_STRUCTOF_H
15
16#include <stddef.h>
17
18/**
19 * @brief Recover a pointer to the containing struct from a member pointer.
20 *
21 * @param p_member Pointer to the member field.
22 * @param type Type of the containing struct.
23 * @param member Name of the member field inside @p type.
24 *
25 * @return Pointer to the enclosing @p type instance.
26 *
27 * Example:
28 * @code
29 * typedef struct { int x; ListNode_t node; } MyItem_t;
30 * ListNode_t *p_node = ...;
31 * MyItem_t *p_item = SPP_STRUCTOF(p_node, MyItem_t, node);
32 * @endcode
33 */
34#define SPP_STRUCTOF(p_member, type, member) \
35 ((type *)((char *)(p_member) - offsetof(type, member)))
36
37#endif /* SPP_STRUCTOF_H */