cache.c reorg

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

cache.c reorg

Alan Modra
In preparation for a followup patch.

        * cache.c: Reorganize file to avoid forward reference.

Index: bfd/cache.c
===================================================================
RCS file: /cvs/src/src/bfd/cache.c,v
retrieving revision 1.24
diff -u -p -r1.24 cache.c
--- bfd/cache.c 26 Oct 2005 12:17:42 -0000 1.24
+++ bfd/cache.c 27 Oct 2005 03:14:50 -0000
@@ -45,8 +45,195 @@ SUBSECTION
 #include "libbfd.h"
 #include "libiberty.h"
 
-static bfd_boolean bfd_cache_delete (bfd *);
+/*
+INTERNAL_FUNCTION
+ BFD_CACHE_MAX_OPEN macro
+
+DESCRIPTION
+ The maximum number of files which the cache will keep open at
+ one time.
+
+.#define BFD_CACHE_MAX_OPEN 10
+
+*/
+
+/* The number of BFD files we have open.  */
+
+static int open_files;
+
+/*
+INTERNAL_FUNCTION
+ bfd_last_cache
+
+SYNOPSIS
+ extern bfd *bfd_last_cache;
+
+DESCRIPTION
+ Zero, or a pointer to the topmost BFD on the chain.  This is
+ used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
+ determine when it can avoid a function call.
+*/
+
+bfd *bfd_last_cache = NULL;
 
