Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 13/22] Replace hand-made linked list of ui_out_hdr by vector and iterator
Date: Thu, 24 Nov 2016 15:28:00 -0000	[thread overview]
Message-ID: <20161124152710.25007-13-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20161124152428.24725-1-simon.marchi@polymtl.ca>

Instead of keeping pointers to first, last and current ui_out_hdr in
ui_out_table, we can use an std::vector and an iterator.  Direct random
access of to vector helps make get_next_header a bit nicer by avoiding
iterating on all the headers.  append_header_to_list is also a bit
simpler.

Also, using unique_ptr inside the vector allows expressing the ownership
of the ui_out_hdr objects by the ui_out_table object, and it simplifies
the destruction.

gdb/ChangeLog:

	* ui-out.c (struct ui_out_hdr) <next>: Remove.
	(struct ui_out_table) <header_first, header_last, header_next>: Remove.
	<headers, headers_iterator>: New fields.
	(ui_out_table_body): Update for the new data structure.
	(ui_out_begin): Likewise.
	(clear_header_list): Likewise.
	(append_header_to_list): Likewise.
	(get_next_header): Likewise.
	(ui_out_query_field): Likewise.
	(ui_out_new): Likewise.
---
 gdb/ui-out.c | 100 ++++++++++++++++++++++++++---------------------------------
 1 file changed, 44 insertions(+), 56 deletions(-)

diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index a40ac0a..8e44698 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -38,7 +38,6 @@ struct ui_out_hdr
     enum ui_align alignment;
     char *col_name;
     char *colhdr;
-    struct ui_out_hdr *next;
   };
 
 struct ui_out_level
@@ -75,14 +74,11 @@ struct ui_out_table
      call).  */
   std::string id;
 
-  /* Points to the first table header (if any).  */
-  struct ui_out_hdr *header_first;
+  /* Pointers to the column headers.  */
+  std::vector<std::unique_ptr<ui_out_hdr>> headers;
 
-  /* Points to the last table header (if any).  */
-  struct ui_out_hdr *header_last;
-
-  /* Points to header of NEXT column to format.  */
-  struct ui_out_hdr *header_next;
+  /* Iterator over the headers vector, used when printing successive fields.  */
+  std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator headers_iterator;
 
 };
 
@@ -224,13 +220,12 @@ after a table_begin and before a table_end."));
     internal_error (__FILE__, __LINE__,
 		    _("extra table_body call not allowed; there must be \
 only one table_body after a table_begin and before a table_end."));
-  if (uiout->table.header_next->colno != uiout->table.columns)
+  if (uiout->table.headers.size () != uiout->table.columns)
     internal_error (__FILE__, __LINE__,
 		    _("number of headers differ from number of table \
 columns."));
 
   uiout->table.body_flag = 1;
-  uiout->table.header_next = uiout->table.header_first;
 
   uo_table_body (uiout);
 }
@@ -314,7 +309,7 @@ specified after table_body."));
      got a new table row.  Put the header pointer back to the start.  */
   if (uiout->table.body_flag
       && uiout->table.entry_level == new_level)
-    uiout->table.header_next = uiout->table.header_first;
+    uiout->table.headers_iterator = uiout->table.headers.begin ();
 
   uo_begin (uiout, type, new_level, id);
 }
@@ -699,18 +694,14 @@ uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
 static void
 clear_header_list (struct ui_out *uiout)
 {
-  while (uiout->table.header_first != NULL)
+  for (auto &it : uiout->table.headers)
     {
-      uiout->table.header_next = uiout->table.header_first;
-      uiout->table.header_first = uiout->table.header_first->next;
-      xfree (uiout->table.header_next->colhdr);
-      xfree (uiout->table.header_next->col_name);
-      delete uiout->table.header_next;
+      xfree (it->colhdr);
+      xfree (it->col_name);
     }
 
-  gdb_assert (uiout->table.header_first == NULL);
-  uiout->table.header_last = NULL;
-  uiout->table.header_next = NULL;
+  uiout->table.headers.clear ();
+  uiout->table.headers_iterator = uiout->table.headers.end ();
 }
 
 static void
