]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: Use abs_diff instead of XFS_ABSDIFF
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 21 Mar 2025 16:31:15 +0000 (09:31 -0700)
committerAndrey Albershteyn <aalbersh@kernel.org>
Mon, 31 Mar 2025 09:45:45 +0000 (11:45 +0200)
Source kernel commit: ca3ac4bf4dc307cea5781dccccf41c1d14c2f82f

We have a central definition for this function since 2023, used by
a number of different parts of the kernel.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
include/platform_defs.h
libxfs/xfs_alloc.c

index 051ee25a5b4feaa674e3b4121e71d422bc3a805c..9af7b4318f8917f363fd3409e5257daca2e46a2a 100644 (file)
@@ -261,4 +261,23 @@ static inline bool __must_check __must_check_overflow(bool overflow)
        __builtin_add_overflow(__a, __b, __d);  \
 }))
 
+/**
+ * abs_diff - return absolute value of the difference between the arguments
+ * @a: the first argument
+ * @b: the second argument
+ *
+ * @a and @b have to be of the same type. With this restriction we compare
+ * signed to signed and unsigned to unsigned. The result is the subtraction
+ * the smaller of the two from the bigger, hence result is always a positive
+ * value.
+ *
+ * Return: an absolute value of the difference between the @a and @b.
+ */
+#define abs_diff(a, b) ({                      \
+       typeof(a) __a = (a);                    \
+       typeof(b) __b = (b);                    \
+       (void)(&__a == &__b);                   \
+       __a > __b ? (__a - __b) : (__b - __a);  \
+})
+
 #endif /* __XFS_PLATFORM_DEFS_H__ */
index 9aebe7227a6148063a12c9e40b14e40d2b04d326..6675be78a7dae883cb43b714ec0432c0fef74a61 100644 (file)
@@ -29,8 +29,6 @@ struct kmem_cache     *xfs_extfree_item_cache;
 
 struct workqueue_struct *xfs_alloc_wq;
 
-#define XFS_ABSDIFF(a,b)       (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
-
 #define        XFSA_FIXUP_BNO_OK       1
 #define        XFSA_FIXUP_CNT_OK       2
 
@@ -406,8 +404,8 @@ xfs_alloc_compute_diff(
                if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
                        if (newlen1 < newlen2 ||
                            (newlen1 == newlen2 &&
-                            XFS_ABSDIFF(newbno1, wantbno) >
-                            XFS_ABSDIFF(newbno2, wantbno)))
+                            abs_diff(newbno1, wantbno) >
+                            abs_diff(newbno2, wantbno)))
                                newbno1 = newbno2;
                } else if (newbno2 != NULLAGBLOCK)
                        newbno1 = newbno2;
@@ -423,7 +421,7 @@ xfs_alloc_compute_diff(
        } else
                newbno1 = freeend - wantlen;
        *newbnop = newbno1;
-       return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
+       return newbno1 == NULLAGBLOCK ? 0 : abs_diff(newbno1, wantbno);
 }
 
 /*