summaryrefslogtreecommitdiff
path: root/tools/include
diff options
context:
space:
mode:
authorAmmar Faizi <ammarfaizi2@gnuweeb.org>2022-03-29 13:17:34 +0300
committerPaul E. McKenney <paulmck@kernel.org>2022-04-21 03:05:46 +0300
commit5a18d07ce3006dbcb3c4cfc7bf1c094a5da19540 (patch)
tree733f0c3e19f7e9717bb29b31a718fb6a97ef56d3 /tools/include
parent544fa1a2d3e61c954ab531f2c790bc79c1745606 (diff)
downloadlinux-5a18d07ce3006dbcb3c4cfc7bf1c094a5da19540.tar.xz
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: Willy Tarreau <w@1wt.eu> Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Diffstat (limited to 'tools/include')
-rw-r--r--tools/include/nolibc/types.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 357e60ad38a8..959997034e55 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -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 */