Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_ |
| 18 | #define ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <string> |
| 22 | #include "base/unix_file/fd_file.h" |
| 23 | |
| 24 | namespace unix_file { |
| 25 | |
| 26 | // Random access file which handles an mmap(2), munmap(2) pair in C++ |
| 27 | // RAII style. When a file is mmapped, the random access file |
| 28 | // interface accesses the mmapped memory directly; otherwise, the |
| 29 | // standard file I/O is used. Whenever a function fails, it returns |
| 30 | // false and errno is set to the corresponding error code. |
| 31 | class MappedFile : public FdFile { |
| 32 | public: |
| 33 | // File modes used in Open(). |
| 34 | enum FileMode { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 35 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 36 | kReadOnlyMode = O_RDONLY | O_LARGEFILE, |
| 37 | kReadWriteMode = O_CREAT | O_RDWR | O_LARGEFILE, |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 38 | #else |
| 39 | kReadOnlyMode = O_RDONLY, |
| 40 | kReadWriteMode = O_CREAT | O_RDWR, |
| 41 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | MappedFile() : FdFile(), file_size_(-1), mapped_file_(NULL) { |
| 45 | } |
| 46 | // Creates a MappedFile using the given file descriptor. Takes ownership of |
| 47 | // the file descriptor. |
| 48 | explicit MappedFile(int fd) : FdFile(fd), file_size_(-1), mapped_file_(NULL) { |
| 49 | } |
| 50 | |
| 51 | // Unmaps and closes the file if needed. |
| 52 | virtual ~MappedFile(); |
| 53 | |
| 54 | // Maps an opened file to memory in the read-only mode. |
| 55 | bool MapReadOnly(); |
| 56 | |
| 57 | // Maps an opened file to memory in the read-write mode. Before the |
| 58 | // file is mapped, it is truncated to 'file_size' bytes. |
| 59 | bool MapReadWrite(int64_t file_size); |
| 60 | |
| 61 | // Unmaps a mapped file so that, e.g., SetLength() may be invoked. |
| 62 | bool Unmap(); |
| 63 | |
| 64 | // RandomAccessFile API. |
| 65 | // The functions below require that the file is open, but it doesn't |
| 66 | // have to be mapped. |
| 67 | virtual int Close(); |
| 68 | virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const; |
| 69 | // SetLength() requires that the file is not mmapped. |
| 70 | virtual int SetLength(int64_t new_length); |
| 71 | virtual int64_t GetLength() const; |
| 72 | virtual int Flush(); |
| 73 | // Write() requires that, if the file is mmapped, it is mmapped in |
| 74 | // the read-write mode. Writes past the end of file are discarded. |
| 75 | virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset); |
| 76 | |
| 77 | // A convenience method equivalent to GetLength(). |
| 78 | int64_t size() const; |
| 79 | |
| 80 | // Returns true if the file has been mmapped. |
| 81 | bool IsMapped() const; |
| 82 | |
| 83 | // Returns a pointer to the start of the memory mapping once the |
| 84 | // file is successfully mapped; crashes otherwise. |
| 85 | char* data() const; |
| 86 | |
| 87 | private: |
| 88 | enum MapMode { |
| 89 | kMapReadOnly = 1, |
| 90 | kMapReadWrite = 2, |
| 91 | }; |
| 92 | |
| 93 | mutable int64_t file_size_; // May be updated in GetLength(). |
| 94 | void* mapped_file_; |
| 95 | MapMode map_mode_; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(MappedFile); |
| 98 | }; |
| 99 | |
| 100 | } // namespace unix_file |
| 101 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 102 | #endif // ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_ |