OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [bfd/] [cache.c] - Diff between revs 579 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 579 Rev 1765
/* BFD library -- caching of file descriptors.
/* BFD library -- caching of file descriptors.
   Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001
   Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
   Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
 
 
This file is part of BFD, the Binary File Descriptor library.
This file is part of BFD, the Binary File Descriptor library.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 
/*
/*
SECTION
SECTION
        File caching
        File caching
 
 
        The file caching mechanism is embedded within BFD and allows
        The file caching mechanism is embedded within BFD and allows
        the application to open as many BFDs as it wants without
        the application to open as many BFDs as it wants without
        regard to the underlying operating system's file descriptor
        regard to the underlying operating system's file descriptor
        limit (often as low as 20 open files).  The module in
        limit (often as low as 20 open files).  The module in
        <<cache.c>> maintains a least recently used list of
        <<cache.c>> maintains a least recently used list of
        <<BFD_CACHE_MAX_OPEN>> files, and exports the name
        <<BFD_CACHE_MAX_OPEN>> files, and exports the name
        <<bfd_cache_lookup>>, which runs around and makes sure that
        <<bfd_cache_lookup>>, which runs around and makes sure that
        the required BFD is open. If not, then it chooses a file to
        the required BFD is open. If not, then it chooses a file to
        close, closes it and opens the one wanted, returning its file
        close, closes it and opens the one wanted, returning its file
        handle.
        handle.
 
 
*/
*/
 
 
#include "bfd.h"
#include "bfd.h"
#include "sysdep.h"
#include "sysdep.h"
#include "libbfd.h"
#include "libbfd.h"
 
 
static void insert PARAMS ((bfd *));
static void insert PARAMS ((bfd *));
static void snip PARAMS ((bfd *));
static void snip PARAMS ((bfd *));
static boolean close_one PARAMS ((void));
static boolean close_one PARAMS ((void));
static boolean bfd_cache_delete PARAMS ((bfd *));
static boolean bfd_cache_delete PARAMS ((bfd *));
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        BFD_CACHE_MAX_OPEN macro
        BFD_CACHE_MAX_OPEN macro
 
 
DESCRIPTION
DESCRIPTION
        The maximum number of files which the cache will keep open at
        The maximum number of files which the cache will keep open at
        one time.
        one time.
 
 
.#define BFD_CACHE_MAX_OPEN 10
.#define BFD_CACHE_MAX_OPEN 10
 
 
*/
*/
 
 
/* The number of BFD files we have open.  */
/* The number of BFD files we have open.  */
 
 
static int open_files;
static int open_files;
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        bfd_last_cache
        bfd_last_cache
 
 
SYNOPSIS
SYNOPSIS
        extern bfd *bfd_last_cache;
        extern bfd *bfd_last_cache;
 
 
DESCRIPTION
DESCRIPTION
        Zero, or a pointer to the topmost BFD on the chain.  This is
        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
        used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
        determine when it can avoid a function call.
        determine when it can avoid a function call.
*/
*/
 
 
bfd *bfd_last_cache;
bfd *bfd_last_cache;
 
 
/*
/*
  INTERNAL_FUNCTION
  INTERNAL_FUNCTION
        bfd_cache_lookup
        bfd_cache_lookup
 
 
  DESCRIPTION
  DESCRIPTION
        Check to see if the required BFD is the same as the last one
        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
        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;
        impunity, since it can't have changed since the last lookup;
        otherwise, it has to perform the complicated lookup function.
        otherwise, it has to perform the complicated lookup function.
 
 
  .#define bfd_cache_lookup(x) \
  .#define bfd_cache_lookup(x) \
  .    ((x)==bfd_last_cache? \
  .    ((x)==bfd_last_cache? \
  .      (FILE*) (bfd_last_cache->iostream): \
  .      (FILE*) (bfd_last_cache->iostream): \
  .       bfd_cache_lookup_worker(x))
  .       bfd_cache_lookup_worker(x))
 
 
 */
 */
 
 