@@ -720,9 +711,8 @@ append_header_to_list (struct ui_out *uiout,
 		       const char *col_name,
 		       const char *colhdr)
 {
-  struct ui_out_hdr *temphdr;
+  std::unique_ptr<ui_out_hdr> temphdr (new ui_out_hdr ());
 
-  temphdr = new ui_out_hdr ();
   temphdr->width = width;
   temphdr->alignment = alignment;
   /* We have to copy the column title as the original may be an
@@ -739,20 +729,9 @@ append_header_to_list (struct ui_out *uiout,
   else
     temphdr->col_name = NULL;
 
-  temphdr->next = NULL;
-  if (uiout->table.header_first == NULL)
-    {
-      temphdr->colno = 1;
-      uiout->table.header_first = temphdr;
-      uiout->table.header_last = temphdr;
-    }
-  else
-    {
-      temphdr->colno = uiout->table.header_last->colno + 1;
-      uiout->table.header_last->next = temphdr;
-      uiout->table.header_last = temphdr;
-    }
-  uiout->table.header_next = uiout->table.header_last;
+  temphdr->colno = uiout->table.headers.size () + 1;
+
+  uiout->table.headers.push_back (std::move (temphdr));
 }
 
 /* Extract the format information for the NEXT header and advance
@@ -766,14 +745,19 @@ get_next_header (struct ui_out *uiout,
 		 char **colhdr)
 {
   /* There may be no headers at all or we may have used all columns.  */
-  if (uiout->table.header_next == NULL)
+  if (uiout->table.headers_iterator == uiout->table.headers.end ())
     return 0;
-  *colno = uiout->table.header_next->colno;
-  *width = uiout->table.header_next->width;
-  *alignment = uiout->table.header_next->alignment;
-  *colhdr = uiout->table.header_next->colhdr;
+
+  ui_out_hdr *hdr = uiout->table.headers_iterator->get ();
+
+  *colno = hdr->colno;
+  *width = hdr->width;
+  *alignment = hdr->alignment;
+  *colhdr = hdr->colhdr;
+
   /* Advance the header pointer to the next entry.  */
-  uiout->table.header_next = uiout->table.header_next->next;
+  uiout->table.headers_iterator++;
+
   return 1;
 }
 
@@ -834,21 +818,26 @@ int
 ui_out_query_field (struct ui_out *uiout, int colno,
 		    int *width, int *alignment, char **col_name)
 {
-  struct ui_out_hdr *hdr;
-
   if (!uiout->table.flag)
     return 0;
 
-  for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
-    if (hdr->colno == colno)
-      {
-	*width = hdr->width;
-	*alignment = hdr->alignment;
-	*col_name = hdr->col_name;
-	return 1;
-      }
+  /* Column numbers are 1-based, so convert to 0-based index.  */
+  int index = colno - 1;
+
+  if (index >= 0 && index < uiout->table.headers.size ())
+    {
+      ui_out_hdr *hdr = uiout->table.headers[index].get ();
+
+      gdb_assert (colno == hdr->colno);
 
-  return 0;
+      *width = hdr->width;
+      *alignment = hdr->alignment;
+      *col_name = hdr->col_name;
+
+      return 1;
+    }
+  else
+    return 0;
 }
 
 /* Initialize private members at startup.  */
@@ -872,8 +861,7 @@ ui_out_new (const struct ui_out_impl *impl, void *data,
   current->field_count = 0;
   uiout->levels.push_back (std::move (current));
 
-  uiout->table.header_first = NULL;
-  uiout->table.header_last = NULL;
-  uiout->table.header_next = NULL;
+  uiout->table.headers_iterator = uiout->table.headers.end ();
+
   return uiout;
 }
