From c4dfafabc575f4995a5aa18241adc275e63c846c Mon Sep 17 00:00:00 2001 From: Sergio Durigan Junior Date: Sun, 10 Sep 2017 12:17:18 -0400 Subject: [PATCH] Use std::vector on tdesc->reg_defs (gdbserver/tdesc.h) This is a followup patch to the build breakage fix on AArch64. While doing the fix, I found it better to convert tdesc->reg_defs (on gdbserver/tdesc.h) from using VEC to using std::vector. This makes the code easier to read and maintain, and also is one more step towards the C++fication. Regtested on BuildBot. 2017-09-16 Sergio Durigan Junior * regcache.c (get_thread_regcache): Update code to use "std::vector" instead of "VEC" for "target_desc.reg_defs". (regcache_cpy): Likewise. (registers_to_string): Likewise. (registers_from_string): Likewise. (find_regno): Likewise. (supply_regblock): Likewise. (regcache_raw_read_unsigned): Likewise. * tdesc.c (init_target_desc): Likewise. (tdesc_create_reg): Likewise. * tdesc.h: Remove declaration of "tdesc_reg_p". Include . (struct target_desc) : Convert to "std::vector". (target_desc): Do not initialize "reg_defs". (~target_desc): Update code to use "std::vector" instead of "VEC" for "target_desc.reg_defs". (operator==): Likewise. --- gdb/gdbserver/ChangeLog | 19 +++++++++++++++++++ gdb/gdbserver/regcache.c | 39 +++++++++++++++++++-------------------- gdb/gdbserver/tdesc.c | 14 ++++++-------- gdb/gdbserver/tdesc.h | 25 ++++++++----------------- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog index 4cfa2acee8c..50b656ee135 100644 --- a/gdb/gdbserver/ChangeLog +++ b/gdb/gdbserver/ChangeLog @@ -1,3 +1,22 @@ +2017-09-16 Sergio Durigan Junior + + * regcache.c (get_thread_regcache): Update code to use "std::vector" + instead of "VEC" for "target_desc.reg_defs". + (regcache_cpy): Likewise. + (registers_to_string): Likewise. + (registers_from_string): Likewise. + (find_regno): Likewise. + (supply_regblock): Likewise. + (regcache_raw_read_unsigned): Likewise. + * tdesc.c (init_target_desc): Likewise. + (tdesc_create_reg): Likewise. + * tdesc.h: Remove declaration of "tdesc_reg_p". Include . + (struct target_desc) : Convert to "std::vector". + (target_desc): Do not initialize "reg_defs". + (~target_desc): Update code to use "std::vector" instead of "VEC" + for "target_desc.reg_defs". + (operator==): Likewise. + 2017-09-15 Simon Marchi * inferiors.h (thread_to_gdb_id): Remove. diff --git a/gdb/gdbserver/regcache.c b/gdb/gdbserver/regcache.c index d1a534cc52a..75480912fb4 100644 --- a/gdb/gdbserver/regcache.c +++ b/gdb/gdbserver/regcache.c @@ -53,9 +53,8 @@ get_thread_regcache (struct thread_info *thread, int fetch) current_thread = thread; /* Invalidate all registers, to prevent stale left-overs. */ - if (!VEC_empty (tdesc_reg_p, regcache->tdesc->reg_defs)) - memset (regcache->register_status, REG_UNAVAILABLE, - VEC_length (tdesc_reg_p, regcache->tdesc->reg_defs)); + memset (regcache->register_status, REG_UNAVAILABLE, + regcache->tdesc->reg_defs.size ()); fetch_inferior_registers (regcache, -1); current_thread = saved_thread; regcache->registers_valid = 1; @@ -146,10 +145,9 @@ init_register_cache (struct regcache *regcache, = (unsigned char *) xcalloc (1, tdesc->registers_size); regcache->registers_owned = 1; regcache->register_status - = (unsigned char *) xmalloc (VEC_length (tdesc_reg_p, tdesc->reg_defs)); - if (!VEC_empty (tdesc_reg_p, tdesc->reg_defs)) - memset ((void *) regcache->register_status, REG_UNAVAILABLE, - VEC_length (tdesc_reg_p, tdesc->reg_defs)); + = (unsigned char *) xmalloc (tdesc->reg_defs.size ()); + memset ((void *) regcache->register_status, REG_UNAVAILABLE, + tdesc->reg_defs.size ()); #else gdb_assert_not_reached ("can't allocate memory from the heap"); #endif @@ -206,7 +204,7 @@ regcache_cpy (struct regcache *dst, struct regcache *src) #ifndef IN_PROCESS_AGENT if (dst->register_status != NULL && src->register_status != NULL) memcpy (dst->register_status, src->register_status, - VEC_length (tdesc_reg_p, src->tdesc->reg_defs)); + src->tdesc->reg_defs.size ()); #endif dst->registers_valid = src->registers_valid; } @@ -219,11 +217,11 @@ registers_to_string (struct regcache *regcache, char *buf) { unsigned char *registers = regcache->registers; const struct target_desc *tdesc = regcache->tdesc; - int i; - reg *reg; - for (i = 0; VEC_iterate (tdesc_reg_p, tdesc->reg_defs, i, reg); i++) + for (int i = 0; i < tdesc->reg_defs.size (); ++i) { + struct reg *reg = tdesc->reg_defs[i]; + if (regcache->register_status[i] == REG_VALID) { bin2hex (registers, buf, register_size (tdesc, i)); @@ -259,12 +257,13 @@ registers_from_string (struct regcache *regcache, char *buf) int find_regno (const struct target_desc *tdesc, const char *name) { - int i; - reg *reg; + for (int i = 0; i < tdesc->reg_defs.size (); ++i) + { + struct reg *reg = tdesc->reg_defs[i]; - for (i = 0; VEC_iterate (tdesc_reg_p, tdesc->reg_defs, i, reg); i++) - if (strcmp (name, reg->name) == 0) - return i; + if (strcmp (name, reg->name) == 0) + return i; + } internal_error (__FILE__, __LINE__, "Unknown register %s requested", name); } @@ -274,7 +273,7 @@ find_regno (const struct target_desc *tdesc, const char *name) struct reg * find_register_by_number (const struct target_desc *tdesc, int n) { - return VEC_index (tdesc_reg_p, tdesc->reg_defs, n); + return tdesc->reg_defs[n]; } #ifndef IN_PROCESS_AGENT @@ -390,7 +389,7 @@ supply_regblock (struct regcache *regcache, const void *buf) { int i; - for (i = 0; i < VEC_length (tdesc_reg_p, tdesc->reg_defs); i++) + for (i = 0; i < tdesc->reg_defs.size (); i++) regcache->register_status[i] = REG_VALID; } #endif @@ -404,7 +403,7 @@ supply_regblock (struct regcache *regcache, const void *buf) { int i; - for (i = 0; i < VEC_length (tdesc_reg_p, tdesc->reg_defs); i++) + for (i = 0; i < tdesc->reg_defs.size (); i++) regcache->register_status[i] = REG_UNAVAILABLE; } #endif @@ -437,7 +436,7 @@ regcache_raw_read_unsigned (struct regcache *regcache, int regnum, gdb_assert (regcache != NULL); gdb_assert (regnum >= 0 - && regnum < VEC_length (tdesc_reg_p, regcache->tdesc->reg_defs)); + && regnum < regcache->tdesc->reg_defs.size ()); size = register_size (regcache->tdesc, regnum); diff --git a/gdb/gdbserver/tdesc.c b/gdb/gdbserver/tdesc.c index 53f36d55647..63d6467d56d 100644 --- a/gdb/gdbserver/tdesc.c +++ b/gdb/gdbserver/tdesc.c @@ -22,11 +22,9 @@ void init_target_desc (struct target_desc *tdesc) { - int offset, i; - struct reg *reg; + int offset = 0; - offset = 0; - for (i = 0; VEC_iterate (tdesc_reg_p, tdesc->reg_defs, i, reg); i++) + for (reg *reg : tdesc->reg_defs) { reg->offset = offset; offset += reg->size; @@ -193,23 +191,23 @@ tdesc_create_reg (struct tdesc_feature *feature, const char *name, { struct target_desc *tdesc = (struct target_desc *) feature; - while (VEC_length (tdesc_reg_p, tdesc->reg_defs) < regnum) + while (tdesc->reg_defs.size () < regnum) { struct reg *reg = XCNEW (struct reg); reg->name = ""; reg->size = 0; - VEC_safe_push (tdesc_reg_p, tdesc->reg_defs, reg); + tdesc->reg_defs.push_back (reg); } gdb_assert (regnum == 0 - || regnum == VEC_length (tdesc_reg_p, tdesc->reg_defs)); + || regnum == tdesc->reg_defs.size ()); struct reg *reg = XCNEW (struct reg); reg->name = name; reg->size = bitsize; - VEC_safe_push (tdesc_reg_p, tdesc->reg_defs, reg); + tdesc->reg_defs.push_back (reg); } /* See arch/tdesc.h. */ diff --git a/gdb/gdbserver/tdesc.h b/gdb/gdbserver/tdesc.h index 71249e4eda0..24efcd17376 100644 --- a/gdb/gdbserver/tdesc.h +++ b/gdb/gdbserver/tdesc.h @@ -22,9 +22,7 @@ #include "arch/tdesc.h" #include "regdef.h" - -typedef struct reg *tdesc_reg_p; -DEF_VEC_P(tdesc_reg_p); +#include struct tdesc_feature {}; @@ -36,7 +34,7 @@ struct target_desc : tdesc_feature { /* A vector of elements of register definitions that describe the inferior's register set. */ - VEC(tdesc_reg_p) *reg_defs; + std::vector reg_defs; /* The register cache size, in bytes. */ int registers_size; @@ -66,17 +64,15 @@ struct target_desc : tdesc_feature public: target_desc () - : reg_defs (NULL), registers_size (0) + : registers_size (0) {} ~target_desc () { int i; - struct reg *reg; - for (i = 0; VEC_iterate (tdesc_reg_p, reg_defs, i, reg); i++) + for (reg *reg : reg_defs) xfree (reg); - VEC_free (tdesc_reg_p, reg_defs); xfree ((char *) arch); xfree ((char *) osabi); @@ -90,18 +86,13 @@ public: bool operator== (const target_desc &other) const { - if (VEC_length (tdesc_reg_p, reg_defs) - != VEC_length (tdesc_reg_p, other.reg_defs)) + if (reg_defs.size () != other.reg_defs.size ()) return false; - struct reg *reg; - - for (int ix = 0; - VEC_iterate (tdesc_reg_p, reg_defs, ix, reg); - ix++) + for (int i = 0; i < reg_defs.size (); ++i) { - struct reg *reg2 - = VEC_index (tdesc_reg_p, other.reg_defs, ix); + struct reg *reg = reg_defs[i]; + struct reg *reg2 = other.reg_defs[i]; if (reg != reg2 && *reg != *reg2) return false; -- 2.30.2