OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gold/] [descriptors.cc] - Blame information for rev 854

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
// descriptors.cc -- manage file descriptors for gold
2
 
3
// Copyright 2008, 2009 Free Software Foundation, Inc.
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 <cstdio>
27
#include <cstring>
28
#include <fcntl.h>
29
#include <unistd.h>
30
 
31
#include "parameters.h"
32
#include "options.h"
33
#include "gold-threads.h"
34
#include "descriptors.h"
35
#include "binary-io.h"
36
 
37
// Very old systems may not define FD_CLOEXEC.
38
#ifndef FD_CLOEXEC
39
#define FD_CLOEXEC 1
40
#endif
41
 
42
// O_CLOEXEC is only available on newer systems.
43
#ifndef O_CLOEXEC
44
#define O_CLOEXEC 0
45
#endif
46
 
47
namespace gold
48
{
49
 
50
// Class Descriptors.
51
 
52
// The default for limit_ is meant to simply be large.  It gets
53
// adjusted downward if we run out of file descriptors.
54
 
55
Descriptors::Descriptors()
56
  : lock_(NULL), initialize_lock_(&this->lock_), open_descriptors_(),
57
    stack_top_(-1), current_(0), limit_(8192 - 16)
58
{
59
  this->open_descriptors_.reserve(128);
60
}
61
 
62
// Open a file.
63
 
64
int
65
Descriptors::open(int descriptor, const char* name, int flags, int mode)
66
{
67
  // We don't initialize this until we are called, because we can't
68
  // initialize a Lock until we have parsed the options to find out
69
  // whether we are running with threads.  We can be called before
70
  // options are valid when reading a linker script.
71
  bool lock_initialized = this->initialize_lock_.initialize();
72
 
73
  gold_assert(lock_initialized || descriptor < 0);
74
 
75
  if (descriptor >= 0)
76
    {
77
      Hold_lock hl(*this->lock_);
78
 
79
      gold_assert(static_cast<size_t>(descriptor)
80
                  < this->open_descriptors_.size());
81
      Open_descriptor* pod = &this->open_descriptors_[descriptor];
82
      if (pod->name == name
83
          || (pod->name != NULL && strcmp(pod->name, name) == 0))
84
        {
85
          gold_assert(!pod->inuse);
86
          pod->inuse = true;
87
          if (descriptor == this->stack_top_)
88
            {
89
              this->stack_top_ = pod->stack_next;
90
              pod->stack_next = -1;
91
              pod->is_on_stack = false;
92
            }
93
          return descriptor;
94
        }
95
    }
96
 
97
  while (true)
98
    {
99
      // We always want to set the close-on-exec flag; we don't
100
      // require callers to pass it.
101
      flags |= O_CLOEXEC;
102
 
103
      // Always open the file as a binary file.
104
      flags |= O_BINARY;
105
 
106
      int new_descriptor = ::open(name, flags, mode);
107
      if (new_descriptor < 0
108
          && errno != ENFILE
109
          && errno != EMFILE)
110
        {
111
          if (descriptor >= 0 && errno == ENOENT)
112
            {
113
              {
114
                Hold_lock hl(*this->lock_);
115
 
116
                gold_error(_("file %s was removed during the link"),
117
                           this->open_descriptors_[descriptor].name);
118
              }
119
 
120
              errno = ENOENT;
121
            }
122
 
123
          return new_descriptor;
124
        }
125
 
126
      if (new_descriptor >= 0)
127
        {
128
          // If we have any plugins, we really do need to set the
129
          // close-on-exec flag, even if O_CLOEXEC is not defined.
130
          // FIXME: In some cases O_CLOEXEC may be defined in the
131
          // header file but not supported by the kernel.
132
          // Unfortunately there doesn't seem to be any obvious way to
133
          // detect that, as unknown flags passed to open are ignored.
134
          if (O_CLOEXEC == 0
135
              && parameters->options_valid()
136
              && parameters->options().has_plugins())
137
            fcntl(new_descriptor, F_SETFD, FD_CLOEXEC);
138
 
139
          {
140
            Hold_optional_lock hl(this->lock_);
141
 
142
            if (static_cast<size_t>(new_descriptor)
143
                >= this->open_descriptors_.size())
144
              this->open_descriptors_.resize(new_descriptor + 64);
145
 
146
            Open_descriptor* pod = &this->open_descriptors_[new_descriptor];
147
            pod->name = name;
148
            pod->stack_next = -1;
149
            pod->inuse = true;
150
            pod->is_write = (flags & O_ACCMODE) != O_RDONLY;
151
            pod->is_on_stack = false;
152
 
153
            ++this->current_;
154
            if (this->current_ >= this->limit_)
155
              this->close_some_descriptor();
156
 
157
            return new_descriptor;
158
          }
159
        }
160
 
161
      // We ran out of file descriptors.
162
      {
163
        Hold_optional_lock hl(this->lock_);
164
 
165
        this->limit_ = this->current_ - 16;
166
        if (this->limit_ < 8)
167
          this->limit_ = 8;
168
        if (!this->close_some_descriptor())
169
          gold_fatal(_("out of file descriptors and couldn't close any"));
170
      }
171
    }
172
}
173
 
174
// Release a descriptor.
175
 
176
void
177
Descriptors::release(int descriptor, bool permanent)
178
{
179
  Hold_optional_lock hl(this->lock_);
180
 
181
  gold_assert(descriptor >= 0
182
              && (static_cast<size_t>(descriptor)
183
                  < this->open_descriptors_.size()));
184
  Open_descriptor* pod = &this->open_descriptors_[descriptor];
185
 
186
  if (permanent
187
      || (this->current_ > this->limit_ && !pod->is_write))
188
    {
189
      if (::close(descriptor) < 0)
190
        gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
191
      pod->name = NULL;
192
      --this->current_;
193
    }
194
  else
195
    {
196
      pod->inuse = false;
197
      if (!pod->is_write && !pod->is_on_stack)
198
        {
199
          pod->stack_next = this->stack_top_;
200
          this->stack_top_ = descriptor;
201
          pod->is_on_stack = true;
202
        }
203
    }
204
}
205
 
206
// Close some descriptor.  The lock is held when this is called.  We
207
// close the descriptor on the top of the free stack.  Note that this
208
// is the opposite of an LRU algorithm--we close the most recently
209
// used descriptor.  That is because the linker tends to cycle through
210
// all the files; after we release a file, we are unlikely to need it
211
// again until we have looked at all the other files.  Return true if
212
// we closed a descriptor.
213
 
214
bool
215
Descriptors::close_some_descriptor()
216
{
217
  int last = -1;
218
  int i = this->stack_top_;
219
  while (i >= 0)
220
    {
221
      gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size());
222
      Open_descriptor* pod = &this->open_descriptors_[i];
223
      if (!pod->inuse && !pod->is_write)
224
        {
225
          if (::close(i) < 0)
226
            gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
227
          --this->current_;
228
          pod->name = NULL;
229
          if (last < 0)
230
            this->stack_top_ = pod->stack_next;
231
          else
232
            this->open_descriptors_[last].stack_next = pod->stack_next;
233
          pod->stack_next = -1;
234
          pod->is_on_stack = false;
235
          return true;
236
        }
237
      last = i;
238
      i = pod->stack_next;
239
    }
240
 
241
  // We couldn't find any descriptors to close.  This is weird but not
242
  // necessarily an error.
243
  return false;
244
}
245
 
246
// The single global variable which manages descriptors.
247
 
248
Descriptors descriptors;
249
 
250
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.