From e3c69974ac166b09a48d5b0a971cf4833d293b90 Mon Sep 17 00:00:00 2001 From: Paul Pluzhnikov Date: Tue, 4 Aug 2009 18:46:05 +0000 Subject: 2009-08-04 Paul Pluzhnikov * objfiles.h (OBJF_KEEPBFD): Delete. (gdb_bfd_unref): New prototype. * objfiles.c (gdb_bfd_unref): New function. (free_objfile): Call gdb_bfd_unref. * solib.c (free_so): Likewise. (symbol_add_stub): Set refcount. --- gdb/objfiles.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'gdb/objfiles.c') diff --git a/gdb/objfiles.c b/gdb/objfiles.c index c983b11..43c5174 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -452,16 +452,7 @@ free_objfile (struct objfile *objfile) /* Discard any data modules have associated with the objfile. */ objfile_free_data (objfile); - /* We always close the bfd, unless the OBJF_KEEPBFD flag is set. */ - - if (objfile->obfd != NULL && !(objfile->flags & OBJF_KEEPBFD)) - { - char *name = bfd_get_filename (objfile->obfd); - if (!bfd_close (objfile->obfd)) - warning (_("cannot close \"%s\": %s"), - name, bfd_errmsg (bfd_get_error ())); - xfree (name); - } + gdb_bfd_unref (objfile->obfd); /* Remove it from the chain of all objfiles. */ @@ -1020,3 +1011,35 @@ objfiles_changed (void) { objfiles_changed_p = 1; /* Rebuild section map next time we need it. */ } + +/* Unreference and possibly close abfd. */ +void +gdb_bfd_unref (struct bfd *abfd) +{ + int *p_refcount; + char *name; + + if (abfd == NULL) + return; + + p_refcount = abfd->usrdata; + + /* Valid range for p_refcount: NULL (single owner), or a pointer + to int counter, which has a value of 1 (single owner) or 2 (shared). */ + gdb_assert (p_refcount == NULL || *p_refcount == 1 || *p_refcount == 2); + + if (p_refcount != NULL) + { + *p_refcount -= 1; + if (*p_refcount > 0) + return; + } + xfree (p_refcount); + abfd->usrdata = NULL; /* Paranoia. */ + + name = bfd_get_filename (abfd); + if (!bfd_close (abfd)) + warning (_("cannot close \"%s\": %s"), + name, bfd_errmsg (bfd_get_error ())); + xfree (name); +} -- cgit v1.1