public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 2/2] don't allocate serial_ops
  2013-12-06 19:09 [PATCH 0/2] constify serial_ops Tom Tromey
  2013-12-06 19:09 ` [PATCH 1/2] make serial_ops const Tom Tromey
@ 2013-12-06 19:09 ` Tom Tromey
  2013-12-09  6:14   ` Yao Qi
  2013-12-19 15:59 ` [PATCH 0/2] constify serial_ops Tom Tromey
  2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2013-12-06 19:09 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Now that struct serial_ops is const everywhere, we can easily turn the
instances into globals.  This patch implements this idea.

On the one hand I think this is nicer since it makes a bit more data
readonly and slightly reduces allocations.  On the other hand it
reduces readability somewhat.

If the readability is a concern to anyone I was thinking I could write
a macro that conditionally uses GCC's designated initializer
extension.

Tested by rebuilding on x86-64 Fedora 18, both natively and using the
mingw cross tools.

2013-12-06  Tom Tromey  <tromey@redhat.com>

	* ser-unix.c (hardwire_ops): New global.
	(_initialize_ser_hardwire): Use it.
	* ser-tcp.c (tcp_ops): New global.
	(_initialize_ser_tcp): Use it.
	* ser-pipe.c (pipe_ops): New global.
	(_initialize_ser_pipe): Use it.
	* ser-mingw.c (hardwire_ops, tty_ops, pipe_ops, tcp_ops): New
	globals.
	(_initialize_ser_windows): Use them.
---
 gdb/ChangeLog   |  12 +++
 gdb/ser-mingw.c | 233 ++++++++++++++++++++++++++++++--------------------------
 gdb/ser-pipe.c  |  50 ++++++------
 gdb/ser-tcp.c   |  56 ++++++++------
 gdb/ser-unix.c  |  57 +++++++-------
 5 files changed, 226 insertions(+), 182 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 342f3a2..1c9e17e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,17 @@
 2013-12-06  Tom Tromey  <tromey@redhat.com>
 
+	* ser-unix.c (hardwire_ops): New global.
+	(_initialize_ser_hardwire): Use it.
+	* ser-tcp.c (tcp_ops): New global.
+	(_initialize_ser_tcp): Use it.
+	* ser-pipe.c (pipe_ops): New global.
+	(_initialize_ser_pipe): Use it.
+	* ser-mingw.c (hardwire_ops, tty_ops, pipe_ops, tcp_ops): New
+	globals.
+	(_initialize_ser_windows): Use them.
+
+2013-12-06  Tom Tromey  <tromey@redhat.com>
+
 	* serial.c (serial_ops_p): New typedef.
 	(serial_ops_list): Now a VEC.
 	(serial_interface_lookup): Return const.  Use VEC_iterate.
diff --git a/gdb/ser-mingw.c b/gdb/ser-mingw.c
index eef40ce..13522dd 100644
--- a/gdb/ser-mingw.c
+++ b/gdb/ser-mingw.c
@@ -1210,6 +1210,128 @@ net_windows_close (struct serial *scb)
   net_close (scb);
 }
 
