Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 04/12] Remove ALL_OBJFILES_SAFE
Date: Sun, 25 Nov 2018 16:54:00 -0000	[thread overview]
Message-ID: <20181125165439.13773-5-tom@tromey.com> (raw)
In-Reply-To: <20181125165439.13773-1-tom@tromey.com>

This removes the ALL_OBJFILES_SAFE macro, replacing the uses with
ranged for loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* common/next-iterator.h (next_adapter): Add Iterator template
	parameter.
	* objfiles.h (ALL_OBJFILES_SAFE): Remove.
	(class all_objfiles_safe): New.
	* jit.c (jit_inferior_exit_hook): Use all_objfiles_safe.
	* objfiles.c (put_objfile_before): Update comment.
	(add_separate_debug_objfile): Likewise.
	(free_all_objfiles): Use all_objfiles_safe.
	(objfile_purge_solibs): Likewise.
---
 gdb/ChangeLog              | 12 ++++++++++++
 gdb/common/next-iterator.h |  4 ++--
 gdb/jit.c                  |  5 +----
 gdb/objfiles.c             | 26 ++++++++++----------------
 gdb/objfiles.h             | 32 ++++++++++++++++++++++++--------
 5 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/gdb/common/next-iterator.h b/gdb/common/next-iterator.h
index 79241a82a4..27c6092f2c 100644
--- a/gdb/common/next-iterator.h
+++ b/gdb/common/next-iterator.h
@@ -72,7 +72,7 @@ private:
 
 /* A range adapter that allows iterating over a linked list.  */
 
