1 /*** cache.c -- Allows you to have more bfds open than your system has fds. */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD 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 1, or (at your option)
12 BFD 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 BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* These declarations should not be needed; libbfd.h's inclusion should
33 /* The maximum number of FDs opened by bfd */
34 #define BFD_CACHE_MAX_OPEN 10
36 /* when this exceeds BFD_CACHE_MAX_OPEN, we get to work */
37 static int open_files
;
39 static bfd
*cache_sentinel
; /* Chain of bfds with active fds we've
50 bfd
*kill
= cache_sentinel
;
51 if (kill
== 0) /* Nothing in the cache */
54 /* We can only close files that want to play this game. */
55 while (!kill
->cacheable
) {
56 kill
= kill
->lru_prev
;
57 if (kill
== cache_sentinel
) /* Nobody wants to play */
61 kill
->where
= ftell((FILE *)(kill
->iostream
));
62 bfd_cache_delete(kill
);
65 /* Cuts the bfd abfd out of the chain in the cache */
70 abfd
->lru_prev
->lru_next
= abfd
->lru_next
;
71 abfd
->lru_next
->lru_prev
= abfd
->lru_prev
;
72 if (cache_sentinel
== abfd
) cache_sentinel
= (bfd
*)NULL
;
76 bfd_cache_delete (abfd
)
79 fclose ((FILE *)(abfd
->iostream
));
81 abfd
->iostream
= NULL
;
92 x
->lru_prev
= y
->lru_prev
;
93 y
->lru_prev
->lru_next
= x
;
105 /* Initialize a BFD by putting it on the cache LRU. */
110 cache_sentinel
= insert(abfd
, cache_sentinel
);
114 bfd_cache_close(abfd
)
117 /* If this file is open then remove from the chain */
120 bfd_cache_delete(abfd
);
124 /* Call the OS to open a file for this BFD. Returns the FILE *
125 (possibly null) that results from this operation. Sets up the
126 BFD so that future accesses know the file is open. */
132 abfd
->cacheable
= true; /* Allow it to be closed later. */
133 if(open_files
>= BFD_CACHE_MAX_OPEN
) {
136 switch (abfd
->direction
) {
139 abfd
->iostream
= (char *) fopen(abfd
->filename
, "r");
142 case write_direction
:
143 if (abfd
->opened_once
== true) {
144 abfd
->iostream
= (char *) fopen(abfd
->filename
, "r+");
145 if (!abfd
->iostream
) {
146 abfd
->iostream
= (char *) fopen(abfd
->filename
, "w+");
150 abfd
->iostream
= (char *) fopen(abfd
->filename
, "w");
151 abfd
->opened_once
= true;
155 if (abfd
->iostream
) {
157 bfd_cache_init (abfd
);
160 return (FILE *)(abfd
->iostream
);
163 /* Find a file descriptor for this BFD. If necessary, open it.
164 If there are already more than BFD_CACHE_MAX_OPEN files open, try to close
165 one first, to avoid running out of file descriptors. */
168 bfd_cache_lookup_worker (abfd
)
171 if (abfd
->my_archive
)
173 abfd
= abfd
->my_archive
;
175 /* Is this file already open .. if so then quick exit */
178 if (abfd
!= cache_sentinel
) {
179 /* Place onto head of lru chain */
181 cache_sentinel
= insert(abfd
, cache_sentinel
);
184 /* This is a bfd without a stream -
185 so it must have been closed or never opened.
186 find an empty cache entry and use it. */
190 if (open_files
>= BFD_CACHE_MAX_OPEN
)
195 BFD_ASSERT(bfd_open_file (abfd
) != (FILE *)NULL
) ;
196 fseek((FILE *)(abfd
->iostream
), abfd
->where
, false);
198 bfd_last_cache
= abfd
;
199 return (FILE *)(abfd
->iostream
);
This page took 0.03648 seconds and 5 git commands to generate.