+/* The serial port driver.  */
+
+static const struct serial_ops hardwire_ops =
+{
+  "hardwire",
+  ser_windows_open,
+  ser_windows_close,
+  NULL,
+  ser_base_readchar,
+  ser_base_write,
+  ser_windows_flush_output,
+  ser_windows_flush_input,
+  ser_windows_send_break,
+  ser_windows_raw,
+  /* These are only used for stdin; we do not need them for serial
+     ports, so supply the standard dummies.  */
+  ser_base_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  ser_windows_setbaudrate,
+  ser_windows_setstopbits,
+  ser_windows_drain_output,
+  ser_base_async,
+  ser_windows_read_prim,
+  ser_windows_write_prim,
+  NULL,
+  ser_windows_wait_handle
+};
+
+/* The dummy serial driver used for terminals.  We only provide the
+   TTY-related methods.  */
+
+static const struct serial_ops tty_ops =
+{
+  "terminal",
+  NULL,
+  ser_console_close,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  ser_console_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  NULL,
+  NULL,
+  ser_base_drain_output,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  ser_console_wait_handle,
+  ser_console_done_wait_handle
+};
+
+/* The pipe interface.  */
+
+static const struct serial_ops pipe_ops =
+{
+  "pipe",
+  pipe_windows_open,
+  pipe_windows_close,
+  pipe_windows_fdopen,
+  ser_base_readchar,
+  ser_base_write,
+  ser_base_flush_output,
+  ser_base_flush_input,
+  ser_base_send_break,
+  ser_base_raw,
+  ser_base_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  ser_base_setbaudrate,
+  ser_base_setstopbits,
+  ser_base_drain_output,
+  ser_base_async,
+  pipe_windows_read,
+  pipe_windows_write,
+  pipe_avail,
+  pipe_wait_handle,
+  pipe_done_wait_handle
+};
+
+/* The TCP/UDP socket driver.  */
+
+static const struct serial_ops tcp_ops =
+{
+  "tcp",
+  net_windows_open,
+  net_windows_close,
+  NULL,
+  ser_base_readchar,
+  ser_base_write,
+  ser_base_flush_output,
+  ser_base_flush_input,
+  ser_tcp_send_break,
+  ser_base_raw,
+  ser_base_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  ser_base_setbaudrate,
+  ser_base_setstopbits,
+  ser_base_drain_output,
+  ser_base_async,
+  net_read_prim,
+  net_write_prim,
+  NULL,
+  net_windows_wait_handle,
+  net_windows_done_wait_handle
+};
+
 void
 _initialize_ser_windows (void)
 {
@@ -1228,88 +1350,9 @@ _initialize_ser_windows (void)
   else
     CancelIo = NULL;
 
-  /* Now register the serial port driver.  */
-  ops = XMALLOC (struct serial_ops);
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "hardwire";
-  ops->open = ser_windows_open;
-  ops->close = ser_windows_close;
-
-  ops->flush_output = ser_windows_flush_output;
-  ops->flush_input = ser_windows_flush_input;
-  ops->send_break = ser_windows_send_break;
-
-  /* These are only used for stdin; we do not need them for serial
-     ports, so supply the standard dummies.  */
-  ops->get_tty_state = ser_base_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-
-  ops->go_raw = ser_windows_raw;
-  ops->setbaudrate = ser_windows_setbaudrate;
-  ops->setstopbits = ser_windows_setstopbits;
-  ops->drain_output = ser_windows_drain_output;
-  ops->readchar = ser_base_readchar;
-  ops->write = ser_base_write;
-  ops->async = ser_base_async;
-  ops->read_prim = ser_windows_read_prim;
-  ops->write_prim = ser_windows_write_prim;
-  ops->wait_handle = ser_windows_wait_handle;
-
-  serial_add_interface (ops);
-
-  /* Next create the dummy serial driver used for terminals.  We only
-     provide the TTY-related methods.  */
-
-  ops = XMALLOC (struct serial_ops);
-  memset (ops, 0, sizeof (struct serial_ops));
-
-  ops->name = "terminal";
-
-  ops->close = ser_console_close;
-  ops->get_tty_state = ser_console_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-  ops->drain_output = ser_base_drain_output;
-  ops->wait_handle = ser_console_wait_handle;
-  ops->done_wait_handle = ser_console_done_wait_handle;
-
-  serial_add_interface (ops);
-
-  /* The pipe interface.  */
-
-  ops = XMALLOC (struct serial_ops);
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "pipe";
-  ops->open = pipe_windows_open;
-  ops->close = pipe_windows_close;
-  ops->fdopen = pipe_windows_fdopen;
-  ops->readchar = ser_base_readchar;
-  ops->write = ser_base_write;
-  ops->flush_output = ser_base_flush_output;
-  ops->flush_input = ser_base_flush_input;
-  ops->send_break = ser_base_send_break;
-  ops->go_raw = ser_base_raw;
-  ops->get_tty_state = ser_base_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-  ops->setbaudrate = ser_base_setbaudrate;
-  ops->setstopbits = ser_base_setstopbits;
-  ops->drain_output = ser_base_drain_output;
-  ops->async = ser_base_async;
-  ops->read_prim = pipe_windows_read;
-  ops->write_prim = pipe_windows_write;
-  ops->wait_handle = pipe_wait_handle;
-  ops->done_wait_handle = pipe_done_wait_handle;
-  ops->avail = pipe_avail;
-
-  serial_add_interface (ops);
+  serial_add_interface (&hardwire_ops);
+  serial_add_interface (&tty_ops);
+  serial_add_interface (&pipe_ops);
 
   /* If WinSock works, register the TCP/UDP socket driver.  */
 
@@ -1317,29 +1360,5 @@ _initialize_ser_windows (void)
     /* WinSock is unavailable.  */
     return;
 
-  ops = XMALLOC (struct serial_ops);
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "tcp";
-  ops->open = net_windows_open;
-  ops->close = net_windows_close;
-  ops->readchar = ser_base_readchar;
-  ops->write = ser_base_write;
-  ops->flush_output = ser_base_flush_output;
-  ops->flush_input = ser_base_flush_input;
-  ops->send_break = ser_tcp_send_break;
-  ops->go_raw = ser_base_raw;
-  ops->get_tty_state = ser_base_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-  ops->setbaudrate = ser_base_setbaudrate;
-  ops->setstopbits = ser_base_setstopbits;
-  ops->drain_output = ser_base_drain_output;
-  ops->async = ser_base_async;
-  ops->read_prim = net_read_prim;
-  ops->write_prim = net_write_prim;
-  ops->wait_handle = net_windows_wait_handle;
-  ops->done_wait_handle = net_windows_done_wait_handle;
-  serial_add_interface (ops);
+  serial_add_interface (&tcp_ops);
 }
diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c
index 70bec5e..c4cef68 100644
--- a/gdb/ser-pipe.c
+++ b/gdb/ser-pipe.c
@@ -206,31 +206,33 @@ gdb_pipe (int pdes[2])
 #endif
 }
 
+static const struct serial_ops pipe_ops =
+{
+  "pipe",
+  pipe_open,
+  pipe_close,
+  NULL,
+  ser_base_readchar,
+  ser_base_write,
+  ser_base_flush_output,
+  ser_base_flush_input,
+  ser_base_send_break,
+  ser_base_raw,
+  ser_base_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  ser_base_setbaudrate,
+  ser_base_setstopbits,
+  ser_base_drain_output,
+  ser_base_async,
+  ser_unix_read_prim,
+  ser_unix_write_prim
+};
+
 void
 _initialize_ser_pipe (void)
 {
-  struct serial_ops *ops = XMALLOC (struct serial_ops);
-
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "pipe";
-  ops->open = pipe_open;
-  ops->close = pipe_close;
-  ops->readchar = ser_base_readchar;
-  ops->write = ser_base_write;
-  ops->flush_output = ser_base_flush_output;
-  ops->flush_input = ser_base_flush_input;
-  ops->send_break = ser_base_send_break;
-  ops->go_raw = ser_base_raw;
-  ops->get_tty_state = ser_base_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-  ops->setbaudrate = ser_base_setbaudrate;
-  ops->setstopbits = ser_base_setstopbits;
-  ops->drain_output = ser_base_drain_output;
-  ops->async = ser_base_async;
-  ops->read_prim = ser_unix_read_prim;
-  ops->write_prim = ser_unix_write_prim;
-  serial_add_interface (ops);
+  serial_add_interface (&pipe_ops);
 }
diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
index c5c5600..2dd342c 100644
--- a/gdb/ser-tcp.c
+++ b/gdb/ser-tcp.c
@@ -372,6 +372,36 @@ show_tcp_cmd (char *args, int from_tty)
   help_list (tcp_show_cmdlist, "show tcp ", -1, gdb_stdout);
 }
 
+#ifndef USE_WIN32API
+
+/* The TCP ops.  */
+
+static const struct serial_ops tcp_ops =
+{
+  "tcp",
+  net_open,
+  net_close,
+  NULL,
+  ser_base_readchar,
+  ser_base_write,
+  ser_base_flush_output,
+  ser_base_flush_input,
+  ser_tcp_send_break,
+  ser_base_raw,
+  ser_base_get_tty_state,
+  ser_base_copy_tty_state,
+  ser_base_set_tty_state,
+  ser_base_print_tty_state,
+  ser_base_noflush_set_tty_state,
+  ser_base_setbaudrate,
+  ser_base_setstopbits,
+  ser_base_drain_output,
+  ser_base_async,
+  net_read_prim,
+  net_write_prim
+};
+
+#endif /* USE_WIN32API */
 
 void
 _initialize_ser_tcp (void)
@@ -380,31 +410,7 @@ _initialize_ser_tcp (void)
   /* Do nothing; the TCP serial operations will be initialized in
      ser-mingw.c.  */
 #else
