Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: matt rice <ratmice@gmail.com>
To: gdb-patches@sourceware.org
Cc: matt rice <ratmice@gmail.com>
Subject: [PATCH 2/7] [python] API for macros: memory management quirks.
Date: Wed, 24 Aug 2011 15:11:00 -0000	[thread overview]
Message-ID: <1314198654-9008-3-git-send-email-ratmice@gmail.com> (raw)
In-Reply-To: <1314198654-9008-1-git-send-email-ratmice@gmail.com>

2011-08-23  Matt Rice  <ratmice@gmail.com

	* dwarf2read.c (macro_start_file): Update args in call to
	new_macro_table.
	* macroscope.c (_initialize_macroscope): Ditto.
	* macrotab.c (struct macro_table): Remove obstack and bcache,
	add objfile.
	(macro_alloc): Replace obstack and bcache usage with those from
	the objfile.
	(macro_free, macro_bcache_free, macro_allow_redefinitions): Ditto
	(macro_bcache, new_macro_table): Ditto, and add assertions.
	(macro_table_objfile): New function.
	* macrotab.h: Replace forward declarations of bcache and obstack
	with objfile.
	(enum macro_table_type, macro_table_objfile): Add.
---
 gdb/dwarf2read.c |    3 +--
 gdb/macroscope.c |    2 +-
 gdb/macrotab.c   |   53 ++++++++++++++++++++++++++++++++---------------------
 gdb/macrotab.h   |   44 ++++++++++++++++++++++++++------------------
 4 files changed, 60 insertions(+), 42 deletions(-)

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index f66c1d9..c56f431 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -14351,8 +14351,7 @@ macro_start_file (int file, int line,
   /* We don't create a macro table for this compilation unit
      at all until we actually get a filename.  */
   if (! pending_macros)
-    pending_macros = new_macro_table (&objfile->objfile_obstack,
-                                      objfile->macro_cache);
+    pending_macros = new_macro_table (objfile, macro_table_type_from_objfile);
 
   if (! current_file)
     /* If we have no current file, then this must be the start_file
diff --git a/gdb/macroscope.c b/gdb/macroscope.c
index b529e68..2447670 100644
--- a/gdb/macroscope.c
+++ b/gdb/macroscope.c
@@ -160,7 +160,7 @@ extern initialize_file_ftype _initialize_macroscope;
 void
 _initialize_macroscope (void)
 {
-  macro_user_macros = new_macro_table (0, 0);
+  macro_user_macros = new_macro_table (NULL, macro_table_type_user_defined);
   macro_set_main (macro_user_macros, "<user-defined>");
   macro_allow_redefinitions (macro_user_macros);
 }
diff --git a/gdb/macrotab.c b/gdb/macrotab.c
index efcf835..b2825d2 100644
--- a/gdb/macrotab.c
+++ b/gdb/macrotab.c
@@ -35,13 +35,10 @@
 
 struct macro_table
 {
-  /* The obstack this table's data should be allocated in, or zero if
-     we should use xmalloc.  */
-  struct obstack *obstack;
-
-  /* The bcache we should use to hold macro names, argument names, and
-     definitions, or zero if we should use xmalloc.  */
-  struct bcache *bcache;
+  /* The objfile who's obstack this table's data should be allocated in,
+     and bcache we should use to hold macro names, argument
+     names, and definitions, or zero if we should use xmalloc. */
+  struct objfile *objfile;
 
   /* The main source file for this compilation unit --- the one whose
      name was given to the compiler.  This is the root of the
@@ -83,8 +80,8 @@ struct macro_table
 static void *
 macro_alloc (int size, struct macro_table *t)
 {
-  if (t->obstack)
-    return obstack_alloc (t->obstack, size);
+  if (t->objfile)
+    return obstack_alloc (&t->objfile->objfile_obstack, size);
   else
     return xmalloc (size);
 }
@@ -93,7 +90,7 @@ macro_alloc (int size, struct macro_table *t)
 static void
 macro_free (void *object, struct macro_table *t)
 {
-  if (t->obstack)
+  if (t->objfile)
     /* There are cases where we need to remove entries from a macro
        table, even when reading debugging information.  This should be
        rare, and there's no easy way to free arbitrary data from an
@@ -110,8 +107,10 @@ macro_free (void *object, struct macro_table *t)
 static const void *
 macro_bcache (struct macro_table *t, const void *addr, int len)
 {
-  if (t->bcache)
-    return bcache (addr, len, t->bcache);
+  gdb_assert (t->objfile == NULL || (t->objfile && t->objfile->macro_cache));
+
+  if (t->objfile && t->objfile->macro_cache)
+    return bcache (addr, len, t->objfile->macro_cache);
   else
     {
       void *copy = xmalloc (len);
@@ -137,7 +136,7 @@ macro_bcache_str (struct macro_table *t, const char *s)
 static void
 macro_bcache_free (struct macro_table *t, void *obj)
 {
-  if (t->bcache)
+  if (t->objfile)
     /* There are cases where we need to remove entries from a macro
        table, even when reading debugging information.  This should be
        rare, and there's no easy way to free data from a bcache, so we
@@ -147,6 +146,12 @@ macro_bcache_free (struct macro_table *t, void *obj)
     xfree (obj);
 }
 
+/* Return the objfile of the macro table, NULL if it is user defined.  */
+struct objfile *
+macro_table_objfile (struct macro_table *table)
+{
+  return table->objfile;
+}
 
 \f
 /* Macro tree keys, w/their comparison, allocation, and freeing functions.  */
@@ -438,7 +443,7 @@ macro_main (struct macro_table *t)
 void
 macro_allow_redefinitions (struct macro_table *t)
 {
-  gdb_assert (! t->obstack);
+  gdb_assert (! t->objfile);
   t->redef_ok = 1;
 }
 
@@ -972,20 +977,26 @@ macro_for_each_in_scope (struct macro_source_file *file, int line,
 
 
 struct macro_table *
-new_macro_table (struct obstack *obstack,
-                 struct bcache *b)
+new_macro_table (struct objfile *objfile, enum macro_table_type table_type)
 {
   struct macro_table *t;
 
+  if (table_type == macro_table_type_user_defined)
+    gdb_assert (objfile == NULL);
+  else if (table_type == macro_table_type_from_objfile)
+    {
+      gdb_assert (objfile != NULL);
+      gdb_assert (objfile->macro_cache != NULL);
+    }
+
   /* First, get storage for the `struct macro_table' itself.  */
-  if (obstack)
-    t = obstack_alloc (obstack, sizeof (*t));
-  else
+  if (table_type == macro_table_type_from_objfile)
+    t = obstack_alloc (&objfile->objfile_obstack, sizeof (*t));
+  else if (table_type == macro_table_type_user_defined)
     t = xmalloc (sizeof (*t));
 
   memset (t, 0, sizeof (*t));
-  t->obstack = obstack;
-  t->bcache = b;
+  t->objfile = objfile;
   t->main_source = NULL;
   t->redef_ok = 0;
   t->definitions = (splay_tree_new_with_allocator
diff --git a/gdb/macrotab.h b/gdb/macrotab.h
index a10351a..a773c49 100644
--- a/gdb/macrotab.h
+++ b/gdb/macrotab.h
@@ -21,8 +21,7 @@
 #ifndef MACROTAB_H
 #define MACROTAB_H
 
-struct obstack;
-struct bcache;
+struct objfile;
 
 /* How do we represent a source location?  I mean, how should we
    represent them within GDB; the user wants to use all sorts of
@@ -149,23 +148,28 @@ struct macro_source_file
 };
 
 
-/* Create a new, empty macro table.  Allocate it in OBSTACK, or use
-   xmalloc if OBSTACK is zero.  Use BCACHE to store all macro names,
-   arguments, definitions, and anything else that might be the same
-   amongst compilation units in an executable file; if BCACHE is zero,
-   don't cache these things.
+enum macro_table_type {
+  macro_table_type_user_defined,
+  macro_table_type_from_objfile
+};
 
-   Note that, if either OBSTACK or BCACHE are non-zero, then removing
-   information from the table may leak memory.  Neither obstacks nor
-   bcaches really allow you to remove information, so although we can
-   update the data structure to record the change, we can't free the
-   old data.  At the moment, since we only provide obstacks and
-   bcaches for macro tables for symtabs, this isn't a problem; only
-   odd debugging information makes a definition and then deletes it at
-   the same source location (although 'gcc -DFOO -UFOO -DFOO=2' does
-   do that in GCC 4.1.2.).  */
-struct macro_table *new_macro_table (struct obstack *obstack,
-                                     struct bcache *bcache);
+/* Create a new, empty macro table.  Allocate it in OBJFILE's obstack,
+   or use xmalloc if OBJFILE is zero.  Use OBJFILE's bcache to store
+   all macro names, arguments, definitions, and anything else that
+   might be the same amongst compilation units in an executable file;
+   if OBJFILE is zero, don't cache these things.
+
+   Note that, if OBJFILE is non-zero, then removing information from the
+   table may leak memory.  Neither obstacks nor bcaches really allow
+   you to remove information, so although we can update the data
+   structure to record the change, we can't free the old data.
+   At the moment, since we only provide obstacks and bcaches for macro
+   tables for symtabs, this isn't a problem; only odd debugging
+   information makes a definition and then deletes it at the same
+   source location (although 'gcc -DFOO -UFOO -DFOO=2' does do that
+   in GCC 4.1.2.).  */
+struct macro_table *new_macro_table (struct objfile *objfile,
+				     enum macro_table_type table_type);
 
 
 /* Free TABLE, and any macro definitions, source file structures,
@@ -173,6 +177,10 @@ struct macro_table *new_macro_table (struct obstack *obstack,
    allocated on an obstack, or if it uses a bcache.  */
 void free_macro_table (struct macro_table *table);
 
+/* Returns the OBJFILE for the given table, if the table
+   is for user-defined macros this function will return NULL.  */
+struct objfile * macro_table_objfile (struct macro_table *table);
+
 
 /* Set FILENAME as the main source file of TABLE.  Return a source
    file structure describing that file; if we record the #definition
-- 
1.7.4.4


  reply	other threads:[~2011-08-24 15:11 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-24 15:11 [PATCH 0/7] [python] API for macros matt rice
2011-08-24 15:11 ` matt rice [this message]
2011-08-26 20:40   ` [PATCH 2/7] [python] API for macros: memory management quirks Tom Tromey
2011-08-30 11:47   ` Phil Muldoon
2011-09-01 22:46     ` Matt Rice
2011-08-24 15:11 ` [PATCH 5/7] [python] API for macros: gdb.Objfile symtabs method matt rice
2011-08-30 13:08   ` Phil Muldoon
2011-09-01 23:18     ` Matt Rice
2011-09-02  1:07       ` Paul_Koning
2011-08-30 17:34   ` Tom Tromey
2011-09-02  0:56     ` Matt Rice
2011-08-24 15:11 ` [PATCH 1/7] [python] API for macros: abort or continuing macro iterators matt rice
2011-08-26 20:23   ` Tom Tromey
2011-08-30 11:10   ` Phil Muldoon
2011-09-01 21:48     ` Matt Rice
2011-08-24 15:12 ` [PATCH 3/7] [python] API for macros: Add gdb.Macro class matt rice
2011-08-30 12:45   ` Phil Muldoon
2011-09-01 22:57     ` Matt Rice
2011-08-24 15:12 ` [PATCH 4/7] [python] API for macros: Add methods to get a gdb.Macro matt rice
2011-08-30 13:04   ` Phil Muldoon
2011-08-30 17:41     ` Tom Tromey
2011-08-30 20:28       ` Phil Muldoon
2011-08-30 20:35         ` Phil Muldoon
2011-09-01 23:13           ` Matt Rice
2011-09-02  1:15             ` Paul_Koning
2011-09-02 10:04               ` Paul_Koning
2011-09-02 12:04             ` Phil Muldoon
2011-08-30 20:38         ` Tom Tromey
2011-08-30 20:58           ` Phil Muldoon
2011-08-24 15:12 ` [PATCH 6/7] [python] API for macros: Add docs matt rice
2011-08-24 20:10   ` Eli Zaretskii
2011-08-25 12:33     ` Matt Rice
2011-08-25 17:36       ` Eli Zaretskii
2011-08-26  8:04         ` Matt Rice
2011-08-26 10:42           ` Eli Zaretskii
2011-08-26 11:17             ` Matt Rice
2011-08-26 12:08               ` Eli Zaretskii
2011-08-26 14:06                 ` Matt Rice
2011-08-26 15:05                   ` Eli Zaretskii
2011-08-24 15:12 ` [PATCH 7/7] [python] API for macros: Add tests matt rice
2011-08-30 13:12   ` Phil Muldoon
2011-08-30 15:54   ` Tom Tromey
2011-08-30  9:44 ` [PATCH 0/7] [python] API for macros Phil Muldoon
2011-09-01 21:33   ` Matt Rice

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=1314198654-9008-3-git-send-email-ratmice@gmail.com \
    --to=ratmice@gmail.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