daily update
[deliverable/binutils-gdb.git] / bfd / cache.c
CommitLineData
252b5132 1/* BFD library -- caching of file descriptors.
c58b9523 2 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002, 2003
5af11cab 3 Free Software Foundation, Inc.
252b5132
RH
4 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/*
23SECTION
24 File caching
25
26 The file caching mechanism is embedded within BFD and allows
27 the application to open as many BFDs as it wants without
28 regard to the underlying operating system's file descriptor
29 limit (often as low as 20 open files). The module in
30 <<cache.c>> maintains a least recently used list of
31 <<BFD_CACHE_MAX_OPEN>> files, and exports the name
32 <<bfd_cache_lookup>>, which runs around and makes sure that
33 the required BFD is open. If not, then it chooses a file to
34 close, closes it and opens the one wanted, returning its file
e60b52c6 35 handle.
252b5132
RH
36
37*/
38
39#include "bfd.h"
40#include "sysdep.h"
41#include "libbfd.h"
42
c58b9523 43static bfd_boolean bfd_cache_delete (bfd *);
252b5132
RH
44
45/*
46INTERNAL_FUNCTION
47 BFD_CACHE_MAX_OPEN macro
48
49DESCRIPTION
50 The maximum number of files which the cache will keep open at
51 one time.
52
53.#define BFD_CACHE_MAX_OPEN 10
54
55*/
56
57/* The number of BFD files we have open. */
58
59static int open_files;
60
61/*
62INTERNAL_FUNCTION
63 bfd_last_cache
64
65SYNOPSIS
66 extern bfd *bfd_last_cache;
67
68DESCRIPTION
69 Zero, or a pointer to the topmost BFD on the chain. This is
70 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
71 determine when it can avoid a function call.
72*/
73
74bfd *bfd_last_cache;
75
76/*
77 INTERNAL_FUNCTION
78 bfd_cache_lookup
e60b52c6 79
252b5132
RH
80 DESCRIPTION
81 Check to see if the required BFD is the same as the last one
82 looked up. If so, then it can use the stream in the BFD with
83 impunity, since it can't have changed since the last lookup;
84 otherwise, it has to perform the complicated lookup function.
e60b52c6 85
252b5132
RH
86 .#define bfd_cache_lookup(x) \
87 . ((x)==bfd_last_cache? \
e60b52c6 88 . (FILE*) (bfd_last_cache->iostream): \
252b5132 89 . bfd_cache_lookup_worker(x))
e60b52c6 90
252b5132
RH
91 */
92
93/* Insert a BFD into the cache. */
94
c58b9523
AM
95static void
96insert (bfd *abfd)
252b5132
RH
97{
98 if (bfd_last_cache == NULL)
99 {
100 abfd->lru_next = abfd;
101 abfd->lru_prev = abfd;
102 }
103 else
104 {
105 abfd->lru_next = bfd_last_cache;
106 abfd->lru_prev = bfd_last_cache->lru_prev;
107 abfd->lru_prev->lru_next = abfd;
108 abfd->lru_next->lru_prev = abfd;
109 }
110 bfd_last_cache = abfd;
111}
112
113/* Remove a BFD from the cache. */
114
c58b9523
AM
115static void
116snip (bfd *abfd)
252b5132
RH
117{
118 abfd->lru_prev->lru_next = abfd->lru_next;
119 abfd->lru_next->lru_prev = abfd->lru_prev;
120 if (abfd == bfd_last_cache)
121 {
122 bfd_last_cache = abfd->lru_next;
123 if (abfd == bfd_last_cache)
124 bfd_last_cache = NULL;
125 }
126}
127
128/* We need to open a new file, and the cache is full. Find the least
129 recently used cacheable BFD and close it. */
130
b34976b6 131static bfd_boolean
c58b9523 132close_one (void)
252b5132
RH
133{
134 register bfd *kill;
135
136 if (bfd_last_cache == NULL)
137 kill = NULL;
138 else
139 {
140 for (kill = bfd_last_cache->lru_prev;
141 ! kill->cacheable;
142 kill = kill->lru_prev)
143 {
144 if (kill == bfd_last_cache)
145 {
146 kill = NULL;
147 break;
148 }
149 }
150 }
151
152 if (kill == NULL)
153 {
154 /* There are no open cacheable BFD's. */
b34976b6 155 return TRUE;
252b5132
RH
156 }
157
158 kill->where = ftell ((FILE *) kill->iostream);
159
160 return bfd_cache_delete (kill);
161}
162
163/* Close a BFD and remove it from the cache. */
164
b34976b6 165static bfd_boolean
c58b9523 166bfd_cache_delete (bfd *abfd)
252b5132 167{
b34976b6 168 bfd_boolean ret;
252b5132
RH
169
170 if (fclose ((FILE *) abfd->iostream) == 0)
b34976b6 171 ret = TRUE;
252b5132
RH
172 else
173 {
b34976b6 174 ret = FALSE;
252b5132
RH
175 bfd_set_error (bfd_error_system_call);
176 }
177
178 snip (abfd);
179
180 abfd->iostream = NULL;
181 --open_files;
182
183 return ret;
184}
185
186/*
187INTERNAL_FUNCTION
188 bfd_cache_init
189
190SYNOPSIS
b34976b6 191 bfd_boolean bfd_cache_init (bfd *abfd);
252b5132
RH
192
193DESCRIPTION
194 Add a newly opened BFD to the cache.
195*/
196
b34976b6 197bfd_boolean
c58b9523 198bfd_cache_init (bfd *abfd)
252b5132
RH
199{
200 BFD_ASSERT (abfd->iostream != NULL);
201 if (open_files >= BFD_CACHE_MAX_OPEN)
202 {
203 if (! close_one ())
b34976b6 204 return FALSE;
252b5132
RH
205 }
206 insert (abfd);
207 ++open_files;
b34976b6 208 return TRUE;
252b5132
RH
209}
210
211/*
212INTERNAL_FUNCTION
213 bfd_cache_close
214
215SYNOPSIS
b34976b6 216 bfd_boolean bfd_cache_close (bfd *abfd);
252b5132
RH
217
218DESCRIPTION
219 Remove the BFD @var{abfd} from the cache. If the attached file is open,
220 then close it too.
221
222RETURNS
b34976b6 223 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
224 returned if all is well.
225*/
226
b34976b6 227bfd_boolean
c58b9523 228bfd_cache_close (bfd *abfd)
252b5132
RH
229{
230 if (abfd->iostream == NULL
231 || (abfd->flags & BFD_IN_MEMORY) != 0)
b34976b6 232 return TRUE;
252b5132
RH
233
234 return bfd_cache_delete (abfd);
235}
236
237/*
238INTERNAL_FUNCTION
239 bfd_open_file
240
241SYNOPSIS
c58b9523 242 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
243
244DESCRIPTION
245 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
246 (possibly <<NULL>>) that results from this operation. Set up the
247 BFD so that future accesses know the file is open. If the <<FILE *>>
248 returned is <<NULL>>, then it won't have been put in the
249 cache, so it won't have to be removed from it.
250*/
251
252FILE *
c58b9523 253bfd_open_file (bfd *abfd)
252b5132 254{
b34976b6 255 abfd->cacheable = TRUE; /* Allow it to be closed later. */
252b5132
RH
256
257 if (open_files >= BFD_CACHE_MAX_OPEN)
258 {
259 if (! close_one ())
260 return NULL;
261 }
262
263 switch (abfd->direction)
264 {
265 case read_direction:
266 case no_direction:
267 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
268 break;
269 case both_direction:
270 case write_direction:
82e51918 271 if (abfd->opened_once)
252b5132
RH
272 {
273 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
274 if (abfd->iostream == NULL)
275 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
276 }
277 else
278 {
9e422a2e
ILT
279 /* Create the file.
280
281 Some operating systems won't let us overwrite a running
282 binary. For them, we want to unlink the file first.
283
284 However, gcc 2.95 will create temporary files using
285 O_EXCL and tight permissions to prevent other users from
286 substituting other .o files during the compilation. gcc
287 will then tell the assembler to use the newly created
288 file as an output file. If we unlink the file here, we
289 open a brief window when another user could still
290 substitute a file.
291
292 So we unlink the output file if and only if it has
293 non-zero size. */
5af11cab
AM
294#ifndef __MSDOS__
295 /* Don't do this for MSDOS: it doesn't care about overwriting
296 a running binary, but if this file is already open by
297 another BFD, we will be in deep trouble if we delete an
298 open file. In fact, objdump does just that if invoked with
299 the --info option. */
9e422a2e
ILT
300 struct stat s;
301
302 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
303 unlink (abfd->filename);
5af11cab 304#endif
c46b7515 305 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
b34976b6 306 abfd->opened_once = TRUE;
252b5132
RH
307 }
308 break;
309 }
310
311 if (abfd->iostream != NULL)
312 {
313 if (! bfd_cache_init (abfd))
314 return NULL;
315 }
316
317 return (FILE *) abfd->iostream;
318}
319
320/*
321INTERNAL_FUNCTION
322 bfd_cache_lookup_worker
323
324SYNOPSIS
c58b9523 325 FILE *bfd_cache_lookup_worker (bfd *abfd);
252b5132
RH
326
327DESCRIPTION
328 Called when the macro <<bfd_cache_lookup>> fails to find a
329 quick answer. Find a file descriptor for @var{abfd}. If
330 necessary, it open it. If there are already more than
331 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
e60b52c6 332 avoid running out of file descriptors.
252b5132
RH
333*/
334
335FILE *
c58b9523 336bfd_cache_lookup_worker (bfd *abfd)
252b5132
RH
337{
338 if ((abfd->flags & BFD_IN_MEMORY) != 0)
339 abort ();
340
e60b52c6 341 if (abfd->my_archive)
252b5132
RH
342 abfd = abfd->my_archive;
343
344 if (abfd->iostream != NULL)
345 {
346 /* Move the file to the start of the cache. */
347 if (abfd != bfd_last_cache)
348 {
349 snip (abfd);
350 insert (abfd);
351 }
352 }
353 else
354 {
355 if (bfd_open_file (abfd) == NULL)
356 return NULL;
dc810e39
AM
357 if (abfd->where != (unsigned long) abfd->where)
358 return NULL;
359 if (fseek ((FILE *) abfd->iostream, (long) abfd->where, SEEK_SET) != 0)
252b5132
RH
360 return NULL;
361 }
362
363 return (FILE *) abfd->iostream;
364}
This page took 0.254514 seconds and 4 git commands to generate.