[sheepdog] [PATCH] introduce intcmp to simplify codes

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Aug 2 11:11:41 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/sheep.h          |   12 ++----------
 include/util.h           |    8 ++++++++
 lib/event.c              |    7 +------
 sheep/cluster/corosync.c |    4 ++--
 sheep/md.c               |    6 +-----
 sheep/recovery.c         |    6 +-----
 6 files changed, 15 insertions(+), 28 deletions(-)

diff --git a/include/sheep.h b/include/sheep.h
index 646522c..e3f7755 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -252,11 +252,7 @@ static inline int node_id_cmp(const struct node_id *node1,
 	if (cmp != 0)
 		return cmp;
 
-	if (node1->port < node2->port)
-		return -1;
-	if (node1->port > node2->port)
-		return 1;
-	return 0;
+	return intcmp(node1->port, node2->port);
 }
 
 static inline int node_cmp(const struct sd_node *node1,
@@ -273,11 +269,7 @@ static inline bool node_eq(const struct sd_node *a, const struct sd_node *b)
 static inline int vnode_cmp(const struct sd_vnode *node1,
 			    const struct sd_vnode *node2)
 {
-	if (node1->id < node2->id)
-		return -1;
-	if (node1->id > node2->id)
-		return 1;
-	return 0;
+	return intcmp(node1->id, node2->id);
 }
 
 static inline int nodes_to_vnodes(struct sd_node *nodes, int nr,
diff --git a/include/util.h b/include/util.h
index aa5341e..82082c2 100644
--- a/include/util.h
+++ b/include/util.h
@@ -71,6 +71,14 @@ static inline void *zalloc(size_t size)
 	return calloc(1, size);
 }
 
+#define intcmp(x, y) \
+({					\
+	typeof(x) _x = (x);		\
+	typeof(y) _y = (y);		\
+	(void) (&_x == &_y);		\
+	_x < _y ? -1 : _x > _y ? 1 : 0;	\
+})
+
 typedef void (*try_to_free_t)(size_t);
 try_to_free_t set_try_to_free_routine(try_to_free_t);
 
diff --git a/lib/event.c b/lib/event.c
index 0c69ac8..db00838 100644
--- a/lib/event.c
+++ b/lib/event.c
@@ -178,12 +178,7 @@ static int epoll_event_cmp(const struct epoll_event *_a, struct epoll_event *_b)
 	b = (struct event_info *)_b->data.ptr;
 
 	/* we need sort event_info array in reverse order */
-	if (a->prio < b->prio)
-		return 1;
-	else if (b->prio < a->prio)
-		return -1;
-
-	return 0;
+	return intcmp(b->prio, a->prio);
 }
 
 static void do_event_loop(int timeout, bool sort_with_prio)
diff --git a/sheep/cluster/corosync.c b/sheep/cluster/corosync.c
index ef403bf..d08b5e8 100644
--- a/sheep/cluster/corosync.c
+++ b/sheep/cluster/corosync.c
@@ -90,9 +90,9 @@ struct corosync_message {
 
 static int cpg_node_cmp(struct cpg_node *a, struct cpg_node *b)
 {
-	int cmp = memcmp(&a->nodeid, &b->nodeid, sizeof(a->nodeid));
+	int cmp = intcmp(a->nodeid, b->nodeid);
 	if (cmp == 0)
-		cmp = memcmp(&a->pid, &b->pid, sizeof(a->pid));
+		cmp = intcmp(a->pid, b->pid);
 	return cmp;
 }
 
diff --git a/sheep/md.c b/sheep/md.c
index e307cf5..dc243fd 100644
--- a/sheep/md.c
+++ b/sheep/md.c
@@ -69,11 +69,7 @@ static struct vdisk *oid_to_vdisk_from(struct vdisk *vds, int nr, uint64_t oid)
 
 static int vdisk_cmp(const struct vdisk *d1, const struct vdisk *d2)
 {
-	if (d1->id < d2->id)
-		return -1;
-	if (d1->id > d2->id)
-		return 1;
-	return 0;
+	return intcmp(d1->id, d2->id);
 }
 
 static inline int disks_to_vdisks(struct disk *ds, int nmds, struct vdisk *vds)
diff --git a/sheep/recovery.c b/sheep/recovery.c
index ea3101a..3c65bd1 100644
--- a/sheep/recovery.c
+++ b/sheep/recovery.c
@@ -91,11 +91,7 @@ static int obj_cmp(const uint64_t *oid1, const uint64_t *oid2)
 	const uint64_t hval1 = fnv_64a_buf(oid1, sizeof(*oid1), FNV1A_64_INIT);
 	const uint64_t hval2 = fnv_64a_buf(oid2, sizeof(*oid2), FNV1A_64_INIT);
 
-	if (hval1 < hval2)
-		return -1;
-	if (hval1 > hval2)
-		return 1;
-	return 0;
+	return intcmp(hval1, hval2);
 }
 
 static inline bool node_is_gateway_only(void)
-- 
1.7.9.5




More information about the sheepdog mailing list