]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/fileread.h
daily update
[thirdparty/binutils-gdb.git] / gold / fileread.h
CommitLineData
bae7f79e
ILT
1// fileread.h -- read files for gold -*- C++ -*-
2
3// Classes used to read data from binary input files.
4
5#ifndef GOLD_FILEREAD_H
6#define GOLD_FILEREAD_H
7
bae7f79e 8#include <list>
ead1e424
ILT
9#include <map>
10#include <string>
bae7f79e
ILT
11
12#include "options.h"
13
14namespace gold
15{
16
17class Dirsearch;
bae7f79e
ILT
18class File_view;
19
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.
23
24class File_read
25{
26 public:
27 File_read()
28 : name_(), descriptor_(-1), lock_count_(0)
29 { }
30 ~File_read();
31
32 // Open a file.
33 bool
34 open(const std::string& name);
35
36 // Return the file name.
37 const std::string&
38 filename() const
39 { return this->name_; }
40
41 // Return the file descriptor.
42 int
43 get_descriptor();
44
45 // Lock the file for access within a particular Task::run execution.
46 // This means that the descriptor can not be closed. This routine
47 // may only be called from the main thread.
48 void
49 lock();
50
51 // Unlock the descriptor, permitting it to be closed if necessary.
52 void
53 unlock();
54
55 // Test whether the object is locked.
56 bool
57 is_locked();
58
59 // Return a view into the file. The pointer will remain valid until
60 // the File_read is unlocked. If PBYTES is NULL, it is an error if
61 // we can not read enough data. Otherwise *PBYTES is set to the
62 // number of bytes read.
63 const unsigned char*
64 get_view(off_t start, off_t size, off_t *pbytes = NULL);
65
66 // Read data from the file into the buffer P. PBYTES is as in
67 // get_view.
68 void
69 read(off_t start, off_t size, void* p, off_t *pbytes = NULL);
70
71 // Return a lasting view into the file. This is allocated with new,
72 // and the caller is responsible for deleting it when done. The
73 // data associated with this view will remain valid until the view
74 // is deleted. PBYTES is handled as with get_view.
75 File_view*
76 get_lasting_view(off_t start, off_t size, off_t *pbytes = NULL);
77
78 private:
79 // This class may not be copied.
80 File_read(const File_read&);
81 File_read& operator=(const File_read&);
82
83 // A view into the file when not using mmap.
84 class View
85 {
86 public:
87 View(off_t start, off_t size, unsigned char* data)
88 : start_(start), size_(size), data_(data), lock_count_(0)
89 { }
90
91 ~View();
92
93 off_t
94 start() const
95 { return this->start_; }
96
97 off_t
98 size() const
99 { return this->size_; }
100
101 unsigned char*
102 data() const
103 { return this->data_; }
104
105 void
106 lock();
107
108 void
109 unlock();
110
111 bool
112 is_locked();
113
114 private:
115 View(const View&);
116 View& operator=(const View&);
117
118 off_t start_;
119 off_t size_;
120 unsigned char* data_;
121 int lock_count_;
122 };
123
124 friend class File_view;
125
ead1e424 126 // Find a view into the file.
bae7f79e
ILT
127 View*
128 find_view(off_t start, off_t size);
129
ead1e424 130 // Read data from the file into a buffer.
bae7f79e
ILT
131 off_t
132 do_read(off_t start, off_t size, void* p, off_t* pbytes);
133
ead1e424 134 // Find or make a view into the file.
bae7f79e
ILT
135 View*
136 find_or_make_view(off_t start, off_t size, off_t* pbytes);
137
ead1e424 138 // Clear the file views.
bae7f79e
ILT
139 void
140 clear_views(bool);
141
ead1e424
ILT
142 // The size of a file page for buffering data.
143 static const off_t page_size = 8192;
144
145 // Given a file offset, return the page offset.
146 static off_t
147 page_offset(off_t file_offset)
148 { return file_offset & ~ (page_size - 1); }
149
150 // Given a file size, return the size to read integral pages.
151 static off_t
152 pages(off_t file_size)
153 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
154
155 // The type of a mapping from page start to views.
156 typedef std::map<off_t, View*> Views;
157
158 // A simple list of Views.
159 typedef std::list<View*> Saved_views;
160
161 // File name.
bae7f79e 162 std::string name_;
ead1e424 163 // File descriptor.
bae7f79e 164 int descriptor_;
ead1e424 165 // Number of locks on the file.
bae7f79e 166 int lock_count_;
ead1e424
ILT
167 // Buffered views into the file.
168 Views views_;
169 // List of views which were locked but had to be removed from views_
170 // because they were not large enough.
171 Saved_views saved_views_;
bae7f79e
ILT
172};
173
174// A view of file data that persists even when the file is unlocked.
175// Callers should destroy these when no longer required. These are
176// obtained form File_read::get_lasting_view. They may only be
177// destroyed when the underlying File_read is locked.
178
179class File_view
180{
181 public:
182 // This may only be called when the underlying File_read is locked.
183 ~File_view();
184
185 // Return a pointer to the data associated with this view.
186 const unsigned char*
187 data() const
188 { return this->data_; }
189
190 private:
191 File_view(const File_view&);
192 File_view& operator=(const File_view&);
193
194 friend class File_read;
195
196 // Callers have to get these via File_read::get_lasting_view.
197 File_view(File_read& file, File_read::View* view, const unsigned char* data)
198 : file_(file), view_(view), data_(data)
199 { }
200
201 File_read& file_;
202 File_read::View* view_;
203 const unsigned char* data_;
204};
205
bae7f79e
ILT
206// All the information we hold for a single input file. This can be
207// an object file, a shared library, or an archive.
208
209class Input_file
210{
211 public:
ead1e424 212 Input_file(const Input_file_argument& input_argument)
bae7f79e
ILT
213 : input_argument_(input_argument)
214 { }
215
216 void
217 open(const General_options&, const Dirsearch&);
218
219 // Return the name given by the user.
220 const char*
221 name() const
222 { return this->input_argument_.name(); }
223
224 // Return the file name.
225 const std::string&
226 filename() const
227 { return this->file_.filename(); }
228
229 File_read&
230 file()
231 { return this->file_; }
232
233 private:
ead1e424
ILT
234 Input_file(const Input_file&);
235 Input_file& operator=(const Input_file&);
236
237 const Input_file_argument& input_argument_;
bae7f79e
ILT
238 File_read file_;
239};
240
241} // end namespace gold
242
243#endif // !defined(GOLD_FILEREAD_H)