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