From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id BC9493857374; Sun, 12 Jun 2022 16:55:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC9493857374 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Use inheritance for addrmap X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 9dad432e10a94f63fc6b17d729c9cf8726e03dab X-Git-Newrev: a692aa3f1dc1106edaa0b93b23bd1179a8833548 Message-Id: <20220612165525.BC9493857374@sourceware.org> Date: Sun, 12 Jun 2022 16:55:25 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Jun 2022 16:55:25 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Da692aa3f1dc1= 106edaa0b93b23bd1179a8833548 commit a692aa3f1dc1106edaa0b93b23bd1179a8833548 Author: Tom Tromey Date: Sat Apr 16 08:54:32 2022 -0600 Use inheritance for addrmap =20 This is a simply C++-ification of the basics of addrmap: it uses virtual methods rather than a table of function pointers, and it changes the concrete implementations to be subclasses. Diff: --- gdb/addrmap.c | 191 ++++++++++++++++++++++++------------------------------= ---- 1 file changed, 80 insertions(+), 111 deletions(-) diff --git a/gdb/addrmap.c b/gdb/addrmap.c index 8141337e484..3f3629f3bb2 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -29,26 +29,18 @@ gdb_static_assert (sizeof (splay_tree_key) >=3D sizeof = (CORE_ADDR *)); gdb_static_assert (sizeof (splay_tree_value) >=3D sizeof (void *)); =20 =0C -/* The "abstract class". */ - -/* Functions implementing the addrmap functions for a particular - implementation. */ -struct addrmap_funcs -{ - void (*set_empty) (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj); - void *(*find) (const addrmap *self, CORE_ADDR addr); - struct addrmap *(*create_fixed) (struct addrmap *self, - struct obstack *obstack); - void (*relocate) (struct addrmap *self, CORE_ADDR offset); - int (*foreach) (struct addrmap *self, addrmap_foreach_fn fn); -}; - +/* The base class for addrmaps. */ =20 -struct addrmap +struct addrmap : public allocate_on_obstack { - const struct addrmap_funcs *funcs; + virtual ~addrmap () =3D default; + + virtual void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) =3D 0; + virtual void *find (CORE_ADDR addr) const =3D 0; + virtual struct addrmap *create_fixed (struct obstack *obstack) =3D 0; + virtual void relocate (CORE_ADDR offset) =3D 0; + virtual int foreach (addrmap_foreach_fn fn) =3D 0; }; =20 =20 @@ -57,21 +49,21 @@ addrmap_set_empty (struct addrmap *map, CORE_ADDR start, CORE_ADDR end_inclusive, void *obj) { - map->funcs->set_empty (map, start, end_inclusive, obj); + map->set_empty (start, end_inclusive, obj); } =20 =20 void * addrmap_find (const addrmap *map, CORE_ADDR addr) { - return map->funcs->find (map, addr); + return map->find (addr); } =20 =20 struct addrmap * addrmap_create_fixed (struct addrmap *original, struct obstack *obstack) { - return original->funcs->create_fixed (original, obstack); + return original->create_fixed (obstack); } =20 =20 @@ -80,14 +72,14 @@ addrmap_create_fixed (struct addrmap *original, struct = obstack *obstack) void addrmap_relocate (struct addrmap *map, CORE_ADDR offset) { - map->funcs->relocate (map, offset); + map->relocate (offset); } =20 =20 int addrmap_foreach (struct addrmap *map, addrmap_foreach_fn fn) { - return map->funcs->foreach (map, fn); + return map->foreach (fn); } =0C /* Fixed address maps. */ @@ -102,9 +94,14 @@ struct addrmap_transition }; =20 =20 -struct addrmap_fixed +struct addrmap_fixed : public addrmap { - struct addrmap addrmap; + void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) override; + void *find (CORE_ADDR addr) const override; + struct addrmap *create_fixed (struct obstack *obstack) override; + void relocate (CORE_ADDR offset) override; + int foreach (addrmap_foreach_fn fn) override; =20 /* The number of transitions in TRANSITIONS. */ size_t num_transitions; @@ -114,14 +111,13 @@ struct addrmap_fixed ADDR - 1 is mapped to something different, we have an entry here containing ADDR and VALUE. (Note that this means we always have an entry for address 0). */ - struct addrmap_transition transitions[1]; + struct addrmap_transition *transitions; }; =20 =20 -static void -addrmap_fixed_set_empty (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj) +void +addrmap_fixed::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) { internal_error (__FILE__, __LINE__, "addrmap_fixed_set_empty: " @@ -129,12 +125,11 @@ addrmap_fixed_set_empty (struct addrmap *self, } =20 =20 -static void * -addrmap_fixed_find (const addrmap *self, CORE_ADDR addr) +void * +addrmap_fixed::find (CORE_ADDR addr) const { - const addrmap_fixed *map =3D (const addrmap_fixed *) self; - const addrmap_transition *bottom =3D &map->transitions[0]; - const addrmap_transition *top =3D &map->transitions[map->num_transitions= - 1]; + const struct addrmap_transition *bottom =3D &transitions[0]; + const struct addrmap_transition *top =3D &transitions[num_transitions - = 1]; =20 while (bottom < top) { @@ -162,8 +157,8 @@ addrmap_fixed_find (const addrmap *self, CORE_ADDR addr) } =20 =20 -static struct addrmap * -addrmap_fixed_create_fixed (struct addrmap *self, struct obstack *obstack) +struct addrmap * +addrmap_fixed::create_fixed (struct obstack *obstack) { internal_error (__FILE__, __LINE__, _("addrmap_create_fixed is not implemented yet " @@ -171,26 +166,24 @@ addrmap_fixed_create_fixed (struct addrmap *self, str= uct obstack *obstack) } =20 =20 -static void -addrmap_fixed_relocate (struct addrmap *self, CORE_ADDR offset) +void +addrmap_fixed::relocate (CORE_ADDR offset) { - struct addrmap_fixed *map =3D (struct addrmap_fixed *) self; size_t i; =20 - for (i =3D 0; i < map->num_transitions; i++) - map->transitions[i].addr +=3D offset; + for (i =3D 0; i < num_transitions; i++) + transitions[i].addr +=3D offset; } =20 =20 -static int -addrmap_fixed_foreach (struct addrmap *self, addrmap_foreach_fn fn) +int +addrmap_fixed::foreach (addrmap_foreach_fn fn) { - struct addrmap_fixed *map =3D (struct addrmap_fixed *) self; size_t i; =20 - for (i =3D 0; i < map->num_transitions; i++) + for (i =3D 0; i < num_transitions; i++) { - int res =3D fn (map->transitions[i].addr, map->transitions[i].value); + int res =3D fn (transitions[i].addr, transitions[i].value); =20 if (res !=3D 0) return res; @@ -200,22 +193,17 @@ addrmap_fixed_foreach (struct addrmap *self, addrmap_= foreach_fn fn) } =20 =20 -static const struct addrmap_funcs addrmap_fixed_funcs =3D -{ - addrmap_fixed_set_empty, - addrmap_fixed_find, - addrmap_fixed_create_fixed, - addrmap_fixed_relocate, - addrmap_fixed_foreach -}; - - =0C /* Mutable address maps. */ =20 -struct addrmap_mutable +struct addrmap_mutable : public addrmap { - struct addrmap addrmap; + void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) override; + void *find (CORE_ADDR addr) const override; + struct addrmap *create_fixed (struct obstack *obstack) override; + void relocate (CORE_ADDR offset) override; + int foreach (addrmap_foreach_fn fn) override; =20 /* The obstack to use for allocations for this map. */ struct obstack *obstack; @@ -258,14 +246,15 @@ allocate_key (struct addrmap_mutable *map, CORE_ADDR = addr) =20 /* Type-correct wrappers for splay tree access. */ static splay_tree_node -addrmap_splay_tree_lookup (struct addrmap_mutable *map, CORE_ADDR addr) +addrmap_splay_tree_lookup (const struct addrmap_mutable *map, CORE_ADDR ad= dr) { return splay_tree_lookup (map->tree, (splay_tree_key) &addr); } =20 =20 static splay_tree_node -addrmap_splay_tree_predecessor (struct addrmap_mutable *map, CORE_ADDR add= r) +addrmap_splay_tree_predecessor (const struct addrmap_mutable *map, + CORE_ADDR addr) { return splay_tree_predecessor (map->tree, (splay_tree_key) &addr); } @@ -334,12 +323,10 @@ force_transition (struct addrmap_mutable *self, CORE_= ADDR addr) } =20 =20 -static void -addrmap_mutable_set_empty (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj) +void +addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) { - struct addrmap_mutable *map =3D (struct addrmap_mutable *) self; splay_tree_node n, next; void *prior_value; =20 @@ -356,14 +343,14 @@ addrmap_mutable_set_empty (struct addrmap *self, - Second pass: remove any unnecessary transitions. */ =20 /* Establish transitions at the start and end. */ - force_transition (map, start); + force_transition (this, start); if (end_inclusive < CORE_ADDR_MAX) - force_transition (map, end_inclusive + 1); + force_transition (this, end_inclusive + 1); =20 /* Walk the area, changing all NULL regions to OBJ. */ - for (n =3D addrmap_splay_tree_lookup (map, start), gdb_assert (n); + for (n =3D addrmap_splay_tree_lookup (this, start), gdb_assert (n); n && addrmap_node_key (n) <=3D end_inclusive; - n =3D addrmap_splay_tree_successor (map, addrmap_node_key (n))) + n =3D addrmap_splay_tree_successor (this, addrmap_node_key (n))) { if (! addrmap_node_value (n)) addrmap_node_set_value (n, obj); @@ -372,34 +359,33 @@ addrmap_mutable_set_empty (struct addrmap *self, /* Walk the area again, removing transitions from any value to itself. Be sure to visit both the transitions we forced above. */ - n =3D addrmap_splay_tree_predecessor (map, start); + n =3D addrmap_splay_tree_predecessor (this, start); prior_value =3D n ? addrmap_node_value (n) : NULL; - for (n =3D addrmap_splay_tree_lookup (map, start), gdb_assert (n); + for (n =3D addrmap_splay_tree_lookup (this, start), gdb_assert (n); n && (end_inclusive =3D=3D CORE_ADDR_MAX || addrmap_node_key (n) <=3D end_inclusive + 1); n =3D next) { - next =3D addrmap_splay_tree_successor (map, addrmap_node_key (n)); + next =3D addrmap_splay_tree_successor (this, addrmap_node_key (n)); if (addrmap_node_value (n) =3D=3D prior_value) - addrmap_splay_tree_remove (map, addrmap_node_key (n)); + addrmap_splay_tree_remove (this, addrmap_node_key (n)); else prior_value =3D addrmap_node_value (n); } } =20 =20 -static void * -addrmap_mutable_find (const addrmap *self, CORE_ADDR addr) +void * +addrmap_mutable::find (CORE_ADDR addr) const { - struct addrmap_mutable *map =3D (struct addrmap_mutable *) self; - splay_tree_node n =3D addrmap_splay_tree_lookup (map, addr); + splay_tree_node n =3D addrmap_splay_tree_lookup (this, addr); if (n !=3D nullptr) { gdb_assert (addrmap_node_key (n) =3D=3D addr); return addrmap_node_value (n); } =20 - n =3D addrmap_splay_tree_predecessor (map, addr); + n =3D addrmap_splay_tree_predecessor (this, addr); if (n !=3D nullptr) { gdb_assert (addrmap_node_key (n) < addr); @@ -438,43 +424,40 @@ splay_foreach_copy (splay_tree_node n, void *closure) } =20 =20 -static struct addrmap * -addrmap_mutable_create_fixed (struct addrmap *self, struct obstack *obstac= k) +struct addrmap * +addrmap_mutable::create_fixed (struct obstack *obstack) { - struct addrmap_mutable *mutable_obj =3D (struct addrmap_mutable *) self; struct addrmap_fixed *fixed; size_t num_transitions; - size_t alloc_len; =20 /* Count the number of transitions in the tree. */ num_transitions =3D 0; - splay_tree_foreach (mutable_obj->tree, splay_foreach_count, &num_transit= ions); + splay_tree_foreach (tree, splay_foreach_count, &num_transitions); =20 /* Include an extra entry for the transition at zero (which fixed maps have, but mutable maps do not.) */ num_transitions++; =20 - alloc_len =3D sizeof (*fixed) - + (num_transitions * sizeof (fixed->transitions[0])); - fixed =3D (struct addrmap_fixed *) obstack_alloc (obstack, alloc_len); - fixed->addrmap.funcs =3D &addrmap_fixed_funcs; + fixed =3D new (obstack) struct addrmap_fixed; fixed->num_transitions =3D 1; + fixed->transitions =3D XOBNEWVEC (obstack, struct addrmap_transition, + num_transitions); fixed->transitions[0].addr =3D 0; fixed->transitions[0].value =3D NULL; =20 /* Copy all entries from the splay tree to the array, in order=20 of increasing address. */ - splay_tree_foreach (mutable_obj->tree, splay_foreach_copy, fixed); + splay_tree_foreach (tree, splay_foreach_copy, fixed); =20 /* We should have filled the array. */ gdb_assert (fixed->num_transitions =3D=3D num_transitions); =20 - return (struct addrmap *) fixed; + return fixed; } =20 =20 -static void -addrmap_mutable_relocate (struct addrmap *self, CORE_ADDR offset) +void +addrmap_mutable::relocate (CORE_ADDR offset) { /* Not needed yet. */ internal_error (__FILE__, __LINE__, @@ -494,26 +477,13 @@ addrmap_mutable_foreach_worker (splay_tree_node node,= void *data) } =20 =20 -static int -addrmap_mutable_foreach (struct addrmap *self, addrmap_foreach_fn fn) +int +addrmap_mutable::foreach (addrmap_foreach_fn fn) { - struct addrmap_mutable *mutable_obj =3D (struct addrmap_mutable *) self; - - return splay_tree_foreach (mutable_obj->tree, addrmap_mutable_foreach_wo= rker, - &fn); + return splay_tree_foreach (tree, addrmap_mutable_foreach_worker, &fn); } =20 =20 -static const struct addrmap_funcs addrmap_mutable_funcs =3D -{ - addrmap_mutable_set_empty, - addrmap_mutable_find, - addrmap_mutable_create_fixed, - addrmap_mutable_relocate, - addrmap_mutable_foreach -}; - - static void * splay_obstack_alloc (int size, void *closure) { @@ -570,9 +540,8 @@ splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_t= ree_key bk) struct addrmap * addrmap_create_mutable (struct obstack *obstack) { - struct addrmap_mutable *map =3D XOBNEW (obstack, struct addrmap_mutable); + struct addrmap_mutable *map =3D new (obstack) struct addrmap_mutable; =20 - map->addrmap.funcs =3D &addrmap_mutable_funcs; map->obstack =3D obstack; =20 /* splay_tree_new_with_allocator uses the provided allocation @@ -587,7 +556,7 @@ addrmap_create_mutable (struct obstack *obstack) splay_obstack_free, map); =20 - return (struct addrmap *) map; + return map; } =20 /* See addrmap.h. */