| 1 |
205 |
julius |
// descriptors.h -- manage file descriptors for gold -*- C++ -*-
|
| 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 |
|
|
#ifndef GOLD_DESCRIPTORS_H
|
| 24 |
|
|
#define GOLD_DESCRIPTORS_H
|
| 25 |
|
|
|
| 26 |
|
|
#include <vector>
|
| 27 |
|
|
|
| 28 |
|
|
#include "gold-threads.h"
|
| 29 |
|
|
|
| 30 |
|
|
namespace gold
|
| 31 |
|
|
{
|
| 32 |
|
|
|
| 33 |
|
|
// This class manages file descriptors for gold.
|
| 34 |
|
|
|
| 35 |
|
|
class Descriptors
|
| 36 |
|
|
{
|
| 37 |
|
|
public:
|
| 38 |
|
|
Descriptors();
|
| 39 |
|
|
|
| 40 |
|
|
// Get a file descriptor for a file. The DESCRIPTOR parameter is
|
| 41 |
|
|
// the descriptor the last time the file was used; this will be -1
|
| 42 |
|
|
// if this is the first time the file is being opened. The NAME,
|
| 43 |
|
|
// FLAGS, and MODE parameters are as for ::open. NAME must be in
|
| 44 |
|
|
// permanent storage. This returns the descriptor to use, which may
|
| 45 |
|
|
// or may not be the same as DESCRIPTOR. If there is an error
|
| 46 |
|
|
// opening the file, this will return -1 with errno set
|
| 47 |
|
|
// appropriately.
|
| 48 |
|
|
int
|
| 49 |
|
|
open(int descriptor, const char* name, int flags, int mode = 0);
|
| 50 |
|
|
|
| 51 |
|
|
// Release the file descriptor DESCRIPTOR. If PERMANENT is true, it
|
| 52 |
|
|
// will be closed, and the caller may not reopen it. If PERMANENT
|
| 53 |
|
|
// is false this doesn't necessarily close the descriptor, but it
|
| 54 |
|
|
// makes it available to be closed; the descriptor must not be used
|
| 55 |
|
|
// again except as an argument to Descriptor::open.
|
| 56 |
|
|
void
|
| 57 |
|
|
release(int descriptor, bool permanent);
|
| 58 |
|
|
|
| 59 |
|
|
private:
|
| 60 |
|
|
// Information kept for a descriptor.
|
| 61 |
|
|
struct Open_descriptor
|
| 62 |
|
|
{
|
| 63 |
|
|
// File name currently associated with descriptor. This is empty
|
| 64 |
|
|
// if none.
|
| 65 |
|
|
const char* name;
|
| 66 |
|
|
// Index of next descriptor on stack of released descriptors.
|
| 67 |
|
|
int stack_next;
|
| 68 |
|
|
// Whether the descriptor is currently in use.
|
| 69 |
|
|
bool inuse;
|
| 70 |
|
|
// Whether this is a write descriptor.
|
| 71 |
|
|
bool is_write;
|
| 72 |
|
|
// Whether the descriptor is on the stack.
|
| 73 |
|
|
bool is_on_stack;
|
| 74 |
|
|
};
|
| 75 |
|
|
|
| 76 |
|
|
bool
|
| 77 |
|
|
close_some_descriptor();
|
| 78 |
|
|
|
| 79 |
|
|
// We need to lock before accessing any fields.
|
| 80 |
|
|
Lock* lock_;
|
| 81 |
|
|
// Used to initialize the lock_ field exactly once.
|
| 82 |
|
|
Initialize_lock initialize_lock_;
|
| 83 |
|
|
// Information for descriptors.
|
| 84 |
|
|
std::vector<Open_descriptor> open_descriptors_;
|
| 85 |
|
|
// Top of stack.
|
| 86 |
|
|
int stack_top_;
|
| 87 |
|
|
// The current number of file descriptors open.
|
| 88 |
|
|
int current_;
|
| 89 |
|
|
// The maximum number of file descriptors we open.
|
| 90 |
|
|
int limit_;
|
| 91 |
|
|
};
|
| 92 |
|
|
|
| 93 |
|
|
// File descriptors are a centralized data structure, and we use a
|
| 94 |
|
|
// global variable rather than passing the data structure into every
|
| 95 |
|
|
// routine that does file I/O.
|
| 96 |
|
|
|
| 97 |
|
|
extern Descriptors descriptors;
|
| 98 |
|
|
|
| 99 |
|
|
inline int
|
| 100 |
|
|
open_descriptor(int descriptor, const char* name, int flags, int mode = 0)
|
| 101 |
|
|
{ return descriptors.open(descriptor, name, flags, mode); }
|
| 102 |
|
|
|
| 103 |
|
|
inline void
|
| 104 |
|
|
release_descriptor(int descriptor, bool permanent)
|
| 105 |
|
|
{ descriptors.release(descriptor, permanent); }
|
| 106 |
|
|
|
| 107 |
|
|
} // End namespace gold.
|
| 108 |
|
|
|
| 109 |
|
|
#endif // !defined(GOLD_DESCRIPTORS_H)
|