Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use swap() #2

Open
GustavoARSilva opened this issue Jan 5, 2022 · 0 comments
Open

Use swap() #2

GustavoARSilva opened this issue Jan 5, 2022 · 0 comments

Comments

@GustavoARSilva
Copy link
Owner

GustavoARSilva commented Jan 5, 2022

If you send a proper patch for this CC me: Gustavo A. R. Silva <[email protected]>

linux-next-20220104$ time make coccicheck -j 128 COCCI=scripts/coccinelle/misc/swap.cocci MODE=patch 2>&1 | tee swap.patch

some examples:

diff -u -p a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c
--- a/kernel/locking/test-ww_mutex.c
+++ b/kernel/locking/test-ww_mutex.c
@@ -400,11 +400,8 @@ static int *get_random_order(int count)

        for (n = count - 1; n > 1; n--) {
                r = get_random_int() % (n + 1);
-               if (r != n) {
-                       tmp = order[n];
-                       order[n] = order[r];
-                       order[r] = tmp;
-               }
+               if (r != n)
+                       swap(order[n], order[r]);
        }

        return order;
diff -u -p a/drivers/net/ethernet/sfc/efx_channels.c b/drivers/net/ethernet/sfc/efx_channels.c
--- a/drivers/net/ethernet/sfc/efx_channels.c
+++ b/drivers/net/ethernet/sfc/efx_channels.c
@@ -819,11 +819,8 @@ int efx_realloc_channels(struct efx_nic
        old_txq_entries = efx->txq_entries;
        efx->rxq_entries = rxq_entries;
        efx->txq_entries = txq_entries;
-       for (i = 0; i < efx->n_channels; i++) {
-               channel = efx->channel[i];
-               efx->channel[i] = other_channel[i];
-               other_channel[i] = channel;
-       }
+       for (i = 0; i < efx->n_channels; i++)
+               swap(efx->channel[i], other_channel[i]);

        /* Restart buffer table allocation */
        efx->next_buffer_table = next_buffer_table;
@@ -865,11 +862,8 @@ rollback:
        /* Swap back */
        efx->rxq_entries = old_rxq_entries;
        efx->txq_entries = old_txq_entries;
-       for (i = 0; i < efx->n_channels; i++) {
-               channel = efx->channel[i];
-               efx->channel[i] = other_channel[i];
-               other_channel[i] = channel;
-       }
+       for (i = 0; i < efx->n_channels; i++)
+               swap(efx->channel[i], other_channel[i]);
        goto out;
 }

diff -u -p a/fs/ntfs3/lib/lzx_decompress.c b/fs/ntfs3/lib/lzx_decompress.c
--- a/fs/ntfs3/lib/lzx_decompress.c
+++ b/fs/ntfs3/lib/lzx_decompress.c
@@ -511,9 +511,7 @@ static int lzx_decompress_block(const st
                         * quirk allows all 3 recent offsets to be handled by
                         * the same code.  (For R0, the swap is a no-op.)
                         */
-                       match_offset = recent_offsets[offset_slot];
-                       recent_offsets[offset_slot] = recent_offsets[0];
-                       recent_offsets[0] = match_offset;
+                       swap(recent_offsets[offset_slot], recent_offsets[0]);
                } else {
                        /* Explicit offset  */

diff -u -p a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
--- a/arch/x86/kernel/unwind_orc.c
+++ b/arch/x86/kernel/unwind_orc.c
@@ -196,7 +196,6 @@ static struct orc_entry *cur_orc_table =
 static void orc_sort_swap(void *_a, void *_b, int size)
 {
        struct orc_entry *orc_a, *orc_b;
-       struct orc_entry orc_tmp;
        int *a = _a, *b = _b, tmp;
        int delta = _b - _a;

@@ -208,9 +207,7 @@ static void orc_sort_swap(void *_a, void
        /* Swap the corresponding .orc_unwind entries: */
        orc_a = cur_orc_table + (a - cur_orc_ip_table);
        orc_b = cur_orc_table + (b - cur_orc_ip_table);
-       orc_tmp = *orc_a;
-       *orc_a = *orc_b;
-       *orc_b = orc_tmp;
+       swap(*orc_a, *orc_b);
 }

 static int orc_sort_cmp(const void *_a, const void *_b)
diff -u -p a/tools/testing/radix-tree/main.c b/tools/testing/radix-tree/main.c
--- a/tools/testing/radix-tree/main.c
+++ b/tools/testing/radix-tree/main.c
@@ -163,11 +163,8 @@ void copy_tag_check(void)
 //     printf("generating radix tree indices...\n");
        start = rand();
        end = rand();
-       if (start > end && (rand() % 10)) {
-               cur = start;
-               start = end;
-               end = cur;
-       }
+       if (start > end && (rand() % 10))
+               swap(start, end);
        /* Specifically create items around the start and the end of the range
         * with high probability to check for off by one errors */
        cur = rand();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant