From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 01/20] Introduce type_allocator
Date: Mon, 13 Mar 2023 16:08:01 -0600 [thread overview]
Message-ID: <20230313-split-objfile-type-allocator-2-v1-1-69ba773ac17b@tromey.com> (raw)
In-Reply-To: <20230313-split-objfile-type-allocator-2-v1-0-69ba773ac17b@tromey.com>
This introduces a new type_allocator class. This class will be used
to abstract out the placement of new types, so that type-creation code
can be simplified and shared.
---
gdb/gdbtypes.c | 118 ++++++++++++++++++++++++++++++++++++++++++++-------------
gdb/gdbtypes.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 189 insertions(+), 27 deletions(-)
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index cfd0a1b62c7..5bb49063d5a 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -173,6 +173,64 @@ show_strict_type_checking (struct ui_file *file, int from_tty,
}
\f
+/* Helper function to initialize a newly allocated type. Set type code
+ to CODE and initialize the type-specific fields accordingly. */
+
+static void
+set_type_code (struct type *type, enum type_code code)
+{
+ type->set_code (code);
+
+ switch (code)
+ {
+ case TYPE_CODE_STRUCT:
+ case TYPE_CODE_UNION:
+ case TYPE_CODE_NAMESPACE:
+ INIT_CPLUS_SPECIFIC (type);
+ break;
+ case TYPE_CODE_FLT:
+ TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
+ break;
+ case TYPE_CODE_FUNC:
+ INIT_FUNC_SPECIFIC (type);
+ break;
+ case TYPE_CODE_FIXED_POINT:
+ INIT_FIXED_POINT_SPECIFIC (type);
+ break;
+ }
+}
+
+/* See gdbtypes.h. */
+
+type *
+type_allocator::new_type ()
+{
+ if (m_smash)
+ return m_data.type;
+
+ obstack *obstack = (m_is_objfile
+ ? &m_data.objfile->objfile_obstack
+ : gdbarch_obstack (m_data.gdbarch));
+
+ /* Alloc the structure and start off with all fields zeroed. */
+ struct type *type = OBSTACK_ZALLOC (obstack, struct type);
+ TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (obstack, struct main_type);
+
+ if (m_is_objfile)
+ {
+ OBJSTAT (m_data.objfile, n_types++);
+ type->set_owner (m_data.objfile);
+ }
+ else
+ type->set_owner (m_data.gdbarch);
+
+ /* Initialize the fields that might not be zero. */
+ type->set_code (TYPE_CODE_UNDEF);
+ TYPE_CHAIN (type) = type; /* Chain back to itself. */
+
+ return type;
+}
+
/* Allocate a new OBJFILE-associated type structure and fill it
with some defaults. Space for the type structure is allocated
on the objfile's objfile_obstack. */
@@ -241,6 +299,39 @@ alloc_type_copy (const struct type *type)
/* See gdbtypes.h. */
+type *
+type_allocator::new_type (enum type_code code, int bit, const char *name)
+{
+ struct type *type = new_type ();
+ set_type_code (type, code);
+ gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
+ type->set_length (bit / TARGET_CHAR_BIT);
+
+ if (name != nullptr)
+ {
+ obstack *obstack = (m_is_objfile
+ ? &m_data.objfile->objfile_obstack
+ : gdbarch_obstack (m_data.gdbarch));
+ type->set_name (obstack_strdup (obstack, name));
+ }
+
+ return type;
+}
+
+/* See gdbtypes.h. */
+
+gdbarch *
+type_allocator::arch ()
+{
+ if (m_smash)
+ return m_data.type->arch ();
+ if (m_is_objfile)
+ return m_data.objfile->arch ();
+ return m_data.gdbarch;
+}
+
+/* See gdbtypes.h. */
+
gdbarch *
type::arch () const
{
@@ -3344,33 +3435,6 @@ allocate_gnat_aux_type (struct type *type)
*(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
}
-/* Helper function to initialize a newly allocated type. Set type code
- to CODE and initialize the type-specific fields accordingly. */
-
-static void
-set_type_code (struct type *type, enum type_code code)
-{
- type->set_code (code);
-
- switch (code)
- {
- case TYPE_CODE_STRUCT:
- case TYPE_CODE_UNION:
- case TYPE_CODE_NAMESPACE:
- INIT_CPLUS_SPECIFIC (type);
- break;
- case TYPE_CODE_FLT:
- TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
- break;
- case TYPE_CODE_FUNC:
- INIT_FUNC_SPECIFIC (type);
- break;
- case TYPE_CODE_FIXED_POINT:
- INIT_FIXED_POINT_SPECIFIC (type);
- break;
- }
-}
-
/* Helper function to verify floating-point format and size.
BIT is the type size in bits; if BIT equals -1, the size is
determined by the floatformat. Returns size to be used. */
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 52475d5afb4..613c22d676b 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2205,6 +2205,104 @@ extern struct type *get_target_type (struct type *type);
extern unsigned int type_length_units (struct type *type);
+/* An object of this type is passed when allocating certain types. It
+ determines where the new type is allocated. Ultimately a type is
+ either allocated on a on an objfile obstack or on a gdbarch
+ obstack. However, it's also possible to request that a new type be
+ allocated on the same obstack as some existing type, or that a
+ "new" type instead overwrite a supplied type object. */
+
+class type_allocator
+{
+public:
+
+ /* Create new types on OBJFILE. */
+ explicit type_allocator (objfile *objfile)
+ : m_is_objfile (true)
+ {
+ m_data.objfile = objfile;
+ }
+
+ /* Create new types on GDBARCH. */
+ explicit type_allocator (gdbarch *gdbarch)
+ {
+ m_data.gdbarch = gdbarch;
+ }
+
+ /* This determines whether a passed-in type should be rewritten in
+ place, or whether it should simply determine where the new type
+ is created. */
+ enum type_allocator_kind
+ {
+ /* Allocate on same obstack as existing type. */
+ SAME = 0,
+ /* Smash the existing type. */
+ SMASH = 1,
+ };
+
+ /* Create new types either on the same obstack as TYPE; or if SMASH
+ is passed, overwrite TYPE. */
+ explicit type_allocator (struct type *type,
+ type_allocator_kind kind = SAME)
+ {
+ if (kind == SAME)
+ {
+ if (type->is_objfile_owned ())
+ {
+ m_data.objfile = type->objfile_owner ();
+ m_is_objfile = true;
+ }
+ else
+ m_data.gdbarch = type->arch_owner ();
+ }
+ else
+ {
+ m_smash = true;
+ m_data.type = type;
+ }
+ }
+
+ /* Create new types on the same obstack as TYPE. */
+ explicit type_allocator (const struct type *type)
+ : m_is_objfile (type->is_objfile_owned ())
+ {
+ if (type->is_objfile_owned ())
+ m_data.objfile = type->objfile_owner ();
+ else
+ m_data.gdbarch = type->arch_owner ();
+ }
+
+ /* Create a new type on the desired obstack. Note that a "new" type
+ is not created if type-smashing was selected at construction. */
+ type *new_type ();
+
+ /* Create a new type on the desired obstack, and fill in its code,
+ length, and name. If NAME is non-null, it is copied to the
+ destination obstack first. Note that a "new" type is not created
+ if type-smashing was selected at construction. */
+ type *new_type (enum type_code code, int bit, const char *name);
+
+ /* Return the architecture associated with this allocator. This
+ comes from whatever object was supplied to the constructor. */
+ gdbarch *arch ();
+
+private:
+
+ /* Where the type should wind up. */
+ union
+ {
+ struct objfile *objfile;
+ struct gdbarch *gdbarch;
+ struct type *type;
+ } m_data {};
+
+ /* True if this allocator uses the objfile field above. */
+ bool m_is_objfile = false;
+ /* True if this allocator uses the type field above, indicating that
+ the "allocation" should be done in-place. */
+ bool m_smash = false;
+};
+
/* * Helper function to construct objfile-owned types. */
extern struct type *init_type (struct objfile *, enum type_code, int,
--
2.39.1
next prev parent reply other threads:[~2023-03-13 22:08 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-13 22:08 [PATCH 00/20] Remove objfile_type Tom Tromey
2023-03-13 22:08 ` Tom Tromey [this message]
2023-03-13 22:08 ` [PATCH 02/20] Remove alloc_type_arch Tom Tromey
2023-03-13 22:08 ` [PATCH 03/20] Remove alloc_type_copy Tom Tromey
2023-03-13 22:08 ` [PATCH 04/20] Remove alloc_type Tom Tromey
2023-03-13 22:08 ` [PATCH 05/20] Reuse existing builtin types Tom Tromey
2023-03-13 22:08 ` [PATCH 06/20] Remove arch_type Tom Tromey
2023-03-13 22:08 ` [PATCH 07/20] Remove init_type Tom Tromey
2023-03-13 22:08 ` [PATCH 08/20] Unify arch_integer_type and init_integer_type Tom Tromey
2023-03-13 22:08 ` [PATCH 09/20] Unify arch_character_type and init_character_type Tom Tromey
2023-03-13 22:08 ` [PATCH 10/20] Unify arch_boolean_type and init_boolean_type Tom Tromey
2023-03-13 22:08 ` [PATCH 11/20] Unify arch_float_type and init_float_type Tom Tromey
2023-03-13 22:08 ` [PATCH 12/20] Unify arch_decfloat_type and init_decfloat_type Tom Tromey
2023-03-13 22:08 ` [PATCH 13/20] Unify arch_pointer_type and init_pointer_type Tom Tromey
2023-03-13 22:08 ` [PATCH 14/20] Use type allocator for range types Tom Tromey
2023-03-14 14:41 ` Simon Marchi
2023-03-18 15:56 ` Tom Tromey
2023-03-13 22:08 ` [PATCH 15/20] Use type allocator for array types Tom Tromey
2023-03-13 22:08 ` [PATCH 16/20] Use type allocator for set types Tom Tromey
2023-03-13 22:08 ` [PATCH 17/20] Use builtin type when appropriate Tom Tromey
2023-03-13 22:08 ` [PATCH 18/20] Rename objfile_type to builtin_type Tom Tromey
2023-03-13 22:08 ` [PATCH 19/20] Add some types to struct builtin_type Tom Tromey
2023-03-13 22:08 ` [PATCH 20/20] Remove objfile_type Tom Tromey
2023-03-14 14:48 ` [PATCH 00/20] " Simon Marchi
2023-03-18 15:57 ` 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=20230313-split-objfile-type-allocator-2-v1-1-69ba773ac17b@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).