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 4/5] C++ify charsets
Date: Sun, 25 Feb 2018 16:32:00 -0000	[thread overview]
Message-ID: <20180225163247.20157-4-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20180225163247.20157-1-simon.marchi@polymtl.ca>

This patch makes the charset list an std::vector instead of a VEC.
Because we must have access to the raw pointers as a simple array, we
can't use a vector of unique_ptr/unique_xmalloc_ptr.  Therefore, wrap
the vector in a simple class to facilitate the cleanup.  This allows
removing one usage of free_char_ptr_vec.

gdb/ChangeLog:

	* charset.c (struct charset_vector): New.
	(charsets): Change type to charset_vector.
	(find_charset_names): Adjust.
	(add_one): Adjust.
	(_initialize_charset): Adjust.
---
 gdb/charset.c | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/gdb/charset.c b/gdb/charset.c
index 98a51794fb..fcb24a4882 100644
--- a/gdb/charset.c
+++ b/gdb/charset.c
@@ -705,21 +705,33 @@ wchar_iterator::iterate (enum wchar_iterate_result *out_result,
   return -1;
 }
 
-/* The charset.c module initialization function.  */
+struct charset_vector
+{
+  ~charset_vector ()
+  {
+    clear ();
+  }
+
+  void clear ()
+  {
+    for (char *c : charsets)
+      xfree (c);
 
-static VEC (char_ptr) *charsets;
+    charsets.clear ();
+  }
+
+  std::vector<char *> charsets;
+};
+
+static charset_vector charsets;
 
 #ifdef PHONY_ICONV
 
 static void
 find_charset_names (void)
 {
-  /* Cast is fine here, because CHARSETS is never released.  Note that
-     the vec does not hold "const char *" pointers instead of "char *"
-     because the non-phony version stores heap-allocated strings in
-     it.  */
-  VEC_safe_push (char_ptr, charsets, (char *) GDB_DEFAULT_HOST_CHARSET);
-  VEC_safe_push (char_ptr, charsets, NULL);
+  charsets.charsets.push_back (xstrdup (GDB_DEFAULT_HOST_CHARSET));
+  charsets.charsets.push_back (NULL);
 }
 
 #else /* PHONY_ICONV */
@@ -740,7 +752,7 @@ add_one (unsigned int count, const char *const *names, void *data)
   unsigned int i;
 
   for (i = 0; i < count; ++i)
-    VEC_safe_push (char_ptr, charsets, xstrdup (names[i]));
+    charsets.charsets.push_back (xstrdup (names[i]));
 
   return 0;
 }
@@ -749,7 +761,8 @@ static void
 find_charset_names (void)
 {
   iconvlist (add_one, NULL);
-  VEC_safe_push (char_ptr, charsets, NULL);
+
+  charsets.charsets.push_back (NULL);
 }
 
 #else
@@ -879,7 +892,7 @@ find_charset_names (void)
 		break;
 	      keep_going = *p;
 	      *p = '\0';
-	      VEC_safe_push (char_ptr, charsets, xstrdup (start));
+	      charsets.charsets.push_back (xstrdup (start));
 	      if (!keep_going)
 		break;
 	      /* Skip any extra spaces.  */
@@ -900,11 +913,10 @@ find_charset_names (void)
   if (fail)
     {
       /* Some error occurred, so drop the vector.  */
-      free_char_ptr_vec (charsets);
-      charsets = NULL;
+      charsets.clear ();
     }
   else
-    VEC_safe_push (char_ptr, charsets, NULL);
+    charsets.charsets.push_back (NULL);
 }
 
 #endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
@@ -994,11 +1006,11 @@ void
 _initialize_charset (void)
 {
   /* The first element is always "auto".  */
-  VEC_safe_push (char_ptr, charsets, xstrdup ("auto"));
+  charsets.charsets.push_back (xstrdup ("auto"));
   find_charset_names ();
 
-  if (VEC_length (char_ptr, charsets) > 1)
-    charset_enum = (const char **) VEC_address (char_ptr, charsets);
+  if (charsets.charsets.size () > 1)
+    charset_enum = (const char **) charsets.charsets.data ();
   else
     charset_enum = default_charset_names;
 
-- 
2.16.1


  parent reply	other threads:[~2018-02-25 16:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-25 16:32 [PATCH 1/5] Make delim_string_to_char_ptr_vec return an std::vector Simon Marchi
2018-02-25 16:32 ` [PATCH 3/5] Make program_space::deleted_solibs a vector of std::string Simon Marchi
2018-02-25 16:32 ` Simon Marchi [this message]
2018-02-25 16:33 ` [PATCH 5/5] Remove free_char_ptr_vec Simon Marchi
2018-02-25 16:47 ` [PATCH 2/5] C++ify program_space Simon Marchi
2018-03-03  4:23 ` [PATCH 1/5] Make delim_string_to_char_ptr_vec return an std::vector Simon Marchi
2018-04-10 19:58 ` Regression with -D_GLIBCXX_DEBUG [Re: [PATCH 1/5] Make delim_string_to_char_ptr_vec return an std::vector] Jan Kratochvil
2018-04-10 20:33   ` Simon Marchi
2018-04-10 20:39     ` Jan Kratochvil
2018-04-10 20:52       ` Simon Marchi

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=20180225163247.20157-4-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