*** empty log message ***
[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 DEFUN_VOID(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 DEFUN(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 DEFUN(bfd_cache_delete,(abfd),
77       bfd *abfd)
78 {
79   fclose ((FILE *)(abfd->iostream));
80   snip (abfd);
81   abfd->iostream = NULL;
82   open_files--;
83   bfd_last_cache = 0;
84 }
85   
86 static bfd *
87 DEFUN(insert,(x,y),
88       bfd *x AND
89       bfd *y)
90 {
91   if (y) {
92     x->lru_next = y;
93     x->lru_prev = y->lru_prev;
94     y->lru_prev->lru_next = x;
95     y->lru_prev = x;
96
97   }
98   else {
99     x->lru_prev = x;
100     x->lru_next = x;
101   }
102   return x;
103 }
104 \f
105
106 /* Initialize a BFD by putting it on the cache LRU.  */
107 void
108 DEFUN(bfd_cache_init,(abfd),
109       bfd *abfd)
110 {
111   cache_sentinel = insert(abfd, cache_sentinel);
112 }
113
114 void
115 DEFUN(bfd_cache_close,(abfd),
116       bfd *abfd)
117 {
118   /* If this file is open then remove from the chain */
119   if (abfd->iostream) 
120     {
121       bfd_cache_delete(abfd);
122     }
123 }
124 \f
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.  */
128
129 FILE *
130 DEFUN(bfd_open_file, (abfd),
131       bfd *abfd)
132 {
133     abfd->cacheable = true;     /* Allow it to be closed later. */
134     if(open_files >= BFD_CACHE_MAX_OPEN) {
135         close_one();
136     }
137     switch (abfd->direction) {
138  case read_direction:
139  case no_direction:
140         abfd->iostream = (char *) fopen(abfd->filename, "r");
141         break;
142  case both_direction:
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+");
148             }
149         } else {
150             /*open for creat */
151             abfd->iostream = (char *) fopen(abfd->filename, "w");
152             abfd->opened_once = true;
153         }
154         break;
155     }
156     if (abfd->iostream) {
157         open_files++;
158         bfd_cache_init (abfd);
159     }
160
161     return (FILE *)(abfd->iostream);
162 }
163
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.  */
167
168 FILE *
169 DEFUN(bfd_cache_lookup_worker,(abfd),
170       bfd *abfd)
171 {
172   if (abfd->my_archive) 
173       {
174         abfd = abfd->my_archive;
175       }
176   /* Is this file already open .. if so then quick exit */
177   if (abfd->iostream) 
178       {
179         if (abfd != cache_sentinel) {
180           /* Place onto head of lru chain */
181           snip (abfd);
182           cache_sentinel = insert(abfd, cache_sentinel);
183         }
184       }
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.  */
188   else 
189       {
190
191         if (open_files >= BFD_CACHE_MAX_OPEN) 
192             {
193               close_one();
194             }
195
196         BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ;
197         fseek((FILE *)(abfd->iostream), abfd->where, false);
198       }
199   bfd_last_cache = abfd;
200   return (FILE *)(abfd->iostream);
201 }