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