+/* Insert a BFD into the cache.  */
+
+static void
+insert (bfd *abfd)
+{
+  if (bfd_last_cache == NULL)
+    {
+      abfd->lru_next = abfd;
+      abfd->lru_prev = abfd;
+    }
+  else
+    {
+      abfd->lru_next = bfd_last_cache;
+      abfd->lru_prev = bfd_last_cache->lru_prev;
+      abfd->lru_prev->lru_next = abfd;
+      abfd->lru_next->lru_prev = abfd;
+    }
+  bfd_last_cache = abfd;
+}
+
+/* Remove a BFD from the cache.  */
+
+static void
+snip (bfd *abfd)
+{
+  abfd->lru_prev->lru_next = abfd->lru_next;
+  abfd->lru_next->lru_prev = abfd->lru_prev;
+  if (abfd == bfd_last_cache)
+    {
+      bfd_last_cache = abfd->lru_next;
+      if (abfd == bfd_last_cache)
+ bfd_last_cache = NULL;
+    }
+}
+
+/* Close a BFD and remove it from the cache.  */
+
+static bfd_boolean
+bfd_cache_delete (bfd *abfd)
+{
+  bfd_boolean ret;
+
+  if (fclose ((FILE *) abfd->iostream) == 0)
+    ret = TRUE;
+  else
+    {
+      ret = FALSE;
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  snip (abfd);
+
+  abfd->iostream = NULL;
+  --open_files;
+
+  return ret;
+}
+
+/* We need to open a new file, and the cache is full.  Find the least
+   recently used cacheable BFD and close it.  */
+
+static bfd_boolean
+close_one (void)
+{
+  register bfd *kill;
+
+  if (bfd_last_cache == NULL)
+    kill = NULL;
+  else
+    {
+      for (kill = bfd_last_cache->lru_prev;
+   ! kill->cacheable;
+   kill = kill->lru_prev)
+ {
+  if (kill == bfd_last_cache)
+    {
+      kill = NULL;
+      break;
+    }
+ }
+    }
+
+  if (kill == NULL)
+    {
+      /* There are no open cacheable BFD's.  */
+      return TRUE;
+    }
+
+  kill->where = real_ftell ((FILE *) kill->iostream);
+
+  return bfd_cache_delete (kill);
+}
+
+/*
+  INTERNAL_FUNCTION
+   bfd_cache_lookup
+
+  DESCRIPTION
+ Check to see if the required BFD is the same as the last one
+ looked up. If so, then it can use the stream in the BFD with
+ impunity, since it can't have changed since the last lookup;
+ otherwise, it has to perform the complicated lookup function.
+
+  .#define bfd_cache_lookup(x) \
+  .    ((x) == bfd_last_cache ? \
+  .      (FILE *) (bfd_last_cache->iostream): \
+  .       bfd_cache_lookup_worker (x))
+
+ */
+
+/*
+INTERNAL_FUNCTION
+ bfd_cache_lookup_worker
+
+SYNOPSIS
+ FILE *bfd_cache_lookup_worker (bfd *abfd);
+
+DESCRIPTION
+ Called when the macro <<bfd_cache_lookup>> fails to find a
+ quick answer.  Find a file descriptor for @var{abfd}.  If
+ necessary, it open it.  If there are already more than
+ <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
+ avoid running out of file descriptors.  It will return NULL
+ if it is unable to (re)open the @var{abfd}.
+*/
+
+FILE *
+bfd_cache_lookup_worker (bfd *abfd)
+{
+  bfd *orig_bfd = abfd;
+  if ((abfd->flags & BFD_IN_MEMORY) != 0)
+    abort ();
+
+  if (abfd->my_archive)
+    abfd = abfd->my_archive;
+
+  if (abfd->iostream != NULL)
+    {
+      /* Move the file to the start of the cache.  */
+      if (abfd != bfd_last_cache)
+ {
+  snip (abfd);
+  insert (abfd);
+ }
+      return (FILE *) abfd->iostream;
+    }
+
+  if (bfd_open_file (abfd) == NULL)
+    ;
+  else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
+    bfd_set_error (bfd_error_system_call);
+  else
+    return (FILE *) abfd->iostream;
+
+  (*_bfd_error_handler) (_("reopening %B: %s\n"),
+ orig_bfd, bfd_errmsg (bfd_get_error ()));
+  return NULL;
+}
 
 static file_ptr
 cache_btell (struct bfd *abfd)
@@ -175,147 +362,6 @@ static const struct bfd_iovec cache_iove
 
 /*
 INTERNAL_FUNCTION
- BFD_CACHE_MAX_OPEN macro
-
-DESCRIPTION
- The maximum number of files which the cache will keep open at
- one time.
-
-.#define BFD_CACHE_MAX_OPEN 10
-
-*/
-
-/* The number of BFD files we have open.  */
-
-static int open_files;
-
-/*
-INTERNAL_FUNCTION
- bfd_last_cache
-
-SYNOPSIS
- extern bfd *bfd_last_cache;
-
-DESCRIPTION
- Zero, or a pointer to the topmost BFD on the chain.  This is
- used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
- determine when it can avoid a function call.
-*/
-
-bfd *bfd_last_cache = NULL;
-
-/*
-  INTERNAL_FUNCTION
-   bfd_cache_lookup
-
-  DESCRIPTION
- Check to see if the required BFD is the same as the last one
- looked up. If so, then it can use the stream in the BFD with
- impunity, since it can't have changed since the last lookup;
- otherwise, it has to perform the complicated lookup function.
-
-  .#define bfd_cache_lookup(x) \
-  .    ((x) == bfd_last_cache ? \
-  .      (FILE *) (bfd_last_cache->iostream): \
-  .       bfd_cache_lookup_worker (x))
-
- */
-
-/* Insert a BFD into the cache.  */
-
-static void
-insert (bfd *abfd)
-{
-  if (bfd_last_cache == NULL)
-    {
-      abfd->lru_next = abfd;
-      abfd->lru_prev = abfd;
-    }
-  else
-    {
-      abfd->lru_next = bfd_last_cache;
-      abfd->lru_prev = bfd_last_cache->lru_prev;
-      abfd->lru_prev->lru_next = abfd;
-      abfd->lru_next->lru_prev = abfd;
-    }
-  bfd_last_cache = abfd;
-}
-
-/* Remove a BFD from the cache.  */
-
-static void
-snip (bfd *abfd)
-{
-  abfd->lru_prev->lru_next = abfd->lru_next;
-  abfd->lru_next->lru_prev = abfd->lru_prev;
-  if (abfd == bfd_last_cache)
-    {
-      bfd_last_cache = abfd->lru_next;
-      if (abfd == bfd_last_cache)
- bfd_last_cache = NULL;
-    }
-}
-
-/* We need to open a new file, and the cache is full.  Find the least
-   recently used cacheable BFD and close it.  */
-
-static bfd_boolean
-close_one (void)
-{
-  register bfd *kill;
-
-  if (bfd_last_cache == NULL)
-    kill = NULL;
-  else
-    {
-      for (kill = bfd_last_cache->lru_prev;
-   ! kill->cacheable;
-   kill = kill->lru_prev)
- {
-  if (kill == bfd_last_cache)
-    {
-      kill = NULL;
-      break;
-    }
- }
-    }
-
-  if (kill == NULL)
-    {
-      /* There are no open cacheable BFD's.  */
-      return TRUE;
-    }
-
-  kill->where = real_ftell ((FILE *) kill->iostream);
-
-  return bfd_cache_delete (kill);
-}
-
-/* Close a BFD and remove it from the cache.  */
-
-static bfd_boolean
-bfd_cache_delete (bfd *abfd)
-{
-  bfd_boolean ret;
-
-  if (fclose ((FILE *) abfd->iostream) == 0)
-    ret = TRUE;
-  else
-    {
-      ret = FALSE;
-      bfd_set_error (bfd_error_system_call);
-    }
-
-  snip (abfd);
-
-  abfd->iostream = NULL;
-  --open_files;
-
-  return ret;
-}
-
-/*
-INTERNAL_FUNCTION
  bfd_cache_init
 
 SYNOPSIS
@@ -480,52 +526,3 @@ bfd_open_file (bfd *abfd)
 
   return (FILE *) abfd->iostream;
 }
-
-/*
-INTERNAL_FUNCTION
- bfd_cache_lookup_worker
-
-SYNOPSIS
- FILE *bfd_cache_lookup_worker (bfd *abfd);
-
-DESCRIPTION
- Called when the macro <<bfd_cache_lookup>> fails to find a
- quick answer.  Find a file descriptor for @var{abfd}.  If
- necessary, it open it.  If there are already more than
- <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
- avoid running out of file descriptors.  It will return NULL
- if it is unable to (re)open the @var{abfd}.
-*/
-
-FILE *
-bfd_cache_lookup_worker (bfd *abfd)
-{
-  bfd *orig_bfd = abfd;
-  if ((abfd->flags & BFD_IN_MEMORY) != 0)
-    abort ();
-
-  if (abfd->my_archive)
-    abfd = abfd->my_archive;
-
-  if (abfd->iostream != NULL)
-    {
-      /* Move the file to the start of the cache.  */
-      if (abfd != bfd_last_cache)
- {
-  snip (abfd);
-  insert (abfd);
- }
-      return (FILE *) abfd->iostream;
-    }
-
-  if (bfd_open_file (abfd) == NULL)
-    ;
-  else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
-    bfd_set_error (bfd_error_system_call);
-  else
-    return (FILE *) abfd->iostream;
-
-  (*_bfd_error_handler) (_("reopening %B: %s\n"),
- orig_bfd, bfd_errmsg (bfd_get_error ()));
-  return NULL;
-}

--
Alan Modra
IBM OzLabs - Linux Technology Centre
Reply | Threaded
Open this post in threaded view
|

Re: cache.c reorg

Alan Modra
        * hppabsd-core.c (hppabsd_core_core_file_p): Use bfd_stat, not fstat.
        * sco5-core.c (sco5_core_file_p): Likewise.
        * trad-core.c (trad_unix_core_file_p): Likewise.

The above allows the following.

        * cache.c (BFD_CACHE_MAX_OPEN): Make private to this file.
        (bfd_last_cache, bfd_cache_lookup, bfd_cache_lookup_worker): Likewise.
        * libbfd-in.h (bfd_cache_lookup_worker, bfd_last_cache): Delete.
        * libbfd.h: Regenerate.

Index: bfd/cache.c
===================================================================
RCS file: /cvs/src/src/bfd/cache.c,v
retrieving revision 1.25
diff -u -p -r1.25 cache.c
--- bfd/cache.c 27 Oct 2005 03:22:13 -0000 1.25
+++ bfd/cache.c 27 Oct 2005 03:28:22 -0000
@@ -45,36 +45,20 @@ SUBSECTION
 #include "libbfd.h"
 #include "libiberty.h"
 
-/*
-INTERNAL_FUNCTION
- BFD_CACHE_MAX_OPEN macro
-
-DESCRIPTION
- The maximum number of files which the cache will keep open at
- one time.
+/* The maximum number of files which the cache will keep open at
+   one time.  */
 
-.#define BFD_CACHE_MAX_OPEN 10
-
-*/
+#define BFD_CACHE_MAX_OPEN 10
 
 /* The number of BFD files we have open.  */
 
 static int open_files;
 
-/*
-INTERNAL_FUNCTION
- bfd_last_cache
-
-SYNOPSIS
- extern bfd *bfd_last_cache;
-
-DESCRIPTION
- Zero, or a pointer to the topmost BFD on the chain.  This is
- used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
- determine when it can avoid a function call.
-*/
+/* Zero, or a pointer to the topmost BFD on the chain.  This is
+   used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
+   determine when it can avoid a function call.  */
 
-bfd *bfd_last_cache = NULL;
+static bfd *bfd_last_cache = NULL;
 
 /* Insert a BFD into the cache.  */
 
@@ -169,40 +153,24 @@ close_one (void)
   return bfd_cache_delete (kill);
 }
 
