changeset 19172:1a5a45c80687

lib: extract sort-helpers into separate sort.h file The macro definition was nothing to do with strings, and we can put trivial common-type comparators here. They didn't need to be static inline, as they can never be inlined anyway, being only used via function pointers, but that preserves the closest equivalent to the current code. Signed-off-by: Phil Carmody <phil@dovecot.fi>
author Phil Carmody <phil@dovecot.fi>
date Mon, 21 Sep 2015 19:51:05 +0300
parents 6377910c19e3
children 2f492fac75b7
files src/lib-imap-client/imapc-msgmap.c src/lib-storage/index/pop3c/pop3c-sync.c src/lib-storage/list/mailbox-list-index-sync.c src/lib/sort.h src/lib/strfuncs.h src/lib/test-timing.c src/plugins/fts-squat/squat-uidlist.c
diffstat 7 files changed, 27 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-imap-client/imapc-msgmap.c	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/lib-imap-client/imapc-msgmap.c	Mon Sep 21 19:51:05 2015 +0300
@@ -47,12 +47,6 @@
 	return *uidp;
 }
 
-static int uint32_cmp(const uint32_t *p1, const uint32_t *p2)
-{
-	return *p1 < *p2 ? -1 :
-		(*p1 > *p2 ? 1 : 0);
-}
-
 bool imapc_msgmap_uid_to_rseq(struct imapc_msgmap *msgmap,
 			      uint32_t uid, uint32_t *rseq_r)
 {
--- a/src/lib-storage/index/pop3c/pop3c-sync.c	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-sync.c	Mon Sep 21 19:51:05 2015 +0300
@@ -189,12 +189,6 @@
 		mailbox_recent_flags_set_seqs(&mbox->box, sync_view, seq1, seq2);
 }
 
-static int uint32_cmp(const uint32_t *u1, const uint32_t *u2)
-{
-	return *u1 < *u2 ? -1 :
-		(*u1 > *u2 ? 1 : 0);
-}
-
 int pop3c_sync(struct pop3c_mailbox *mbox)
 {
         struct mail_index_sync_ctx *index_sync_ctx;
--- a/src/lib-storage/list/mailbox-list-index-sync.c	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/lib-storage/list/mailbox-list-index-sync.c	Mon Sep 21 19:51:05 2015 +0300
@@ -143,12 +143,6 @@
 	}
 }
 
-static int uint32_cmp(const uint32_t *p1, const uint32_t *p2)
-{
-	return *p1 < *p2 ? -1 :
-		(*p1 > *p2 ? 1 : 0);
-}
-
 static void
 mailbox_list_index_sync_names(struct mailbox_list_index_sync_context *ctx)
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/sort.h	Mon Sep 21 19:51:05 2015 +0300
@@ -0,0 +1,24 @@
+#ifndef SORT_H
+#define SORT_H
+
+#define INTEGER_CMP(name, type)					     \
+	static inline int name(const type *i1, const type *i2)	     \
+	{							     \
+		if (*i1 < *i2)					     \
+			return -1;				     \
+		else if (*i1 > *i2)				     \
+			return 1;				     \
+		else						     \
+			return 0;				     \
+	}
+
+INTEGER_CMP(uint64_cmp, uint64_t)
+INTEGER_CMP(uint32_cmp, uint32_t)
+
+#define i_qsort(base, nmemb, size, cmp) \
+	qsort(base, nmemb, size +					\
+	      CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*base) *), \
+					      typeof(const typeof(*base) *))), \
+	      (int (*)(const void *, const void *))cmp)
+
+#endif
--- a/src/lib/strfuncs.h	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/lib/strfuncs.h	Mon Sep 21 19:51:05 2015 +0300
@@ -94,11 +94,9 @@
 const char **p_strarray_dup(pool_t pool, const char *const *arr)
 	ATTR_MALLOC ATTR_RETURNS_NONNULL;
 
-#define i_qsort(base, nmemb, size, cmp) \
-	qsort(base, nmemb, size + \
-		CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*base) *), \
-						typeof(const typeof(*base) *))), \
-		(int (*)(const void *, const void *))cmp)
+/* FIXME: v2.3 - sort and search APIs belong into their own header, not here */
+#include "sort.h"
+
 #define i_bsearch(key, base, nmemb, size, cmp) \
 	bsearch(key, base, nmemb, size + \
 		CALLBACK_TYPECHECK(cmp, int (*)(typeof(const typeof(*key) *), \
--- a/src/lib/test-timing.c	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/lib/test-timing.c	Mon Sep 21 19:51:05 2015 +0300
@@ -5,16 +5,6 @@
 
 #include <stdlib.h>
 
-static int uint64_cmp(const uint64_t *i1, const uint64_t *i2)
-{
-	if (*i1 < *i2)
-		return -1;
-	else if (*i1 > *i2)
-		return 1;
-	else
-		return 0;
-}
-
 static void
 test_timing_verify(const struct timing *t, const int64_t *input,
 		   unsigned int input_size)
--- a/src/plugins/fts-squat/squat-uidlist.c	Mon Sep 21 17:03:19 2015 +0300
+++ b/src/plugins/fts-squat/squat-uidlist.c	Mon Sep 21 19:51:05 2015 +0300
@@ -1382,11 +1382,6 @@
 	return 0;
 }
 
-static int uint32_cmp(const uint32_t *key, const uint32_t *data)
-{
-	return (int)*key - (int)*data;
-}
-
 static int
 squat_uidlist_get_offset(struct squat_uidlist *uidlist, uint32_t uid_list_idx,
 			 uint32_t *offset_r, uint32_t *num_r)