[PATCH 1/3] stddef: Add offsetofp() and offsetofendp() for pointer arithmetic
Bence Csokas
bence.csokas at arm.com
Thu Nov 5 09:16:16 PST 2026
Many call sites of offsetof() use it for the purpose of pointer
artihmetic. Thus, a common usage pattern is:
offsetof(typeof(*ptr), member)
Capturing this in one macro helps keep lines short and terse, while
still making the purpose clear. It also helps discourage spelling out
the type name on every callsite, leading to more maintainable code.
Signed-off-by: Bence Csokas <bence.csokas at arm.com>
---
include/linux/stddef.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index e1851c50c89b..87f0e131d235 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -32,6 +32,22 @@ enum {
#define offsetofend(TYPE, MEMBER) \
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
+/**
+ * offsetofp() - Get the offset of a struct field from a pointer to the struct
+ *
+ * @P: Pointer to the structure
+ * @MEMBER: The member within the structure to get the start offset of
+ */
+#define offsetofp(P, MEMBER) offsetof(typeof(*(P)), MEMBER)
+
+/**
+ * offsetofendp() - Get the end of a struct field from a pointer to the struct
+ *
+ * @P: Pointer to the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofendp(P, MEMBER) offsetofend(typeof(*(P)), MEMBER)
+
/**
* struct_group() - Wrap a set of declarations in a mirrored struct
*
--
2.56.0
More information about the linux-snps-arc
mailing list