/* Insert a BFD into the cache.  */
/* Insert a BFD into the cache.  */
 
 
static INLINE void
static INLINE void
insert (abfd)
insert (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  if (bfd_last_cache == NULL)
  if (bfd_last_cache == NULL)
    {
    {
      abfd->lru_next = abfd;
      abfd->lru_next = abfd;
      abfd->lru_prev = abfd;
      abfd->lru_prev = abfd;
    }
    }
  else
  else
    {
    {
      abfd->lru_next = bfd_last_cache;
      abfd->lru_next = bfd_last_cache;
      abfd->lru_prev = bfd_last_cache->lru_prev;
      abfd->lru_prev = bfd_last_cache->lru_prev;
      abfd->lru_prev->lru_next = abfd;
      abfd->lru_prev->lru_next = abfd;
      abfd->lru_next->lru_prev = abfd;
      abfd->lru_next->lru_prev = abfd;
    }
    }
  bfd_last_cache = abfd;
  bfd_last_cache = abfd;
}
}
 
 
/* Remove a BFD from the cache.  */
/* Remove a BFD from the cache.  */
 
 
static INLINE void
static INLINE void
snip (abfd)
snip (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  abfd->lru_prev->lru_next = abfd->lru_next;
  abfd->lru_prev->lru_next = abfd->lru_next;
  abfd->lru_next->lru_prev = abfd->lru_prev;
  abfd->lru_next->lru_prev = abfd->lru_prev;
  if (abfd == bfd_last_cache)
  if (abfd == bfd_last_cache)
    {
    {
      bfd_last_cache = abfd->lru_next;
      bfd_last_cache = abfd->lru_next;
      if (abfd == bfd_last_cache)
      if (abfd == bfd_last_cache)
        bfd_last_cache = NULL;
        bfd_last_cache = NULL;
    }
    }
}
}
 
 
/* We need to open a new file, and the cache is full.  Find the least
/* We need to open a new file, and the cache is full.  Find the least
   recently used cacheable BFD and close it.  */
   recently used cacheable BFD and close it.  */
 
 
static boolean
static boolean
close_one ()
close_one ()
{
{
  register bfd *kill;
  register bfd *kill;
 
 
  if (bfd_last_cache == NULL)
  if (bfd_last_cache == NULL)
    kill = NULL;
    kill = NULL;
  else
  else
    {
    {
      for (kill = bfd_last_cache->lru_prev;
      for (kill = bfd_last_cache->lru_prev;
           ! kill->cacheable;
           ! kill->cacheable;
           kill = kill->lru_prev)
           kill = kill->lru_prev)
        {
        {
          if (kill == bfd_last_cache)
          if (kill == bfd_last_cache)
            {
            {
              kill = NULL;
              kill = NULL;
              break;
              break;
            }
            }
        }
        }
    }
    }
 
 
  if (kill == NULL)
  if (kill == NULL)
    {
    {
      /* There are no open cacheable BFD's.  */
      /* There are no open cacheable BFD's.  */
      return true;
      return true;
    }
    }
 
 
  kill->where = ftell ((FILE *) kill->iostream);
  kill->where = ftell ((FILE *) kill->iostream);
 
 
  return bfd_cache_delete (kill);
  return bfd_cache_delete (kill);
}
}
 
 
/* Close a BFD and remove it from the cache.  */
/* Close a BFD and remove it from the cache.  */
 
 
static boolean
static boolean
bfd_cache_delete (abfd)
bfd_cache_delete (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  boolean ret;
  boolean ret;
 
 
  if (fclose ((FILE *) abfd->iostream) == 0)
  if (fclose ((FILE *) abfd->iostream) == 0)
    ret = true;
    ret = true;
  else
  else
    {
    {
      ret = false;
      ret = false;
      bfd_set_error (bfd_error_system_call);
      bfd_set_error (bfd_error_system_call);
    }
    }
 
 
  snip (abfd);
  snip (abfd);
 
 
  abfd->iostream = NULL;
  abfd->iostream = NULL;
  --open_files;
  --open_files;
 
 
  return ret;
  return ret;
}
}
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        bfd_cache_init
        bfd_cache_init
 
 
SYNOPSIS
SYNOPSIS
        boolean bfd_cache_init (bfd *abfd);
        boolean bfd_cache_init (bfd *abfd);
 
 
DESCRIPTION
DESCRIPTION
        Add a newly opened BFD to the cache.
        Add a newly opened BFD to the cache.
*/
*/
 
 
boolean
boolean
bfd_cache_init (abfd)
bfd_cache_init (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  BFD_ASSERT (abfd->iostream != NULL);
  BFD_ASSERT (abfd->iostream != NULL);
  if (open_files >= BFD_CACHE_MAX_OPEN)
  if (open_files >= BFD_CACHE_MAX_OPEN)
    {
    {
      if (! close_one ())
      if (! close_one ())
        return false;
        return false;
    }
    }
  insert (abfd);
  insert (abfd);
  ++open_files;
  ++open_files;
  return true;
  return true;
}
}
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        bfd_cache_close
        bfd_cache_close
 
 
SYNOPSIS
SYNOPSIS
        boolean bfd_cache_close (bfd *abfd);
        boolean bfd_cache_close (bfd *abfd);
 
 
DESCRIPTION
DESCRIPTION
        Remove the BFD @var{abfd} from the cache. If the attached file is open,
        Remove the BFD @var{abfd} from the cache. If the attached file is open,
        then close it too.
        then close it too.
 
 
RETURNS
RETURNS
        <<false>> is returned if closing the file fails, <<true>> is
        <<false>> is returned if closing the file fails, <<true>> is
        returned if all is well.
        returned if all is well.
*/
*/
 
 
boolean
boolean
bfd_cache_close (abfd)
bfd_cache_close (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  if (abfd->iostream == NULL
  if (abfd->iostream == NULL
      || (abfd->flags & BFD_IN_MEMORY) != 0)
      || (abfd->flags & BFD_IN_MEMORY) != 0)
    return true;
    return true;
 
 
  return bfd_cache_delete (abfd);
  return bfd_cache_delete (abfd);
}
}
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        bfd_open_file
        bfd_open_file
 
 
SYNOPSIS
SYNOPSIS
        FILE* bfd_open_file(bfd *abfd);
        FILE* bfd_open_file(bfd *abfd);
 
 
DESCRIPTION
DESCRIPTION
        Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
        Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
        (possibly <<NULL>>) that results from this operation.  Set up the
        (possibly <<NULL>>) that results from this operation.  Set up the
        BFD so that future accesses know the file is open. If the <<FILE *>>
        BFD so that future accesses know the file is open. If the <<FILE *>>
        returned is <<NULL>>, then it won't have been put in the
        returned is <<NULL>>, then it won't have been put in the
        cache, so it won't have to be removed from it.
        cache, so it won't have to be removed from it.
