cache.c revision 78828
1169691Skan/* BFD library -- caching of file descriptors.
2169691Skan   Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001
3169691Skan   Free Software Foundation, Inc.
4169691Skan   Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
5169691Skan
6169691SkanThis file is part of BFD, the Binary File Descriptor library.
7169691Skan
8169691SkanThis program is free software; you can redistribute it and/or modify
9169691Skanit under the terms of the GNU General Public License as published by
10169691Skanthe Free Software Foundation; either version 2 of the License, or
11169691Skan(at your option) any later version.
12169691Skan
13169691SkanThis program is distributed in the hope that it will be useful,
14169691Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
15169691SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16169691SkanGNU General Public License for more details.
17169691Skan
18169691SkanYou should have received a copy of the GNU General Public License
19169691Skanalong with this program; if not, write to the Free Software
20169691SkanFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21169691Skan
22169691Skan/*
23169691SkanSECTION
24169691Skan	File caching
25169691Skan
26169691Skan	The file caching mechanism is embedded within BFD and allows
27169691Skan	the application to open as many BFDs as it wants without
28169691Skan	regard to the underlying operating system's file descriptor
29169691Skan	limit (often as low as 20 open files).  The module in
30169691Skan	<<cache.c>> maintains a least recently used list of
31169691Skan	<<BFD_CACHE_MAX_OPEN>> files, and exports the name
32169691Skan	<<bfd_cache_lookup>>, which runs around and makes sure that
33169691Skan	the required BFD is open. If not, then it chooses a file to
34169691Skan	close, closes it and opens the one wanted, returning its file
35169691Skan	handle.
36169691Skan
37169691Skan*/
38169691Skan
39169691Skan#include "bfd.h"
40169691Skan#include "sysdep.h"
41169691Skan#include "libbfd.h"
42169691Skan
43169691Skanstatic void insert PARAMS ((bfd *));
44169691Skanstatic void snip PARAMS ((bfd *));
45169691Skanstatic boolean close_one PARAMS ((void));
46169691Skanstatic boolean bfd_cache_delete PARAMS ((bfd *));
47169691Skan
48169691Skan/*
49169691SkanINTERNAL_FUNCTION
50169691Skan	BFD_CACHE_MAX_OPEN macro
51169691Skan
52169691SkanDESCRIPTION
53169691Skan	The maximum number of files which the cache will keep open at
54169691Skan	one time.
55169691Skan
56169691Skan.#define BFD_CACHE_MAX_OPEN 10
57169691Skan
58169691Skan*/
59169691Skan
60169691Skan/* The number of BFD files we have open.  */
61169691Skan
62169691Skanstatic int open_files;
63169691Skan
64169691Skan/*
65169691SkanINTERNAL_FUNCTION
66169691Skan	bfd_last_cache
67169691Skan
68169691SkanSYNOPSIS
69169691Skan	extern bfd *bfd_last_cache;
70169691Skan
71169691SkanDESCRIPTION
72169691Skan	Zero, or a pointer to the topmost BFD on the chain.  This is
73169691Skan	used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
74169691Skan	determine when it can avoid a function call.
75169691Skan*/
76169691Skan
77169691Skanbfd *bfd_last_cache;
78169691Skan
79169691Skan/*
80169691Skan  INTERNAL_FUNCTION
81169691Skan  	bfd_cache_lookup
82169691Skan
83169691Skan  DESCRIPTION
84169691Skan 	Check to see if the required BFD is the same as the last one
85169691Skan 	looked up. If so, then it can use the stream in the BFD with
86169691Skan 	impunity, since it can't have changed since the last lookup;
87169691Skan 	otherwise, it has to perform the complicated lookup function.
88169691Skan
89169691Skan  .#define bfd_cache_lookup(x) \
90169691Skan  .    ((x)==bfd_last_cache? \
91169691Skan  .      (FILE*) (bfd_last_cache->iostream): \
92169691Skan  .       bfd_cache_lookup_worker(x))
93169691Skan
94169691Skan */
95169691Skan
96169691Skan/* Insert a BFD into the cache.  */
97169691Skan
98169691Skanstatic INLINE void
99169691Skaninsert (abfd)
100169691Skan     bfd *abfd;
101169691Skan{
102169691Skan  if (bfd_last_cache == NULL)
103169691Skan    {
104169691Skan      abfd->lru_next = abfd;
105169691Skan      abfd->lru_prev = abfd;
106169691Skan    }
107169691Skan  else
108169691Skan    {
109169691Skan      abfd->lru_next = bfd_last_cache;
110169691Skan      abfd->lru_prev = bfd_last_cache->lru_prev;
111169691Skan      abfd->lru_prev->lru_next = abfd;
112169691Skan      abfd->lru_next->lru_prev = abfd;
113169691Skan    }
114169691Skan  bfd_last_cache = abfd;
115169691Skan}
116169691Skan
117169691Skan/* Remove a BFD from the cache.  */
118169691Skan
119169691Skanstatic INLINE void
120169691Skansnip (abfd)
121169691Skan     bfd *abfd;
122169691Skan{
123169691Skan  abfd->lru_prev->lru_next = abfd->lru_next;
124169691Skan  abfd->lru_next->lru_prev = abfd->lru_prev;
125169691Skan  if (abfd == bfd_last_cache)
126169691Skan    {
127169691Skan      bfd_last_cache = abfd->lru_next;
128169691Skan      if (abfd == bfd_last_cache)
129169691Skan	bfd_last_cache = NULL;
130169691Skan    }
131169691Skan}
132169691Skan
133169691Skan/* We need to open a new file, and the cache is full.  Find the least
134169691Skan   recently used cacheable BFD and close it.  */
135169691Skan
136169691Skanstatic boolean
137169691Skanclose_one ()
138169691Skan{
139169691Skan  register bfd *kill;
140169691Skan
141169691Skan  if (bfd_last_cache == NULL)
142169691Skan    kill = NULL;
143169691Skan  else
144169691Skan    {
145169691Skan      for (kill = bfd_last_cache->lru_prev;
146169691Skan	   ! kill->cacheable;
147169691Skan	   kill = kill->lru_prev)
148169691Skan	{
149169691Skan	  if (kill == bfd_last_cache)
150169691Skan	    {
151169691Skan	      kill = NULL;
152169691Skan	      break;
153169691Skan	    }
154169691Skan	}
155169691Skan    }
156169691Skan
157169691Skan  if (kill == NULL)
158169691Skan    {
159169691Skan      /* There are no open cacheable BFD's.  */
160169691Skan      return true;
161169691Skan    }
162169691Skan
163169691Skan  kill->where = ftell ((FILE *) kill->iostream);
164169691Skan
165169691Skan  return bfd_cache_delete (kill);
166169691Skan}
167169691Skan
168169691Skan/* Close a BFD and remove it from the cache.  */
169169691Skan
170169691Skanstatic boolean
171169691Skanbfd_cache_delete (abfd)
172169691Skan     bfd *abfd;
173169691Skan{
174169691Skan  boolean ret;
175169691Skan
176169691Skan  if (fclose ((FILE *) abfd->iostream) == 0)
177169691Skan    ret = true;
178169691Skan  else
179169691Skan    {
180169691Skan      ret = false;
181169691Skan      bfd_set_error (bfd_error_system_call);
182169691Skan    }
183169691Skan
184169691Skan  snip (abfd);
185169691Skan
186169691Skan  abfd->iostream = NULL;
187169691Skan  --open_files;
188169691Skan
189169691Skan  return ret;
190169691Skan}
191169691Skan
192169691Skan/*
193169691SkanINTERNAL_FUNCTION
194169691Skan	bfd_cache_init
195169691Skan
196169691SkanSYNOPSIS
197169691Skan	boolean bfd_cache_init (bfd *abfd);
198169691Skan
199169691SkanDESCRIPTION
200169691Skan	Add a newly opened BFD to the cache.
201169691Skan*/
202169691Skan
203169691Skanboolean
204169691Skanbfd_cache_init (abfd)
205169691Skan     bfd *abfd;
206169691Skan{
207169691Skan  BFD_ASSERT (abfd->iostream != NULL);
208169691Skan  if (open_files >= BFD_CACHE_MAX_OPEN)
209169691Skan    {
210169691Skan      if (! close_one ())
211169691Skan	return false;
212169691Skan    }
213169691Skan  insert (abfd);
214169691Skan  ++open_files;
215169691Skan  return true;
216169691Skan}
217169691Skan
218169691Skan/*
219169691SkanINTERNAL_FUNCTION
220169691Skan	bfd_cache_close
221169691Skan
222169691SkanSYNOPSIS
223169691Skan	boolean bfd_cache_close (bfd *abfd);
224169691Skan
225169691SkanDESCRIPTION
226169691Skan	Remove the BFD @var{abfd} from the cache. If the attached file is open,
227169691Skan	then close it too.
228169691Skan
229169691SkanRETURNS
230169691Skan	<<false>> is returned if closing the file fails, <<true>> is
231169691Skan	returned if all is well.
232169691Skan*/
233169691Skan
234169691Skanboolean
235169691Skanbfd_cache_close (abfd)
236169691Skan     bfd *abfd;
237169691Skan{
238169691Skan  if (abfd->iostream == NULL
239169691Skan      || (abfd->flags & BFD_IN_MEMORY) != 0)
240169691Skan    return true;
241169691Skan
242169691Skan  return bfd_cache_delete (abfd);
243169691Skan}
244169691Skan
245169691Skan/*
246169691SkanINTERNAL_FUNCTION
247169691Skan	bfd_open_file
248169691Skan
249169691SkanSYNOPSIS
250169691Skan	FILE* bfd_open_file(bfd *abfd);
251169691Skan
252169691SkanDESCRIPTION
253169691Skan	Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
254169691Skan	(possibly <<NULL>>) that results from this operation.  Set up the
255169691Skan	BFD so that future accesses know the file is open. If the <<FILE *>>
256169691Skan	returned is <<NULL>>, then it won't have been put in the
257169691Skan	cache, so it won't have to be removed from it.
258169691Skan*/
259169691Skan
260169691SkanFILE *
261169691Skanbfd_open_file (abfd)
262169691Skan     bfd *abfd;
263169691Skan{
264169691Skan  abfd->cacheable = true;	/* Allow it to be closed later.  */
265169691Skan
266169691Skan  if (open_files >= BFD_CACHE_MAX_OPEN)
267169691Skan    {
268169691Skan      if (! close_one ())
269169691Skan	return NULL;
270169691Skan    }
271169691Skan
272169691Skan  switch (abfd->direction)
273169691Skan    {
274169691Skan    case read_direction:
275169691Skan    case no_direction:
276169691Skan      abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
277169691Skan      break;
278169691Skan    case both_direction:
279169691Skan    case write_direction:
280169691Skan      if (abfd->opened_once == true)
281169691Skan	{
282169691Skan	  abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
283169691Skan	  if (abfd->iostream == NULL)
284169691Skan	    abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
285169691Skan	}
286169691Skan      else
287169691Skan	{
288169691Skan	  /* Create the file.
289169691Skan
290169691Skan	     Some operating systems won't let us overwrite a running
291169691Skan	     binary.  For them, we want to unlink the file first.
292169691Skan
293169691Skan	     However, gcc 2.95 will create temporary files using
294169691Skan	     O_EXCL and tight permissions to prevent other users from
295169691Skan	     substituting other .o files during the compilation.  gcc
296169691Skan	     will then tell the assembler to use the newly created
297169691Skan	     file as an output file.  If we unlink the file here, we
298169691Skan	     open a brief window when another user could still
299169691Skan	     substitute a file.
300169691Skan
301169691Skan	     So we unlink the output file if and only if it has
302169691Skan	     non-zero size.  */
303#ifndef __MSDOS__
304	  /* Don't do this for MSDOS: it doesn't care about overwriting
305	     a running binary, but if this file is already open by
306	     another BFD, we will be in deep trouble if we delete an
307	     open file.  In fact, objdump does just that if invoked with
308	     the --info option.  */
309	  struct stat s;
310
311	  if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
312	    unlink (abfd->filename);
313#endif
314	  abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
315	  abfd->opened_once = true;
316	}
317      break;
318    }
319
320  if (abfd->iostream != NULL)
321    {
322      if (! bfd_cache_init (abfd))
323	return NULL;
324    }
325
326  return (FILE *) abfd->iostream;
327}
328
329/*
330INTERNAL_FUNCTION
331	bfd_cache_lookup_worker
332
333SYNOPSIS
334	FILE *bfd_cache_lookup_worker(bfd *abfd);
335
336DESCRIPTION
337	Called when the macro <<bfd_cache_lookup>> fails to find a
338	quick answer.  Find a file descriptor for @var{abfd}.  If
339	necessary, it open it.  If there are already more than
340	<<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
341	avoid running out of file descriptors.
342*/
343
344FILE *
345bfd_cache_lookup_worker (abfd)
346     bfd *abfd;
347{
348  if ((abfd->flags & BFD_IN_MEMORY) != 0)
349    abort ();
350
351  if (abfd->my_archive)
352    abfd = abfd->my_archive;
353
354  if (abfd->iostream != NULL)
355    {
356      /* Move the file to the start of the cache.  */
357      if (abfd != bfd_last_cache)
358	{
359	  snip (abfd);
360	  insert (abfd);
361	}
362    }
363  else
364    {
365      if (bfd_open_file (abfd) == NULL)
366	return NULL;
367      if (fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
368	return NULL;
369    }
370
371  return (FILE *) abfd->iostream;
372}
373