-  struct serial_ops *ops;
-
-  ops = XMALLOC (struct serial_ops);
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "tcp";
-  ops->open = net_open;
-  ops->close = net_close;
-  ops->readchar = ser_base_readchar;
-  ops->write = ser_base_write;
-  ops->flush_output = ser_base_flush_output;
-  ops->flush_input = ser_base_flush_input;
-  ops->send_break = ser_tcp_send_break;
-  ops->go_raw = ser_base_raw;
-  ops->get_tty_state = ser_base_get_tty_state;
-  ops->copy_tty_state = ser_base_copy_tty_state;
-  ops->set_tty_state = ser_base_set_tty_state;
-  ops->print_tty_state = ser_base_print_tty_state;
-  ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
-  ops->setbaudrate = ser_base_setbaudrate;
-  ops->setstopbits = ser_base_setstopbits;
-  ops->drain_output = ser_base_drain_output;
-  ops->async = ser_base_async;
-  ops->read_prim = net_read_prim;
-  ops->write_prim = net_write_prim;
-  serial_add_interface (ops);
+  serial_add_interface (&tcp_ops);
 #endif /* USE_WIN32API */
 
   add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index d8e4294..bd8d433 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -905,36 +905,41 @@ hardwire_close (struct serial *scb)
 }
 \f
 \f
