Commit | Line | Data |
---|---|---|
4a81b561 DHW |
1 | /*** cache.c -- Allows you to have more bfds open than your system has fds. */ |
2 | ||
3 | /* Copyright (C) 1990, 1991 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of BFD, the Binary File Diddler. | |
6 | ||
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) | |
10 | any later version. | |
11 | ||
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. | |
16 | ||
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. */ | |
20 | ||
21 | /* $Id$ */ | |
22 | #include "sysdep.h" | |
23 | #include "bfd.h" | |
24 | #include "libbfd.h" | |
25 | ||
4a81b561 DHW |
26 | /* The maximum number of FDs opened by bfd */ |
27 | #define BFD_CACHE_MAX_OPEN 10 | |
28 | ||
29 | /* when this exceeds BFD_CACHE_MAX_OPEN, we get to work */ | |
30 | static int open_files; | |
31 | ||
32 | static bfd *cache_sentinel; /* Chain of bfds with active fds we've | |
33 | opened */ | |
4a81b561 | 34 | |
fc723380 JG |
35 | bfd *bfd_last_cache; /* Zero, or a pointer to the topmost |
36 | bfd on the chain. This is used by the | |
37 | bfd_cache_lookup() macro in libbfd.h | |
38 | to determine when it can avoid a function | |
39 | call. */ | |
40 | ||
41 | static void bfd_cache_delete(); | |
4a81b561 DHW |
42 | \f |
43 | ||
44 | static void | |
9872a49c | 45 | DEFUN_VOID(close_one) |
4a81b561 DHW |
46 | { |
47 | bfd *kill = cache_sentinel; | |
48 | if (kill == 0) /* Nothing in the cache */ | |
49 | return ; | |
50 | ||
51 | /* We can only close files that want to play this game. */ | |
52 | while (!kill->cacheable) { | |
53 | kill = kill->lru_prev; | |
54 | if (kill == cache_sentinel) /* Nobody wants to play */ | |
55 | return ; | |
56 | } | |
57 | ||
58 | kill->where = ftell((FILE *)(kill->iostream)); | |
59 | bfd_cache_delete(kill); | |
4a81b561 | 60 | } |
fc723380 | 61 | |
4a81b561 DHW |
62 | /* Cuts the bfd abfd out of the chain in the cache */ |
63 | static void | |
9872a49c SC |
64 | DEFUN(snip,(abfd), |
65 | bfd *abfd) | |
4a81b561 DHW |
66 | { |
67 | abfd->lru_prev->lru_next = abfd->lru_next; | |
68 | abfd->lru_next->lru_prev = abfd->lru_prev; | |
69 | if (cache_sentinel == abfd) cache_sentinel = (bfd *)NULL; | |
70 | } | |
71 | ||
72 | static void | |
9872a49c SC |
73 | DEFUN(bfd_cache_delete,(abfd), |
74 | bfd *abfd) | |
4a81b561 DHW |
75 | { |
76 | fclose ((FILE *)(abfd->iostream)); | |
77 | snip (abfd); | |
78 | abfd->iostream = NULL; | |
79 | open_files--; | |
9872a49c | 80 | bfd_last_cache = 0; |
4a81b561 DHW |
81 | } |
82 | ||
83 | static bfd * | |
9872a49c SC |
84 | DEFUN(insert,(x,y), |
85 | bfd *x AND | |
86 | bfd *y) | |
4a81b561 DHW |
87 | { |
88 | if (y) { | |
89 | x->lru_next = y; | |
90 | x->lru_prev = y->lru_prev; | |
91 | y->lru_prev->lru_next = x; | |
92 | y->lru_prev = x; | |
93 | ||
94 | } | |
95 | else { | |
96 | x->lru_prev = x; | |
97 | x->lru_next = x; | |
98 | } | |
99 | return x; | |
100 | } | |
101 | \f | |
102 | ||
103 | /* Initialize a BFD by putting it on the cache LRU. */ | |
104 | void | |
9872a49c SC |
105 | DEFUN(bfd_cache_init,(abfd), |
106 | bfd *abfd) | |
4a81b561 DHW |
107 | { |
108 | cache_sentinel = insert(abfd, cache_sentinel); | |
109 | } | |
110 | ||
111 | void | |
9872a49c SC |
112 | DEFUN(bfd_cache_close,(abfd), |
113 | bfd *abfd) | |
4a81b561 DHW |
114 | { |
115 | /* If this file is open then remove from the chain */ | |
116 | if (abfd->iostream) | |
117 | { | |
118 | bfd_cache_delete(abfd); | |
119 | } | |
120 | } | |
121 | \f | |
122 | /* Call the OS to open a file for this BFD. Returns the FILE * | |
123 | (possibly null) that results from this operation. Sets up the | |
124 | BFD so that future accesses know the file is open. */ | |
125 | ||
126 | FILE * | |
9872a49c SC |
127 | DEFUN(bfd_open_file, (abfd), |
128 | bfd *abfd) | |
4a81b561 DHW |
129 | { |
130 | abfd->cacheable = true; /* Allow it to be closed later. */ | |
131 | if(open_files >= BFD_CACHE_MAX_OPEN) { | |
132 | close_one(); | |
133 | } | |
134 | switch (abfd->direction) { | |
fc723380 JG |
135 | case read_direction: |
136 | case no_direction: | |
4a81b561 DHW |
137 | abfd->iostream = (char *) fopen(abfd->filename, "r"); |
138 | break; | |
fc723380 JG |
139 | case both_direction: |
140 | case write_direction: | |
4a81b561 DHW |
141 | if (abfd->opened_once == true) { |
142 | abfd->iostream = (char *) fopen(abfd->filename, "r+"); | |
143 | if (!abfd->iostream) { | |
144 | abfd->iostream = (char *) fopen(abfd->filename, "w+"); | |
145 | } | |
146 | } else { | |
147 | /*open for creat */ | |
148 | abfd->iostream = (char *) fopen(abfd->filename, "w"); | |
149 | abfd->opened_once = true; | |
150 | } | |
151 | break; | |
152 | } | |
153 | if (abfd->iostream) { | |
154 | open_files++; | |
155 | bfd_cache_init (abfd); | |
156 | } | |
157 | ||
158 | return (FILE *)(abfd->iostream); | |
159 | } | |
160 | ||
161 | /* Find a file descriptor for this BFD. If necessary, open it. | |
162 | If there are already more than BFD_CACHE_MAX_OPEN files open, try to close | |
163 | one first, to avoid running out of file descriptors. */ | |
164 | ||
165 | FILE * | |
9872a49c SC |
166 | DEFUN(bfd_cache_lookup_worker,(abfd), |
167 | bfd *abfd) | |
4a81b561 DHW |
168 | { |
169 | if (abfd->my_archive) | |
9872a49c SC |
170 | { |
171 | abfd = abfd->my_archive; | |
172 | } | |
4a81b561 DHW |
173 | /* Is this file already open .. if so then quick exit */ |
174 | if (abfd->iostream) | |
9872a49c SC |
175 | { |
176 | if (abfd != cache_sentinel) { | |
177 | /* Place onto head of lru chain */ | |
178 | snip (abfd); | |
179 | cache_sentinel = insert(abfd, cache_sentinel); | |
180 | } | |
4a81b561 | 181 | } |
4a81b561 DHW |
182 | /* This is a bfd without a stream - |
183 | so it must have been closed or never opened. | |
184 | find an empty cache entry and use it. */ | |
185 | else | |
9872a49c | 186 | { |
4a81b561 | 187 | |
9872a49c SC |
188 | if (open_files >= BFD_CACHE_MAX_OPEN) |
189 | { | |
190 | close_one(); | |
191 | } | |
4a81b561 | 192 | |
9872a49c SC |
193 | BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ; |
194 | fseek((FILE *)(abfd->iostream), abfd->where, false); | |
195 | } | |
196 | bfd_last_cache = abfd; | |
4a81b561 DHW |
197 | return (FILE *)(abfd->iostream); |
198 | } |