]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sort.h: hoist cmp_int() into generic header file
authorFedor Pchelkin <pchelkin@ispras.ru>
Sun, 27 Apr 2025 20:14:49 +0000 (23:14 +0300)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 12 May 2025 00:54:12 +0000 (17:54 -0700)
Deduplicate the same functionality implemented in several places by
moving the cmp_int() helper macro into linux/sort.h.

The macro performs a three-way comparison of the arguments mostly useful
in different sorting strategies and algorithms.

Link: https://lkml.kernel.org/r/20250427201451.900730-1-pchelkin@ispras.ru
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
Suggested-by: Darrick J. Wong <djwong@kernel.org>
Acked-by: Kent Overstreet <kent.overstreet@linux.dev>
Acked-by: Coly Li <colyli@kernel.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Coly Li <colyli@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
drivers/md/bcache/btree.c
fs/bcachefs/util.h
fs/pipe.c
fs/xfs/xfs_zone_gc.c
include/linux/sort.h

index ed40d86006564d22c2e14cd2e51dcc320fb8b6c2..2cc2eb24dc8af60596bef3143db6deaaa2daeb22 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/sched/clock.h>
 #include <linux/rculist.h>
 #include <linux/delay.h>
+#include <linux/sort.h>
 #include <trace/events/bcache.h>
 
 /*
@@ -559,8 +560,6 @@ static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
        }
 }
 
-#define cmp_int(l, r)          ((l > r) - (l < r))
-
 #ifdef CONFIG_PROVE_LOCKING
 static int btree_lock_cmp_fn(const struct lockdep_map *_a,
                             const struct lockdep_map *_b)
index 3e52c7f8ddd225d74d3cd9a4871833327dfd5ecd..7ec1fc8b46f9015f943a25b825b9701964650994 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/preempt.h>
 #include <linux/ratelimit.h>
 #include <linux/slab.h>
+#include <linux/sort.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
 
@@ -669,8 +670,6 @@ static inline void percpu_memset(void __percpu *p, int c, size_t bytes)
 
 u64 *bch2_acc_percpu_u64s(u64 __percpu *, unsigned);
 
-#define cmp_int(l, r)          ((l > r) - (l < r))
-
 static inline int u8_cmp(u8 l, u8 r)
 {
        return cmp_int(l, r);
index da45edd68c4166908b25e70e54b2430bbfcb7671..45077c37bad154ef146b047834d35d489fcc4d8d 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -26,6 +26,7 @@
 #include <linux/memcontrol.h>
 #include <linux/watch_queue.h>
 #include <linux/sysctl.h>
+#include <linux/sort.h>
 
 #include <linux/uaccess.h>
 #include <asm/ioctls.h>
@@ -76,8 +77,6 @@ static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
  * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
  */
 
-#define cmp_int(l, r)          ((l > r) - (l < r))
-
 #ifdef CONFIG_PROVE_LOCKING
 static int pipe_lock_cmp_fn(const struct lockdep_map *a,
                            const struct lockdep_map *b)
index 81c94dd1d5966bc97ee320f42905fa6ad99c483d..2f9caa3eb8284524bdcca8f5f0f49a4fcab78fcb 100644 (file)
@@ -290,8 +290,6 @@ xfs_zone_gc_query_cb(
        return 0;
 }
 
-#define cmp_int(l, r)          ((l > r) - (l < r))
-
 static int
 xfs_zone_gc_rmap_rec_cmp(
        const void                      *a,
index 8e5603b10941003ca55e63b9153ff15f477ecf7c..c01ef804a0eb2156f85a7f3f3481277025bdec42 100644 (file)
@@ -4,6 +4,16 @@
 
 #include <linux/types.h>
 
+/**
+ * cmp_int - perform a three-way comparison of the arguments
+ * @l: the left argument
+ * @r: the right argument
+ *
+ * Return: 1 if the left argument is greater than the right one; 0 if the
+ * arguments are equal; -1 if the left argument is less than the right one.
+ */
+#define cmp_int(l, r) (((l) > (r)) - ((l) < (r)))
+
 void sort_r(void *base, size_t num, size_t size,
            cmp_r_func_t cmp_func,
            swap_r_func_t swap_func,