-template<typename T>
+template<typename T, typename Iterator = next_iterator<T>>
 class next_adapter
 {
 public:
@@ -82,7 +82,7 @@ public:
   {
   }
 
-  using iterator = next_iterator<T>;
+  using iterator = Iterator;
 
   iterator begin () const
   {
diff --git a/gdb/jit.c b/gdb/jit.c
index 2f5f6f8ade..a217c3ca75 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -1393,10 +1393,7 @@ jit_breakpoint_re_set (void)
 static void
 jit_inferior_exit_hook (struct inferior *inf)
 {
-  struct objfile *objf;
-  struct objfile *temp;
-
-  ALL_OBJFILES_SAFE (objf, temp)
+  for (struct objfile *objf : all_objfiles_safe (current_program_space))
     {
       struct jit_objfile_data *objf_data
 	= (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 90d2502dde..19047694af 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -514,7 +514,7 @@ objfile_separate_debug_iterate (const struct objfile *parent,
 
 /* Put one object file before a specified on in the global list.
    This can be used to make sure an object file is destroyed before
-   another when using ALL_OBJFILES_SAFE to free all objfiles.  */
+   another when using all_objfiles_safe to free all objfiles.  */
 void
 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
 {
@@ -587,7 +587,7 @@ add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
   parent->separate_debug_objfile = objfile;
 
   /* Put the separate debug object before the normal one, this is so that
-     usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
+     usage of all_objfiles_safe will stay safe.  */
   put_objfile_before (objfile, parent);
 }
 
@@ -730,17 +730,14 @@ objfile::~objfile ()
 void
 free_all_objfiles (void)
 {
-  struct objfile *objfile, *temp;
   struct so_list *so;
 
   /* Any objfile referencewould become stale.  */
   for (so = master_so_list (); so; so = so->next)
     gdb_assert (so->objfile == NULL);
 
-  ALL_OBJFILES_SAFE (objfile, temp)
-  {
+  for (struct objfile *objfile : all_objfiles_safe (current_program_space))
     delete objfile;
-  }
   clear_symtab_users (0);
 }
 \f
@@ -1047,17 +1044,14 @@ have_full_symbols (void)
 void
 objfile_purge_solibs (void)
 {
-  struct objfile *objf;
-  struct objfile *temp;
-
-  ALL_OBJFILES_SAFE (objf, temp)
-  {
-    /* We assume that the solib package has been purged already, or will
-       be soon.  */
+  for (struct objfile *objf : all_objfiles_safe (current_program_space))
+    {
+      /* We assume that the solib package has been purged already, or will
+	 be soon.  */
 
-    if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
-      delete objf;
-  }
+      if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
+	delete objf;
+    }
 }
 
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index dee64c816a..1d310e5584 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -29,6 +29,7 @@
 #include "gdb_bfd.h"
 #include <vector>
 #include "common/next-iterator.h"
+#include "common/safe-iterator.h"
 
 struct bcache;
 struct htab;
@@ -581,21 +582,36 @@ public:
   }
 };
 
+/* An iterarable object that can be used to iterate over all
+   objfiles.  The basic use is in a foreach, like:
 
-/* Traverse all object files in the current program space.
-   ALL_OBJFILES_SAFE works even if you delete the objfile during the
-   traversal.  */
+   for (struct objfile *objf : all_objfiles_safe (pspace)) { ... }
+
+   This variant uses a basic_safe_iterator so that objfiles can be
+   deleted during iteration.  */
+
+class all_objfiles_safe
+  : public next_adapter<struct objfile,
+			basic_safe_iterator<next_iterator<objfile>>>
+{
+public:
+
+  explicit all_objfiles_safe (struct program_space *pspace)
+    : next_adapter<struct objfile,
+		   basic_safe_iterator<next_iterator<objfile>>>
+        (pspace->objfiles)
+  {
+  }
+};
+
+
+/* Traverse all object files in the current program space.  */
 
 #define ALL_OBJFILES(obj)			    \
   for ((obj) = current_program_space->objfiles; \
        (obj) != NULL;				    \
        (obj) = (obj)->next)
 
-#define ALL_OBJFILES_SAFE(obj,nxt)			\
-  for ((obj) = current_program_space->objfiles;	\
-       (obj) != NULL? ((nxt)=(obj)->next,1) :0;	\
-       (obj) = (nxt))
-
 /* Traverse all symtabs in one objfile.  */
 
 #define ALL_OBJFILE_FILETABS(objfile, cu, s) \
-- 
2.17.2


  parent reply	other threads:[~2018-11-25 16:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
2018-11-25 16:54 ` [PATCH 07/12] Remove ALL_COMPUNITS Tom Tromey
2018-11-25 16:54 ` Tom Tromey [this message]
2018-11-25 16:54 ` [PATCH 08/12] Remove ALL_COMPUNIT_FILETABS Tom Tromey
2018-11-25 16:54 ` [PATCH 11/12] Remove ALL_OBJSECTIONS Tom Tromey
2018-11-25 16:54 ` [PATCH 05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS Tom Tromey
2018-11-25 16:54 ` [PATCH 03/12] Remove most uses of ALL_OBJFILES Tom Tromey
2018-11-25 16:54 ` [PATCH 12/12] Remove ALL_OBJFILE_PSYMTABS Tom Tromey
2018-11-25 16:54 ` [PATCH 01/12] Introduce all_objfiles and next_iterator Tom Tromey
2018-11-25 16:54 ` [PATCH 09/12] Remove ALL_OBJFILE_FILETABS Tom Tromey
2018-11-25 16:54 ` [PATCH 10/12] Remove ALL_OBJFILES and ALL_FILETABS Tom Tromey
2018-11-25 16:55 ` [PATCH 02/12] Remove ALL_PSPACE_OBJFILES Tom Tromey
2018-11-25 16:55 ` [PATCH 06/12] Remove ALL_OBJFILE_COMPUNITS Tom Tromey
2018-12-23  7:00 ` [PATCH 00/12] Remove some ALL_* iteration macros Joel Brobecker
2018-12-24 20:54   ` Tom Tromey
2018-12-26 17:30     ` Simon Marchi
2018-12-26 22:28       ` Tom Tromey
2018-12-26 22:32         ` Tom Tromey
2018-12-26 22:35           ` Simon Marchi
2018-12-26 22:52             ` Tom Tromey
2018-12-26 23:45               ` Tom Tromey
2018-12-27  0:46                 ` Simon Marchi
2018-12-27  6:22                   ` Tom Tromey
2018-12-27  1:52 ` Simon Marchi
2019-01-03 21:46   ` Tom Tromey
2019-01-03 22:45     ` Simon Marchi
2019-01-06 20:10       ` Tom Tromey
2019-01-09 19:49         ` Simon Marchi
2019-01-10  1:29           ` Tom Tromey
2019-01-10 16:45         ` Pedro Alves
2019-01-10 18:10           ` Tom Tromey
2019-01-10 19:58             ` Pedro Alves
2019-01-10 16:44 ` Pedro Alves
2019-01-10 18:06   ` Tom Tromey
2019-01-10 18:09     ` Pedro Alves
2019-01-10 22:53       ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181125165439.13773-5-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox