All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tipc: Use bsearch library function
@ 2017-09-09  3:18 Thomas Meyer
  2017-09-11 21:30 ` David Miller
  0 siblings, 1 reply; 21+ messages in thread
From: Thomas Meyer @ 2017-09-09  3:18 UTC (permalink / raw)
  To: jon.maloy, ying.xue, davem, netdev, tipc-discussion, linux-kernel
  Cc: Thomas Meyer

Use common library function rather than explicitly coding
some variant of it yourself.

Signed-off-by: Thomas Meyer <thomas@m3y3r.de>
---
 net/tipc/name_table.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/net/tipc/name_table.c b/net/tipc/name_table.c
index bd0aac87b41a..345454106390 100644
--- a/net/tipc/name_table.c
+++ b/net/tipc/name_table.c
@@ -44,6 +44,7 @@
 #include "addr.h"
 #include "node.h"
 #include <net/genetlink.h>
+#include <linux/bsearch.h>
 
 #define TIPC_NAMETBL_SIZE 1024		/* must be a power of 2 */
 
@@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_hea
 	return nseq;
 }
 
+static int nameseq_find_subseq_cmp(const void *key, const void *elt)
+{
+	u32 instance = *(u32 *)key;
+	struct sub_seq *sseq = (struct sub_seq *)elt;
+
+	if (instance < sseq->lower)
+		return -1;
+	else if (instance > sseq->upper)
+		return 1;
+	return 0;
+}
+
 /**
  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
  *
@@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_hea
 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
 					   u32 instance)
 {
-	struct sub_seq *sseqs = nseq->sseqs;
-	int low = 0;
-	int high = nseq->first_free - 1;
-	int mid;
-
-	while (low <= high) {
-		mid = (low + high) / 2;
-		if (instance < sseqs[mid].lower)
-			high = mid - 1;
-		else if (instance > sseqs[mid].upper)
-			low = mid + 1;
-		else
-			return &sseqs[mid];
-	}
-	return NULL;
+	return bsearch(&instance, nseq->sseqs, nseq->first_free,
+		       sizeof(struct sub_seq), nameseq_find_subseq_cmp);
 }
 
 /**
-- 
2.11.0

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

end of thread, other threads:[~2017-09-19  1:08 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-09  3:18 [PATCH] tipc: Use bsearch library function Thomas Meyer
2017-09-11 21:30 ` David Miller
2017-09-12  9:24   ` David Laight
2017-09-12  9:24     ` David Laight
2017-09-16  7:50   ` [PATCH V2] " Thomas Meyer
2017-09-16  9:02     ` Ying Xue
2017-09-16  9:26       ` Joe Perches
2017-09-16  9:36         ` Ying Xue
2017-09-16  9:58           ` Joe Perches
2017-09-16 10:10             ` Ying Xue
2017-09-16 10:17               ` Joe Perches
2017-09-16 13:20                 ` Jon Maloy
2017-09-16 13:20                   ` Jon Maloy
2017-09-17 15:00                   ` Thomas Meyer
2017-09-17 15:00                     ` Thomas Meyer
2017-09-17 16:27                     ` Jon Maloy
2017-09-17 16:27                       ` Jon Maloy
2017-09-17 21:15                       ` Joe Perches
2017-09-17 21:15                         ` Joe Perches
2017-09-19  1:08                         ` Jon Maloy
2017-09-19  1:08                           ` Jon Maloy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.