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)
34 open(const std::string
& name
);
36 // Return the file name.
39 { return this->name_
; }
41 // Return the file descriptor.
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.
51 // Unlock the descriptor, permitting it to be closed if necessary.
55 // Test whether the object is locked.
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.
64 get_view(off_t start
, off_t size
, off_t
*pbytes
= NULL
);
66 // Read data from the file into the buffer P. PBYTES is as in
69 read(off_t start
, off_t size
, void* p
, off_t
*pbytes
= NULL
);
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.
76 get_lasting_view(off_t start
, off_t size
, off_t
*pbytes
= NULL
);
79 // This class may not be copied.
80 File_read(const File_read
&);
81 File_read
& operator=(const File_read
&);
83 // A view into the file when not using mmap.
87 View(off_t start
, off_t size
, unsigned char* data
)
88 : start_(start
), size_(size
), data_(data
), lock_count_(0)
95 { return this->start_
; }
99 { return this->size_
; }
103 { return this->data_
; }
116 View
& operator=(const View
&);
120 unsigned char* data_
;
124 friend class File_view
;
127 find_view(off_t start
, off_t size
);
130 do_read(off_t start
, off_t size
, void* p
, off_t
* pbytes
);
133 find_or_make_view(off_t start
, off_t size
, off_t
* pbytes
);
141 std::list
<View
*> view_list_
;
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.
152 // This may only be called when the underlying File_read is locked.
155 // Return a pointer to the data associated with this view.
158 { return this->data_
; }
161 File_view(const File_view
&);
162 File_view
& operator=(const File_view
&);
164 friend class File_read
;
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
)
172 File_read::View
* view_
;
173 const unsigned char* data_
;
176 // All the information we hold for a single input file. This can be
177 // an object file, a shared library, or an archive.
182 Input_file(const Input_argument
& input_argument
)
183 : input_argument_(input_argument
)
187 open(const General_options
&, const Dirsearch
&);
189 // Return the name given by the user.
192 { return this->input_argument_
.name(); }
194 // Return the file name.
197 { return this->file_
.filename(); }
201 { return this->file_
; }
204 const Input_argument
& input_argument_
;
208 } // end namespace gold
210 #endif // !defined(GOLD_FILEREAD_H)