Message ID | 08C93960-8ED8-431A-B786-3455FF149B77@arm.com |
---|---|
State | New |
Headers | show |
Alan Hayward <Alan.Hayward@arm.com> writes: > Regcache is a class. There is no need for explicit xmalloc > and xfree methods, it is much simpler to use new and delete. > new/delete isn't simpler than xmalloc/xfree, IMO. We still need to take care of releasing resources. > > diff --git a/gdb/frame.c b/gdb/frame.c > index 30e4aeab7e2901074c289ac4d96ebda885805a29..7fd4b07a2e95f28b2eb6a18dea4d2071f0ece4e2 100644 > --- a/gdb/frame.c > +++ b/gdb/frame.c > @@ -1021,13 +1021,12 @@ struct regcache * > frame_save_as_regcache (struct frame_info *this_frame) > { > struct address_space *aspace = get_frame_address_space (this_frame); > - struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame), > - aspace); > - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); > + regcache *backup = new regcache (get_frame_arch (this_frame), aspace); > + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); > It makes no sense to change make_cleanup_regcache_xfree to make_cleanup_regcache_delete. We still have to use cleanup. > - regcache_save (regcache, do_frame_register_read, this_frame); > + regcache_save (backup, do_frame_register_read, this_frame); > discard_cleanups (cleanups); > - return regcache; > + return backup; > } > > void > @@ -1063,7 +1062,7 @@ frame_pop (struct frame_info *this_frame) > trying to extract the old values from the current regcache while > at the same time writing new values into that same cache. */ > scratch = frame_save_as_regcache (prev_frame); > - cleanups = make_cleanup_regcache_xfree (scratch); > + cleanups = make_cleanup_regcache_delete (scratch); scratch is only used within this function, so we can change it to a local object (instead of a pointer), and call regcache_save here. Then, we can remove the cleanups. > diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c > index 0bf587ffe702c68f538fe2877cce6114e64ee1bd..1edcf752c82230fc65669f675e10735ac8e4f654 100644 > --- a/gdb/mi/mi-main.c > +++ b/gdb/mi/mi-main.c > @@ -1051,7 +1051,7 @@ mi_cmd_data_list_changed_registers (const char *command, char **argv, int argc) > > prev_regs = this_regs; > this_regs = frame_save_as_regcache (get_selected_frame (NULL)); > - cleanup = make_cleanup_regcache_xfree (prev_regs); > + cleanup = make_cleanup_regcache_delete (prev_regs); Why don't you do this? then, we don't need this make_cleanup_regcache_xfree. std::unique_ptr<regcache> prev_regs (this_regs); > > /* Note that the test for a valid register must include checking the > gdbarch_register_name because gdbarch_num_regs may be allocated > diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c > index 324b29d90c1266a73f172da20a6015174189625f..42aff2cd1bf3834268b0b58dcf00dac1c52add96 100644 > --- a/gdb/ppc-linux-tdep.c > +++ b/gdb/ppc-linux-tdep.c > @@ -1364,13 +1364,13 @@ ppu2spu_sniffer (const struct frame_unwind *self, > = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache); > > struct address_space *aspace = get_frame_address_space (this_frame); > - struct regcache *regcache = regcache_xmalloc (data.gdbarch, aspace); > - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); > - regcache_save (regcache, ppu2spu_unwind_register, &data); > + regcache *backup = new regcache (data.gdbarch, aspace); > + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); > + regcache_save (backup, ppu2spu_unwind_register, &data); > discard_cleanups (cleanups); We can use std::unique_ptr<regcache> too here. After call regcache_save, call .release () to return the raw pointer to cache->regcache. > > cache->frame_id = frame_id_build (base, func); > - cache->regcache = regcache; > + cache->regcache = backup; > *this_prologue_cache = cache; > return 1; > } > @@ -1383,7 +1383,7 @@ static void > ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache) > { > struct ppu2spu_cache *cache = (struct ppu2spu_cache *) this_cache; > - regcache_xfree (cache->regcache); > + delete cache->regcache; > } > > static const struct frame_unwind ppu2spu_unwind = { > diff --git a/gdb/regcache.h b/gdb/regcache.h > index aeac54e8d7b91e2bcf6a3e1ce8e781c3f994306b..00b87db7d145205b74859c08ca5a9d852441a4aa 100644 > --- a/gdb/regcache.h > +++ b/gdb/regcache.h > @@ -36,10 +36,7 @@ extern target_regcache *get_thread_arch_aspace_regcache (ptid_t, > struct gdbarch *, > struct address_space *); > > -void regcache_xfree (struct regcache *regcache); > -struct cleanup *make_cleanup_regcache_xfree (struct regcache *regcache); > -struct regcache *regcache_xmalloc (struct gdbarch *gdbarch, > - struct address_space *aspace); > +struct cleanup *make_cleanup_regcache_delete (regcache *regcache); > > /* Return REGCACHE's ptid. */ > > @@ -261,12 +258,7 @@ public: > regcache (const regcache &) = delete; > void operator= (const regcache &) = delete; > > - /* class regcache is only extended in unit test, so only mark it > - virtual when selftest is enabled. */ > -#if GDB_SELF_TEST > - virtual > -#endif > - ~regcache () > + virtual ~regcache () This is not related to this patch. It should be in patch #1.
> On 23 Aug 2017, at 10:32, Yao Qi <qiyaoltc@gmail.com> wrote: > > Alan Hayward <Alan.Hayward@arm.com> writes: > >> Regcache is a class. There is no need for explicit xmalloc >> and xfree methods, it is much simpler to use new and delete. >> > > new/delete isn't simpler than xmalloc/xfree, IMO. We still need to take > care of releasing resources. Agreed, but I wanted to only have one way of doing the same thing. We have to have a constructor/destructor. regcache_xmalloc and regcache_xfree are (almost) one liners that call new/delete. > >> >> diff --git a/gdb/frame.c b/gdb/frame.c >> index 30e4aeab7e2901074c289ac4d96ebda885805a29..7fd4b07a2e95f28b2eb6a18dea4d2071f0ece4e2 100644 >> --- a/gdb/frame.c >> +++ b/gdb/frame.c >> @@ -1021,13 +1021,12 @@ struct regcache * >> frame_save_as_regcache (struct frame_info *this_frame) >> { >> struct address_space *aspace = get_frame_address_space (this_frame); >> - struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame), >> - aspace); >> - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); >> + regcache *backup = new regcache (get_frame_arch (this_frame), aspace); >> + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); >> > > It makes no sense to change make_cleanup_regcache_xfree to > make_cleanup_regcache_delete. We still have to use cleanup. I made this change to keep the consistent with the other changes in this patch. Using new together with something_xfree() sounds wrong. If I make all the changes below, then I think we can remove this function. > >> - regcache_save (regcache, do_frame_register_read, this_frame); >> + regcache_save (backup, do_frame_register_read, this_frame); >> discard_cleanups (cleanups); >> - return regcache; >> + return backup; >> } >> >> void >> @@ -1063,7 +1062,7 @@ frame_pop (struct frame_info *this_frame) >> trying to extract the old values from the current regcache while >> at the same time writing new values into that same cache. */ >> scratch = frame_save_as_regcache (prev_frame); >> - cleanups = make_cleanup_regcache_xfree (scratch); >> + cleanups = make_cleanup_regcache_delete (scratch); > > scratch is only used within this function, so we can change it to a > local object (instead of a pointer), and call regcache_save here. Then, > we can remove the cleanups. Ok. > >> diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c >> index 0bf587ffe702c68f538fe2877cce6114e64ee1bd..1edcf752c82230fc65669f675e10735ac8e4f654 100644 >> --- a/gdb/mi/mi-main.c >> +++ b/gdb/mi/mi-main.c >> @@ -1051,7 +1051,7 @@ mi_cmd_data_list_changed_registers (const char *command, char **argv, int argc) >> >> prev_regs = this_regs; >> this_regs = frame_save_as_regcache (get_selected_frame (NULL)); >> - cleanup = make_cleanup_regcache_xfree (prev_regs); >> + cleanup = make_cleanup_regcache_delete (prev_regs); > > Why don't you do this? then, we don't need this make_cleanup_regcache_xfree. > > std::unique_ptr<regcache> prev_regs (this_regs); This is new to me. Happy to use it. > >> >> /* Note that the test for a valid register must include checking the >> gdbarch_register_name because gdbarch_num_regs may be allocated >> diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c >> index 324b29d90c1266a73f172da20a6015174189625f..42aff2cd1bf3834268b0b58dcf00dac1c52add96 100644 >> --- a/gdb/ppc-linux-tdep.c >> +++ b/gdb/ppc-linux-tdep.c >> @@ -1364,13 +1364,13 @@ ppu2spu_sniffer (const struct frame_unwind *self, >> = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache); >> >> struct address_space *aspace = get_frame_address_space (this_frame); >> - struct regcache *regcache = regcache_xmalloc (data.gdbarch, aspace); >> - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); >> - regcache_save (regcache, ppu2spu_unwind_register, &data); >> + regcache *backup = new regcache (data.gdbarch, aspace); >> + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); >> + regcache_save (backup, ppu2spu_unwind_register, &data); >> discard_cleanups (cleanups); > > We can use std::unique_ptr<regcache> too here. After call > regcache_save, call .release () to return the raw pointer to cache->regcache. Ok. > >> >> cache->frame_id = frame_id_build (base, func); >> - cache->regcache = regcache; >> + cache->regcache = backup; >> *this_prologue_cache = cache; >> return 1; >> } >> @@ -1383,7 +1383,7 @@ static void >> ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache) >> { >> struct ppu2spu_cache *cache = (struct ppu2spu_cache *) this_cache; >> - regcache_xfree (cache->regcache); >> + delete cache->regcache; >> } >> >> static const struct frame_unwind ppu2spu_unwind = { >> diff --git a/gdb/regcache.h b/gdb/regcache.h >> index aeac54e8d7b91e2bcf6a3e1ce8e781c3f994306b..00b87db7d145205b74859c08ca5a9d852441a4aa 100644 >> --- a/gdb/regcache.h >> +++ b/gdb/regcache.h >> @@ -36,10 +36,7 @@ extern target_regcache *get_thread_arch_aspace_regcache (ptid_t, >> struct gdbarch *, >> struct address_space *); >> >> -void regcache_xfree (struct regcache *regcache); >> -struct cleanup *make_cleanup_regcache_xfree (struct regcache *regcache); >> -struct regcache *regcache_xmalloc (struct gdbarch *gdbarch, >> - struct address_space *aspace); >> +struct cleanup *make_cleanup_regcache_delete (regcache *regcache); >> >> /* Return REGCACHE's ptid. */ >> >> @@ -261,12 +258,7 @@ public: >> regcache (const regcache &) = delete; >> void operator= (const regcache &) = delete; >> >> - /* class regcache is only extended in unit test, so only mark it >> - virtual when selftest is enabled. */ >> -#if GDB_SELF_TEST >> - virtual >> -#endif >> - ~regcache () >> + virtual ~regcache () > > This is not related to this patch. It should be in patch #1. Ok. > > -- > Yao (齐尧)
diff --git a/gdb/frame.c b/gdb/frame.c index 30e4aeab7e2901074c289ac4d96ebda885805a29..7fd4b07a2e95f28b2eb6a18dea4d2071f0ece4e2 100644 --- a/gdb/frame.c +++ b/gdb/frame.c @@ -1021,13 +1021,12 @@ struct regcache * frame_save_as_regcache (struct frame_info *this_frame) { struct address_space *aspace = get_frame_address_space (this_frame); - struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame), - aspace); - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); + regcache *backup = new regcache (get_frame_arch (this_frame), aspace); + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); - regcache_save (regcache, do_frame_register_read, this_frame); + regcache_save (backup, do_frame_register_read, this_frame); discard_cleanups (cleanups); - return regcache; + return backup; } void @@ -1063,7 +1062,7 @@ frame_pop (struct frame_info *this_frame) trying to extract the old values from the current regcache while at the same time writing new values into that same cache. */ scratch = frame_save_as_regcache (prev_frame); - cleanups = make_cleanup_regcache_xfree (scratch); + cleanups = make_cleanup_regcache_delete (scratch); /* FIXME: cagney/2003-03-16: It should be possible to tell the target's register cache that it is about to be hit with a burst diff --git a/gdb/infrun.c b/gdb/infrun.c index d6723fde85c90bce2a93d1ab308332f9387c3f15..6510aec548f6495ca64f5b566ff3628734698113 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -8963,7 +8963,7 @@ make_cleanup_restore_infcall_suspend_state void discard_infcall_suspend_state (struct infcall_suspend_state *inf_state) { - regcache_xfree (inf_state->registers); + delete inf_state->registers; xfree (inf_state->siginfo_data); xfree (inf_state); } diff --git a/gdb/jit.c b/gdb/jit.c index 725d41ef0a5b01a22d5994bdd4b7b336fd3c10a9..e96572a8d1d7fe16561be7ba8a61b56d6bb1e85d 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -1168,7 +1168,7 @@ jit_dealloc_cache (struct frame_info *this_frame, void *cache) struct jit_unwind_private *priv_data = (struct jit_unwind_private *) cache; gdb_assert (priv_data->regcache != NULL); - regcache_xfree (priv_data->regcache); + delete priv_data->regcache; xfree (priv_data); } @@ -1206,7 +1206,7 @@ jit_frame_sniffer (const struct frame_unwind *self, *cache = XCNEW (struct jit_unwind_private); priv_data = (struct jit_unwind_private *) *cache; - priv_data->regcache = regcache_xmalloc (gdbarch, aspace); + priv_data->regcache = new regcache (gdbarch, aspace); priv_data->this_frame = this_frame; callbacks.priv_data = priv_data; diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c index 573a3e9ecf040bb77b3f116e60289ba60266ffd0..032ff62807b728be336bf0e5daf59e47615d4799 100644 --- a/gdb/linux-fork.c +++ b/gdb/linux-fork.c @@ -129,7 +129,7 @@ free_fork (struct fork_info *fp) if (fp) { if (fp->savedregs) - regcache_xfree (fp->savedregs); + delete fp->savedregs; if (fp->filepos) xfree (fp->filepos); xfree (fp); @@ -295,7 +295,7 @@ fork_save_infrun_state (struct fork_info *fp, int clobber_regs) DIR *d; if (fp->savedregs) - regcache_xfree (fp->savedregs); + delete fp->savedregs; fp->savedregs = regcache_dup (get_current_regcache ()); fp->clobber_regs = clobber_regs; diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 0bf587ffe702c68f538fe2877cce6114e64ee1bd..1edcf752c82230fc65669f675e10735ac8e4f654 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1051,7 +1051,7 @@ mi_cmd_data_list_changed_registers (const char *command, char **argv, int argc) prev_regs = this_regs; this_regs = frame_save_as_regcache (get_selected_frame (NULL)); - cleanup = make_cleanup_regcache_xfree (prev_regs); + cleanup = make_cleanup_regcache_delete (prev_regs); /* Note that the test for a valid register must include checking the gdbarch_register_name because gdbarch_num_regs may be allocated diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c index 324b29d90c1266a73f172da20a6015174189625f..42aff2cd1bf3834268b0b58dcf00dac1c52add96 100644 --- a/gdb/ppc-linux-tdep.c +++ b/gdb/ppc-linux-tdep.c @@ -1364,13 +1364,13 @@ ppu2spu_sniffer (const struct frame_unwind *self, = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache); struct address_space *aspace = get_frame_address_space (this_frame); - struct regcache *regcache = regcache_xmalloc (data.gdbarch, aspace); - struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache); - regcache_save (regcache, ppu2spu_unwind_register, &data); + regcache *backup = new regcache (data.gdbarch, aspace); + struct cleanup *cleanups = make_cleanup_regcache_delete (backup); + regcache_save (backup, ppu2spu_unwind_register, &data); discard_cleanups (cleanups); cache->frame_id = frame_id_build (base, func); - cache->regcache = regcache; + cache->regcache = backup; *this_prologue_cache = cache; return 1; } @@ -1383,7 +1383,7 @@ static void ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache) { struct ppu2spu_cache *cache = (struct ppu2spu_cache *) this_cache; - regcache_xfree (cache->regcache); + delete cache->regcache; } static const struct frame_unwind ppu2spu_unwind = { diff --git a/gdb/regcache.h b/gdb/regcache.h index aeac54e8d7b91e2bcf6a3e1ce8e781c3f994306b..00b87db7d145205b74859c08ca5a9d852441a4aa 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -36,10 +36,7 @@ extern target_regcache *get_thread_arch_aspace_regcache (ptid_t, struct gdbarch *, struct address_space *); -void regcache_xfree (struct regcache *regcache); -struct cleanup *make_cleanup_regcache_xfree (struct regcache *regcache); -struct regcache *regcache_xmalloc (struct gdbarch *gdbarch, - struct address_space *aspace); +struct cleanup *make_cleanup_regcache_delete (regcache *regcache); /* Return REGCACHE's ptid. */ @@ -261,12 +258,7 @@ public: regcache (const regcache &) = delete; void operator= (const regcache &) = delete; - /* class regcache is only extended in unit test, so only mark it - virtual when selftest is enabled. */ -#if GDB_SELF_TEST - virtual -#endif - ~regcache () + virtual ~regcache () { xfree (m_registers); xfree (m_register_status); diff --git a/gdb/regcache.c b/gdb/regcache.c index 524ac5754f6d2c1b00097b15a40b928845e45a59..e7da5a622e861e1e6a1938f91a0dd9a39942a050 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -249,31 +249,18 @@ regcache_get_ptid (const struct regcache *regcache) return regcache->ptid (); } -struct regcache * -regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace) -{ - return new regcache (gdbarch, aspace); -} - -void -regcache_xfree (struct regcache *regcache) -{ - if (regcache == NULL) - return; - - delete regcache; -} - static void -do_regcache_xfree (void *data) +do_regcache_delete (void *data) { - regcache_xfree ((struct regcache *) data); + if (data == NULL) + return; + delete (regcache *) data; } struct cleanup * -make_cleanup_regcache_xfree (struct regcache *regcache) +make_cleanup_regcache_delete (struct regcache *regcache) { - return make_cleanup (do_regcache_xfree, regcache); + return make_cleanup (do_regcache_delete, regcache); } /* Cleanup routines for invalidating a register. */ diff --git a/gdb/spu-tdep.c b/gdb/spu-tdep.c index ca1a3fc99f99a506446925a20bd3598631930a53..1ac763da7ede9d4b9f4bff1e8d71551b0c776fa1 100644 --- a/gdb/spu-tdep.c +++ b/gdb/spu-tdep.c @@ -1288,7 +1288,7 @@ static void spu2ppu_dealloc_cache (struct frame_info *self, void *this_cache) { struct spu2ppu_cache *cache = (struct spu2ppu_cache *) this_cache; - regcache_xfree (cache->regcache); + delete cache->regcache; } static const struct frame_unwind spu2ppu_unwind = {