*/
*/
 
 
FILE *
FILE *
bfd_open_file (abfd)
bfd_open_file (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  abfd->cacheable = true;       /* Allow it to be closed later.  */
  abfd->cacheable = true;       /* Allow it to be closed later.  */
 
 
  if (open_files >= BFD_CACHE_MAX_OPEN)
  if (open_files >= BFD_CACHE_MAX_OPEN)
    {
    {
      if (! close_one ())
      if (! close_one ())
        return NULL;
        return NULL;
    }
    }
 
 
  switch (abfd->direction)
  switch (abfd->direction)
    {
    {
    case read_direction:
    case read_direction:
    case no_direction:
    case no_direction:
      abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
      abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
      break;
      break;
    case both_direction:
    case both_direction:
    case write_direction:
    case write_direction:
      if (abfd->opened_once == true)
      if (abfd->opened_once == true)
        {
        {
          abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
          abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
          if (abfd->iostream == NULL)
          if (abfd->iostream == NULL)
            abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
            abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
        }
        }
      else
      else
        {
        {
          /* Create the file.
          /* Create the file.
 
 
             Some operating systems won't let us overwrite a running
             Some operating systems won't let us overwrite a running
             binary.  For them, we want to unlink the file first.
             binary.  For them, we want to unlink the file first.
 
 
             However, gcc 2.95 will create temporary files using
             However, gcc 2.95 will create temporary files using
             O_EXCL and tight permissions to prevent other users from
             O_EXCL and tight permissions to prevent other users from
             substituting other .o files during the compilation.  gcc
             substituting other .o files during the compilation.  gcc
             will then tell the assembler to use the newly created
             will then tell the assembler to use the newly created
             file as an output file.  If we unlink the file here, we
             file as an output file.  If we unlink the file here, we
             open a brief window when another user could still
             open a brief window when another user could still
             substitute a file.
             substitute a file.
 
 
             So we unlink the output file if and only if it has
             So we unlink the output file if and only if it has
             non-zero size.  */
             non-zero size.  */
#ifndef __MSDOS__
#ifndef __MSDOS__
          /* Don't do this for MSDOS: it doesn't care about overwriting
          /* Don't do this for MSDOS: it doesn't care about overwriting
             a running binary, but if this file is already open by
             a running binary, but if this file is already open by
             another BFD, we will be in deep trouble if we delete an
             another BFD, we will be in deep trouble if we delete an
             open file.  In fact, objdump does just that if invoked with
             open file.  In fact, objdump does just that if invoked with
             the --info option.  */
             the --info option.  */
          struct stat s;
          struct stat s;
 
 
          if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
          if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
            unlink (abfd->filename);
            unlink (abfd->filename);
#endif
#endif
          abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
          abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
          abfd->opened_once = true;
          abfd->opened_once = true;
        }
        }
      break;
      break;
    }
    }
 
 
  if (abfd->iostream != NULL)
  if (abfd->iostream != NULL)
    {
    {
      if (! bfd_cache_init (abfd))
      if (! bfd_cache_init (abfd))
        return NULL;
        return NULL;
    }
    }
 
 
  return (FILE *) abfd->iostream;
  return (FILE *) abfd->iostream;
}
}
 
 
/*
/*
INTERNAL_FUNCTION
INTERNAL_FUNCTION
        bfd_cache_lookup_worker
        bfd_cache_lookup_worker
 
 
SYNOPSIS
SYNOPSIS
        FILE *bfd_cache_lookup_worker(bfd *abfd);
        FILE *bfd_cache_lookup_worker(bfd *abfd);
 
 
DESCRIPTION
DESCRIPTION
        Called when the macro <<bfd_cache_lookup>> fails to find a
        Called when the macro <<bfd_cache_lookup>> fails to find a
        quick answer.  Find a file descriptor for @var{abfd}.  If
        quick answer.  Find a file descriptor for @var{abfd}.  If
        necessary, it open it.  If there are already more than
        necessary, it open it.  If there are already more than
        <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
        <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
        avoid running out of file descriptors.
        avoid running out of file descriptors.
*/
*/
 
 
FILE *
FILE *
bfd_cache_lookup_worker (abfd)
bfd_cache_lookup_worker (abfd)
     bfd *abfd;
     bfd *abfd;
{
{
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
    abort ();
    abort ();
 
 
  if (abfd->my_archive)
  if (abfd->my_archive)
    abfd = abfd->my_archive;
    abfd = abfd->my_archive;
 
 
  if (abfd->iostream != NULL)
  if (abfd->iostream != NULL)
    {
    {
      /* Move the file to the start of the cache.  */
      /* Move the file to the start of the cache.  */
      if (abfd != bfd_last_cache)
      if (abfd != bfd_last_cache)
        {
        {
          snip (abfd);
          snip (abfd);
          insert (abfd);
          insert (abfd);
        }
        }
    }
    }
  else
  else
    {
    {
      if (bfd_open_file (abfd) == NULL)
      if (bfd_open_file (abfd) == NULL)
        return NULL;
        return NULL;
      if (fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
      if (fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
        return NULL;
        return NULL;
    }
    }
 
 
  return (FILE *) abfd->iostream;
  return (FILE *) abfd->iostream;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.