1 /* BFD library -- caching of file descriptors.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 The file caching mechanism is embedded within BFD and allows the application to open as many
24 BFDs as it wants without regard to the underlying operating system's
25 file descriptor limit (often as low as 20 open files).
27 The module in @code{cache.c} maintains a least recently used list of
28 @code{BFD_CACHE_MAX_OPEN} files, and exports the name
29 @code{bfd_cache_lookup} which runs around and makes sure that the
30 required BFD is open. If not, then it chooses a file to close, closes
31 it and opens the one wanted, returning its file handle.
43 /*proto-internal* BFD_CACHE_MAX_OPEN
44 The maxiumum number of files which the cache will keep open at one
47 #define BFD_CACHE_MAX_OPEN 10
53 static int open_files;
55 static bfd *cache_sentinel; /* Chain of BFDs with active fds we've
58 /*proto-internal* bfd_last_cache
59 Zero, or a pointer to the topmost BFD on the chain. This is used by
60 the @code{bfd_cache_lookup} macro in @file{libbfd.h} to determine when
61 it can avoid a function call.
63 extern bfd *bfd_last_cache;
70 /*proto-internal* bfd_cache_lookup
71 Checks to see if the required BFD is the same as the last one looked
72 up. If so then it can use the iostream in the BFD with impunity, since
73 it can't have changed since the last lookup, otherwise it has to
74 perform the complicated lookup function
76 #define bfd_cache_lookup(x) \
77 ((x)==bfd_last_cache? \
78 (FILE*)(bfd_last_cache->iostream): \
79 bfd_cache_lookup_worker(x))
85 static void bfd_cache_delete();
91 bfd *kill = cache_sentinel;
92 if (kill == 0) /* Nothing in the cache */
95 /* We can only close files that want to play this game. */
96 while (!kill->cacheable) {
97 kill = kill->lru_prev;
98 if (kill == cache_sentinel) /* Nobody wants to play */
102 kill->where = ftell((FILE *)(kill->iostream));
103 bfd_cache_delete(kill);
106 /* Cuts the BFD abfd out of the chain in the cache */
111 abfd->lru_prev->lru_next = abfd->lru_next;
112 abfd->lru_next->lru_prev = abfd->lru_prev;
113 if (cache_sentinel == abfd) cache_sentinel = (bfd *)NULL;
117 DEFUN(bfd_cache_delete,(abfd),
120 fclose ((FILE *)(abfd->iostream));
122 abfd->iostream = NULL;
134 x->lru_prev = y->lru_prev;
135 y->lru_prev->lru_next = x;
149 Initialize a BFD by putting it on the cache LRU.
150 *; PROTO(void, bfd_cache_init, (bfd *));
154 DEFUN(bfd_cache_init,(abfd),
157 cache_sentinel = insert(abfd, cache_sentinel);
163 Remove the BFD from the cache. If the attatched file is open, then close it too.
164 *; PROTO(void, bfd_cache_close, (bfd *));
167 DEFUN(bfd_cache_close,(abfd),
170 /* If this file is open then remove from the chain */
173 bfd_cache_delete(abfd);
179 Call the OS to open a file for this BFD. Returns the FILE *
180 (possibly null) that results from this operation. Sets up the
181 BFD so that future accesses know the file is open. If the FILE *
182 returned is null, then there is won't have been put in the cache, so
183 it won't have to be removed from it.
184 *; PROTO(FILE *, bfd_open_file, (bfd *));
187 DEFUN(bfd_open_file, (abfd),
190 abfd->cacheable = true; /* Allow it to be closed later. */
191 if(open_files >= BFD_CACHE_MAX_OPEN) {
194 switch (abfd->direction) {
197 abfd->iostream = (char *) fopen(abfd->filename, "r");
200 case write_direction:
201 if (abfd->opened_once == true) {
202 abfd->iostream = (char *) fopen(abfd->filename, "r+");
203 if (!abfd->iostream) {
204 abfd->iostream = (char *) fopen(abfd->filename, "w+");
208 abfd->iostream = (char *) fopen(abfd->filename, "w");
209 abfd->opened_once = true;
213 if (abfd->iostream) {
215 bfd_cache_init (abfd);
218 return (FILE *)(abfd->iostream);
222 *i bfd_cache_lookup_worker
223 Called when the macro @code{bfd_cache_lookup} fails to find a quick
224 answer. Finds a file descriptor for this BFD. If necessary, it open it.
225 If there are already more than BFD_CACHE_MAX_OPEN files open, it trys to close
226 one first, to avoid running out of file descriptors.
227 *; PROTO(FILE *, bfd_cache_lookup_worker, (bfd *));
232 DEFUN(bfd_cache_lookup_worker,(abfd),
235 if (abfd->my_archive)
237 abfd = abfd->my_archive;
239 /* Is this file already open .. if so then quick exit */
242 if (abfd != cache_sentinel) {
243 /* Place onto head of lru chain */
245 cache_sentinel = insert(abfd, cache_sentinel);
248 /* This is a BFD without a stream -
249 so it must have been closed or never opened.
250 find an empty cache entry and use it. */
254 if (open_files >= BFD_CACHE_MAX_OPEN)
259 BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ;
260 fseek((FILE *)(abfd->iostream), abfd->where, false);
262 bfd_last_cache = abfd;
263 return (FILE *)(abfd->iostream);