Break out default pbytes argument to read and get_view routines,
[external/binutils.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), lock_count_(0), views_(),
49       saved_views_(), contents_(NULL), contents_size_(0)
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 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.
87   const unsigned char*
88   get_view(off_t start, off_t size);
89
90   // Return a view into the file starting at file offset START, for up
91   // to SIZE bytes.  Set *PBYTES to the number of bytes read.  This
92   // may be less than SIZE.  The pointer will remain valid until the
93   // File_read is unlocked.
94   const unsigned char*
95   get_view_and_size(off_t start, off_t size, off_t* pbytes);
96
97   // Read data from the file into the buffer P starting at file offset
98   // START for SIZE bytes.
99   void
100   read(off_t start, off_t size, void* p);
101
102   // Read up to SIZE bytes from the file into the buffer P starting at
103   // file offset START.  Set *PBYTES to the number of bytes read.
104   void
105   read_up_to(off_t start, off_t size, void* p, off_t* pbytes);
106
107   // Return a lasting view into the file starting at file offset START
108   // for SIZE bytes.  This is allocated with new, and the caller is
109   // responsible for deleting it when done.  The data associated with
110   // this view will remain valid until the view is deleted.  It is an
111   // error if we can not read enough data from the file.
112   File_view*
113   get_lasting_view(off_t start, off_t size);
114
115  private:
116   // This class may not be copied.
117   File_read(const File_read&);
118   File_read& operator=(const File_read&);
119
120   // A view into the file when not using mmap.
121   class View
122   {
123    public:
124     View(off_t start, off_t size, const unsigned char* data)
125       : start_(start), size_(size), data_(data), lock_count_(0)
126     { }
127
128     ~View();
129
130     off_t
131     start() const
132     { return this->start_; }
133
134     off_t
135     size() const
136     { return this->size_; }
137
138     const unsigned char*
139     data() const
140     { return this->data_; }
141
142     void
143     lock();
144
145     void
146     unlock();
147
148     bool
149     is_locked();
150
151    private:
152     View(const View&);
153     View& operator=(const View&);
154
155     off_t start_;
156     off_t size_;
157     const unsigned char* data_;
158     int lock_count_;
159   };
160
161   friend class File_view;
162
163   // Find a view into the file.
164   View*
165   find_view(off_t start, off_t size);
166
167   // Read data from the file into a buffer.
168   off_t
169   do_read(off_t start, off_t size, void* p, off_t* pbytes);
170
171   // Find or make a view into the file.
172   View*
173   find_or_make_view(off_t start, off_t size, off_t* pbytes);
174
175   // Clear the file views.
176   void
177   clear_views(bool);
178
179   // The size of a file page for buffering data.
180   static const off_t page_size = 8192;
181
182   // Given a file offset, return the page offset.
183   static off_t
184   page_offset(off_t file_offset)
185   { return file_offset & ~ (page_size - 1); }
186
187   // Given a file size, return the size to read integral pages.
188   static off_t
189   pages(off_t file_size)
190   { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
191
192   // The type of a mapping from page start to views.
193   typedef std::map<off_t, View*> Views;
194
195   // A simple list of Views.
196   typedef std::list<View*> Saved_views;
197
198   // File name.
199   std::string name_;
200   // File descriptor.
201   int descriptor_;
202   // Number of locks on the file.
203   int lock_count_;
204   // Buffered views into the file.
205   Views views_;
206   // List of views which were locked but had to be removed from views_
207   // because they were not large enough.
208   Saved_views saved_views_;
209   // Specified file contents.  Used only for testing purposes.
210   const unsigned char* contents_;
211   // Specified file size.  Used only for testing purposes.
212   off_t contents_size_;
213 };
214
215 // A view of file data that persists even when the file is unlocked.
216 // Callers should destroy these when no longer required.  These are
217 // obtained form File_read::get_lasting_view.  They may only be
218 // destroyed when the underlying File_read is locked.
219
220 class File_view
221 {
222  public:
223   // This may only be called when the underlying File_read is locked.
224   ~File_view();
225
226   // Return a pointer to the data associated with this view.
227   const unsigned char*
228   data() const
229   { return this->data_; }
230
231  private:
232   File_view(const File_view&);
233   File_view& operator=(const File_view&);
234
235   friend class File_read;
236
237   // Callers have to get these via File_read::get_lasting_view.
238   File_view(File_read& file, File_read::View* view, const unsigned char* data)
239     : file_(file), view_(view), data_(data)
240   { }
241
242   File_read& file_;
243   File_read::View* view_;
244   const unsigned char* data_;
245 };
246
247 // All the information we hold for a single input file.  This can be
248 // an object file, a shared library, or an archive.
249
250 class Input_file
251 {
252  public:
253   Input_file(const Input_file_argument* input_argument)
254     : input_argument_(input_argument), file_()
255   { }
256
257   // Create an input file with the contents already provided.  This is
258   // only used for testing.  With this path, don't call the open
259   // method.
260   Input_file(const char* name, const unsigned char* contents, off_t size);
261
262   // Open the file.
263   void
264   open(const General_options&, const Dirsearch&);
265
266   // Return the name given by the user.
267   const char*
268   name() const
269   { return this->input_argument_->name(); }
270
271   // Return the file name.
272   const std::string&
273   filename() const
274   { return this->file_.filename(); }
275
276   // Return the position dependent options.
277   const Position_dependent_options&
278   options() const
279   { return this->input_argument_->options(); }
280
281   // Return the file.
282   File_read&
283   file()
284   { return this->file_; }
285
286  private:
287   Input_file(const Input_file&);
288   Input_file& operator=(const Input_file&);
289
290   const Input_file_argument* input_argument_;
291   File_read file_;
292 };
293
294 } // end namespace gold
295
296 #endif // !defined(GOLD_FILEREAD_H)