From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 5/6] Change registry to use less memory
Date: Mon, 30 May 2022 11:59:33 -0600 [thread overview]
Message-ID: <20220530175934.3872892-6-tom@tromey.com> (raw)
In-Reply-To: <20220530175934.3872892-1-tom@tromey.com>
The registry code creates "registry_data" objects that hold the free
function and the index; then the registry keys refer to this object.
However, only the index is really useful, and now that registries have
a private implementation, just the index can be stored and we can
reduce the memory use of registries a little bit. This also
simplifies the code somewhat.
---
gdb/registry.h | 47 ++++++++++++++++++++---------------------------
1 file changed, 20 insertions(+), 27 deletions(-)
diff --git a/gdb/registry.h b/gdb/registry.h
index 27592ff6e2a..f2d952ac847 100644
--- a/gdb/registry.h
+++ b/gdb/registry.h
@@ -159,7 +159,7 @@ class registry
}
/* The underlying key. */
- const typename registry<T>::registry_data *m_key;
+ const unsigned m_key;
};
/* Clear all the data associated with this container. This is
@@ -167,13 +167,16 @@ class registry
void clear_registry ()
{
/* Call all the free functions. */
- for (const auto &datum : get_registrations ())
+ std::vector<registry_data_callback> ®istrations
+ = get_registrations ();
+ unsigned last = registrations.size ();
+ for (unsigned i = 0; i < last; ++i)
{
- void *elt = m_fields[datum->index];
+ void *elt = m_fields[i];
if (elt != nullptr)
{
- datum->free (elt);
- m_fields[datum->index] = nullptr;
+ registrations[i] (elt);
+ m_fields[i] = nullptr;
}
}
}
@@ -183,50 +186,40 @@ class registry
/* Registry callbacks have this type. */
typedef void (*registry_data_callback) (void *);
- /* The type of a key. */
- struct registry_data
- {
- unsigned index;
- registry_data_callback free;
- };
-
/* Get a new key for this particular registry. FREE is a callback.
When the container object is destroyed, all FREE functions are
called. The data associated with the container object is passed
to the callback. */
- static const registry_data *new_key (registry_data_callback free)
+ static unsigned new_key (registry_data_callback free)
{
- std::unique_ptr<registry_data> result (new registry_data);
- std::vector<std::unique_ptr<registry_data>> ®istrations
+ std::vector<registry_data_callback> ®istrations
= get_registrations ();
- result->index = registrations.size ();
- result->free = free;
- registrations.emplace_back (std::move (result));
- return registrations.back ().get ();
+ unsigned result = registrations.size ();
+ registrations.push_back (free);
+ return result;
}
/* Set the datum associated with KEY in this container. */
- void set (const registry_data *key, void *datum)
+ void set (unsigned key, void *datum)
{
- m_fields[key->index] = datum;
+ m_fields[key] = datum;
}
/* Fetch the datum associated with KEY in this container. If 'set'
has not been called for this key, nullptr is returned. */
- void *get (const registry_data *key)
+ void *get (unsigned key)
{
- return m_fields[key->index];
+ return m_fields[key];
}
/* The data stored in this instance. */
std::vector<void *> m_fields;
/* Return a reference to the vector of all the registrations that
- have been made. We do separate allocations here so that the
- addresses are stable and can be used as keys. */
- static std::vector<std::unique_ptr<registry_data>> &get_registrations ()
+ have been made. */
+ static std::vector<registry_data_callback> &get_registrations ()
{
- static std::vector<std::unique_ptr<registry_data>> registrations;
+ static std::vector<registry_data_callback> registrations;
return registrations;
}
};
--
2.34.1
next prev parent reply other threads:[~2022-05-30 18:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-30 17:59 [PATCH 0/6] Rewrite registry.h Tom Tromey
2022-05-30 17:59 ` [PATCH 1/6] Change address_space to use new and delete Tom Tromey
2022-05-30 17:59 ` [PATCH 2/6] Change allocation of type-copying hash table Tom Tromey
2022-05-30 17:59 ` [PATCH 3/6] Remove some unused functions from guile code Tom Tromey
2022-05-30 17:59 ` [PATCH 4/6] Rewrite registry.h Tom Tromey
2022-06-07 10:55 ` Lancelot SIX via Gdb-patches
2022-06-08 1:33 ` Tom Tromey
2022-06-08 18:51 ` Tom Tromey
2022-05-30 17:59 ` Tom Tromey [this message]
2022-05-30 17:59 ` [PATCH 6/6] Remove some unneeded checks in Guile code Tom Tromey
2022-07-28 20:13 ` [PATCH 0/6] Rewrite registry.h Tom Tromey
2022-07-29 2:33 ` Simon Marchi via Gdb-patches
2022-08-02 2:46 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220530175934.3872892-6-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox