Skip to content

Commit 5d0291f

Browse files
committed
Add container_of helpers for embedded node design
Introduce a simple container operation to support embedded node design for the timer and scheduler subsystems This change allows container access from the embedded list node.
1 parent afa6148 commit 5d0291f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/private/utils.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88
*/
99

1010
#include <lib/libc.h>
11+
#include <stddef.h>
12+
13+
/*
14+
* container_of - get the pointer to the parent structure from a member pointer
15+
*
16+
* @ptr: pointer to the struct member
17+
* @type: type of the parent structure
18+
* @member: name of the member within the parent structure
19+
*
20+
* This macro computes the address of the parent structure by subtracting
21+
* the member's offset within the structure.
22+
*/
23+
#define container_of(ptr, type, member) \
24+
((type *) ((char *) (ptr) - offsetof(type, member)))
25+
26+
/* Return the tcb_t */
27+
#define tcb_from_global_node(p) container_of(p, tcb_t, global_node)
28+
#define tcb_from_mutex_node(p) container_of(p, tcb_t, mutex_node)
29+
30+
/* Member access for timer_t */
31+
#define timer_from_node(p) container_of(p, timer_t, t_node)
32+
#define timer_from_running_node(p) container_of(p, timer_t, t_running_node)
1133

1234
/* Compiler Optimization Hints
1335
*

0 commit comments

Comments
 (0)