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. Signed-off-by: Alexander Kuleshov <kuleshovmail@xxxxxxxxx> --- disk-utils/partx.h | 55 ++++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/disk-utils/partx.h b/disk-utils/partx.h index 254cd85..28165b3 100644 --- a/disk-utils/partx.h +++ b/disk-utils/partx.h @@ -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); } -- 2.6.0 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html