* elf32-arm.c (elf32_arm_check_relocs): Correct symbian_p test.
[deliverable/binutils-gdb.git] / gold / descriptors.cc
CommitLineData
2a00e4fb
ILT
1// descriptors.cc -- manage file descriptors for gold
2
0f7c0701 3// Copyright 2008, 2009 Free Software Foundation, Inc.
2a00e4fb
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
25#include <cerrno>
26#include <cstring>
27#include <fcntl.h>
28#include <unistd.h>
29
30#include "parameters.h"
31#include "gold-threads.h"
32#include "descriptors.h"
33
34namespace gold
35{
36
37// Class Descriptors.
38
39// The default for limit_ is meant to simply be large. It gets
40// adjusted downward if we run out of file descriptors.
41
42Descriptors::Descriptors()
43 : lock_(NULL), open_descriptors_(), stack_top_(-1), current_(0),
44 limit_(8192 - 16)
45{
46 this->open_descriptors_.reserve(128);
47}
48
49// Open a file.
50
51int
52Descriptors::open(int descriptor, const char* name, int flags, int mode)
53{
54 // We don't initialize this until we are called, because we can't
55 // initialize a Lock until we have parsed the options to find out
56 // whether we are running with threads. We can be called before
57 // options are valid when reading a linker script.
58 if (this->lock_ == NULL)
59 {
60 if (parameters->options_valid())
61 this->lock_ = new Lock();
62 else
63 gold_assert(descriptor < 0);
64 }
65
66 if (descriptor >= 0)
67 {
68 Hold_lock hl(*this->lock_);
69
70 gold_assert(static_cast<size_t>(descriptor)
71 < this->open_descriptors_.size());
72 Open_descriptor* pod = &this->open_descriptors_[descriptor];
73 if (pod->name == name
74 || (pod->name != NULL && strcmp(pod->name, name) == 0))
75 {
76 gold_assert(!pod->inuse);
77 pod->inuse = true;
61edd21f
ILT
78 if (descriptor == this->stack_top_)
79 {
80 this->stack_top_ = pod->stack_next;
81 pod->stack_next = -1;
82 pod->is_on_stack = false;
83 }
2a00e4fb
ILT
84 return descriptor;
85 }
86 }
87
88 while (true)
89 {
90 int new_descriptor = ::open(name, flags, mode);
91 if (new_descriptor < 0
92 && errno != ENFILE
93 && errno != EMFILE)
94 {
95 if (descriptor >= 0 && errno == ENOENT)
96 {
97 {
98 Hold_lock hl(*this->lock_);
99
100 gold_error(_("file %s was removed during the link"),
101 this->open_descriptors_[descriptor].name);
102 }
103
104 errno = ENOENT;
105 }
106
107 return new_descriptor;
108 }
109
110 if (new_descriptor >= 0)
111 {
112 Hold_optional_lock hl(this->lock_);
113
114 if (static_cast<size_t>(new_descriptor)
115 >= this->open_descriptors_.size())
116 this->open_descriptors_.resize(new_descriptor + 64);
117
118 Open_descriptor* pod = &this->open_descriptors_[new_descriptor];
119 pod->name = name;
120 pod->stack_next = -1;
121 pod->inuse = true;
122 pod->is_write = (flags & O_ACCMODE) != O_RDONLY;
61edd21f 123 pod->is_on_stack = false;
2a00e4fb 124
0f7c0701 125 ++this->current_;
2a00e4fb
ILT
126 if (this->current_ >= this->limit_)
127 this->close_some_descriptor();
128
129 return new_descriptor;
130 }
131
132 // We ran out of file descriptors.
133 {
134 Hold_optional_lock hl(this->lock_);
135
136 this->limit_ = this->current_ - 16;
137 if (this->limit_ < 8)
138 this->limit_ = 8;
139 if (!this->close_some_descriptor())
140 gold_fatal(_("out of file descriptors and couldn't close any"));
141 }
142 }
143}
144
145// Release a descriptor.
146
147void
148Descriptors::release(int descriptor, bool permanent)
149{
150 Hold_optional_lock hl(this->lock_);
151
152 gold_assert(descriptor >= 0
153 && (static_cast<size_t>(descriptor)
154 < this->open_descriptors_.size()));
155 Open_descriptor* pod = &this->open_descriptors_[descriptor];
156
157 if (permanent
158 || (this->current_ > this->limit_ && !pod->is_write))
159 {
160 if (::close(descriptor) < 0)
161 gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
162 pod->name = NULL;
163 --this->current_;
164 }
165 else
166 {
167 pod->inuse = false;
61edd21f 168 if (!pod->is_write && !pod->is_on_stack)
2a00e4fb
ILT
169 {
170 pod->stack_next = this->stack_top_;
171 this->stack_top_ = descriptor;
61edd21f 172 pod->is_on_stack = true;
2a00e4fb
ILT
173 }
174 }
175}
176
177// Close some descriptor. The lock is held when this is called. We
178// close the descriptor on the top of the free stack. Note that this
179// is the opposite of an LRU algorithm--we close the most recently
180// used descriptor. That is because the linker tends to cycle through
181// all the files; after we release a file, we are unlikely to need it
182// again until we have looked at all the other files. Return true if
183// we closed a descriptor.
184
185bool
186Descriptors::close_some_descriptor()
187{
188 int last = -1;
189 int i = this->stack_top_;
190 while (i >= 0)
191 {
192 gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size());
193 Open_descriptor* pod = &this->open_descriptors_[i];
194 if (!pod->inuse && !pod->is_write)
195 {
196 if (::close(i) < 0)
197 gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
198 --this->current_;
199 pod->name = NULL;
200 if (last < 0)
201 this->stack_top_ = pod->stack_next;
202 else
203 this->open_descriptors_[last].stack_next = pod->stack_next;
61edd21f
ILT
204 pod->stack_next = -1;
205 pod->is_on_stack = false;
2a00e4fb
ILT
206 return true;
207 }
208 last = i;
209 i = pod->stack_next;
210 }
211
212 // We couldn't find any descriptors to close. This is weird but not
213 // necessarily an error.
214 return false;
215}
216
217// The single global variable which manages descriptors.
218
219Descriptors descriptors;
220
221} // End namespace gold.
This page took 0.050275 seconds and 4 git commands to generate.