partx: introduce INIT_BLGPG macro
PATCH 1/1 partx: introduce INIT_BLGPG macro
The disk-utils/partx.h provides three functions:
partx_{del,add,resize}_partition() that contain almost the same code
- initialization of the blkpg_partition and blkpg_ioctl_arg structs.
Let's provide INIT_PKG macro that will do all work to prevent code duplication.
--- disk-utils/partx.h | 55 ++++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 33 deletions(-)
disk-utils/partx.h +22 -33
@@ -16,21 +16,26 @@# define BLKPG_RESIZE_PARTITION 3/* since Linux 3.6 */ #endif +#define INIT_BLKPG (ioctl_arg, action, partno, start, size) do { \ + struct blkpg_partition p; \ + p.pno = partno; \ + p.start = start; \ + p.length = size; \ + p.devname[0] = 0; \ + p.volname[0] = 0; \ + ioctl_arg.op = action; \ + ioctl_arg.flags = 0; \ + ioctl_arg.datalen = sizeof(p); \ + ioctl_arg.data = &p; \ +} while (0) +static inline int partx_del_partition (int fd,unsigned int partno) {struct blkpg_ioctl_arg a; -struct blkpg_partition p; - - p.pno = partno; - p.start =0 ; - p.length =0 ; - p.devname[0 ] =0 ; - p.volname[0 ] =0 ; - a.op =BLKPG_DEL_PARTITION ; - a.flags =0 ; - a.datalen =sizeof (p); - a.data = &p; +uint64_t start =0 ; +uint64_t size =0 ; +INIT_BLKPG (a,BLKPG_DEL_PARTITION , partno, start, size);return ioctl (fd,BLKPG , &a); }
@@ -38,18 +43,10 @@ static inline int partx_add_partition(int fd, int partno, uint64_t start, uint64_t size) {struct blkpg_ioctl_arg a; -struct blkpg_partition p; - - p.pno = partno; - p.start = start <<9 ; - p.length = size <<9 ; - p.devname[0 ] =0 ; - p.volname[0 ] =0 ; - a.op =BLKPG_ADD_PARTITION ; - a.flags =0 ; - a.datalen =sizeof (p); - a.data = &p; + start = start <<9 ; + size = size <<9 ; +INIT_BLKPG (a,BLKPG_ADD_PARTITION , partno, start, size);return ioctl (fd,BLKPG , &a); }
@@ -57,18 +54,10 @@ static inline int partx_resize_partition(int fd, int partno, uint64_t start, uint64_t size) {struct blkpg_ioctl_arg a; -struct blkpg_partition p; - - p.pno = partno; - p.start = start <<9 ; - p.length = size <<9 ; - p.devname[0 ] =0 ; - p.volname[0 ] =0 ; - a.op =BLKPG_RESIZE_PARTITION ; - a.flags =0 ; - a.datalen =sizeof (p); - a.data = &p; + start = start <<9 ; + size = size <<9 ; +INIT_BLKPG (a,BLKPG_RESIZE_PARTITION , partno, start, size);return ioctl (fd,BLKPG , &a); }
Signature · 1 line
2.6.0
Karel Zak
maintainerQuoted 3 lines · Alexander Kuleshov
The
disk-utils/partx.hprovides three functions:
partx_{del,add,resize}_partition() that contain almost the same code
- initialization of theblkpg_partitionandblkpg_ioctl_argstructs.
Good point,
Quoted 2 lines
Let's provide
INIT_PKGmacro that will do all work to prevent code duplication.
but I don't like the implementation :-)
disk-utils/partx.h line 20
Quoted 2 lines
disk-utils/partx.h +2
-0+#define INIT_BLKPG (ioctl_arg, action, partno, start, size) do { \ + struct blkpg_partition p; \
It's bad manner to hide any struct declaration within a macro if you expect a valid reference to the struct outside the macro scope. My solution (already in git tree):
#define INIT_BLKPG_PARTITION(_partno, _start, _size) { \
.pno = (_partno), \
.start = (_start) << 9, \
.length = (_size) << 9, \
.devname[0] = 0, \
.volname[0] = 0 \
}#define INIT_BLKPG_ARG(_action, _part) { \
.op = (_action), \
.flags = 0, \
.datalen = sizeof(*(_part)), \
.data = (_part) \
}static inline int partx_add_partition(int fd, int partno,
uint64_t start, uint64_t size)
{
struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, start, size);
struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_ADD_PARTITION, &p);
return ioctl(fd, BLKPG, &a);
}IMHO it's more readable and it does not hide struct blkpg_partition.
Hmm.. .devname[0] = 0 and .volname[0] = 0 initialization is unnecessary, unspecified struct members should be automatically zeroized.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com