+struct regcache_list
+{
+ struct regcache *regcache;
+ struct regcache_list *next;
+};
+
+static struct regcache_list *current_regcache;
+
+struct regcache *
+get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
+ struct address_space *aspace)
+{
+ struct regcache_list *list;
+ struct regcache *new_regcache;
+
+ for (list = current_regcache; list; list = list->next)
+ if (ptid_equal (list->regcache->ptid, ptid)
+ && get_regcache_arch (list->regcache) == gdbarch)
+ return list->regcache;
+
+ new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
+ new_regcache->ptid = ptid;
+
+ list = xmalloc (sizeof (struct regcache_list));
+ list->regcache = new_regcache;
+ list->next = current_regcache;
+ current_regcache = list;
+
+ return new_regcache;
+}
+
+struct regcache *
+get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
+{
+ struct address_space *aspace;
+
+ /* For the benefit of "maint print registers" & co when debugging an
+ executable, allow dumping the regcache even when there is no
+ thread selected (target_thread_address_space internal-errors if
+ no address space is found). Note that normal user commands will
+ fail higher up on the call stack due to no
+ target_has_registers. */
+ aspace = (ptid_equal (null_ptid, ptid)
+ ? NULL
+ : target_thread_address_space (ptid));
+
+ return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
+}
+
+static ptid_t current_thread_ptid;
+static struct gdbarch *current_thread_arch;
+
+struct regcache *
+get_thread_regcache (ptid_t ptid)
+{
+ if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
+ {
+ current_thread_ptid = ptid;
+ current_thread_arch = target_thread_architecture (ptid);
+ }
+
+ return get_thread_arch_regcache (ptid, current_thread_arch);
+}
+
+struct regcache *
+get_current_regcache (void)
+{
+ return get_thread_regcache (inferior_ptid);
+}