]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdbsupport: use iterator range in parallel_for_each interface
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 19 Sep 2025 20:27:02 +0000 (16:27 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 30 Sep 2025 19:37:20 +0000 (19:37 +0000)
I think it would be convenient for parallel_for_each to pass an
iterator_range to the worker function, instead of separate begin and end
parameters.  This allows using a ranged for loop directly.

Change-Id: I8f9681da65b0eb00b738379dfd2f4dc6fb1ee612
Approved-By: Tom Tromey <tom@tromey.com>
gdb/minsyms.c
gdb/unittests/parallel-for-selftests.c
gdbsupport/parallel-for.h

index cc50febba6c90b12b8b1dc2b021bdc22060d198a..d3a8d670d1b5b8daa3266b69a371ba197ec2aba8 100644 (file)
@@ -1412,36 +1412,36 @@ public:
 #endif
   {}
 
-  void operator() (minimal_symbol *start, minimal_symbol *end) noexcept
+  void operator() (iterator_range<minimal_symbol *> msym_range) noexcept
   {
-    for (minimal_symbol *msym = start; msym < end; ++msym)
+    for (minimal_symbol &msym : msym_range)
       {
-       size_t idx = msym - m_msymbols;
-       m_hash_values[idx].name_length = strlen (msym->linkage_name ());
+       size_t idx = &msym - m_msymbols;
+       m_hash_values[idx].name_length = strlen (msym.linkage_name ());
 
-       if (!msym->name_set)
+       if (!msym.name_set)
          {
            /* This will be freed later, by compute_and_set_names.  */
            gdb::unique_xmalloc_ptr<char> demangled_name
-             = symbol_find_demangled_name (msym, msym->linkage_name ());
-           msym->set_demangled_name (demangled_name.release (),
+             = symbol_find_demangled_name (&msym, msym.linkage_name ());
+           msym.set_demangled_name (demangled_name.release (),
                                      &m_per_bfd->storage_obstack);
-           msym->name_set = 1;
+           msym.name_set = 1;
          }
 
        /* This mangled_name_hash computation has to be outside of
           the name_set check, or compute_and_set_names below will
           be called with an invalid hash value.  */
        m_hash_values[idx].mangled_name_hash
-         = fast_hash (msym->linkage_name (), m_hash_values[idx].name_length);
-       m_hash_values[idx].minsym_hash = msymbol_hash (msym->linkage_name ());
+         = fast_hash (msym.linkage_name (), m_hash_values[idx].name_length);
+       m_hash_values[idx].minsym_hash = msymbol_hash (msym.linkage_name ());
 
        /* We only use this hash code if the search name differs
           from the linkage name.  See the code in
           build_minimal_symbol_hash_tables.  */
-       if (msym->search_name () != msym->linkage_name ())
+       if (msym.search_name () != msym.linkage_name ())
          m_hash_values[idx].minsym_demangled_hash
-           = search_name_hash (msym->language (), msym->search_name ());
+           = search_name_hash (msym.language (), msym.search_name ());
       }
 
     {
@@ -1450,14 +1450,14 @@ public:
 #if CXX_STD_THREAD
       std::lock_guard<std::mutex> guard (m_demangled_mutex);
 #endif
-      for (minimal_symbol *msym = start; msym < end; ++msym)
+      for (minimal_symbol &msym : msym_range)
        {
-         size_t idx = msym - m_msymbols;
-         std::string_view name (msym->linkage_name (),
+         size_t idx = &msym - m_msymbols;
+         std::string_view name (msym.linkage_name (),
                                 m_hash_values[idx].name_length);
          hashval_t hashval = m_hash_values[idx].mangled_name_hash;
 
-         msym->compute_and_set_names (name, false, m_per_bfd, hashval);
+         msym.compute_and_set_names (name, false, m_per_bfd, hashval);
        }
     }
   }
index 9d86aa67b106c73c165cf14304574cc5ecbdef67..54556c9861c62a9fc3aaa629c5ec266c0d992926 100644 (file)
@@ -42,7 +42,7 @@ struct save_restore_n_threads
   int n_threads;
 };
 
-using foreach_callback_t = gdb::function_view<void (int *first, int *last)>;
+using foreach_callback_t = gdb::function_view<void (iterator_range<int *> range)>;
 using do_foreach_t = gdb::function_view<void (int *first, int *last,
                                             foreach_callback_t)>;
 
@@ -63,16 +63,16 @@ test_one (do_foreach_t do_foreach, int upper_bound)
   /* The (unfortunate) reason why we don't use std::vector<int>::iterator as
      the parallel-for-each iterator type is that std::atomic won't work with
      that type when building with -D_GLIBCXX_DEBUG.  */
-  do_foreach (input.data (), input.data () + input.size (),
-             [&] (int *start, int *end)
+  do_foreach (input.data (),  input.data () + input.size (),
+             [&] (iterator_range<int *> range)
                {
                  /* We shouldn't receive empty ranges.  */
-                 SELF_CHECK (start != end);
+                 SELF_CHECK (!range.empty ());
 
                  std::lock_guard lock (mtx);
 
-                 for (int *i = start; i < end; ++i)
-                   output.emplace_back (*i * 2);
+                 for (int i : range)
+                   output.emplace_back (i * 2);
                });
 
   /* Verify that each item was processed exactly once.  */
@@ -109,9 +109,9 @@ test_parallel_for_each ()
     {
     }
 
-    void operator() (int *first, int *last)
+    void operator() (iterator_range<int *> range)
     {
-      return m_callback (first, last);
+      return m_callback (range);
     }
 
   private:
index 8d085df42fb9a2938abf6c3c84751fd714566872..d8bb762d9b0beabe53f5bf1381c3926472a28d5e 100644 (file)
@@ -23,6 +23,7 @@
 #include <algorithm>
 #include <atomic>
 #include <tuple>
+#include "gdbsupport/iterator-range.h"
 #include "gdbsupport/thread-pool.h"
 
 namespace gdb
@@ -114,7 +115,7 @@ parallel_for_each (const RandomIt first, const RandomIt last,
                          static_cast<size_t> (this_batch_first - first),
                          static_cast<size_t> (this_batch_last - first));
 
-         worker (this_batch_first, this_batch_last);
+         worker ({ this_batch_first, this_batch_last });
        }
     };
 
@@ -138,7 +139,7 @@ sequential_for_each (RandomIt first, RandomIt last, WorkerArgs &&...worker_args)
   if (first == last)
     return;
 
-  Worker (std::forward<WorkerArgs> (worker_args)...) (first, last);
+  Worker (std::forward<WorkerArgs> (worker_args)...) ({ first, last });
 }
 
 }