Skip to contentSkip to conversation

container_of: add container_first() macro

PATCH Superseded Gustavo A. R. Silva 4 participants 4 messages Sep 20, 2024 – Sep 21, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 container_of: add container_first() macro

This is like container_of() but it has an assert to ensure that it's using the first struct member.

---
 include/linux/container_of.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/container_of.h +18 -0
index 713890c867be..928c5865c1b4 100644
@@ -22,6 +22,24 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })
 
+/**
+ * container_first - cast first member of a structure out to the containing
+ *		     structure
+ * @ptr:	     the pointer to the member.
+ * @type:	     the type of the container struct this is embedded in.
+ * @member:	     the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */
+#define container_first(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);                                   \
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
+		      __same_type(*(ptr), void),                        \
+		      "pointer type mismatch in container_of()");       \
+	static_assert(offsetof(type, member) == 0, "not first member");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+
 /**
  * container_of_const - cast a member of a structure out to the containing
  *			structure and preserve the const-ness of the pointer
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Gustavo A. R. Silva

This is like container_of() but it has an assert to ensure that it's using the first struct member.

I just remembered that Greg wanted this based on container_of_const().

Really container_of_const() should be renamed to container_of() and container_of() should be renamed to container_of_helper() and we would add a #define container_of_const container_of for the transition.

Quoted 23 lines
---
 include/linux/container_of.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/container_of.h +8 -0
index 713890c867be..928c5865c1b4 100644
@@ -22,6 +22,24 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })
 
+/**
+ * container_first - cast first member of a structure out to the containing
+ *		     structure
+ * @ptr:	     the pointer to the member.
+ * @type:	     the type of the container struct this is embedded in.
+ * @member:	     the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.

include/linux/container_of.h line 42

Quoted 10 lines
include/linux/container_of.h +10 -0
+ */
+#define container_first(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);                                   \
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
+		      __same_type(*(ptr), void),                        \
+		      "pointer type mismatch in container_of()");       \
+	static_assert(offsetof(type, member) == 0, "not first member");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+

I added too many blank lines.

regards,
dan carpenter

#2 lore raw
Quoted 6 lines · Dan Carpenter
Quoted 3 lines · Gustavo A. R. Silva

This is like container_of() but it has an assert to ensure that it's using the first struct member.

I just remembered that Greg wanted this based on container_of_const().

Yes, that would be best.

Quoted 3 lines

Really container_of_const() should be renamed to container_of() and container_of() should be renamed to container_of_helper() and we would add a #define container_of_const container_of for the transition.

I agree, but I wonder if it's safe to just do that already and how many build warnings would happen. Last I checked it was a lot.

thanks,

greg k-h

#3 lore raw
Quoted 9 lines · Greg KH
Quoted 6 lines · Dan Carpenter
Quoted 3 lines · Gustavo A. R. Silva

This is like container_of() but it has an assert to ensure that it's using the first struct member.

I just remembered that Greg wanted this based on container_of_const().

Yes, that would be best.

Okay, I'll change that.

--
Gustavo

Quoted 12 lines
Quoted 4 lines

Really container_of_const() should be renamed to container_of() and container_of() should be renamed to container_of_helper() and we would add a #define container_of_const container_of for the transition.

I agree, but I wonder if it's safe to just do that already and how many build warnings would happen. Last I checked it was a lot.

thanks,

greg k-h