Remove get_view_and_size.
[deliverable/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()
5a6f7e2d
ILT
48 : name_(), descriptor_(-1), lock_count_(0), views_(),
49 saved_views_(), contents_(NULL), contents_size_(0)
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
ba45d247
ILT
83 // Return a view into the file starting at file offset START for
84 // SIZE bytes. The pointer will remain valid until the File_read is
85 // unlocked. It is an error if we can not read enough data from the
86 // file.
bae7f79e 87 const unsigned char*
ba45d247 88 get_view(off_t start, off_t size);
bae7f79e 89
ba45d247
ILT
90 // Read data from the file into the buffer P starting at file offset
91 // START for SIZE bytes.
92 void
93 read(off_t start, off_t size, void* p);
94
95 // Read up to SIZE bytes from the file into the buffer P starting at
96 // file offset START. Set *PBYTES to the number of bytes read.
bae7f79e 97 void
ba45d247 98 read_up_to(off_t start, off_t size, void* p, off_t* pbytes);
bae7f79e 99
ba45d247
ILT
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.
bae7f79e 105 File_view*
ba45d247 106 get_lasting_view(off_t start, off_t size);
bae7f79e
ILT
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:
e214a02b 117 View(off_t start, off_t size, const unsigned char* data)
bae7f79e
ILT
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
e214a02b 131 const unsigned char*
bae7f79e
ILT
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_;
e214a02b 150 const unsigned char* data_;
bae7f79e
ILT
151 int lock_count_;
152 };
153
154 friend class File_view;
155
ead1e424 156 // Find a view into the file.
bae7f79e
ILT
157 View*
158 find_view(off_t start, off_t size);
159
ead1e424 160 // Read data from the file into a buffer.
bae7f79e
ILT
161 off_t
162 do_read(off_t start, off_t size, void* p, off_t* pbytes);
163
ead1e424 164 // Find or make a view into the file.
bae7f79e 165 View*
bae3688d 166 find_or_make_view(off_t start, off_t size);
bae7f79e 167
ead1e424 168 // Clear the file views.
bae7f79e
ILT
169 void
170 clear_views(bool);
171
ead1e424
ILT
172 // The size of a file page for buffering data.
173 static const off_t page_size = 8192;
174
175 // Given a file offset, return the page offset.
176 static off_t
177 page_offset(off_t file_offset)
178 { return file_offset & ~ (page_size - 1); }
179
180 // Given a file size, return the size to read integral pages.
181 static off_t
182 pages(off_t file_size)
183 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
184
185 // The type of a mapping from page start to views.
186 typedef std::map<off_t, View*> Views;
187
188 // A simple list of Views.
189 typedef std::list<View*> Saved_views;
190
191 // File name.
bae7f79e 192 std::string name_;
ead1e424 193 // File descriptor.
bae7f79e 194 int descriptor_;
ead1e424 195 // Number of locks on the file.
bae7f79e 196 int lock_count_;
ead1e424
ILT
197 // Buffered views into the file.
198 Views views_;
199 // List of views which were locked but had to be removed from views_
200 // because they were not large enough.
201 Saved_views saved_views_;
5a6f7e2d
ILT
202 // Specified file contents. Used only for testing purposes.
203 const unsigned char* contents_;
204 // Specified file size. Used only for testing purposes.
205 off_t contents_size_;
bae7f79e
ILT
206};
207
208// A view of file data that persists even when the file is unlocked.
209// Callers should destroy these when no longer required. These are
210// obtained form File_read::get_lasting_view. They may only be
211// destroyed when the underlying File_read is locked.
212
213class File_view
214{
215 public:
216 // This may only be called when the underlying File_read is locked.
217 ~File_view();
218
219 // Return a pointer to the data associated with this view.
220 const unsigned char*
221 data() const
222 { return this->data_; }
223
224 private:
225 File_view(const File_view&);
226 File_view& operator=(const File_view&);
227
228 friend class File_read;
229
230 // Callers have to get these via File_read::get_lasting_view.
231 File_view(File_read& file, File_read::View* view, const unsigned char* data)
232 : file_(file), view_(view), data_(data)
233 { }
234
235 File_read& file_;
236 File_read::View* view_;
237 const unsigned char* data_;
238};
239
bae7f79e
ILT
240// All the information we hold for a single input file. This can be
241// an object file, a shared library, or an archive.
242
243class Input_file
244{
245 public:
5a6f7e2d
ILT
246 Input_file(const Input_file_argument* input_argument)
247 : input_argument_(input_argument), file_()
bae7f79e
ILT
248 { }
249
5a6f7e2d
ILT
250 // Create an input file with the contents already provided. This is
251 // only used for testing. With this path, don't call the open
252 // method.
253 Input_file(const char* name, const unsigned char* contents, off_t size);
254
255 // Open the file.
bae7f79e
ILT
256 void
257 open(const General_options&, const Dirsearch&);
258
259 // Return the name given by the user.
260 const char*
261 name() const
5a6f7e2d 262 { return this->input_argument_->name(); }
bae7f79e
ILT
263
264 // Return the file name.
265 const std::string&
266 filename() const
267 { return this->file_.filename(); }
268
4973341a
ILT
269 // Return the position dependent options.
270 const Position_dependent_options&
271 options() const
272 { return this->input_argument_->options(); }
273
274 // Return the file.
bae7f79e
ILT
275 File_read&
276 file()
277 { return this->file_; }
278
279 private:
ead1e424
ILT
280 Input_file(const Input_file&);
281 Input_file& operator=(const Input_file&);
282
5a6f7e2d 283 const Input_file_argument* input_argument_;
bae7f79e
ILT
284 File_read file_;
285};
286
287} // end namespace gold
288
289#endif // !defined(GOLD_FILEREAD_H)
This page took 0.071429 seconds and 4 git commands to generate.