Re: [PATCH 02/26] gdbserver: convert new_register_cache into a regcache constructor

Simon Marchi
2023-12-21T20:19:25Z

On 2/28/23 06:28, Tankut Baris Aktemur via Gdb-patches wrote:
> Convert the `new_register_cache` function into a constructor of the
> regcache struct. Also define a default ctor because it is used in
> other places, in particular, tracepoint.cc.
> ---
> gdbserver/regcache.cc | 11 +++--------
> gdbserver/regcache.h | 8 ++++----
> gdbserver/server.cc | 2 +-
> 3 files changed, 8 insertions(+), 13 deletions(-)
>
> diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
> index 7987c406ab4..1410dd96551 100644
> --- a/gdbserver/regcache.cc
> +++ b/gdbserver/regcache.cc
> @@ -43,7 +43,7 @@ get_thread_regcache (struct thread_info *thread, int fetch)
>
> gdb_assert (proc->tdesc != NULL);
>
> - regcache = new_register_cache (proc->tdesc);
> + regcache = new struct regcache (proc->tdesc);
> set_thread_regcache_data (thread, regcache);
> }
>
> @@ -151,15 +151,10 @@ regcache::initialize (const target_desc *tdesc,
>
> #ifndef IN_PROCESS_AGENT
>
> -struct regcache *
> -new_register_cache (const struct target_desc *tdesc)
> +regcache::regcache (const target_desc *tdesc)
> {
> - struct regcache *regcache = new struct regcache;
> -
> gdb_assert (tdesc->registers_size != 0);
> - regcache->initialize (tdesc, nullptr);
> -
> - return regcache;
> + initialize (tdesc, nullptr);
> }
>
> void
> diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
> index 15b7e2b4dff..0002726ed00 100644
> --- a/gdbserver/regcache.h
> +++ b/gdbserver/regcache.h
> @@ -44,6 +44,10 @@ struct regcache : public reg_buffer_common
> #ifndef IN_PROCESS_AGENT
> /* One of REG_UNAVAILABLE or REG_VALID. */
> unsigned char *register_status = nullptr;
> +
> + /* Constructors. */
> + regcache () = default;
> + regcache (const target_desc *tdesc);

Ok, so with this patch I understood better the two modes regcache can
operate, it can allocate its own memory or use a buffer passed to the
initialize method. I think my previous suggestion still stands. You
could have:

regcache (const target_desc *tdesc, unsigned char *regbuf);
regcache (const target_desc *tdesc);

... former using the passed-in buffer and the latter allocating its own
buffer. I don't think we would need a default constructor then (and we
should use DISALLOW_COPY_AND_ASSIGN).

Simon