-/*
-  INTERNAL_FUNCTION
-   bfd_cache_lookup
-
-  DESCRIPTION
- Check to see if the required BFD is the same as the last one
- looked up. If so, then it can use the stream in the BFD with
- impunity, since it can't have changed since the last lookup;
- otherwise, it has to perform the complicated lookup function.
-
-  .#define bfd_cache_lookup(x) \
-  .    ((x) == bfd_last_cache ? \
-  .      (FILE *) (bfd_last_cache->iostream): \
-  .       bfd_cache_lookup_worker (x))
-
- */
-
-/*
-INTERNAL_FUNCTION
- bfd_cache_lookup_worker
+/* Check to see if the required BFD is the same as the last one
+   looked up. If so, then it can use the stream in the BFD with
+   impunity, since it can't have changed since the last lookup;
+   otherwise, it has to perform the complicated lookup function.  */
+
+#define bfd_cache_lookup(x) \
+  ((x) == bfd_last_cache \
+   ? (FILE *) (bfd_last_cache->iostream) \
+   : bfd_cache_lookup_worker (x))
+
+/* Called when the macro <<bfd_cache_lookup>> fails to find a
+   quick answer.  Find a file descriptor for @var{abfd}.  If
+   necessary, it open it.  If there are already more than
+   <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
+   avoid running out of file descriptors.  It will return NULL
+   if it is unable to (re)open the @var{abfd}.  */
 
