1 // descriptors.cc -- manage file descriptors for gold
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
30 #include "parameters.h"
31 #include "gold-threads.h"
32 #include "descriptors.h"
39 // The default for limit_ is meant to simply be large. It gets
40 // adjusted downward if we run out of file descriptors.
42 Descriptors::Descriptors()
43 : lock_(NULL
), open_descriptors_(), stack_top_(-1), current_(0),
46 this->open_descriptors_
.reserve(128);
52 Descriptors::open(int descriptor
, const char* name
, int flags
, int mode
)
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
)
60 if (parameters
->options_valid())
61 this->lock_
= new Lock();
63 gold_assert(descriptor
< 0);
68 Hold_lock
hl(*this->lock_
);
70 gold_assert(static_cast<size_t>(descriptor
)
71 < this->open_descriptors_
.size());
72 Open_descriptor
* pod
= &this->open_descriptors_
[descriptor
];
74 || (pod
->name
!= NULL
&& strcmp(pod
->name
, name
) == 0))
76 gold_assert(!pod
->inuse
);
84 int new_descriptor
= ::open(name
, flags
, mode
);
85 if (new_descriptor
< 0
89 if (descriptor
>= 0 && errno
== ENOENT
)
92 Hold_lock
hl(*this->lock_
);
94 gold_error(_("file %s was removed during the link"),
95 this->open_descriptors_
[descriptor
].name
);
101 return new_descriptor
;
104 if (new_descriptor
>= 0)
106 Hold_optional_lock
hl(this->lock_
);
108 if (static_cast<size_t>(new_descriptor
)
109 >= this->open_descriptors_
.size())
110 this->open_descriptors_
.resize(new_descriptor
+ 64);
112 Open_descriptor
* pod
= &this->open_descriptors_
[new_descriptor
];
114 pod
->stack_next
= -1;
116 pod
->is_write
= (flags
& O_ACCMODE
) != O_RDONLY
;
119 if (this->current_
>= this->limit_
)
120 this->close_some_descriptor();
122 return new_descriptor
;
125 // We ran out of file descriptors.
127 Hold_optional_lock
hl(this->lock_
);
129 this->limit_
= this->current_
- 16;
130 if (this->limit_
< 8)
132 if (!this->close_some_descriptor())
133 gold_fatal(_("out of file descriptors and couldn't close any"));
138 // Release a descriptor.
141 Descriptors::release(int descriptor
, bool permanent
)
143 Hold_optional_lock
hl(this->lock_
);
145 gold_assert(descriptor
>= 0
146 && (static_cast<size_t>(descriptor
)
147 < this->open_descriptors_
.size()));
148 Open_descriptor
* pod
= &this->open_descriptors_
[descriptor
];
151 || (this->current_
> this->limit_
&& !pod
->is_write
))
153 if (::close(descriptor
) < 0)
154 gold_warning(_("while closing %s: %s"), pod
->name
, strerror(errno
));
163 pod
->stack_next
= this->stack_top_
;
164 this->stack_top_
= descriptor
;
169 // Close some descriptor. The lock is held when this is called. We
170 // close the descriptor on the top of the free stack. Note that this
171 // is the opposite of an LRU algorithm--we close the most recently
172 // used descriptor. That is because the linker tends to cycle through
173 // all the files; after we release a file, we are unlikely to need it
174 // again until we have looked at all the other files. Return true if
175 // we closed a descriptor.
178 Descriptors::close_some_descriptor()
181 int i
= this->stack_top_
;
184 gold_assert(static_cast<size_t>(i
) < this->open_descriptors_
.size());
185 Open_descriptor
* pod
= &this->open_descriptors_
[i
];
186 if (!pod
->inuse
&& !pod
->is_write
)
189 gold_warning(_("while closing %s: %s"), pod
->name
, strerror(errno
));
193 this->stack_top_
= pod
->stack_next
;
195 this->open_descriptors_
[last
].stack_next
= pod
->stack_next
;
202 // We couldn't find any descriptors to close. This is weird but not
203 // necessarily an error.
207 // The single global variable which manages descriptors.
209 Descriptors descriptors
;
211 } // End namespace gold.
This page took 0.034206 seconds and 4 git commands to generate.