2007-08-06 Paul Brook <paul@codesourcery.com>
[binutils.git] / gold / fileread.h
blob178e7f355e446f2afb8678f8bfb679596aa27234
1 // fileread.h -- read files for gold -*- C++ -*-
3 // Classes used to read data from binary input files.
5 #ifndef GOLD_FILEREAD_H
6 #define GOLD_FILEREAD_H
8 #include <list>
9 #include <map>
10 #include <string>
12 #include "options.h"
14 namespace gold
17 class Dirsearch;
18 class File_view;
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.
24 class File_read
26 public:
27 File_read()
28 : name_(), descriptor_(-1), lock_count_(0), views_(),
29 saved_views_(), contents_(NULL), contents_size_(0)
30 { }
32 ~File_read();
34 // Open a file.
35 bool
36 open(const std::string& name);
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);
44 // Return the file name.
45 const std::string&
46 filename() const
47 { return this->name_; }
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();
55 // Unlock the descriptor, permitting it to be closed if necessary.
56 void
57 unlock();
59 // Test whether the object is locked.
60 bool
61 is_locked();
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*
68 get_view(off_t start, off_t size, off_t *pbytes = NULL);
70 // Read data from the file into the buffer P. PBYTES is as in
71 // get_view.
72 void
73 read(off_t start, off_t size, void* p, off_t *pbytes = NULL);
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);
82 private:
83 // This class may not be copied.
84 File_read(const File_read&);
85 File_read& operator=(const File_read&);
87 // A view into the file when not using mmap.
88 class View
90 public:
91 View(off_t start, off_t size, unsigned char* data)
92 : start_(start), size_(size), data_(data), lock_count_(0)
93 { }
95 ~View();
97 off_t
98 start() const
99 { return this->start_; }
101 off_t
102 size() const
103 { return this->size_; }
105 unsigned char*
106 data() const
107 { return this->data_; }
109 void
110 lock();
112 void
113 unlock();
115 bool
116 is_locked();
118 private:
119 View(const View&);
120 View& operator=(const View&);
122 off_t start_;
123 off_t size_;
124 unsigned char* data_;
125 int lock_count_;
128 friend class File_view;
130 // Find a view into the file.
131 View*
132 find_view(off_t start, off_t size);
134 // Read data from the file into a buffer.
135 off_t
136 do_read(off_t start, off_t size, void* p, off_t* pbytes);
138 // Find or make a view into the file.
139 View*
140 find_or_make_view(off_t start, off_t size, off_t* pbytes);
142 // Clear the file views.
143 void
144 clear_views(bool);
146 // The size of a file page for buffering data.
147 static const off_t page_size = 8192;
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); }
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); }
159 // The type of a mapping from page start to views.
160 typedef std::map<off_t, View*> Views;
162 // A simple list of Views.
163 typedef std::list<View*> Saved_views;
165 // File name.
166 std::string name_;
167 // File descriptor.
168 int descriptor_;
169 // Number of locks on the file.
170 int lock_count_;
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_;
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_;
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.
187 class File_view
189 public:
190 // This may only be called when the underlying File_read is locked.
191 ~File_view();
193 // Return a pointer to the data associated with this view.
194 const unsigned char*
195 data() const
196 { return this->data_; }
198 private:
199 File_view(const File_view&);
200 File_view& operator=(const File_view&);
202 friend class File_read;
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)
209 File_read& file_;
210 File_read::View* view_;
211 const unsigned char* data_;
214 // All the information we hold for a single input file. This can be
215 // an object file, a shared library, or an archive.
217 class Input_file
219 public:
220 Input_file(const Input_file_argument* input_argument)
221 : input_argument_(input_argument), file_()
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);
229 // Open the file.
230 void
231 open(const General_options&, const Dirsearch&);
233 // Return the name given by the user.
234 const char*
235 name() const
236 { return this->input_argument_->name(); }
238 // Return the file name.
239 const std::string&
240 filename() const
241 { return this->file_.filename(); }
243 File_read&
244 file()
245 { return this->file_; }
247 private:
248 Input_file(const Input_file&);
249 Input_file& operator=(const Input_file&);
251 const Input_file_argument* input_argument_;
252 File_read file_;
255 } // end namespace gold
257 #endif // !defined(GOLD_FILEREAD_H)