aboutsummaryrefslogtreecommitdiff
path: root/gdb/bcache.h
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2024-03-20 13:24:33 -0600
committerTom Tromey <tromey@adacore.com>2024-06-04 10:13:17 -0600
commit7149dfe819b16c0238fb67f55a47e9295c20ff1b (patch)
tree3d6e0285ec95cfd5ca444fb077deb6d17ac884ca /gdb/bcache.h
parent56fefe83f7e607842fa95f6bb7d71f1645ce6c15 (diff)
downloadfsf-binutils-gdb-7149dfe819b16c0238fb67f55a47e9295c20ff1b.zip
fsf-binutils-gdb-7149dfe819b16c0238fb67f55a47e9295c20ff1b.tar.gz
fsf-binutils-gdb-7149dfe819b16c0238fb67f55a47e9295c20ff1b.tar.bz2
Make bcache more type-safe
The bcache uses memcpy to make copies of the data passed to it. In C++, this is only safe for trivially-copyable types. This patch changes bcache to require this property, and slightly changes the API to make it easier to use when copying a single object. It also makes the new 'insert' template methods return the correct type.
Diffstat (limited to 'gdb/bcache.h')
-rw-r--r--gdb/bcache.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/gdb/bcache.h b/gdb/bcache.h
index 17a0988..a77cd00 100644
--- a/gdb/bcache.h
+++ b/gdb/bcache.h
@@ -152,7 +152,26 @@ struct bcache
were newly added to the cache, or to false if the bytes were
found in the cache. */
- const void *insert (const void *addr, int length, bool *added = nullptr);
+ template<typename T, typename = gdb::Requires<std::is_trivially_copyable<T>>>
+ const T *insert (const T *addr, int length, bool *added = nullptr)
+ {
+ return (const T *) this->insert ((const void *) addr, length, added);
+ }
+
+ /* Find a copy of OBJECT in this bcache. If BCACHE has never seen
+ those bytes before, add a copy of them to BCACHE. In either
+ case, return a pointer to BCACHE's copy of that string. Since
+ the cached value is meant to be read-only, return a const buffer.
+ If ADDED is not NULL, set *ADDED to true if the bytes were newly
+ added to the cache, or to false if the bytes were found in the
+ cache. */
+
+ template<typename T, typename = gdb::Requires<std::is_trivially_copyable<T>>>
+ const T *insert (const T &object, bool *added = nullptr)
+ {
+ return (const T *) this->insert ((const void *) &object, sizeof (object),
+ added);
+ }
/* Print statistics on this bcache's memory usage and efficacity at
eliminating duplication. TYPE should be a string describing the
@@ -173,6 +192,10 @@ protected:
private:
+ /* Implementation of the templated 'insert' methods. */
+
+ const void *insert (const void *addr, int length, bool *added);
+
/* All the bstrings are allocated here. */
struct obstack m_cache {};