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
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.
28 : name_(), descriptor_(-1), lock_count_(0), views_(),
29 saved_views_(), contents_(NULL
), contents_size_(0)
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
42 open(const std::string
& name
, const unsigned char* contents
, off_t size
);
44 // Return the file name.
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.
55 // Unlock the descriptor, permitting it to be closed if necessary.
59 // Test whether the object 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.
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
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.
80 get_lasting_view(off_t start
, off_t size
, off_t
*pbytes
= NULL
);
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.
91 View(off_t start
, off_t size
, unsigned char* data
)
92 : start_(start
), size_(size
), data_(data
), lock_count_(0)
99 { return this->start_
; }
103 { return this->size_
; }
107 { return this->data_
; }
120 View
& operator=(const View
&);
124 unsigned char* data_
;
128 friend class File_view
;
130 // Find a view into the file.
132 find_view(off_t start
, off_t size
);
134 // Read data from the file into a buffer.
136 do_read(off_t start
, off_t size
, void* p
, off_t
* pbytes
);
138 // Find or make a view into the file.
140 find_or_make_view(off_t start
, off_t size
, off_t
* pbytes
);
142 // Clear the file views.
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.
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.
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
;
169 // Number of locks on the file.
171 // Buffered views into the file.
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.
190 // This may only be called when the underlying File_read is locked.
193 // Return a pointer to the data associated with this view.
196 { return this->data_
; }
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
)
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.
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
227 Input_file(const char* name
, const unsigned char* contents
, off_t size
);
231 open(const General_options
&, const Dirsearch
&);
233 // Return the name given by the user.
236 { return this->input_argument_
->name(); }
238 // Return the file name.
241 { return this->file_
.filename(); }
245 { return this->file_
; }
248 Input_file(const Input_file
&);
249 Input_file
& operator=(const Input_file
&);
251 const Input_file_argument
* input_argument_
;
255 } // end namespace gold
257 #endif // !defined(GOLD_FILEREAD_H)
This page took 0.034358 seconds and 4 git commands to generate.