diff options
author | Simon Marchi <simon.marchi@polymtl.ca> | 2023-01-27 14:46:50 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2023-01-30 10:22:42 -0500 |
commit | 5867ab870b8aa36ae490ec6e4e8e4c55be11ccf1 (patch) | |
tree | c5049320fa15c17055ef6bf20935118170545080 /gdb/addrmap.h | |
parent | 8d31d08fe61059fb94c02ae30c2b7cd0ea738df0 (diff) | |
download | binutils-gdb-5867ab870b8aa36ae490ec6e4e8e4c55be11ccf1.tar.gz |
gdb: provide const-correct versions of addrmap::find and addrmap::foreach
Users of addrmap::find and addrmap::foreach that have a const addrmap
should ideally receive const pointers to objects, to indicate they
should not be modified. However, users that have a non-const addrmap
should still receive a non-const pointer.
To achieve this, without adding more virtual methods, make the existing
find and foreach virtual methods private and prefix them with "do_".
Add small non-const and const wrappers for find and foreach.
Obviously, the const can be cast away, but if using static_cast
instead of C-style casts, then the compiler won't let you cast
the const away. I changed all the callers of addrmap::find and
addrmap::foreach I could find to make them use static_cast.
Change-Id: Ia8e69d022564f80d961413658fe6068edc71a094
Diffstat (limited to 'gdb/addrmap.h')
-rw-r--r-- | gdb/addrmap.h | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/gdb/addrmap.h b/gdb/addrmap.h index a5b784efaf2..e00dda6e711 100644 --- a/gdb/addrmap.h +++ b/gdb/addrmap.h @@ -36,8 +36,10 @@ /* The type of a function used to iterate over the map. OBJ is NULL for unmapped regions. */ -typedef gdb::function_view<int (CORE_ADDR start_addr, void *obj)> - addrmap_foreach_fn; +using addrmap_foreach_fn + = gdb::function_view<int (CORE_ADDR start_addr, void *obj)>; +using addrmap_foreach_const_fn + = gdb::function_view<int (CORE_ADDR start_addr, const void *obj)>; /* The base class for addrmaps. */ struct addrmap @@ -85,7 +87,11 @@ struct addrmap void *obj) = 0; /* Return the object associated with ADDR in MAP. */ - virtual void *find (CORE_ADDR addr) const = 0; + const void *find (CORE_ADDR addr) const + { return this->do_find (addr); } + + void *find (CORE_ADDR addr) + { return this->do_find (addr); } /* Relocate all the addresses in MAP by OFFSET. (This can be applied to either mutable or immutable maps.) */ @@ -95,7 +101,19 @@ struct addrmap If FN ever returns a non-zero value, the iteration ceases immediately, and the value is returned. Otherwise, this function returns 0. */ - virtual int foreach (addrmap_foreach_fn fn) = 0; + int foreach (addrmap_foreach_const_fn fn) const + { return this->do_foreach (fn); } + + int foreach (addrmap_foreach_fn fn) + { return this->do_foreach (fn); } + + +private: + /* Worker for find, implemented by sub-classes. */ + virtual void *do_find (CORE_ADDR addr) const = 0; + + /* Worker for foreach, implemented by sub-classes. */ + virtual int do_foreach (addrmap_foreach_fn fn) const = 0; }; struct addrmap_mutable; @@ -111,11 +129,11 @@ public: void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, void *obj) override; - void *find (CORE_ADDR addr) const override; void relocate (CORE_ADDR offset) override; - int foreach (addrmap_foreach_fn fn) override; private: + void *do_find (CORE_ADDR addr) const override; + int do_foreach (addrmap_foreach_fn fn) const override; /* A transition: a point in an address map where the value changes. The map maps ADDR to VALUE, but if ADDR > 0, it maps ADDR-1 to @@ -149,11 +167,11 @@ public: void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, void *obj) override; - void *find (CORE_ADDR addr) const override; void relocate (CORE_ADDR offset) override; - int foreach (addrmap_foreach_fn fn) override; private: + void *do_find (CORE_ADDR addr) const override; + int do_foreach (addrmap_foreach_fn fn) const override; /* A splay tree, with a node for each transition; there is a transition at address T if T-1 and T map to different objects. |