Get rid of VEC (mem_region)

Message ID 1508168838-17957-1-git-send-email-simon.marchi@ericsson.com
State New, archived
Headers

Commit Message

Simon Marchi Oct. 16, 2017, 3:47 p.m. UTC
  This patch removes VEC (mem_region).  Doing so requires touching a lot
of little things here and there.

The fields in mem_attrib are now initialized during construction.  The
values match those that were in default_mem_attrib (now removed).
unknown_mem_attrib is also removed, and replaced with a static method
(mem_attrib::unknown) that returns the equivalent.

mem_region is initialized in a way similar to mem_region_init (now
removed) did.

I found the organization of mem_region_list and target_mem_region_list a
bit confusing.  Sometimes mem_region_list points to the same vector as
target_mem_region_list (and therefore does not own it), and sometimes
(when the user manually edits the mem regions) points to another vector,
and in this case owns it.  To avoid this ambiguity, I think it is
simpler to have two vectors, one for target-defined regions and one for
user-defined regions, and have mem_region_list point to one or the
other.  There are now no vector objects dynamically allocated, both are
static.

The make-target-delegates script does not generate valid code when a
target method returns a type with a parameter list.  For this reason, I
created a typedef (mem_region_vector) that's only used in the target_ops
structure.  If you speak perl, you are welcome to improve the script!

Regtested on the buildbot.

gdb/ChangeLog:

	* memattr.h: Don't include vec.h.
	(struct mem_attrib): Initialize fields.
	<unknown>: New static method.
	(struct mem_region): Add constructors, operator<, initialize
	fields.
	(mem_region_s): Remove.
	(DEF_VEC_O(mem_region_s)): Remove.
	(mem_region_init): Remove.
	(mem_region_cmp): Remove.
	* memattr.c: Include algorithm.
	(default_mem_attrib, unknown_mem_attrib): Remove.
	(user_mem_region_list): New global.
	(target_mem_region_list, mem_region_list): Change type to
	std::vector<mem_region>.
	(mem_use_target): Now a function.
	(target_mem_regions_valid): Change type to bool.
	(mem_region_lessthan, mem_region_cmp, mem_region_init): Remove.
	(require_user_regions): Adjust.
	(require_target_regions): Adjust.
	(create_mem_region): Adjust.
	(lookup_mem_region): Adjust.
	(invalidate_target_mem_regions): Adjust.
	(mem_clear): Rename to...
	(user_mem_clear): ... this, and adjust.
	(mem_command): Adjust.
	(info_mem_command): Adjust.
	(mem_enable, enable_mem_command, mem_disable,
	disable_mem_command): Adjust.
	(mem_delete): Adjust.
	(delete_mem_command): Adjust.
	* memory-map.h (parse_memory_map): Return an std::vector.
	* memory-map.c (parse_memory_map): Likewise.
	(struct memory_map_parsing_data): Add constructor.
	<memory_map>: Point to std::vector.
	(memory_map_start_memory): Adjust.
	(memory_map_end_memory): Adjust.
	(memory_map_end_property): Adjust.
	(clear_result): Remove.
	* remote.c (remote_memory_map): Return an std::vector.
	* target-debug.h (target_debug_print_VEC_mem_region_s__p):
	Remove.
	(target_debug_print_mem_region_vector): New.
	* target-delegates.c: Regenerate.
	* target.h (mem_region_vector): New typedef.
	(to_memory_map): Return mem_region_vector.
	(target_memory_map): Return an std::vector.
	* target.c (target_memory_map): Return an std::vector.
	(flash_erase_command): Adjust.
---
 gdb/memattr.c          | 268 +++++++++++++++++--------------------------------
 gdb/memattr.h          |  67 +++++++++----
 gdb/memory-map.c       |  51 ++++------
 gdb/memory-map.h       |   5 +-
 gdb/remote.c           |   4 +-
 gdb/target-debug.h     |   4 +-
 gdb/target-delegates.c |  12 +--
 gdb/target.c           |  44 ++++----
 gdb/target.h           |  11 +-
 9 files changed, 194 insertions(+), 272 deletions(-)
  

Comments

Simon Marchi Oct. 21, 2017, 4:17 p.m. UTC | #1
On 2017-10-16 11:47 AM, Simon Marchi wrote:
> This patch removes VEC (mem_region).  Doing so requires touching a lot
> of little things here and there.
> 
> The fields in mem_attrib are now initialized during construction.  The
> values match those that were in default_mem_attrib (now removed).
> unknown_mem_attrib is also removed, and replaced with a static method
> (mem_attrib::unknown) that returns the equivalent.
> 
> mem_region is initialized in a way similar to mem_region_init (now
> removed) did.
> 
> I found the organization of mem_region_list and target_mem_region_list a
> bit confusing.  Sometimes mem_region_list points to the same vector as
> target_mem_region_list (and therefore does not own it), and sometimes
> (when the user manually edits the mem regions) points to another vector,
> and in this case owns it.  To avoid this ambiguity, I think it is
> simpler to have two vectors, one for target-defined regions and one for
> user-defined regions, and have mem_region_list point to one or the
> other.  There are now no vector objects dynamically allocated, both are
> static.
> 
> The make-target-delegates script does not generate valid code when a
> target method returns a type with a parameter list.  For this reason, I
> created a typedef (mem_region_vector) that's only used in the target_ops
> structure.  If you speak perl, you are welcome to improve the script!
> 
> Regtested on the buildbot.

