This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / bfd / cache.c
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
26 /* These declarations should not be needed; libbfd.h's inclusion should
27    have handled it.
28    int fclose();
29    int fseek();
30 */
31
32
33 /* The maximum number of FDs opened by bfd */
34 #define BFD_CACHE_MAX_OPEN 10
35
36 /* when this exceeds BFD_CACHE_MAX_OPEN, we get to work */
37 static int open_files;
38
39 static bfd *cache_sentinel;     /* Chain of bfds with active fds we've
40                                    opened */
41 static void
42 bfd_cache_delete();
43
44 bfd *bfd_last_cache;
45 \f
46
47 static void
48 close_one()
49 {
50     bfd *kill = cache_sentinel;
51     if (kill == 0)              /* Nothing in the cache */
52         return ;
53
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 */
58            return ;
59     }
60
61     kill->where = ftell((FILE *)(kill->iostream));
62     bfd_cache_delete(kill);
63
64 }
65 /* Cuts the bfd abfd out of the chain in the cache */
66 static void 
67 snip (abfd)
68      bfd *abfd;
69 {
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;
73 }
74
75 static void
76 bfd_cache_delete (abfd)
77      bfd *abfd;
78 {
79   fclose ((FILE *)(abfd->iostream));
80   snip (abfd);
81   abfd->iostream = NULL;
82   open_files--;
83 }
84   
85 static bfd *
86 insert(x,y)
87 bfd *x;
88 bfd *y;
89 {
90   if (y) {
91     x->lru_next = y;
92     x->lru_prev = y->lru_prev;
93     y->lru_prev->lru_next = x;
94     y->lru_prev = x;
95
96   }
97   else {
98     x->lru_prev = x;
99     x->lru_next = x;
100   }
101   return x;
102 }
103 \f
104
105 /* Initialize a BFD by putting it on the cache LRU.  */
106 void
107 bfd_cache_init(abfd)
108 bfd *abfd;
109 {
110   cache_sentinel = insert(abfd, cache_sentinel);
111 }
112
113 void
114 bfd_cache_close(abfd)
115 bfd *abfd;
116 {
117   /* If this file is open then remove from the chain */
118   if (abfd->iostream) 
119     {
120       bfd_cache_delete(abfd);
121     }
122 }
123 \f
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.  */
127
128 FILE *
129 bfd_open_file (abfd)
130      bfd *abfd;
131 {
132     abfd->cacheable = true;     /* Allow it to be closed later. */
133     if(open_files >= BFD_CACHE_MAX_OPEN) {
134         close_one();
135     }
136     switch (abfd->direction) {
137  case read_direction:
138  case no_direction:
139         abfd->iostream = (char *) fopen(abfd->filename, "r");
140         break;
141  case both_direction:
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+");
147             }
148         } else {
149             /*open for creat */
150             abfd->iostream = (char *) fopen(abfd->filename, "w");
151             abfd->opened_once = true;
152         }
153         break;
154     }
155     if (abfd->iostream) {
156         open_files++;
157         bfd_cache_init (abfd);
158     }
159
160     return (FILE *)(abfd->iostream);
161 }
162
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.  */
166
167 FILE *
168 bfd_cache_lookup_worker (abfd)
169      bfd *abfd;
170 {
171   if (abfd->my_archive) 
172     {
173       abfd = abfd->my_archive;
174     }
175   /* Is this file already open .. if so then quick exit */
176   if (abfd->iostream) 
177     {
178       if (abfd != cache_sentinel) {
179         /* Place onto head of lru chain */
180         snip (abfd);
181         cache_sentinel = insert(abfd, cache_sentinel);
182       }
183     }
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.  */
187   else 
188     {
189
190       if (open_files >= BFD_CACHE_MAX_OPEN) 
191         {
192         close_one();
193     }
194
195       BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ;
196       fseek((FILE *)(abfd->iostream), abfd->where, false);
197     }
198 bfd_last_cache = abfd;
199   return (FILE *)(abfd->iostream);
200 }