-void
-_initialize_ser_hardwire (void)
-{
-  struct serial_ops *ops = XMALLOC (struct serial_ops);
 
-  memset (ops, 0, sizeof (struct serial_ops));
-  ops->name = "hardwire";
-  ops->open = hardwire_open;
-  ops->close = hardwire_close;
+/* The hardwire ops.  */
+
+static const struct serial_ops hardwire_ops =
+{
+  "hardwire",
+  hardwire_open,
+  hardwire_close,
+  NULL,
   /* FIXME: Don't replace this with the equivalent ser_base*() until
      the old TERMIOS/SGTTY/... timer code has been flushed.  cagney
      1999-09-16.  */
-  ops->readchar = hardwire_readchar;
-  ops->write = ser_base_write;
-  ops->flush_output = hardwire_flush_output;
-  ops->flush_input = hardwire_flush_input;
-  ops->send_break = hardwire_send_break;
-  ops->go_raw = hardwire_raw;
-  ops->get_tty_state = hardwire_get_tty_state;
-  ops->copy_tty_state = hardwire_copy_tty_state;
-  ops->set_tty_state = hardwire_set_tty_state;
-  ops->print_tty_state = hardwire_print_tty_state;
-  ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
-  ops->setbaudrate = hardwire_setbaudrate;
-  ops->setstopbits = hardwire_setstopbits;
-  ops->drain_output = hardwire_drain_output;
-  ops->async = ser_base_async;
-  ops->read_prim = ser_unix_read_prim;
-  ops->write_prim = ser_unix_write_prim;
-  serial_add_interface (ops);
+  hardwire_readchar,
+  ser_base_write,
+  hardwire_flush_output,
+  hardwire_flush_input,
+  hardwire_send_break,
+  hardwire_raw,
+  hardwire_get_tty_state,
+  hardwire_copy_tty_state,
+  hardwire_set_tty_state,
+  hardwire_print_tty_state,
+  hardwire_noflush_set_tty_state,
+  hardwire_setbaudrate,
+  hardwire_setstopbits,
+  hardwire_drain_output,
+  ser_base_async,
+  ser_unix_read_prim,
+  ser_unix_write_prim
+};
+
+void
+_initialize_ser_hardwire (void)
+{
+  serial_add_interface (&hardwire_ops);
 
 #ifdef HAVE_TERMIOS
 #ifdef CRTSCTS
-- 
1.8.1.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] make serial_ops const
  2013-12-06 19:09 [PATCH 0/2] constify serial_ops Tom Tromey
@ 2013-12-06 19:09 ` Tom Tromey
  2013-12-06 19:09 ` [PATCH 2/2] don't allocate serial_ops Tom Tromey
  2013-12-19 15:59 ` [PATCH 0/2] constify serial_ops Tom Tromey
  2 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2013-12-06 19:09 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I noticed that the serial_ops vtable is not const, but really it ought
to be.

This patch constifies it, removing the only mutable field in the
process.

Tested by rebuilding on x86-64 Fedora 18, both natively and using the
mingw cross tools.

2013-12-06  Tom Tromey  <tromey@redhat.com>

	* serial.c (serial_ops_p): New typedef.
	(serial_ops_list): Now a VEC.
	(serial_interface_lookup): Return const.  Use VEC_iterate.
	(serial_add_interface): Make parameter const.
	(serial_open): Update.
	(serial_fdopen_ops): Make 'ops' const.
	(serial_pipe): Update.
	* ser-tcp.c (_initialize_ser_tcp): Update.
	* ser-pipe.c (_initialize_ser_pipe): Update.
	* ser-unix.c (_initialize_ser_hardwire): Update.
	* ser-mingw.c (_initialize_ser_windows): Update.
	* ser-go32.c (dos_ops): Now const.  Update.
	* serial.h (struct serial) <ops>: Now const.
	(struct serial_ops) <next>: Remove.
	(serial_add_interface): Make parameter const.
---
 gdb/ChangeLog   | 18 ++++++++++++++++++
 gdb/ser-go32.c  |  3 +--
 gdb/ser-mingw.c |  4 ----
 gdb/ser-pipe.c  |  1 -
 gdb/ser-tcp.c   |  1 -
 gdb/ser-unix.c  |  1 -
 gdb/serial.c    | 27 +++++++++++++++------------
 gdb/serial.h    |  5 ++---
 8 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 68935db..342f3a2 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,23 @@
 2013-12-06  Tom Tromey  <tromey@redhat.com>
 
+	* serial.c (serial_ops_p): New typedef.
+	(serial_ops_list): Now a VEC.
+	(serial_interface_lookup): Return const.  Use VEC_iterate.
+	(serial_add_interface): Make parameter const.
+	(serial_open): Update.
+	(serial_fdopen_ops): Make 'ops' const.
+	(serial_pipe): Update.
+	* ser-tcp.c (_initialize_ser_tcp): Update.
+	* ser-pipe.c (_initialize_ser_pipe): Update.
+	* ser-unix.c (_initialize_ser_hardwire): Update.
+	* ser-mingw.c (_initialize_ser_windows): Update.
+	* ser-go32.c (dos_ops): Now const.  Update.
+	* serial.h (struct serial) <ops>: Now const.
+	(struct serial_ops) <next>: Remove.
+	(serial_add_interface): Make parameter const.
+
+2013-12-06  Tom Tromey  <tromey@redhat.com>
+
 	* break-catch-throw.c (fetch_probe_arguments): Use
 	get_probe_argument_count and evaluate_probe_argument.
 	* elfread.c (elf_get_probe_argument_count)
diff --git a/gdb/ser-go32.c b/gdb/ser-go32.c
index 4268e2c..6b87f86 100644
--- a/gdb/ser-go32.c
+++ b/gdb/ser-go32.c
@@ -848,10 +848,9 @@ dos_sendbreak (struct serial *scb)
 }
 
 
-static struct serial_ops dos_ops =
+static const struct serial_ops dos_ops =
 {
   "hardwire",
-  0,
   dos_open,
   dos_close,
   NULL,				/* fdopen, not implemented */
diff --git a/gdb/ser-mingw.c b/gdb/ser-mingw.c
index 0431ea9..eef40ce 100644
--- a/gdb/ser-mingw.c
+++ b/gdb/ser-mingw.c
@@ -1232,7 +1232,6 @@ _initialize_ser_windows (void)
   ops = XMALLOC (struct serial_ops);
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "hardwire";
-  ops->next = 0;
   ops->open = ser_windows_open;
   ops->close = ser_windows_close;
 
@@ -1268,7 +1267,6 @@ _initialize_ser_windows (void)
   memset (ops, 0, sizeof (struct serial_ops));
 
   ops->name = "terminal";
-  ops->next = 0;
 
   ops->close = ser_console_close;
   ops->get_tty_state = ser_console_get_tty_state;
@@ -1287,7 +1285,6 @@ _initialize_ser_windows (void)
   ops = XMALLOC (struct serial_ops);
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "pipe";
-  ops->next = 0;
   ops->open = pipe_windows_open;
   ops->close = pipe_windows_close;
   ops->fdopen = pipe_windows_fdopen;
@@ -1323,7 +1320,6 @@ _initialize_ser_windows (void)
   ops = XMALLOC (struct serial_ops);
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "tcp";
-  ops->next = 0;
   ops->open = net_windows_open;
   ops->close = net_windows_close;
   ops->readchar = ser_base_readchar;
diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c
index b4ab672..70bec5e 100644
--- a/gdb/ser-pipe.c
+++ b/gdb/ser-pipe.c
@@ -213,7 +213,6 @@ _initialize_ser_pipe (void)
 
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "pipe";
-  ops->next = 0;
   ops->open = pipe_open;
   ops->close = pipe_close;
   ops->readchar = ser_base_readchar;
diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
index d8c1ed6..c5c5600 100644
--- a/gdb/ser-tcp.c
+++ b/gdb/ser-tcp.c
@@ -385,7 +385,6 @@ _initialize_ser_tcp (void)
   ops = XMALLOC (struct serial_ops);
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "tcp";
-  ops->next = 0;
   ops->open = net_open;
   ops->close = net_close;
   ops->readchar = ser_base_readchar;
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index 1f1372b..d8e4294 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -912,7 +912,6 @@ _initialize_ser_hardwire (void)
 
   memset (ops, 0, sizeof (struct serial_ops));
   ops->name = "hardwire";
-  ops->next = 0;
   ops->open = hardwire_open;
   ops->close = hardwire_close;
   /* FIXME: Don't replace this with the equivalent ser_base*() until
diff --git a/gdb/serial.c b/gdb/serial.c
index 78e9085..0e0c4f7 100644
--- a/gdb/serial.c
+++ b/gdb/serial.c
@@ -30,9 +30,12 @@ extern void _initialize_serial (void);
 
 static unsigned int global_serial_debug_p;
 
-/* Linked list of serial I/O handlers.  */
+typedef const struct serial_ops *serial_ops_p;
+DEF_VEC_P (serial_ops_p);
 
-static struct serial_ops *serial_ops_list = NULL;
+/* Serial I/O handlers.  */
+
+VEC (serial_ops_p) *serial_ops_list = NULL;
 
 /* Pointer to list of scb's.  */
 
@@ -44,7 +47,7 @@ static struct serial *scb_base;
 static char *serial_logfile = NULL;
 static struct ui_file *serial_logfp = NULL;
 
-static struct serial_ops *serial_interface_lookup (const char *);
+static const struct serial_ops *serial_interface_lookup (const char *);
 static void serial_logchar (struct ui_file *stream,
 			    int ch_type, int ch, int timeout);
 static const char logbase_hex[] = "hex";
@@ -143,12 +146,13 @@ serial_log_command (const char *cmd)
 }
 
 \f
-static struct serial_ops *
+static const struct serial_ops *
 serial_interface_lookup (const char *name)
 {
-  struct serial_ops *ops;
+  const struct serial_ops *ops;
+  int i;
 
-  for (ops = serial_ops_list; ops; ops = ops->next)
+  for (i = 0; VEC_iterate (serial_ops_p, serial_ops_list, i, ops); ++i)
     if (strcmp (name, ops->name) == 0)
       return ops;
 
@@ -156,10 +160,9 @@ serial_interface_lookup (const char *name)
 }
 
 void
-serial_add_interface (struct serial_ops *optable)
+serial_add_interface (const struct serial_ops *optable)
 {
-  optable->next = serial_ops_list;
-  serial_ops_list = optable;
+  VEC_safe_push (serial_ops_p, serial_ops_list, optable);
 }
 
 /* Return the open serial device for FD, if found, or NULL if FD is
@@ -183,7 +186,7 @@ struct serial *
 serial_open (const char *name)
 {
   struct serial *scb;
-  struct serial_ops *ops;
+  const struct serial_ops *ops;
   const char *open_name = name;
 
   if (strcmp (name, "pc") == 0)
@@ -246,7 +249,7 @@ serial_open (const char *name)
    interface ops OPS.  */
 
 static struct serial *
-serial_fdopen_ops (const int fd, struct serial_ops *ops)
+serial_fdopen_ops (const int fd, const struct serial_ops *ops)
 {
   struct serial *scb;
 
@@ -584,7 +587,7 @@ serial_done_wait_handle (struct serial *scb)
 int
 serial_pipe (struct serial *scbs[2])
 {
-  struct serial_ops *ops;
+  const struct serial_ops *ops;
   int fildes[2];
 
   ops = serial_interface_lookup ("pipe");
diff --git a/gdb/serial.h b/gdb/serial.h
index 7a97e28..86c3960 100644
--- a/gdb/serial.h
+++ b/gdb/serial.h
@@ -228,7 +228,7 @@ struct serial
        If != -1, this descriptor should be non-blocking or
        ops->avail should be non-NULL.  */
     int error_fd;               
-    struct serial_ops *ops;	/* Function vector */
+    const struct serial_ops *ops; /* Function vector */
     void *state;       		/* Local context info for open FD */
     serial_ttystate ttystate;	/* Not used (yet) */
     int bufcnt;			/* Amount of data remaining in receive
@@ -251,7 +251,6 @@ struct serial
 struct serial_ops
   {
     char *name;
-    struct serial_ops *next;
     int (*open) (struct serial *, const char *name);
     void (*close) (struct serial *);
     int (*fdopen) (struct serial *, int fd);
@@ -301,7 +300,7 @@ struct serial_ops
 
 /* Add a new serial interface to the interface list.  */
 
-extern void serial_add_interface (struct serial_ops * optable);
+extern void serial_add_interface (const struct serial_ops * optable);
 
 /* File in which to record the remote debugging session.  */
 
-- 
1.8.1.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 0/2] constify serial_ops
@ 2013-12-06 19:09 Tom Tromey
  2013-12-06 19:09 ` [PATCH 1/2] make serial_ops const Tom Tromey
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tom Tromey @ 2013-12-06 19:09 UTC (permalink / raw)
  To: gdb-patches

I noticed that struct serial_ops really ought to be const, so I wrote
this little series to constify it.

I consider the first patch to be pretty obvious.  However the second
one is less clear.  Give it a read.

I regtested this series on x86-64 Fedora 18, and also rebuilt it using
the mingw cross tools.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] don't allocate serial_ops
  2013-12-06 19:09 ` [PATCH 2/2] don't allocate serial_ops Tom Tromey
@ 2013-12-09  6:14   ` Yao Qi
  2013-12-10 20:29     ` Tom Tromey
  0 siblings, 1 reply; 6+ messages in thread
From: Yao Qi @ 2013-12-09  6:14 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 12/07/2013 03:09 AM, Tom Tromey wrote:
> Now that struct serial_ops is const everywhere, we can easily turn the
> instances into globals.  This patch implements this idea.
>

Is it better to turn them into static locals of _initialize_ser_XXX
functions?

> On the one hand I think this is nicer since it makes a bit more data
> readonly and slightly reduces allocations.  On the other hand it
> reduces readability somewhat.
>
> If the readability is a concern to anyone I was thinking I could write
> a macro that conditionally uses GCC's designated initializer
> extension.

The patched code is still quite readable to me.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] don't allocate serial_ops
  2013-12-09  6:14   ` Yao Qi
