| 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 <list> |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "options.h" |
| 13 | |
| 14 | namespace gold |
| 15 | { |
| 16 | |
| 17 | class Dirsearch; |
| 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), views_(), |
| 29 | saved_views_(), contents_(NULL), contents_size_(0) |
| 30 | { } |
| 31 | |
| 32 | ~File_read(); |
| 33 | |
| 34 | // Open a file. |
| 35 | bool |
| 36 | open(const std::string& name); |
| 37 | |
| 38 | // Pretend to open the file, but provide the file contents. No |
| 39 | // actual file system activity will occur. This is used for |
| 40 | // testing. |
| 41 | bool |
| 42 | open(const std::string& name, const unsigned char* contents, off_t size); |
| 43 | |
| 44 | // Return the file name. |
| 45 | const std::string& |
| 46 | filename() const |
| 47 | { return this->name_; } |
| 48 | |
| 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. |
| 52 | void |
| 53 | lock(); |
| 54 | |
| 55 | // Unlock the descriptor, permitting it to be closed if necessary. |
| 56 | void |
| 57 | unlock(); |
| 58 | |
| 59 | // Test whether the object is locked. |
| 60 | bool |
| 61 | is_locked(); |
| 62 | |
| 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. |
| 67 | const unsigned char* |
| 68 | get_view(off_t start, off_t size, off_t *pbytes = NULL); |
| 69 | |
| 70 | // Read data from the file into the buffer P. PBYTES is as in |
| 71 | // get_view. |
| 72 | void |
| 73 | read(off_t start, off_t size, void* p, off_t *pbytes = NULL); |
| 74 | |
| 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. |
| 79 | File_view* |
| 80 | get_lasting_view(off_t start, off_t size, off_t *pbytes = NULL); |
| 81 | |
| 82 | private: |
| 83 | // This class may not be copied. |
| 84 | File_read(const File_read&); |
| 85 | File_read& operator=(const File_read&); |
| 86 | |
| 87 | // A view into the file when not using mmap. |
| 88 | class View |
| 89 | { |
| 90 | public: |
| 91 | View(off_t start, off_t size, unsigned char* data) |
| 92 | : start_(start), size_(size), data_(data), lock_count_(0) |
| 93 | { } |
| 94 | |
| 95 | ~View(); |
| 96 | |
| 97 | off_t |
| 98 | start() const |
| 99 | { return this->start_; } |
| 100 | |
| 101 | off_t |
| 102 | size() const |
| 103 | { return this->size_; } |
| 104 | |
| 105 | unsigned char* |
| 106 | data() const |
| 107 | { return this->data_; } |
| 108 | |
| 109 | void |
| 110 | lock(); |
| 111 | |
| 112 | void |
| 113 | unlock(); |
| 114 | |
| 115 | bool |
| 116 | is_locked(); |
| 117 | |
| 118 | private: |
| 119 | View(const View&); |
| 120 | View& operator=(const View&); |
| 121 | |
| 122 | off_t start_; |
| 123 | off_t size_; |
| 124 | unsigned char* data_; |
| 125 | int lock_count_; |
| 126 | }; |
| 127 | |
| 128 | friend class File_view; |
| 129 | |
| 130 | // Find a view into the file. |
| 131 | View* |
| 132 | find_view(off_t start, off_t size); |
| 133 | |
| 134 | // Read data from the file into a buffer. |
| 135 | off_t |
| 136 | do_read(off_t start, off_t size, void* p, off_t* pbytes); |
| 137 | |
| 138 | // Find or make a view into the file. |
| 139 | View* |
| 140 | find_or_make_view(off_t start, off_t size, off_t* pbytes); |
| 141 | |
| 142 | // Clear the file views. |
| 143 | void |
| 144 | clear_views(bool); |
| 145 | |
| 146 | // The size of a file page for buffering data. |
| 147 | static const off_t page_size = 8192; |
| 148 | |
| 149 | // Given a file offset, return the page offset. |
| 150 | static off_t |
| 151 | page_offset(off_t file_offset) |
| 152 | { return file_offset & ~ (page_size - 1); } |
| 153 | |
| 154 | // Given a file size, return the size to read integral pages. |
| 155 | static off_t |
| 156 | pages(off_t file_size) |
| 157 | { return (file_size + (page_size - 1)) & ~ (page_size - 1); } |
| 158 | |
| 159 | // The type of a mapping from page start to views. |
| 160 | typedef std::map<off_t, View*> Views; |
| 161 | |
| 162 | // A simple list of Views. |
| 163 | typedef std::list<View*> Saved_views; |
| 164 | |
| 165 | // File name. |
| 166 | std::string name_; |
| 167 | // File descriptor. |
| 168 | int descriptor_; |
| 169 | // Number of locks on the file. |
| 170 | int lock_count_; |
| 171 | // Buffered views into the file. |
| 172 | Views views_; |
| 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_; |
| 180 | }; |
| 181 | |
| 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. |
| 186 | |
| 187 | class File_view |
| 188 | { |
| 189 | public: |
| 190 | // This may only be called when the underlying File_read is locked. |
| 191 | ~File_view(); |
| 192 | |
| 193 | // Return a pointer to the data associated with this view. |
| 194 | const unsigned char* |
| 195 | data() const |
| 196 | { return this->data_; } |
| 197 | |
| 198 | private: |
| 199 | File_view(const File_view&); |
| 200 | File_view& operator=(const File_view&); |
| 201 | |
| 202 | friend class File_read; |
| 203 | |
| 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) |
| 207 | { } |
| 208 | |
| 209 | File_read& file_; |
| 210 | File_read::View* view_; |
| 211 | const unsigned char* data_; |
| 212 | }; |
| 213 | |
| 214 | // All the information we hold for a single input file. This can be |
| 215 | // an object file, a shared library, or an archive. |
| 216 | |
| 217 | class Input_file |
| 218 | { |
| 219 | public: |
| 220 | Input_file(const Input_file_argument* input_argument) |
| 221 | : input_argument_(input_argument), file_() |
| 222 | { } |
| 223 | |
| 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 |
| 226 | // method. |
| 227 | Input_file(const char* name, const unsigned char* contents, off_t size); |
| 228 | |
| 229 | // Open the file. |
| 230 | void |
| 231 | open(const General_options&, const Dirsearch&); |
| 232 | |
| 233 | // Return the name given by the user. |
| 234 | const char* |
| 235 | name() const |
| 236 | { return this->input_argument_->name(); } |
| 237 | |
| 238 | // Return the file name. |
| 239 | const std::string& |
| 240 | filename() const |
| 241 | { return this->file_.filename(); } |
| 242 | |
| 243 | File_read& |
| 244 | file() |
| 245 | { return this->file_; } |
| 246 | |
| 247 | private: |
| 248 | Input_file(const Input_file&); |
| 249 | Input_file& operator=(const Input_file&); |
| 250 | |
| 251 | const Input_file_argument* input_argument_; |
| 252 | File_read file_; |
| 253 | }; |
| 254 | |
| 255 | } // end namespace gold |
| 256 | |
| 257 | #endif // !defined(GOLD_FILEREAD_H) |