diff options
author | Tom Tromey <tromey@adacore.com> | 2024-02-26 10:19:07 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2024-03-21 12:21:24 -0600 |
commit | 7f032bbedf3e66f6695d4df0d149c2e8033224da (patch) | |
tree | 3e29a70d55d9e9952f81d6f91411e2cee13ac028 /gdbsupport/gdb_obstack.h | |
parent | 9069d69398649cd9a54e4dcac8548b10ece8a304 (diff) | |
download | binutils-7f032bbedf3e66f6695d4df0d149c2e8033224da.zip binutils-7f032bbedf3e66f6695d4df0d149c2e8033224da.tar.gz binutils-7f032bbedf3e66f6695d4df0d149c2e8033224da.tar.bz2 |
Require trivial destructor in allocate_on_obstack
This patch makes allocate_on_obstack a little bit safer, by enforcing
the rule that objects allocated on an obstack must have a trivial
destructor.
The static assert is done in a method -- doing it inside the class
itself won't work because the class is incomplete at that point.
Diffstat (limited to 'gdbsupport/gdb_obstack.h')
-rw-r--r-- | gdbsupport/gdb_obstack.h | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/gdbsupport/gdb_obstack.h b/gdbsupport/gdb_obstack.h index 20f8886..7b3bb05 100644 --- a/gdbsupport/gdb_obstack.h +++ b/gdbsupport/gdb_obstack.h @@ -133,14 +133,18 @@ struct auto_obstack : obstack { obstack_free (this, obstack_base (this)); } }; -/* Objects are allocated on obstack instead of heap. */ +/* Objects are allocated on obstack instead of heap. This is a mixin + that uses CRTP to ensure that the type in question is trivially + destructible. */ +template<typename T> struct allocate_on_obstack { allocate_on_obstack () = default; void* operator new (size_t size, struct obstack *obstack) { + static_assert (IsFreeable<T>::value); return obstack_alloc (obstack, size); } |