-- 
2.10.0


  parent reply	other threads:[~2016-11-24 15:28 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-24 15:24 [PATCH 00/22] Convert ui-out subsystem to C++ Simon Marchi
2016-11-24 15:24 ` [PATCH 06/22] Remove verbosity from ui_out_message and friends Simon Marchi
2016-11-24 15:24 ` [PATCH 09/22] Use std::vector for ui_out::levels Simon Marchi
2016-11-24 15:24 ` [PATCH 07/22] Remove stale comments Simon Marchi
2016-11-24 18:38   ` Pedro Alves
2016-11-26 15:29     ` Simon Marchi
2016-11-24 15:24 ` [PATCH 02/22] Rename ui_out_data to mi_ui_out_data Simon Marchi
2016-11-24 15:24 ` [PATCH 08/22] Use new/delete instead of malloc/free-based functions Simon Marchi
2016-11-24 15:24 ` [PATCH 01/22] Remove unused functions and declarations Simon Marchi
2016-11-24 15:24 ` [PATCH 03/22] Remove ui_out_destroy Simon Marchi
2016-11-24 15:27 ` [PATCH 10/22] Use std::vector for mi_ui_out_data::streams Simon Marchi
2016-11-24 18:38   ` Pedro Alves
2016-11-26 15:48     ` Simon Marchi
2016-11-24 15:28 ` [PATCH 15/22] Class-ify ui_out_hdr Simon Marchi
2016-11-24 15:28 ` [PATCH 12/22] Use std::string in ui_out_table Simon Marchi
2016-11-24 15:28 ` Simon Marchi [this message]
2016-11-24 18:41   ` [PATCH 13/22] Replace hand-made linked list of ui_out_hdr by vector and iterator Pedro Alves
2016-11-26 16:13     ` Simon Marchi
2016-12-01 20:22       ` Simon Marchi
2016-12-01 20:23         ` Pedro Alves
2016-11-24 15:28 ` [PATCH 17/22] Simplify ui-out level code Simon Marchi
2016-11-24 18:42   ` Pedro Alves
2016-11-26 16:39     ` Simon Marchi
2016-11-30 12:08       ` Pedro Alves
2016-11-24 15:28 ` [PATCH 11/22] Use std::vector for cli_ui_out_data::streams Simon Marchi
2016-11-24 18:41   ` Pedro Alves
2016-11-26 15:51     ` Simon Marchi
2016-11-24 15:28 ` [PATCH 14/22] Use std::string for ui_out_hdr's text fields Simon Marchi
2016-11-24 15:28 ` [PATCH 18/22] ui_out_table: Replace boolean flag with enum Simon Marchi
2016-11-24 18:42   ` Pedro Alves
2016-11-26 16:47     ` Simon Marchi
2016-11-30 12:10       ` Pedro Alves
2016-11-24 15:28 ` [PATCH 16/22] Class-ify ui_out_level Simon Marchi
2016-11-24 18:41   ` Pedro Alves
2016-11-26 16:22     ` Simon Marchi
2016-11-30 12:07       ` Pedro Alves
2016-11-30 12:41         ` Antoine Tremblay
2016-11-30 13:27           ` Pedro Alves
2016-11-30 13:47             ` Antoine Tremblay
2016-11-30 14:17               ` Pedro Alves
2016-11-30 14:21                 ` Antoine Tremblay
2016-11-30 20:40         ` Simon Marchi
2016-11-24 15:32 ` [PATCH 22/22] Introduce enum_flag type for ui_out flags Simon Marchi
2016-11-30 13:34   ` Pedro Alves
2016-11-30 21:32     ` Simon Marchi
2016-12-02 22:22     ` [pushed] " Simon Marchi
2016-11-24 15:36 ` [PATCH 04/22] Fix return value of uo_redirect Simon Marchi
2016-11-24 15:36 ` [PATCH 05/22] Constify wrap_here/wrap_hint code path Simon Marchi
2016-11-24 16:08 ` [PATCH 00/22] Convert ui-out subsystem to C++ Simon Marchi
2016-11-24 18:46   ` Pedro Alves
2016-11-24 19:15     ` Simon Marchi
2016-11-24 20:33       ` Simon Marchi
2016-11-24 18:47   ` Pedro Alves
2016-11-27  3:14     ` Simon Marchi
2016-12-01  2:51     ` Simon Marchi
2016-12-01 21:24       ` Simon Marchi
2016-11-24 19:11 ` [PATCH 20/22] Class-ify ui_out_table Simon Marchi
2016-11-30 12:29   ` Pedro Alves
2016-11-24 19:19 ` [PATCH 21/22] Class-ify ui_out Simon Marchi
2016-11-30 12:46   ` Pedro Alves
2016-11-30 21:47     ` Simon Marchi
2016-11-26 15:20 ` [PATCH 19/22] Class-ify ui_out_impl simon.marchi
2016-11-30 13:09   ` Pedro Alves
2016-11-30 22:38     ` Simon Marchi
2016-11-30 22:58       ` Pedro Alves
2016-12-01 19:04         ` Simon Marchi
2016-12-01 19:30           ` Pedro Alves
     [not found] ` <20161124153228.25177-20-simon.marchi@polymtl.ca>
2016-11-26 17:18   ` [PATCH 20/22] Class-ify ui_out_table Simon Marchi
2016-11-30 12:30     ` Pedro Alves
2016-11-30 21:48       ` [PATCH v2] " Simon Marchi
2016-11-30 23:01         ` Pedro Alves

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=20161124152710.25007-13-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --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