Commit 5a18d07c authored by Ammar Faizi's avatar Ammar Faizi Committed by Paul E. McKenney
Browse files

tools/nolibc/types: Implement `offsetof()` and `container_of()` macro



Implement `offsetof()` and `container_of()` macro. The first use case
of these macros is for `malloc()`, `realloc()` and `free()`.

Acked-by: default avatarWilly Tarreau <w@1wt.eu>
Signed-off-by: default avatarAmmar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
parent 544fa1a2
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -191,4 +191,15 @@ struct stat {
#define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
#define minor(dev) ((unsigned int)(((dev) & 0xff))

#ifndef offsetof
#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
#endif

#ifndef container_of
#define container_of(PTR, TYPE, FIELD) ({			\
	__typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR);	\
	(TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD));	\
})
#endif

#endif /* _NOLIBC_TYPES_H */