1 /* BFD library -- caching of file descriptors.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004 Free Software Foundation, Inc.
6 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 The file caching mechanism is embedded within BFD and allows
29 the application to open as many BFDs as it wants without
30 regard to the underlying operating system's file descriptor
31 limit (often as low as 20 open files). The module in
32 <<cache.c>> maintains a least recently used list of
33 <<BFD_CACHE_MAX_OPEN>> files, and exports the name
34 <<bfd_cache_lookup>>, which runs around and makes sure that
35 the required BFD is open. If not, then it chooses a file to
36 close, closes it and opens the one wanted, returning its file
45 static bfd_boolean
bfd_cache_delete (bfd
*);
49 BFD_CACHE_MAX_OPEN macro
52 The maximum number of files which the cache will keep open at
55 .#define BFD_CACHE_MAX_OPEN 10
59 /* The number of BFD files we have open. */
61 static int open_files
;
68 extern bfd *bfd_last_cache;
71 Zero, or a pointer to the topmost BFD on the chain. This is
72 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
73 determine when it can avoid a function call.
83 Check to see if the required BFD is the same as the last one
84 looked up. If so, then it can use the stream in the BFD with
85 impunity, since it can't have changed since the last lookup;
86 otherwise, it has to perform the complicated lookup function.
88 .#define bfd_cache_lookup(x) \
89 . ((x)==bfd_last_cache? \
90 . (FILE*) (bfd_last_cache->iostream): \
91 . bfd_cache_lookup_worker(x))
95 /* Insert a BFD into the cache. */
100 if (bfd_last_cache
== NULL
)
102 abfd
->lru_next
= abfd
;
103 abfd
->lru_prev
= abfd
;
107 abfd
->lru_next
= bfd_last_cache
;
108 abfd
->lru_prev
= bfd_last_cache
->lru_prev
;
109 abfd
->lru_prev
->lru_next
= abfd
;
110 abfd
->lru_next
->lru_prev
= abfd
;
112 bfd_last_cache
= abfd
;
115 /* Remove a BFD from the cache. */
120 abfd
->lru_prev
->lru_next
= abfd
->lru_next
;
121 abfd
->lru_next
->lru_prev
= abfd
->lru_prev
;
122 if (abfd
== bfd_last_cache
)
124 bfd_last_cache
= abfd
->lru_next
;
125 if (abfd
== bfd_last_cache
)
126 bfd_last_cache
= NULL
;
130 /* We need to open a new file, and the cache is full. Find the least
131 recently used cacheable BFD and close it. */
138 if (bfd_last_cache
== NULL
)
142 for (kill
= bfd_last_cache
->lru_prev
;
144 kill
= kill
->lru_prev
)
146 if (kill
== bfd_last_cache
)
156 /* There are no open cacheable BFD's. */
160 kill
->where
= real_ftell ((FILE *) kill
->iostream
);
162 return bfd_cache_delete (kill
);
165 /* Close a BFD and remove it from the cache. */
168 bfd_cache_delete (bfd
*abfd
)
172 if (fclose ((FILE *) abfd
->iostream
) == 0)
177 bfd_set_error (bfd_error_system_call
);
182 abfd
->iostream
= NULL
;
193 bfd_boolean bfd_cache_init (bfd *abfd);
196 Add a newly opened BFD to the cache.
200 bfd_cache_init (bfd
*abfd
)
202 BFD_ASSERT (abfd
->iostream
!= NULL
);
203 if (open_files
>= BFD_CACHE_MAX_OPEN
)
218 bfd_boolean bfd_cache_close (bfd *abfd);
221 Remove the BFD @var{abfd} from the cache. If the attached file is open,
225 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
226 returned if all is well.
230 bfd_cache_close (bfd
*abfd
)
232 if (abfd
->iostream
== NULL
233 || (abfd
->flags
& BFD_IN_MEMORY
) != 0)
236 return bfd_cache_delete (abfd
);
244 FILE* bfd_open_file (bfd *abfd);
247 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
248 (possibly <<NULL>>) that results from this operation. Set up the
249 BFD so that future accesses know the file is open. If the <<FILE *>>
250 returned is <<NULL>>, then it won't have been put in the
251 cache, so it won't have to be removed from it.
255 bfd_open_file (bfd
*abfd
)
257 abfd
->cacheable
= TRUE
; /* Allow it to be closed later. */
259 if (open_files
>= BFD_CACHE_MAX_OPEN
)
265 switch (abfd
->direction
)
269 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_RB
);
272 case write_direction
:
273 if (abfd
->opened_once
)
275 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_RUB
);
276 if (abfd
->iostream
== NULL
)
277 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_WUB
);
283 Some operating systems won't let us overwrite a running
284 binary. For them, we want to unlink the file first.
286 However, gcc 2.95 will create temporary files using
287 O_EXCL and tight permissions to prevent other users from
288 substituting other .o files during the compilation. gcc
289 will then tell the assembler to use the newly created
290 file as an output file. If we unlink the file here, we
291 open a brief window when another user could still
294 So we unlink the output file if and only if it has
297 /* Don't do this for MSDOS: it doesn't care about overwriting
298 a running binary, but if this file is already open by
299 another BFD, we will be in deep trouble if we delete an
300 open file. In fact, objdump does just that if invoked with
301 the --info option. */
304 if (stat (abfd
->filename
, &s
) == 0 && s
.st_size
!= 0)
305 unlink (abfd
->filename
);
307 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_WUB
);
308 abfd
->opened_once
= TRUE
;
313 if (abfd
->iostream
!= NULL
)
315 if (! bfd_cache_init (abfd
))
319 return (FILE *) abfd
->iostream
;
324 bfd_cache_lookup_worker
327 FILE *bfd_cache_lookup_worker (bfd *abfd);
330 Called when the macro <<bfd_cache_lookup>> fails to find a
331 quick answer. Find a file descriptor for @var{abfd}. If
332 necessary, it open it. If there are already more than
333 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
334 avoid running out of file descriptors.
338 bfd_cache_lookup_worker (bfd
*abfd
)
340 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
343 if (abfd
->my_archive
)
344 abfd
= abfd
->my_archive
;
346 if (abfd
->iostream
!= NULL
)
348 /* Move the file to the start of the cache. */
349 if (abfd
!= bfd_last_cache
)
357 if (bfd_open_file (abfd
) == NULL
)
359 if (abfd
->where
!= (unsigned long) abfd
->where
)
361 if (real_fseek ((FILE *) abfd
->iostream
, abfd
->where
, SEEK_SET
) != 0)
365 return (FILE *) abfd
->iostream
;