Initial revision
[deliverable/binutils-gdb.git] / bfd / cache.c
CommitLineData
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
5This file is part of BFD, the Binary File Diddler.
6
7BFD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 1, or (at your option)
10any later version.
11
12BFD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with BFD; see the file COPYING. If not, write to
19the 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 */
30static int open_files;
31
32static bfd *cache_sentinel; /* Chain of bfds with active fds we've
33 opened */
4a81b561 34
fc723380
JG
35bfd *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
41static void bfd_cache_delete();
4a81b561
DHW
42\f
43
44static void
9872a49c 45DEFUN_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 */
63static void
9872a49c
SC
64DEFUN(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
72static void
9872a49c
SC
73DEFUN(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
83static bfd *
9872a49c
SC
84DEFUN(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. */
104void
9872a49c
SC
105DEFUN(bfd_cache_init,(abfd),
106 bfd *abfd)
4a81b561
DHW
107{
108 cache_sentinel = insert(abfd, cache_sentinel);
109}
110
111void
9872a49c
SC
112DEFUN(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
126FILE *
9872a49c
SC
127DEFUN(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
165FILE *
9872a49c
SC
166DEFUN(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}
This page took 0.032768 seconds and 4 git commands to generate.