add typeof_member() macro
PATCH 1/2 add typeof_member() macro
Add typeof_member() macro so that types can be exctracted without introducing dummy variables.
---
include/linux/kernel.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/kernel.h +2 -0
@@ -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) \
PATCH 2/2 proc: use typeof_member() macro
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.
---
Note 2: 100% false positives with checkpatch.pl :^)
fs/proc/inode.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-)
fs/proc/inode.c +17 -10
@@ -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;/*
Re: [PATCH 2/2] proc: use typeof_member() macro
Quoted 17 lines · On Wed, 29 May 2019 22:11:10 +0300 Alexey Dobriyan <[email protected]> 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....
fs/proc/inode.c static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) · line 203
Quoted 5 lines
fs/proc/inode.c +1
-1@@ -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.
From: Alexey Dobriyan
Quoted 14 lines
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 <[email protected]>---
include/linux/kernel.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/kernel.h line 91
Quoted 4 lines
include/linux/kernel.h +1
-0@@ -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)
Quoted 18 lines · David Laight
From: Alexey Dobriyan
Quoted 17 lines
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 <[email protected]>---
include/linux/kernel.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/kernel.h +0
-0@@ -88,6 +88,8 @@*/ #define FIELD_SIZEOF (t, f) (sizeof(((t*)0)->f))
Quoted 6 lines
include/linux/kernel.h line 91
Quoted 1 line
include/linux/kernel.h +1
-0+#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.
Quoted 5 lines · David Laight
From: Alexey Dobriyan
Quoted 1 line
+#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.
Quoted 1 line
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.