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 /* The maximum number of FDs opened by bfd */
27 #define BFD_CACHE_MAX_OPEN 10
29 /* when this exceeds BFD_CACHE_MAX_OPEN, we get to work */
30 static int open_files
;
32 static bfd
*cache_sentinel
; /* Chain of bfds with active fds we've
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
41 static void bfd_cache_delete();
47 bfd
*kill
= cache_sentinel
;
48 if (kill
== 0) /* Nothing in the cache */
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 */
58 kill
->where
= ftell((FILE *)(kill
->iostream
));
59 bfd_cache_delete(kill
);
62 /* Cuts the bfd abfd out of the chain in the cache */
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
;
73 DEFUN(bfd_cache_delete
,(abfd
),
76 fclose ((FILE *)(abfd
->iostream
));
78 abfd
->iostream
= NULL
;
90 x
->lru_prev
= y
->lru_prev
;
91 y
->lru_prev
->lru_next
= x
;
103 /* Initialize a BFD by putting it on the cache LRU. */
105 DEFUN(bfd_cache_init
,(abfd
),
108 cache_sentinel
= insert(abfd
, cache_sentinel
);
112 DEFUN(bfd_cache_close
,(abfd
),
115 /* If this file is open then remove from the chain */
118 bfd_cache_delete(abfd
);
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. */
127 DEFUN(bfd_open_file
, (abfd
),
130 abfd
->cacheable
= true; /* Allow it to be closed later. */
131 if(open_files
>= BFD_CACHE_MAX_OPEN
) {
134 switch (abfd
->direction
) {
137 abfd
->iostream
= (char *) fopen(abfd
->filename
, "r");
140 case write_direction
:
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+");
148 abfd
->iostream
= (char *) fopen(abfd
->filename
, "w");
149 abfd
->opened_once
= true;
153 if (abfd
->iostream
) {
155 bfd_cache_init (abfd
);
158 return (FILE *)(abfd
->iostream
);
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. */
166 DEFUN(bfd_cache_lookup_worker
,(abfd
),
169 if (abfd
->my_archive
)
171 abfd
= abfd
->my_archive
;
173 /* Is this file already open .. if so then quick exit */
176 if (abfd
!= cache_sentinel
) {
177 /* Place onto head of lru chain */
179 cache_sentinel
= insert(abfd
, cache_sentinel
);
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. */
188 if (open_files
>= BFD_CACHE_MAX_OPEN
)
193 BFD_ASSERT(bfd_open_file (abfd
) != (FILE *)NULL
) ;
194 fseek((FILE *)(abfd
->iostream
), abfd
->where
, false);
196 bfd_last_cache
= abfd
;
197 return (FILE *)(abfd
->iostream
);
This page took 0.033145 seconds and 4 git commands to generate.