1 // fileread.h -- read files for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 // Classes used to read data from binary input files.
25 #ifndef GOLD_FILEREAD_H
26 #define GOLD_FILEREAD_H
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.
48 : name_(), descriptor_(-1), lock_count_(0), views_(),
49 saved_views_(), contents_(NULL), contents_size_(0)
56 open(const std::string& name);
58 // Pretend to open the file, but provide the file contents. No
59 // actual file system activity will occur. This is used for
62 open(const std::string& name, const unsigned char* contents, off_t size);
64 // Return the file name.
67 { return this->name_; }
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.
75 // Unlock the descriptor, permitting it to be closed if necessary.
79 // Test whether the object is locked.
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
88 get_view(off_t start, off_t size);
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.
95 get_view_and_size(off_t start, off_t size, off_t* pbytes);
97 // Read data from the file into the buffer P starting at file offset
98 // START for SIZE bytes.
100 read(off_t start, off_t size, void* p);
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.
105 read_up_to(off_t start, off_t size, void* p, off_t* pbytes);
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.
113 get_lasting_view(off_t start, off_t size);
116 // This class may not be copied.
117 File_read(const File_read&);
118 File_read& operator=(const File_read&);
120 // A view into the file when not using mmap.
124 View(off_t start, off_t size, const unsigned char* data)
125 : start_(start), size_(size), data_(data), lock_count_(0)
132 { return this->start_; }
136 { return this->size_; }
140 { return this->data_; }
153 View& operator=(const View&);
157 const unsigned char* data_;
161 friend class File_view;
163 // Find a view into the file.
165 find_view(off_t start, off_t size);
167 // Read data from the file into a buffer.
169 do_read(off_t start, off_t size, void* p, off_t* pbytes);
171 // Find or make a view into the file.
173 find_or_make_view(off_t start, off_t size, off_t* pbytes);
175 // Clear the file views.
179 // The size of a file page for buffering data.
180 static const off_t page_size = 8192;
182 // Given a file offset, return the page offset.
184 page_offset(off_t file_offset)
185 { return file_offset & ~ (page_size - 1); }
187 // Given a file size, return the size to read integral pages.
189 pages(off_t file_size)
190 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
192 // The type of a mapping from page start to views.
193 typedef std::map<off_t, View*> Views;
195 // A simple list of Views.
196 typedef std::list<View*> Saved_views;
202 // Number of locks on the file.
204 // Buffered views into the file.
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_;
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.
223 // This may only be called when the underlying File_read is locked.
226 // Return a pointer to the data associated with this view.
229 { return this->data_; }
232 File_view(const File_view&);
233 File_view& operator=(const File_view&);
235 friend class File_read;
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)
243 File_read::View* view_;
244 const unsigned char* data_;
247 // All the information we hold for a single input file. This can be
248 // an object file, a shared library, or an archive.
253 Input_file(const Input_file_argument* input_argument)
254 : input_argument_(input_argument), file_()
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
260 Input_file(const char* name, const unsigned char* contents, off_t size);
264 open(const General_options&, const Dirsearch&);
266 // Return the name given by the user.
269 { return this->input_argument_->name(); }
271 // Return the file name.
274 { return this->file_.filename(); }
276 // Return the position dependent options.
277 const Position_dependent_options&
279 { return this->input_argument_->options(); }
284 { return this->file_; }
287 Input_file(const Input_file&);
288 Input_file& operator=(const Input_file&);
290 const Input_file_argument* input_argument_;
294 } // end namespace gold
296 #endif // !defined(GOLD_FILEREAD_H)