@ 2013-12-10 20:29     ` Tom Tromey
  0 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2013-12-10 20:29 UTC (permalink / raw)
  To: Yao Qi; +Cc: gdb-patches

Tom> Now that struct serial_ops is const everywhere, we can easily turn the
Tom> instances into globals.  This patch implements this idea.

Yao> Is it better to turn them into static locals of _initialize_ser_XXX
Yao> functions?

I tend to avoid static locals.  I don't know that I have a particularly
good reason for it, though.

Yao> The patched code is still quite readable to me.

Thanks.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] constify serial_ops
  2013-12-06 19:09 [PATCH 0/2] constify serial_ops Tom Tromey
  2013-12-06 19:09 ` [PATCH 1/2] make serial_ops const Tom Tromey
  2013-12-06 19:09 ` [PATCH 2/2] don't allocate serial_ops Tom Tromey
@ 2013-12-19 15:59 ` Tom Tromey
  2 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2013-12-19 15:59 UTC (permalink / raw)
  To: gdb-patches

>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

Tom> I noticed that struct serial_ops really ought to be const, so I wrote
Tom> this little series to constify it.

Tom> I consider the first patch to be pretty obvious.  However the second
Tom> one is less clear.  Give it a read.

Tom> I regtested this series on x86-64 Fedora 18, and also rebuilt it using
Tom> the mingw cross tools.

I'm checking these in now.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-12-19 15:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-06 19:09 [PATCH 0/2] constify serial_ops Tom Tromey
2013-12-06 19:09 ` [PATCH 1/2] make serial_ops const Tom Tromey
2013-12-06 19:09 ` [PATCH 2/2] don't allocate serial_ops Tom Tromey
2013-12-09  6:14   ` Yao Qi
2013-12-10 20:29     ` Tom Tromey
2013-12-19 15:59 ` [PATCH 0/2] constify serial_ops Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).