-SYNOPSIS
- FILE *bfd_cache_lookup_worker (bfd *abfd);
-
-DESCRIPTION
- Called when the macro <<bfd_cache_lookup>> fails to find a
- quick answer.  Find a file descriptor for @var{abfd}.  If
- necessary, it open it.  If there are already more than
- <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
- avoid running out of file descriptors.  It will return NULL
- if it is unable to (re)open the @var{abfd}.
-*/
-
-FILE *
+static FILE *
 bfd_cache_lookup_worker (bfd *abfd)
 {
   bfd *orig_bfd = abfd;
Index: bfd/hppabsd-core.c
===================================================================
RCS file: /cvs/src/src/bfd/hppabsd-core.c,v
retrieving revision 1.16
diff -u -p -r1.16 hppabsd-core.c
--- bfd/hppabsd-core.c 26 Oct 2005 12:17:42 -0000 1.16
+++ bfd/hppabsd-core.c 27 Oct 2005 03:28:22 -0000
@@ -137,16 +137,11 @@ hppabsd_core_core_file_p (abfd)
   /* Sanity checks.  Make sure the size of the core file matches the
      the size computed from information within the core itself.  */
   {
-    FILE *stream = bfd_cache_lookup (abfd);
     struct stat statbuf;
 
-    if (stream == NULL)
+    if (bfd_stat (abfd, &statbuf) < 0)
       return NULL;
-    if (fstat (fileno (stream), &statbuf) < 0)
-      {
- bfd_set_error (bfd_error_system_call);
- return NULL;
-      }
+
     if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) > statbuf.st_size)
       {
  bfd_set_error (bfd_error_file_truncated);
Index: bfd/libbfd-in.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd-in.h,v
retrieving revision 1.57
diff -u -p -r1.57 libbfd-in.h
--- bfd/libbfd-in.h 24 Oct 2005 01:40:58 -0000 1.57
+++ bfd/libbfd-in.h 27 Oct 2005 03:28:23 -0000
@@ -652,11 +652,6 @@ extern void _bfd_abort
 extern file_ptr real_ftell (FILE *file);
 extern int real_fseek (FILE *file, file_ptr offset, int whence);
 
-FILE * bfd_cache_lookup_worker
-  (bfd *);
-
-extern bfd *bfd_last_cache;
-
 /* List of supported target vectors, and the default vector (if
    bfd_default_vector[0] is NULL, there is no default).  */
 extern const bfd_target * const *bfd_target_vector;
Index: bfd/libbfd.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd.h,v
retrieving revision 1.161
diff -u -p -r1.161 libbfd.h
--- bfd/libbfd.h 25 Oct 2005 17:40:10 -0000 1.161
+++ bfd/libbfd.h 27 Oct 2005 03:28:23 -0000
@@ -657,11 +657,6 @@ extern void _bfd_abort
 extern file_ptr real_ftell (FILE *file);
 extern int real_fseek (FILE *file, file_ptr offset, int whence);
 
-FILE * bfd_cache_lookup_worker
-  (bfd *);
-
-extern bfd *bfd_last_cache;
-
 /* List of supported target vectors, and the default vector (if
    bfd_default_vector[0] is NULL, there is no default).  */
 extern const bfd_target * const *bfd_target_vector;
@@ -767,21 +762,12 @@ struct _bfd_window_internal {
   unsigned mapped : 1;         /* 1 = mmap, 0 = malloc */
 };
 /* Extracted from cache.c.  */
-#define BFD_CACHE_MAX_OPEN 10
-extern bfd *bfd_last_cache;
-
-#define bfd_cache_lookup(x) \
-    ((x) == bfd_last_cache ? \
-      (FILE *) (bfd_last_cache->iostream): \
-       bfd_cache_lookup_worker (x))
 bfd_boolean bfd_cache_init (bfd *abfd);
 
 bfd_boolean bfd_cache_close (bfd *abfd);
 
 FILE* bfd_open_file (bfd *abfd);
 
-FILE *bfd_cache_lookup_worker (bfd *abfd);
-
 /* Extracted from reloc.c.  */
 #ifdef _BFD_MAKE_TABLE_bfd_reloc_code_real
 
Index: bfd/sco5-core.c
===================================================================
RCS file: /cvs/src/src/bfd/sco5-core.c,v
retrieving revision 1.16
diff -u -p -r1.16 sco5-core.c
--- bfd/sco5-core.c 26 Oct 2005 12:17:42 -0000 1.16
+++ bfd/sco5-core.c 27 Oct 2005 03:28:23 -0000
@@ -126,16 +126,11 @@ sco5_core_file_p (abfd)
   /* Read coreoffsets region at end of core (see core(FP)).  */
 
   {
-    FILE *stream = bfd_cache_lookup (abfd);
     struct stat statbuf;
 
-    if (stream == NULL)
+    if (bfd_stat (abfd, &statbuf) < 0)
       return NULL;
-    if (fstat (fileno (stream), &statbuf) < 0)
-      {
- bfd_set_error (bfd_error_system_call);
- return NULL;
-      }
+
     coresize = statbuf.st_size;
   }
   /* Last long in core is sizeof struct coreoffsets, read it */
Index: bfd/trad-core.c
===================================================================
RCS file: /cvs/src/src/bfd/trad-core.c,v
retrieving revision 1.21
diff -u -p -r1.21 trad-core.c
--- bfd/trad-core.c 26 Oct 2005 12:17:42 -0000 1.21
+++ bfd/trad-core.c 27 Oct 2005 03:28:23 -0000
@@ -109,16 +109,11 @@ trad_unix_core_file_p (abfd)
 
   /* Check that the size claimed is no greater than the file size.  */
   {
-    FILE *stream = bfd_cache_lookup (abfd);
     struct stat statbuf;
 
-    if (stream == NULL)
+    if (bfd_stat (abfd, &statbuf) < 0)
       return 0;
-    if (fstat (fileno (stream), &statbuf) < 0)
-      {
- bfd_set_error (bfd_error_system_call);
- return 0;
-      }
+
     if ((unsigned long) (NBPG * (UPAGES + u.u_dsize
 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
  - u.u_tsize

--
Alan Modra
IBM OzLabs - Linux Technology Centre
Reply | Threaded
Open this post in threaded view
|

Re: cache.c reorg

Alan Modra
On Thu, Oct 27, 2005 at 01:06:56PM +0930, Alan Modra wrote:

> * hppabsd-core.c (hppabsd_core_core_file_p): Use bfd_stat, not fstat.
> * sco5-core.c (sco5_core_file_p): Likewise.
> * trad-core.c (trad_unix_core_file_p): Likewise.
>
> The above allows the following.
>
> * cache.c (BFD_CACHE_MAX_OPEN): Make private to this file.
> (bfd_last_cache, bfd_cache_lookup, bfd_cache_lookup_worker): Likewise.
> * libbfd-in.h (bfd_cache_lookup_worker, bfd_last_cache): Delete.
> * libbfd.h: Regenerate.

Bah, missed this one.

        * bfdwin.c (bfd_get_file_window): Seek into file in place of
        using bfd_cache_lookup.

Index: bfd/bfdwin.c
===================================================================
RCS file: /cvs/src/src/bfd/bfdwin.c,v
retrieving revision 1.7
diff -u -p -r1.7 bfdwin.c
--- bfd/bfdwin.c 26 Oct 2005 12:17:42 -0000 1.7
+++ bfd/bfdwin.c 27 Oct 2005 04:04:24 -0000
@@ -144,7 +145,6 @@ bfd_get_file_window (bfd *abfd,
       file_ptr file_offset, offset2;
       size_t real_size;
       int fd;
-      FILE *f;
 
       /* Find the real file and the real offset into it.  */
       while (abfd->my_archive != NULL)
@@ -152,10 +152,13 @@ bfd_get_file_window (bfd *abfd,
   offset += abfd->origin;
   abfd = abfd->my_archive;
  }
-      f = bfd_cache_lookup (abfd);
-      if (f == NULL)
+
+      /* Seek into the file, to ensure it is open if cacheable.  */
+      if (abfd->iostream == NULL
+  && (abfd->iovec == NULL
+      || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
  return FALSE;
-      fd = fileno (f);
+      fd = fileno ((FILE *) abfd->iostream);
 
       /* Compute offsets and size for mmap and for the user's data.  */
       offset2 = offset % pagesize;

--
Alan Modra
IBM OzLabs - Linux Technology Centre