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 DEFUN(bfd_cache_delete
,(abfd
),
79 fclose ((FILE *)(abfd
->iostream
));
81 abfd
->iostream
= NULL
;
93 x
->lru_prev
= y
->lru_prev
;
94 y
->lru_prev
->lru_next
= x
;
106 /* Initialize a BFD by putting it on the cache LRU. */
108 DEFUN(bfd_cache_init
,(abfd
),
111 cache_sentinel
= insert(abfd
, cache_sentinel
);
115 DEFUN(bfd_cache_close
,(abfd
),
118 /* If this file is open then remove from the chain */
121 bfd_cache_delete(abfd
);
125 /* Call the OS to open a file for this BFD. Returns the FILE *
126 (possibly null) that results from this operation. Sets up the
127 BFD so that future accesses know the file is open. */
130 DEFUN(bfd_open_file
, (abfd
),
133 abfd
->cacheable
= true; /* Allow it to be closed later. */
134 if(open_files
>= BFD_CACHE_MAX_OPEN
) {
137 switch (abfd
->direction
) {
140 abfd
->iostream
= (char *) fopen(abfd
->filename
, "r");
143 case write_direction
:
144 if (abfd
->opened_once
== true) {
145 abfd
->iostream
= (char *) fopen(abfd
->filename
, "r+");
146 if (!abfd
->iostream
) {
147 abfd
->iostream
= (char *) fopen(abfd
->filename
, "w+");
151 abfd
->iostream
= (char *) fopen(abfd
->filename
, "w");
152 abfd
->opened_once
= true;
156 if (abfd
->iostream
) {
158 bfd_cache_init (abfd
);
161 return (FILE *)(abfd
->iostream
);
164 /* Find a file descriptor for this BFD. If necessary, open it.
165 If there are already more than BFD_CACHE_MAX_OPEN files open, try to close
166 one first, to avoid running out of file descriptors. */
169 DEFUN(bfd_cache_lookup_worker
,(abfd
),
172 if (abfd
->my_archive
)
174 abfd
= abfd
->my_archive
;
176 /* Is this file already open .. if so then quick exit */
179 if (abfd
!= cache_sentinel
) {
180 /* Place onto head of lru chain */
182 cache_sentinel
= insert(abfd
, cache_sentinel
);
185 /* This is a bfd without a stream -
186 so it must have been closed or never opened.
187 find an empty cache entry and use it. */
191 if (open_files
>= BFD_CACHE_MAX_OPEN
)
196 BFD_ASSERT(bfd_open_file (abfd
) != (FILE *)NULL
) ;
197 fseek((FILE *)(abfd
->iostream
), abfd
->where
, false);
199 bfd_last_cache
= abfd
;
200 return (FILE *)(abfd
->iostream
);
This page took 0.034099 seconds and 5 git commands to generate.