Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.h -- read files for gold -*- C++ -*- |
2 | ||
3 | // Classes used to read data from binary input files. | |
4 | ||
5 | #ifndef GOLD_FILEREAD_H | |
6 | #define GOLD_FILEREAD_H | |
7 | ||
bae7f79e | 8 | #include <list> |
ead1e424 ILT |
9 | #include <map> |
10 | #include <string> | |
bae7f79e ILT |
11 | |
12 | #include "options.h" | |
13 | ||
14 | namespace gold | |
15 | { | |
16 | ||
17 | class Dirsearch; | |
bae7f79e ILT |
18 | class File_view; |
19 | ||
20 | // File_read manages a file descriptor for a file we are reading. We | |
21 | // close file descriptors if we run out of them, so this class reopens | |
22 | // the file as needed. | |
23 | ||
24 | class File_read | |
25 | { | |
26 | public: | |
27 | File_read() | |
5a6f7e2d ILT |
28 | : name_(), descriptor_(-1), lock_count_(0), views_(), |
29 | saved_views_(), contents_(NULL), contents_size_(0) | |
bae7f79e | 30 | { } |
5a6f7e2d | 31 | |
bae7f79e ILT |
32 | ~File_read(); |
33 | ||
34 | // Open a file. | |
35 | bool | |
36 | open(const std::string& name); | |
37 | ||
5a6f7e2d ILT |
38 | // Pretend to open the file, but provide the file contents. No |
39 | // actual file system activity will occur. This is used for | |
40 | // testing. | |
41 | bool | |
42 | open(const std::string& name, const unsigned char* contents, off_t size); | |
43 | ||
bae7f79e ILT |
44 | // Return the file name. |
45 | const std::string& | |
46 | filename() const | |
47 | { return this->name_; } | |
48 | ||
bae7f79e ILT |
49 | // Lock the file for access within a particular Task::run execution. |
50 | // This means that the descriptor can not be closed. This routine | |
51 | // may only be called from the main thread. | |
52 | void | |
53 | lock(); | |
54 | ||
55 | // Unlock the descriptor, permitting it to be closed if necessary. | |
56 | void | |
57 | unlock(); | |
4973341a | 58 | |
bae7f79e ILT |
59 | // Test whether the object is locked. |
60 | bool | |
61 | is_locked(); | |
62 | ||
63 | // Return a view into the file. The pointer will remain valid until | |
64 | // the File_read is unlocked. If PBYTES is NULL, it is an error if | |
65 | // we can not read enough data. Otherwise *PBYTES is set to the | |
66 | // number of bytes read. | |
67 | const unsigned char* | |
4973341a | 68 | get_view(off_t start, off_t size, off_t* pbytes = NULL); |
bae7f79e ILT |
69 | |
70 | // Read data from the file into the buffer P. PBYTES is as in | |
71 | // get_view. | |
72 | void | |
4973341a | 73 | read(off_t start, off_t size, void* p, off_t* pbytes = NULL); |
bae7f79e ILT |
74 | |
75 | // Return a lasting view into the file. This is allocated with new, | |
76 | // and the caller is responsible for deleting it when done. The | |
77 | // data associated with this view will remain valid until the view | |
78 | // is deleted. PBYTES is handled as with get_view. | |
79 | File_view* | |
80 | get_lasting_view(off_t start, off_t size, off_t *pbytes = NULL); | |
81 | ||
82 | private: | |
83 | // This class may not be copied. | |
84 | File_read(const File_read&); | |
85 | File_read& operator=(const File_read&); | |
86 | ||
87 | // A view into the file when not using mmap. | |
88 | class View | |
89 | { | |
90 | public: | |
91 | View(off_t start, off_t size, unsigned char* data) | |
92 | : start_(start), size_(size), data_(data), lock_count_(0) | |
93 | { } | |
94 | ||
95 | ~View(); | |
96 | ||
97 | off_t | |
98 | start() const | |
99 | { return this->start_; } | |
100 | ||
101 | off_t | |
102 | size() const | |
103 | { return this->size_; } | |
104 | ||
105 | unsigned char* | |
106 | data() const | |
107 | { return this->data_; } | |
108 | ||
109 | void | |
110 | lock(); | |
111 | ||
112 | void | |
113 | unlock(); | |
114 | ||
115 | bool | |
116 | is_locked(); | |
117 | ||
118 | private: | |
119 | View(const View&); | |
120 | View& operator=(const View&); | |
121 | ||
122 | off_t start_; | |
123 | off_t size_; | |
124 | unsigned char* data_; | |
125 | int lock_count_; | |
126 | }; | |
127 | ||
128 | friend class File_view; | |
129 | ||
ead1e424 | 130 | // Find a view into the file. |
bae7f79e ILT |
131 | View* |
132 | find_view(off_t start, off_t size); | |
133 | ||
ead1e424 | 134 | // Read data from the file into a buffer. |
bae7f79e ILT |
135 | off_t |
136 | do_read(off_t start, off_t size, void* p, off_t* pbytes); | |
137 | ||
ead1e424 | 138 | // Find or make a view into the file. |
bae7f79e ILT |
139 | View* |
140 | find_or_make_view(off_t start, off_t size, off_t* pbytes); | |
141 | ||
ead1e424 | 142 | // Clear the file views. |
bae7f79e ILT |
143 | void |
144 | clear_views(bool); | |
145 | ||
ead1e424 ILT |
146 | // The size of a file page for buffering data. |
147 | static const off_t page_size = 8192; | |
148 | ||
149 | // Given a file offset, return the page offset. | |
150 | static off_t | |
151 | page_offset(off_t file_offset) | |
152 | { return file_offset & ~ (page_size - 1); } | |
153 | ||
154 | // Given a file size, return the size to read integral pages. | |
155 | static off_t | |
156 | pages(off_t file_size) | |
157 | { return (file_size + (page_size - 1)) & ~ (page_size - 1); } | |
158 | ||
159 | // The type of a mapping from page start to views. | |
160 | typedef std::map<off_t, View*> Views; | |
161 | ||
162 | // A simple list of Views. | |
163 | typedef std::list<View*> Saved_views; | |
164 | ||
165 | // File name. | |
bae7f79e | 166 | std::string name_; |
ead1e424 | 167 | // File descriptor. |
bae7f79e | 168 | int descriptor_; |
ead1e424 | 169 | // Number of locks on the file. |
bae7f79e | 170 | int lock_count_; |
ead1e424 ILT |
171 | // Buffered views into the file. |
172 | Views views_; | |
173 | // List of views which were locked but had to be removed from views_ | |
174 | // because they were not large enough. | |
175 | Saved_views saved_views_; | |
5a6f7e2d ILT |
176 | // Specified file contents. Used only for testing purposes. |
177 | const unsigned char* contents_; | |
178 | // Specified file size. Used only for testing purposes. | |
179 | off_t contents_size_; | |
bae7f79e ILT |
180 | }; |
181 | ||
182 | // A view of file data that persists even when the file is unlocked. | |
183 | // Callers should destroy these when no longer required. These are | |
184 | // obtained form File_read::get_lasting_view. They may only be | |
185 | // destroyed when the underlying File_read is locked. | |
186 | ||
187 | class File_view | |
188 | { | |
189 | public: | |
190 | // This may only be called when the underlying File_read is locked. | |
191 | ~File_view(); | |
192 | ||
193 | // Return a pointer to the data associated with this view. | |
194 | const unsigned char* | |
195 | data() const | |
196 | { return this->data_; } | |
197 | ||
198 | private: | |
199 | File_view(const File_view&); | |
200 | File_view& operator=(const File_view&); | |
201 | ||
202 | friend class File_read; | |
203 | ||
204 | // Callers have to get these via File_read::get_lasting_view. | |
205 | File_view(File_read& file, File_read::View* view, const unsigned char* data) | |
206 | : file_(file), view_(view), data_(data) | |
207 | { } | |
208 | ||
209 | File_read& file_; | |
210 | File_read::View* view_; | |
211 | const unsigned char* data_; | |
212 | }; | |
213 | ||
bae7f79e ILT |
214 | // All the information we hold for a single input file. This can be |
215 | // an object file, a shared library, or an archive. | |
216 | ||
217 | class Input_file | |
218 | { | |
219 | public: | |
5a6f7e2d ILT |
220 | Input_file(const Input_file_argument* input_argument) |
221 | : input_argument_(input_argument), file_() | |
bae7f79e ILT |
222 | { } |
223 | ||
5a6f7e2d ILT |
224 | // Create an input file with the contents already provided. This is |
225 | // only used for testing. With this path, don't call the open | |
226 | // method. | |
227 | Input_file(const char* name, const unsigned char* contents, off_t size); | |
228 | ||
229 | // Open the file. | |
bae7f79e ILT |
230 | void |
231 | open(const General_options&, const Dirsearch&); | |
232 | ||
233 | // Return the name given by the user. | |
234 | const char* | |
235 | name() const | |
5a6f7e2d | 236 | { return this->input_argument_->name(); } |
bae7f79e ILT |
237 | |
238 | // Return the file name. | |
239 | const std::string& | |
240 | filename() const | |
241 | { return this->file_.filename(); } | |
242 | ||
4973341a ILT |
243 | // Return the position dependent options. |
244 | const Position_dependent_options& | |
245 | options() const | |
246 | { return this->input_argument_->options(); } | |
247 | ||
248 | // Return the file. | |
bae7f79e ILT |
249 | File_read& |
250 | file() | |
251 | { return this->file_; } | |
252 | ||
253 | private: | |
ead1e424 ILT |
254 | Input_file(const Input_file&); |
255 | Input_file& operator=(const Input_file&); | |
256 | ||
5a6f7e2d | 257 | const Input_file_argument* input_argument_; |
bae7f79e ILT |
258 | File_read file_; |
259 | }; | |
260 | ||
261 | } // end namespace gold | |
262 | ||
263 | #endif // !defined(GOLD_FILEREAD_H) |