1*a9fa9459Szrj // descriptors.h -- manage file descriptors for gold -*- C++ -*-
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2008-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj
23*a9fa9459Szrj #ifndef GOLD_DESCRIPTORS_H
24*a9fa9459Szrj #define GOLD_DESCRIPTORS_H
25*a9fa9459Szrj
26*a9fa9459Szrj #include <vector>
27*a9fa9459Szrj
28*a9fa9459Szrj #include "gold-threads.h"
29*a9fa9459Szrj
30*a9fa9459Szrj namespace gold
31*a9fa9459Szrj {
32*a9fa9459Szrj
33*a9fa9459Szrj // This class manages file descriptors for gold.
34*a9fa9459Szrj
35*a9fa9459Szrj class Descriptors
36*a9fa9459Szrj {
37*a9fa9459Szrj public:
38*a9fa9459Szrj Descriptors();
39*a9fa9459Szrj
40*a9fa9459Szrj // Get a file descriptor for a file. The DESCRIPTOR parameter is
41*a9fa9459Szrj // the descriptor the last time the file was used; this will be -1
42*a9fa9459Szrj // if this is the first time the file is being opened. The NAME,
43*a9fa9459Szrj // FLAGS, and MODE parameters are as for ::open. NAME must be in
44*a9fa9459Szrj // permanent storage. This returns the descriptor to use, which may
45*a9fa9459Szrj // or may not be the same as DESCRIPTOR. If there is an error
46*a9fa9459Szrj // opening the file, this will return -1 with errno set
47*a9fa9459Szrj // appropriately.
48*a9fa9459Szrj int
49*a9fa9459Szrj open(int descriptor, const char* name, int flags, int mode = 0);
50*a9fa9459Szrj
51*a9fa9459Szrj // Release the file descriptor DESCRIPTOR. If PERMANENT is true, it
52*a9fa9459Szrj // will be closed, and the caller may not reopen it. If PERMANENT
53*a9fa9459Szrj // is false this doesn't necessarily close the descriptor, but it
54*a9fa9459Szrj // makes it available to be closed; the descriptor must not be used
55*a9fa9459Szrj // again except as an argument to Descriptor::open.
56*a9fa9459Szrj void
57*a9fa9459Szrj release(int descriptor, bool permanent);
58*a9fa9459Szrj
59*a9fa9459Szrj // Close all the descriptors open for reading.
60*a9fa9459Szrj void
61*a9fa9459Szrj close_all();
62*a9fa9459Szrj
63*a9fa9459Szrj private:
64*a9fa9459Szrj // Information kept for a descriptor.
65*a9fa9459Szrj struct Open_descriptor
66*a9fa9459Szrj {
67*a9fa9459Szrj // File name currently associated with descriptor. This is empty
68*a9fa9459Szrj // if none.
69*a9fa9459Szrj const char* name;
70*a9fa9459Szrj // Index of next descriptor on stack of released descriptors.
71*a9fa9459Szrj int stack_next;
72*a9fa9459Szrj // Whether the descriptor is currently in use.
73*a9fa9459Szrj bool inuse;
74*a9fa9459Szrj // Whether this is a write descriptor.
75*a9fa9459Szrj bool is_write;
76*a9fa9459Szrj // Whether the descriptor is on the stack.
77*a9fa9459Szrj bool is_on_stack;
78*a9fa9459Szrj };
79*a9fa9459Szrj
80*a9fa9459Szrj bool
81*a9fa9459Szrj close_some_descriptor();
82*a9fa9459Szrj
83*a9fa9459Szrj // We need to lock before accessing any fields.
84*a9fa9459Szrj Lock* lock_;
85*a9fa9459Szrj // Used to initialize the lock_ field exactly once.
86*a9fa9459Szrj Initialize_lock initialize_lock_;
87*a9fa9459Szrj // Information for descriptors.
88*a9fa9459Szrj std::vector<Open_descriptor> open_descriptors_;
89*a9fa9459Szrj // Top of stack.
90*a9fa9459Szrj int stack_top_;
91*a9fa9459Szrj // The current number of file descriptors open.
92*a9fa9459Szrj int current_;
93*a9fa9459Szrj // The maximum number of file descriptors we open.
94*a9fa9459Szrj int limit_;
95*a9fa9459Szrj };
96*a9fa9459Szrj
97*a9fa9459Szrj // File descriptors are a centralized data structure, and we use a
98*a9fa9459Szrj // global variable rather than passing the data structure into every
99*a9fa9459Szrj // routine that does file I/O.
100*a9fa9459Szrj
101*a9fa9459Szrj extern Descriptors descriptors;
102*a9fa9459Szrj
103*a9fa9459Szrj inline int
104*a9fa9459Szrj open_descriptor(int descriptor, const char* name, int flags, int mode = 0)
105*a9fa9459Szrj { return descriptors.open(descriptor, name, flags, mode); }
106*a9fa9459Szrj
107*a9fa9459Szrj inline void
release_descriptor(int descriptor,bool permanent)108*a9fa9459Szrj release_descriptor(int descriptor, bool permanent)
109*a9fa9459Szrj { descriptors.release(descriptor, permanent); }
110*a9fa9459Szrj
111*a9fa9459Szrj inline void
close_all_descriptors()112*a9fa9459Szrj close_all_descriptors()
113*a9fa9459Szrj { descriptors.close_all(); }
114*a9fa9459Szrj
115*a9fa9459Szrj } // End namespace gold.
116*a9fa9459Szrj
117*a9fa9459Szrj #endif // !defined(GOLD_DESCRIPTORS_H)
118