linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] add typeof_member() macro
@ 2019-05-29 19:07 Alexey Dobriyan
  2019-05-29 19:11 ` [PATCH 2/2] proc: use " Alexey Dobriyan
  2019-05-30 11:37 ` [PATCH 1/2] add " David Laight
  0 siblings, 2 replies; 6+ messages in thread
From: Alexey Dobriyan @ 2019-05-29 19:07 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

Add typeof_member() macro so that types can be exctracted without
introducing dummy variables.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 include/linux/kernel.h |    2 ++
 1 file changed, 2 insertions(+)

--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -88,6 +88,8 @@
  */
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 
+#define typeof_member(T, m)	typeof(((T*)0)->m)
+
 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
 
 #define DIV_ROUND_DOWN_ULL(ll, d) \

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] proc: use typeof_member() macro
  2019-05-29 19:07 [PATCH 1/2] add typeof_member() macro Alexey Dobriyan
@ 2019-05-29 19:11 ` Alexey Dobriyan
  2019-05-29 22:33   ` Andrew Morton
  2019-05-30 11:37 ` [PATCH 1/2] add " David Laight
  1 sibling, 1 reply; 6+ messages in thread
From: Alexey Dobriyan @ 2019-05-29 19:11 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

Don't repeat function signatures twice.

This is a kind-of-precursor for "struct proc_ops".

Note:

	typeof(pde->proc_fops->...) ...;

can't be used because ->proc_fops is "const struct file_operations *".
"const" prevents assignment down the code and it can't be deleted
in the type system.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

	Note 2: 100% false positives with checkpatch.pl :^)

 fs/proc/inode.c |   27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -200,7 +200,8 @@ static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	loff_t rv = -EINVAL;
 	if (use_pde(pde)) {
-		loff_t (*llseek)(struct file *, loff_t, int);
+		typeof_member(struct file_operations, llseek) llseek;
+
 		llseek = pde->proc_fops->llseek;
 		if (!llseek)
 			llseek = default_llseek;
@@ -212,10 +213,11 @@ static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
 
 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 {
-	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	ssize_t rv = -EIO;
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, read) read;
+
 		read = pde->proc_fops->read;
 		if (read)
 			rv = read(file, buf, count, ppos);
@@ -226,10 +228,11 @@ static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count,
 
 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
 {
-	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	ssize_t rv = -EIO;
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, write) write;
+
 		write = pde->proc_fops->write;
 		if (write)
 			rv = write(file, buf, count, ppos);
@@ -242,8 +245,9 @@ static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
 {
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	__poll_t rv = DEFAULT_POLLMASK;
-	__poll_t (*poll)(struct file *, struct poll_table_struct *);
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, poll) poll;
+
 		poll = pde->proc_fops->poll;
 		if (poll)
 			rv = poll(file, pts);
@@ -256,8 +260,9 @@ static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigne
 {
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	long rv = -ENOTTY;
-	long (*ioctl)(struct file *, unsigned int, unsigned long);
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, unlocked_ioctl) ioctl;
+
 		ioctl = pde->proc_fops->unlocked_ioctl;
 		if (ioctl)
 			rv = ioctl(file, cmd, arg);
@@ -271,8 +276,9 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned
 {
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	long rv = -ENOTTY;
-	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, compat_ioctl) compat_ioctl;
+
 		compat_ioctl = pde->proc_fops->compat_ioctl;
 		if (compat_ioctl)
 			rv = compat_ioctl(file, cmd, arg);
@@ -286,8 +292,9 @@ static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct proc_dir_entry *pde = PDE(file_inode(file));
 	int rv = -EIO;
-	int (*mmap)(struct file *, struct vm_area_struct *);
 	if (use_pde(pde)) {
+		typeof_member(struct file_operations, mmap) mmap;
+
 		mmap = pde->proc_fops->mmap;
 		if (mmap)
 			rv = mmap(file, vma);
@@ -305,7 +312,7 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
 	unsigned long rv = -EIO;
 
 	if (use_pde(pde)) {
-		typeof(proc_reg_get_unmapped_area) *get_area;
+		typeof_member(struct file_operations, get_unmapped_area) get_area;
 
 		get_area = pde->proc_fops->get_unmapped_area;
 #ifdef CONFIG_MMU
@@ -326,8 +333,8 @@ static int proc_reg_open(struct inode *inode, struct file *file)
 {
 	struct proc_dir_entry *pde = PDE(inode);
 	int rv = 0;
-	int (*open)(struct inode *, struct file *);
-	int (*release)(struct inode *, struct file *);
+	typeof_member(struct file_operations, open) open;
+	typeof_member(struct file_operations, release) release;
 	struct pde_opener *pdeo;
 
 	/*

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] proc: use typeof_member() macro
  2019-05-29 19:11 ` [PATCH 2/2] proc: use " Alexey Dobriyan
