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