I pushed these two patches in.

Simon
  

Patch

diff --git a/gdb/memattr.c b/gdb/memattr.c
index 6ed126b..61e65c1 100644
--- a/gdb/memattr.c
+++ b/gdb/memattr.c
@@ -28,40 +28,26 @@ 
 #include "vec.h"
 #include "breakpoint.h"
 #include "cli/cli-utils.h"
+#include <algorithm>
 
-const struct mem_attrib default_mem_attrib =
-{
-  MEM_RW,			/* mode */
-  MEM_WIDTH_UNSPECIFIED,
-  0,				/* hwbreak */
-  0,				/* cache */
-  0,				/* verify */
-  -1 /* Flash blocksize not specified.  */
-};
-
-const struct mem_attrib unknown_mem_attrib =
-{
-  MEM_NONE,			/* mode */
-  MEM_WIDTH_UNSPECIFIED,
-  0,				/* hwbreak */
-  0,				/* cache */
-  0,				/* verify */
-  -1 /* Flash blocksize not specified.  */
-};
-
-
-VEC(mem_region_s) *mem_region_list, *target_mem_region_list;
+static std::vector<mem_region> user_mem_region_list, target_mem_region_list;
+static std::vector<mem_region> *mem_region_list = &target_mem_region_list;
 static int mem_number = 0;
 
 /* If this flag is set, the memory region list should be automatically
    updated from the target.  If it is clear, the list is user-controlled
    and should be left alone.  */
-static int mem_use_target = 1;
+
+static bool
+mem_use_target ()
+{
+  return mem_region_list == &target_mem_region_list;
+}
 
 /* If this flag is set, we have tried to fetch the target memory regions
    since the last time it was invalidated.  If that list is still
    empty, then the target can't supply memory regions.  */
-static int target_mem_regions_valid;
+static bool target_mem_regions_valid;
 
 /* If this flag is set, gdb will assume that memory ranges not
    specified by the memory map have type MEM_NONE, and will
@@ -81,44 +67,6 @@  show_inaccessible_by_default (struct ui_file *file, int from_tty,
 			      "will be treated as RAM.\n"));          
 }
 
-
-/* Predicate function which returns true if LHS should sort before RHS
-   in a list of memory regions, useful for VEC_lower_bound.  */
-
-static int
-mem_region_lessthan (const struct mem_region *lhs,
-		     const struct mem_region *rhs)
-{
-  return lhs->lo < rhs->lo;
-}
-
-/* A helper function suitable for qsort, used to sort a
-   VEC(mem_region_s) by starting address.  */
-
-int
-mem_region_cmp (const void *untyped_lhs, const void *untyped_rhs)
-{
-  const struct mem_region *lhs = (const struct mem_region *) untyped_lhs;
-  const struct mem_region *rhs = (const struct mem_region *) untyped_rhs;
-
-  if (lhs->lo < rhs->lo)
-    return -1;
-  else if (lhs->lo == rhs->lo)
-    return 0;
-  else
-    return 1;
-}
-
-/* Allocate a new memory region, with default settings.  */
-
-void
-mem_region_init (struct mem_region *newobj)
-{
-  memset (newobj, 0, sizeof (struct mem_region));
-  newobj->enabled_p = 1;
-  newobj->attrib = default_mem_attrib;
-}
-
 /* This function should be called before any command which would
    modify the memory region list.  It will handle switching from
    a target-provided list to a local list, if necessary.  */
@@ -130,16 +78,16 @@  require_user_regions (int from_tty)
   int ix, length;
 
   /* If we're already using a user-provided list, nothing to do.  */
-  if (!mem_use_target)
+  if (!mem_use_target ())
     return;
 
   /* Switch to a user-provided list (possibly a copy of the current
      one).  */
-  mem_use_target = 0;
+  mem_region_list = &user_mem_region_list;
 
   /* If we don't have a target-provided region list yet, then
      no need to warn.  */
-  if (mem_region_list == NULL)
+  if (target_mem_region_list.empty ())
     return;
 
   /* Otherwise, let the user know how to get back.  */
@@ -147,11 +95,9 @@  require_user_regions (int from_tty)
     warning (_("Switching to manual control of memory regions; use "
 	       "\"mem auto\" to fetch regions from the target again."));
 