@ 2019-05-29 22:33   ` Andrew Morton
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2019-05-29 22:33 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: linux-kernel

On Wed, 29 May 2019 22:11:10 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:

> Don't repeat function signatures twice.
> 
> This is a kind-of-precursor for "struct proc_ops".
> 
> Note:
> 
> 	typeof(pde->proc_fops->...) ...;
> 
> can't be used because ->proc_fops is "const struct file_operations *".
> "const" prevents assignment down the code and it can't be deleted
> in the type system.
>
> ...
>
> --- a/fs/proc/inode.c
> +++ b/fs/proc/inode.c
> @@ -200,7 +200,8 @@ static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
>  	struct proc_dir_entry *pde = PDE(file_inode(file));
>  	loff_t rv = -EINVAL;
>  	if (use_pde(pde)) {
> -		loff_t (*llseek)(struct file *, loff_t, int);
> +		typeof_member(struct file_operations, llseek) llseek;

ooh.  That's pretty nifty.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH 1/2] add typeof_member() macro
  2019-05-29 19:07 [PATCH 1/2] add typeof_member() macro Alexey Dobriyan
  2019-05-29 19:11 ` [PATCH 2/2] proc: use " Alexey Dobriyan
@ 2019-05-30 11:37 ` David Laight
  2019-05-30 11:53   ` Joe Perches
  2019-05-30 19:57   ` Alexey Dobriyan
  1 sibling, 2 replies; 6+ messages in thread
From: David Laight @ 2019-05-30 11:37 UTC (permalink / raw)
  To: 'Alexey Dobriyan', akpm; +Cc: linux-kernel

From: Alexey Dobriyan
> Sent: 29 May 2019 20:07
> 
> Add typeof_member() macro so that types can be exctracted without
> introducing dummy variables.
> 
> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> ---
> 
>  include/linux/kernel.h |    2 ++
>  1 file changed, 2 insertions(+)
> 
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -88,6 +88,8 @@
>   */
>  #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> 
> +#define typeof_member(T, m)	typeof(((T*)0)->m)

Should probably be 't' (not 'T') and upper case ?

Hmmm.... the #define is longer that what it expands to ...

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] add typeof_member() macro
  2019-05-30 11:37 ` [PATCH 1/2] add " David Laight
@ 2019-05-30 11:53   ` Joe Perches
  2019-05-30 19:57   ` Alexey Dobriyan
  1 sibling, 0 replies; 6+ messages in thread
From: Joe Perches @ 2019-05-30 11:53 UTC (permalink / raw)
  To: David Laight, 'Alexey Dobriyan', akpm; +Cc: linux-kernel

On Thu, 2019-05-30 at 11:37 +0000, David Laight wrote:
> From: Alexey Dobriyan
> > Sent: 29 May 2019 20:07
> > 
> > Add typeof_member() macro so that types can be exctracted without
> > introducing dummy variables.
> > 
> > Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> > ---
> > 
> >  include/linux/kernel.h |    2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -88,6 +88,8 @@
> >   */
> >  #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> > 
> > +#define typeof_member(T, m)	typeof(((T*)0)->m)

>
> Should probably be 't' (not 'T') and upper case ?
> 
> Hmmm.... the #define is longer that what it expands to ...

While I did object to the avoidance in the obvious
misnaming of FIELD_SIZEOF, this could reasonably
be named FIELD_TYPEOF for symmetry.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] add typeof_member() macro
  2019-05-30 11:37 ` [PATCH 1/2] add " David Laight
  2019-05-30 11:53   ` Joe Perches
@ 2019-05-30 19:57   ` Alexey Dobriyan
  1 sibling, 0 replies; 6+ messages in thread
From: Alexey Dobriyan @ 2019-05-30 19:57 UTC (permalink / raw)
  To: David Laight; +Cc: akpm, linux-kernel

On Thu, May 30, 2019 at 11:37:42AM +0000, David Laight wrote:
> From: Alexey Dobriyan

> > +#define typeof_member(T, m)	typeof(((T*)0)->m)
> 
> Should probably be 't' (not 'T') and upper case ?

T comes from C++ where they uppercase types in template arguments.
It makes sense as types stand out sligthly around lowercase identifiers.

> Hmmm.... the #define is longer that what it expands to ...

It hides cast of 0 which is implementation detail :^)

Proper syntax would be

	typeof(struct foo::bar)

but one can only dream.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-05-30 19:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-29 19:07 [PATCH 1/2] add typeof_member() macro Alexey Dobriyan
2019-05-29 19:11 ` [PATCH 2/2] proc: use " Alexey Dobriyan
2019-05-29 22:33   ` Andrew Morton
2019-05-30 11:37 ` [PATCH 1/2] add " David Laight
2019-05-30 11:53   ` Joe Perches
2019-05-30 19:57   ` Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).