Initial CVS checkin of gold
[deliverable/binutils-gdb.git] / gold / fileread.h
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
8 #include <string>
9 #include <list>
10
11 #include "options.h"
12
13 namespace gold
14 {
15
16 class Dirsearch;
17
18 class 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
24 class 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
126 View*
127 find_view(off_t start, off_t size);
128
129 off_t
130 do_read(off_t start, off_t size, void* p, off_t* pbytes);
131
132 View*
133 find_or_make_view(off_t start, off_t size, off_t* pbytes);
134
135 void
136 clear_views(bool);
137
138 std::string name_;
139 int descriptor_;
140 int lock_count_;
141 std::list<View*> view_list_;
142 };
143
144 // A view of file data that persists even when the file is unlocked.
145 // Callers should destroy these when no longer required. These are
146 // obtained form File_read::get_lasting_view. They may only be
147 // destroyed when the underlying File_read is locked.
148
149 class File_view
150 {
151 public:
152 // This may only be called when the underlying File_read is locked.
153 ~File_view();
154
155 // Return a pointer to the data associated with this view.
156 const unsigned char*
157 data() const
158 { return this->data_; }
159
160 private:
161 File_view(const File_view&);
162 File_view& operator=(const File_view&);
163
164 friend class File_read;
165
166 // Callers have to get these via File_read::get_lasting_view.
167 File_view(File_read& file, File_read::View* view, const unsigned char* data)
168 : file_(file), view_(view), data_(data)
169 { }
170
171 File_read& file_;
172 File_read::View* view_;
173 const unsigned char* data_;
174 };
175
176 // An object which locks a file using RAII.
177
178 class File_read_lock
179 {
180 public:
181 File_read_lock(File_read& file)
182 : file_(file)
183 { this->file_.lock(); }
184
185 ~File_read_lock()
186 { this->file_.unlock(); }
187
188 private:
189 File_read& file_;
190 };
191
192 // All the information we hold for a single input file. This can be
193 // an object file, a shared library, or an archive.
194
195 class Input_file
196 {
197 public:
198 Input_file(const Input_argument& input_argument)
199 : input_argument_(input_argument)
200 { }
201
202 void
203 open(const General_options&, const Dirsearch&);
204
205 // Return the name given by the user.
206 const char*
207 name() const
208 { return this->input_argument_.name(); }
209
210 // Return the file name.
211 const std::string&
212 filename() const
213 { return this->file_.filename(); }
214
215 File_read&
216 file()
217 { return this->file_; }
218
219 private:
220 const Input_argument& input_argument_;
221 File_read file_;
222 };
223
224 } // end namespace gold
225
226 #endif // !defined(GOLD_FILEREAD_H)
This page took 0.03641 seconds and 5 git commands to generate.