-  /* And create a new list for the user to modify.  */
-  length = VEC_length (mem_region_s, target_mem_region_list);
-  mem_region_list = VEC_alloc (mem_region_s, length);
-  for (ix = 0; VEC_iterate (mem_region_s, target_mem_region_list, ix, m); ix++)
-    VEC_quick_push (mem_region_s, mem_region_list, m);
+  /* And create a new list (copy of the target-supplied regions) for the user
+     to modify.  */
+  user_mem_region_list = target_mem_region_list;
 }
 
 /* This function should be called before any command which would
@@ -162,21 +108,19 @@  require_user_regions (int from_tty)
 static void
 require_target_regions (void)
 {
-  if (mem_use_target && !target_mem_regions_valid)
+  if (mem_use_target () && !target_mem_regions_valid)
     {
-      target_mem_regions_valid = 1;
+      target_mem_regions_valid = true;
       target_mem_region_list = target_memory_map ();
-      mem_region_list = target_mem_region_list;
     }
 }
 
+/* Create a new user-defined memory region.  */
+
 static void
-create_mem_region (CORE_ADDR lo, CORE_ADDR hi,
-		   const struct mem_attrib *attrib)
+create_user_mem_region (CORE_ADDR lo, CORE_ADDR hi,
+			const mem_attrib &attrib)
 {
-  struct mem_region newobj;
-  int i, ix;
-
   /* lo == hi is a useless empty region.  */
   if (lo >= hi && hi != 0)
     {
@@ -184,30 +128,28 @@  create_mem_region (CORE_ADDR lo, CORE_ADDR hi,
       return;
     }
 
-  mem_region_init (&newobj);
-  newobj.lo = lo;
-  newobj.hi = hi;
+  mem_region newobj (lo, hi, attrib);
 
-  ix = VEC_lower_bound (mem_region_s, mem_region_list, &newobj,
-			mem_region_lessthan);
+  auto it = std::lower_bound (user_mem_region_list.begin (),
+			      user_mem_region_list.end (),
+			      newobj);
+  int ix = std::distance (user_mem_region_list.begin (), it);
 
   /* Check for an overlapping memory region.  We only need to check
      in the vicinity - at most one before and one after the
      insertion point.  */
-  for (i = ix - 1; i < ix + 1; i++)
+  for (int i = ix - 1; i < ix + 1; i++)
     {
-      struct mem_region *n;
-
       if (i < 0)
 	continue;
-      if (i >= VEC_length (mem_region_s, mem_region_list))
+      if (i >= user_mem_region_list.size ())
 	continue;
 
-      n = VEC_index (mem_region_s, mem_region_list, i);
+      mem_region &n = user_mem_region_list[i];
 
-      if ((lo >= n->lo && (lo < n->hi || n->hi == 0)) 
-	  || (hi > n->lo && (hi <= n->hi || n->hi == 0))
-	  || (lo <= n->lo && ((hi >= n->hi && n->hi != 0) || hi == 0)))
+      if ((lo >= n.lo && (lo < n.hi || n.hi == 0))
+	  || (hi > n.lo && (hi <= n.hi || n.hi == 0))
+	  || (lo <= n.lo && ((hi >= n.hi && n.hi != 0) || hi == 0)))
 	{
 	  printf_unfiltered (_("overlapping memory region\n"));
 	  return;
@@ -215,17 +157,16 @@  create_mem_region (CORE_ADDR lo, CORE_ADDR hi,
     }
 
   newobj.number = ++mem_number;
-  newobj.attrib = *attrib;
-  VEC_safe_insert (mem_region_s, mem_region_list, ix, &newobj);
+  user_mem_region_list.insert (it, newobj);
 }
 
 /*
- * Look up the memory region cooresponding to ADDR.
+ * Look up the memory region corresponding to ADDR.
  */
 struct mem_region *
 lookup_mem_region (CORE_ADDR addr)
 {
-  static struct mem_region region;
+  static struct mem_region region (0, 0);
   struct mem_region *m;
   CORE_ADDR lo;
   CORE_ADDR hi;
@@ -244,32 +185,32 @@  lookup_mem_region (CORE_ADDR addr)
   lo = 0;
   hi = 0;
 
-  /* Either find memory range containing ADDRESS, or set LO and HI
+  /* Either find memory range containing ADDR, or set LO and HI
      to the nearest boundaries of an existing memory range.
      
      If we ever want to support a huge list of memory regions, this
      check should be replaced with a binary search (probably using
      VEC_lower_bound).  */
-  for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
+  for (mem_region &m : *mem_region_list)
     {
-      if (m->enabled_p == 1)
+      if (m.enabled_p == 1)
 	{
 	  /* If the address is in the memory region, return that
 	     memory range.  */
-	  if (addr >= m->lo && (addr < m->hi || m->hi == 0))
-	    return m;
+	  if (addr >= m.lo && (addr < m.hi || m.hi == 0))
+	    return &m;
 
 	  /* This (correctly) won't match if m->hi == 0, representing
 	     the top of the address space, because CORE_ADDR is unsigned;
 	     no value of LO is less than zero.  */
-	  if (addr >= m->hi && lo < m->hi)
-	    lo = m->hi;
+	  if (addr >= m.hi && lo < m.hi)
+	    lo = m.hi;
 
 	  /* This will never set HI to zero; if we're here and ADDR
 	     is at or below M, and the region starts at zero, then ADDR
 	     would have been in the region.  */
-	  if (addr <= m->lo && (hi == 0 || hi > m->lo))
-	    hi = m->lo;
+	  if (addr <= m.lo && (hi == 0 || hi > m.lo))
+	    hi = m.lo;
 	}
     }
 
@@ -281,10 +222,10 @@  lookup_mem_region (CORE_ADDR addr)
   /* When no memory map is defined at all, we always return 
      'default_mem_attrib', so that we do not make all memory 
      inaccessible for targets that don't provide a memory map.  */
-  if (inaccessible_by_default && !VEC_empty (mem_region_s, mem_region_list))
-    region.attrib = unknown_mem_attrib;
+  if (inaccessible_by_default && !mem_region_list->empty ())
+    region.attrib = mem_attrib::unknown ();
   else
-    region.attrib = default_mem_attrib;
+    region.attrib = mem_attrib ();
 
   return &region;
 }
@@ -297,18 +238,16 @@  invalidate_target_mem_regions (void)
   if (!target_mem_regions_valid)
     return;
 
-  target_mem_regions_valid = 0;
-  VEC_free (mem_region_s, target_mem_region_list);
-  if (mem_use_target)
-    mem_region_list = NULL;
+  target_mem_regions_valid = false;
+  target_mem_region_list.clear ();
 }
 
-/* Clear memory region list.  */
+/* Clear user-defined memory region list.  */
 
 static void
-mem_clear (void)
+user_mem_clear (void)
 {
-  VEC_free (mem_region_s, mem_region_list);
+  user_mem_region_list.clear ();
 }
 
 
@@ -317,7 +256,6 @@  mem_command (char *args, int from_tty)
 {
   CORE_ADDR lo, hi;
   char *tok;
-  struct mem_attrib attrib;
 
   if (!args)
     error_no_arg (_("No mem"));
@@ -325,16 +263,12 @@  mem_command (char *args, int from_tty)
   /* For "mem auto", switch back to using a target provided list.  */
   if (strcmp (args, "auto") == 0)
     {
-      if (mem_use_target)
+      if (mem_use_target ())
 	return;
 
-      if (mem_region_list != target_mem_region_list)
-	{
-	  mem_clear ();
-	  mem_region_list = target_mem_region_list;
-	}
+      user_mem_clear ();
+      mem_region_list = &target_mem_region_list;
 
-      mem_use_target = 1;
       return;
     }
 
@@ -350,7 +284,7 @@  mem_command (char *args, int from_tty)
     error (_("no hi address"));
   hi = parse_and_eval_address (tok);
 
-  attrib = default_mem_attrib;
+  mem_attrib attrib;
   while ((tok = strtok (NULL, " \t")) != NULL)
     {
       if (strcmp (tok, "rw") == 0)
@@ -404,25 +338,21 @@  mem_command (char *args, int from_tty)
 	error (_("unknown attribute: %s"), tok);
     }
 
-  create_mem_region (lo, hi, &attrib);
+  create_user_mem_region (lo, hi, attrib);
 }
 
 
 static void
 info_mem_command (char *args, int from_tty)
 {
-  struct mem_region *m;
-  struct mem_attrib *attrib;
-  int ix;
-
-  if (mem_use_target)
+  if (mem_use_target ())
     printf_filtered (_("Using memory regions provided by the target.\n"));
   else
     printf_filtered (_("Using user-defined memory regions.\n"));
 
   require_target_regions ();
 
-  if (!mem_region_list)
+  if (mem_region_list->empty ())
     {
       printf_unfiltered (_("There are no memory regions defined.\n"));
       return;
@@ -439,33 +369,33 @@  info_mem_command (char *args, int from_tty)
   printf_filtered ("Attrs ");
   printf_filtered ("\n");
 
-  for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
+  for (const mem_region &m : *mem_region_list)
     {
       const char *tmp;
 
       printf_filtered ("%-3d %-3c\t",
-		       m->number,
-		       m->enabled_p ? 'y' : 'n');
+		       m.number,
+		       m.enabled_p ? 'y' : 'n');
       if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
-	tmp = hex_string_custom (m->lo, 8);
+	tmp = hex_string_custom (m.lo, 8);
       else
-	tmp = hex_string_custom (m->lo, 16);
+	tmp = hex_string_custom (m.lo, 16);
       
       printf_filtered ("%s ", tmp);
 
       if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
 	{
-	  if (m->hi == 0)
+	  if (m.hi == 0)
 	    tmp = "0x100000000";
 	  else
-	    tmp = hex_string_custom (m->hi, 8);
+	    tmp = hex_string_custom (m.hi, 8);
 	}
       else
 	{
-	  if (m->hi == 0)
+	  if (m.hi == 0)
 	    tmp = "0x10000000000000000";
 	  else
-	    tmp = hex_string_custom (m->hi, 16);
+	    tmp = hex_string_custom (m.hi, 16);
 	}
 
       printf_filtered ("%s ", tmp);
@@ -482,8 +412,7 @@  info_mem_command (char *args, int from_tty)
        * time, we may want to consider printing tokens only if they
        * are different from the default attribute.  */
 
-      attrib = &m->attrib;
-      switch (attrib->mode)
+      switch (m.attrib.mode)
 	{
 	case MEM_RW:
 	  printf_filtered ("rw ");
@@ -495,11 +424,11 @@  info_mem_command (char *args, int from_tty)
 	  printf_filtered ("wo ");
 	  break;
 	case MEM_FLASH:
-	  printf_filtered ("flash blocksize 0x%x ", attrib->blocksize);
+	  printf_filtered ("flash blocksize 0x%x ", m.attrib.blocksize);
 	  break;
 	}
 
-      switch (attrib->width)
+      switch (m.attrib.width)
 	{
 	case MEM_WIDTH_8:
 	  printf_filtered ("8 ");
@@ -524,7 +453,7 @@  info_mem_command (char *args, int from_tty)
 	printf_filtered ("swbreak");
 #endif
 
-      if (attrib->cache)
+      if (m.attrib.cache)
 	printf_filtered ("cache ");
       else
 	printf_filtered ("nocache ");
@@ -548,13 +477,10 @@  info_mem_command (char *args, int from_tty)
 static void
 mem_enable (int num)
 {
-  struct mem_region *m;
-  int ix;
-
-  for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
-    if (m->number == num)
+  for (mem_region &m : *mem_region_list)
+    if (m.number == num)
       {
-	m->enabled_p = 1;
+	m.enabled_p = 1;
 	return;
       }
   printf_unfiltered (_("No memory region number %d.\n"), num);
@@ -563,25 +489,21 @@  mem_enable (int num)
 static void
 enable_mem_command (const char *args, int from_tty)
 {
-  int num;
-  struct mem_region *m;
-  int ix;
-
   require_user_regions (from_tty);
 
   target_dcache_invalidate ();
 
   if (args == NULL || *args == '\0')
     { /* Enable all mem regions.  */
-      for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
-	m->enabled_p = 1;
+      for (mem_region &m : *mem_region_list)
+	m.enabled_p = 1;
     }
   else
     {
       number_or_range_parser parser (args);
       while (!parser.finished ())
 	{
-	  num = parser.get_number ();
+	  int num = parser.get_number ();
 	  mem_enable (num);
 	}
     }
@@ -593,13 +515,10 @@  enable_mem_command (const char *args, int from_tty)
 static void
 mem_disable (int num)
 {
-  struct mem_region *m;
-  int ix;
-
-  for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
-    if (m->number == num)
+  for (mem_region &m : *mem_region_list)
+    if (m.number == num)
       {
-	m->enabled_p = 0;
+	m.enabled_p = 0;
 	return;
       }
   printf_unfiltered (_("No memory region number %d.\n"), num);
@@ -617,8 +536,8 @@  disable_mem_command (const char *args, int from_tty)
       struct mem_region *m;
       int ix;
 
-      for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
-	m->enabled_p = 0;
+      for (mem_region &m : *mem_region_list)
+	m.enabled_p = false;
     }
   else
     {
@@ -645,17 +564,16 @@  mem_delete (int num)
       return;
     }
 
-  for (ix = 0; VEC_iterate (mem_region_s, mem_region_list, ix, m); ix++)
-    if (m->number == num)
-      break;
-
-  if (m == NULL)
+  auto it = std::remove_if (mem_region_list->begin (), mem_region_list->end (),
+		  [num] (const mem_region &m)
     {
-      printf_unfiltered (_("No memory region number %d.\n"), num);
-      return;
-    }
+      return m.number == num;
+    });
 
-  VEC_ordered_remove (mem_region_s, mem_region_list, ix);
+  if (it != mem_region_list->end ())
+    mem_region_list->erase (it);
+  else
+    printf_unfiltered (_("No memory region number %d.\n"), num);
 }
 
 static void
@@ -668,7 +586,7 @@  delete_mem_command (const char *args, int from_tty)
   if (args == NULL || *args == '\0')
     {
       if (query (_("Delete all memory regions? ")))
-	mem_clear ();
+	user_mem_clear ();
       dont_repeat ();
       return;
     }
diff --git a/gdb/memattr.h b/gdb/memattr.h
index e869fa3..df71b32 100644
--- a/gdb/memattr.h
+++ b/gdb/memattr.h
@@ -20,8 +20,6 @@ 
 #ifndef MEMATTR_H
 #define MEMATTR_H
 
-#include "vec.h"
-
 enum mem_access_mode
 {
   MEM_NONE,                     /* Memory that is not physically present.  */
@@ -54,27 +52,62 @@  enum mem_access_width
  
 struct mem_attrib 
 {
+  static mem_attrib unknown ()
+  {
+    mem_attrib attrib;
+
+    attrib.mode = MEM_NONE;
+
+    return attrib;
+  }
+
   /* read/write, read-only, or write-only */
-  enum mem_access_mode mode;
+  enum mem_access_mode mode = MEM_RW;
 
-  enum mem_access_width width;
+  enum mem_access_width width = MEM_WIDTH_UNSPECIFIED;
 
   /* enables hardware breakpoints */
-  int hwbreak;
+  int hwbreak = 0;
   
   /* enables host-side caching of memory region data */
-  int cache;
+  int cache = 0;
   
   /* Enables memory verification.  After a write, memory is re-read
      to verify that the write was successful.  */
-  int verify;
+  int verify = 0;
 
   /* Block size.  Only valid if mode == MEM_FLASH.  */
-  int blocksize;
+  int blocksize = -1;
 };
 
 struct mem_region 
 {
+  /* Create a mem_region with default attributes.  */
+
+  mem_region (CORE_ADDR lo_, CORE_ADDR hi_)
+    : lo (lo_), hi (hi_)
+  {}
+
+  /* Create a mem_region with access mode MODE_, but otherwise default
+     attributes.  */
+
+  mem_region (CORE_ADDR lo_, CORE_ADDR hi_, mem_access_mode mode_)
+    : lo (lo_), hi (hi_)
+  {
+    attrib.mode = mode_;
+  }
+
+  /* Create a mem_region with attributes ATTRIB_.  */
+
+  mem_region (CORE_ADDR lo_, CORE_ADDR hi_, const mem_attrib &attrib_)
+    : lo (lo_), hi (hi_), attrib (attrib_)
+  {}
+
+  bool operator< (const mem_region &other) const
+  {
+    return this->lo < other.lo;
+  }
+
   /* Lowest address in the region.  */
   CORE_ADDR lo;
   /* Address past the highest address of the region. 
@@ -82,28 +115,18 @@  struct mem_region
   CORE_ADDR hi;
 
   /* Item number of this memory region.  */
-  int number;
+  int number = 0;
 
-  /* Status of this memory region (enabled if non-zero, otherwise
+  /* Status of this memory region (enabled if true, otherwise
      disabled).  */
-  int enabled_p;
+  bool enabled_p = true;
 
   /* Attributes for this region.  */
-  struct mem_attrib attrib;
+  mem_attrib attrib;
 };
 
-/* Declare a vector type for a group of mem_region structures.  The
-   typedef is necessary because vec.h can not handle a struct tag.
-   Except during construction, these vectors are kept sorted.  */
-typedef struct mem_region mem_region_s;
-DEF_VEC_O(mem_region_s);
-
 extern struct mem_region *lookup_mem_region(CORE_ADDR);
 
 void invalidate_target_mem_regions (void);
 
-void mem_region_init (struct mem_region *);
-
-int mem_region_cmp (const void *, const void *);
-
 #endif	/* MEMATTR_H */
diff --git a/gdb/memory-map.c b/gdb/memory-map.c
index ff05394..9582ceb 100644
--- a/gdb/memory-map.c
+++ b/gdb/memory-map.c
@@ -22,7 +22,7 @@ 
 
 #if !defined(HAVE_LIBEXPAT)
 
-VEC(mem_region_s) *
+std::vector<mem_region>
 parse_memory_map (const char *memory_map)
 {
   static int have_warned;
@@ -34,7 +34,7 @@  parse_memory_map (const char *memory_map)
 		 "at compile time"));
     }
 
-  return NULL;
+  return std::vector<mem_region> ();
 }
 
 #else /* HAVE_LIBEXPAT */
@@ -44,7 +44,11 @@  parse_memory_map (const char *memory_map)
 /* Internal parsing data passed to all XML callbacks.  */
 struct memory_map_parsing_data
 {
-  VEC(mem_region_s) **memory_map;
+  memory_map_parsing_data (std::vector<mem_region> *memory_map_)
+  : memory_map (memory_map_)
+  {}
+
+  std::vector<mem_region> *memory_map;
 
   std::string property_name;
 };
@@ -58,7 +62,6 @@  memory_map_start_memory (struct gdb_xml_parser *parser,
 {
   struct memory_map_parsing_data *data
     = (struct memory_map_parsing_data *) user_data;
-  struct mem_region *r = VEC_safe_push (mem_region_s, *data->memory_map, NULL);
   ULONGEST *start_p, *length_p, *type_p;
 
   start_p
@@ -68,11 +71,8 @@  memory_map_start_memory (struct gdb_xml_parser *parser,
   type_p
     = (ULONGEST *) xml_find_attribute (attributes, "type")->value;
 
-  mem_region_init (r);
-  r->lo = *start_p;
-  r->hi = r->lo + *length_p;
-  r->attrib.mode = (enum mem_access_mode) *type_p;
-  r->attrib.blocksize = -1;
+  data->memory_map->emplace_back (*start_p, *length_p,
+				  (enum mem_access_mode) *type_p);
 }
 
 /* Handle the end of a <memory> element.  Verify that any necessary
@@ -85,9 +85,9 @@  memory_map_end_memory (struct gdb_xml_parser *parser,
 {
   struct memory_map_parsing_data *data
     = (struct memory_map_parsing_data *) user_data;
-  struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
+  const mem_region &r = data->memory_map->back ();
 
-  if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1)
+  if (r.attrib.mode == MEM_FLASH && r.attrib.blocksize == -1)
     gdb_xml_error (parser, _("Flash block size is not set"));
 }
 
@@ -119,25 +119,15 @@  memory_map_end_property (struct gdb_xml_parser *parser,
 
   if (data->property_name == "blocksize")
     {
-      struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
+      mem_region &r = data->memory_map->back ();
 
-      r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
+      r.attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
     }
   else
     gdb_xml_debug (parser, _("Unknown property \"%s\""),
 		   data->property_name.c_str ());
 }
 
-/* Discard the constructed memory map (if an error occurs).  */
-
-static void
-clear_result (void *p)
-{
-  VEC(mem_region_s) **result = (VEC(mem_region_s) **) p;
-  VEC_free (mem_region_s, *result);
-  *result = NULL;
-}
-
 /* The allowed elements and attributes for an XML memory map.  */
 
 const struct gdb_xml_attribute property_attributes[] = {
@@ -178,25 +168,20 @@  const struct gdb_xml_element memory_map_elements[] = {
   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
 };
 
-VEC(mem_region_s) *
+std::vector<mem_region>
 parse_memory_map (const char *memory_map)
 {
-  VEC(mem_region_s) *result = NULL;
-  struct cleanup *back_to;
-  struct memory_map_parsing_data data = { NULL };
+  std::vector<mem_region> ret;
+  memory_map_parsing_data data (&ret);
 
-  data.memory_map = &result;
-  back_to = make_cleanup (clear_result, &result);
   if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
 			   memory_map, &data) == 0)
     {
       /* Parsed successfully, keep the result.  */
-      discard_cleanups (back_to);
-      return result;
+      return ret;
     }
 
-  do_cleanups (back_to);
-  return NULL;
+  return std::vector<mem_region> ();
 }
 
 #endif /* HAVE_LIBEXPAT */
diff --git a/gdb/memory-map.h b/gdb/memory-map.h
index 06d7cc3..a1a4d53 100644
--- a/gdb/memory-map.h
+++ b/gdb/memory-map.h
@@ -25,8 +25,7 @@ 
 
 /* Parses XML memory map passed as argument and returns the memory
    regions it describes.  On any error, emits error message and
-   returns 0.  Does not throw.  Ownership of result is passed to the
-   caller.  */
-VEC(mem_region_s) *parse_memory_map (const char *memory_map);
+   return an empty vector.  Does not throw.  */
+std::vector<mem_region> parse_memory_map (const char *memory_map);
 
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index b38ace9..c2d6f56 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -10904,10 +10904,10 @@  remote_rcmd (struct target_ops *self, const char *command,
     }
 }
 
-static VEC(mem_region_s) *
+static std::vector<mem_region>
 remote_memory_map (struct target_ops *ops)
 {
-  VEC(mem_region_s) *result = NULL;
+  std::vector<mem_region> result;
   char *text = target_read_stralloc (&current_target,
 				     TARGET_OBJECT_MEMORY_MAP, NULL);
 
diff --git a/gdb/target-debug.h b/gdb/target-debug.h
index 580ecd4..14196b4 100644
--- a/gdb/target-debug.h
+++ b/gdb/target-debug.h
@@ -114,8 +114,8 @@ 
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_bfd_p(X) \
   target_debug_do_print (host_address_to_string (X))
-#define target_debug_print_VEC_mem_region_s__p(X)	\
-  target_debug_do_print (host_address_to_string (X))
+#define target_debug_print_mem_region_vector(X) \
+  target_debug_do_print (host_address_to_string (X.data ()))
 #define target_debug_print_VEC_static_tracepoint_marker_p__p(X)	\
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_const_struct_target_desc_p(X)	\
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 00efbb1..e0d7a9a 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -2148,29 +2148,29 @@  debug_get_memory_xfer_limit (struct target_ops *self)
   return result;
 }
 
-static VEC(mem_region_s) *
+static mem_region_vector
 delegate_memory_map (struct target_ops *self)
 {
   self = self->beneath;
   return self->to_memory_map (self);
 }
 
-static VEC(mem_region_s) *
+static mem_region_vector
 tdefault_memory_map (struct target_ops *self)
 {
-  return NULL;
+  return std::vector<mem_region> ();
 }
 
-static VEC(mem_region_s) *
+static mem_region_vector
 debug_memory_map (struct target_ops *self)
 {
-  VEC(mem_region_s) * result;
+  mem_region_vector result;
   fprintf_unfiltered (gdb_stdlog, "-> %s->to_memory_map (...)\n", debug_target.to_shortname);
   result = debug_target.to_memory_map (&debug_target);
   fprintf_unfiltered (gdb_stdlog, "<- %s->to_memory_map (", debug_target.to_shortname);
   target_debug_print_struct_target_ops_p (&debug_target);
   fputs_unfiltered (") = ", gdb_stdlog);
-  target_debug_print_VEC_mem_region_s__p (result);
+  target_debug_print_mem_region_vector (result);
   fputs_unfiltered ("\n", gdb_stdlog);
   return result;
 }
diff --git a/gdb/target.c b/gdb/target.c
index 4a7589d..245f08a 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -1479,34 +1479,31 @@  target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
 
 /* Fetch the target's memory map.  */
 
-VEC(mem_region_s) *
+std::vector<mem_region>
 target_memory_map (void)
 {
-  VEC(mem_region_s) *result;
-  struct mem_region *last_one, *this_one;
-  int ix;
-  result = current_target.to_memory_map (&current_target);
-  if (result == NULL)
-    return NULL;
+  std::vector<mem_region> result
+    = current_target.to_memory_map (&current_target);
+  if (result.empty ())
+    return result;
 
-  qsort (VEC_address (mem_region_s, result),
-	 VEC_length (mem_region_s, result),
-	 sizeof (struct mem_region), mem_region_cmp);
+  std::sort (result.begin (), result.end ());
 
   /* Check that regions do not overlap.  Simultaneously assign
      a numbering for the "mem" commands to use to refer to
      each region.  */
-  last_one = NULL;
-  for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
+  mem_region *last_one = NULL;
+  for (size_t ix = 0; ix < result.size (); ix++)
     {
+      mem_region *this_one = &result[ix];
       this_one->number = ix;
 
-      if (last_one && last_one->hi > this_one->lo)
+      if (last_one != NULL && last_one->hi > this_one->lo)
 	{
 	  warning (_("Overlapping regions in memory map: ignoring"));
-	  VEC_free (mem_region_s, result);
-	  return NULL;
+	  return std::vector<mem_region> ();
 	}
+
       last_one = this_one;
     }
 
@@ -3817,30 +3814,25 @@  flash_erase_command (char *cmd, int from_tty)
 {
   /* Used to communicate termination of flash operations to the target.  */
   bool found_flash_region = false;
-  struct mem_region *m;
   struct gdbarch *gdbarch = target_gdbarch ();
 
-  VEC(mem_region_s) *mem_regions = target_memory_map ();
+  std::vector<mem_region> mem_regions = target_memory_map ();
 
   /* Iterate over all memory regions.  */
-  for (int i = 0; VEC_iterate (mem_region_s, mem_regions, i, m); i++)
+  for (const mem_region &m : mem_regions)
     {
-      /* Fetch the memory attribute.  */
-      struct mem_attrib *attrib = &m->attrib;
-
       /* Is this a flash memory region?  */
-      if (attrib->mode == MEM_FLASH)
+      if (m.attrib.mode == MEM_FLASH)
         {
           found_flash_region = true;
-          target_flash_erase (m->lo, m->hi - m->lo);
+          target_flash_erase (m.lo, m.hi - m.lo);
 
 	  ui_out_emit_tuple tuple_emitter (current_uiout, "erased-regions");
 
           current_uiout->message (_("Erasing flash memory region at address "));
-          current_uiout->field_fmt ("address", "%s", paddress (gdbarch,
-								 m->lo));
+          current_uiout->field_fmt ("address", "%s", paddress (gdbarch, m.lo));
           current_uiout->message (", size = ");
-          current_uiout->field_fmt ("size", "%s", hex_string (m->hi - m->lo));
+          current_uiout->field_fmt ("size", "%s", hex_string (m.hi - m.lo));
           current_uiout->message ("\n");
         }
     }
diff --git a/gdb/target.h b/gdb/target.h
index 581c89b..7028f67 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -419,6 +419,11 @@  typedef void async_callback_ftype (enum inferior_event_type event_type,
 #define TARGET_DEFAULT_RETURN(ARG)
 #define TARGET_DEFAULT_FUNC(ARG)
 
+/* Define a typedef, because make-target-delegates doesn't currently handle type
+   names with templates.  */
+
+typedef std::vector<mem_region> mem_region_vector;
+
 struct target_ops
   {
     struct target_ops *beneath;	/* To the target under this one.  */
@@ -773,8 +778,8 @@  struct target_ops
        This method should not cache data; if the memory map could
        change unexpectedly, it should be invalidated, and higher
        layers will re-fetch it.  */
-    VEC(mem_region_s) *(*to_memory_map) (struct target_ops *)
-      TARGET_DEFAULT_RETURN (NULL);
+    mem_region_vector (*to_memory_map) (struct target_ops *)
+      TARGET_DEFAULT_RETURN (std::vector<mem_region> ());
 
     /* Erases the region of flash memory starting at ADDRESS, of
        length LENGTH.
@@ -1462,7 +1467,7 @@  extern int target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr,
 /* Fetches the target's memory map.  If one is found it is sorted
    and returned, after some consistency checking.  Otherwise, NULL
    is returned.  */
-VEC(mem_region_s) *target_memory_map (void);
+std::vector<mem_region> target_memory_map (void);
 
 /* Erases all flash memory regions on the target.  */
 void flash_erase_command (char *cmd, int from_tty);