+/* 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 = untyped_lhs;
+ const struct mem_region *rhs = 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 *new)
+{
+ memset (new, 0, sizeof (struct mem_region));
+ new->enabled_p = 1;
+ new->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. */
+
+static void
+require_user_regions (int from_tty)
+{
+ struct mem_region *m;
+ int ix, length;
+
+ /* If we're already using a user-provided list, nothing to do. */
+ if (!mem_use_target)
+ return;
+
+ /* Switch to a user-provided list (possibly a copy of the current
+ one). */
+ mem_use_target = 0;
+
+ /* If we don't have a target-provided region list yet, then
+ no need to warn. */
+ if (mem_region_list == NULL)
+ return;
+
+ /* Otherwise, let the user know how to get back. */
+ if (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);
+}
+
+/* This function should be called before any command which would
+ read the memory region list, other than those which call
+ require_user_regions. It will handle fetching the
+ target-provided list, if necessary. */
+
+static void
+require_target_regions (void)
+{
+ if (mem_use_target && !target_mem_regions_valid)
+ {
+ target_mem_regions_valid = 1;
+ target_mem_region_list = target_memory_map ();
+ mem_region_list = target_mem_region_list;
+ }
+}
+