Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Sergio Durigan Junior <sergiodj@redhat.com>
Cc: GDB Patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH v3] C++ify gdb/common/environ.c
Date: Wed, 19 Apr 2017 04:56:00 -0000	[thread overview]
Message-ID: <843eaee444605d981d452a1801a24ebf@polymtl.ca> (raw)
In-Reply-To: <20170418030319.12637-1-sergiodj@redhat.com>

On 2017-04-17 23:03, Sergio Durigan Junior wrote:
> Changes from v2 (mostly from Simon's review):
> 
> - Not using std::unique_ptr for gdb_environ anymore.
> 
> - Got rid of "_environ" suffixes from the names of the methods.
> 
> - Remove (void) from methods without parameters.
> 
> - Constify return of 'get' method.
> 
> - Change 'const char *' for 'const std::string &' on methods'
>   parameters.
> 
> - Returning a 'const std::vector<char *> &' on the 'get_vector'
>   method.
> 
> - Typos, and other minor nits.
> 
> 
> 
> Disclaimer: this patch depends on
> <https://sourceware.org/ml/gdb-patches/2017-03/msg00551.html> to be
> fully testable.
> 
> As part of the preparation necessary for my upcoming task, I'd like to
> propose that we turn gdb_environ into a class.  The approach taken
> here is simple: the class gdb_environ contains everything that is
> needed to manipulate the environment variables.  These variables are
> stored in two data structures: an unordered_set, good because lookups
> are O(n), and an std::vector<char *>, which can be converted to a
> 'char **' and passed as argument to functions that need it.

It still says O(n) ;)

> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
> index 3145d01..131835e 100644
> --- a/gdb/common/environ.c
> +++ b/gdb/common/environ.c
> @@ -18,165 +18,156 @@
>  #include "common-defs.h"
>  #include "environ.h"
>  #include <algorithm>
> -\f
> +#include <utility>
> 
> -/* Return a new environment object.  */
> +/* See common/environ.h.  */
> 
> -struct gdb_environ *
> -make_environ (void)
> +void
> +gdb_environ::init ()
>  {
> -  struct gdb_environ *e;
> +  extern char **environ;
> +
> +  if (environ == NULL)
> +    return;
> 
> -  e = XNEW (struct gdb_environ);
> +  for (int i = 0; environ[i] != NULL; ++i)
> +    {
> +      std::string v = std::string (environ[i]);
> +      std::size_t pos = v.find ('=');
> +      std::string var = v.substr (0, pos);
> +      std::string value = v.substr (pos + 1);
> 
> -  e->allocated = 10;
> -  e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char 
> *));
> -  e->vector[0] = 0;
> -  return e;
> +      this->m_environ_map.insert (std::make_pair (var, value));

I see that make_pair has a move/xvalue version:

   template< class T1, class T2 >
   std::pair<V1,V2> make_pair( T1&& t, T2&& u );

Does that mean that we could/should do

   this->m_environ_map.insert (std::make_pair (std::move (var), std::move 
(value)));

in order to reuse the resources of the existing strings instead of 
copying them?

> +const char *
> +gdb_environ::get (const std::string &var) const
> +{
> +  try
>      {
> -      e->allocated = std::max (i, e->allocated + 10);
> -      e->vector = (char **) xrealloc ((char *) e->vector,
> -				      (e->allocated + 1) * sizeof (char *));
> +      return (char *) this->m_environ_map.at (var).c_str ();

You can remove the cast here.

I didn't think about it at first, but some unit tests for this class 
would be nice as well.

Thanks,

Simon


  reply	other threads:[~2017-04-19  4:56 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-13  4:05 [PATCH] " Sergio Durigan Junior
2017-04-15 18:51 ` [PATCH v2] " Sergio Durigan Junior
2017-04-15 21:22   ` Simon Marchi
2017-04-18  2:49     ` Sergio Durigan Junior
2017-04-16  5:09   ` Simon Marchi
2017-04-16 17:32     ` Sergio Durigan Junior
2017-04-18  3:03 ` [PATCH v3] " Sergio Durigan Junior
2017-04-19  4:56   ` Simon Marchi [this message]
2017-04-19 16:30     ` Pedro Alves
2017-04-19 18:14   ` Pedro Alves
2017-05-01  2:22     ` Sergio Durigan Junior
2017-05-04 15:30       ` Pedro Alves
2017-06-14 19:22 ` [PATCH v4] " Sergio Durigan Junior
2017-06-16 15:45   ` Pedro Alves
2017-06-16 18:01     ` Sergio Durigan Junior
2017-06-16 18:23       ` Pedro Alves
2017-06-16 21:59         ` Sergio Durigan Junior
2017-06-16 22:23 ` [PATCH v5] " Sergio Durigan Junior
2017-06-17  8:54   ` Simon Marchi
2017-06-19  4:19     ` Sergio Durigan Junior
2017-06-19 13:40       ` Pedro Alves
2017-06-19 16:19         ` Sergio Durigan Junior
2017-06-19 12:13     ` Pedro Alves
2017-06-20 14:02       ` Pedro Alves
2017-06-19  4:36 ` [PATCH v6] " Sergio Durigan Junior
2017-06-19  4:51   ` Sergio Durigan Junior
2017-06-19  7:18     ` Simon Marchi
2017-06-19 14:26       ` Pedro Alves
2017-06-19 15:30         ` Simon Marchi
2017-06-19 15:44           ` Pedro Alves
2017-06-19 15:47             ` Pedro Alves
2017-06-19 16:26             ` Simon Marchi
2017-06-19 16:55               ` Pedro Alves
2017-06-19 17:59                 ` Sergio Durigan Junior
2017-06-19 18:09                   ` Pedro Alves
2017-06-19 18:23                     ` Sergio Durigan Junior
2017-06-19 18:36                       ` Pedro Alves
2017-06-19 18:38                         ` Pedro Alves
2017-06-19 14:26   ` Pedro Alves
2017-06-19 16:13     ` Sergio Durigan Junior
2017-06-19 16:38       ` Pedro Alves
2017-06-19 16:46         ` Sergio Durigan Junior
2017-06-19 18:27 ` [PATCH v7] " Sergio Durigan Junior
2017-06-20  3:27 ` [PATCH v8] " Sergio Durigan Junior
2017-06-20 12:13   ` Pedro Alves
2017-06-20 12:46   ` Simon Marchi
2017-06-20 13:00     ` Sergio Durigan Junior

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=843eaee444605d981d452a1801a24ebf@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=sergiodj@redhat.com \
    /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